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