1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/search/instant_controller.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/content_settings/content_settings_provider.h"
11 #include "chrome/browser/content_settings/host_content_settings_map.h"
12 #include "chrome/browser/platform_util.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/search/instant_service.h"
15 #include "chrome/browser/search/instant_service_factory.h"
16 #include "chrome/browser/search/search.h"
17 #include "chrome/browser/search_engines/search_terms_data.h"
18 #include "chrome/browser/search_engines/template_url_service.h"
19 #include "chrome/browser/search_engines/template_url_service_factory.h"
20 #include "chrome/browser/ui/browser_instant_controller.h"
21 #include "chrome/browser/ui/search/instant_tab.h"
22 #include "chrome/browser/ui/search/search_tab_helper.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/content_settings_types.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/common/search_urls.h"
27 #include "chrome/common/url_constants.h"
28 #include "components/sessions/serialized_navigation_entry.h"
29 #include "content/public/browser/navigation_entry.h"
30 #include "content/public/browser/notification_service.h"
31 #include "content/public/browser/render_process_host.h"
32 #include "content/public/browser/render_widget_host_view.h"
33 #include "content/public/browser/web_contents.h"
34 #include "net/base/escape.h"
35 #include "net/base/network_change_notifier.h"
38 #if defined(TOOLKIT_VIEWS)
39 #include "ui/views/widget/widget.h"
44 bool IsContentsFrom(const InstantPage* page,
45 const content::WebContents* contents) {
46 return page && (page->contents() == contents);
49 // Adds a transient NavigationEntry to the supplied |contents|'s
50 // NavigationController if the page's URL has not already been updated with the
51 // supplied |search_terms|. Sets the |search_terms| on the transient entry for
52 // search terms extraction to work correctly.
53 void EnsureSearchTermsAreSet(content::WebContents* contents,
54 const base::string16& search_terms) {
55 content::NavigationController* controller = &contents->GetController();
57 // If search terms are already correct or there is already a transient entry
58 // (there shouldn't be), bail out early.
59 if (chrome::GetSearchTerms(contents) == search_terms ||
60 controller->GetTransientEntry())
63 const content::NavigationEntry* entry = controller->GetLastCommittedEntry();
64 content::NavigationEntry* transient = controller->CreateNavigationEntry(
67 entry->GetTransitionType(),
70 contents->GetBrowserContext());
71 transient->SetExtraData(sessions::kSearchTermsKey, search_terms);
72 controller->SetTransientEntry(transient);
74 SearchTabHelper::FromWebContents(contents)->NavigationEntryUpdated();
79 InstantController::InstantController(BrowserInstantController* browser)
83 InstantController::~InstantController() {
86 bool InstantController::SubmitQuery(const base::string16& search_terms) {
87 if (instant_tab_ && instant_tab_->supports_instant() &&
88 search_mode_.is_origin_search()) {
89 // Use |instant_tab_| to run the query if we're already on a search results
90 // page. (NOTE: in particular, we do not send the query to NTPs.)
91 SearchTabHelper::FromWebContents(instant_tab_->contents())->Submit(
93 instant_tab_->contents()->Focus();
94 EnsureSearchTermsAreSet(instant_tab_->contents(), search_terms);
100 void InstantController::SearchModeChanged(const SearchMode& old_mode,
101 const SearchMode& new_mode) {
102 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf(
103 "SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode.origin,
104 old_mode.mode, new_mode.origin, new_mode.mode));
106 search_mode_ = new_mode;
110 void InstantController::ActiveTabChanged() {
111 LOG_INSTANT_DEBUG_EVENT(this, "ActiveTabChanged");
115 void InstantController::TabDeactivated(content::WebContents* contents) {
116 // If user is deactivating an NTP tab, log the number of mouseovers for this
118 if (chrome::IsInstantNTP(contents))
119 InstantTab::EmitNtpStatistics(contents);
122 void InstantController::LogDebugEvent(const std::string& info) const {
125 debug_events_.push_front(std::make_pair(
126 base::Time::Now().ToInternalValue(), info));
127 static const size_t kMaxDebugEventSize = 2000;
128 if (debug_events_.size() > kMaxDebugEventSize)
129 debug_events_.pop_back();
132 void InstantController::ClearDebugEvents() {
133 debug_events_.clear();
136 Profile* InstantController::profile() const {
137 return browser_->profile();
140 InstantTab* InstantController::instant_tab() const {
141 return instant_tab_.get();
144 void InstantController::InstantSupportChanged(
145 InstantSupportState instant_support) {
146 // Handle INSTANT_SUPPORT_YES here because InstantPage is not hooked up to the
147 // active tab. Search model changed listener in InstantPage will handle other
149 if (instant_support != INSTANT_SUPPORT_YES)
155 void InstantController::InstantSupportDetermined(
156 const content::WebContents* contents,
157 bool supports_instant) {
158 DCHECK(IsContentsFrom(instant_tab(), contents));
160 if (!supports_instant)
161 base::MessageLoop::current()->DeleteSoon(FROM_HERE, instant_tab_.release());
163 content::NotificationService::current()->Notify(
164 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
165 content::Source<InstantController>(this),
166 content::NotificationService::NoDetails());
169 void InstantController::InstantPageAboutToNavigateMainFrame(
170 const content::WebContents* contents,
172 DCHECK(IsContentsFrom(instant_tab(), contents));
174 // The Instant tab navigated. Send it the data it needs to display
176 UpdateInfoForInstantTab();
179 void InstantController::ResetInstantTab() {
180 if (!search_mode_.is_origin_default()) {
181 content::WebContents* active_tab = browser_->GetActiveWebContents();
182 if (!instant_tab_ || active_tab != instant_tab_->contents()) {
183 instant_tab_.reset(new InstantTab(this, browser_->profile()));
184 instant_tab_->Init(active_tab);
185 UpdateInfoForInstantTab();
188 instant_tab_.reset();
192 void InstantController::UpdateInfoForInstantTab() {
194 // Update theme details.
195 InstantService* instant_service = GetInstantService();
196 if (instant_service) {
197 instant_service->UpdateThemeInfo();
198 instant_service->UpdateMostVisitedItemsInfo();
203 InstantService* InstantController::GetInstantService() const {
204 return InstantServiceFactory::GetForProfile(profile());