OLD | NEW |
---|---|
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" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/thumbnails/thumbnail_tab_helper.h" | 10 #include "chrome/browser/thumbnails/thumbnail_tab_helper.h" |
(...skipping 29 matching lines...) Expand all Loading... | |
40 DISALLOW_COPY_AND_ASSIGN(InstantLoaderUserData); | 40 DISALLOW_COPY_AND_ASSIGN(InstantLoaderUserData); |
41 }; | 41 }; |
42 | 42 |
43 } | 43 } |
44 | 44 |
45 // WebContentsDelegateImpl ----------------------------------------------------- | 45 // WebContentsDelegateImpl ----------------------------------------------------- |
46 | 46 |
47 class InstantLoader::WebContentsDelegateImpl | 47 class InstantLoader::WebContentsDelegateImpl |
48 : public ConstrainedWindowTabHelperDelegate, | 48 : public ConstrainedWindowTabHelperDelegate, |
49 public CoreTabHelperDelegate, | 49 public CoreTabHelperDelegate, |
50 public content::WebContentsDelegate, | 50 public content::WebContentsDelegate { |
51 public content::WebContentsObserver { | |
52 public: | 51 public: |
53 explicit WebContentsDelegateImpl(InstantLoader* loader); | 52 explicit WebContentsDelegateImpl(InstantLoader* loader); |
54 | 53 |
55 bool is_pointer_down_from_activate() const { | 54 bool is_pointer_down_from_activate() const { |
56 return is_pointer_down_from_activate_; | 55 return is_pointer_down_from_activate_; |
57 } | 56 } |
58 | 57 |
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: | 58 private: |
64 // Overridden from ConstrainedWindowTabHelperDelegate: | 59 // Overridden from ConstrainedWindowTabHelperDelegate: |
65 virtual bool ShouldFocusConstrainedWindow() OVERRIDE; | 60 virtual bool ShouldFocusConstrainedWindow() OVERRIDE; |
66 | 61 |
67 // Overridden from CoreTabHelperDelegate: | 62 // Overridden from CoreTabHelperDelegate: |
68 virtual void SwapTabContents(content::WebContents* old_contents, | 63 virtual void SwapTabContents(content::WebContents* old_contents, |
69 content::WebContents* new_contents) OVERRIDE; | 64 content::WebContents* new_contents) OVERRIDE; |
70 | 65 |
71 // Overridden from content::WebContentsDelegate: | 66 // Overridden from content::WebContentsDelegate: |
72 virtual bool ShouldSuppressDialogs() OVERRIDE; | 67 virtual bool ShouldSuppressDialogs() OVERRIDE; |
73 virtual bool ShouldFocusPageAfterCrash() OVERRIDE; | 68 virtual bool ShouldFocusPageAfterCrash() OVERRIDE; |
74 virtual void LostCapture() OVERRIDE; | 69 virtual void LostCapture() OVERRIDE; |
75 virtual void WebContentsFocused(content::WebContents* contents) OVERRIDE; | 70 virtual void WebContentsFocused(content::WebContents* contents) OVERRIDE; |
76 virtual bool CanDownload(content::RenderViewHost* render_view_host, | 71 virtual bool CanDownload(content::RenderViewHost* render_view_host, |
77 int request_id, | 72 int request_id, |
78 const std::string& request_method) OVERRIDE; | 73 const std::string& request_method) OVERRIDE; |
79 virtual void HandleMouseDown() OVERRIDE; | 74 virtual void HandleMouseDown() OVERRIDE; |
80 virtual void HandleMouseUp() OVERRIDE; | 75 virtual void HandleMouseUp() OVERRIDE; |
81 virtual void HandlePointerActivate() OVERRIDE; | 76 virtual void HandlePointerActivate() OVERRIDE; |
82 virtual void HandleGestureEnd() OVERRIDE; | 77 virtual void HandleGestureEnd() OVERRIDE; |
83 virtual void DragEnded() OVERRIDE; | 78 virtual void DragEnded() OVERRIDE; |
84 virtual bool OnGoToEntryOffset(int offset) OVERRIDE; | 79 virtual bool OnGoToEntryOffset(int offset) OVERRIDE; |
85 | 80 |
86 // Overridden from content::WebContentsObserver: | |
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(); | 81 void CommitFromPointerReleaseIfNecessary(); |
108 void MaybeSetAndNotifyInstantSupportDetermined(bool supports_instant); | |
109 | 82 |
110 InstantLoader* const loader_; | 83 InstantLoader* const loader_; |
111 | 84 |
112 // True if the mouse or a touch pointer is down from an activate. | 85 // True if the mouse or a touch pointer is down from an activate. |
113 bool is_pointer_down_from_activate_; | 86 bool is_pointer_down_from_activate_; |
114 | 87 |
115 DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl); | 88 DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl); |
116 }; | 89 }; |
117 | 90 |
118 InstantLoader::WebContentsDelegateImpl::WebContentsDelegateImpl( | 91 InstantLoader::WebContentsDelegateImpl::WebContentsDelegateImpl( |
119 InstantLoader* loader) | 92 InstantLoader* loader) |
120 : loader_(loader), | 93 : loader_(loader), |
121 is_pointer_down_from_activate_(false) { | 94 is_pointer_down_from_activate_(false) { |
122 } | 95 } |
123 | 96 |
124 void InstantLoader::WebContentsDelegateImpl::ObserveContents( | |
125 content::WebContents* web_contents) { | |
126 Observe(web_contents); | |
127 } | |
128 | |
129 bool InstantLoader::WebContentsDelegateImpl::ShouldFocusConstrainedWindow() { | 97 bool InstantLoader::WebContentsDelegateImpl::ShouldFocusConstrainedWindow() { |
130 // Return false so that constrained windows are not initially focused. If we | 98 // Return false so that constrained windows are not initially focused. If we |
131 // did otherwise the preview would prematurely get committed when focus goes | 99 // did otherwise the preview would prematurely get committed when focus goes |
132 // to the constrained window. | 100 // to the constrained window. |
133 return false; | 101 return false; |
134 } | 102 } |
135 | 103 |
136 void InstantLoader::WebContentsDelegateImpl::SwapTabContents( | 104 void InstantLoader::WebContentsDelegateImpl::SwapTabContents( |
137 content::WebContents* old_contents, | 105 content::WebContents* old_contents, |
138 content::WebContents* new_contents) { | 106 content::WebContents* new_contents) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 // If the user drags, we won't get a mouse up (at least on Linux). Commit the | 155 // 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 | 156 // Instant result when the drag ends, so that during the drag the page won't |
189 // move around. | 157 // move around. |
190 CommitFromPointerReleaseIfNecessary(); | 158 CommitFromPointerReleaseIfNecessary(); |
191 } | 159 } |
192 | 160 |
193 bool InstantLoader::WebContentsDelegateImpl::OnGoToEntryOffset(int offset) { | 161 bool InstantLoader::WebContentsDelegateImpl::OnGoToEntryOffset(int offset) { |
194 return false; | 162 return false; |
195 } | 163 } |
196 | 164 |
197 void InstantLoader::WebContentsDelegateImpl::DidFinishLoad( | |
198 int64 frame_id, | |
199 const GURL& validated_url, | |
200 bool is_main_frame, | |
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 } | |
231 } | |
232 | |
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 | 165 void InstantLoader::WebContentsDelegateImpl |
260 ::CommitFromPointerReleaseIfNecessary() { | 166 ::CommitFromPointerReleaseIfNecessary() { |
261 if (is_pointer_down_from_activate_) { | 167 if (is_pointer_down_from_activate_) { |
262 is_pointer_down_from_activate_ = false; | 168 is_pointer_down_from_activate_ = false; |
263 loader_->controller_->CommitInstantLoader(loader_); | 169 loader_->controller_->CommitInstantLoader(loader_); |
264 } | 170 } |
265 } | 171 } |
266 | 172 |
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 --------------------------------------------------------------- | 173 // InstantLoader --------------------------------------------------------------- |
277 | 174 |
278 // static | 175 // static |
279 InstantLoader* InstantLoader::FromWebContents( | 176 InstantLoader* InstantLoader::FromWebContents( |
280 content::WebContents* web_contents) { | 177 content::WebContents* web_contents) { |
281 InstantLoaderUserData* data = static_cast<InstantLoaderUserData*>( | 178 InstantLoaderUserData* data = static_cast<InstantLoaderUserData*>( |
282 web_contents->GetUserData(&kUserDataKey)); | 179 web_contents->GetUserData(&kUserDataKey)); |
283 return data ? data->loader() : NULL; | 180 return data ? data->loader() : NULL; |
284 } | 181 } |
285 | 182 |
286 InstantLoader::InstantLoader(InstantController* controller, | 183 InstantLoader::InstantLoader(InstantController* controller, |
287 const std::string& instant_url, | 184 const std::string& instant_url, |
288 const TabContents* tab_contents) | 185 const TabContents* tab_contents) |
289 : controller_(controller), | 186 : InstantClient(controller, NULL), |
sky
2012/11/27 01:07:51
I get why you subclassed here, but it feels a bit
samarth
2012/11/27 18:09:43
This might be what sky is already saying, but what
sreeram
2012/11/27 18:41:33
I understand the style guide's preference for comp
sreeram
2012/11/29 07:33:19
Done. Composition wins.
| |
290 preview_delegate_(new WebContentsDelegateImpl( | 187 preview_delegate_(new WebContentsDelegateImpl( |
291 ALLOW_THIS_IN_INITIALIZER_LIST(this))), | 188 ALLOW_THIS_IN_INITIALIZER_LIST(this))), |
292 preview_contents_( | 189 preview_contents_( |
293 TabContents::Factory::CreateTabContents( | 190 TabContents::Factory::CreateTabContents( |
294 content::WebContents::CreateWithSessionStorage( | 191 content::WebContents::CreateWithSessionStorage( |
295 tab_contents->profile(), NULL, MSG_ROUTING_NONE, | 192 tab_contents->profile(), NULL, MSG_ROUTING_NONE, |
296 tab_contents->web_contents(), | 193 tab_contents->web_contents(), |
297 tab_contents->web_contents()->GetController(). | 194 tab_contents->web_contents()->GetController(). |
298 GetSessionStorageNamespaceMap()))), | 195 GetSessionStorageNamespaceMap()))), |
299 supports_instant_(false), | |
300 instant_url_(instant_url) { | 196 instant_url_(instant_url) { |
301 } | 197 } |
302 | 198 |
303 InstantLoader::~InstantLoader() { | 199 InstantLoader::~InstantLoader() { |
304 } | 200 } |
305 | 201 |
306 void InstantLoader::Init() { | 202 void InstantLoader::Init() { |
307 SetupPreviewContents(); | 203 SetupPreviewContents(); |
308 // This HTTP header and value are set on loads that originate from instant. | 204 // This HTTP header and value are set on loads that originate from instant. |
309 const char kInstantHeader[] = "X-Purpose: Instant"; | 205 const char kInstantHeader[] = "X-Purpose: Instant"; |
310 DVLOG(1) << "LoadURL: " << instant_url_; | 206 DVLOG(1) << "LoadURL: " << instant_url_; |
311 preview_contents_->web_contents()->GetController().LoadURL(GURL(instant_url_), | 207 contents()->GetController().LoadURL(GURL(instant_url_), content::Referrer(), |
312 content::Referrer(), content::PAGE_TRANSITION_GENERATED, kInstantHeader); | 208 content::PAGE_TRANSITION_GENERATED, kInstantHeader); |
313 preview_contents_->web_contents()->WasHidden(); | 209 contents()->WasHidden(); |
314 } | 210 } |
315 | 211 |
316 void InstantLoader::Update(const string16& user_text, bool verbatim) { | 212 void InstantLoader::Cancel(const string16& text) { |
317 // TODO: Support real cursor position. | 213 Send(new ChromeViewMsg_SearchBoxCancel(routing_id(), text)); |
318 last_navigation_ = history::HistoryAddPageArgs(); | |
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 } | 214 } |
324 | 215 |
325 void InstantLoader::SetOmniboxBounds(const gfx::Rect& bounds) { | 216 void InstantLoader::SetOmniboxBounds(const gfx::Rect& bounds) { |
326 content::RenderViewHost* rvh = | 217 Send(new ChromeViewMsg_SearchBoxResize(routing_id(), bounds)); |
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 } | 218 } |
338 | 219 |
339 void InstantLoader::SendThemeBackgroundInfo( | 220 void InstantLoader::SendThemeBackgroundInfo( |
340 const ThemeBackgroundInfo& theme_info) { | 221 const ThemeBackgroundInfo& theme_info) { |
341 content::RenderViewHost* rvh = | 222 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info)); |
342 preview_contents_->web_contents()->GetRenderViewHost(); | |
343 rvh->Send(new ChromeViewMsg_SearchBoxThemeChanged(rvh->GetRoutingID(), | |
344 theme_info)); | |
345 } | 223 } |
346 | 224 |
347 void InstantLoader::SendThemeAreaHeight(int height) { | 225 void InstantLoader::SendThemeAreaHeight(int height) { |
348 content::RenderViewHost* rvh = | 226 Send(new ChromeViewMsg_SearchBoxThemeAreaHeightChanged(routing_id(), height)); |
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 } | 227 } |
360 | 228 |
361 void InstantLoader::SearchModeChanged(const chrome::search::Mode& mode) { | 229 void InstantLoader::SearchModeChanged(const chrome::search::Mode& mode) { |
362 content::RenderViewHost* rvh = | 230 Send(new ChromeViewMsg_SearchBoxModeChanged(routing_id(), mode)); |
363 preview_contents_->web_contents()->GetRenderViewHost(); | |
364 rvh->Send(new ChromeViewMsg_SearchBoxModeChanged(rvh->GetRoutingID(), mode)); | |
365 } | 231 } |
366 | 232 |
367 void InstantLoader::DidNavigate( | 233 void InstantLoader::DidNavigate( |
368 const history::HistoryAddPageArgs& add_page_args) { | 234 const history::HistoryAddPageArgs& add_page_args) { |
369 last_navigation_ = add_page_args; | 235 last_navigation_ = add_page_args; |
370 } | 236 } |
371 | 237 |
372 TabContents* InstantLoader::ReleasePreviewContents(InstantCommitType type, | 238 void InstantLoader::CleanupPreviewContents() { |
373 const string16& text) { | 239 contents()->RemoveUserData(&kUserDataKey); |
374 content::RenderViewHost* rvh = | 240 contents()->SetDelegate(NULL); |
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 } | |
383 | 241 |
384 void InstantLoader::CleanupPreviewContents() { | 242 BlockedContentTabHelper::FromWebContents(contents())-> |
385 content::WebContents* old_contents = preview_contents_->web_contents(); | |
386 old_contents->RemoveUserData(&kUserDataKey); | |
387 old_contents->SetDelegate(NULL); | |
388 preview_delegate_->ObserveContents(NULL); | |
389 | |
390 BlockedContentTabHelper::FromWebContents(old_contents)-> | |
391 SetAllContentsBlocked(false); | 243 SetAllContentsBlocked(false); |
392 ConstrainedWindowTabHelper::FromWebContents(old_contents)->set_delegate(NULL); | 244 ConstrainedWindowTabHelper::FromWebContents(contents())->set_delegate(NULL); |
393 TabSpecificContentSettings::FromWebContents(old_contents)-> | 245 TabSpecificContentSettings::FromWebContents(contents())-> |
394 SetPopupsBlocked(false); | 246 SetPopupsBlocked(false); |
395 CoreTabHelper::FromWebContents(old_contents)->set_delegate(NULL); | 247 CoreTabHelper::FromWebContents(contents())->set_delegate(NULL); |
396 if (ThumbnailTabHelper* thumbnail_tab_helper = | 248 if (ThumbnailTabHelper* thumbnail_tab_helper = |
397 ThumbnailTabHelper::FromWebContents(old_contents)) | 249 ThumbnailTabHelper::FromWebContents(contents())) |
398 thumbnail_tab_helper->set_enabled(true); | 250 thumbnail_tab_helper->set_enabled(true); |
399 | 251 |
400 #if defined(OS_MACOSX) | 252 #if defined(OS_MACOSX) |
401 if (content::RenderWidgetHostView* rwhv = | 253 if (content::RenderWidgetHostView* rwhv = |
402 old_contents->GetRenderWidgetHostView()) | 254 contents()->GetRenderWidgetHostView()) |
403 rwhv->SetTakesFocusOnlyOnMouseDown(false); | 255 rwhv->SetTakesFocusOnlyOnMouseDown(false); |
404 registrar_.Remove(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, | 256 registrar_.Remove(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, |
405 content::Source<content::NavigationController>( | 257 content::Source<content::NavigationController>( |
406 &old_contents->GetController())); | 258 &contents()->GetController())); |
407 #endif | 259 #endif |
260 | |
261 content::WebContentsObserver::Observe(NULL); | |
408 } | 262 } |
409 | 263 |
410 bool InstantLoader::IsPointerDownFromActivate() const { | 264 bool InstantLoader::IsPointerDownFromActivate() const { |
411 return preview_delegate_->is_pointer_down_from_activate(); | 265 return preview_delegate_->is_pointer_down_from_activate(); |
412 } | 266 } |
413 | 267 |
268 bool InstantLoader::OnMessageReceived(const IPC::Message& message) { | |
269 if (InstantClient::OnMessageReceived(message)) | |
270 return true; | |
271 | |
272 bool handled = true; | |
273 IPC_BEGIN_MESSAGE_MAP(InstantLoader, message) | |
274 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantPreview, | |
275 OnShowInstantPreview); | |
276 IPC_MESSAGE_UNHANDLED(handled = false) | |
277 IPC_END_MESSAGE_MAP() | |
278 return handled; | |
279 } | |
280 | |
414 void InstantLoader::Observe(int type, | 281 void InstantLoader::Observe(int type, |
415 const content::NotificationSource& source, | 282 const content::NotificationSource& source, |
416 const content::NotificationDetails& details) { | 283 const content::NotificationDetails& details) { |
417 #if defined(OS_MACOSX) | 284 #if defined(OS_MACOSX) |
418 if (type == content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED) { | 285 if (type == content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED) { |
419 if (content::RenderWidgetHostView* rwhv = | 286 if (content::RenderWidgetHostView* rwhv = |
420 preview_contents_->web_contents()->GetRenderWidgetHostView()) | 287 contents()->GetRenderWidgetHostView()) |
421 rwhv->SetTakesFocusOnlyOnMouseDown(true); | 288 rwhv->SetTakesFocusOnlyOnMouseDown(true); |
422 return; | 289 return; |
423 } | 290 } |
424 NOTREACHED(); | 291 NOTREACHED(); |
425 #endif | 292 #endif |
426 } | 293 } |
427 | 294 |
428 void InstantLoader::SetupPreviewContents() { | 295 void InstantLoader::SetupPreviewContents() { |
429 content::WebContents* new_contents = preview_contents_->web_contents(); | 296 content::WebContentsObserver::Observe(preview_contents_->web_contents()); |
430 new_contents->SetUserData(&kUserDataKey, new InstantLoaderUserData(this)); | 297 |
431 WebContentsDelegateImpl* new_delegate = preview_delegate_.get(); | 298 contents()->SetUserData(&kUserDataKey, new InstantLoaderUserData(this)); |
432 new_contents->SetDelegate(new_delegate); | 299 contents()->SetDelegate(preview_delegate_.get()); |
433 new_delegate->ObserveContents(new_contents); | |
434 | 300 |
435 // Disable popups and such (mainly to avoid losing focus and reverting the | 301 // Disable popups and such (mainly to avoid losing focus and reverting the |
436 // preview prematurely). | 302 // preview prematurely). |
437 BlockedContentTabHelper::FromWebContents(new_contents)-> | 303 BlockedContentTabHelper::FromWebContents(contents())-> |
438 SetAllContentsBlocked(true); | 304 SetAllContentsBlocked(true); |
439 ConstrainedWindowTabHelper::FromWebContents(new_contents)-> | 305 ConstrainedWindowTabHelper::FromWebContents(contents())-> |
440 set_delegate(new_delegate); | 306 set_delegate(preview_delegate_.get()); |
441 TabSpecificContentSettings::FromWebContents(new_contents)-> | 307 TabSpecificContentSettings::FromWebContents(contents())-> |
442 SetPopupsBlocked(true); | 308 SetPopupsBlocked(true); |
443 CoreTabHelper::FromWebContents(new_contents)->set_delegate(new_delegate); | 309 CoreTabHelper::FromWebContents(contents())-> |
310 set_delegate(preview_delegate_.get()); | |
444 if (ThumbnailTabHelper* thumbnail_tab_helper = | 311 if (ThumbnailTabHelper* thumbnail_tab_helper = |
445 ThumbnailTabHelper::FromWebContents(new_contents)) | 312 ThumbnailTabHelper::FromWebContents(contents())) |
446 thumbnail_tab_helper->set_enabled(false); | 313 thumbnail_tab_helper->set_enabled(false); |
447 | 314 |
448 #if defined(OS_MACOSX) | 315 #if defined(OS_MACOSX) |
449 // If |preview_contents_| does not currently have a RWHV, we will call | 316 // If |preview_contents_| does not currently have a RWHV, we will call |
450 // SetTakesFocusOnlyOnMouseDown() as a result of the RENDER_VIEW_HOST_CHANGED | 317 // SetTakesFocusOnlyOnMouseDown() as a result of the RENDER_VIEW_HOST_CHANGED |
451 // notification. | 318 // notification. |
452 if (content::RenderWidgetHostView* rwhv = | 319 if (content::RenderWidgetHostView* rwhv = |
453 new_contents->GetRenderWidgetHostView()) | 320 contents()->GetRenderWidgetHostView()) |
454 rwhv->SetTakesFocusOnlyOnMouseDown(true); | 321 rwhv->SetTakesFocusOnlyOnMouseDown(true); |
455 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, | 322 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, |
456 content::Source<content::NavigationController>( | 323 content::Source<content::NavigationController>( |
457 &new_contents->GetController())); | 324 &contents()->GetController())); |
458 #endif | 325 #endif |
459 } | 326 } |
460 | 327 |
461 void InstantLoader::ReplacePreviewContents(content::WebContents* old_contents, | 328 void InstantLoader::ReplacePreviewContents(content::WebContents* old_contents, |
462 content::WebContents* new_contents) { | 329 content::WebContents* new_contents) { |
463 DCHECK_EQ(old_contents, preview_contents_->web_contents()); | 330 DCHECK_EQ(old_contents, contents()); |
464 CleanupPreviewContents(); | 331 CleanupPreviewContents(); |
465 // We release here without deleting so that the caller still has the | 332 // We release here without deleting so that the caller still has the |
466 // responsibility for deleting the TabContents. | 333 // responsibility for deleting the TabContents. |
467 ignore_result(preview_contents_.release()); | 334 ignore_result(preview_contents_.release()); |
468 preview_contents_.reset(TabContents::FromWebContents(new_contents)); | 335 preview_contents_.reset(TabContents::FromWebContents(new_contents)); |
469 SetupPreviewContents(); | 336 SetupPreviewContents(); |
470 controller_->SwappedTabContents(this); | 337 controller_->SwappedTabContents(this); |
471 } | 338 } |
339 | |
340 void InstantLoader::OnShowInstantPreview(int page_id, | |
341 InstantShownReason reason, | |
342 int height, | |
343 InstantSizeUnits units) { | |
344 content::NavigationEntry* entry = | |
345 contents()->GetController().GetActiveEntry(); | |
346 if (entry && page_id == entry->GetPageID()) { | |
347 NotifyInstantSupportDetermined(true); | |
348 controller_->ShowInstantPreview(this, reason, height, units); | |
349 } | |
350 } | |
OLD | NEW |