OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/search/instant_overlay.h" | |
6 | |
7 #include "base/auto_reset.h" | |
8 #include "base/supports_user_data.h" | |
9 #include "chrome/browser/search/search.h" | |
10 #include "chrome/common/url_constants.h" | |
11 #include "content/public/browser/web_contents.h" | |
12 | |
13 namespace { | |
14 | |
15 int kUserDataKey; | |
16 | |
17 class InstantOverlayUserData : public base::SupportsUserData::Data { | |
18 public: | |
19 explicit InstantOverlayUserData(InstantOverlay* overlay) | |
20 : overlay_(overlay) {} | |
21 | |
22 virtual InstantOverlay* overlay() const { return overlay_; } | |
23 | |
24 private: | |
25 virtual ~InstantOverlayUserData() {} | |
26 | |
27 InstantOverlay* const overlay_; | |
28 | |
29 DISALLOW_COPY_AND_ASSIGN(InstantOverlayUserData); | |
30 }; | |
31 | |
32 } // namespace | |
33 | |
34 // static | |
35 InstantOverlay* InstantOverlay::FromWebContents( | |
36 const content::WebContents* web_contents) { | |
37 InstantOverlayUserData* data = static_cast<InstantOverlayUserData*>( | |
38 web_contents->GetUserData(&kUserDataKey)); | |
39 return data ? data->overlay() : NULL; | |
40 } | |
41 | |
42 InstantOverlay::InstantOverlay(InstantController* controller, | |
43 const std::string& instant_url, | |
44 bool is_incognito) | |
45 : InstantPage(controller, instant_url, is_incognito), | |
46 loader_(this), | |
47 is_stale_(false), | |
48 is_pointer_down_from_activate_(false) { | |
49 } | |
50 | |
51 InstantOverlay::~InstantOverlay() { | |
52 } | |
53 | |
54 void InstantOverlay::InitContents(Profile* profile, | |
55 const content::WebContents* active_tab) { | |
56 loader_.Init(GURL(instant_url()), profile, active_tab, | |
57 base::Bind(&InstantOverlay::HandleStalePage, | |
58 base::Unretained(this))); | |
59 SetContents(loader_.contents()); | |
60 contents()->SetUserData(&kUserDataKey, new InstantOverlayUserData(this)); | |
61 loader_.Load(); | |
62 } | |
63 | |
64 scoped_ptr<content::WebContents> InstantOverlay::ReleaseContents() { | |
65 contents()->RemoveUserData(&kUserDataKey); | |
66 SetContents(NULL); | |
67 return loader_.ReleaseContents(); | |
68 } | |
69 | |
70 void InstantOverlay::DidNavigate( | |
71 const history::HistoryAddPageArgs& add_page_args) { | |
72 last_navigation_ = add_page_args; | |
73 } | |
74 | |
75 void InstantOverlay::Update(const string16& text, | |
76 size_t selection_start, | |
77 size_t selection_end, | |
78 bool verbatim) { | |
79 last_navigation_ = history::HistoryAddPageArgs(); | |
80 sender()->Update(text, selection_start, selection_end, verbatim); | |
81 } | |
82 | |
83 void InstantOverlay::OnSwappedContents() { | |
84 contents()->RemoveUserData(&kUserDataKey); | |
85 SetContents(loader_.contents()); | |
86 contents()->SetUserData(&kUserDataKey, new InstantOverlayUserData(this)); | |
87 instant_controller()->SwappedOverlayContents(); | |
88 } | |
89 | |
90 void InstantOverlay::OnFocus() { | |
91 // The overlay is getting focus. Equivalent to it being clicked. | |
92 base::AutoReset<bool> reset(&is_pointer_down_from_activate_, true); | |
93 instant_controller()->FocusedOverlayContents(); | |
94 } | |
95 | |
96 void InstantOverlay::OnMouseDown() { | |
97 is_pointer_down_from_activate_ = true; | |
98 } | |
99 | |
100 void InstantOverlay::OnMouseUp() { | |
101 if (is_pointer_down_from_activate_) { | |
102 is_pointer_down_from_activate_ = false; | |
103 instant_controller()->CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); | |
104 } | |
105 } | |
106 | |
107 content::WebContents* InstantOverlay::OpenURLFromTab( | |
108 content::WebContents* source, | |
109 const content::OpenURLParams& params) { | |
110 if (!supports_instant()) { | |
111 // If the page doesn't yet support Instant, it hasn't fully loaded. | |
112 // This is a redirect that we should allow. http://crbug.com/177948 | |
113 content::NavigationController::LoadURLParams load_params(params.url); | |
114 load_params.transition_type = params.transition; | |
115 load_params.referrer = params.referrer; | |
116 load_params.extra_headers = params.extra_headers; | |
117 load_params.is_renderer_initiated = params.is_renderer_initiated; | |
118 load_params.transferred_global_request_id = | |
119 params.transferred_global_request_id; | |
120 load_params.is_cross_site_redirect = params.is_cross_site_redirect; | |
121 | |
122 contents()->GetController().LoadURLWithParams(load_params); | |
123 return contents(); | |
124 } | |
125 | |
126 // We will allow the navigate to continue if we are able to commit the | |
127 // overlay. | |
128 // | |
129 // First, cache the overlay contents since committing it will cause the | |
130 // contents to be released (and be set to NULL). | |
131 content::WebContents* overlay = contents(); | |
132 if (instant_controller()->CommitIfPossible(INSTANT_COMMIT_NAVIGATED)) { | |
133 // If the commit was successful, the overlay's delegate should be the tab | |
134 // strip, which will be able to handle the navigation. | |
135 DCHECK_NE(&loader_, overlay->GetDelegate()); | |
136 return overlay->GetDelegate()->OpenURLFromTab(source, params); | |
137 } | |
138 return NULL; | |
139 } | |
140 | |
141 void InstantOverlay::LoadCompletedMainFrame() { | |
142 instant_controller()->OverlayLoadCompletedMainFrame(); | |
143 } | |
144 | |
145 void InstantOverlay::HandleStalePage() { | |
146 is_stale_ = true; | |
147 instant_controller()->ReloadOverlayIfStale(); | |
148 } | |
OLD | NEW |