Chromium Code Reviews| 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 |
| 37 bool GetInstantRestrictedIDFromPath(int render_view_id, | |
| 38 const std::string& path, | |
| 39 InstantRestrictedID* id) { | |
| 40 // Check that the path is of Most visited item ID form. | |
| 41 std::vector<std::string> tokens; | |
| 42 if (Tokenize(path, "/", &tokens) != 2) | |
| 43 return false; | |
| 44 | |
| 45 int view_id = 0; | |
| 46 if (!base::StringToInt(tokens[0], &view_id) || view_id != render_view_id) | |
| 47 return false; | |
| 48 return base::StringToInt(tokens[1], id); | |
| 49 } | |
| 50 | |
| 51 bool ParseRestrictedFaviconUrl(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 // In Instand Extended, the part of the favicon URL which is supposed to | |
|
samarth
2013/06/20 00:31:29
No need to keep mentioning Instant Extended (espec
pedro (no code reviews)
2013/06/20 22:43:45
Done.
| |
| 66 // contain the URL from which the favicon is being requested actually | |
| 67 // contains a pair in the format "<view_id>/<restricted_id>". If the favicon | |
| 68 // URL is not in the expected format then the execution must be stopped, | |
| 69 // returning |false|. | |
| 70 std::string id_part = raw_path.substr(parsed.path_index); | |
| 71 InstantRestrictedID id; | |
| 72 if (!GetInstantRestrictedIDFromPath(render_view_id, id_part, &id)) | |
| 73 return false; | |
| 74 | |
| 75 // The piece of the URL which details the favicon parameters should be | |
| 76 // returned so Instant Extended is able to reconstuct the URL, replacing | |
| 77 // the restricted_id with the actuall URL from which the favicon is being | |
|
samarth
2013/06/20 00:31:29
sp. actuall
pedro (no code reviews)
2013/06/20 22:43:45
Done.
| |
| 78 // requested. | |
| 79 *favicon_params = raw_path.substr(0, parsed.path_index); | |
| 80 *rid = id; | |
| 81 return true; | |
| 82 } | |
| 83 | |
| 35 // Parses |url| and fills in |id| with the InstantRestrictedID obtained from the | 84 // Parses |url| and fills in |id| with the InstantRestrictedID obtained from the |
| 36 // |url|. |render_view_id| is the ID of the associated RenderView. | 85 // |url|. |render_view_id| is the ID of the associated RenderView. |
| 37 // | 86 // |
| 38 // Valid |url| forms: | 87 // Valid |url| forms: |
|
samarth
2013/06/20 00:31:29
Update this comment since this is only expected to
pedro (no code reviews)
2013/06/20 22:43:45
Done.
| |
| 39 // chrome-search://favicon/<view_id>/<restricted_id> | 88 // chrome-search://favicon/<view_id>/<restricted_id> |
| 40 // chrome-search://thumb/<view_id>/<restricted_id> | 89 // chrome-search://thumb/<view_id>/<restricted_id> |
| 41 // | 90 // |
| 42 // If the |url| is valid, returns true and fills in |id| with restricted_id | 91 // 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. | 92 // value. If the |url| is invalid, returns false and |id| is not set. |
| 44 bool GetInstantRestrictedIDFromURL(int render_view_id, | 93 bool GetInstantRestrictedIDFromURL(int render_view_id, |
|
samarth
2013/06/20 00:31:29
Please rename this function and the one above to b
pedro (no code reviews)
2013/06/20 22:43:45
Done.
| |
| 45 const GURL& url, | 94 const GURL& url, |
| 46 InstantRestrictedID* id) { | 95 InstantRestrictedID* id) { |
| 47 // Strip leading path. | 96 // Strip leading slash. |
| 48 std::string path = url.path().substr(1); | 97 std::string path = url.path(); |
| 98 DCHECK_GT(path.length(), (size_t) 0); | |
| 99 DCHECK_EQ(path[0], '/'); | |
| 100 path = path.substr(1); | |
| 49 | 101 |
| 50 // Check that the path is of Most visited item ID form. | 102 return internal::GetInstantRestrictedIDFromPath(render_view_id, path, id); |
| 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 } | 103 } |
| 60 | 104 |
| 61 } // namespace internal | 105 } // namespace internal |
| 62 | 106 |
| 63 SearchBox::SearchBox(content::RenderView* render_view) | 107 SearchBox::SearchBox(content::RenderView* render_view) |
| 64 : content::RenderViewObserver(render_view), | 108 : content::RenderViewObserver(render_view), |
| 65 content::RenderViewObserverTracker<SearchBox>(render_view), | 109 content::RenderViewObserverTracker<SearchBox>(render_view), |
| 66 verbatim_(false), | 110 verbatim_(false), |
| 67 query_is_restricted_(false), | 111 query_is_restricted_(false), |
| 68 selection_start_(0), | 112 selection_start_(0), |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); | 245 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); |
| 202 if (most_visited_item_url.is_empty()) | 246 if (most_visited_item_url.is_empty()) |
| 203 return false; | 247 return false; |
| 204 *url = GURL(base::StringPrintf("chrome-search://thumb/%s", | 248 *url = GURL(base::StringPrintf("chrome-search://thumb/%s", |
| 205 most_visited_item_url.spec().c_str())); | 249 most_visited_item_url.spec().c_str())); |
| 206 return true; | 250 return true; |
| 207 } | 251 } |
| 208 | 252 |
| 209 bool SearchBox::GenerateFaviconURLFromTransientURL(const GURL& transient_url, | 253 bool SearchBox::GenerateFaviconURLFromTransientURL(const GURL& transient_url, |
| 210 GURL* url) const { | 254 GURL* url) const { |
| 211 InstantRestrictedID rid = 0; | 255 std::string favicon_params; |
| 212 if (!internal::GetInstantRestrictedIDFromURL(render_view()->GetRoutingID(), | 256 InstantRestrictedID rid; |
| 213 transient_url, &rid)) { | 257 bool success = internal::ParseRestrictedFaviconUrl( |
|
samarth
2013/06/20 00:31:29
If the favicon URL parser can't parse this URL, we
pedro (no code reviews)
2013/06/20 22:43:45
Done.
| |
| 214 return false; | 258 render_view()->GetRoutingID(), transient_url, &favicon_params, &rid); |
| 259 | |
| 260 InstantMostVisitedItem item; | |
| 261 if (success && GetMostVisitedItemWithID(rid, &item)) { | |
| 262 GURL item_url = item.url; | |
| 263 *url = GURL(base::StringPrintf("chrome-search://favicon/%s%s", | |
| 264 favicon_params.c_str(), | |
| 265 item_url.spec().c_str())); | |
| 266 } else { | |
| 267 *url = GURL("chrome-search://favicon/"); | |
|
samarth
2013/06/20 00:31:29
This should keep favicon_params part of the string
pedro (no code reviews)
2013/06/20 22:43:45
Done.
| |
| 215 } | 268 } |
| 216 | |
| 217 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); | |
| 218 if (most_visited_item_url.is_empty()) | |
| 219 return false; | |
| 220 *url = GURL(base::StringPrintf("chrome-search://favicon/%s", | |
| 221 most_visited_item_url.spec().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 |