blob: dc188814794d3ef54a775dc6db55a4995d657c9e [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"
Eric Seckler8652dcd52018-09-20 10:42:288#include "base/task/post_task.h"
sky4bdad242014-09-18 20:22:209#include "chrome/browser/infobars/infobar_service.h"
[email protected]7acfaf92012-07-11 15:51:5910#include "chrome/browser/profiles/profile.h"
[email protected]0a46856e2013-04-24 00:33:0211#include "chrome/browser/search/instant_service.h"
12#include "chrome/browser/search/instant_service_factory.h"
[email protected]a7b8e43d2013-03-18 18:52:4313#include "chrome/browser/search/search.h"
Marc Treibc4de3f12017-09-05 12:36:1514#include "chrome/browser/search_engines/template_url_service_factory.h"
15#include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
[email protected]7acfaf92012-07-11 15:51:5916#include "chrome/browser/ui/browser.h"
[email protected]e41982a72012-11-20 07:16:5117#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]0c9406632013-02-08 01:13:3318#include "chrome/common/url_constants.h"
Eric Seckler8652dcd52018-09-20 10:42:2819#include "content/public/browser/browser_task_traits.h"
treib75d66152016-01-14 18:19:4020#include "content/public/browser/browser_thread.h"
mathp880d3282015-02-20 00:10:0521#include "content/public/browser/navigation_controller.h"
Lukasz Anforowicz7d7fc4b2017-10-04 15:05:0422#include "content/public/browser/render_frame_host.h"
[email protected]0a46856e2013-04-24 00:33:0223#include "content/public/browser/render_process_host.h"
[email protected]0a46856e2013-04-24 00:33:0224#include "content/public/browser/web_contents.h"
treib75d66152016-01-14 18:19:4025#include "content/public/browser/web_contents_user_data.h"
mathp880d3282015-02-20 00:10:0526#include "content/public/common/referrer.h"
27#include "ui/base/page_transition_types.h"
[email protected]c4b2af22014-05-11 19:48:5328
29// Helpers --------------------------------------------------------------------
[email protected]233f0f962013-02-27 21:14:1930
[email protected]e97887c2013-12-11 01:27:3131namespace {
32
treib75d66152016-01-14 18:19:4033// 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.
36class TabReloader : public content::WebContentsUserData<TabReloader> {
37 public:
avie8937a52017-05-02 02:17:2038 ~TabReloader() override {}
39
treib75d66152016-01-14 18:19:4040 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 Roman495db682019-07-12 16:03:2448 : web_contents_(web_contents) {
Sami Kyostilaf7d83b02019-08-07 11:49:0649 base::PostTask(FROM_HERE, {content::BrowserThread::UI},
50 base::BindOnce(&TabReloader::ReloadImpl,
51 weak_ptr_factory_.GetWeakPtr()));
treib75d66152016-01-14 18:19:4052 }
treib75d66152016-01-14 18:19:4053
54 void ReloadImpl() {
toyoshim6142d96f2016-12-19 09:07:2555 web_contents_->GetController().Reload(content::ReloadType::NORMAL, false);
treib75d66152016-01-14 18:19:4056
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 Roman495db682019-07-12 16:03:2467 base::WeakPtrFactory<TabReloader> weak_ptr_factory_{this};
François Doray4f51d5d2018-12-03 22:26:2468 WEB_CONTENTS_USER_DATA_KEY_DECL();
treib75d66152016-01-14 18:19:4069};
70
François Doray4f51d5d2018-12-03 22:26:2471WEB_CONTENTS_USER_DATA_KEY_IMPL(TabReloader)
72
[email protected]e97887c2013-12-11 01:27:3173} // namespace
74
[email protected]c4b2af22014-05-11 19:48:5375// BrowserInstantController ---------------------------------------------------
[email protected]7acfaf92012-07-11 15:51:5976
77BrowserInstantController::BrowserInstantController(Browser* browser)
Marc Treibf4e8daff2017-09-27 08:40:3478 : browser_(browser), instant_(profile(), browser_->tab_strip_model()) {
Marc Treibc4de3f12017-09-05 12:36:1579 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 Bangcc280792018-01-17 23:33:5584 std::make_unique<SearchEngineBaseURLTracker>(
David Benjamin2e989772019-08-01 16:36:0485 template_url_service, std::make_unique<UIThreadSearchTermsData>(),
Marc Treibc4de3f12017-09-05 12:36:1586 base::Bind(&BrowserInstantController::OnSearchEngineBaseURLChanged,
87 base::Unretained(this)));
88 }
[email protected]7acfaf92012-07-11 15:51:5989}
90
Marc Treib7376bd82017-09-26 08:03:2091BrowserInstantController::~BrowserInstantController() = default;
[email protected]0c9406632013-02-08 01:13:3392
Marc Treibc4de3f12017-09-05 12:36:1593void BrowserInstantController::OnSearchEngineBaseURLChanged(
94 SearchEngineBaseURLTracker::ChangeReason change_reason) {
[email protected]0a46856e2013-04-24 00:33:0295 InstantService* instant_service =
[email protected]c8a118e2013-09-24 21:33:4096 InstantServiceFactory::GetForProfile(profile());
[email protected]0a46856e2013-04-24 00:33:0297 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 Treibc9c35002017-10-16 10:09:33107 // Send the new NTP URL to the renderer.
Lukasz Anforowicz7d7fc4b2017-10-04 15:05:04108 content::RenderProcessHost* rph = contents->GetMainFrame()->GetProcess();
Marc Treibc9c35002017-10-16 10:09:33109 instant_service->SendNewTabPageURLToRenderer(rph);
[email protected]0a46856e2013-04-24 00:33:02110
[email protected]2309e912013-10-01 01:33:30111 if (!instant_service->IsInstantProcess(rph->GetID()))
112 continue;
sky4bdad242014-09-18 20:22:20113
Marc Treibc4de3f12017-09-05 12:36:15114 bool google_base_url_domain_changed =
115 change_reason ==
116 SearchEngineBaseURLTracker::ChangeReason::GOOGLE_BASE_URL;
Marc Treibbfe01712017-09-26 11:49:45117 if (google_base_url_domain_changed) {
tfarina21866dd2016-04-01 06:54:19118 GURL local_ntp_url(chrome::kChromeSearchLocalNtpUrl);
Marc Treibbfe01712017-09-26 11:49:45119 // Replace the server NTP with the local NTP (or reload the local NTP).
tfarina21866dd2016-04-01 06:54:19120 content::NavigationController::LoadURLParams params(local_ntp_url);
mathp880d3282015-02-20 00:10:05121 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
treib75d66152016-01-14 18:19:40127 // non-privileged renderer.
128 TabReloader::Reload(contents);
mathp880d3282015-02-20 00:10:05129 }
[email protected]0a46856e2013-04-24 00:33:02130 }
[email protected]0a46856e2013-04-24 00:33:02131}
Marc Treibf4e8daff2017-09-27 08:40:34132
133Profile* BrowserInstantController::profile() const {
134 return browser_->profile();
135}