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