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