OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
14 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
15 #include "base/timer.h" | 15 #include "base/timer.h" |
16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "chrome/browser/favicon/favicon_service.h" | 18 #include "chrome/browser/favicon/favicon_service.h" |
19 #include "chrome/browser/history/history_marshaling.h" | 19 #include "chrome/browser/history/history_marshaling.h" |
20 #include "chrome/browser/history/history_tab_helper.h" | 20 #include "chrome/browser/history/history_tab_helper.h" |
21 #include "chrome/browser/instant/instant_loader_delegate.h" | 21 #include "chrome/browser/instant/instant_loader_delegate.h" |
22 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/browser/search_engines/template_url.h" | 23 #include "chrome/browser/search_engines/template_url.h" |
24 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" | 24 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" |
25 #include "chrome/browser/ui/download/download_tab_helper.h" | 25 #include "chrome/browser/ui/download/download_tab_helper.h" |
26 #include "chrome/browser/ui/download/download_tab_helper_delegate.h" | 26 #include "chrome/browser/ui/download/download_tab_helper_delegate.h" |
27 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 27 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 28 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper_delegate.h" |
28 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
29 #include "chrome/common/render_messages.h" | 30 #include "chrome/common/render_messages.h" |
30 #include "content/browser/renderer_host/render_view_host.h" | 31 #include "content/browser/renderer_host/render_view_host.h" |
31 #include "content/browser/renderer_host/render_widget_host.h" | 32 #include "content/browser/renderer_host/render_widget_host.h" |
32 #include "content/browser/renderer_host/render_widget_host_view.h" | 33 #include "content/browser/renderer_host/render_widget_host_view.h" |
33 #include "content/browser/tab_contents/navigation_controller.h" | 34 #include "content/browser/tab_contents/navigation_controller.h" |
34 #include "content/browser/tab_contents/navigation_entry.h" | 35 #include "content/browser/tab_contents/navigation_entry.h" |
35 #include "content/browser/tab_contents/provisional_load_details.h" | 36 #include "content/browser/tab_contents/provisional_load_details.h" |
36 #include "content/browser/tab_contents/tab_contents.h" | 37 #include "content/browser/tab_contents/tab_contents.h" |
37 #include "content/browser/tab_contents/tab_contents_delegate.h" | 38 #include "content/browser/tab_contents/tab_contents_delegate.h" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 default: | 138 default: |
138 NOTREACHED(); | 139 NOTREACHED(); |
139 break; | 140 break; |
140 } | 141 } |
141 } | 142 } |
142 | 143 |
143 // TabContentsDelegateImpl ----------------------------------------------------- | 144 // TabContentsDelegateImpl ----------------------------------------------------- |
144 | 145 |
145 class InstantLoader::TabContentsDelegateImpl | 146 class InstantLoader::TabContentsDelegateImpl |
146 : public TabContentsDelegate, | 147 : public TabContentsDelegate, |
| 148 public TabContentsWrapperDelegate, |
147 public NotificationObserver, | 149 public NotificationObserver, |
148 public TabContentsObserver, | 150 public TabContentsObserver, |
149 public DownloadTabHelperDelegate { | 151 public DownloadTabHelperDelegate { |
150 public: | 152 public: |
151 explicit TabContentsDelegateImpl(InstantLoader* loader); | 153 explicit TabContentsDelegateImpl(InstantLoader* loader); |
152 | 154 |
153 // Invoked prior to loading a new URL. | 155 // Invoked prior to loading a new URL. |
154 void PrepareForNewLoad(); | 156 void PrepareForNewLoad(); |
155 | 157 |
156 // Invoked when the preview paints. Invokes PreviewPainted on the loader. | 158 // Invoked when the preview paints. Invokes PreviewPainted on the loader. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 // move around. | 215 // move around. |
214 virtual void DragEnded() OVERRIDE; | 216 virtual void DragEnded() OVERRIDE; |
215 virtual void HandleMouseUp() OVERRIDE; | 217 virtual void HandleMouseUp() OVERRIDE; |
216 virtual void HandleMouseActivate() OVERRIDE; | 218 virtual void HandleMouseActivate() OVERRIDE; |
217 virtual bool OnGoToEntryOffset(int offset) OVERRIDE; | 219 virtual bool OnGoToEntryOffset(int offset) OVERRIDE; |
218 virtual bool ShouldAddNavigationToHistory( | 220 virtual bool ShouldAddNavigationToHistory( |
219 const history::HistoryAddPageArgs& add_page_args, | 221 const history::HistoryAddPageArgs& add_page_args, |
220 NavigationType::Type navigation_type) OVERRIDE; | 222 NavigationType::Type navigation_type) OVERRIDE; |
221 virtual bool ShouldShowHungRendererDialog() OVERRIDE; | 223 virtual bool ShouldShowHungRendererDialog() OVERRIDE; |
222 | 224 |
| 225 // TabContentsWrapperDelegate: |
| 226 virtual void SwapTabContents(TabContentsWrapper* old_tc, |
| 227 TabContentsWrapper* new_tc) OVERRIDE; |
| 228 |
223 // TabContentsObserver: | 229 // TabContentsObserver: |
224 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 230 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
225 | 231 |
226 // DownloadTabHelperDelegate: | 232 // DownloadTabHelperDelegate: |
227 virtual bool CanDownload(int request_id) OVERRIDE; | 233 virtual bool CanDownload(int request_id) OVERRIDE; |
228 virtual void OnStartDownload(DownloadItem* download, | 234 virtual void OnStartDownload(DownloadItem* download, |
229 TabContentsWrapper* tab) OVERRIDE; | 235 TabContentsWrapper* tab) OVERRIDE; |
230 | 236 |
231 private: | 237 private: |
232 typedef std::vector<scoped_refptr<history::HistoryAddPageArgs> > | 238 typedef std::vector<scoped_refptr<history::HistoryAddPageArgs> > |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 return false; | 553 return false; |
548 } | 554 } |
549 | 555 |
550 bool InstantLoader::TabContentsDelegateImpl::ShouldShowHungRendererDialog() { | 556 bool InstantLoader::TabContentsDelegateImpl::ShouldShowHungRendererDialog() { |
551 // If we allow the hung renderer dialog to be shown it'll gain focus, | 557 // If we allow the hung renderer dialog to be shown it'll gain focus, |
552 // stealing focus from the omnibox causing instant to be cancelled. Return | 558 // stealing focus from the omnibox causing instant to be cancelled. Return |
553 // false so that doesn't happen. | 559 // false so that doesn't happen. |
554 return false; | 560 return false; |
555 } | 561 } |
556 | 562 |
| 563 // If this is being called, something is swapping in to our preview_contents_ |
| 564 // before we've added it to the tab strip. |
| 565 void InstantLoader::TabContentsDelegateImpl::SwapTabContents( |
| 566 TabContentsWrapper* old_tc, |
| 567 TabContentsWrapper* new_tc) { |
| 568 loader_->ReplacePreviewContents(old_tc, new_tc); |
| 569 } |
| 570 |
| 571 |
557 bool InstantLoader::TabContentsDelegateImpl::OnMessageReceived( | 572 bool InstantLoader::TabContentsDelegateImpl::OnMessageReceived( |
558 const IPC::Message& message) { | 573 const IPC::Message& message) { |
559 bool handled = true; | 574 bool handled = true; |
560 IPC_BEGIN_MESSAGE_MAP(TabContentsDelegateImpl, message) | 575 IPC_BEGIN_MESSAGE_MAP(TabContentsDelegateImpl, message) |
561 IPC_MESSAGE_HANDLER(ViewHostMsg_SetSuggestions, OnSetSuggestions) | 576 IPC_MESSAGE_HANDLER(ViewHostMsg_SetSuggestions, OnSetSuggestions) |
562 IPC_MESSAGE_HANDLER(ViewHostMsg_InstantSupportDetermined, | 577 IPC_MESSAGE_HANDLER(ViewHostMsg_InstantSupportDetermined, |
563 OnInstantSupportDetermined) | 578 OnInstantSupportDetermined) |
564 IPC_MESSAGE_UNHANDLED(handled = false) | 579 IPC_MESSAGE_UNHANDLED(handled = false) |
565 IPC_END_MESSAGE_MAP() | 580 IPC_END_MESSAGE_MAP() |
566 return handled; | 581 return handled; |
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
988 | 1003 |
989 if (preview_contents_.get() && is_showing_instant() && | 1004 if (preview_contents_.get() && is_showing_instant() && |
990 (force_if_waiting || !is_waiting_for_load())) { | 1005 (force_if_waiting || !is_waiting_for_load())) { |
991 last_omnibox_bounds_ = omnibox_bounds_; | 1006 last_omnibox_bounds_ = omnibox_bounds_; |
992 RenderViewHost* host = preview_contents_->render_view_host(); | 1007 RenderViewHost* host = preview_contents_->render_view_host(); |
993 host->Send(new ViewMsg_SearchBoxResize( | 1008 host->Send(new ViewMsg_SearchBoxResize( |
994 host->routing_id(), GetOmniboxBoundsInTermsOfPreview())); | 1009 host->routing_id(), GetOmniboxBoundsInTermsOfPreview())); |
995 } | 1010 } |
996 } | 1011 } |
997 | 1012 |
998 void InstantLoader::CreatePreviewContents(TabContentsWrapper* tab_contents) { | 1013 void InstantLoader::ReplacePreviewContents(TabContentsWrapper* old_tc, |
999 TabContents* new_contents = | 1014 TabContentsWrapper* new_tc) { |
1000 new TabContents( | 1015 DCHECK(old_tc == preview_contents_); |
1001 tab_contents->profile(), NULL, MSG_ROUTING_NONE, NULL, NULL); | 1016 // We release here without deleting so that the caller still has reponsibility |
1002 preview_contents_.reset(new TabContentsWrapper(new_contents)); | 1017 // for deleting the TabContentsWrapper. |
| 1018 ignore_result(preview_contents_.release()); |
| 1019 preview_contents_.reset(new_tc); |
| 1020 |
| 1021 // Make sure the new preview contents acts like the old one. |
| 1022 SetupPreviewContents(old_tc); |
| 1023 |
| 1024 // Cleanup the old preview contents. |
| 1025 old_tc->download_tab_helper()->set_delegate(NULL); |
| 1026 old_tc->tab_contents()->set_delegate(NULL); |
| 1027 old_tc->set_delegate(NULL); |
| 1028 |
| 1029 #if defined(OS_MACOSX) |
| 1030 registrar_.Remove(this, |
| 1031 NotificationType::RENDER_VIEW_HOST_CHANGED, |
| 1032 Source<NavigationController>(&old_tc->controller())); |
| 1033 #endif |
| 1034 registrar_.Remove(this, |
| 1035 NotificationType::NAV_ENTRY_COMMITTED, |
| 1036 Source<NavigationController>(&old_tc->controller())); |
| 1037 |
| 1038 // We prerendered so we should be ready to show. If we're ready, swap in |
| 1039 // immediately, otherwise show the preview as normal. |
| 1040 if (ready_) |
| 1041 delegate_->SwappedTabContents(this); |
| 1042 else |
| 1043 ShowPreview(); |
| 1044 } |
| 1045 |
| 1046 void InstantLoader::SetupPreviewContents(TabContentsWrapper* tab_contents) { |
| 1047 preview_contents_->set_delegate(preview_tab_contents_delegate_.get()); |
| 1048 preview_contents_->tab_contents()->set_delegate( |
| 1049 preview_tab_contents_delegate_.get()); |
1003 preview_contents_->blocked_content_tab_helper()->SetAllContentsBlocked(true); | 1050 preview_contents_->blocked_content_tab_helper()->SetAllContentsBlocked(true); |
1004 preview_tab_contents_delegate_.reset(new TabContentsDelegateImpl(this)); | 1051 |
1005 new_contents->set_delegate(preview_tab_contents_delegate_.get()); | 1052 // Propagate the max page id. That way if we end up merging the two |
| 1053 // NavigationControllers (which happens if we commit) none of the page ids |
| 1054 // will overlap. |
| 1055 int32 max_page_id = tab_contents->tab_contents()->GetMaxPageID(); |
| 1056 if (max_page_id != -1) |
| 1057 preview_contents_->controller().set_max_restored_page_id(max_page_id + 1); |
| 1058 |
1006 preview_contents_->download_tab_helper()->set_delegate( | 1059 preview_contents_->download_tab_helper()->set_delegate( |
1007 preview_tab_contents_delegate_.get()); | 1060 preview_tab_contents_delegate_.get()); |
1008 | 1061 |
1009 gfx::Rect tab_bounds; | |
1010 tab_contents->view()->GetContainerBounds(&tab_bounds); | |
1011 preview_contents_->view()->SizeContents(tab_bounds.size()); | |
1012 | |
1013 #if defined(OS_MACOSX) | 1062 #if defined(OS_MACOSX) |
1014 // If |preview_contents_| does not currently have a RWHV, we will call | 1063 // If |preview_contents_| does not currently have a RWHV, we will call |
1015 // SetTakesFocusOnlyOnMouseDown() as a result of the | 1064 // SetTakesFocusOnlyOnMouseDown() as a result of the |
1016 // RENDER_VIEW_HOST_CHANGED notification. | 1065 // RENDER_VIEW_HOST_CHANGED notification. |
1017 if (preview_contents_->tab_contents()->GetRenderWidgetHostView()) { | 1066 if (preview_contents_->tab_contents()->GetRenderWidgetHostView()) { |
1018 preview_contents_->tab_contents()->GetRenderWidgetHostView()-> | 1067 preview_contents_->tab_contents()->GetRenderWidgetHostView()-> |
1019 SetTakesFocusOnlyOnMouseDown(true); | 1068 SetTakesFocusOnlyOnMouseDown(true); |
1020 } | 1069 } |
1021 registrar_.Add( | 1070 registrar_.Add( |
1022 this, | 1071 this, |
1023 NotificationType::RENDER_VIEW_HOST_CHANGED, | 1072 NotificationType::RENDER_VIEW_HOST_CHANGED, |
1024 Source<NavigationController>(&preview_contents_->controller())); | 1073 Source<NavigationController>(&preview_contents_->controller())); |
1025 #endif | 1074 #endif |
1026 | 1075 |
1027 registrar_.Add( | 1076 registrar_.Add( |
1028 this, | 1077 this, |
1029 NotificationType::NAV_ENTRY_COMMITTED, | 1078 NotificationType::NAV_ENTRY_COMMITTED, |
1030 Source<NavigationController>(&preview_contents_->controller())); | 1079 Source<NavigationController>(&preview_contents_->controller())); |
1031 | 1080 |
| 1081 gfx::Rect tab_bounds; |
| 1082 tab_contents->view()->GetContainerBounds(&tab_bounds); |
| 1083 preview_contents_->view()->SizeContents(tab_bounds.size()); |
| 1084 } |
| 1085 |
| 1086 void InstantLoader::CreatePreviewContents(TabContentsWrapper* tab_contents) { |
| 1087 TabContents* new_contents = |
| 1088 new TabContents( |
| 1089 tab_contents->profile(), NULL, MSG_ROUTING_NONE, NULL, NULL); |
| 1090 preview_contents_.reset(new TabContentsWrapper(new_contents)); |
| 1091 preview_tab_contents_delegate_.reset(new TabContentsDelegateImpl(this)); |
| 1092 SetupPreviewContents(tab_contents); |
| 1093 |
1032 preview_contents_->tab_contents()->ShowContents(); | 1094 preview_contents_->tab_contents()->ShowContents(); |
1033 } | 1095 } |
OLD | NEW |