[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] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 16 | #include "chrome/browser/ui/search/instant_search_prerenderer.h" |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 17 | #include "chrome/browser/ui/search/search_model.h" |
[email protected] | 9d3d1170 | 2012-11-08 01:01:12 | [diff] [blame] | 18 | #include "chrome/browser/ui/search/search_tab_helper.h" |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 19 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
kmadhusu | 562c49c8 | 2014-11-18 02:08:03 | [diff] [blame] | 20 | #include "chrome/common/instant_types.h" |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 21 | #include "chrome/common/url_constants.h" |
blundell | 7dbd379 | 2015-08-05 15:14:19 | [diff] [blame] | 22 | #include "components/omnibox/browser/omnibox_popup_model.h" |
| 23 | #include "components/omnibox/browser/omnibox_view.h" |
treib | 75d6615 | 2016-01-14 18:19:40 | [diff] [blame^] | 24 | #include "content/public/browser/browser_thread.h" |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 25 | #include "content/public/browser/navigation_controller.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 26 | #include "content/public/browser/render_process_host.h" |
[email protected] | 233f0f96 | 2013-02-27 21:14:19 | [diff] [blame] | 27 | #include "content/public/browser/user_metrics.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 28 | #include "content/public/browser/web_contents.h" |
treib | 75d6615 | 2016-01-14 18:19:40 | [diff] [blame^] | 29 | #include "content/public/browser/web_contents_user_data.h" |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 30 | #include "content/public/common/referrer.h" |
| 31 | #include "ui/base/page_transition_types.h" |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 32 | |
| 33 | // Helpers -------------------------------------------------------------------- |
[email protected] | 233f0f96 | 2013-02-27 21:14:19 | [diff] [blame] | 34 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 35 | namespace { |
| 36 | |
| 37 | InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) { |
| 38 | DCHECK(profile); |
| 39 | InstantService* instant_service = |
| 40 | InstantServiceFactory::GetForProfile(profile); |
| 41 | return instant_service ? instant_service->instant_search_prerenderer() : NULL; |
| 42 | } |
| 43 | |
treib | 75d6615 | 2016-01-14 18:19:40 | [diff] [blame^] | 44 | // Helper class for posting a task to reload a tab, to avoid doing a re-entrant |
| 45 | // navigation, since it can be called when starting a navigation. This class |
| 46 | // makes sure to only execute the reload if the WebContents still exists. |
| 47 | class TabReloader : public content::WebContentsUserData<TabReloader> { |
| 48 | public: |
| 49 | static void Reload(content::WebContents* web_contents) { |
| 50 | TabReloader::CreateForWebContents(web_contents); |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | friend class content::WebContentsUserData<TabReloader>; |
| 55 | |
| 56 | explicit TabReloader(content::WebContents* web_contents) |
| 57 | : web_contents_(web_contents), weak_ptr_factory_(this) { |
| 58 | content::BrowserThread::PostTask( |
| 59 | content::BrowserThread::UI, |
| 60 | FROM_HERE, |
| 61 | base::Bind(&TabReloader::ReloadImpl, weak_ptr_factory_.GetWeakPtr())); |
| 62 | } |
| 63 | ~TabReloader() override {} |
| 64 | |
| 65 | void ReloadImpl() { |
| 66 | web_contents_->GetController().Reload(false); |
| 67 | |
| 68 | // As the reload was not triggered by the user we don't want to close any |
| 69 | // infobars. We have to tell the InfoBarService after the reload, |
| 70 | // otherwise it would ignore this call when |
| 71 | // WebContentsObserver::DidStartNavigationToPendingEntry is invoked. |
| 72 | InfoBarService::FromWebContents(web_contents_)->set_ignore_next_reload(); |
| 73 | |
| 74 | web_contents_->RemoveUserData(UserDataKey()); |
| 75 | } |
| 76 | |
| 77 | content::WebContents* web_contents_; |
| 78 | base::WeakPtrFactory<TabReloader> weak_ptr_factory_; |
| 79 | }; |
| 80 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 81 | } // namespace |
| 82 | |
treib | 75d6615 | 2016-01-14 18:19:40 | [diff] [blame^] | 83 | DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabReloader); |
| 84 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 85 | |
| 86 | // BrowserInstantController --------------------------------------------------- |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 87 | |
| 88 | BrowserInstantController::BrowserInstantController(Browser* browser) |
[email protected] | 8a23670 | 2012-09-28 13:30:57 | [diff] [blame] | 89 | : browser_(browser), |
[email protected] | 77759005 | 2014-01-17 22:11:54 | [diff] [blame] | 90 | instant_(this) { |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 91 | browser_->search_model()->AddObserver(this); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 92 | |
| 93 | InstantService* instant_service = |
| 94 | InstantServiceFactory::GetForProfile(profile()); |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 95 | instant_service->AddObserver(this); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | BrowserInstantController::~BrowserInstantController() { |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 99 | browser_->search_model()->RemoveObserver(this); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 100 | |
| 101 | InstantService* instant_service = |
| 102 | InstantServiceFactory::GetForProfile(profile()); |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 103 | instant_service->RemoveObserver(this); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 106 | bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition, |
| 107 | const GURL& url) { |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 108 | // Unsupported dispositions. |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 109 | if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW || |
| 110 | disposition == NEW_FOREGROUND_TAB) |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 111 | return false; |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 112 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 113 | // The omnibox currently doesn't use other dispositions, so we don't attempt |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 114 | // 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] | 115 | // support for the new disposition. |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 116 | DCHECK(disposition == CURRENT_TAB) << disposition; |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 117 | |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 118 | const base::string16& search_terms = |
sdefresne | 51bbec7b | 2015-08-03 14:18:13 | [diff] [blame] | 119 | search::ExtractSearchTermsFromURL(profile(), url); |
kmadhusu | 562c49c8 | 2014-11-18 02:08:03 | [diff] [blame] | 120 | EmbeddedSearchRequestParams request_params(url); |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame] | 121 | if (search_terms.empty()) |
| 122 | return false; |
| 123 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 124 | InstantSearchPrerenderer* prerenderer = |
| 125 | GetInstantSearchPrerenderer(profile()); |
[email protected] | f028cf5b | 2014-01-24 07:36:03 | [diff] [blame] | 126 | if (prerenderer) { |
| 127 | if (prerenderer->CanCommitQuery(GetActiveWebContents(), search_terms)) { |
| 128 | // Submit query to render the prefetched results. Browser will swap the |
| 129 | // prerendered contents with the active tab contents. |
kmadhusu | 562c49c8 | 2014-11-18 02:08:03 | [diff] [blame] | 130 | prerenderer->Commit(search_terms, request_params); |
[email protected] | f028cf5b | 2014-01-24 07:36:03 | [diff] [blame] | 131 | return false; |
| 132 | } else { |
| 133 | prerenderer->Cancel(); |
| 134 | } |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 135 | } |
| 136 | |
[email protected] | 50da190 | 2014-02-14 02:40:19 | [diff] [blame] | 137 | // If we will not be replacing search terms from this URL, don't send to |
| 138 | // InstantController. |
sdefresne | 51bbec7b | 2015-08-03 14:18:13 | [diff] [blame] | 139 | if (!search::IsQueryExtractionAllowedForURL(profile(), url)) |
[email protected] | 50da190 | 2014-02-14 02:40:19 | [diff] [blame] | 140 | return false; |
kmadhusu | 562c49c8 | 2014-11-18 02:08:03 | [diff] [blame] | 141 | return instant_.SubmitQuery(search_terms, request_params); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 142 | } |
| 143 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 144 | Profile* BrowserInstantController::profile() const { |
| 145 | return browser_->profile(); |
| 146 | } |
| 147 | |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 148 | content::WebContents* BrowserInstantController::GetActiveWebContents() const { |
| 149 | return browser_->tab_strip_model()->GetActiveWebContents(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 152 | void BrowserInstantController::ActiveTabChanged() { |
| 153 | instant_.ActiveTabChanged(); |
| 154 | } |
| 155 | |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 156 | void BrowserInstantController::TabDeactivated(content::WebContents* contents) { |
| 157 | instant_.TabDeactivated(contents); |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 158 | |
| 159 | InstantSearchPrerenderer* prerenderer = |
| 160 | GetInstantSearchPrerenderer(profile()); |
| 161 | if (prerenderer) |
| 162 | prerenderer->Cancel(); |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 165 | void BrowserInstantController::ModelChanged( |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 166 | const SearchModel::State& old_state, |
| 167 | const SearchModel::State& new_state) { |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 168 | if (old_state.mode != new_state.mode) { |
| 169 | const SearchMode& new_mode = new_state.mode; |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 170 | |
[email protected] | a780c7b2 | 2013-08-02 18:36:59 | [diff] [blame] | 171 | // Record some actions corresponding to the mode change. Note that to get |
| 172 | // the full story, it's necessary to look at other UMA actions as well, |
| 173 | // such as tab switches. |
| 174 | if (new_mode.is_search_results()) |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 175 | content::RecordAction(base::UserMetricsAction("InstantExtended.ShowSRP")); |
[email protected] | a780c7b2 | 2013-08-02 18:36:59 | [diff] [blame] | 176 | else if (new_mode.is_ntp()) |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 177 | content::RecordAction(base::UserMetricsAction("InstantExtended.ShowNTP")); |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 178 | |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 179 | instant_.SearchModeChanged(old_state.mode, new_mode); |
[email protected] | c19ba104 | 2013-03-11 17:17:13 | [diff] [blame] | 180 | } |
| 181 | |
[email protected] | 4066a695d | 2013-06-20 14:08:54 | [diff] [blame] | 182 | if (old_state.instant_support != new_state.instant_support) |
| 183 | instant_.InstantSupportChanged(new_state.instant_support); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 184 | } |
| 185 | |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 186 | void BrowserInstantController::DefaultSearchProviderChanged( |
| 187 | bool google_base_url_domain_changed) { |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 188 | InstantService* instant_service = |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 189 | InstantServiceFactory::GetForProfile(profile()); |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 190 | if (!instant_service) |
| 191 | return; |
| 192 | |
| 193 | TabStripModel* tab_model = browser_->tab_strip_model(); |
| 194 | int count = tab_model->count(); |
| 195 | for (int index = 0; index < count; ++index) { |
| 196 | content::WebContents* contents = tab_model->GetWebContentsAt(index); |
| 197 | if (!contents) |
| 198 | continue; |
| 199 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 200 | // Send new search URLs to the renderer. |
| 201 | content::RenderProcessHost* rph = contents->GetRenderProcessHost(); |
| 202 | instant_service->SendSearchURLsToRenderer(rph); |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 203 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 204 | if (!instant_service->IsInstantProcess(rph->GetID())) |
| 205 | continue; |
sky | 4bdad24 | 2014-09-18 20:22:20 | [diff] [blame] | 206 | |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 207 | if (google_base_url_domain_changed && |
| 208 | SearchTabHelper::FromWebContents(contents)->model()->mode().is_ntp()) { |
| 209 | // Replace the server NTP with the local NTP. |
sdefresne | 51bbec7b | 2015-08-03 14:18:13 | [diff] [blame] | 210 | content::NavigationController::LoadURLParams params( |
| 211 | search::GetLocalInstantURL(profile())); |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 212 | params.should_replace_current_entry = true; |
| 213 | params.referrer = content::Referrer(); |
| 214 | params.transition_type = ui::PAGE_TRANSITION_RELOAD; |
| 215 | contents->GetController().LoadURLWithParams(params); |
| 216 | } else { |
| 217 | // Reload the contents to ensure that it gets assigned to a |
treib | 75d6615 | 2016-01-14 18:19:40 | [diff] [blame^] | 218 | // non-privileged renderer. |
| 219 | TabReloader::Reload(contents); |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 220 | } |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 221 | } |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 222 | } |