blob: f90c46f31c7100a583f25e5a2b36539f861f9a91 [file] [log] [blame]
[email protected]e41982a72012-11-20 07:16:511// Copyright 2012 The Chromium Authors. All rights reserved.
[email protected]7acfaf92012-07-11 15:51:592// 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]0a46856e2013-04-24 00:33:027#include "base/bind.h"
sky4bdad242014-09-18 20:22:208#include "chrome/browser/infobars/infobar_service.h"
[email protected]7acfaf92012-07-11 15:51:599#include "chrome/browser/profiles/profile.h"
[email protected]0a46856e2013-04-24 00:33:0210#include "chrome/browser/search/instant_service.h"
11#include "chrome/browser/search/instant_service_factory.h"
[email protected]a7b8e43d2013-03-18 18:52:4312#include "chrome/browser/search/search.h"
Marc Treibc4de3f12017-09-05 12:36:1513#include "chrome/browser/search_engines/template_url_service_factory.h"
14#include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
[email protected]7acfaf92012-07-11 15:51:5915#include "chrome/browser/ui/browser.h"
[email protected]e41982a72012-11-20 07:16:5116#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]0c9406632013-02-08 01:13:3317#include "chrome/common/url_constants.h"
Eric Seckler8652dcd52018-09-20 10:42:2818#include "content/public/browser/browser_task_traits.h"
treib75d66152016-01-14 18:19:4019#include "content/public/browser/browser_thread.h"
mathp880d3282015-02-20 00:10:0520#include "content/public/browser/navigation_controller.h"
Lukasz Anforowicz7d7fc4b2017-10-04 15:05:0421#include "content/public/browser/render_frame_host.h"
[email protected]0a46856e2013-04-24 00:33:0222#include "content/public/browser/render_process_host.h"
[email protected]0a46856e2013-04-24 00:33:0223#include "content/public/browser/web_contents.h"
treib75d66152016-01-14 18:19:4024#include "content/public/browser/web_contents_user_data.h"
mathp880d3282015-02-20 00:10:0525#include "content/public/common/referrer.h"
26#include "ui/base/page_transition_types.h"
[email protected]c4b2af22014-05-11 19:48:5327
28// Helpers --------------------------------------------------------------------
[email protected]233f0f962013-02-27 21:14:1929
[email protected]e97887c2013-12-11 01:27:3130namespace {
31
treib75d66152016-01-14 18:19:4032// 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.
35class TabReloader : public content::WebContentsUserData<TabReloader> {
36 public:
avie8937a52017-05-02 02:17:2037 ~TabReloader() override {}
38
treib75d66152016-01-14 18:19:4039 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 Roman495db682019-07-12 16:03:2447 : web_contents_(web_contents) {
Gabriel Charetteb2bbd482020-05-27 01:24:2148 content::GetUIThreadTaskRunner({})->PostTask(
49 FROM_HERE, base::BindOnce(&TabReloader::ReloadImpl,
Sami Kyostilaf7d83b02019-08-07 11:49:0650 weak_ptr_factory_.GetWeakPtr()));
treib75d66152016-01-14 18:19:4051 }
treib75d66152016-01-14 18:19:4052
53 void ReloadImpl() {
toyoshim6142d96f2016-12-19 09:07:2554 web_contents_->GetController().Reload(content::ReloadType::NORMAL, false);
treib75d66152016-01-14 18:19:4055
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 Roman495db682019-07-12 16:03:2466 base::WeakPtrFactory<TabReloader> weak_ptr_factory_{this};
François Doray4f51d5d2018-12-03 22:26:2467 WEB_CONTENTS_USER_DATA_KEY_DECL();
treib75d66152016-01-14 18:19:4068};
69
François Doray4f51d5d2018-12-03 22:26:2470WEB_CONTENTS_USER_DATA_KEY_IMPL(TabReloader)
71
[email protected]e97887c2013-12-11 01:27:3172} // namespace
73
[email protected]c4b2af22014-05-11 19:48:5374// BrowserInstantController ---------------------------------------------------
[email protected]7acfaf92012-07-11 15:51:5975
76BrowserInstantController::BrowserInstantController(Browser* browser)
Marc Treibf4e8daff2017-09-27 08:40:3477 : browser_(browser), instant_(profile(), browser_->tab_strip_model()) {
Marc Treibc4de3f12017-09-05 12:36:1578 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 Bangcc280792018-01-17 23:33:5583 std::make_unique<SearchEngineBaseURLTracker>(
David Benjamin2e989772019-08-01 16:36:0484 template_url_service, std::make_unique<UIThreadSearchTermsData>(),
Marc Treibc4de3f12017-09-05 12:36:1585 base::Bind(&BrowserInstantController::OnSearchEngineBaseURLChanged,
86 base::Unretained(this)));
87 }
[email protected]7acfaf92012-07-11 15:51:5988}
89
Marc Treib7376bd82017-09-26 08:03:2090BrowserInstantController::~BrowserInstantController() = default;
[email protected]0c9406632013-02-08 01:13:3391
Marc Treibc4de3f12017-09-05 12:36:1592void BrowserInstantController::OnSearchEngineBaseURLChanged(
93 SearchEngineBaseURLTracker::ChangeReason change_reason) {
[email protected]0a46856e2013-04-24 00:33:0294 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-Moslimany3446e95e2020-09-22 20:17:06101 bool is_ntp = contents->GetMainFrame()->GetSiteInstance()->GetSiteURL() ==
102 GURL(chrome::kChromeUINewTabPageURL);
[email protected]0a46856e2013-04-24 00:33:02103
Esmael El-Moslimany3446e95e2020-09-22 20:17:06104 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]2309e912013-10-01 01:33:30117 continue;
sky4bdad242014-09-18 20:22:20118
Esmael El-Moslimany3446e95e2020-09-22 20:17:06119 // 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]0a46856e2013-04-24 00:33:02127 }
[email protected]0a46856e2013-04-24 00:33:02128}
Marc Treibf4e8daff2017-09-27 08:40:34129
130Profile* BrowserInstantController::profile() const {
131 return browser_->profile();
132}