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_search_prerenderer.h"
22 #include "chrome/browser/ui/search/instant_tab.h"
23 #include "chrome/browser/ui/search/search_tab_helper.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/content_settings_types.h"
26 #include "chrome/common/pref_names.h"
27 #include "chrome/common/search_urls.h"
28 #include "chrome/common/url_constants.h"
29 #include "components/sessions/serialized_navigation_entry.h"
30 #include "content/public/browser/navigation_entry.h"
31 #include "content/public/browser/notification_service.h"
32 #include "content/public/browser/render_process_host.h"
33 #include "content/public/browser/render_widget_host_view.h"
34 #include "content/public/browser/web_contents.h"
35 #include "content/public/browser/web_contents_view.h"
36 #include "net/base/escape.h"
37 #include "net/base/network_change_notifier.h"
40 #if defined(TOOLKIT_VIEWS)
41 #include "ui/views/widget/widget.h"
46 bool IsContentsFrom(const InstantPage* page,
47 const content::WebContents* contents) {
48 return page && (page->contents() == contents);
51 // Adds a transient NavigationEntry to the supplied |contents|'s
52 // NavigationController if the page's URL has not already been updated with the
53 // supplied |search_terms|. Sets the |search_terms| on the transient entry for
54 // search terms extraction to work correctly.
55 void EnsureSearchTermsAreSet(content::WebContents* contents,
56 const base::string16& search_terms) {
57 content::NavigationController* controller = &contents->GetController();
59 // If search terms are already correct or there is already a transient entry
60 // (there shouldn't be), bail out early.
61 if (chrome::GetSearchTerms(contents) == search_terms ||
62 controller->GetTransientEntry())
65 const content::NavigationEntry* entry = controller->GetLastCommittedEntry();
66 content::NavigationEntry* transient = controller->CreateNavigationEntry(
69 entry->GetTransitionType(),
72 contents->GetBrowserContext());
73 transient->SetExtraData(sessions::kSearchTermsKey, search_terms);
74 controller->SetTransientEntry(transient);
76 SearchTabHelper::FromWebContents(contents)->NavigationEntryUpdated();
81 InstantController::InstantController(BrowserInstantController* browser)
85 InstantController::~InstantController() {
88 void InstantController::SetSuggestionToPrefetch(
89 const InstantSuggestion& suggestion) {
91 SearchTabHelper::FromWebContents(instant_tab_->contents())->
92 IsSearchResultsPage()) {
93 if (chrome::ShouldPrefetchSearchResultsOnSRP() ||
94 chrome::ShouldPrefetchSearchResults()) {
95 SearchTabHelper::FromWebContents(instant_tab_->contents())->
96 SetSuggestionToPrefetch(suggestion);
99 if (chrome::ShouldPrefetchSearchResults()) {
100 InstantSearchPrerenderer* prerenderer =
101 InstantSearchPrerenderer::GetForProfile(profile());
103 prerenderer->Prerender(suggestion);
108 bool InstantController::SubmitQuery(const base::string16& search_terms) {
109 if (instant_tab_ && instant_tab_->supports_instant() &&
110 search_mode_.is_origin_search()) {
111 // Use |instant_tab_| to run the query if we're already on a search results
112 // page. (NOTE: in particular, we do not send the query to NTPs.)
113 SearchTabHelper::FromWebContents(instant_tab_->contents())->Submit(
115 instant_tab_->contents()->GetView()->Focus();
116 EnsureSearchTermsAreSet(instant_tab_->contents(), search_terms);
122 void InstantController::SearchModeChanged(const SearchMode& old_mode,
123 const SearchMode& new_mode) {
124 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf(
125 "SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode.origin,
126 old_mode.mode, new_mode.origin, new_mode.mode));
128 search_mode_ = new_mode;
132 void InstantController::ActiveTabChanged() {
133 LOG_INSTANT_DEBUG_EVENT(this, "ActiveTabChanged");
137 void InstantController::TabDeactivated(content::WebContents* contents) {
138 // If user is deactivating an NTP tab, log the number of mouseovers for this
140 if (chrome::IsInstantNTP(contents))
141 InstantTab::EmitNtpStatistics(contents);
144 void InstantController::LogDebugEvent(const std::string& info) const {
147 debug_events_.push_front(std::make_pair(
148 base::Time::Now().ToInternalValue(), info));
149 static const size_t kMaxDebugEventSize = 2000;
150 if (debug_events_.size() > kMaxDebugEventSize)
151 debug_events_.pop_back();
154 void InstantController::ClearDebugEvents() {
155 debug_events_.clear();
158 Profile* InstantController::profile() const {
159 return browser_->profile();
162 InstantTab* InstantController::instant_tab() const {
163 return instant_tab_.get();
166 void InstantController::InstantSupportChanged(
167 InstantSupportState instant_support) {
168 // Handle INSTANT_SUPPORT_YES here because InstantPage is not hooked up to the
169 // active tab. Search model changed listener in InstantPage will handle other
171 if (instant_support != INSTANT_SUPPORT_YES)
177 void InstantController::InstantSupportDetermined(
178 const content::WebContents* contents,
179 bool supports_instant) {
180 DCHECK(IsContentsFrom(instant_tab(), contents));
182 if (!supports_instant)
183 base::MessageLoop::current()->DeleteSoon(FROM_HERE, instant_tab_.release());
185 content::NotificationService::current()->Notify(
186 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
187 content::Source<InstantController>(this),
188 content::NotificationService::NoDetails());
191 void InstantController::InstantPageAboutToNavigateMainFrame(
192 const content::WebContents* contents,
194 DCHECK(IsContentsFrom(instant_tab(), contents));
196 // The Instant tab navigated. Send it the data it needs to display
198 UpdateInfoForInstantTab();
201 void InstantController::ResetInstantTab() {
202 if (!search_mode_.is_origin_default()) {
203 content::WebContents* active_tab = browser_->GetActiveWebContents();
204 if (!instant_tab_ || active_tab != instant_tab_->contents()) {
205 instant_tab_.reset(new InstantTab(this, browser_->profile()));
206 instant_tab_->Init(active_tab);
207 UpdateInfoForInstantTab();
210 instant_tab_.reset();
214 void InstantController::UpdateInfoForInstantTab() {
216 // Update theme details.
217 InstantService* instant_service = GetInstantService();
218 if (instant_service) {
219 instant_service->UpdateThemeInfo();
220 instant_service->UpdateMostVisitedItemsInfo();
225 InstantService* InstantController::GetInstantService() const {
226 return InstantServiceFactory::GetForProfile(profile());