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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
// 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/renderer/searchbox/searchbox.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/url_constants.h"
#include "chrome/renderer/searchbox/searchbox_extension.h"
#include "content/public/renderer/render_view.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
namespace {
// Size of the results cache.
const size_t kMaxInstantAutocompleteResultItemCacheSize = 100;
}
SearchBox::SearchBox(content::RenderView* render_view)
: content::RenderViewObserver(render_view),
content::RenderViewObserverTracker<SearchBox>(render_view),
verbatim_(false),
selection_start_(0),
selection_end_(0),
start_margin_(0),
is_key_capture_enabled_(false),
display_instant_results_(false),
omnibox_font_size_(0),
autocomplete_results_cache_(kMaxInstantAutocompleteResultItemCacheSize),
most_visited_items_cache_(kMaxInstantMostVisitedItemCacheSize) {
}
SearchBox::~SearchBox() {
}
void SearchBox::SetSuggestions(
const std::vector<InstantSuggestion>& suggestions) {
if (!suggestions.empty() &&
suggestions[0].behavior == INSTANT_COMPLETE_REPLACE) {
query_ = suggestions[0].text;
verbatim_ = true;
selection_start_ = selection_end_ = query_.size();
}
// Explicitly allow empty vector to be sent to the browser.
render_view()->Send(new ChromeViewHostMsg_SetSuggestions(
render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions));
}
void SearchBox::ClearQuery() {
query_.clear();
}
void SearchBox::ShowInstantOverlay(int height, InstantSizeUnits units) {
render_view()->Send(new ChromeViewHostMsg_ShowInstantOverlay(
render_view()->GetRoutingID(), render_view()->GetPageId(), height,
units));
}
void SearchBox::FocusOmnibox() {
render_view()->Send(new ChromeViewHostMsg_FocusOmnibox(
render_view()->GetRoutingID(), render_view()->GetPageId()));
}
void SearchBox::StartCapturingKeyStrokes() {
render_view()->Send(new ChromeViewHostMsg_StartCapturingKeyStrokes(
render_view()->GetRoutingID(), render_view()->GetPageId()));
}
void SearchBox::StopCapturingKeyStrokes() {
render_view()->Send(new ChromeViewHostMsg_StopCapturingKeyStrokes(
render_view()->GetRoutingID(), render_view()->GetPageId()));
}
void SearchBox::NavigateToURL(const GURL& url,
content::PageTransition transition,
WindowOpenDisposition disposition) {
render_view()->Send(new ChromeViewHostMsg_SearchBoxNavigate(
render_view()->GetRoutingID(), render_view()->GetPageId(),
url, transition, disposition));
}
void SearchBox::DeleteMostVisitedItem(
InstantRestrictedID most_visited_item_id) {
render_view()->Send(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
render_view()->GetRoutingID(), most_visited_item_id));
}
void SearchBox::UndoMostVisitedDeletion(
InstantRestrictedID most_visited_item_id) {
render_view()->Send(new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
render_view()->GetRoutingID(), most_visited_item_id));
}
void SearchBox::UndoAllMostVisitedDeletions() {
render_view()->Send(
new ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
render_view()->GetRoutingID()));
}
void SearchBox::ShowBars() {
DVLOG(1) << render_view() << " ShowBars";
render_view()->Send(new ChromeViewHostMsg_SearchBoxShowBars(
render_view()->GetRoutingID(), render_view()->GetPageId()));
}
void SearchBox::HideBars() {
DVLOG(1) << render_view() << " HideBars";
render_view()->Send(new ChromeViewHostMsg_SearchBoxHideBars(
render_view()->GetRoutingID(), render_view()->GetPageId()));
}
int SearchBox::GetStartMargin() const {
return static_cast<int>(start_margin_ / GetZoom());
}
gfx::Rect SearchBox::GetPopupBounds() const {
double zoom = GetZoom();
return gfx::Rect(static_cast<int>(popup_bounds_.x() / zoom),
static_cast<int>(popup_bounds_.y() / zoom),
static_cast<int>(popup_bounds_.width() / zoom),
static_cast<int>(popup_bounds_.height() / zoom));
}
void SearchBox::GetAutocompleteResults(
std::vector<InstantAutocompleteResultIDPair>* results) const {
autocomplete_results_cache_.GetCurrentItems(results);
}
bool SearchBox::GetAutocompleteResultWithID(
InstantRestrictedID autocomplete_result_id,
InstantAutocompleteResult* result) const {
return autocomplete_results_cache_.GetItemWithRestrictedID(
autocomplete_result_id, result);
}
const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() {
return theme_info_;
}
bool SearchBox::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(SearchBox, message)
IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange)
IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit)
IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel)
IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize)
IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxMarginChange, OnMarginChange)
IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxBarsHidden, OnBarsHidden)
IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant,
OnDetermineIfPageSupportsInstant)
IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxAutocompleteResults,
OnAutocompleteResults)
IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxUpOrDownKeyPressed,
OnUpOrDownKeyPressed)
IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancelSelection,
OnCancelSelection)
IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSetDisplayInstantResults,
OnSetDisplayInstantResults)
IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxKeyCaptureChanged,
OnKeyCaptureChange)
IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxThemeChanged,
OnThemeChanged)
IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxFontInformation,
OnFontInformationReceived)
IPC_MESSAGE_HANDLER(
ChromeViewMsg_SearchBoxGrantChromeSearchAccessFromOrigin,
OnGrantChromeSearchAccessFromOrigin)
IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxMostVisitedItemsChanged,
OnMostVisitedChanged)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void SearchBox::DidClearWindowObject(WebKit::WebFrame* frame) {
extensions_v8::SearchBoxExtension::DispatchOnWindowReady(frame);
}
void SearchBox::OnChange(const string16& query,
bool verbatim,
size_t selection_start,
size_t selection_end) {
query_ = query;
verbatim_ = verbatim;
selection_start_ = selection_start;
selection_end_ = selection_end;
if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
DVLOG(1) << render_view() << " OnChange";
extensions_v8::SearchBoxExtension::DispatchChange(
render_view()->GetWebView()->mainFrame());
}
}
void SearchBox::OnSubmit(const string16& query) {
query_ = query;
verbatim_ = true;
selection_start_ = selection_end_ = query_.size();
if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
DVLOG(1) << render_view() << " OnSubmit";
extensions_v8::SearchBoxExtension::DispatchSubmit(
render_view()->GetWebView()->mainFrame());
}
Reset();
}
void SearchBox::OnCancel(const string16& query) {
query_ = query;
verbatim_ = true;
selection_start_ = selection_end_ = query_.size();
if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
DVLOG(1) << render_view() << " OnCancel";
extensions_v8::SearchBoxExtension::DispatchCancel(
render_view()->GetWebView()->mainFrame());
}
Reset();
}
void SearchBox::OnPopupResize(const gfx::Rect& bounds) {
popup_bounds_ = bounds;
if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
DVLOG(1) << render_view() << " OnPopupResize";
extensions_v8::SearchBoxExtension::DispatchResize(
render_view()->GetWebView()->mainFrame());
}
}
void SearchBox::OnMarginChange(int margin, int width) {
start_margin_ = margin;
// Override only the width parameter of the popup bounds.
popup_bounds_.set_width(width);
if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
extensions_v8::SearchBoxExtension::DispatchMarginChange(
render_view()->GetWebView()->mainFrame());
}
}
void SearchBox::OnBarsHidden() {
if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
extensions_v8::SearchBoxExtension::DispatchBarsHidden(
render_view()->GetWebView()->mainFrame());
}
}
void SearchBox::OnDetermineIfPageSupportsInstant() {
if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant(
render_view()->GetWebView()->mainFrame());
DVLOG(1) << render_view() << " PageSupportsInstant: " << result;
render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined(
render_view()->GetRoutingID(), render_view()->GetPageId(), result));
}
}
void SearchBox::OnAutocompleteResults(
const std::vector<InstantAutocompleteResult>& results) {
autocomplete_results_cache_.AddItems(results);
if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
DVLOG(1) << render_view() << " OnAutocompleteResults";
extensions_v8::SearchBoxExtension::DispatchAutocompleteResults(
render_view()->GetWebView()->mainFrame());
}
}
void SearchBox::OnUpOrDownKeyPressed(int count) {
if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
DVLOG(1) << render_view() << " OnKeyPress: " << count;
extensions_v8::SearchBoxExtension::DispatchUpOrDownKeyPress(
render_view()->GetWebView()->mainFrame(), count);
}
}
void SearchBox::OnCancelSelection(const string16& query) {
// TODO(sreeram): crbug.com/176101 The state reset below are somewhat wrong.
query_ = query;
verbatim_ = true;
selection_start_ = selection_end_ = query_.size();
if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
DVLOG(1) << render_view() << " OnKeyPress ESC";
extensions_v8::SearchBoxExtension::DispatchEscKeyPress(
render_view()->GetWebView()->mainFrame());
}
}
void SearchBox::OnKeyCaptureChange(bool is_key_capture_enabled) {
if (is_key_capture_enabled != is_key_capture_enabled_ &&
render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
is_key_capture_enabled_ = is_key_capture_enabled;
DVLOG(1) << render_view() << " OnKeyCaptureChange";
extensions_v8::SearchBoxExtension::DispatchKeyCaptureChange(
render_view()->GetWebView()->mainFrame());
}
}
void SearchBox::OnSetDisplayInstantResults(bool display_instant_results) {
display_instant_results_ = display_instant_results;
}
void SearchBox::OnThemeChanged(const ThemeBackgroundInfo& theme_info) {
theme_info_ = theme_info;
if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
extensions_v8::SearchBoxExtension::DispatchThemeChange(
render_view()->GetWebView()->mainFrame());
}
}
void SearchBox::OnFontInformationReceived(const string16& omnibox_font,
size_t omnibox_font_size) {
omnibox_font_ = omnibox_font;
omnibox_font_size_ = omnibox_font_size;
}
void SearchBox::OnGrantChromeSearchAccessFromOrigin(const GURL& origin_url) {
string16 chrome_search_scheme(ASCIIToUTF16(chrome::kChromeSearchScheme));
WebKit::WebSecurityPolicy::addOriginAccessWhitelistEntry(
origin_url,
chrome_search_scheme,
ASCIIToUTF16(chrome::kChromeUIFaviconHost),
false);
WebKit::WebSecurityPolicy::addOriginAccessWhitelistEntry(
origin_url,
chrome_search_scheme,
ASCIIToUTF16(chrome::kChromeUIThemeHost),
false);
WebKit::WebSecurityPolicy::addOriginAccessWhitelistEntry(
origin_url,
chrome_search_scheme,
ASCIIToUTF16(chrome::kChromeUIThumbnailHost),
false);
}
double SearchBox::GetZoom() const {
WebKit::WebView* web_view = render_view()->GetWebView();
if (web_view) {
double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel());
if (zoom != 0)
return zoom;
}
return 1.0;
}
void SearchBox::Reset() {
query_.clear();
verbatim_ = false;
selection_start_ = 0;
selection_end_ = 0;
popup_bounds_ = gfx::Rect();
start_margin_ = 0;
is_key_capture_enabled_ = false;
theme_info_ = ThemeBackgroundInfo();
// Don't reset display_instant_results_ to prevent clearing it on committed
// results pages in extended mode. Otherwise resetting it is a no-op because
// a new loader is created when it changes; see crbug.com/164662.
// Also don't reset omnibox_font_ or omnibox_font_size_ since it never
// changes.
}
void SearchBox::OnMostVisitedChanged(
const std::vector<InstantMostVisitedItemIDPair>& items) {
most_visited_items_cache_.AddItemsWithRestrictedID(items);
if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) {
extensions_v8::SearchBoxExtension::DispatchMostVisitedChanged(
render_view()->GetWebView()->mainFrame());
}
}
void SearchBox::GetMostVisitedItems(
std::vector<InstantMostVisitedItemIDPair>* items) const {
return most_visited_items_cache_.GetCurrentItems(items);
}
bool SearchBox::GetMostVisitedItemWithID(
InstantRestrictedID most_visited_item_id,
InstantMostVisitedItem* item) const {
return most_visited_items_cache_.GetItemWithRestrictedID(most_visited_item_id,
item);
}
|