[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" |
sky | 4bdad24 | 2014-09-18 20:22:20 | [diff] [blame] | 8 | #include "chrome/browser/infobars/infobar_service.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 9 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 10 | #include "chrome/browser/search/instant_service.h" |
| 11 | #include "chrome/browser/search/instant_service_factory.h" |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 12 | #include "chrome/browser/search/search.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 13 | #include "chrome/browser/ui/browser.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | d57ce6a | 2014-07-03 15:39:23 | [diff] [blame] | 15 | #include "chrome/browser/ui/location_bar/location_bar.h" |
[email protected] | 6cf51b6 | 2013-08-10 13:49:22 | [diff] [blame] | 16 | #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 17 | #include "chrome/browser/ui/omnibox/omnibox_view.h" |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 18 | #include "chrome/browser/ui/search/instant_search_prerenderer.h" |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 19 | #include "chrome/browser/ui/search/search_model.h" |
[email protected] | 9d3d1170 | 2012-11-08 01:01:12 | [diff] [blame] | 20 | #include "chrome/browser/ui/search/search_tab_helper.h" |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 21 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
kmadhusu | 562c49c8 | 2014-11-18 02:08:03 | [diff] [blame] | 22 | #include "chrome/common/instant_types.h" |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 23 | #include "chrome/common/url_constants.h" |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 24 | #include "content/public/browser/navigation_controller.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" |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 28 | #include "content/public/common/referrer.h" |
| 29 | #include "ui/base/page_transition_types.h" |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 30 | |
| 31 | // Helpers -------------------------------------------------------------------- |
[email protected] | 233f0f96 | 2013-02-27 21:14:19 | [diff] [blame] | 32 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) { |
| 36 | DCHECK(profile); |
| 37 | InstantService* instant_service = |
| 38 | InstantServiceFactory::GetForProfile(profile); |
| 39 | return instant_service ? instant_service->instant_search_prerenderer() : NULL; |
| 40 | } |
| 41 | |
| 42 | } // namespace |
| 43 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 44 | |
| 45 | // BrowserInstantController --------------------------------------------------- |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 46 | |
| 47 | BrowserInstantController::BrowserInstantController(Browser* browser) |
[email protected] | 8a23670 | 2012-09-28 13:30:57 | [diff] [blame] | 48 | : browser_(browser), |
[email protected] | 77759005 | 2014-01-17 22:11:54 | [diff] [blame] | 49 | instant_(this) { |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 50 | browser_->search_model()->AddObserver(this); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 51 | |
| 52 | InstantService* instant_service = |
| 53 | InstantServiceFactory::GetForProfile(profile()); |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 54 | instant_service->AddObserver(this); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | BrowserInstantController::~BrowserInstantController() { |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 58 | browser_->search_model()->RemoveObserver(this); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 59 | |
| 60 | InstantService* instant_service = |
| 61 | InstantServiceFactory::GetForProfile(profile()); |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 62 | instant_service->RemoveObserver(this); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 65 | bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition, |
| 66 | const GURL& url) { |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 67 | // Unsupported dispositions. |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 68 | if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW || |
| 69 | disposition == NEW_FOREGROUND_TAB) |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 70 | return false; |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 71 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 72 | // The omnibox currently doesn't use other dispositions, so we don't attempt |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 73 | // 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] | 74 | // support for the new disposition. |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 75 | DCHECK(disposition == CURRENT_TAB) << disposition; |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 76 | |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 77 | const base::string16& search_terms = |
sdefresne | 51bbec7b | 2015-08-03 14:18:13 | [diff] [blame^] | 78 | search::ExtractSearchTermsFromURL(profile(), url); |
kmadhusu | 562c49c8 | 2014-11-18 02:08:03 | [diff] [blame] | 79 | EmbeddedSearchRequestParams request_params(url); |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 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. |
kmadhusu | 562c49c8 | 2014-11-18 02:08:03 | [diff] [blame] | 89 | prerenderer->Commit(search_terms, request_params); |
[email protected] | f028cf5b | 2014-01-24 07:36:03 | [diff] [blame] | 90 | return false; |
| 91 | } else { |
| 92 | prerenderer->Cancel(); |
| 93 | } |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 50da190 | 2014-02-14 02:40:19 | [diff] [blame] | 96 | // If we will not be replacing search terms from this URL, don't send to |
| 97 | // InstantController. |
sdefresne | 51bbec7b | 2015-08-03 14:18:13 | [diff] [blame^] | 98 | if (!search::IsQueryExtractionAllowedForURL(profile(), url)) |
[email protected] | 50da190 | 2014-02-14 02:40:19 | [diff] [blame] | 99 | return false; |
kmadhusu | 562c49c8 | 2014-11-18 02:08:03 | [diff] [blame] | 100 | return instant_.SubmitQuery(search_terms, request_params); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 103 | Profile* BrowserInstantController::profile() const { |
| 104 | return browser_->profile(); |
| 105 | } |
| 106 | |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 107 | content::WebContents* BrowserInstantController::GetActiveWebContents() const { |
| 108 | return browser_->tab_strip_model()->GetActiveWebContents(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 111 | void BrowserInstantController::ActiveTabChanged() { |
| 112 | instant_.ActiveTabChanged(); |
| 113 | } |
| 114 | |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 115 | void BrowserInstantController::TabDeactivated(content::WebContents* contents) { |
| 116 | instant_.TabDeactivated(contents); |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 117 | |
| 118 | InstantSearchPrerenderer* prerenderer = |
| 119 | GetInstantSearchPrerenderer(profile()); |
| 120 | if (prerenderer) |
| 121 | prerenderer->Cancel(); |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 122 | } |
| 123 | |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 124 | void BrowserInstantController::ModelChanged( |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 125 | const SearchModel::State& old_state, |
| 126 | const SearchModel::State& new_state) { |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 127 | if (old_state.mode != new_state.mode) { |
| 128 | const SearchMode& new_mode = new_state.mode; |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 129 | |
[email protected] | a780c7b2 | 2013-08-02 18:36:59 | [diff] [blame] | 130 | // Record some actions corresponding to the mode change. Note that to get |
| 131 | // the full story, it's necessary to look at other UMA actions as well, |
| 132 | // such as tab switches. |
| 133 | if (new_mode.is_search_results()) |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 134 | content::RecordAction(base::UserMetricsAction("InstantExtended.ShowSRP")); |
[email protected] | a780c7b2 | 2013-08-02 18:36:59 | [diff] [blame] | 135 | else if (new_mode.is_ntp()) |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 136 | content::RecordAction(base::UserMetricsAction("InstantExtended.ShowNTP")); |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 137 | |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 138 | instant_.SearchModeChanged(old_state.mode, new_mode); |
[email protected] | c19ba104 | 2013-03-11 17:17:13 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 141 | if (old_state.instant_support != new_state.instant_support) |
| 142 | instant_.InstantSupportChanged(new_state.instant_support); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 143 | } |
| 144 | |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 145 | void BrowserInstantController::DefaultSearchProviderChanged( |
| 146 | bool google_base_url_domain_changed) { |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 147 | InstantService* instant_service = |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 148 | InstantServiceFactory::GetForProfile(profile()); |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 149 | if (!instant_service) |
| 150 | return; |
| 151 | |
| 152 | TabStripModel* tab_model = browser_->tab_strip_model(); |
| 153 | int count = tab_model->count(); |
| 154 | for (int index = 0; index < count; ++index) { |
| 155 | content::WebContents* contents = tab_model->GetWebContentsAt(index); |
| 156 | if (!contents) |
| 157 | continue; |
| 158 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 159 | // Send new search URLs to the renderer. |
| 160 | content::RenderProcessHost* rph = contents->GetRenderProcessHost(); |
| 161 | instant_service->SendSearchURLsToRenderer(rph); |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 162 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 163 | if (!instant_service->IsInstantProcess(rph->GetID())) |
| 164 | continue; |
sky | 4bdad24 | 2014-09-18 20:22:20 | [diff] [blame] | 165 | |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 166 | if (google_base_url_domain_changed && |
| 167 | SearchTabHelper::FromWebContents(contents)->model()->mode().is_ntp()) { |
| 168 | // Replace the server NTP with the local NTP. |
sdefresne | 51bbec7b | 2015-08-03 14:18:13 | [diff] [blame^] | 169 | content::NavigationController::LoadURLParams params( |
| 170 | search::GetLocalInstantURL(profile())); |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 171 | params.should_replace_current_entry = true; |
| 172 | params.referrer = content::Referrer(); |
| 173 | params.transition_type = ui::PAGE_TRANSITION_RELOAD; |
| 174 | contents->GetController().LoadURLWithParams(params); |
| 175 | } else { |
| 176 | // Reload the contents to ensure that it gets assigned to a |
| 177 | // non-priviledged renderer. |
| 178 | contents->GetController().Reload(false); |
sky | 4bdad24 | 2014-09-18 20:22:20 | [diff] [blame] | 179 | |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 180 | // As the reload was not triggered by the user we don't want to close any |
| 181 | // infobars. We have to tell the InfoBarService after the reload, |
| 182 | // otherwise it would ignore this call when |
| 183 | // WebContentsObserver::DidStartNavigationToPendingEntry is invoked. |
| 184 | InfoBarService::FromWebContents(contents)->set_ignore_next_reload(); |
| 185 | } |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 186 | } |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 187 | } |