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 |
(...skipping 14 matching lines...) Expand all Loading... |
108 } | 115 } |
109 | 116 |
110 gfx::Rect SearchBox::GetPopupBounds() const { | 117 gfx::Rect SearchBox::GetPopupBounds() const { |
111 double zoom = GetZoom(); | 118 double zoom = GetZoom(); |
112 return gfx::Rect(static_cast<int>(popup_bounds_.x() / zoom), | 119 return gfx::Rect(static_cast<int>(popup_bounds_.x() / zoom), |
113 static_cast<int>(popup_bounds_.y() / zoom), | 120 static_cast<int>(popup_bounds_.y() / zoom), |
114 static_cast<int>(popup_bounds_.width() / zoom), | 121 static_cast<int>(popup_bounds_.width() / zoom), |
115 static_cast<int>(popup_bounds_.height() / zoom)); | 122 static_cast<int>(popup_bounds_.height() / zoom)); |
116 } | 123 } |
117 | 124 |
118 const std::vector<InstantAutocompleteResult>& | 125 void SearchBox::GetAutocompleteResults( |
119 SearchBox::GetAutocompleteResults() { | 126 std::vector<InstantAutocompleteResultIDPair>* results) const { |
120 // Remember the last requested autocomplete_results to account for race | 127 autocomplete_results_cache_.GetCurrentItems(results); |
121 // conditions between autocomplete providers returning new data and the user | |
122 // clicking on a suggestion. | |
123 last_autocomplete_results_ = autocomplete_results_; | |
124 last_results_base_ = results_base_; | |
125 return autocomplete_results_; | |
126 } | 128 } |
127 | 129 |
128 const InstantAutocompleteResult* SearchBox::GetAutocompleteResultWithId( | 130 bool SearchBox::GetAutocompleteResultWithID( |
129 size_t autocomplete_result_id) const { | 131 InstantRestrictedID autocomplete_result_id, |
130 if (autocomplete_result_id < last_results_base_ || | 132 InstantAutocompleteResult* result) const { |
131 autocomplete_result_id >= | 133 return autocomplete_results_cache_.GetItemWithRestrictedID( |
132 last_results_base_ + last_autocomplete_results_.size()) | 134 autocomplete_result_id, result); |
133 return NULL; | |
134 return &last_autocomplete_results_[ | |
135 autocomplete_result_id - last_results_base_]; | |
136 } | 135 } |
137 | 136 |
138 const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() { | 137 const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() { |
139 return theme_info_; | 138 return theme_info_; |
140 } | 139 } |
141 | 140 |
142 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 141 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
143 bool handled = true; | 142 bool handled = true; |
144 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 143 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
145 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 144 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( | 249 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( |
251 render_view()->GetWebView()->mainFrame()); | 250 render_view()->GetWebView()->mainFrame()); |
252 DVLOG(1) << render_view() << " PageSupportsInstant: " << result; | 251 DVLOG(1) << render_view() << " PageSupportsInstant: " << result; |
253 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( | 252 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( |
254 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); | 253 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); |
255 } | 254 } |
256 } | 255 } |
257 | 256 |
258 void SearchBox::OnAutocompleteResults( | 257 void SearchBox::OnAutocompleteResults( |
259 const std::vector<InstantAutocompleteResult>& results) { | 258 const std::vector<InstantAutocompleteResult>& results) { |
260 results_base_ += autocomplete_results_.size(); | 259 autocomplete_results_cache_.AddItems(results); |
261 autocomplete_results_ = results; | |
262 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 260 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
263 DVLOG(1) << render_view() << " OnAutocompleteResults"; | 261 DVLOG(1) << render_view() << " OnAutocompleteResults"; |
264 extensions_v8::SearchBoxExtension::DispatchAutocompleteResults( | 262 extensions_v8::SearchBoxExtension::DispatchAutocompleteResults( |
265 render_view()->GetWebView()->mainFrame()); | 263 render_view()->GetWebView()->mainFrame()); |
266 } | 264 } |
267 } | 265 } |
268 | 266 |
269 void SearchBox::OnUpOrDownKeyPressed(int count) { | 267 void SearchBox::OnUpOrDownKeyPressed(int count) { |
270 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 268 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
271 DVLOG(1) << render_view() << " OnKeyPress: " << count; | 269 DVLOG(1) << render_view() << " OnKeyPress: " << count; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 return zoom; | 339 return zoom; |
342 } | 340 } |
343 return 1.0; | 341 return 1.0; |
344 } | 342 } |
345 | 343 |
346 void SearchBox::Reset() { | 344 void SearchBox::Reset() { |
347 query_.clear(); | 345 query_.clear(); |
348 verbatim_ = false; | 346 verbatim_ = false; |
349 selection_start_ = 0; | 347 selection_start_ = 0; |
350 selection_end_ = 0; | 348 selection_end_ = 0; |
351 results_base_ = 0; | |
352 popup_bounds_ = gfx::Rect(); | 349 popup_bounds_ = gfx::Rect(); |
353 start_margin_ = 0; | 350 start_margin_ = 0; |
354 autocomplete_results_.clear(); | |
355 is_key_capture_enabled_ = false; | 351 is_key_capture_enabled_ = false; |
356 theme_info_ = ThemeBackgroundInfo(); | 352 theme_info_ = ThemeBackgroundInfo(); |
357 // Don't reset display_instant_results_ to prevent clearing it on committed | 353 // Don't reset display_instant_results_ to prevent clearing it on committed |
358 // results pages in extended mode. Otherwise resetting it is a no-op because | 354 // results pages in extended mode. Otherwise resetting it is a no-op because |
359 // a new loader is created when it changes; see crbug.com/164662. | 355 // a new loader is created when it changes; see crbug.com/164662. |
360 // Also don't reset omnibox_font_ or omnibox_font_size_ since it never | 356 // Also don't reset omnibox_font_ or omnibox_font_size_ since it never |
361 // changes. | 357 // changes. |
362 } | 358 } |
363 | 359 |
364 void SearchBox::OnMostVisitedChanged( | 360 void SearchBox::OnMostVisitedChanged( |
365 const std::vector<InstantMostVisitedItem>& items) { | 361 const std::vector<InstantMostVisitedItemIDPair>& items) { |
366 most_visited_items_ = items; | 362 most_visited_items_cache_.AddItemsWithRestrictedID(items); |
367 | 363 |
368 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 364 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
369 extensions_v8::SearchBoxExtension::DispatchMostVisitedChanged( | 365 extensions_v8::SearchBoxExtension::DispatchMostVisitedChanged( |
370 render_view()->GetWebView()->mainFrame()); | 366 render_view()->GetWebView()->mainFrame()); |
371 } | 367 } |
372 } | 368 } |
373 | 369 |
374 const std::vector<InstantMostVisitedItem>& | 370 void SearchBox::GetMostVisitedItems( |
375 SearchBox::GetMostVisitedItems() const { | 371 std::vector<InstantMostVisitedItemIDPair>* items) const { |
376 return most_visited_items_; | 372 return most_visited_items_cache_.GetCurrentItems(items); |
377 } | 373 } |
| 374 |
| 375 bool SearchBox::GetMostVisitedItemWithID( |
| 376 InstantRestrictedID most_visited_item_id, |
| 377 InstantMostVisitedItem* item) const { |
| 378 return most_visited_items_cache_.GetItemWithRestrictedID(most_visited_item_id, |
| 379 item); |
| 380 } |
OLD | NEW |