[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/ui/browser_instant_controller.h" |
| 6 | |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 8 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 26015ac | 2013-02-27 02:50:13 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_web_ui.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 10 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 11 | #include "chrome/browser/search/instant_service.h" |
| 12 | #include "chrome/browser/search/instant_service_factory.h" |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 13 | #include "chrome/browser/search/search.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser_window.h" |
| 16 | #include "chrome/browser/ui/omnibox/location_bar.h" |
[email protected] | 6cf51b6 | 2013-08-10 13:49:22 | [diff] [blame] | 17 | #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 18 | #include "chrome/browser/ui/omnibox/omnibox_view.h" |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 19 | #include "chrome/browser/ui/search/instant_search_prerenderer.h" |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 20 | #include "chrome/browser/ui/search/search_model.h" |
[email protected] | 9d3d1170 | 2012-11-08 01:01:12 | [diff] [blame] | 21 | #include "chrome/browser/ui/search/search_tab_helper.h" |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 22 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 23 | #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 24 | #include "chrome/common/url_constants.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 25 | #include "content/public/browser/render_process_host.h" |
[email protected] | 233f0f96 | 2013-02-27 21:14:19 | [diff] [blame] | 26 | #include "content/public/browser/user_metrics.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 27 | #include "content/public/browser/web_contents.h" |
[email protected] | 6cf51b6 | 2013-08-10 13:49:22 | [diff] [blame] | 28 | #include "content/public/browser/web_contents_view.h" |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 29 | |
[email protected] | e6e30ac | 2014-01-13 21:24:39 | [diff] [blame] | 30 | using base::UserMetricsAction; |
[email protected] | 233f0f96 | 2013-02-27 21:14:19 | [diff] [blame] | 31 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) { |
| 35 | DCHECK(profile); |
| 36 | InstantService* instant_service = |
| 37 | InstantServiceFactory::GetForProfile(profile); |
| 38 | return instant_service ? instant_service->instant_search_prerenderer() : NULL; |
| 39 | } |
| 40 | |
| 41 | } // namespace |
| 42 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 43 | //////////////////////////////////////////////////////////////////////////////// |
| 44 | // BrowserInstantController, public: |
| 45 | |
| 46 | BrowserInstantController::BrowserInstantController(Browser* browser) |
[email protected] | 8a23670 | 2012-09-28 13:30:57 | [diff] [blame] | 47 | : browser_(browser), |
[email protected] | 77759005 | 2014-01-17 22:11:54 | [diff] [blame] | 48 | instant_(this) { |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 49 | browser_->search_model()->AddObserver(this); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 50 | |
| 51 | InstantService* instant_service = |
| 52 | InstantServiceFactory::GetForProfile(profile()); |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 53 | instant_service->AddObserver(this); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | BrowserInstantController::~BrowserInstantController() { |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 57 | browser_->search_model()->RemoveObserver(this); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 58 | |
| 59 | InstantService* instant_service = |
| 60 | InstantServiceFactory::GetForProfile(profile()); |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 61 | instant_service->RemoveObserver(this); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 64 | bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition, |
| 65 | const GURL& url) { |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 66 | // Unsupported dispositions. |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 67 | if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW || |
| 68 | disposition == NEW_FOREGROUND_TAB) |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 69 | return false; |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 70 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 71 | // The omnibox currently doesn't use other dispositions, so we don't attempt |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 72 | // to handle them. If you hit this DCHECK file a bug and I'll (sky) add |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 73 | // support for the new disposition. |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 74 | DCHECK(disposition == CURRENT_TAB) << disposition; |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 75 | |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 76 | // If we will not be replacing search terms from this URL, don't send to |
| 77 | // InstantController. |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 78 | const base::string16& search_terms = |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 79 | chrome::GetSearchTermsFromURL(browser_->profile(), url); |
| 80 | if (search_terms.empty()) |
| 81 | return false; |
| 82 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 83 | InstantSearchPrerenderer* prerenderer = |
| 84 | GetInstantSearchPrerenderer(profile()); |
[email protected] | f028cf5b | 2014-01-24 07:36:03 | [diff] [blame^] | 85 | if (prerenderer) { |
| 86 | if (prerenderer->CanCommitQuery(GetActiveWebContents(), search_terms)) { |
| 87 | // Submit query to render the prefetched results. Browser will swap the |
| 88 | // prerendered contents with the active tab contents. |
| 89 | prerenderer->Commit(search_terms); |
| 90 | return false; |
| 91 | } else { |
| 92 | prerenderer->Cancel(); |
| 93 | } |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 96 | return instant_.SubmitQuery(search_terms); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 99 | Profile* BrowserInstantController::profile() const { |
| 100 | return browser_->profile(); |
| 101 | } |
| 102 | |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 103 | content::WebContents* BrowserInstantController::GetActiveWebContents() const { |
| 104 | return browser_->tab_strip_model()->GetActiveWebContents(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 107 | void BrowserInstantController::ActiveTabChanged() { |
| 108 | instant_.ActiveTabChanged(); |
| 109 | } |
| 110 | |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 111 | void BrowserInstantController::TabDeactivated(content::WebContents* contents) { |
| 112 | instant_.TabDeactivated(contents); |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 113 | |
| 114 | InstantSearchPrerenderer* prerenderer = |
| 115 | GetInstantSearchPrerenderer(profile()); |
| 116 | if (prerenderer) |
| 117 | prerenderer->Cancel(); |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | fcde79a | 2013-02-28 02:25:09 | [diff] [blame] | 120 | void BrowserInstantController::SetOmniboxBounds(const gfx::Rect& bounds) { |
| 121 | instant_.SetOmniboxBounds(bounds); |
[email protected] | ec4aad54 | 2012-12-14 01:11:04 | [diff] [blame] | 122 | } |
| 123 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 124 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 125 | // BrowserInstantController, SearchModelObserver implementation: |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 126 | |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 127 | void BrowserInstantController::ModelChanged( |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 128 | const SearchModel::State& old_state, |
| 129 | const SearchModel::State& new_state) { |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 130 | if (old_state.mode != new_state.mode) { |
| 131 | const SearchMode& new_mode = new_state.mode; |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 132 | |
[email protected] | a780c7b2 | 2013-08-02 18:36:59 | [diff] [blame] | 133 | // Record some actions corresponding to the mode change. Note that to get |
| 134 | // the full story, it's necessary to look at other UMA actions as well, |
| 135 | // such as tab switches. |
| 136 | if (new_mode.is_search_results()) |
| 137 | content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP")); |
| 138 | else if (new_mode.is_ntp()) |
| 139 | content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP")); |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 140 | |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 141 | instant_.SearchModeChanged(old_state.mode, new_mode); |
[email protected] | c19ba104 | 2013-03-11 17:17:13 | [diff] [blame] | 142 | } |
| 143 | |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 144 | if (old_state.instant_support != new_state.instant_support) |
| 145 | instant_.InstantSupportChanged(new_state.instant_support); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 146 | } |
| 147 | |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 148 | //////////////////////////////////////////////////////////////////////////////// |
| 149 | // BrowserInstantController, InstantServiceObserver implementation: |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 150 | |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 151 | void BrowserInstantController::DefaultSearchProviderChanged() { |
| 152 | ReloadTabsInInstantProcess(); |
| 153 | } |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 154 | |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 155 | void BrowserInstantController::GoogleURLUpdated() { |
| 156 | ReloadTabsInInstantProcess(); |
| 157 | } |
| 158 | |
| 159 | void BrowserInstantController::ReloadTabsInInstantProcess() { |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 160 | InstantService* instant_service = |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 161 | InstantServiceFactory::GetForProfile(profile()); |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 162 | if (!instant_service) |
| 163 | return; |
| 164 | |
| 165 | TabStripModel* tab_model = browser_->tab_strip_model(); |
| 166 | int count = tab_model->count(); |
| 167 | for (int index = 0; index < count; ++index) { |
| 168 | content::WebContents* contents = tab_model->GetWebContentsAt(index); |
| 169 | if (!contents) |
| 170 | continue; |
| 171 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 172 | // Send new search URLs to the renderer. |
| 173 | content::RenderProcessHost* rph = contents->GetRenderProcessHost(); |
| 174 | instant_service->SendSearchURLsToRenderer(rph); |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 175 | |
| 176 | // Reload the contents to ensure that it gets assigned to a non-priviledged |
| 177 | // renderer. |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 178 | if (!instant_service->IsInstantProcess(rph->GetID())) |
| 179 | continue; |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 180 | contents->GetController().Reload(false); |
| 181 | } |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 182 | } |