blob: 65f6cf0737c8bc39797f417e9af6b07505262e2f [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"
treib75d66152016-01-14 18:19:4018#include "content/public/browser/browser_thread.h"
mathp880d3282015-02-20 00:10:0519#include "content/public/browser/navigation_controller.h"
[email protected]0a46856e2013-04-24 00:33:0220#include "content/public/browser/render_process_host.h"
[email protected]0a46856e2013-04-24 00:33:0221#include "content/public/browser/web_contents.h"
treib75d66152016-01-14 18:19:4022#include "content/public/browser/web_contents_user_data.h"
mathp880d3282015-02-20 00:10:0523#include "content/public/common/referrer.h"
24#include "ui/base/page_transition_types.h"
[email protected]c4b2af22014-05-11 19:48:5325
26// Helpers --------------------------------------------------------------------
[email protected]233f0f962013-02-27 21:14:1927
[email protected]e97887c2013-12-11 01:27:3128namespace {
29
treib75d66152016-01-14 18:19:4030// 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.
33class TabReloader : public content::WebContentsUserData<TabReloader> {
34 public:
avie8937a52017-05-02 02:17:2035 ~TabReloader() override {}
36
treib75d66152016-01-14 18:19:4037 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(
tzik22036cc2017-04-21 04:08:1847 content::BrowserThread::UI, FROM_HERE,
48 base::BindOnce(&TabReloader::ReloadImpl,
49 weak_ptr_factory_.GetWeakPtr()));
treib75d66152016-01-14 18:19:4050 }
treib75d66152016-01-14 18:19:4051
52 void ReloadImpl() {
toyoshim6142d96f2016-12-19 09:07:2553 web_contents_->GetController().Reload(content::ReloadType::NORMAL, false);
treib75d66152016-01-14 18:19:4054
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]e97887c2013-12-11 01:27:3168} // namespace
69
treib75d66152016-01-14 18:19:4070DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabReloader);
71
[email protected]c4b2af22014-05-11 19:48:5372
73// BrowserInstantController ---------------------------------------------------
[email protected]7acfaf92012-07-11 15:51:5974
75BrowserInstantController::BrowserInstantController(Browser* browser)
Marc Treibbfe01712017-09-26 11:49:4576 : browser_(browser), instant_(this) {
Marc Treibc4de3f12017-09-05 12:36:1577 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]7acfaf92012-07-11 15:51:5988}
89
Marc Treib7376bd82017-09-26 08:03:2090BrowserInstantController::~BrowserInstantController() = default;
[email protected]0c9406632013-02-08 01:13:3391
[email protected]0c9406632013-02-08 01:13:3392Profile* BrowserInstantController::profile() const {
93 return browser_->profile();
94}
95
Marc Treib7376bd82017-09-26 08:03:2096void BrowserInstantController::OnTabActivated(
97 content::WebContents* web_contents) {
Marc Treibbfe01712017-09-26 11:49:4598 instant_.OnTabActivated(web_contents);
[email protected]e41982a72012-11-20 07:16:5199}
100
Marc Treib7376bd82017-09-26 08:03:20101void BrowserInstantController::OnTabDeactivated(
102 content::WebContents* web_contents) {
Marc Treibbfe01712017-09-26 11:49:45103 instant_.OnTabDeactivated(web_contents);
Marc Treib7376bd82017-09-26 08:03:20104}
105
106void BrowserInstantController::OnTabDetached(
107 content::WebContents* web_contents) {
Marc Treibbfe01712017-09-26 11:49:45108 instant_.OnTabDetached(web_contents);
[email protected]7acfaf92012-07-11 15:51:59109}
110
Marc Treibc4de3f12017-09-05 12:36:15111void BrowserInstantController::OnSearchEngineBaseURLChanged(
112 SearchEngineBaseURLTracker::ChangeReason change_reason) {
[email protected]0a46856e2013-04-24 00:33:02113 InstantService* instant_service =
[email protected]c8a118e2013-09-24 21:33:40114 InstantServiceFactory::GetForProfile(profile());
[email protected]0a46856e2013-04-24 00:33:02115 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]2309e912013-10-01 01:33:30125 // Send new search URLs to the renderer.
126 content::RenderProcessHost* rph = contents->GetRenderProcessHost();
127 instant_service->SendSearchURLsToRenderer(rph);
[email protected]0a46856e2013-04-24 00:33:02128
[email protected]2309e912013-10-01 01:33:30129 if (!instant_service->IsInstantProcess(rph->GetID()))
130 continue;
sky4bdad242014-09-18 20:22:20131
Marc Treibc4de3f12017-09-05 12:36:15132 bool google_base_url_domain_changed =
133 change_reason ==
134 SearchEngineBaseURLTracker::ChangeReason::GOOGLE_BASE_URL;
Marc Treibbfe01712017-09-26 11:49:45135 if (google_base_url_domain_changed) {
tfarina21866dd2016-04-01 06:54:19136 GURL local_ntp_url(chrome::kChromeSearchLocalNtpUrl);
Marc Treibbfe01712017-09-26 11:49:45137 // Replace the server NTP with the local NTP (or reload the local NTP).
tfarina21866dd2016-04-01 06:54:19138 content::NavigationController::LoadURLParams params(local_ntp_url);
mathp880d3282015-02-20 00:10:05139 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
treib75d66152016-01-14 18:19:40145 // non-privileged renderer.
146 TabReloader::Reload(contents);
mathp880d3282015-02-20 00:10:05147 }
[email protected]0a46856e2013-04-24 00:33:02148 }
[email protected]0a46856e2013-04-24 00:33:02149}