| 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> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/favicon_types.h" |
| 14 #include "chrome/common/favicon_url_parser.h" |
| 13 #include "chrome/common/omnibox_focus_state.h" | 15 #include "chrome/common/omnibox_focus_state.h" |
| 14 #include "chrome/common/render_messages.h" | 16 #include "chrome/common/render_messages.h" |
| 15 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 16 #include "chrome/renderer/searchbox/searchbox_extension.h" | 18 #include "chrome/renderer/searchbox/searchbox_extension.h" |
| 17 #include "content/public/renderer/render_view.h" | 19 #include "content/public/renderer/render_view.h" |
| 18 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
| 19 #include "grit/renderer_resources.h" | 21 #include "grit/renderer_resources.h" |
| 20 #include "net/base/escape.h" | 22 #include "net/base/escape.h" |
| 21 #include "third_party/WebKit/public/web/WebDocument.h" | 23 #include "third_party/WebKit/public/web/WebDocument.h" |
| 22 #include "third_party/WebKit/public/web/WebFrame.h" | 24 #include "third_party/WebKit/public/web/WebFrame.h" |
| 23 #include "third_party/WebKit/public/web/WebView.h" | 25 #include "third_party/WebKit/public/web/WebView.h" |
| 24 #include "ui/base/resource/resource_bundle.h" | 26 #include "ui/base/resource/resource_bundle.h" |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 28 // Size of the results cache. | 30 // Size of the results cache. |
| 29 const size_t kMaxInstantAutocompleteResultItemCacheSize = 100; | 31 const size_t kMaxInstantAutocompleteResultItemCacheSize = 100; |
| 30 | 32 |
| 31 } // namespace | 33 } // namespace |
| 32 | 34 |
| 33 namespace internal { // for testing | 35 namespace internal { // for testing |
| 34 | 36 |
| 35 // Parses |url| and fills in |id| with the InstantRestrictedID obtained from the | 37 bool GetInstantRestrictedIDFromPath(int render_view_id, |
| 36 // |url|. |render_view_id| is the ID of the associated RenderView. | 38 const std::string& path, |
| 37 // | 39 InstantRestrictedID* id) { |
| 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. | 40 // Check that the path is of Most visited item ID form. |
| 51 std::vector<std::string> tokens; | 41 std::vector<std::string> tokens; |
| 52 if (Tokenize(path, "/", &tokens) != 2) | 42 if (Tokenize(path, "/", &tokens) != 2) |
| 53 return false; | 43 return false; |
| 54 | 44 |
| 55 int view_id = 0; | 45 int view_id = 0; |
| 56 if (!base::StringToInt(tokens[0], &view_id) || view_id != render_view_id) | 46 if (!base::StringToInt(tokens[0], &view_id) || view_id != render_view_id) |
| 57 return false; | 47 return false; |
| 58 return base::StringToInt(tokens[1], id); | 48 return base::StringToInt(tokens[1], id); |
| 59 } | 49 } |
| 60 | 50 |
| 51 bool GetRestrictedIDFromFaviconUrl(int render_view_id, |
| 52 const GURL& url, |
| 53 std::string* favicon_params, |
| 54 InstantRestrictedID* rid) { |
| 55 // Strip leading slash. |
| 56 std::string raw_path = url.path(); |
| 57 DCHECK_GT(raw_path.length(), (size_t) 0); |
| 58 DCHECK_EQ(raw_path[0], '/'); |
| 59 raw_path = raw_path.substr(1); |
| 60 |
| 61 chrome::ParsedFaviconPath parsed; |
| 62 if (!chrome::ParseFaviconPath(raw_path, chrome::FAVICON, &parsed)) |
| 63 return false; |
| 64 |
| 65 // The part of the URL which details the favicon parameters should be returned |
| 66 // so the favicon URL can be reconstructed, by replacing the restricted_id |
| 67 // with the actual URL from which the favicon is being requested. |
| 68 *favicon_params = raw_path.substr(0, parsed.path_index); |
| 69 |
| 70 // The part of the favicon URL which is supposed to contain the URL from |
| 71 // which the favicon is being requested actually contains a pair in the |
| 72 // format "<view_id>/<restricted_id>". If the favicon URL is not in the |
| 73 // expected format then the execution must be stopped, returning |false|. |
| 74 std::string id_part = raw_path.substr(parsed.path_index); |
| 75 InstantRestrictedID id; |
| 76 if (!GetInstantRestrictedIDFromPath(render_view_id, id_part, &id)) |
| 77 return true; |
| 78 |
| 79 *rid = id; |
| 80 return true; |
| 81 } |
| 82 |
| 83 // Parses a thumbnail |url| and fills in |id| with the InstantRestrictedID |
| 84 // obtained from the |url|. |render_view_id| is the ID of the associated |
| 85 // RenderView. |
| 86 // |
| 87 // Valid |url| forms: |
| 88 // chrome-search://thumb/<view_id>/<restricted_id> |
| 89 // |
| 90 // If the |url| is valid, returns true and fills in |id| with restricted_id |
| 91 // value. If the |url| is invalid, returns false and |id| is not set. |
| 92 bool GetRestrictedIDFromThumbnailUrl(int render_view_id, |
| 93 const GURL& url, |
| 94 InstantRestrictedID* id) { |
| 95 // Strip leading slash. |
| 96 std::string path = url.path(); |
| 97 DCHECK_GT(path.length(), (size_t) 0); |
| 98 DCHECK_EQ(path[0], '/'); |
| 99 path = path.substr(1); |
| 100 |
| 101 return internal::GetInstantRestrictedIDFromPath(render_view_id, path, id); |
| 102 } |
| 103 |
| 61 } // namespace internal | 104 } // namespace internal |
| 62 | 105 |
| 63 SearchBox::SearchBox(content::RenderView* render_view) | 106 SearchBox::SearchBox(content::RenderView* render_view) |
| 64 : content::RenderViewObserver(render_view), | 107 : content::RenderViewObserver(render_view), |
| 65 content::RenderViewObserverTracker<SearchBox>(render_view), | 108 content::RenderViewObserverTracker<SearchBox>(render_view), |
| 66 verbatim_(false), | 109 verbatim_(false), |
| 67 query_is_restricted_(false), | 110 query_is_restricted_(false), |
| 68 selection_start_(0), | 111 selection_start_(0), |
| 69 selection_end_(0), | 112 selection_end_(0), |
| 70 start_margin_(0), | 113 start_margin_(0), |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 autocomplete_result_id, result); | 229 autocomplete_result_id, result); |
| 187 } | 230 } |
| 188 | 231 |
| 189 const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() { | 232 const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() { |
| 190 return theme_info_; | 233 return theme_info_; |
| 191 } | 234 } |
| 192 | 235 |
| 193 bool SearchBox::GenerateThumbnailURLFromTransientURL(const GURL& transient_url, | 236 bool SearchBox::GenerateThumbnailURLFromTransientURL(const GURL& transient_url, |
| 194 GURL* url) const { | 237 GURL* url) const { |
| 195 InstantRestrictedID rid = 0; | 238 InstantRestrictedID rid = 0; |
| 196 if (!internal::GetInstantRestrictedIDFromURL(render_view()->GetRoutingID(), | 239 if (!internal::GetRestrictedIDFromThumbnailUrl(render_view()->GetRoutingID(), |
| 197 transient_url, &rid)) { | 240 transient_url, &rid)) { |
| 198 return false; | 241 return false; |
| 199 } | 242 } |
| 200 | 243 |
| 201 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); | 244 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); |
| 202 if (most_visited_item_url.is_empty()) | 245 if (most_visited_item_url.is_empty()) |
| 203 return false; | 246 return false; |
| 204 *url = GURL(base::StringPrintf("chrome-search://thumb/%s", | 247 *url = GURL(base::StringPrintf("chrome-search://thumb/%s", |
| 205 most_visited_item_url.spec().c_str())); | 248 most_visited_item_url.spec().c_str())); |
| 206 return true; | 249 return true; |
| 207 } | 250 } |
| 208 | 251 |
| 209 bool SearchBox::GenerateFaviconURLFromTransientURL(const GURL& transient_url, | 252 bool SearchBox::GenerateFaviconURLFromTransientURL(const GURL& transient_url, |
| 210 GURL* url) const { | 253 GURL* url) const { |
| 211 InstantRestrictedID rid = 0; | 254 std::string favicon_params; |
| 212 if (!internal::GetInstantRestrictedIDFromURL(render_view()->GetRoutingID(), | 255 InstantRestrictedID rid = -1; |
| 213 transient_url, &rid)) { | 256 bool success = internal::GetRestrictedIDFromFaviconUrl( |
| 257 render_view()->GetRoutingID(), transient_url, &favicon_params, &rid); |
| 258 if (!success) |
| 214 return false; | 259 return false; |
| 215 } | |
| 216 | 260 |
| 217 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); | 261 InstantMostVisitedItem item; |
| 218 if (most_visited_item_url.is_empty()) | 262 std::string item_url = ""; |
| 219 return false; | 263 if (rid != -1 && GetMostVisitedItemWithID(rid, &item)) |
| 220 *url = GURL(base::StringPrintf("chrome-search://favicon/%s", | 264 item_url = item.url.spec(); |
| 221 most_visited_item_url.spec().c_str())); | 265 |
| 266 *url = GURL(base::StringPrintf("chrome-search://favicon/%s%s", |
| 267 favicon_params.c_str(), |
| 268 item_url.c_str())); |
| 222 return true; | 269 return true; |
| 223 } | 270 } |
| 224 | 271 |
| 225 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 272 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
| 226 bool handled = true; | 273 bool handled = true; |
| 227 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 274 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
| 228 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 275 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
| 229 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) | 276 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) |
| 230 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) | 277 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) |
| 231 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize) | 278 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize) |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 InstantMostVisitedItem item; | 567 InstantMostVisitedItem item; |
| 521 return GetMostVisitedItemWithID(item_id, &item) ? item.url : GURL(); | 568 return GetMostVisitedItemWithID(item_id, &item) ? item.url : GURL(); |
| 522 } | 569 } |
| 523 | 570 |
| 524 void SearchBox::OnToggleVoiceSearch() { | 571 void SearchBox::OnToggleVoiceSearch() { |
| 525 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 572 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 526 extensions_v8::SearchBoxExtension::DispatchToggleVoiceSearch( | 573 extensions_v8::SearchBoxExtension::DispatchToggleVoiceSearch( |
| 527 render_view()->GetWebView()->mainFrame()); | 574 render_view()->GetWebView()->mainFrame()); |
| 528 } | 575 } |
| 529 } | 576 } |
| OLD | NEW |