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