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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
// Copyright 2013 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_page.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/ui/search/instant_ipc_sender.h"
#include "chrome/browser/ui/search/search_model.h"
#include "chrome/browser/ui/search/search_tab_helper.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/frame_navigate_params.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/font.h"
InstantPage::Delegate::~Delegate() {
}
InstantPage::~InstantPage() {
if (contents())
SearchTabHelper::FromWebContents(contents())->model()->RemoveObserver(this);
}
bool InstantPage::supports_instant() const {
return contents() ?
SearchTabHelper::FromWebContents(contents())->SupportsInstant() : false;
}
const std::string& InstantPage::instant_url() const {
return instant_url_;
}
bool InstantPage::IsLocal() const {
return contents() &&
(contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) ||
contents()->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl));
}
void InstantPage::InitializeFonts() {
#if defined(OS_MACOSX)
// This value should be kept in sync with OmniboxViewMac::GetFieldFont.
const gfx::Font& omnibox_font =
ui::ResourceBundle::GetSharedInstance().GetFont(
ui::ResourceBundle::MediumFont).DeriveFont(1);
#else
const gfx::Font& omnibox_font =
ui::ResourceBundle::GetSharedInstance().GetFont(
ui::ResourceBundle::MediumFont);
#endif
sender()->SetFontInformation(UTF8ToUTF16(omnibox_font.GetFontName()),
omnibox_font.GetFontSize());
}
InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url,
bool is_incognito)
: delegate_(delegate),
ipc_sender_(InstantIPCSender::Create(is_incognito)),
instant_url_(instant_url),
is_incognito_(is_incognito) {
}
void InstantPage::SetContents(content::WebContents* web_contents) {
ClearContents();
if (!web_contents)
return;
sender()->SetContents(web_contents);
Observe(web_contents);
SearchModel* model = SearchTabHelper::FromWebContents(contents())->model();
model->AddObserver(this);
// Already know whether the page supports instant.
if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN)
InstantSupportDetermined(model->instant_support() == INSTANT_SUPPORT_YES);
}
bool InstantPage::ShouldProcessRenderViewCreated() {
return false;
}
bool InstantPage::ShouldProcessRenderViewGone() {
return false;
}
bool InstantPage::ShouldProcessAboutToNavigateMainFrame() {
return false;
}
bool InstantPage::ShouldProcessSetSuggestions() {
return false;
}
bool InstantPage::ShouldProcessShowInstantOverlay() {
return false;
}
bool InstantPage::ShouldProcessFocusOmnibox() {
return false;
}
bool InstantPage::ShouldProcessNavigateToURL() {
return false;
}
bool InstantPage::ShouldProcessDeleteMostVisitedItem() {
return false;
}
bool InstantPage::ShouldProcessUndoMostVisitedDeletion() {
return false;
}
bool InstantPage::ShouldProcessUndoAllMostVisitedDeletions() {
return false;
}
void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) {
if (ShouldProcessRenderViewCreated())
delegate_->InstantPageRenderViewCreated(contents());
}
bool InstantPage::OnMessageReceived(const IPC::Message& message) {
if (is_incognito_)
return false;
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(InstantPage, message)
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions)
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay,
OnShowInstantOverlay)
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox)
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate,
OnSearchBoxNavigate);
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem,
OnDeleteMostVisitedItem);
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion,
OnUndoMostVisitedDeletion);
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions,
OnUndoAllMostVisitedDeletions);
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void InstantPage::RenderViewGone(base::TerminationStatus /* status */) {
if (ShouldProcessRenderViewGone())
delegate_->InstantPageRenderViewGone(contents());
}
void InstantPage::DidCommitProvisionalLoadForFrame(
int64 /* frame_id */,
bool is_main_frame,
const GURL& url,
content::PageTransition /* transition_type */,
content::RenderViewHost* /* render_view_host */) {
if (is_main_frame && ShouldProcessAboutToNavigateMainFrame())
delegate_->InstantPageAboutToNavigateMainFrame(contents(), url);
}
void InstantPage::DidNavigateMainFrame(
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& /* params */) {
// A 204 can be sent by the search provider as a lightweight signal
// to fall back to the local page, and we obviously want to fall back
// if we get any response code that indicates an error.
if (details.http_status_code == 204 || details.http_status_code >= 400)
delegate_->InstantPageLoadFailed(contents());
}
void InstantPage::DidFailProvisionalLoad(
int64 /* frame_id */,
bool is_main_frame,
const GURL& /* validated_url */,
int /* error_code */,
const string16& /* error_description */,
content::RenderViewHost* /* render_view_host */) {
if (is_main_frame)
delegate_->InstantPageLoadFailed(contents());
}
void InstantPage::ModelChanged(const SearchModel::State& old_state,
const SearchModel::State& new_state) {
if (old_state.instant_support != new_state.instant_support)
InstantSupportDetermined(new_state.instant_support == INSTANT_SUPPORT_YES);
}
void InstantPage::InstantSupportDetermined(bool supports_instant) {
delegate_->InstantSupportDetermined(contents(), supports_instant);
// If the page doesn't support Instant, stop listening to it.
if (!supports_instant)
ClearContents();
}
void InstantPage::OnSetSuggestions(
int page_id,
const std::vector<InstantSuggestion>& suggestions) {
if (!contents()->IsActiveEntry(page_id))
return;
SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
if (!ShouldProcessSetSuggestions())
return;
delegate_->SetSuggestions(contents(), suggestions);
}
void InstantPage::OnShowInstantOverlay(int page_id,
int height,
InstantSizeUnits units) {
if (!contents()->IsActiveEntry(page_id))
return;
SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
delegate_->LogDropdownShown();
if (!ShouldProcessShowInstantOverlay())
return;
delegate_->ShowInstantOverlay(contents(), height, units);
}
void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) {
if (!contents()->IsActiveEntry(page_id))
return;
SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
if (!ShouldProcessFocusOmnibox())
return;
delegate_->FocusOmnibox(contents(), state);
}
void InstantPage::OnSearchBoxNavigate(int page_id,
const GURL& url,
content::PageTransition transition,
WindowOpenDisposition disposition,
bool is_search_type) {
if (!contents()->IsActiveEntry(page_id))
return;
SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
if (!ShouldProcessNavigateToURL())
return;
delegate_->NavigateToURL(
contents(), url, transition, disposition, is_search_type);
}
void InstantPage::OnDeleteMostVisitedItem(int page_id, const GURL& url) {
if (!contents()->IsActiveEntry(page_id))
return;
SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
if (!ShouldProcessDeleteMostVisitedItem())
return;
delegate_->DeleteMostVisitedItem(url);
}
void InstantPage::OnUndoMostVisitedDeletion(int page_id, const GURL& url) {
if (!contents()->IsActiveEntry(page_id))
return;
SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
if (!ShouldProcessUndoMostVisitedDeletion())
return;
delegate_->UndoMostVisitedDeletion(url);
}
void InstantPage::OnUndoAllMostVisitedDeletions(int page_id) {
if (!contents()->IsActiveEntry(page_id))
return;
SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
if (!ShouldProcessUndoAllMostVisitedDeletions())
return;
delegate_->UndoAllMostVisitedDeletions();
}
void InstantPage::ClearContents() {
if (contents())
SearchTabHelper::FromWebContents(contents())->model()->RemoveObserver(this);
sender()->SetContents(NULL);
Observe(NULL);
}
|