| 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/favicon_types.h" |
| 14 #include "chrome/common/favicon/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" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 42 return false; | 44 return false; |
| 43 } | 45 } |
| 44 } | 46 } |
| 45 return true; | 47 return true; |
| 46 } | 48 } |
| 47 | 49 |
| 48 } // namespace | 50 } // namespace |
| 49 | 51 |
| 50 namespace internal { // for testing | 52 namespace internal { // for testing |
| 51 | 53 |
| 52 // Parses |url| and fills in |id| with the InstantRestrictedID obtained from the | 54 bool GetInstantRestrictedIDFromPath(int render_view_id, |
| 53 // |url|. |render_view_id| is the ID of the associated RenderView. | 55 const std::string& path, |
| 54 // | 56 InstantRestrictedID* id) { |
| 55 // Valid |url| forms: | |
| 56 // chrome-search://favicon/<view_id>/<restricted_id> | |
| 57 // chrome-search://thumb/<view_id>/<restricted_id> | |
| 58 // | |
| 59 // If the |url| is valid, returns true and fills in |id| with restricted_id | |
| 60 // value. If the |url| is invalid, returns false and |id| is not set. | |
| 61 bool GetInstantRestrictedIDFromURL(int render_view_id, | |
| 62 const GURL& url, | |
| 63 InstantRestrictedID* id) { | |
| 64 // Strip leading path. | |
| 65 std::string path = url.path().substr(1); | |
| 66 | |
| 67 // Check that the path is of Most visited item ID form. | 57 // Check that the path is of Most visited item ID form. |
| 68 std::vector<std::string> tokens; | 58 std::vector<std::string> tokens; |
| 69 if (Tokenize(path, "/", &tokens) != 2) | 59 if (Tokenize(path, "/", &tokens) != 2) |
| 70 return false; | 60 return false; |
| 71 | 61 |
| 72 int view_id = 0; | 62 int view_id = 0; |
| 73 if (!base::StringToInt(tokens[0], &view_id) || view_id != render_view_id) | 63 if (!base::StringToInt(tokens[0], &view_id) || view_id != render_view_id) |
| 74 return false; | 64 return false; |
| 75 return base::StringToInt(tokens[1], id); | 65 return base::StringToInt(tokens[1], id); |
| 76 } | 66 } |
| 77 | 67 |
| 68 bool GetRestrictedIDFromFaviconUrl(int render_view_id, |
| 69 const GURL& url, |
| 70 std::string* favicon_params, |
| 71 InstantRestrictedID* rid) { |
| 72 // Strip leading slash. |
| 73 std::string raw_path = url.path(); |
| 74 DCHECK_GT(raw_path.length(), (size_t) 0); |
| 75 DCHECK_EQ(raw_path[0], '/'); |
| 76 raw_path = raw_path.substr(1); |
| 77 |
| 78 chrome::ParsedFaviconPath parsed; |
| 79 if (!chrome::ParseFaviconPath(raw_path, chrome::FAVICON, &parsed)) |
| 80 return false; |
| 81 |
| 82 // The part of the URL which details the favicon parameters should be returned |
| 83 // so the favicon URL can be reconstructed, by replacing the restricted_id |
| 84 // with the actual URL from which the favicon is being requested. |
| 85 *favicon_params = raw_path.substr(0, parsed.path_index); |
| 86 |
| 87 // The part of the favicon URL which is supposed to contain the URL from |
| 88 // which the favicon is being requested actually contains a pair in the |
| 89 // format "<view_id>/<restricted_id>". If the favicon URL is not in the |
| 90 // expected format then the execution must be stopped, returning |false|. |
| 91 std::string id_part = raw_path.substr(parsed.path_index); |
| 92 InstantRestrictedID id; |
| 93 if (!GetInstantRestrictedIDFromPath(render_view_id, id_part, &id)) |
| 94 return true; |
| 95 |
| 96 *rid = id; |
| 97 return true; |
| 98 } |
| 99 |
| 100 // Parses a thumbnail |url| and fills in |id| with the InstantRestrictedID |
| 101 // obtained from the |url|. |render_view_id| is the ID of the associated |
| 102 // RenderView. |
| 103 // |
| 104 // Valid |url| forms: |
| 105 // chrome-search://thumb/<view_id>/<restricted_id> |
| 106 // |
| 107 // If the |url| is valid, returns true and fills in |id| with restricted_id |
| 108 // value. If the |url| is invalid, returns false and |id| is not set. |
| 109 bool GetRestrictedIDFromThumbnailUrl(int render_view_id, |
| 110 const GURL& url, |
| 111 InstantRestrictedID* id) { |
| 112 // Strip leading slash. |
| 113 std::string path = url.path(); |
| 114 DCHECK_GT(path.length(), (size_t) 0); |
| 115 DCHECK_EQ(path[0], '/'); |
| 116 path = path.substr(1); |
| 117 |
| 118 return internal::GetInstantRestrictedIDFromPath(render_view_id, path, id); |
| 119 } |
| 120 |
| 78 } // namespace internal | 121 } // namespace internal |
| 79 | 122 |
| 80 SearchBox::SearchBox(content::RenderView* render_view) | 123 SearchBox::SearchBox(content::RenderView* render_view) |
| 81 : content::RenderViewObserver(render_view), | 124 : content::RenderViewObserver(render_view), |
| 82 content::RenderViewObserverTracker<SearchBox>(render_view), | 125 content::RenderViewObserverTracker<SearchBox>(render_view), |
| 83 verbatim_(false), | 126 verbatim_(false), |
| 84 query_is_restricted_(false), | 127 query_is_restricted_(false), |
| 85 selection_start_(0), | 128 selection_start_(0), |
| 86 selection_end_(0), | 129 selection_end_(0), |
| 87 start_margin_(0), | 130 start_margin_(0), |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 autocomplete_result_id, result); | 252 autocomplete_result_id, result); |
| 210 } | 253 } |
| 211 | 254 |
| 212 const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() { | 255 const ThemeBackgroundInfo& SearchBox::GetThemeBackgroundInfo() { |
| 213 return theme_info_; | 256 return theme_info_; |
| 214 } | 257 } |
| 215 | 258 |
| 216 bool SearchBox::GenerateThumbnailURLFromTransientURL(const GURL& transient_url, | 259 bool SearchBox::GenerateThumbnailURLFromTransientURL(const GURL& transient_url, |
| 217 GURL* url) const { | 260 GURL* url) const { |
| 218 InstantRestrictedID rid = 0; | 261 InstantRestrictedID rid = 0; |
| 219 if (!internal::GetInstantRestrictedIDFromURL(render_view()->GetRoutingID(), | 262 if (!internal::GetRestrictedIDFromThumbnailUrl(render_view()->GetRoutingID(), |
| 220 transient_url, &rid)) { | 263 transient_url, &rid)) { |
| 221 return false; | 264 return false; |
| 222 } | 265 } |
| 223 | 266 |
| 224 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); | 267 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); |
| 225 if (most_visited_item_url.is_empty()) | 268 if (most_visited_item_url.is_empty()) |
| 226 return false; | 269 return false; |
| 227 *url = GURL(base::StringPrintf("chrome-search://thumb/%s", | 270 *url = GURL(base::StringPrintf("chrome-search://thumb/%s", |
| 228 most_visited_item_url.spec().c_str())); | 271 most_visited_item_url.spec().c_str())); |
| 229 return true; | 272 return true; |
| 230 } | 273 } |
| 231 | 274 |
| 232 bool SearchBox::GenerateFaviconURLFromTransientURL(const GURL& transient_url, | 275 bool SearchBox::GenerateFaviconURLFromTransientURL(const GURL& transient_url, |
| 233 GURL* url) const { | 276 GURL* url) const { |
| 234 InstantRestrictedID rid = 0; | 277 std::string favicon_params; |
| 235 if (!internal::GetInstantRestrictedIDFromURL(render_view()->GetRoutingID(), | 278 InstantRestrictedID rid = -1; |
| 236 transient_url, &rid)) { | 279 bool success = internal::GetRestrictedIDFromFaviconUrl( |
| 280 render_view()->GetRoutingID(), transient_url, &favicon_params, &rid); |
| 281 if (!success) |
| 237 return false; | 282 return false; |
| 238 } | |
| 239 | 283 |
| 240 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); | 284 InstantMostVisitedItem item; |
| 241 if (most_visited_item_url.is_empty()) | 285 std::string item_url = ""; |
| 242 return false; | 286 if (rid != -1 && GetMostVisitedItemWithID(rid, &item)) |
| 243 *url = GURL(base::StringPrintf("chrome-search://favicon/%s", | 287 item_url = item.url.spec(); |
| 244 most_visited_item_url.spec().c_str())); | 288 |
| 289 *url = GURL(base::StringPrintf("chrome-search://favicon/%s%s", |
| 290 favicon_params.c_str(), |
| 291 item_url.c_str())); |
| 245 return true; | 292 return true; |
| 246 } | 293 } |
| 247 | 294 |
| 248 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 295 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
| 249 bool handled = true; | 296 bool handled = true; |
| 250 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 297 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
| 251 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 298 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
| 252 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) | 299 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) |
| 253 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) | 300 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) |
| 254 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize) | 301 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize) |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 InstantMostVisitedItem item; | 606 InstantMostVisitedItem item; |
| 560 return GetMostVisitedItemWithID(item_id, &item) ? item.url : GURL(); | 607 return GetMostVisitedItemWithID(item_id, &item) ? item.url : GURL(); |
| 561 } | 608 } |
| 562 | 609 |
| 563 void SearchBox::OnToggleVoiceSearch() { | 610 void SearchBox::OnToggleVoiceSearch() { |
| 564 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 611 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 565 extensions_v8::SearchBoxExtension::DispatchToggleVoiceSearch( | 612 extensions_v8::SearchBoxExtension::DispatchToggleVoiceSearch( |
| 566 render_view()->GetWebView()->mainFrame()); | 613 render_view()->GetWebView()->mainFrame()); |
| 567 } | 614 } |
| 568 } | 615 } |
| OLD | NEW |