[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] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 76 | const base::string16& search_terms = |
[email protected] | 50da190 | 2014-02-14 02:40:19 | [diff] [blame^] | 77 | chrome::ExtractSearchTermsFromURL(profile(), url); |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 78 | if (search_terms.empty()) |
| 79 | return false; |
| 80 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 81 | InstantSearchPrerenderer* prerenderer = |
| 82 | GetInstantSearchPrerenderer(profile()); |
[email protected] | f028cf5b | 2014-01-24 07:36:03 | [diff] [blame] | 83 | if (prerenderer) { |
| 84 | if (prerenderer->CanCommitQuery(GetActiveWebContents(), search_terms)) { |
| 85 | // Submit query to render the prefetched results. Browser will swap the |
| 86 | // prerendered contents with the active tab contents. |
| 87 | prerenderer->Commit(search_terms); |
| 88 | return false; |
| 89 | } else { |
| 90 | prerenderer->Cancel(); |
| 91 | } |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 92 | } |
| 93 | |
[email protected] | 50da190 | 2014-02-14 02:40:19 | [diff] [blame^] | 94 | // If we will not be replacing search terms from this URL, don't send to |
| 95 | // InstantController. |
| 96 | if (!chrome::IsQueryExtractionAllowedForURL(profile(), url)) |
| 97 | return false; |
| 98 | |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 99 | return instant_.SubmitQuery(search_terms); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 102 | Profile* BrowserInstantController::profile() const { |
| 103 | return browser_->profile(); |
| 104 | } |
| 105 | |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 106 | content::WebContents* BrowserInstantController::GetActiveWebContents() const { |
| 107 | return browser_->tab_strip_model()->GetActiveWebContents(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 110 | void BrowserInstantController::ActiveTabChanged() { |
| 111 | instant_.ActiveTabChanged(); |
| 112 | } |
| 113 | |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 114 | void BrowserInstantController::TabDeactivated(content::WebContents* contents) { |
| 115 | instant_.TabDeactivated(contents); |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 116 | |
| 117 | InstantSearchPrerenderer* prerenderer = |
| 118 | GetInstantSearchPrerenderer(profile()); |
| 119 | if (prerenderer) |
| 120 | prerenderer->Cancel(); |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 123 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 124 | // BrowserInstantController, SearchModelObserver implementation: |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 125 | |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 126 | void BrowserInstantController::ModelChanged( |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 127 | const SearchModel::State& old_state, |
| 128 | const SearchModel::State& new_state) { |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 129 | if (old_state.mode != new_state.mode) { |
| 130 | const SearchMode& new_mode = new_state.mode; |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 131 | |
[email protected] | a780c7b2 | 2013-08-02 18:36:59 | [diff] [blame] | 132 | // Record some actions corresponding to the mode change. Note that to get |
| 133 | // the full story, it's necessary to look at other UMA actions as well, |
| 134 | // such as tab switches. |
| 135 | if (new_mode.is_search_results()) |
| 136 | content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP")); |
| 137 | else if (new_mode.is_ntp()) |
| 138 | content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP")); |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 139 | |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 140 | instant_.SearchModeChanged(old_state.mode, new_mode); |
[email protected] | c19ba104 | 2013-03-11 17:17:13 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 143 | if (old_state.instant_support != new_state.instant_support) |
| 144 | instant_.InstantSupportChanged(new_state.instant_support); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 147 | //////////////////////////////////////////////////////////////////////////////// |
| 148 | // BrowserInstantController, InstantServiceObserver implementation: |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 149 | |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 150 | void BrowserInstantController::DefaultSearchProviderChanged() { |
| 151 | ReloadTabsInInstantProcess(); |
| 152 | } |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 153 | |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 154 | void BrowserInstantController::GoogleURLUpdated() { |
| 155 | ReloadTabsInInstantProcess(); |
| 156 | } |
| 157 | |
| 158 | void BrowserInstantController::ReloadTabsInInstantProcess() { |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 159 | InstantService* instant_service = |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 160 | InstantServiceFactory::GetForProfile(profile()); |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 161 | if (!instant_service) |
| 162 | return; |
| 163 | |
| 164 | TabStripModel* tab_model = browser_->tab_strip_model(); |
| 165 | int count = tab_model->count(); |
| 166 | for (int index = 0; index < count; ++index) { |
| 167 | content::WebContents* contents = tab_model->GetWebContentsAt(index); |
| 168 | if (!contents) |
| 169 | continue; |
| 170 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 171 | // Send new search URLs to the renderer. |
| 172 | content::RenderProcessHost* rph = contents->GetRenderProcessHost(); |
| 173 | instant_service->SendSearchURLsToRenderer(rph); |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 174 | |
| 175 | // Reload the contents to ensure that it gets assigned to a non-priviledged |
| 176 | // renderer. |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 177 | if (!instant_service->IsInstantProcess(rph->GetID())) |
| 178 | continue; |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 179 | contents->GetController().Reload(false); |
| 180 | } |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 181 | } |