| 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 <string> |
| 8 |
| 7 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/string_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 10 #include "chrome/common/omnibox_focus_state.h" | 13 #include "chrome/common/omnibox_focus_state.h" |
| 11 #include "chrome/common/render_messages.h" | 14 #include "chrome/common/render_messages.h" |
| 12 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
| 13 #include "chrome/renderer/searchbox/searchbox_extension.h" | 16 #include "chrome/renderer/searchbox/searchbox_extension.h" |
| 14 #include "content/public/renderer/render_view.h" | 17 #include "content/public/renderer/render_view.h" |
| 18 #include "googleurl/src/gurl.h" |
| 15 #include "grit/renderer_resources.h" | 19 #include "grit/renderer_resources.h" |
| 16 #include "net/base/escape.h" | 20 #include "net/base/escape.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 20 #include "ui/base/resource/resource_bundle.h" | 24 #include "ui/base/resource/resource_bundle.h" |
| 21 | 25 |
| 22 namespace { | 26 namespace { |
| 23 | 27 |
| 24 // Size of the results cache. | 28 // Size of the results cache. |
| 25 const size_t kMaxInstantAutocompleteResultItemCacheSize = 100; | 29 const size_t kMaxInstantAutocompleteResultItemCacheSize = 100; |
| 26 | 30 |
| 27 } // namespace | 31 } // namespace |
| 28 | 32 |
| 33 namespace internal { // for testing |
| 34 |
| 35 // Parses |url| and fills in |id| with the InstantRestrictedID obtained from the |
| 36 // |url|. |render_view_id| is the ID of the associated RenderView. |
| 37 // |
| 38 // Valid |url| forms: |
| 39 // chrome-search://favicon/<view_id>/<restricted_id> |
| 40 // chrome-search://thumb/<view_id>/<restricted_id> |
| 41 // |
| 42 // If the |url| is valid, returns true and fills in |id| with restricted_id |
| 43 // value. If the |url| is invalid, returns false and |id| is not set. |
| 44 bool GetInstantRestrictedIDFromURL(int render_view_id, |
| 45 const GURL& url, |
| 46 InstantRestrictedID* id) { |
| 47 // Strip leading path. |
| 48 std::string path = url.path().substr(1); |
| 49 |
| 50 // Check that the path is of Most visited item ID form. |
| 51 std::vector<std::string> tokens; |
| 52 if (Tokenize(path, "/", &tokens) != 2) |
| 53 return false; |
| 54 |
| 55 int view_id = 0; |
| 56 if (!base::StringToInt(tokens[0], &view_id) || view_id != render_view_id) |
| 57 return false; |
| 58 return base::StringToInt(tokens[1], id); |
| 59 } |
| 60 |
| 61 } // namespace internal |
| 62 |
| 29 SearchBox::SearchBox(content::RenderView* render_view) | 63 SearchBox::SearchBox(content::RenderView* render_view) |
| 30 : content::RenderViewObserver(render_view), | 64 : content::RenderViewObserver(render_view), |
| 31 content::RenderViewObserverTracker<SearchBox>(render_view), | 65 content::RenderViewObserverTracker<SearchBox>(render_view), |
| 32 verbatim_(false), | 66 verbatim_(false), |
| 33 query_is_restricted_(false), | 67 query_is_restricted_(false), |
| 34 selection_start_(0), | 68 selection_start_(0), |
| 35 selection_end_(0), | 69 selection_end_(0), |
| 36 start_margin_(0), | 70 start_margin_(0), |
| 37 is_focused_(false), | 71 is_focused_(false), |
| 38 is_key_capture_enabled_(false), | 72 is_key_capture_enabled_(false), |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 InstantRestrictedID autocomplete_result_id, | 182 InstantRestrictedID autocomplete_result_id, |
| 149 InstantAutocompleteResult* result) const { | 183 InstantAutocompleteResult* result) const { |
| 150 return autocomplete_results_cache_.GetItemWithRestrictedID( | 184 return autocomplete_results_cache_.GetItemWithRestrictedID( |
| 151 autocomplete_result_id, result); | 185 autocomplete_result_id, result); |
| 152 } | 186 } |
| 153 | 187 |
| 154 const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() { | 188 const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() { |
| 155 return theme_info_; | 189 return theme_info_; |
| 156 } | 190 } |
| 157 | 191 |
| 192 bool SearchBox::GenerateThumbnailURLFromTransientURL(const GURL& transient_url, |
| 193 GURL* url) const { |
| 194 InstantRestrictedID rid = 0; |
| 195 if (!internal::GetInstantRestrictedIDFromURL(render_view()->GetRoutingID(), |
| 196 transient_url, &rid)) { |
| 197 return false; |
| 198 } |
| 199 |
| 200 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); |
| 201 if (most_visited_item_url.is_empty()) |
| 202 return false; |
| 203 *url = GURL(base::StringPrintf("chrome-search://thumb/%s", |
| 204 most_visited_item_url.spec().c_str())); |
| 205 return true; |
| 206 } |
| 207 |
| 208 bool SearchBox::GenerateFaviconURLFromTransientURL(const GURL& transient_url, |
| 209 GURL* url) const { |
| 210 InstantRestrictedID rid = 0; |
| 211 if (!internal::GetInstantRestrictedIDFromURL(render_view()->GetRoutingID(), |
| 212 transient_url, &rid)) { |
| 213 return false; |
| 214 } |
| 215 |
| 216 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); |
| 217 if (most_visited_item_url.is_empty()) |
| 218 return false; |
| 219 *url = GURL(base::StringPrintf("chrome-search://favicon/%s", |
| 220 most_visited_item_url.spec().c_str())); |
| 221 return true; |
| 222 } |
| 223 |
| 158 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 224 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
| 159 bool handled = true; | 225 bool handled = true; |
| 160 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 226 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
| 161 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 227 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
| 162 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) | 228 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) |
| 163 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) | 229 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) |
| 164 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize) | 230 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize) |
| 165 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxMarginChange, OnMarginChange) | 231 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxMarginChange, OnMarginChange) |
| 166 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxBarsHidden, OnBarsHidden) | 232 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxBarsHidden, OnBarsHidden) |
| 167 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, | 233 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 // changes. | 466 // changes. |
| 401 } | 467 } |
| 402 | 468 |
| 403 void SearchBox::SetQuery(const string16& query, bool verbatim) { | 469 void SearchBox::SetQuery(const string16& query, bool verbatim) { |
| 404 query_ = query; | 470 query_ = query; |
| 405 verbatim_ = verbatim; | 471 verbatim_ = verbatim; |
| 406 query_is_restricted_ = false; | 472 query_is_restricted_ = false; |
| 407 } | 473 } |
| 408 | 474 |
| 409 void SearchBox::OnMostVisitedChanged( | 475 void SearchBox::OnMostVisitedChanged( |
| 410 const std::vector<InstantMostVisitedItemIDPair>& items) { | 476 const std::vector<InstantMostVisitedItem>& items) { |
| 411 most_visited_items_cache_.AddItemsWithRestrictedID(items); | 477 most_visited_items_cache_.AddItems(items); |
| 412 | |
| 413 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 478 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 414 extensions_v8::SearchBoxExtension::DispatchMostVisitedChanged( | 479 extensions_v8::SearchBoxExtension::DispatchMostVisitedChanged( |
| 415 render_view()->GetWebView()->mainFrame()); | 480 render_view()->GetWebView()->mainFrame()); |
| 416 } | 481 } |
| 417 } | 482 } |
| 418 | 483 |
| 419 void SearchBox::GetMostVisitedItems( | 484 void SearchBox::GetMostVisitedItems( |
| 420 std::vector<InstantMostVisitedItemIDPair>* items) const { | 485 std::vector<InstantMostVisitedItemIDPair>* items) const { |
| 421 return most_visited_items_cache_.GetCurrentItems(items); | 486 return most_visited_items_cache_.GetCurrentItems(items); |
| 422 } | 487 } |
| 423 | 488 |
| 424 bool SearchBox::GetMostVisitedItemWithID( | 489 bool SearchBox::GetMostVisitedItemWithID( |
| 425 InstantRestrictedID most_visited_item_id, | 490 InstantRestrictedID most_visited_item_id, |
| 426 InstantMostVisitedItem* item) const { | 491 InstantMostVisitedItem* item) const { |
| 427 return most_visited_items_cache_.GetItemWithRestrictedID(most_visited_item_id, | 492 return most_visited_items_cache_.GetItemWithRestrictedID(most_visited_item_id, |
| 428 item); | 493 item); |
| 429 } | 494 } |
| 430 | 495 |
| 431 GURL SearchBox::GetURLForMostVisitedItem(InstantRestrictedID item_id) const { | 496 GURL SearchBox::GetURLForMostVisitedItem(InstantRestrictedID item_id) const { |
| 432 InstantMostVisitedItem item; | 497 InstantMostVisitedItem item; |
| 433 return GetMostVisitedItemWithID(item_id, &item) ? item.url : GURL(); | 498 return GetMostVisitedItemWithID(item_id, &item) ? item.url : GURL(); |
| 434 } | 499 } |
| 435 | 500 |
| 436 void SearchBox::OnToggleVoiceSearch() { | 501 void SearchBox::OnToggleVoiceSearch() { |
| 437 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 502 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 438 extensions_v8::SearchBoxExtension::DispatchToggleVoiceSearch( | 503 extensions_v8::SearchBoxExtension::DispatchToggleVoiceSearch( |
| 439 render_view()->GetWebView()->mainFrame()); | 504 render_view()->GetWebView()->mainFrame()); |
| 440 } | 505 } |
| 441 } | 506 } |
| OLD | NEW |