[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" |
Marc Treib | c4de3f1 | 2017-09-05 12:36:15 | [diff] [blame] | 13 | #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 14 | #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser.h" |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 16 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 17 | #include "chrome/common/url_constants.h" |
treib | 75d6615 | 2016-01-14 18:19:40 | [diff] [blame] | 18 | #include "content/public/browser/browser_thread.h" |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 19 | #include "content/public/browser/navigation_controller.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 20 | #include "content/public/browser/render_process_host.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 21 | #include "content/public/browser/web_contents.h" |
treib | 75d6615 | 2016-01-14 18:19:40 | [diff] [blame] | 22 | #include "content/public/browser/web_contents_user_data.h" |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 23 | #include "content/public/common/referrer.h" |
| 24 | #include "ui/base/page_transition_types.h" |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 25 | |
| 26 | // Helpers -------------------------------------------------------------------- |
[email protected] | 233f0f96 | 2013-02-27 21:14:19 | [diff] [blame] | 27 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 28 | namespace { |
| 29 | |
treib | 75d6615 | 2016-01-14 18:19:40 | [diff] [blame] | 30 | // Helper class for posting a task to reload a tab, to avoid doing a re-entrant |
| 31 | // navigation, since it can be called when starting a navigation. This class |
| 32 | // makes sure to only execute the reload if the WebContents still exists. |
| 33 | class TabReloader : public content::WebContentsUserData<TabReloader> { |
| 34 | public: |
avi | e8937a5 | 2017-05-02 02:17:20 | [diff] [blame] | 35 | ~TabReloader() override {} |
| 36 | |
treib | 75d6615 | 2016-01-14 18:19:40 | [diff] [blame] | 37 | static void Reload(content::WebContents* web_contents) { |
| 38 | TabReloader::CreateForWebContents(web_contents); |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | friend class content::WebContentsUserData<TabReloader>; |
| 43 | |
| 44 | explicit TabReloader(content::WebContents* web_contents) |
| 45 | : web_contents_(web_contents), weak_ptr_factory_(this) { |
| 46 | content::BrowserThread::PostTask( |
tzik | 22036cc | 2017-04-21 04:08:18 | [diff] [blame] | 47 | content::BrowserThread::UI, FROM_HERE, |
| 48 | base::BindOnce(&TabReloader::ReloadImpl, |
| 49 | weak_ptr_factory_.GetWeakPtr())); |
treib | 75d6615 | 2016-01-14 18:19:40 | [diff] [blame] | 50 | } |
treib | 75d6615 | 2016-01-14 18:19:40 | [diff] [blame] | 51 | |
| 52 | void ReloadImpl() { |
toyoshim | 6142d96f | 2016-12-19 09:07:25 | [diff] [blame] | 53 | web_contents_->GetController().Reload(content::ReloadType::NORMAL, false); |
treib | 75d6615 | 2016-01-14 18:19:40 | [diff] [blame] | 54 | |
| 55 | // As the reload was not triggered by the user we don't want to close any |
| 56 | // infobars. We have to tell the InfoBarService after the reload, |
| 57 | // otherwise it would ignore this call when |
| 58 | // WebContentsObserver::DidStartNavigationToPendingEntry is invoked. |
| 59 | InfoBarService::FromWebContents(web_contents_)->set_ignore_next_reload(); |
| 60 | |
| 61 | web_contents_->RemoveUserData(UserDataKey()); |
| 62 | } |
| 63 | |
| 64 | content::WebContents* web_contents_; |
| 65 | base::WeakPtrFactory<TabReloader> weak_ptr_factory_; |
| 66 | }; |
| 67 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 68 | } // namespace |
| 69 | |
treib | 75d6615 | 2016-01-14 18:19:40 | [diff] [blame] | 70 | DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabReloader); |
| 71 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 72 | |
| 73 | // BrowserInstantController --------------------------------------------------- |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 74 | |
| 75 | BrowserInstantController::BrowserInstantController(Browser* browser) |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame^] | 76 | : browser_(browser), instant_(this) { |
Marc Treib | c4de3f1 | 2017-09-05 12:36:15 | [diff] [blame] | 77 | TemplateURLService* template_url_service = |
| 78 | TemplateURLServiceFactory::GetForProfile(profile()); |
| 79 | // TemplateURLService can be null in tests. |
| 80 | if (template_url_service) { |
| 81 | search_engine_base_url_tracker_ = |
| 82 | base::MakeUnique<SearchEngineBaseURLTracker>( |
| 83 | template_url_service, |
| 84 | base::MakeUnique<UIThreadSearchTermsData>(profile()), |
| 85 | base::Bind(&BrowserInstantController::OnSearchEngineBaseURLChanged, |
| 86 | base::Unretained(this))); |
| 87 | } |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 88 | } |
| 89 | |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 90 | BrowserInstantController::~BrowserInstantController() = default; |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 91 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 92 | Profile* BrowserInstantController::profile() const { |
| 93 | return browser_->profile(); |
| 94 | } |
| 95 | |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 96 | void BrowserInstantController::OnTabActivated( |
| 97 | content::WebContents* web_contents) { |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame^] | 98 | instant_.OnTabActivated(web_contents); |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 99 | } |
| 100 | |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 101 | void BrowserInstantController::OnTabDeactivated( |
| 102 | content::WebContents* web_contents) { |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame^] | 103 | instant_.OnTabDeactivated(web_contents); |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void BrowserInstantController::OnTabDetached( |
| 107 | content::WebContents* web_contents) { |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame^] | 108 | instant_.OnTabDetached(web_contents); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 109 | } |
| 110 | |
Marc Treib | c4de3f1 | 2017-09-05 12:36:15 | [diff] [blame] | 111 | void BrowserInstantController::OnSearchEngineBaseURLChanged( |
| 112 | SearchEngineBaseURLTracker::ChangeReason change_reason) { |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 113 | InstantService* instant_service = |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 114 | InstantServiceFactory::GetForProfile(profile()); |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 115 | if (!instant_service) |
| 116 | return; |
| 117 | |
| 118 | TabStripModel* tab_model = browser_->tab_strip_model(); |
| 119 | int count = tab_model->count(); |
| 120 | for (int index = 0; index < count; ++index) { |
| 121 | content::WebContents* contents = tab_model->GetWebContentsAt(index); |
| 122 | if (!contents) |
| 123 | continue; |
| 124 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 125 | // Send new search URLs to the renderer. |
| 126 | content::RenderProcessHost* rph = contents->GetRenderProcessHost(); |
| 127 | instant_service->SendSearchURLsToRenderer(rph); |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 128 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 129 | if (!instant_service->IsInstantProcess(rph->GetID())) |
| 130 | continue; |
sky | 4bdad24 | 2014-09-18 20:22:20 | [diff] [blame] | 131 | |
Marc Treib | c4de3f1 | 2017-09-05 12:36:15 | [diff] [blame] | 132 | bool google_base_url_domain_changed = |
| 133 | change_reason == |
| 134 | SearchEngineBaseURLTracker::ChangeReason::GOOGLE_BASE_URL; |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame^] | 135 | if (google_base_url_domain_changed) { |
tfarina | 21866dd | 2016-04-01 06:54:19 | [diff] [blame] | 136 | GURL local_ntp_url(chrome::kChromeSearchLocalNtpUrl); |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame^] | 137 | // Replace the server NTP with the local NTP (or reload the local NTP). |
tfarina | 21866dd | 2016-04-01 06:54:19 | [diff] [blame] | 138 | content::NavigationController::LoadURLParams params(local_ntp_url); |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 139 | params.should_replace_current_entry = true; |
| 140 | params.referrer = content::Referrer(); |
| 141 | params.transition_type = ui::PAGE_TRANSITION_RELOAD; |
| 142 | contents->GetController().LoadURLWithParams(params); |
| 143 | } else { |
| 144 | // Reload the contents to ensure that it gets assigned to a |
treib | 75d6615 | 2016-01-14 18:19:40 | [diff] [blame] | 145 | // non-privileged renderer. |
| 146 | TabReloader::Reload(contents); |
mathp | 880d328 | 2015-02-20 00:10:05 | [diff] [blame] | 147 | } |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 148 | } |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 149 | } |