Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: chrome/browser/instant/instant_loader.cc

Issue 11421079: Persist the Instant API to committed search result pages. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Kittens live! Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/instant/instant_loader.h" 5 #include "chrome/browser/instant/instant_loader.h"
6 6
7 #include "chrome/browser/content_settings/tab_specific_content_settings.h" 7 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
8 #include "chrome/browser/instant/instant_controller.h" 8 #include "chrome/browser/instant/instant_controller.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/thumbnails/thumbnail_tab_helper.h" 9 #include "chrome/browser/thumbnails/thumbnail_tab_helper.h"
11 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" 10 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h"
12 #include "chrome/browser/ui/constrained_window_tab_helper.h" 11 #include "chrome/browser/ui/constrained_window_tab_helper.h"
13 #include "chrome/browser/ui/constrained_window_tab_helper_delegate.h" 12 #include "chrome/browser/ui/constrained_window_tab_helper_delegate.h"
14 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" 13 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
15 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h" 14 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h"
16 #include "chrome/browser/ui/tab_contents/tab_contents.h" 15 #include "chrome/browser/ui/tab_contents/tab_contents.h"
17 #include "chrome/common/render_messages.h"
18 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/notification_source.h" 16 #include "content/public/browser/notification_source.h"
20 #include "content/public/browser/notification_types.h" 17 #include "content/public/browser/notification_types.h"
21 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/render_widget_host_view.h" 18 #include "content/public/browser/render_widget_host_view.h"
23 #include "content/public/browser/web_contents_delegate.h" 19 #include "content/public/browser/web_contents_delegate.h"
20 #include "ipc/ipc_message.h"
24 21
25 namespace { 22 namespace {
26 23
27 int kUserDataKey; 24 int kUserDataKey;
28 25
29 class InstantLoaderUserData : public base::SupportsUserData::Data { 26 class InstantLoaderUserData : public base::SupportsUserData::Data {
30 public: 27 public:
31 explicit InstantLoaderUserData(InstantLoader* loader) : loader_(loader) {} 28 explicit InstantLoaderUserData(InstantLoader* loader) : loader_(loader) {}
32 29
33 InstantLoader* loader() const { return loader_; } 30 InstantLoader* loader() const { return loader_; }
34 31
35 private: 32 private:
36 ~InstantLoaderUserData() {} 33 ~InstantLoaderUserData() {}
37 34
38 InstantLoader* const loader_; 35 InstantLoader* const loader_;
39 36
40 DISALLOW_COPY_AND_ASSIGN(InstantLoaderUserData); 37 DISALLOW_COPY_AND_ASSIGN(InstantLoaderUserData);
41 }; 38 };
42 39
43 } 40 }
44 41
45 // WebContentsDelegateImpl ----------------------------------------------------- 42 // WebContentsDelegateImpl -----------------------------------------------------
46 43
47 class InstantLoader::WebContentsDelegateImpl 44 class InstantLoader::WebContentsDelegateImpl
48 : public ConstrainedWindowTabHelperDelegate, 45 : public ConstrainedWindowTabHelperDelegate,
49 public CoreTabHelperDelegate, 46 public CoreTabHelperDelegate,
50 public content::WebContentsDelegate, 47 public content::WebContentsDelegate {
51 public content::WebContentsObserver {
52 public: 48 public:
53 explicit WebContentsDelegateImpl(InstantLoader* loader); 49 explicit WebContentsDelegateImpl(InstantLoader* loader);
54 50
55 bool is_pointer_down_from_activate() const {
56 return is_pointer_down_from_activate_;
57 }
58
59 // Start observing |web_contents| instead of whatever is currently being
60 // observed. If |web_contents| is NULL, effectively stops observing.
61 void ObserveContents(content::WebContents* web_contents);
62
63 private: 51 private:
64 // Overridden from ConstrainedWindowTabHelperDelegate: 52 // Overridden from ConstrainedWindowTabHelperDelegate:
65 virtual bool ShouldFocusConstrainedWindow() OVERRIDE; 53 virtual bool ShouldFocusConstrainedWindow() OVERRIDE;
66 54
67 // Overridden from CoreTabHelperDelegate: 55 // Overridden from CoreTabHelperDelegate:
68 virtual void SwapTabContents(content::WebContents* old_contents, 56 virtual void SwapTabContents(content::WebContents* old_contents,
69 content::WebContents* new_contents) OVERRIDE; 57 content::WebContents* new_contents) OVERRIDE;
70 58
71 // Overridden from content::WebContentsDelegate: 59 // Overridden from content::WebContentsDelegate:
72 virtual bool ShouldSuppressDialogs() OVERRIDE; 60 virtual bool ShouldSuppressDialogs() OVERRIDE;
73 virtual bool ShouldFocusPageAfterCrash() OVERRIDE; 61 virtual bool ShouldFocusPageAfterCrash() OVERRIDE;
74 virtual void LostCapture() OVERRIDE; 62 virtual void LostCapture() OVERRIDE;
75 virtual void WebContentsFocused(content::WebContents* contents) OVERRIDE; 63 virtual void WebContentsFocused(content::WebContents* contents) OVERRIDE;
76 virtual bool CanDownload(content::RenderViewHost* render_view_host, 64 virtual bool CanDownload(content::RenderViewHost* render_view_host,
77 int request_id, 65 int request_id,
78 const std::string& request_method) OVERRIDE; 66 const std::string& request_method) OVERRIDE;
79 virtual void HandleMouseDown() OVERRIDE; 67 virtual void HandleMouseDown() OVERRIDE;
80 virtual void HandleMouseUp() OVERRIDE; 68 virtual void HandleMouseUp() OVERRIDE;
81 virtual void HandlePointerActivate() OVERRIDE; 69 virtual void HandlePointerActivate() OVERRIDE;
82 virtual void HandleGestureEnd() OVERRIDE; 70 virtual void HandleGestureEnd() OVERRIDE;
83 virtual void DragEnded() OVERRIDE; 71 virtual void DragEnded() OVERRIDE;
84 virtual bool OnGoToEntryOffset(int offset) OVERRIDE; 72 virtual bool OnGoToEntryOffset(int offset) OVERRIDE;
85 73
86 // Overridden from content::WebContentsObserver: 74 void MaybeCommitFromPointerRelease();
87 virtual void DidFinishLoad(
88 int64 frame_id,
89 const GURL& validated_url,
90 bool is_main_frame,
91 content::RenderViewHost* render_view_host) OVERRIDE;
92 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
93
94 // Message from renderer indicating the page has suggestions.
95 void OnSetSuggestions(int page_id,
96 const std::vector<InstantSuggestion>& suggestions);
97
98 // Message from the renderer determining whether it supports the Instant API.
99 void OnInstantSupportDetermined(int page_id, bool result);
100
101 // Message from the renderer requesting the preview be shown.
102 void OnShowInstantPreview(int page_id,
103 InstantShownReason reason,
104 int height,
105 InstantSizeUnits units);
106
107 void CommitFromPointerReleaseIfNecessary();
108 void MaybeSetAndNotifyInstantSupportDetermined(bool supports_instant);
109 75
110 InstantLoader* const loader_; 76 InstantLoader* const loader_;
111 77
112 // True if the mouse or a touch pointer is down from an activate.
113 bool is_pointer_down_from_activate_;
114
115 DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl); 78 DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl);
116 }; 79 };
117 80
118 InstantLoader::WebContentsDelegateImpl::WebContentsDelegateImpl( 81 InstantLoader::WebContentsDelegateImpl::WebContentsDelegateImpl(
119 InstantLoader* loader) 82 InstantLoader* loader)
120 : loader_(loader), 83 : loader_(loader) {
121 is_pointer_down_from_activate_(false) {
122 }
123
124 void InstantLoader::WebContentsDelegateImpl::ObserveContents(
125 content::WebContents* web_contents) {
126 Observe(web_contents);
127 } 84 }
128 85
129 bool InstantLoader::WebContentsDelegateImpl::ShouldFocusConstrainedWindow() { 86 bool InstantLoader::WebContentsDelegateImpl::ShouldFocusConstrainedWindow() {
130 // Return false so that constrained windows are not initially focused. If we 87 // Return false so that constrained windows are not initially focused. If we
131 // did otherwise the preview would prematurely get committed when focus goes 88 // did otherwise the preview would prematurely get committed when focus goes
132 // to the constrained window. 89 // to the constrained window.
133 return false; 90 return false;
134 } 91 }
135 92
136 void InstantLoader::WebContentsDelegateImpl::SwapTabContents( 93 void InstantLoader::WebContentsDelegateImpl::SwapTabContents(
137 content::WebContents* old_contents, 94 content::WebContents* old_contents,
138 content::WebContents* new_contents) { 95 content::WebContents* new_contents) {
139 // If this is being called, something is swapping in to our 96 // If this is being called, something is swapping in to loader's |contents_|
140 // |preview_contents_| before we've added it to the tab strip. 97 // before we've added it to the tab strip.
141 loader_->ReplacePreviewContents(old_contents, new_contents); 98 loader_->ReplacePreviewContents(old_contents, new_contents);
142 } 99 }
143 100
144 bool InstantLoader::WebContentsDelegateImpl::ShouldSuppressDialogs() { 101 bool InstantLoader::WebContentsDelegateImpl::ShouldSuppressDialogs() {
145 // Any message shown during Instant cancels Instant, so we suppress them. 102 // Any message shown during Instant cancels Instant, so we suppress them.
146 return true; 103 return true;
147 } 104 }
148 105
149 bool InstantLoader::WebContentsDelegateImpl::ShouldFocusPageAfterCrash() { 106 bool InstantLoader::WebContentsDelegateImpl::ShouldFocusPageAfterCrash() {
150 return false; 107 return false;
151 } 108 }
152 109
153 void InstantLoader::WebContentsDelegateImpl::LostCapture() { 110 void InstantLoader::WebContentsDelegateImpl::LostCapture() {
154 CommitFromPointerReleaseIfNecessary(); 111 MaybeCommitFromPointerRelease();
155 } 112 }
156 113
157 void InstantLoader::WebContentsDelegateImpl::WebContentsFocused( 114 void InstantLoader::WebContentsDelegateImpl::WebContentsFocused(
158 content::WebContents* contents) { 115 content::WebContents* /* contents */) {
159 loader_->controller_->InstantLoaderContentsFocused(loader_); 116 loader_->controller_->InstantLoaderContentsFocused();
160 } 117 }
161 118
162 bool InstantLoader::WebContentsDelegateImpl::CanDownload( 119 bool InstantLoader::WebContentsDelegateImpl::CanDownload(
163 content::RenderViewHost* render_view_host, 120 content::RenderViewHost* /* render_view_host */,
164 int request_id, 121 int /* request_id */,
165 const std::string& request_method) { 122 const std::string& /* request_method */) {
166 // Downloads are disabled. 123 // Downloads are disabled.
167 return false; 124 return false;
168 } 125 }
169 126
170 void InstantLoader::WebContentsDelegateImpl::HandleMouseDown() { 127 void InstantLoader::WebContentsDelegateImpl::HandleMouseDown() {
171 is_pointer_down_from_activate_ = true; 128 loader_->is_pointer_down_from_activate_ = true;
172 } 129 }
173 130
174 void InstantLoader::WebContentsDelegateImpl::HandleMouseUp() { 131 void InstantLoader::WebContentsDelegateImpl::HandleMouseUp() {
175 CommitFromPointerReleaseIfNecessary(); 132 MaybeCommitFromPointerRelease();
176 } 133 }
177 134
178 void InstantLoader::WebContentsDelegateImpl::HandlePointerActivate() { 135 void InstantLoader::WebContentsDelegateImpl::HandlePointerActivate() {
179 is_pointer_down_from_activate_ = true; 136 loader_->is_pointer_down_from_activate_ = true;
180 } 137 }
181 138
182 void InstantLoader::WebContentsDelegateImpl::HandleGestureEnd() { 139 void InstantLoader::WebContentsDelegateImpl::HandleGestureEnd() {
183 CommitFromPointerReleaseIfNecessary(); 140 MaybeCommitFromPointerRelease();
184 } 141 }
185 142
186 void InstantLoader::WebContentsDelegateImpl::DragEnded() { 143 void InstantLoader::WebContentsDelegateImpl::DragEnded() {
187 // If the user drags, we won't get a mouse up (at least on Linux). Commit the 144 // If the user drags, we won't get a mouse up (at least on Linux). Commit the
188 // Instant result when the drag ends, so that during the drag the page won't 145 // Instant result when the drag ends, so that during the drag the page won't
189 // move around. 146 // move around.
190 CommitFromPointerReleaseIfNecessary(); 147 MaybeCommitFromPointerRelease();
191 } 148 }
192 149
193 bool InstantLoader::WebContentsDelegateImpl::OnGoToEntryOffset(int offset) { 150 bool InstantLoader::WebContentsDelegateImpl::OnGoToEntryOffset(int offset) {
194 return false; 151 return false;
195 } 152 }
196 153
197 void InstantLoader::WebContentsDelegateImpl::DidFinishLoad( 154 void InstantLoader::WebContentsDelegateImpl::MaybeCommitFromPointerRelease() {
198 int64 frame_id, 155 if (loader_->is_pointer_down_from_activate_) {
199 const GURL& validated_url, 156 loader_->is_pointer_down_from_activate_ = false;
200 bool is_main_frame, 157 loader_->controller_->CommitIfCurrent(INSTANT_COMMIT_FOCUS_LOST);
201 content::RenderViewHost* render_view_host) {
202 if (is_main_frame && !loader_->supports_instant_)
203 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id()));
204 }
205
206 bool InstantLoader::WebContentsDelegateImpl::OnMessageReceived(
207 const IPC::Message& message) {
208 bool handled = true;
209 IPC_BEGIN_MESSAGE_MAP(WebContentsDelegateImpl, message)
210 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions)
211 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined,
212 OnInstantSupportDetermined)
213 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantPreview,
214 OnShowInstantPreview);
215 IPC_MESSAGE_UNHANDLED(handled = false)
216 IPC_END_MESSAGE_MAP()
217 return handled;
218 }
219
220 void InstantLoader::WebContentsDelegateImpl::OnSetSuggestions(
221 int page_id,
222 const std::vector<InstantSuggestion>& suggestions) {
223 DCHECK(loader_->preview_contents());
224 DCHECK(loader_->preview_contents_->web_contents());
225 content::NavigationEntry* entry = loader_->preview_contents_->web_contents()->
226 GetController().GetActiveEntry();
227 if (entry && page_id == entry->GetPageID()) {
228 MaybeSetAndNotifyInstantSupportDetermined(true);
229 loader_->controller_->SetSuggestions(loader_, suggestions);
230 } 158 }
231 } 159 }
232 160
233 void InstantLoader::WebContentsDelegateImpl::OnInstantSupportDetermined(
234 int page_id,
235 bool result) {
236 DCHECK(loader_->preview_contents());
237 DCHECK(loader_->preview_contents_->web_contents());
238 content::NavigationEntry* entry = loader_->preview_contents_->web_contents()->
239 GetController().GetActiveEntry();
240 if (entry && page_id == entry->GetPageID())
241 MaybeSetAndNotifyInstantSupportDetermined(result);
242 }
243
244 void InstantLoader::WebContentsDelegateImpl::OnShowInstantPreview(
245 int page_id,
246 InstantShownReason reason,
247 int height,
248 InstantSizeUnits units) {
249 DCHECK(loader_->preview_contents());
250 DCHECK(loader_->preview_contents_->web_contents());
251 content::NavigationEntry* entry = loader_->preview_contents_->web_contents()->
252 GetController().GetActiveEntry();
253 if (entry && page_id == entry->GetPageID()) {
254 MaybeSetAndNotifyInstantSupportDetermined(true);
255 loader_->controller_->ShowInstantPreview(loader_, reason, height, units);
256 }
257 }
258
259 void InstantLoader::WebContentsDelegateImpl
260 ::CommitFromPointerReleaseIfNecessary() {
261 if (is_pointer_down_from_activate_) {
262 is_pointer_down_from_activate_ = false;
263 loader_->controller_->CommitInstantLoader(loader_);
264 }
265 }
266
267 void InstantLoader::WebContentsDelegateImpl
268 ::MaybeSetAndNotifyInstantSupportDetermined(bool supports_instant) {
269 // If we already determined that the loader supports Instant, nothing to do.
270 if (!loader_->supports_instant_) {
271 loader_->supports_instant_ = supports_instant;
272 loader_->controller_->InstantSupportDetermined(loader_, supports_instant);
273 }
274 }
275
276 // InstantLoader --------------------------------------------------------------- 161 // InstantLoader ---------------------------------------------------------------
277 162
278 // static 163 // static
279 InstantLoader* InstantLoader::FromWebContents( 164 InstantLoader* InstantLoader::FromWebContents(
280 content::WebContents* web_contents) { 165 const content::WebContents* web_contents) {
281 InstantLoaderUserData* data = static_cast<InstantLoaderUserData*>( 166 InstantLoaderUserData* data = static_cast<InstantLoaderUserData*>(
282 web_contents->GetUserData(&kUserDataKey)); 167 web_contents->GetUserData(&kUserDataKey));
283 return data ? data->loader() : NULL; 168 return data ? data->loader() : NULL;
284 } 169 }
285 170
286 InstantLoader::InstantLoader(InstantController* controller, 171 InstantLoader::InstantLoader(InstantController* controller,
287 const std::string& instant_url, 172 const std::string& instant_url)
288 const TabContents* tab_contents) 173 : client_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
289 : controller_(controller), 174 controller_(controller),
290 preview_delegate_(new WebContentsDelegateImpl( 175 delegate_(new WebContentsDelegateImpl(
291 ALLOW_THIS_IN_INITIALIZER_LIST(this))), 176 ALLOW_THIS_IN_INITIALIZER_LIST(this))),
292 preview_contents_( 177 instant_url_(instant_url),
293 TabContents::Factory::CreateTabContents(
294 content::WebContents::CreateWithSessionStorage(
295 tab_contents->profile(), NULL, MSG_ROUTING_NONE,
296 tab_contents->web_contents(),
297 tab_contents->web_contents()->GetController().
298 GetSessionStorageNamespaceMap()))),
299 supports_instant_(false), 178 supports_instant_(false),
300 instant_url_(instant_url) { 179 is_pointer_down_from_activate_(false) {
301 } 180 }
302 181
303 InstantLoader::~InstantLoader() { 182 InstantLoader::~InstantLoader() {
304 } 183 }
305 184
306 void InstantLoader::Init() { 185 void InstantLoader::InitContents(const content::WebContents* active_tab) {
186 contents_.reset(content::WebContents::CreateWithSessionStorage(
187 active_tab->GetBrowserContext(), NULL, MSG_ROUTING_NONE, active_tab,
188 active_tab->GetController().GetSessionStorageNamespaceMap()));
189 // Not a leak. TabContents will delete itself when the WebContents is gone.
190 TabContents::Factory::CreateTabContents(contents());
307 SetupPreviewContents(); 191 SetupPreviewContents();
308 // This HTTP header and value are set on loads that originate from instant. 192
193 // This HTTP header and value are set on loads that originate from Instant.
309 const char kInstantHeader[] = "X-Purpose: Instant"; 194 const char kInstantHeader[] = "X-Purpose: Instant";
310 DVLOG(1) << "LoadURL: " << instant_url_; 195 DVLOG(1) << "LoadURL: " << instant_url_;
311 preview_contents_->web_contents()->GetController().LoadURL(GURL(instant_url_), 196 contents_->GetController().LoadURL(GURL(instant_url_), content::Referrer(),
312 content::Referrer(), content::PAGE_TRANSITION_GENERATED, kInstantHeader); 197 content::PAGE_TRANSITION_GENERATED, kInstantHeader);
313 preview_contents_->web_contents()->WasHidden(); 198 contents_->WasHidden();
314 } 199 }
315 200
316 void InstantLoader::Update(const string16& user_text, bool verbatim) { 201 content::WebContents* InstantLoader::ReleaseContents() {
317 // TODO: Support real cursor position. 202 CleanupPreviewContents();
318 last_navigation_ = history::HistoryAddPageArgs(); 203 return contents_.release();
319 content::RenderViewHost* rvh =
320 preview_contents_->web_contents()->GetRenderViewHost();
321 rvh->Send(new ChromeViewMsg_SearchBoxChange(rvh->GetRoutingID(), user_text,
322 verbatim, user_text.size(), user_text.size()));
323 }
324
325 void InstantLoader::SetOmniboxBounds(const gfx::Rect& bounds) {
326 content::RenderViewHost* rvh =
327 preview_contents_->web_contents()->GetRenderViewHost();
328 rvh->Send(new ChromeViewMsg_SearchBoxResize(rvh->GetRoutingID(), bounds));
329 }
330
331 void InstantLoader::SendAutocompleteResults(
332 const std::vector<InstantAutocompleteResult>& results) {
333 content::RenderViewHost* rvh =
334 preview_contents_->web_contents()->GetRenderViewHost();
335 rvh->Send(new ChromeViewMsg_SearchBoxAutocompleteResults(rvh->GetRoutingID(),
336 results));
337 }
338
339 void InstantLoader::SendThemeBackgroundInfo(
340 const ThemeBackgroundInfo& theme_info) {
341 content::RenderViewHost* rvh =
342 preview_contents_->web_contents()->GetRenderViewHost();
343 rvh->Send(new ChromeViewMsg_SearchBoxThemeChanged(rvh->GetRoutingID(),
344 theme_info));
345 }
346
347 void InstantLoader::SendThemeAreaHeight(int height) {
348 content::RenderViewHost* rvh =
349 preview_contents_->web_contents()->GetRenderViewHost();
350 rvh->Send(new ChromeViewMsg_SearchBoxThemeAreaHeightChanged(
351 rvh->GetRoutingID(), height));
352 }
353
354 void InstantLoader::OnUpOrDownKeyPressed(int count) {
355 content::RenderViewHost* rvh =
356 preview_contents_->web_contents()->GetRenderViewHost();
357 rvh->Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(rvh->GetRoutingID(),
358 count));
359 }
360
361 void InstantLoader::SearchModeChanged(const chrome::search::Mode& mode) {
362 content::RenderViewHost* rvh =
363 preview_contents_->web_contents()->GetRenderViewHost();
364 rvh->Send(new ChromeViewMsg_SearchBoxModeChanged(rvh->GetRoutingID(), mode));
365 } 204 }
366 205
367 void InstantLoader::DidNavigate( 206 void InstantLoader::DidNavigate(
368 const history::HistoryAddPageArgs& add_page_args) { 207 const history::HistoryAddPageArgs& add_page_args) {
369 last_navigation_ = add_page_args; 208 last_navigation_ = add_page_args;
370 } 209 }
371 210
372 TabContents* InstantLoader::ReleasePreviewContents(InstantCommitType type, 211 void InstantLoader::Update(const string16& text, bool verbatim) {
373 const string16& text) { 212 last_navigation_ = history::HistoryAddPageArgs();
374 content::RenderViewHost* rvh = 213 client_.Update(text, verbatim);
375 preview_contents_->web_contents()->GetRenderViewHost();
376 if (type == INSTANT_COMMIT_FOCUS_LOST)
377 rvh->Send(new ChromeViewMsg_SearchBoxCancel(rvh->GetRoutingID(), text));
378 else
379 rvh->Send(new ChromeViewMsg_SearchBoxSubmit(rvh->GetRoutingID(), text));
380 CleanupPreviewContents();
381 return preview_contents_.release();
382 } 214 }
383 215
384 void InstantLoader::CleanupPreviewContents() { 216 void InstantLoader::Submit(const string16& text) {
385 content::WebContents* old_contents = preview_contents_->web_contents(); 217 client_.Submit(text);
386 old_contents->RemoveUserData(&kUserDataKey);
387 old_contents->SetDelegate(NULL);
388 preview_delegate_->ObserveContents(NULL);
389
390 BlockedContentTabHelper::FromWebContents(old_contents)->
391 SetAllContentsBlocked(false);
392 ConstrainedWindowTabHelper::FromWebContents(old_contents)->set_delegate(NULL);
393 TabSpecificContentSettings::FromWebContents(old_contents)->
394 SetPopupsBlocked(false);
395 CoreTabHelper::FromWebContents(old_contents)->set_delegate(NULL);
396 if (ThumbnailTabHelper* thumbnail_tab_helper =
397 ThumbnailTabHelper::FromWebContents(old_contents))
398 thumbnail_tab_helper->set_enabled(true);
399
400 #if defined(OS_MACOSX)
401 if (content::RenderWidgetHostView* rwhv =
402 old_contents->GetRenderWidgetHostView())
403 rwhv->SetTakesFocusOnlyOnMouseDown(false);
404 registrar_.Remove(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
405 content::Source<content::NavigationController>(
406 &old_contents->GetController()));
407 #endif
408 } 218 }
409 219
410 bool InstantLoader::IsPointerDownFromActivate() const { 220 void InstantLoader::Cancel(const string16& text) {
411 return preview_delegate_->is_pointer_down_from_activate(); 221 client_.Cancel(text);
222 }
223
224 void InstantLoader::SetOmniboxBounds(const gfx::Rect& bounds) {
225 client_.SetOmniboxBounds(bounds);
226 }
227
228 void InstantLoader::SendAutocompleteResults(
229 const std::vector<InstantAutocompleteResult>& results) {
230 client_.SendAutocompleteResults(results);
231 }
232
233 void InstantLoader::UpOrDownKeyPressed(int count) {
234 client_.UpOrDownKeyPressed(count);
235 }
236
237 void InstantLoader::SearchModeChanged(const chrome::search::Mode& mode) {
238 client_.SearchModeChanged(mode);
239 }
240
241 void InstantLoader::SendThemeBackgroundInfo(
242 const ThemeBackgroundInfo& theme_info) {
243 client_.SendThemeBackgroundInfo(theme_info);
244 }
245
246 void InstantLoader::SendThemeAreaHeight(int height) {
247 client_.SendThemeAreaHeight(height);
248 }
249
250 void InstantLoader::SetSuggestions(
251 const std::vector<InstantSuggestion>& suggestions) {
252 InstantSupportDetermined(true);
253 controller_->SetSuggestions(contents(), suggestions);
254 }
255
256 void InstantLoader::InstantSupportDetermined(bool supports_instant) {
257 // If we had already determined that the page supports Instant, nothing to do.
258 if (supports_instant_)
259 return;
260
261 supports_instant_ = supports_instant;
262 controller_->InstantSupportDetermined(contents(), supports_instant);
263 }
264
265 void InstantLoader::ShowInstantPreview(InstantShownReason reason,
266 int height,
267 InstantSizeUnits units) {
268 InstantSupportDetermined(true);
269 controller_->ShowInstantPreview(reason, height, units);
412 } 270 }
413 271
414 void InstantLoader::Observe(int type, 272 void InstantLoader::Observe(int type,
415 const content::NotificationSource& source, 273 const content::NotificationSource& source,
416 const content::NotificationDetails& details) { 274 const content::NotificationDetails& details) {
417 #if defined(OS_MACOSX) 275 #if defined(OS_MACOSX)
418 if (type == content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED) { 276 if (type == content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED) {
419 if (content::RenderWidgetHostView* rwhv = 277 if (content::RenderWidgetHostView* rwhv =
420 preview_contents_->web_contents()->GetRenderWidgetHostView()) 278 contents_->GetRenderWidgetHostView())
421 rwhv->SetTakesFocusOnlyOnMouseDown(true); 279 rwhv->SetTakesFocusOnlyOnMouseDown(true);
422 return; 280 return;
423 } 281 }
424 NOTREACHED(); 282 NOTREACHED();
425 #endif 283 #endif
426 } 284 }
427 285
428 void InstantLoader::SetupPreviewContents() { 286 void InstantLoader::SetupPreviewContents() {
429 content::WebContents* new_contents = preview_contents_->web_contents(); 287 client_.SetContents(contents());
430 new_contents->SetUserData(&kUserDataKey, new InstantLoaderUserData(this)); 288 contents_->SetUserData(&kUserDataKey, new InstantLoaderUserData(this));
431 WebContentsDelegateImpl* new_delegate = preview_delegate_.get(); 289 contents_->SetDelegate(delegate_.get());
432 new_contents->SetDelegate(new_delegate);
433 new_delegate->ObserveContents(new_contents);
434 290
435 // Disable popups and such (mainly to avoid losing focus and reverting the 291 // Disable popups and such (mainly to avoid losing focus and reverting the
436 // preview prematurely). 292 // preview prematurely).
437 BlockedContentTabHelper::FromWebContents(new_contents)-> 293 if (BlockedContentTabHelper* blocked_content_tab_helper =
438 SetAllContentsBlocked(true); 294 BlockedContentTabHelper::FromWebContents(contents()))
439 ConstrainedWindowTabHelper::FromWebContents(new_contents)-> 295 blocked_content_tab_helper->SetAllContentsBlocked(true);
440 set_delegate(new_delegate); 296 if (ConstrainedWindowTabHelper* constrained_window_tab_helper =
441 TabSpecificContentSettings::FromWebContents(new_contents)-> 297 ConstrainedWindowTabHelper::FromWebContents(contents()))
442 SetPopupsBlocked(true); 298 constrained_window_tab_helper->set_delegate(delegate_.get());
443 CoreTabHelper::FromWebContents(new_contents)->set_delegate(new_delegate); 299 if (TabSpecificContentSettings* tab_specific_content_settings =
300 TabSpecificContentSettings::FromWebContents(contents()))
301 tab_specific_content_settings->SetPopupsBlocked(true);
302 if (CoreTabHelper* core_tab_helper =
303 CoreTabHelper::FromWebContents(contents()))
304 core_tab_helper->set_delegate(delegate_.get());
444 if (ThumbnailTabHelper* thumbnail_tab_helper = 305 if (ThumbnailTabHelper* thumbnail_tab_helper =
445 ThumbnailTabHelper::FromWebContents(new_contents)) 306 ThumbnailTabHelper::FromWebContents(contents()))
446 thumbnail_tab_helper->set_enabled(false); 307 thumbnail_tab_helper->set_enabled(false);
447 308
448 #if defined(OS_MACOSX) 309 #if defined(OS_MACOSX)
449 // If |preview_contents_| does not currently have a RWHV, we will call 310 // If |contents_| doesn't yet have a RWHV, SetTakesFocusOnlyOnMouseDown() will
450 // SetTakesFocusOnlyOnMouseDown() as a result of the RENDER_VIEW_HOST_CHANGED 311 // be called later, when NOTIFICATION_RENDER_VIEW_HOST_CHANGED is received.
451 // notification.
452 if (content::RenderWidgetHostView* rwhv = 312 if (content::RenderWidgetHostView* rwhv =
453 new_contents->GetRenderWidgetHostView()) 313 contents_->GetRenderWidgetHostView())
454 rwhv->SetTakesFocusOnlyOnMouseDown(true); 314 rwhv->SetTakesFocusOnlyOnMouseDown(true);
455 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, 315 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
456 content::Source<content::NavigationController>( 316 content::Source<content::NavigationController>(
457 &new_contents->GetController())); 317 &contents_->GetController()));
318 #endif
319 }
320
321 void InstantLoader::CleanupPreviewContents() {
322 client_.SetContents(NULL);
323 contents_->RemoveUserData(&kUserDataKey);
324 contents_->SetDelegate(NULL);
325
326 if (BlockedContentTabHelper* blocked_content_tab_helper =
327 BlockedContentTabHelper::FromWebContents(contents()))
328 blocked_content_tab_helper->SetAllContentsBlocked(false);
329 if (ConstrainedWindowTabHelper* constrained_window_tab_helper =
330 ConstrainedWindowTabHelper::FromWebContents(contents()))
331 constrained_window_tab_helper->set_delegate(NULL);
332 if (TabSpecificContentSettings* tab_specific_content_settings =
333 TabSpecificContentSettings::FromWebContents(contents()))
334 tab_specific_content_settings->SetPopupsBlocked(false);
335 if (CoreTabHelper* core_tab_helper =
336 CoreTabHelper::FromWebContents(contents()))
337 core_tab_helper->set_delegate(NULL);
338 if (ThumbnailTabHelper* thumbnail_tab_helper =
339 ThumbnailTabHelper::FromWebContents(contents()))
340 thumbnail_tab_helper->set_enabled(true);
341
342 #if defined(OS_MACOSX)
343 if (content::RenderWidgetHostView* rwhv =
344 contents_->GetRenderWidgetHostView())
345 rwhv->SetTakesFocusOnlyOnMouseDown(false);
346 registrar_.Remove(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
347 content::Source<content::NavigationController>(
348 &contents_->GetController()));
458 #endif 349 #endif
459 } 350 }
460 351
461 void InstantLoader::ReplacePreviewContents(content::WebContents* old_contents, 352 void InstantLoader::ReplacePreviewContents(content::WebContents* old_contents,
462 content::WebContents* new_contents) { 353 content::WebContents* new_contents) {
463 DCHECK_EQ(old_contents, preview_contents_->web_contents()); 354 DCHECK_EQ(old_contents, contents());
464 CleanupPreviewContents(); 355 CleanupPreviewContents();
465 // We release here without deleting so that the caller still has the 356 // We release here without deleting so that the caller still has the
466 // responsibility for deleting the TabContents. 357 // responsibility for deleting the WebContents.
467 ignore_result(preview_contents_.release()); 358 ignore_result(contents_.release());
468 preview_contents_.reset(TabContents::FromWebContents(new_contents)); 359 contents_.reset(new_contents);
469 SetupPreviewContents(); 360 SetupPreviewContents();
470 controller_->SwappedTabContents(this); 361 controller_->SwappedWebContents();
471 } 362 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698