blob: 8dd31cc5aa5d8eebf1da4aec32381eb0c1e663de [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
avi655876a2015-12-25 07:18:157#include <stddef.h>
dcheng4a9d9822015-12-26 22:35:308#include <utility>
avi655876a2015-12-25 07:18:159
fdoray283082bd2016-06-02 20:18:4610#include "base/location.h"
[email protected]f92d6582013-06-10 22:02:3611#include "base/strings/stringprintf.h"
fdoray283082bd2016-06-02 20:18:4612#include "base/threading/thread_task_runner_handle.h"
[email protected]1a7ff742013-07-12 00:26:2313#include "chrome/browser/chrome_notification_types.h"
[email protected]d58688c62013-07-03 23:09:1214#include "chrome/browser/profiles/profile.h"
[email protected]a7b8e43d2013-03-18 18:52:4315#include "chrome/browser/search/instant_service.h"
16#include "chrome/browser/search/instant_service_factory.h"
17#include "chrome/browser/search/search.h"
[email protected]4418dbb2012-10-25 03:21:5418#include "chrome/browser/ui/browser_instant_controller.h"
[email protected]03bb7102013-03-17 22:44:4719#include "chrome/browser/ui/search/instant_tab.h"
[email protected]23f02b22012-10-26 06:08:2720#include "chrome/browser/ui/search/search_tab_helper.h"
blundell47c6d8a2015-09-24 11:06:4021#include "components/sessions/core/serialized_navigation_entry.h"
treib63441182016-07-13 09:29:4422#include "content/public/browser/navigation_controller.h"
[email protected]c55e3b82012-08-09 15:27:0523#include "content/public/browser/navigation_entry.h"
[email protected]ad50def52011-10-19 23:17:0724#include "content/public/browser/notification_service.h"
[email protected]3d6a8952012-12-14 03:18:0725#include "content/public/browser/web_contents.h"
[email protected]a46d67d92013-10-07 22:39:2026#include "url/gurl.h"
[email protected]fd2b9ce2010-08-11 04:03:5727
[email protected]c55e3b82012-08-09 15:27:0528namespace {
29
sfiera948c97f2016-07-13 19:37:5630bool IsContentsFrom(const InstantTab* page,
[email protected]0c9406632013-02-08 01:13:3331 const content::WebContents* contents) {
lucinka.brozkovac25f7fb92014-09-04 06:42:5632 return page && (page->web_contents() == contents);
[email protected]0c9406632013-02-08 01:13:3333}
34
[email protected]0b684262013-02-20 02:18:2135// Adds a transient NavigationEntry to the supplied |contents|'s
36// NavigationController if the page's URL has not already been updated with the
37// supplied |search_terms|. Sets the |search_terms| on the transient entry for
38// search terms extraction to work correctly.
39void EnsureSearchTermsAreSet(content::WebContents* contents,
[email protected]e9273e192013-12-11 17:51:4940 const base::string16& search_terms) {
[email protected]988cf722013-03-08 00:42:0041 content::NavigationController* controller = &contents->GetController();
[email protected]0b684262013-02-20 02:18:2142
43 // If search terms are already correct or there is already a transient entry
44 // (there shouldn't be), bail out early.
sdefresne51bbec7b2015-08-03 14:18:1345 if (search::GetSearchTerms(contents) == search_terms ||
[email protected]988cf722013-03-08 00:42:0046 controller->GetTransientEntry())
[email protected]0b684262013-02-20 02:18:2147 return;
48
[email protected]d8dca262013-11-20 06:16:2449 const content::NavigationEntry* entry = controller->GetLastCommittedEntry();
dcheng9603ab92016-04-08 04:17:3250 std::unique_ptr<content::NavigationEntry> transient =
avi25764702015-06-23 15:43:3751 controller->CreateNavigationEntry(
dcheng9603ab92016-04-08 04:17:3252 entry->GetURL(), entry->GetReferrer(), entry->GetTransitionType(),
53 false, std::string(), contents->GetBrowserContext());
[email protected]40a7e412013-04-29 18:13:0154 transient->SetExtraData(sessions::kSearchTermsKey, search_terms);
dcheng4a9d9822015-12-26 22:35:3055 controller->SetTransientEntry(std::move(transient));
[email protected]0b684262013-02-20 02:18:2156
[email protected]165fe422013-03-27 06:34:0357 SearchTabHelper::FromWebContents(contents)->NavigationEntryUpdated();
[email protected]0b684262013-02-20 02:18:2158}
59
[email protected]b67d0a42012-09-04 20:57:3560} // namespace
61
[email protected]a780c7b22013-08-02 18:36:5962InstantController::InstantController(BrowserInstantController* browser)
[email protected]416d7532014-02-28 22:58:0663 : browser_(browser) {
[email protected]e41982a72012-11-20 07:16:5164}
65
[email protected]6b723f82010-10-05 20:14:2766InstantController::~InstantController() {
[email protected]0c9406632013-02-08 01:13:3367}
68
kmadhusu562c49c82014-11-18 02:08:0369bool InstantController::SubmitQuery(const base::string16& search_terms,
70 const EmbeddedSearchRequestParams& params) {
treibcdd79372016-07-25 13:20:1171 if (!instant_tab_ || !instant_tab_->web_contents())
72 return false;
73
74 SearchTabHelper* search_tab =
75 SearchTabHelper::FromWebContents(instant_tab_->web_contents());
76 if (!search_tab->SupportsInstant() || !search_mode_.is_origin_search())
77 return false;
78
79 // Use |instant_tab_| to run the query if we're already on a search results
80 // page. (NOTE: in particular, we do not send the query to NTPs.)
81 search_tab->Submit(search_terms, params);
82 instant_tab_->web_contents()->Focus();
83 EnsureSearchTermsAreSet(instant_tab_->web_contents(), search_terms);
84 return true;
[email protected]413558cb2013-06-10 16:44:4585}
86
[email protected]165fe422013-03-27 06:34:0387void InstantController::SearchModeChanged(const SearchMode& old_mode,
88 const SearchMode& new_mode) {
treibcdd79372016-07-25 13:20:1189 LogDebugEvent(base::StringPrintf(
[email protected]b97059d2013-01-26 00:06:4090 "SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode.origin,
91 old_mode.mode, new_mode.origin, new_mode.mode));
[email protected]cd533bf2012-12-04 19:14:5992
[email protected]e41982a72012-11-20 07:16:5193 search_mode_ = new_mode;
[email protected]cd533bf2012-12-04 19:14:5994 ResetInstantTab();
[email protected]0b10c9ff2012-10-09 17:31:5595}
96
[email protected]e41982a72012-11-20 07:16:5197void InstantController::ActiveTabChanged() {
treibcdd79372016-07-25 13:20:1198 LogDebugEvent("ActiveTabChanged");
[email protected]d58688c62013-07-03 23:09:1299 ResetInstantTab();
[email protected]e41982a72012-11-20 07:16:51100}
101
[email protected]0c9406632013-02-08 01:13:33102void InstantController::LogDebugEvent(const std::string& info) const {
103 DVLOG(1) << info;
104
105 debug_events_.push_front(std::make_pair(
106 base::Time::Now().ToInternalValue(), info));
107 static const size_t kMaxDebugEventSize = 2000;
108 if (debug_events_.size() > kMaxDebugEventSize)
109 debug_events_.pop_back();
110}
111
[email protected]66be51812013-03-05 22:28:09112void InstantController::ClearDebugEvents() {
113 debug_events_.clear();
114}
115
[email protected]4066a695d2013-06-20 14:08:54116void InstantController::InstantSupportChanged(
117 InstantSupportState instant_support) {
sfiera948c97f2016-07-13 19:37:56118 // Handle INSTANT_SUPPORT_YES here because InstantTab is not hooked up to the
119 // active tab. Search model changed listener in InstantTab will handle other
[email protected]4066a695d2013-06-20 14:08:54120 // cases.
121 if (instant_support != INSTANT_SUPPORT_YES)
122 return;
123
124 ResetInstantTab();
125}
126
[email protected]0c9406632013-02-08 01:13:33127void InstantController::InstantSupportDetermined(
128 const content::WebContents* contents,
129 bool supports_instant) {
treibcdd79372016-07-25 13:20:11130 DCHECK(IsContentsFrom(instant_tab_.get(), contents));
[email protected]ab3e66672013-03-20 23:32:16131
[email protected]4ff347e2013-07-22 19:39:00132 if (!supports_instant)
fdoray283082bd2016-06-02 20:18:46133 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE,
134 instant_tab_.release());
[email protected]0c9406632013-02-08 01:13:33135
[email protected]4ff347e2013-07-22 19:39:00136 content::NotificationService::current()->Notify(
137 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
138 content::Source<InstantController>(this),
139 content::NotificationService::NoDetails());
[email protected]0c9406632013-02-08 01:13:33140}
141
sfiera948c97f2016-07-13 19:37:56142void InstantController::InstantTabAboutToNavigateMainFrame(
[email protected]0c9406632013-02-08 01:13:33143 const content::WebContents* contents,
144 const GURL& url) {
treibcdd79372016-07-25 13:20:11145 DCHECK(IsContentsFrom(instant_tab_.get(), contents));
[email protected]4ff347e2013-07-22 19:39:00146
147 // The Instant tab navigated. Send it the data it needs to display
148 // properly.
149 UpdateInfoForInstantTab();
[email protected]a6827652012-11-20 23:41:08150}
151
[email protected]cd533bf2012-12-04 19:14:59152void InstantController::ResetInstantTab() {
[email protected]215ebc42013-06-22 00:24:50153 if (!search_mode_.is_origin_default()) {
[email protected]cd533bf2012-12-04 19:14:59154 content::WebContents* active_tab = browser_->GetActiveWebContents();
lucinka.brozkovac25f7fb92014-09-04 06:42:56155 if (!instant_tab_ || active_tab != instant_tab_->web_contents()) {
treibcdd79372016-07-25 13:20:11156 instant_tab_.reset(new InstantTab(this, active_tab));
157 instant_tab_->Init();
[email protected]8cc9e532013-05-06 21:01:47158 UpdateInfoForInstantTab();
[email protected]cd533bf2012-12-04 19:14:59159 }
[email protected]cd533bf2012-12-04 19:14:59160 } else {
161 instant_tab_.reset();
162 }
[email protected]0a387472010-10-07 00:18:20163}
164
[email protected]8cc9e532013-05-06 21:01:47165void InstantController::UpdateInfoForInstantTab() {
166 if (instant_tab_) {
[email protected]6af41782013-06-22 13:49:11167 // Update theme details.
168 InstantService* instant_service = GetInstantService();
[email protected]ed68ae32013-06-29 20:46:48169 if (instant_service) {
[email protected]6af41782013-06-22 13:49:11170 instant_service->UpdateThemeInfo();
[email protected]ed68ae32013-06-29 20:46:48171 instant_service->UpdateMostVisitedItemsInfo();
172 }
[email protected]8cc9e532013-05-06 21:01:47173 }
174}
175
[email protected]6af41782013-06-22 13:49:11176InstantService* InstantController::GetInstantService() const {
treibcdd79372016-07-25 13:20:11177 return InstantServiceFactory::GetForProfile(browser_->profile());
[email protected]6af41782013-06-22 13:49:11178}