blob: 87bbc1d884174e224b99904dbeca133f29445140 [file] [log] [blame]
[email protected]e41982a72012-11-20 07:16:511// Copyright 2012 The Chromium Authors. All rights reserved.
[email protected]fd2b9ce2010-08-11 04:03:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]03bb7102013-03-17 22:44:475#include "chrome/browser/ui/search/instant_controller.h"
[email protected]fd2b9ce2010-08-11 04:03:576
[email protected]2b3ded92013-06-19 05:48:437#include "base/prefs/pref_service.h"
[email protected]f92d65882013-06-10 22:02:368#include "base/strings/stringprintf.h"
[email protected]1a7ff742013-07-12 00:26:239#include "chrome/browser/chrome_notification_types.h"
[email protected]ba6680f2010-11-01 20:35:0810#include "chrome/browser/platform_util.h"
[email protected]d58688c62013-07-03 23:09:1211#include "chrome/browser/profiles/profile.h"
[email protected]a7b8e43d2013-03-18 18:52:4312#include "chrome/browser/search/instant_service.h"
13#include "chrome/browser/search/instant_service_factory.h"
14#include "chrome/browser/search/search.h"
[email protected]8e5c89a2011-06-07 18:13:3315#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]4418dbb2012-10-25 03:21:5416#include "chrome/browser/ui/browser_instant_controller.h"
[email protected]03bb7102013-03-17 22:44:4717#include "chrome/browser/ui/search/instant_tab.h"
[email protected]23f02b22012-10-26 06:08:2718#include "chrome/browser/ui/search/search_tab_helper.h"
[email protected]fd2b9ce2010-08-11 04:03:5719#include "chrome/common/chrome_switches.h"
[email protected]2309e912013-10-01 01:33:3020#include "chrome/common/search_urls.h"
[email protected]0c9406632013-02-08 01:13:3321#include "chrome/common/url_constants.h"
[email protected]bf5c532d2014-07-05 00:29:5322#include "components/search_engines/template_url_service.h"
blundell47c6d8a2015-09-24 11:06:4023#include "components/sessions/core/serialized_navigation_entry.h"
[email protected]c55e3b82012-08-09 15:27:0524#include "content/public/browser/navigation_entry.h"
[email protected]ad50def52011-10-19 23:17:0725#include "content/public/browser/notification_service.h"
[email protected]d928589a2013-03-14 13:12:3926#include "content/public/browser/render_process_host.h"
[email protected]5626b0892012-02-20 14:46:5827#include "content/public/browser/render_widget_host_view.h"
[email protected]3d6a8952012-12-14 03:18:0728#include "content/public/browser/web_contents.h"
[email protected]a7c22e52012-10-12 19:19:0529#include "net/base/escape.h"
[email protected]5aaa17d2013-05-06 22:44:3430#include "net/base/network_change_notifier.h"
[email protected]a46d67d92013-10-07 22:39:2031#include "url/gurl.h"
[email protected]fd2b9ce2010-08-11 04:03:5732
[email protected]c55e3b82012-08-09 15:27:0533namespace {
34
[email protected]0c9406632013-02-08 01:13:3335bool IsContentsFrom(const InstantPage* page,
36 const content::WebContents* contents) {
lucinka.brozkovac25f7fb92014-09-04 06:42:5637 return page && (page->web_contents() == contents);
[email protected]0c9406632013-02-08 01:13:3338}
39
[email protected]0b684262013-02-20 02:18:2140// Adds a transient NavigationEntry to the supplied |contents|'s
41// NavigationController if the page's URL has not already been updated with the
42// supplied |search_terms|. Sets the |search_terms| on the transient entry for
43// search terms extraction to work correctly.
44void EnsureSearchTermsAreSet(content::WebContents* contents,
[email protected]e9273e192013-12-11 17:51:4945 const base::string16& search_terms) {
[email protected]988cf722013-03-08 00:42:0046 content::NavigationController* controller = &contents->GetController();
[email protected]0b684262013-02-20 02:18:2147
48 // If search terms are already correct or there is already a transient entry
49 // (there shouldn't be), bail out early.
sdefresne51bbec7b2015-08-03 14:18:1350 if (search::GetSearchTerms(contents) == search_terms ||
[email protected]988cf722013-03-08 00:42:0051 controller->GetTransientEntry())
[email protected]0b684262013-02-20 02:18:2152 return;
53
[email protected]d8dca262013-11-20 06:16:2454 const content::NavigationEntry* entry = controller->GetLastCommittedEntry();
avi25764702015-06-23 15:43:3755 scoped_ptr<content::NavigationEntry> transient =
56 controller->CreateNavigationEntry(
57 entry->GetURL(),
58 entry->GetReferrer(),
59 entry->GetTransitionType(),
60 false,
61 std::string(),
62 contents->GetBrowserContext());
[email protected]40a7e412013-04-29 18:13:0163 transient->SetExtraData(sessions::kSearchTermsKey, search_terms);
avi25764702015-06-23 15:43:3764 controller->SetTransientEntry(transient.Pass());
[email protected]0b684262013-02-20 02:18:2165
[email protected]165fe422013-03-27 06:34:0366 SearchTabHelper::FromWebContents(contents)->NavigationEntryUpdated();
[email protected]0b684262013-02-20 02:18:2167}
68
[email protected]b67d0a42012-09-04 20:57:3569} // namespace
70
[email protected]a780c7b22013-08-02 18:36:5971InstantController::InstantController(BrowserInstantController* browser)
[email protected]416d7532014-02-28 22:58:0672 : browser_(browser) {
[email protected]e41982a72012-11-20 07:16:5173}
74
[email protected]6b723f82010-10-05 20:14:2775InstantController::~InstantController() {
[email protected]0c9406632013-02-08 01:13:3376}
77
kmadhusu562c49c82014-11-18 02:08:0378bool InstantController::SubmitQuery(const base::string16& search_terms,
79 const EmbeddedSearchRequestParams& params) {
[email protected]a780c7b22013-08-02 18:36:5980 if (instant_tab_ && instant_tab_->supports_instant() &&
[email protected]413558cb2013-06-10 16:44:4581 search_mode_.is_origin_search()) {
82 // Use |instant_tab_| to run the query if we're already on a search results
83 // page. (NOTE: in particular, we do not send the query to NTPs.)
lucinka.brozkovac25f7fb92014-09-04 06:42:5684 SearchTabHelper::FromWebContents(instant_tab_->web_contents())->Submit(
kmadhusu562c49c82014-11-18 02:08:0385 search_terms, params);
lucinka.brozkovac25f7fb92014-09-04 06:42:5686 instant_tab_->web_contents()->Focus();
87 EnsureSearchTermsAreSet(instant_tab_->web_contents(), search_terms);
[email protected]413558cb2013-06-10 16:44:4588 return true;
89 }
90 return false;
91}
92
[email protected]165fe422013-03-27 06:34:0393void InstantController::SearchModeChanged(const SearchMode& old_mode,
94 const SearchMode& new_mode) {
[email protected]b97059d2013-01-26 00:06:4095 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf(
96 "SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode.origin,
97 old_mode.mode, new_mode.origin, new_mode.mode));
[email protected]cd533bf2012-12-04 19:14:5998
[email protected]e41982a72012-11-20 07:16:5199 search_mode_ = new_mode;
[email protected]cd533bf2012-12-04 19:14:59100 ResetInstantTab();
[email protected]0b10c9ff2012-10-09 17:31:55101}
102
[email protected]e41982a72012-11-20 07:16:51103void InstantController::ActiveTabChanged() {
[email protected]b97059d2013-01-26 00:06:40104 LOG_INSTANT_DEBUG_EVENT(this, "ActiveTabChanged");
[email protected]d58688c62013-07-03 23:09:12105 ResetInstantTab();
[email protected]e41982a72012-11-20 07:16:51106}
107
[email protected]3d6a8952012-12-14 03:18:07108void InstantController::TabDeactivated(content::WebContents* contents) {
[email protected]ebc80462013-07-08 18:47:14109 // If user is deactivating an NTP tab, log the number of mouseovers for this
110 // NTP session.
sdefresne51bbec7b2015-08-03 14:18:13111 if (search::IsInstantNTP(contents))
[email protected]6b7063e52013-12-04 22:15:23112 InstantTab::EmitNtpStatistics(contents);
[email protected]a0b84662010-10-04 23:22:04113}
114
[email protected]0c9406632013-02-08 01:13:33115void InstantController::LogDebugEvent(const std::string& info) const {
116 DVLOG(1) << info;
117
118 debug_events_.push_front(std::make_pair(
119 base::Time::Now().ToInternalValue(), info));
120 static const size_t kMaxDebugEventSize = 2000;
121 if (debug_events_.size() > kMaxDebugEventSize)
122 debug_events_.pop_back();
123}
124
[email protected]66be51812013-03-05 22:28:09125void InstantController::ClearDebugEvents() {
126 debug_events_.clear();
127}
128
[email protected]cc459062013-05-02 08:05:25129Profile* InstantController::profile() const {
130 return browser_->profile();
131}
132
[email protected]cc459062013-05-02 08:05:25133InstantTab* InstantController::instant_tab() const {
134 return instant_tab_.get();
135}
136
[email protected]4066a695d2013-06-20 14:08:54137void InstantController::InstantSupportChanged(
138 InstantSupportState instant_support) {
139 // Handle INSTANT_SUPPORT_YES here because InstantPage is not hooked up to the
140 // active tab. Search model changed listener in InstantPage will handle other
141 // cases.
142 if (instant_support != INSTANT_SUPPORT_YES)
143 return;
144
145 ResetInstantTab();
146}
147
[email protected]0c9406632013-02-08 01:13:33148void InstantController::InstantSupportDetermined(
149 const content::WebContents* contents,
150 bool supports_instant) {
[email protected]4ff347e2013-07-22 19:39:00151 DCHECK(IsContentsFrom(instant_tab(), contents));
[email protected]ab3e66672013-03-20 23:32:16152
[email protected]4ff347e2013-07-22 19:39:00153 if (!supports_instant)
154 base::MessageLoop::current()->DeleteSoon(FROM_HERE, instant_tab_.release());
[email protected]0c9406632013-02-08 01:13:33155
[email protected]4ff347e2013-07-22 19:39:00156 content::NotificationService::current()->Notify(
157 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
158 content::Source<InstantController>(this),
159 content::NotificationService::NoDetails());
[email protected]0c9406632013-02-08 01:13:33160}
161
162void InstantController::InstantPageAboutToNavigateMainFrame(
163 const content::WebContents* contents,
164 const GURL& url) {
[email protected]4ff347e2013-07-22 19:39:00165 DCHECK(IsContentsFrom(instant_tab(), contents));
166
167 // The Instant tab navigated. Send it the data it needs to display
168 // properly.
169 UpdateInfoForInstantTab();
[email protected]a6827652012-11-20 23:41:08170}
171
[email protected]cd533bf2012-12-04 19:14:59172void InstantController::ResetInstantTab() {
[email protected]215ebc42013-06-22 00:24:50173 if (!search_mode_.is_origin_default()) {
[email protected]cd533bf2012-12-04 19:14:59174 content::WebContents* active_tab = browser_->GetActiveWebContents();
lucinka.brozkovac25f7fb92014-09-04 06:42:56175 if (!instant_tab_ || active_tab != instant_tab_->web_contents()) {
[email protected]4ff347e2013-07-22 19:39:00176 instant_tab_.reset(new InstantTab(this, browser_->profile()));
[email protected]0c9406632013-02-08 01:13:33177 instant_tab_->Init(active_tab);
[email protected]8cc9e532013-05-06 21:01:47178 UpdateInfoForInstantTab();
[email protected]cd533bf2012-12-04 19:14:59179 }
[email protected]cd533bf2012-12-04 19:14:59180 } else {
181 instant_tab_.reset();
182 }
[email protected]0a387472010-10-07 00:18:20183}
184
[email protected]8cc9e532013-05-06 21:01:47185void InstantController::UpdateInfoForInstantTab() {
186 if (instant_tab_) {
[email protected]6af41782013-06-22 13:49:11187 // Update theme details.
188 InstantService* instant_service = GetInstantService();
[email protected]ed68ae32013-06-29 20:46:48189 if (instant_service) {
[email protected]6af41782013-06-22 13:49:11190 instant_service->UpdateThemeInfo();
[email protected]ed68ae32013-06-29 20:46:48191 instant_service->UpdateMostVisitedItemsInfo();
192 }
[email protected]8cc9e532013-05-06 21:01:47193 }
194}
195
[email protected]6af41782013-06-22 13:49:11196InstantService* InstantController::GetInstantService() const {
197 return InstantServiceFactory::GetForProfile(profile());
198}