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/instant/instant_loader_delegate.h" | 20 #include "chrome/browser/instant/instant_loader_delegate.h" |
21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/search_engines/template_url.h" | 22 #include "chrome/browser/search_engines/template_url.h" |
23 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" | 23 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" |
24 #include "chrome/browser/ui/download/download_tab_helper.h" | |
25 #include "chrome/browser/ui/download/download_tab_helper_delegate.h" | |
26 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 24 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
27 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
28 #include "chrome/common/render_messages.h" | 26 #include "chrome/common/render_messages.h" |
29 #include "content/browser/renderer_host/render_view_host.h" | 27 #include "content/browser/renderer_host/render_view_host.h" |
30 #include "content/browser/renderer_host/render_widget_host.h" | 28 #include "content/browser/renderer_host/render_widget_host.h" |
31 #include "content/browser/renderer_host/render_widget_host_view.h" | 29 #include "content/browser/renderer_host/render_widget_host_view.h" |
32 #include "content/browser/tab_contents/navigation_controller.h" | 30 #include "content/browser/tab_contents/navigation_controller.h" |
33 #include "content/browser/tab_contents/navigation_entry.h" | 31 #include "content/browser/tab_contents/navigation_entry.h" |
34 #include "content/browser/tab_contents/provisional_load_details.h" | 32 #include "content/browser/tab_contents/provisional_load_details.h" |
35 #include "content/browser/tab_contents/tab_contents.h" | 33 #include "content/browser/tab_contents/tab_contents.h" |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 NOTREACHED(); | 135 NOTREACHED(); |
138 break; | 136 break; |
139 } | 137 } |
140 } | 138 } |
141 | 139 |
142 // TabContentsDelegateImpl ----------------------------------------------------- | 140 // TabContentsDelegateImpl ----------------------------------------------------- |
143 | 141 |
144 class InstantLoader::TabContentsDelegateImpl | 142 class InstantLoader::TabContentsDelegateImpl |
145 : public TabContentsDelegate, | 143 : public TabContentsDelegate, |
146 public NotificationObserver, | 144 public NotificationObserver, |
147 public TabContentsObserver, | 145 public TabContentsObserver { |
148 public DownloadTabHelperDelegate { | |
149 public: | 146 public: |
150 explicit TabContentsDelegateImpl(InstantLoader* loader); | 147 explicit TabContentsDelegateImpl(InstantLoader* loader); |
151 | 148 |
152 // Invoked prior to loading a new URL. | 149 // Invoked prior to loading a new URL. |
153 void PrepareForNewLoad(); | 150 void PrepareForNewLoad(); |
154 | 151 |
155 // Invoked when the preview paints. Invokes PreviewPainted on the loader. | 152 // Invoked when the preview paints. Invokes PreviewPainted on the loader. |
156 void PreviewPainted(); | 153 void PreviewPainted(); |
157 | 154 |
158 bool is_mouse_down_from_activate() const { | 155 bool is_mouse_down_from_activate() const { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 virtual void BeforeUnloadFired(TabContents* tab, | 201 virtual void BeforeUnloadFired(TabContents* tab, |
205 bool proceed, | 202 bool proceed, |
206 bool* proceed_to_fire_unload) OVERRIDE; | 203 bool* proceed_to_fire_unload) OVERRIDE; |
207 virtual void SetFocusToLocationBar(bool select_all) OVERRIDE; | 204 virtual void SetFocusToLocationBar(bool select_all) OVERRIDE; |
208 virtual bool ShouldFocusPageAfterCrash() OVERRIDE; | 205 virtual bool ShouldFocusPageAfterCrash() OVERRIDE; |
209 virtual void LostCapture() OVERRIDE; | 206 virtual void LostCapture() OVERRIDE; |
210 // If the user drags, we won't get a mouse up (at least on Linux). Commit the | 207 // If the user drags, we won't get a mouse up (at least on Linux). Commit the |
211 // instant result when the drag ends, so that during the drag the page won't | 208 // instant result when the drag ends, so that during the drag the page won't |
212 // move around. | 209 // move around. |
213 virtual void DragEnded() OVERRIDE; | 210 virtual void DragEnded() OVERRIDE; |
| 211 virtual bool CanDownload(int request_id) OVERRIDE; |
214 virtual void HandleMouseUp() OVERRIDE; | 212 virtual void HandleMouseUp() OVERRIDE; |
215 virtual void HandleMouseActivate() OVERRIDE; | 213 virtual void HandleMouseActivate() OVERRIDE; |
216 virtual bool OnGoToEntryOffset(int offset) OVERRIDE; | 214 virtual bool OnGoToEntryOffset(int offset) OVERRIDE; |
217 virtual bool ShouldAddNavigationToHistory( | 215 virtual bool ShouldAddNavigationToHistory( |
218 const history::HistoryAddPageArgs& add_page_args, | 216 const history::HistoryAddPageArgs& add_page_args, |
219 NavigationType::Type navigation_type) OVERRIDE; | 217 NavigationType::Type navigation_type) OVERRIDE; |
220 virtual bool ShouldShowHungRendererDialog() OVERRIDE; | 218 virtual bool ShouldShowHungRendererDialog() OVERRIDE; |
221 | 219 |
222 // TabContentsObserver: | 220 // TabContentsObserver: |
223 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 221 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
224 | 222 |
225 // DownloadTabHelperDelegate: | |
226 virtual bool CanDownload(int request_id) OVERRIDE; | |
227 virtual void OnStartDownload(DownloadItem* download, | |
228 TabContentsWrapper* tab) OVERRIDE; | |
229 | |
230 private: | 223 private: |
231 typedef std::vector<scoped_refptr<history::HistoryAddPageArgs> > | 224 typedef std::vector<scoped_refptr<history::HistoryAddPageArgs> > |
232 AddPageVector; | 225 AddPageVector; |
233 | 226 |
234 // Message from renderer indicating the page has suggestions. | 227 // Message from renderer indicating the page has suggestions. |
235 void OnSetSuggestions( | 228 void OnSetSuggestions( |
236 int32 page_id, | 229 int32 page_id, |
237 const std::vector<std::string>& suggestions, | 230 const std::vector<std::string>& suggestions, |
238 InstantCompleteBehavior behavior); | 231 InstantCompleteBehavior behavior); |
239 | 232 |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 } | 504 } |
512 | 505 |
513 void InstantLoader::TabContentsDelegateImpl::LostCapture() { | 506 void InstantLoader::TabContentsDelegateImpl::LostCapture() { |
514 CommitFromMouseReleaseIfNecessary(); | 507 CommitFromMouseReleaseIfNecessary(); |
515 } | 508 } |
516 | 509 |
517 void InstantLoader::TabContentsDelegateImpl::DragEnded() { | 510 void InstantLoader::TabContentsDelegateImpl::DragEnded() { |
518 CommitFromMouseReleaseIfNecessary(); | 511 CommitFromMouseReleaseIfNecessary(); |
519 } | 512 } |
520 | 513 |
| 514 bool InstantLoader::TabContentsDelegateImpl::CanDownload(int request_id) { |
| 515 // Downloads are disabled. |
| 516 return false; |
| 517 } |
| 518 |
521 void InstantLoader::TabContentsDelegateImpl::HandleMouseUp() { | 519 void InstantLoader::TabContentsDelegateImpl::HandleMouseUp() { |
522 CommitFromMouseReleaseIfNecessary(); | 520 CommitFromMouseReleaseIfNecessary(); |
523 } | 521 } |
524 | 522 |
525 void InstantLoader::TabContentsDelegateImpl::HandleMouseActivate() { | 523 void InstantLoader::TabContentsDelegateImpl::HandleMouseActivate() { |
526 is_mouse_down_from_activate_ = true; | 524 is_mouse_down_from_activate_ = true; |
527 } | 525 } |
528 | 526 |
529 bool InstantLoader::TabContentsDelegateImpl::OnGoToEntryOffset(int offset) { | 527 bool InstantLoader::TabContentsDelegateImpl::OnGoToEntryOffset(int offset) { |
530 return false; | 528 return false; |
(...skipping 24 matching lines...) Expand all Loading... |
555 bool handled = true; | 553 bool handled = true; |
556 IPC_BEGIN_MESSAGE_MAP(TabContentsDelegateImpl, message) | 554 IPC_BEGIN_MESSAGE_MAP(TabContentsDelegateImpl, message) |
557 IPC_MESSAGE_HANDLER(ViewHostMsg_SetSuggestions, OnSetSuggestions) | 555 IPC_MESSAGE_HANDLER(ViewHostMsg_SetSuggestions, OnSetSuggestions) |
558 IPC_MESSAGE_HANDLER(ViewHostMsg_InstantSupportDetermined, | 556 IPC_MESSAGE_HANDLER(ViewHostMsg_InstantSupportDetermined, |
559 OnInstantSupportDetermined) | 557 OnInstantSupportDetermined) |
560 IPC_MESSAGE_UNHANDLED(handled = false) | 558 IPC_MESSAGE_UNHANDLED(handled = false) |
561 IPC_END_MESSAGE_MAP() | 559 IPC_END_MESSAGE_MAP() |
562 return handled; | 560 return handled; |
563 } | 561 } |
564 | 562 |
565 bool InstantLoader::TabContentsDelegateImpl::CanDownload(int request_id) { | |
566 // Downloads are disabled. | |
567 return false; | |
568 } | |
569 | |
570 void InstantLoader::TabContentsDelegateImpl::OnStartDownload( | |
571 DownloadItem* download, TabContentsWrapper* tab) { | |
572 // Downloads are disabled. | |
573 } | |
574 | |
575 void InstantLoader::TabContentsDelegateImpl::OnSetSuggestions( | 563 void InstantLoader::TabContentsDelegateImpl::OnSetSuggestions( |
576 int32 page_id, | 564 int32 page_id, |
577 const std::vector<std::string>& suggestions, | 565 const std::vector<std::string>& suggestions, |
578 InstantCompleteBehavior behavior) { | 566 InstantCompleteBehavior behavior) { |
579 TabContentsWrapper* source = loader_->preview_contents(); | 567 TabContentsWrapper* source = loader_->preview_contents(); |
580 if (!source->controller().GetActiveEntry() || | 568 if (!source->controller().GetActiveEntry() || |
581 page_id != source->controller().GetActiveEntry()->page_id()) | 569 page_id != source->controller().GetActiveEntry()->page_id()) |
582 return; | 570 return; |
583 | 571 |
584 if (suggestions.empty()) | 572 if (suggestions.empty()) |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
815 #if defined(OS_MACOSX) | 803 #if defined(OS_MACOSX) |
816 preview_contents_->tab_contents()->GetRenderWidgetHostView()-> | 804 preview_contents_->tab_contents()->GetRenderWidgetHostView()-> |
817 SetTakesFocusOnlyOnMouseDown(false); | 805 SetTakesFocusOnlyOnMouseDown(false); |
818 registrar_.Remove( | 806 registrar_.Remove( |
819 this, | 807 this, |
820 NotificationType::RENDER_VIEW_HOST_CHANGED, | 808 NotificationType::RENDER_VIEW_HOST_CHANGED, |
821 Source<NavigationController>(&preview_contents_->controller())); | 809 Source<NavigationController>(&preview_contents_->controller())); |
822 #endif | 810 #endif |
823 } | 811 } |
824 preview_contents_->tab_contents()->set_delegate(NULL); | 812 preview_contents_->tab_contents()->set_delegate(NULL); |
825 preview_contents_->download_tab_helper()->set_delegate(NULL); | |
826 ready_ = false; | 813 ready_ = false; |
827 } | 814 } |
828 update_bounds_timer_.Stop(); | 815 update_bounds_timer_.Stop(); |
829 return preview_contents_.release(); | 816 return preview_contents_.release(); |
830 } | 817 } |
831 | 818 |
832 bool InstantLoader::ShouldCommitInstantOnMouseUp() { | 819 bool InstantLoader::ShouldCommitInstantOnMouseUp() { |
833 return delegate_->ShouldCommitInstantOnMouseUp(); | 820 return delegate_->ShouldCommitInstantOnMouseUp(); |
834 } | 821 } |
835 | 822 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
978 SendBoundsToPage(false); | 965 SendBoundsToPage(false); |
979 } | 966 } |
980 | 967 |
981 void InstantLoader::SendBoundsToPage(bool force_if_waiting) { | 968 void InstantLoader::SendBoundsToPage(bool force_if_waiting) { |
982 if (last_omnibox_bounds_ == omnibox_bounds_) | 969 if (last_omnibox_bounds_ == omnibox_bounds_) |
983 return; | 970 return; |
984 | 971 |
985 if (preview_contents_.get() && is_showing_instant() && | 972 if (preview_contents_.get() && is_showing_instant() && |
986 (force_if_waiting || !is_waiting_for_load())) { | 973 (force_if_waiting || !is_waiting_for_load())) { |
987 last_omnibox_bounds_ = omnibox_bounds_; | 974 last_omnibox_bounds_ = omnibox_bounds_; |
988 RenderViewHost* host = preview_contents_->render_view_host(); | 975 RenderViewHost*host = preview_contents_->render_view_host(); |
989 host->Send(new ViewMsg_SearchBoxResize( | 976 host->Send(new ViewMsg_SearchBoxResize( |
990 host->routing_id(), GetOmniboxBoundsInTermsOfPreview())); | 977 host->routing_id(), GetOmniboxBoundsInTermsOfPreview())); |
991 } | 978 } |
992 } | 979 } |
993 | 980 |
994 void InstantLoader::CreatePreviewContents(TabContentsWrapper* tab_contents) { | 981 void InstantLoader::CreatePreviewContents(TabContentsWrapper* tab_contents) { |
995 TabContents* new_contents = | 982 TabContents* new_contents = |
996 new TabContents( | 983 new TabContents( |
997 tab_contents->profile(), NULL, MSG_ROUTING_NONE, NULL, NULL); | 984 tab_contents->profile(), NULL, MSG_ROUTING_NONE, NULL, NULL); |
998 preview_contents_.reset(new TabContentsWrapper(new_contents)); | 985 preview_contents_.reset(new TabContentsWrapper(new_contents)); |
999 preview_contents_->blocked_content_tab_helper()->SetAllContentsBlocked(true); | 986 preview_contents_->blocked_content_tab_helper()->SetAllContentsBlocked(true); |
1000 // Propagate the max page id. That way if we end up merging the two | 987 // Propagate the max page id. That way if we end up merging the two |
1001 // NavigationControllers (which happens if we commit) none of the page ids | 988 // NavigationControllers (which happens if we commit) none of the page ids |
1002 // will overlap. | 989 // will overlap. |
1003 int32 max_page_id = tab_contents->tab_contents()->GetMaxPageID(); | 990 int32 max_page_id = tab_contents->tab_contents()->GetMaxPageID(); |
1004 if (max_page_id != -1) | 991 if (max_page_id != -1) |
1005 preview_contents_->controller().set_max_restored_page_id(max_page_id + 1); | 992 preview_contents_->controller().set_max_restored_page_id(max_page_id + 1); |
1006 | 993 |
1007 preview_tab_contents_delegate_.reset(new TabContentsDelegateImpl(this)); | 994 preview_tab_contents_delegate_.reset(new TabContentsDelegateImpl(this)); |
1008 new_contents->set_delegate(preview_tab_contents_delegate_.get()); | 995 new_contents->set_delegate(preview_tab_contents_delegate_.get()); |
1009 preview_contents_->download_tab_helper()->set_delegate( | |
1010 preview_tab_contents_delegate_.get()); | |
1011 | 996 |
1012 gfx::Rect tab_bounds; | 997 gfx::Rect tab_bounds; |
1013 tab_contents->view()->GetContainerBounds(&tab_bounds); | 998 tab_contents->view()->GetContainerBounds(&tab_bounds); |
1014 preview_contents_->view()->SizeContents(tab_bounds.size()); | 999 preview_contents_->view()->SizeContents(tab_bounds.size()); |
1015 | 1000 |
1016 #if defined(OS_MACOSX) | 1001 #if defined(OS_MACOSX) |
1017 // If |preview_contents_| does not currently have a RWHV, we will call | 1002 // If |preview_contents_| does not currently have a RWHV, we will call |
1018 // SetTakesFocusOnlyOnMouseDown() as a result of the | 1003 // SetTakesFocusOnlyOnMouseDown() as a result of the |
1019 // RENDER_VIEW_HOST_CHANGED notification. | 1004 // RENDER_VIEW_HOST_CHANGED notification. |
1020 if (preview_contents_->tab_contents()->GetRenderWidgetHostView()) { | 1005 if (preview_contents_->tab_contents()->GetRenderWidgetHostView()) { |
1021 preview_contents_->tab_contents()->GetRenderWidgetHostView()-> | 1006 preview_contents_->tab_contents()->GetRenderWidgetHostView()-> |
1022 SetTakesFocusOnlyOnMouseDown(true); | 1007 SetTakesFocusOnlyOnMouseDown(true); |
1023 } | 1008 } |
1024 registrar_.Add( | 1009 registrar_.Add( |
1025 this, | 1010 this, |
1026 NotificationType::RENDER_VIEW_HOST_CHANGED, | 1011 NotificationType::RENDER_VIEW_HOST_CHANGED, |
1027 Source<NavigationController>(&preview_contents_->controller())); | 1012 Source<NavigationController>(&preview_contents_->controller())); |
1028 #endif | 1013 #endif |
1029 | 1014 |
1030 registrar_.Add( | 1015 registrar_.Add( |
1031 this, | 1016 this, |
1032 NotificationType::NAV_ENTRY_COMMITTED, | 1017 NotificationType::NAV_ENTRY_COMMITTED, |
1033 Source<NavigationController>(&preview_contents_->controller())); | 1018 Source<NavigationController>(&preview_contents_->controller())); |
1034 | 1019 |
1035 preview_contents_->tab_contents()->ShowContents(); | 1020 preview_contents_->tab_contents()->ShowContents(); |
1036 } | 1021 } |
OLD | NEW |