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