OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/history/history_types.h" | 8 #include "chrome/browser/history/history_types.h" |
9 #include "chrome/browser/instant/instant_loader_delegate.h" | 9 #include "chrome/browser/instant/instant_loader_delegate.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 public CoreTabHelperDelegate, | 31 public CoreTabHelperDelegate, |
32 public content::WebContentsDelegate, | 32 public content::WebContentsDelegate, |
33 public content::WebContentsObserver { | 33 public content::WebContentsObserver { |
34 public: | 34 public: |
35 explicit WebContentsDelegateImpl(InstantLoader* loader); | 35 explicit WebContentsDelegateImpl(InstantLoader* loader); |
36 | 36 |
37 bool is_pointer_down_from_activate() const { | 37 bool is_pointer_down_from_activate() const { |
38 return is_pointer_down_from_activate_; | 38 return is_pointer_down_from_activate_; |
39 } | 39 } |
40 | 40 |
41 // Start observing the given |web_contents| instead of whatever is currently | |
42 // being observed. If |web_contents| is NULL, effectively stops observing. | |
43 void ObserveContents(content::WebContents* web_contents); | |
44 | |
41 // ConstrainedWindowTabHelperDelegate: | 45 // ConstrainedWindowTabHelperDelegate: |
42 virtual bool ShouldFocusConstrainedWindow() OVERRIDE; | 46 virtual bool ShouldFocusConstrainedWindow() OVERRIDE; |
43 | 47 |
44 // CoreTabHelperDelegate: | 48 // CoreTabHelperDelegate: |
45 virtual void SwapTabContents(TabContents* old_tc, | 49 virtual void SwapTabContents(TabContents* old_tc, |
46 TabContents* new_tc) OVERRIDE; | 50 TabContents* new_tc) OVERRIDE; |
47 | 51 |
48 // content::WebContentsDelegate: | 52 // content::WebContentsDelegate: |
49 virtual bool ShouldSuppressDialogs() OVERRIDE; | 53 virtual bool ShouldSuppressDialogs() OVERRIDE; |
50 virtual bool ShouldFocusPageAfterCrash() OVERRIDE; | 54 virtual bool ShouldFocusPageAfterCrash() OVERRIDE; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 InstantLoader* const loader_; | 90 InstantLoader* const loader_; |
87 | 91 |
88 // True if the mouse or a touch pointer is down from an activate. | 92 // True if the mouse or a touch pointer is down from an activate. |
89 bool is_pointer_down_from_activate_; | 93 bool is_pointer_down_from_activate_; |
90 | 94 |
91 DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl); | 95 DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl); |
92 }; | 96 }; |
93 | 97 |
94 InstantLoader::WebContentsDelegateImpl::WebContentsDelegateImpl( | 98 InstantLoader::WebContentsDelegateImpl::WebContentsDelegateImpl( |
95 InstantLoader* loader) | 99 InstantLoader* loader) |
96 : content::WebContentsObserver(loader->preview_contents_->web_contents()), | 100 : loader_(loader), |
97 loader_(loader), | |
98 is_pointer_down_from_activate_(false) { | 101 is_pointer_down_from_activate_(false) { |
99 } | 102 } |
100 | 103 |
104 void InstantLoader::WebContentsDelegateImpl::ObserveContents( | |
105 content::WebContents* web_contents) { | |
106 Observe(web_contents); | |
107 } | |
108 | |
101 bool InstantLoader::WebContentsDelegateImpl::ShouldFocusConstrainedWindow() { | 109 bool InstantLoader::WebContentsDelegateImpl::ShouldFocusConstrainedWindow() { |
102 // Return false so that constrained windows are not initially focused. If we | 110 // Return false so that constrained windows are not initially focused. If we |
103 // did otherwise the preview would prematurely get committed when focus goes | 111 // did otherwise the preview would prematurely get committed when focus goes |
104 // to the constrained window. | 112 // to the constrained window. |
105 return false; | 113 return false; |
106 } | 114 } |
107 | 115 |
108 void InstantLoader::WebContentsDelegateImpl::SwapTabContents( | 116 void InstantLoader::WebContentsDelegateImpl::SwapTabContents( |
109 TabContents* old_tc, | 117 TabContents* old_tc, |
110 TabContents* new_tc) { | 118 TabContents* new_tc) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 OnInstantSupportDetermined) | 201 OnInstantSupportDetermined) |
194 IPC_MESSAGE_UNHANDLED(handled = false) | 202 IPC_MESSAGE_UNHANDLED(handled = false) |
195 IPC_END_MESSAGE_MAP() | 203 IPC_END_MESSAGE_MAP() |
196 return handled; | 204 return handled; |
197 } | 205 } |
198 | 206 |
199 void InstantLoader::WebContentsDelegateImpl::OnSetSuggestions( | 207 void InstantLoader::WebContentsDelegateImpl::OnSetSuggestions( |
200 int page_id, | 208 int page_id, |
201 const std::vector<string16>& suggestions, | 209 const std::vector<string16>& suggestions, |
202 InstantCompleteBehavior behavior) { | 210 InstantCompleteBehavior behavior) { |
211 DCHECK(loader_->preview_contents() && | |
212 loader_->preview_contents_->web_contents()); | |
213 // TODO(sreeram): Remove this 'if' bandaid once bug 141875 is confirmed fixed. | |
214 if (!loader_->preview_contents() || | |
215 !loader_->preview_contents_->web_contents()) { | |
216 return; | |
217 } | |
203 content::NavigationEntry* entry = loader_->preview_contents_->web_contents()-> | 218 content::NavigationEntry* entry = loader_->preview_contents_->web_contents()-> |
204 GetController().GetActiveEntry(); | 219 GetController().GetActiveEntry(); |
205 if (entry && page_id == entry->GetPageID()) { | 220 if (entry && page_id == entry->GetPageID()) { |
206 MaybeSetAndNotifyInstantSupportDetermined(true); | 221 MaybeSetAndNotifyInstantSupportDetermined(true); |
207 loader_->loader_delegate_->SetSuggestions(loader_, suggestions, behavior); | 222 loader_->loader_delegate_->SetSuggestions(loader_, suggestions, behavior); |
208 } | 223 } |
209 } | 224 } |
210 | 225 |
211 void InstantLoader::WebContentsDelegateImpl::OnInstantSupportDetermined( | 226 void InstantLoader::WebContentsDelegateImpl::OnInstantSupportDetermined( |
212 int page_id, | 227 int page_id, |
213 bool result) { | 228 bool result) { |
229 DCHECK(loader_->preview_contents() && | |
230 loader_->preview_contents_->web_contents()); | |
231 // TODO(sreeram): Remove this 'if' bandaid once bug 141875 is confirmed fixed. | |
232 if (!loader_->preview_contents() || | |
Shishir
2012/08/10 22:11:15
How will you know what is fixing the issue: this i
sreeram
2012/08/10 22:13:54
I'll know when debug builds don't crash (the DCHEC
| |
233 !loader_->preview_contents_->web_contents()) { | |
234 return; | |
235 } | |
214 content::NavigationEntry* entry = loader_->preview_contents_->web_contents()-> | 236 content::NavigationEntry* entry = loader_->preview_contents_->web_contents()-> |
215 GetController().GetActiveEntry(); | 237 GetController().GetActiveEntry(); |
216 if (entry && page_id == entry->GetPageID()) | 238 if (entry && page_id == entry->GetPageID()) |
217 MaybeSetAndNotifyInstantSupportDetermined(result); | 239 MaybeSetAndNotifyInstantSupportDetermined(result); |
218 } | 240 } |
219 | 241 |
220 | 242 |
221 void InstantLoader::WebContentsDelegateImpl | 243 void InstantLoader::WebContentsDelegateImpl |
222 ::CommitFromPointerReleaseIfNecessary() { | 244 ::CommitFromPointerReleaseIfNecessary() { |
223 if (is_pointer_down_from_activate_) { | 245 if (is_pointer_down_from_activate_) { |
224 is_pointer_down_from_activate_ = false; | 246 is_pointer_down_from_activate_ = false; |
225 loader_->loader_delegate_->CommitInstantLoader(loader_); | 247 loader_->loader_delegate_->CommitInstantLoader(loader_); |
226 } | 248 } |
227 } | 249 } |
228 | 250 |
229 void InstantLoader::WebContentsDelegateImpl | 251 void InstantLoader::WebContentsDelegateImpl |
230 ::MaybeSetAndNotifyInstantSupportDetermined(bool supports_instant) { | 252 ::MaybeSetAndNotifyInstantSupportDetermined(bool supports_instant) { |
231 // If we already determined that the loader supports Instant, nothing to do. | 253 // If we already determined that the loader supports Instant, nothing to do. |
232 if (loader_->supports_instant_) | 254 if (loader_->supports_instant_) |
233 return; | 255 return; |
234 | 256 |
235 loader_->supports_instant_ = supports_instant; | 257 loader_->supports_instant_ = supports_instant; |
236 loader_->loader_delegate_->InstantSupportDetermined(loader_, | 258 loader_->loader_delegate_->InstantSupportDetermined(loader_, |
237 supports_instant); | 259 supports_instant); |
238 | 260 |
239 // If the page doesn't support the Instant API, InstantController schedules | 261 // If the page doesn't support the Instant API, InstantController schedules |
240 // the loader for destruction. Stop sending the controller any more messages, | 262 // the loader for destruction. Stop sending the controller any more messages, |
241 // by severing the connection from the WebContents to us (its delegate). | 263 // by severing the connection from the WebContents to us (its delegate). |
242 if (!supports_instant) | 264 if (!supports_instant) { |
243 loader_->preview_contents_->web_contents()->SetDelegate(NULL); | 265 loader_->preview_contents_->web_contents()->SetDelegate(NULL); |
266 ObserveContents(NULL); | |
267 } | |
244 } | 268 } |
245 | 269 |
246 // InstantLoader --------------------------------------------------------------- | 270 // InstantLoader --------------------------------------------------------------- |
247 | 271 |
248 InstantLoader::InstantLoader(InstantLoaderDelegate* delegate, | 272 InstantLoader::InstantLoader(InstantLoaderDelegate* delegate, |
249 const std::string& instant_url, | 273 const std::string& instant_url, |
250 const TabContents* tab_contents) | 274 const TabContents* tab_contents) |
251 : loader_delegate_(delegate), | 275 : loader_delegate_(delegate), |
252 preview_contents_(new TabContents(content::WebContents::Create( | 276 preview_contents_(new TabContents(content::WebContents::Create( |
253 tab_contents->profile(), NULL, MSG_ROUTING_NONE, | 277 tab_contents->profile(), NULL, MSG_ROUTING_NONE, |
254 tab_contents->web_contents(), | 278 tab_contents->web_contents(), |
255 tab_contents->web_contents()->GetController(). | 279 tab_contents->web_contents()->GetController(). |
256 GetSessionStorageNamespace()))), | 280 GetSessionStorageNamespace()))), |
257 preview_delegate_(new WebContentsDelegateImpl( | 281 preview_delegate_(new WebContentsDelegateImpl( |
258 ALLOW_THIS_IN_INITIALIZER_LIST(this))), | 282 ALLOW_THIS_IN_INITIALIZER_LIST(this))), |
259 supports_instant_(false), | 283 supports_instant_(false), |
260 instant_url_(instant_url) { | 284 instant_url_(instant_url) { |
261 } | 285 } |
262 | 286 |
263 InstantLoader::~InstantLoader() { | 287 InstantLoader::~InstantLoader() { |
264 if (preview_contents()) | 288 if (preview_contents()) { |
265 preview_contents_->web_contents()->SetDelegate(NULL); | 289 preview_contents_->web_contents()->SetDelegate(NULL); |
290 preview_delegate_->ObserveContents(NULL); | |
291 } | |
266 } | 292 } |
267 | 293 |
268 void InstantLoader::Init() { | 294 void InstantLoader::Init() { |
269 SetupPreviewContents(); | 295 SetupPreviewContents(); |
270 // This HTTP header and value are set on loads that originate from instant. | 296 // This HTTP header and value are set on loads that originate from instant. |
271 const char* const kInstantHeader = "X-Purpose: Instant"; | 297 const char* const kInstantHeader = "X-Purpose: Instant"; |
272 preview_contents_->web_contents()->GetController().LoadURL(GURL(instant_url_), | 298 preview_contents_->web_contents()->GetController().LoadURL(GURL(instant_url_), |
273 content::Referrer(), content::PAGE_TRANSITION_GENERATED, kInstantHeader); | 299 content::Referrer(), content::PAGE_TRANSITION_GENERATED, kInstantHeader); |
274 preview_contents_->web_contents()->WasHidden(); | 300 preview_contents_->web_contents()->WasHidden(); |
275 } | 301 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
318 return; | 344 return; |
319 } | 345 } |
320 NOTREACHED(); | 346 NOTREACHED(); |
321 #endif | 347 #endif |
322 } | 348 } |
323 | 349 |
324 void InstantLoader::SetupPreviewContents() { | 350 void InstantLoader::SetupPreviewContents() { |
325 content::WebContents* new_contents = preview_contents_->web_contents(); | 351 content::WebContents* new_contents = preview_contents_->web_contents(); |
326 WebContentsDelegateImpl* new_delegate = preview_delegate_.get(); | 352 WebContentsDelegateImpl* new_delegate = preview_delegate_.get(); |
327 new_contents->SetDelegate(new_delegate); | 353 new_contents->SetDelegate(new_delegate); |
354 new_delegate->ObserveContents(new_contents); | |
328 | 355 |
329 // Disable popups and such (mainly to avoid losing focus and reverting the | 356 // Disable popups and such (mainly to avoid losing focus and reverting the |
330 // preview prematurely). | 357 // preview prematurely). |
331 preview_contents_->blocked_content_tab_helper()->SetAllContentsBlocked(true); | 358 preview_contents_->blocked_content_tab_helper()->SetAllContentsBlocked(true); |
332 preview_contents_->constrained_window_tab_helper()->set_delegate( | 359 preview_contents_->constrained_window_tab_helper()->set_delegate( |
333 new_delegate); | 360 new_delegate); |
334 preview_contents_->content_settings()->SetPopupsBlocked(true); | 361 preview_contents_->content_settings()->SetPopupsBlocked(true); |
335 preview_contents_->core_tab_helper()->set_delegate(new_delegate); | 362 preview_contents_->core_tab_helper()->set_delegate(new_delegate); |
336 if (ThumbnailGenerator* tg = preview_contents_->thumbnail_generator()) | 363 if (ThumbnailGenerator* tg = preview_contents_->thumbnail_generator()) |
337 tg->set_enabled(false); | 364 tg->set_enabled(false); |
338 | 365 |
339 #if defined(OS_MACOSX) | 366 #if defined(OS_MACOSX) |
340 // If |preview_contents_| does not currently have a RWHV, we will call | 367 // If |preview_contents_| does not currently have a RWHV, we will call |
341 // SetTakesFocusOnlyOnMouseDown() as a result of the RENDER_VIEW_HOST_CHANGED | 368 // SetTakesFocusOnlyOnMouseDown() as a result of the RENDER_VIEW_HOST_CHANGED |
342 // notification. | 369 // notification. |
343 if (content::RenderWidgetHostView* rwhv = | 370 if (content::RenderWidgetHostView* rwhv = |
344 new_contents->GetRenderWidgetHostView()) { | 371 new_contents->GetRenderWidgetHostView()) { |
345 rwhv->SetTakesFocusOnlyOnMouseDown(true); | 372 rwhv->SetTakesFocusOnlyOnMouseDown(true); |
346 } | 373 } |
347 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, | 374 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, |
348 content::Source<content::NavigationController>( | 375 content::Source<content::NavigationController>( |
349 &new_contents->GetController())); | 376 &new_contents->GetController())); |
350 #endif | 377 #endif |
351 } | 378 } |
352 | 379 |
353 void InstantLoader::CleanupPreviewContents() { | 380 void InstantLoader::CleanupPreviewContents() { |
354 content::WebContents* old_contents = preview_contents_->web_contents(); | 381 content::WebContents* old_contents = preview_contents_->web_contents(); |
355 old_contents->SetDelegate(NULL); | 382 old_contents->SetDelegate(NULL); |
383 preview_delegate_->ObserveContents(NULL); | |
356 | 384 |
357 preview_contents_->blocked_content_tab_helper()->SetAllContentsBlocked(false); | 385 preview_contents_->blocked_content_tab_helper()->SetAllContentsBlocked(false); |
358 preview_contents_->constrained_window_tab_helper()->set_delegate(NULL); | 386 preview_contents_->constrained_window_tab_helper()->set_delegate(NULL); |
359 preview_contents_->content_settings()->SetPopupsBlocked(false); | 387 preview_contents_->content_settings()->SetPopupsBlocked(false); |
360 preview_contents_->core_tab_helper()->set_delegate(NULL); | 388 preview_contents_->core_tab_helper()->set_delegate(NULL); |
361 if (ThumbnailGenerator* tg = preview_contents_->thumbnail_generator()) | 389 if (ThumbnailGenerator* tg = preview_contents_->thumbnail_generator()) |
362 tg->set_enabled(true); | 390 tg->set_enabled(true); |
363 | 391 |
364 #if defined(OS_MACOSX) | 392 #if defined(OS_MACOSX) |
365 if (content::RenderWidgetHostView* rwhv = | 393 if (content::RenderWidgetHostView* rwhv = |
(...skipping 10 matching lines...) Expand all Loading... | |
376 TabContents* new_tc) { | 404 TabContents* new_tc) { |
377 DCHECK(old_tc == preview_contents_); | 405 DCHECK(old_tc == preview_contents_); |
378 CleanupPreviewContents(); | 406 CleanupPreviewContents(); |
379 // We release here without deleting so that the caller still has the | 407 // We release here without deleting so that the caller still has the |
380 // responsibility for deleting the TabContents. | 408 // responsibility for deleting the TabContents. |
381 ignore_result(preview_contents_.release()); | 409 ignore_result(preview_contents_.release()); |
382 preview_contents_.reset(new_tc); | 410 preview_contents_.reset(new_tc); |
383 SetupPreviewContents(); | 411 SetupPreviewContents(); |
384 loader_delegate_->SwappedTabContents(this); | 412 loader_delegate_->SwappedTabContents(this); |
385 } | 413 } |
OLD | NEW |