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/platform_util.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search/instant_service.h"
13 #include "chrome/browser/search/instant_service_factory.h"
14 #include "chrome/browser/search/search.h"
15 #include "chrome/browser/search_engines/template_url_service_factory.h"
16 #include "chrome/browser/ui/browser_instant_controller.h"
17 #include "chrome/browser/ui/search/instant_tab.h"
18 #include "chrome/browser/ui/search/search_tab_helper.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/search_urls.h"
21 #include "chrome/common/url_constants.h"
22 #include "components/search_engines/template_url_service.h"
23 #include "components/sessions/serialized_navigation_entry.h"
24 #include "content/public/browser/navigation_entry.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/render_process_host.h"
27 #include "content/public/browser/render_widget_host_view.h"
28 #include "content/public/browser/web_contents.h"
29 #include "net/base/escape.h"
30 #include "net/base/network_change_notifier.h"
35 bool IsContentsFrom(const InstantPage
* page
,
36 const content::WebContents
* contents
) {
37 return page
&& (page
->web_contents() == contents
);
40 // 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.
44 void EnsureSearchTermsAreSet(content::WebContents
* contents
,
45 const base::string16
& search_terms
) {
46 content::NavigationController
* controller
= &contents
->GetController();
48 // If search terms are already correct or there is already a transient entry
49 // (there shouldn't be), bail out early.
50 if (chrome::GetSearchTerms(contents
) == search_terms
||
51 controller
->GetTransientEntry())
54 const content::NavigationEntry
* entry
= controller
->GetLastCommittedEntry();
55 content::NavigationEntry
* transient
= controller
->CreateNavigationEntry(
58 entry
->GetTransitionType(),
61 contents
->GetBrowserContext());
62 transient
->SetExtraData(sessions::kSearchTermsKey
, search_terms
);
63 controller
->SetTransientEntry(transient
);
65 SearchTabHelper::FromWebContents(contents
)->NavigationEntryUpdated();
70 InstantController::InstantController(BrowserInstantController
* browser
)
74 InstantController::~InstantController() {
77 bool InstantController::SubmitQuery(const base::string16
& search_terms
,
78 const EmbeddedSearchRequestParams
& params
) {
79 if (instant_tab_
&& instant_tab_
->supports_instant() &&
80 search_mode_
.is_origin_search()) {
81 // Use |instant_tab_| to run the query if we're already on a search results
82 // page. (NOTE: in particular, we do not send the query to NTPs.)
83 SearchTabHelper::FromWebContents(instant_tab_
->web_contents())->Submit(
84 search_terms
, params
);
85 instant_tab_
->web_contents()->Focus();
86 EnsureSearchTermsAreSet(instant_tab_
->web_contents(), search_terms
);
92 void InstantController::SearchModeChanged(const SearchMode
& old_mode
,
93 const SearchMode
& new_mode
) {
94 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf(
95 "SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode
.origin
,
96 old_mode
.mode
, new_mode
.origin
, new_mode
.mode
));
98 search_mode_
= new_mode
;
102 void InstantController::ActiveTabChanged() {
103 LOG_INSTANT_DEBUG_EVENT(this, "ActiveTabChanged");
107 void InstantController::TabDeactivated(content::WebContents
* contents
) {
108 // If user is deactivating an NTP tab, log the number of mouseovers for this
110 if (chrome::IsInstantNTP(contents
))
111 InstantTab::EmitNtpStatistics(contents
);
114 void InstantController::LogDebugEvent(const std::string
& info
) const {
117 debug_events_
.push_front(std::make_pair(
118 base::Time::Now().ToInternalValue(), info
));
119 static const size_t kMaxDebugEventSize
= 2000;
120 if (debug_events_
.size() > kMaxDebugEventSize
)
121 debug_events_
.pop_back();
124 void InstantController::ClearDebugEvents() {
125 debug_events_
.clear();
128 Profile
* InstantController::profile() const {
129 return browser_
->profile();
132 InstantTab
* InstantController::instant_tab() const {
133 return instant_tab_
.get();
136 void InstantController::InstantSupportChanged(
137 InstantSupportState instant_support
) {
138 // Handle INSTANT_SUPPORT_YES here because InstantPage is not hooked up to the
139 // active tab. Search model changed listener in InstantPage will handle other
141 if (instant_support
!= INSTANT_SUPPORT_YES
)
147 void InstantController::InstantSupportDetermined(
148 const content::WebContents
* contents
,
149 bool supports_instant
) {
150 DCHECK(IsContentsFrom(instant_tab(), contents
));
152 if (!supports_instant
)
153 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, instant_tab_
.release());
155 content::NotificationService::current()->Notify(
156 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
157 content::Source
<InstantController
>(this),
158 content::NotificationService::NoDetails());
161 void InstantController::InstantPageAboutToNavigateMainFrame(
162 const content::WebContents
* contents
,
164 DCHECK(IsContentsFrom(instant_tab(), contents
));
166 // The Instant tab navigated. Send it the data it needs to display
168 UpdateInfoForInstantTab();
171 void InstantController::ResetInstantTab() {
172 if (!search_mode_
.is_origin_default()) {
173 content::WebContents
* active_tab
= browser_
->GetActiveWebContents();
174 if (!instant_tab_
|| active_tab
!= instant_tab_
->web_contents()) {
175 instant_tab_
.reset(new InstantTab(this, browser_
->profile()));
176 instant_tab_
->Init(active_tab
);
177 UpdateInfoForInstantTab();
180 instant_tab_
.reset();
184 void InstantController::UpdateInfoForInstantTab() {
186 // Update theme details.
187 InstantService
* instant_service
= GetInstantService();
188 if (instant_service
) {
189 instant_service
->UpdateThemeInfo();
190 instant_service
->UpdateMostVisitedItemsInfo();
195 InstantService
* InstantController::GetInstantService() const {
196 return InstantServiceFactory::GetForProfile(profile());