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/common/url_constants.h" | 9 #include "chrome/common/url_constants.h" |
10 #include "chrome/renderer/searchbox/searchbox_extension.h" | 10 #include "chrome/renderer/searchbox/searchbox_extension.h" |
11 #include "content/public/renderer/render_view.h" | 11 #include "content/public/renderer/render_view.h" |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
14 | 14 |
| 15 namespace { |
| 16 // Size of the results cache. |
| 17 const size_t kMaxInstantAutocompleteResultItemCacheSize = 100; |
| 18 } |
| 19 |
15 SearchBox::SearchBox(content::RenderView* render_view) | 20 SearchBox::SearchBox(content::RenderView* render_view) |
16 : content::RenderViewObserver(render_view), | 21 : content::RenderViewObserver(render_view), |
17 content::RenderViewObserverTracker<SearchBox>(render_view), | 22 content::RenderViewObserverTracker<SearchBox>(render_view), |
18 verbatim_(false), | 23 verbatim_(false), |
19 selection_start_(0), | 24 selection_start_(0), |
20 selection_end_(0), | 25 selection_end_(0), |
21 results_base_(0), | |
22 start_margin_(0), | 26 start_margin_(0), |
23 last_results_base_(0), | |
24 is_key_capture_enabled_(false), | 27 is_key_capture_enabled_(false), |
25 display_instant_results_(false), | 28 display_instant_results_(false), |
26 omnibox_font_size_(0) { | 29 omnibox_font_size_(0), |
| 30 autocomplete_results_cache_(kMaxInstantAutocompleteResultItemCacheSize), |
| 31 most_visited_items_cache_(kMaxInstantMostVisitedItemCacheSize) { |
27 } | 32 } |
28 | 33 |
29 SearchBox::~SearchBox() { | 34 SearchBox::~SearchBox() { |
30 } | 35 } |
31 | 36 |
32 void SearchBox::SetSuggestions( | 37 void SearchBox::SetSuggestions( |
33 const std::vector<InstantSuggestion>& suggestions) { | 38 const std::vector<InstantSuggestion>& suggestions) { |
34 if (!suggestions.empty() && | 39 if (!suggestions.empty() && |
35 suggestions[0].behavior == INSTANT_COMPLETE_REPLACE) { | 40 suggestions[0].behavior == INSTANT_COMPLETE_REPLACE) { |
36 query_ = suggestions[0].text; | 41 query_ = suggestions[0].text; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 } | 75 } |
71 | 76 |
72 void SearchBox::NavigateToURL(const GURL& url, | 77 void SearchBox::NavigateToURL(const GURL& url, |
73 content::PageTransition transition, | 78 content::PageTransition transition, |
74 WindowOpenDisposition disposition) { | 79 WindowOpenDisposition disposition) { |
75 render_view()->Send(new ChromeViewHostMsg_SearchBoxNavigate( | 80 render_view()->Send(new ChromeViewHostMsg_SearchBoxNavigate( |
76 render_view()->GetRoutingID(), render_view()->GetPageId(), | 81 render_view()->GetRoutingID(), render_view()->GetPageId(), |
77 url, transition, disposition)); | 82 url, transition, disposition)); |
78 } | 83 } |
79 | 84 |
80 void SearchBox::DeleteMostVisitedItem(uint64 most_visited_item_id) { | 85 void SearchBox::DeleteMostVisitedItem( |
| 86 InstantRestrictedID most_visited_item_id) { |
81 render_view()->Send(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem( | 87 render_view()->Send(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem( |
82 render_view()->GetRoutingID(), most_visited_item_id)); | 88 render_view()->GetRoutingID(), most_visited_item_id)); |
83 } | 89 } |
84 | 90 |
85 void SearchBox::UndoMostVisitedDeletion(uint64 most_visited_item_id) { | 91 void SearchBox::UndoMostVisitedDeletion( |
| 92 InstantRestrictedID most_visited_item_id) { |
86 render_view()->Send(new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion( | 93 render_view()->Send(new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion( |
87 render_view()->GetRoutingID(), most_visited_item_id)); | 94 render_view()->GetRoutingID(), most_visited_item_id)); |
88 } | 95 } |
89 | 96 |
90 void SearchBox::UndoAllMostVisitedDeletions() { | 97 void SearchBox::UndoAllMostVisitedDeletions() { |
91 render_view()->Send( | 98 render_view()->Send( |
92 new ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions( | 99 new ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions( |
93 render_view()->GetRoutingID())); | 100 render_view()->GetRoutingID())); |
94 } | 101 } |
95 | 102 |
96 int SearchBox::GetStartMargin() const { | 103 int SearchBox::GetStartMargin() const { |
97 return static_cast<int>(start_margin_ / GetZoom()); | 104 return static_cast<int>(start_margin_ / GetZoom()); |
98 } | 105 } |
99 | 106 |
100 gfx::Rect SearchBox::GetPopupBounds() const { | 107 gfx::Rect SearchBox::GetPopupBounds() const { |
101 double zoom = GetZoom(); | 108 double zoom = GetZoom(); |
102 return gfx::Rect(static_cast<int>(popup_bounds_.x() / zoom), | 109 return gfx::Rect(static_cast<int>(popup_bounds_.x() / zoom), |
103 static_cast<int>(popup_bounds_.y() / zoom), | 110 static_cast<int>(popup_bounds_.y() / zoom), |
104 static_cast<int>(popup_bounds_.width() / zoom), | 111 static_cast<int>(popup_bounds_.width() / zoom), |
105 static_cast<int>(popup_bounds_.height() / zoom)); | 112 static_cast<int>(popup_bounds_.height() / zoom)); |
106 } | 113 } |
107 | 114 |
108 const std::vector<InstantAutocompleteResult>& | 115 void SearchBox::GetAutocompleteResults( |
109 SearchBox::GetAutocompleteResults() { | 116 std::vector<InstantAutocompleteResultIDPair>* results) const { |
110 // Remember the last requested autocomplete_results to account for race | 117 autocomplete_results_cache_.GetCurrentItems(results); |
111 // conditions between autocomplete providers returning new data and the user | |
112 // clicking on a suggestion. | |
113 last_autocomplete_results_ = autocomplete_results_; | |
114 last_results_base_ = results_base_; | |
115 return autocomplete_results_; | |
116 } | 118 } |
117 | 119 |
118 const InstantAutocompleteResult* SearchBox::GetAutocompleteResultWithId( | 120 bool SearchBox::GetAutocompleteResultWithId( |
119 size_t autocomplete_result_id) const { | 121 InstantRestrictedID autocomplete_result_id, |
120 if (autocomplete_result_id < last_results_base_ || | 122 InstantAutocompleteResult* result) const { |
121 autocomplete_result_id >= | 123 return autocomplete_results_cache_.GetItemWithRestrictedID( |
122 last_results_base_ + last_autocomplete_results_.size()) | 124 autocomplete_result_id, result); |
123 return NULL; | |
124 return &last_autocomplete_results_[ | |
125 autocomplete_result_id - last_results_base_]; | |
126 } | 125 } |
127 | 126 |
128 const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() { | 127 const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() { |
129 return theme_info_; | 128 return theme_info_; |
130 } | 129 } |
131 | 130 |
132 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 131 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
133 bool handled = true; | 132 bool handled = true; |
134 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 133 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
135 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 134 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( | 231 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( |
233 render_view()->GetWebView()->mainFrame()); | 232 render_view()->GetWebView()->mainFrame()); |
234 DVLOG(1) << render_view() << " PageSupportsInstant: " << result; | 233 DVLOG(1) << render_view() << " PageSupportsInstant: " << result; |
235 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( | 234 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( |
236 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); | 235 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); |
237 } | 236 } |
238 } | 237 } |
239 | 238 |
240 void SearchBox::OnAutocompleteResults( | 239 void SearchBox::OnAutocompleteResults( |
241 const std::vector<InstantAutocompleteResult>& results) { | 240 const std::vector<InstantAutocompleteResult>& results) { |
242 results_base_ += autocomplete_results_.size(); | 241 autocomplete_results_cache_.AddItems(results); |
243 autocomplete_results_ = results; | |
244 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 242 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
245 DVLOG(1) << render_view() << " OnAutocompleteResults"; | 243 DVLOG(1) << render_view() << " OnAutocompleteResults"; |
246 extensions_v8::SearchBoxExtension::DispatchAutocompleteResults( | 244 extensions_v8::SearchBoxExtension::DispatchAutocompleteResults( |
247 render_view()->GetWebView()->mainFrame()); | 245 render_view()->GetWebView()->mainFrame()); |
248 } | 246 } |
249 } | 247 } |
250 | 248 |
251 void SearchBox::OnUpOrDownKeyPressed(int count) { | 249 void SearchBox::OnUpOrDownKeyPressed(int count) { |
252 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 250 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
253 DVLOG(1) << render_view() << " OnKeyPress: " << count; | 251 DVLOG(1) << render_view() << " OnKeyPress: " << count; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 return zoom; | 321 return zoom; |
324 } | 322 } |
325 return 1.0; | 323 return 1.0; |
326 } | 324 } |
327 | 325 |
328 void SearchBox::Reset() { | 326 void SearchBox::Reset() { |
329 query_.clear(); | 327 query_.clear(); |
330 verbatim_ = false; | 328 verbatim_ = false; |
331 selection_start_ = 0; | 329 selection_start_ = 0; |
332 selection_end_ = 0; | 330 selection_end_ = 0; |
333 results_base_ = 0; | |
334 popup_bounds_ = gfx::Rect(); | 331 popup_bounds_ = gfx::Rect(); |
335 start_margin_ = 0; | 332 start_margin_ = 0; |
336 autocomplete_results_.clear(); | |
337 is_key_capture_enabled_ = false; | 333 is_key_capture_enabled_ = false; |
338 theme_info_ = ThemeBackgroundInfo(); | 334 theme_info_ = ThemeBackgroundInfo(); |
339 // Don't reset display_instant_results_ to prevent clearing it on committed | 335 // Don't reset display_instant_results_ to prevent clearing it on committed |
340 // results pages in extended mode. Otherwise resetting it is a no-op because | 336 // results pages in extended mode. Otherwise resetting it is a no-op because |
341 // a new loader is created when it changes; see crbug.com/164662. | 337 // a new loader is created when it changes; see crbug.com/164662. |
342 // Also don't reset omnibox_font_ or omnibox_font_size_ since it never | 338 // Also don't reset omnibox_font_ or omnibox_font_size_ since it never |
343 // changes. | 339 // changes. |
344 } | 340 } |
345 | 341 |
346 void SearchBox::OnMostVisitedChanged( | 342 void SearchBox::OnMostVisitedChanged( |
347 const std::vector<InstantMostVisitedItem>& items) { | 343 const std::vector<InstantMostVisitedItemIDPair>& items) { |
348 most_visited_items_ = items; | 344 most_visited_items_cache_.AddItemsWithRestrictedID(items); |
349 | 345 |
350 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 346 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
351 extensions_v8::SearchBoxExtension::DispatchMostVisitedChanged( | 347 extensions_v8::SearchBoxExtension::DispatchMostVisitedChanged( |
352 render_view()->GetWebView()->mainFrame()); | 348 render_view()->GetWebView()->mainFrame()); |
353 } | 349 } |
354 } | 350 } |
355 | 351 |
356 const std::vector<InstantMostVisitedItem>& | 352 void SearchBox::GetMostVisitedItems( |
357 SearchBox::GetMostVisitedItems() const { | 353 std::vector<InstantMostVisitedItemIDPair>* items) const { |
358 return most_visited_items_; | 354 return most_visited_items_cache_.GetCurrentItems(items); |
359 } | 355 } |
| 356 |
| 357 bool SearchBox::GetMostVisitedItemWithID( |
| 358 InstantRestrictedID most_visited_item_id, |
| 359 InstantMostVisitedItem* item) const { |
| 360 return most_visited_items_cache_.GetItemWithRestrictedID(most_visited_item_id, |
| 361 item); |
| 362 } |
OLD | NEW |