1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/search/instant_controller.h"
#include <stddef.h>
#include <utility>
#include "base/prefs/pref_service.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/instant_service.h"
#include "chrome/browser/search/instant_service_factory.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/browser_instant_controller.h"
#include "chrome/browser/ui/search/instant_tab.h"
#include "chrome/browser/ui/search/search_tab_helper.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/search_urls.h"
#include "chrome/common/url_constants.h"
#include "components/search_engines/template_url_service.h"
#include "components/sessions/core/serialized_navigation_entry.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "net/base/escape.h"
#include "net/base/network_change_notifier.h"
#include "url/gurl.h"
namespace {
bool IsContentsFrom(const InstantPage* page,
const content::WebContents* contents) {
return page && (page->web_contents() == contents);
}
// Adds a transient NavigationEntry to the supplied |contents|'s
// NavigationController if the page's URL has not already been updated with the
// supplied |search_terms|. Sets the |search_terms| on the transient entry for
// search terms extraction to work correctly.
void EnsureSearchTermsAreSet(content::WebContents* contents,
const base::string16& search_terms) {
content::NavigationController* controller = &contents->GetController();
// If search terms are already correct or there is already a transient entry
// (there shouldn't be), bail out early.
if (search::GetSearchTerms(contents) == search_terms ||
controller->GetTransientEntry())
return;
const content::NavigationEntry* entry = controller->GetLastCommittedEntry();
scoped_ptr<content::NavigationEntry> transient =
controller->CreateNavigationEntry(
entry->GetURL(),
entry->GetReferrer(),
entry->GetTransitionType(),
false,
std::string(),
contents->GetBrowserContext());
transient->SetExtraData(sessions::kSearchTermsKey, search_terms);
controller->SetTransientEntry(std::move(transient));
SearchTabHelper::FromWebContents(contents)->NavigationEntryUpdated();
}
} // namespace
InstantController::InstantController(BrowserInstantController* browser)
: browser_(browser) {
}
InstantController::~InstantController() {
}
bool InstantController::SubmitQuery(const base::string16& search_terms,
const EmbeddedSearchRequestParams& params) {
if (instant_tab_ && instant_tab_->supports_instant() &&
search_mode_.is_origin_search()) {
// Use |instant_tab_| to run the query if we're already on a search results
// page. (NOTE: in particular, we do not send the query to NTPs.)
SearchTabHelper::FromWebContents(instant_tab_->web_contents())->Submit(
search_terms, params);
instant_tab_->web_contents()->Focus();
EnsureSearchTermsAreSet(instant_tab_->web_contents(), search_terms);
return true;
}
return false;
}
void InstantController::SearchModeChanged(const SearchMode& old_mode,
const SearchMode& new_mode) {
LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf(
"SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode.origin,
old_mode.mode, new_mode.origin, new_mode.mode));
search_mode_ = new_mode;
ResetInstantTab();
}
void InstantController::ActiveTabChanged() {
LOG_INSTANT_DEBUG_EVENT(this, "ActiveTabChanged");
ResetInstantTab();
}
void InstantController::TabDeactivated(content::WebContents* contents) {
// If user is deactivating an NTP tab, log the number of mouseovers for this
// NTP session.
if (search::IsInstantNTP(contents))
InstantTab::EmitNtpStatistics(contents);
}
void InstantController::LogDebugEvent(const std::string& info) const {
DVLOG(1) << info;
debug_events_.push_front(std::make_pair(
base::Time::Now().ToInternalValue(), info));
static const size_t kMaxDebugEventSize = 2000;
if (debug_events_.size() > kMaxDebugEventSize)
debug_events_.pop_back();
}
void InstantController::ClearDebugEvents() {
debug_events_.clear();
}
Profile* InstantController::profile() const {
return browser_->profile();
}
InstantTab* InstantController::instant_tab() const {
return instant_tab_.get();
}
void InstantController::InstantSupportChanged(
InstantSupportState instant_support) {
// Handle INSTANT_SUPPORT_YES here because InstantPage is not hooked up to the
// active tab. Search model changed listener in InstantPage will handle other
// cases.
if (instant_support != INSTANT_SUPPORT_YES)
return;
ResetInstantTab();
}
void InstantController::InstantSupportDetermined(
const content::WebContents* contents,
bool supports_instant) {
DCHECK(IsContentsFrom(instant_tab(), contents));
if (!supports_instant)
base::MessageLoop::current()->DeleteSoon(FROM_HERE, instant_tab_.release());
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
content::Source<InstantController>(this),
content::NotificationService::NoDetails());
}
void InstantController::InstantPageAboutToNavigateMainFrame(
const content::WebContents* contents,
const GURL& url) {
DCHECK(IsContentsFrom(instant_tab(), contents));
// The Instant tab navigated. Send it the data it needs to display
// properly.
UpdateInfoForInstantTab();
}
void InstantController::ResetInstantTab() {
if (!search_mode_.is_origin_default()) {
content::WebContents* active_tab = browser_->GetActiveWebContents();
if (!instant_tab_ || active_tab != instant_tab_->web_contents()) {
instant_tab_.reset(new InstantTab(this, browser_->profile()));
instant_tab_->Init(active_tab);
UpdateInfoForInstantTab();
}
} else {
instant_tab_.reset();
}
}
void InstantController::UpdateInfoForInstantTab() {
if (instant_tab_) {
// Update theme details.
InstantService* instant_service = GetInstantService();
if (instant_service) {
instant_service->UpdateThemeInfo();
instant_service->UpdateMostVisitedItemsInfo();
}
}
}
InstantService* InstantController::GetInstantService() const {
return InstantServiceFactory::GetForProfile(profile());
}
|