OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/searchbox/searchbox.h" | 5 #include "chrome/renderer/searchbox/searchbox.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/common/render_messages.h" | 8 #include "chrome/common/render_messages.h" |
9 #include "chrome/renderer/searchbox/searchbox_extension.h" | 9 #include "chrome/renderer/searchbox/searchbox_extension.h" |
10 #include "content/public/renderer/render_view.h" | 10 #include "content/public/renderer/render_view.h" |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // Prefix for a thumbnail URL. | 15 // Prefix for a thumbnail URL. |
16 const char kThumbnailUrlPrefix[] = "chrome-search://thumb/"; | 16 const char kThumbnailUrlPrefix[] = "chrome-search://thumb/"; |
17 | 17 |
18 // Prefix for a thumbnail URL. | 18 // Prefix for a thumbnail URL. |
19 const char kFaviconUrlPrefix[] = "chrome-search://favicon/"; | 19 const char kFaviconUrlPrefix[] = "chrome-search://favicon/"; |
20 | 20 |
| 21 // Size of the results cache. |
| 22 const size_t kMaxResultsCacheSize = 1000; |
21 } | 23 } |
22 | 24 |
23 SearchBox::SearchBox(content::RenderView* render_view) | 25 SearchBox::SearchBox(content::RenderView* render_view) |
24 : content::RenderViewObserver(render_view), | 26 : content::RenderViewObserver(render_view), |
25 content::RenderViewObserverTracker<SearchBox>(render_view), | 27 content::RenderViewObserverTracker<SearchBox>(render_view), |
26 verbatim_(false), | 28 verbatim_(false), |
27 selection_start_(0), | 29 selection_start_(0), |
28 selection_end_(0), | 30 selection_end_(0), |
29 results_base_(0), | |
30 start_margin_(0), | 31 start_margin_(0), |
31 last_results_base_(0), | 32 autocomplete_results_cache_(kMaxResultsCacheSize), |
32 is_key_capture_enabled_(false), | 33 is_key_capture_enabled_(false), |
33 display_instant_results_(false), | 34 display_instant_results_(false), |
34 omnibox_font_size_(0), | 35 omnibox_font_size_(0), |
35 last_restricted_id_(0) { | 36 last_restricted_id_(0) { |
36 } | 37 } |
37 | 38 |
38 SearchBox::~SearchBox() { | 39 SearchBox::~SearchBox() { |
39 } | 40 } |
40 | 41 |
41 void SearchBox::SetSuggestions( | 42 void SearchBox::SetSuggestions( |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 } | 100 } |
100 | 101 |
101 gfx::Rect SearchBox::GetPopupBounds() const { | 102 gfx::Rect SearchBox::GetPopupBounds() const { |
102 double zoom = GetZoom(); | 103 double zoom = GetZoom(); |
103 return gfx::Rect(static_cast<int>(popup_bounds_.x() / zoom), | 104 return gfx::Rect(static_cast<int>(popup_bounds_.x() / zoom), |
104 static_cast<int>(popup_bounds_.y() / zoom), | 105 static_cast<int>(popup_bounds_.y() / zoom), |
105 static_cast<int>(popup_bounds_.width() / zoom), | 106 static_cast<int>(popup_bounds_.width() / zoom), |
106 static_cast<int>(popup_bounds_.height() / zoom)); | 107 static_cast<int>(popup_bounds_.height() / zoom)); |
107 } | 108 } |
108 | 109 |
109 const std::vector<InstantAutocompleteResult>& | 110 void SearchBox::GetAutocompleteResults( |
110 SearchBox::GetAutocompleteResults() { | 111 std::vector<std::pair<size_t, InstantAutocompleteResult> >* |
111 // Remember the last requested autocomplete_results to account for race | 112 results) const { |
112 // conditions between autocomplete providers returning new data and the user | 113 autocomplete_results_cache_.GetCurrentItems(results); |
113 // clicking on a suggestion. | |
114 last_autocomplete_results_ = autocomplete_results_; | |
115 last_results_base_ = results_base_; | |
116 return autocomplete_results_; | |
117 } | 114 } |
118 | 115 |
119 const InstantAutocompleteResult* SearchBox::GetAutocompleteResultWithId( | 116 bool SearchBox::GetAutocompleteResultWithId( |
120 size_t restricted_id) const { | 117 size_t restricted_id, |
121 if (restricted_id < last_results_base_ || | 118 InstantAutocompleteResult* result) const { |
122 restricted_id >= last_results_base_ + last_autocomplete_results_.size()) | 119 return autocomplete_results_cache_.GetItemWithRestrictedId(restricted_id, |
123 return NULL; | 120 result); |
124 return &last_autocomplete_results_[restricted_id - last_results_base_]; | |
125 } | 121 } |
126 | 122 |
127 const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() { | 123 const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() { |
128 return theme_info_; | 124 return theme_info_; |
129 } | 125 } |
130 | 126 |
131 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 127 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
132 bool handled = true; | 128 bool handled = true; |
133 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 129 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
134 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 130 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( | 224 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( |
229 render_view()->GetWebView()->mainFrame()); | 225 render_view()->GetWebView()->mainFrame()); |
230 DVLOG(1) << render_view() << " PageSupportsInstant: " << result; | 226 DVLOG(1) << render_view() << " PageSupportsInstant: " << result; |
231 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( | 227 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( |
232 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); | 228 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); |
233 } | 229 } |
234 } | 230 } |
235 | 231 |
236 void SearchBox::OnAutocompleteResults( | 232 void SearchBox::OnAutocompleteResults( |
237 const std::vector<InstantAutocompleteResult>& results) { | 233 const std::vector<InstantAutocompleteResult>& results) { |
238 results_base_ += autocomplete_results_.size(); | 234 autocomplete_results_cache_.AddItems(results); |
239 autocomplete_results_ = results; | |
240 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 235 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
241 DVLOG(1) << render_view() << " OnAutocompleteResults"; | 236 DVLOG(1) << render_view() << " OnAutocompleteResults"; |
242 extensions_v8::SearchBoxExtension::DispatchAutocompleteResults( | 237 extensions_v8::SearchBoxExtension::DispatchAutocompleteResults( |
243 render_view()->GetWebView()->mainFrame()); | 238 render_view()->GetWebView()->mainFrame()); |
244 } | 239 } |
245 } | 240 } |
246 | 241 |
247 void SearchBox::OnUpOrDownKeyPressed(int count) { | 242 void SearchBox::OnUpOrDownKeyPressed(int count) { |
248 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 243 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
249 DVLOG(1) << render_view() << " OnKeyPress: " << count; | 244 DVLOG(1) << render_view() << " OnKeyPress: " << count; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 size_t omnibox_font_size) { | 295 size_t omnibox_font_size) { |
301 omnibox_font_ = omnibox_font; | 296 omnibox_font_ = omnibox_font; |
302 omnibox_font_size_ = omnibox_font_size; | 297 omnibox_font_size_ = omnibox_font_size; |
303 } | 298 } |
304 | 299 |
305 void SearchBox::Reset() { | 300 void SearchBox::Reset() { |
306 query_.clear(); | 301 query_.clear(); |
307 verbatim_ = false; | 302 verbatim_ = false; |
308 selection_start_ = 0; | 303 selection_start_ = 0; |
309 selection_end_ = 0; | 304 selection_end_ = 0; |
310 results_base_ = 0; | |
311 popup_bounds_ = gfx::Rect(); | 305 popup_bounds_ = gfx::Rect(); |
| 306 autocomplete_results_cache_.Reset(); |
312 start_margin_ = 0; | 307 start_margin_ = 0; |
313 autocomplete_results_.clear(); | |
314 is_key_capture_enabled_ = false; | 308 is_key_capture_enabled_ = false; |
315 theme_info_ = ThemeBackgroundInfo(); | 309 theme_info_ = ThemeBackgroundInfo(); |
316 // Don't reset display_instant_results_ to prevent clearing it on committed | 310 // Don't reset display_instant_results_ to prevent clearing it on committed |
317 // results pages in extended mode. Otherwise resetting it is a no-op because | 311 // results pages in extended mode. Otherwise resetting it is a no-op because |
318 // a new loader is created when it changes; see crbug.com/164662. | 312 // a new loader is created when it changes; see crbug.com/164662. |
319 // Also don't reset omnibox_font_ or omnibox_font_size_ since it never | 313 // Also don't reset omnibox_font_ or omnibox_font_size_ since it never |
320 // changes. | 314 // changes. |
321 } | 315 } |
322 | 316 |
323 void SearchBox::OnMostVisitedChanged( | 317 void SearchBox::OnMostVisitedChanged( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 GURL url = GURL(ostr.str()); | 349 GURL url = GURL(ostr.str()); |
356 return UTF8ToUTF16(url.spec()); | 350 return UTF8ToUTF16(url.spec()); |
357 } | 351 } |
358 | 352 |
359 string16 SearchBox::GenerateFaviconUrl(int id) { | 353 string16 SearchBox::GenerateFaviconUrl(int id) { |
360 std::ostringstream ostr; | 354 std::ostringstream ostr; |
361 ostr << kFaviconUrlPrefix << id; | 355 ostr << kFaviconUrlPrefix << id; |
362 GURL url = GURL(ostr.str()); | 356 GURL url = GURL(ostr.str()); |
363 return UTF8ToUTF16(url.spec()); | 357 return UTF8ToUTF16(url.spec()); |
364 } | 358 } |
OLD | NEW |