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/Source/WebKit/chromium/public/WebDocument.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/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 | |
| 35 // Parses |url| and fills in |id| with the InstantRestrictedID obtained from the | 51 // Parses |url| and fills in |id| with the InstantRestrictedID obtained from the |
| 36 // |url|. |render_view_id| is the ID of the associated RenderView. | 52 // |url|. |render_view_id| is the ID of the associated RenderView. |
| 37 // | 53 // |
| 38 // Valid |url| forms: | 54 // Valid |url| forms: |
| 39 // chrome-search://favicon/<view_id>/<restricted_id> | 55 // chrome-search://favicon/<view_id>/<restricted_id> |
| 40 // chrome-search://thumb/<view_id>/<restricted_id> | 56 // chrome-search://thumb/<view_id>/<restricted_id> |
| 41 // | 57 // |
| 42 // If the |url| is valid, returns true and fills in |id| with restricted_id | 58 // 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. | 59 // value. If the |url| is invalid, returns false and |id| is not set. |
| 44 bool GetInstantRestrictedIDFromURL(int render_view_id, | 60 bool GetInstantRestrictedIDFromURL(int render_view_id, |
| 45 const GURL& url, | 61 const GURL& url, |
| 46 InstantRestrictedID* id) { | 62 InstantRestrictedID* id) { |
| 47 // Strip leading path. | 63 // Strip leading path. |
| 48 std::string path = url.path().substr(1); | 64 std::string path = url.path().substr(1); |
| 49 | 65 |
| 50 // Check that the path is of Most visited item ID form. | 66 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 } | 67 } |
| 60 | 68 |
| 61 } // namespace internal | 69 } // namespace internal |
| 62 | 70 |
| 63 SearchBox::SearchBox(content::RenderView* render_view) | 71 SearchBox::SearchBox(content::RenderView* render_view) |
| 64 : content::RenderViewObserver(render_view), | 72 : content::RenderViewObserver(render_view), |
| 65 content::RenderViewObserverTracker<SearchBox>(render_view), | 73 content::RenderViewObserverTracker<SearchBox>(render_view), |
| 66 verbatim_(false), | 74 verbatim_(false), |
| 67 query_is_restricted_(false), | 75 query_is_restricted_(false), |
| 68 selection_start_(0), | 76 selection_start_(0), |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 198 } | 206 } |
| 199 | 207 |
| 200 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); | 208 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); |
| 201 if (most_visited_item_url.is_empty()) | 209 if (most_visited_item_url.is_empty()) |
| 202 return false; | 210 return false; |
| 203 *url = GURL(base::StringPrintf("chrome-search://thumb/%s", | 211 *url = GURL(base::StringPrintf("chrome-search://thumb/%s", |
| 204 most_visited_item_url.spec().c_str())); | 212 most_visited_item_url.spec().c_str())); |
| 205 return true; | 213 return true; |
| 206 } | 214 } |
| 207 | 215 |
| 208 bool SearchBox::GenerateFaviconURLFromTransientURL(const GURL& transient_url, | 216 bool SearchBox::GenerateFaviconURLFromTransientURL(const GURL& transient_url, |
|
samarth
2013/06/14 21:54:46
Please update the docs in the .h for this.
pedro (no code reviews)
2013/06/18 22:35:13
Done.
| |
| 209 GURL* url) const { | 217 GURL* url) const { |
| 210 InstantRestrictedID rid = 0; | 218 // TODO(pedrosimonetti): DO NOT SUBMIT |
|
samarth
2013/06/14 21:54:46
Take out debugging info
pedro (no code reviews)
2013/06/18 22:35:13
Done. I left debugging info in there to make it ea
| |
| 211 if (!internal::GetInstantRestrictedIDFromURL(render_view()->GetRoutingID(), | 219 LOG(ERROR) << "~~~~~~~~~~~~~ ::WillSendRequest " << transient_url.spec(); |
| 212 transient_url, &rid)) { | 220 |
| 213 return false; | 221 // Strip leading slash. |
| 222 std::string raw_path = transient_url.path().substr(1); | |
|
samarth
2013/06/14 21:54:46
Don't just assume: please add checks that the path
pedro (no code reviews)
2013/06/18 22:35:13
Done.
| |
| 223 bool is_icon_url = false; | |
| 224 std::string favicon_url; | |
| 225 int size_in_dip = 16; | |
|
samarth
2013/06/14 21:54:46
This should be set by ParseFaviconPath, no? Don't
pedro (no code reviews)
2013/06/18 22:35:13
I'm using the struct now.
| |
| 226 ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_100P; | |
| 227 std::string params = ""; | |
| 228 | |
| 229 bool success = chrome::ParseFaviconPath(raw_path, false, chrome::FAVICON, | |
| 230 &is_icon_url, &favicon_url, &size_in_dip, &scale_factor, ¶ms); | |
|
samarth
2013/06/14 21:54:46
Just return false here if success is not true. Or
samarth
2013/06/14 21:54:46
If you follow my comment from the parse and return
pedro (no code reviews)
2013/06/18 22:35:13
Done.
pedro (no code reviews)
2013/06/18 22:35:13
Done.
| |
| 231 // Now that the favicon URL has been parsed, update |raw_path| with | |
| 232 // |favicon_url| which is expected to be the id of a Most Visited | |
| 233 // item in Instant Extended. | |
| 234 raw_path = favicon_url; | |
| 235 | |
| 236 // Try to convert the |raw_path| to int, which is expected to be the id | |
| 237 // of a Most Visited item, and then check if there is a Most Visited item | |
| 238 // with that id. | |
| 239 InstantRestrictedID id; | |
| 240 InstantMostVisitedItem item; | |
| 241 bool hasId = internal::GetInstantRestrictedIDFromPath( | |
| 242 render_view()->GetRoutingID(), raw_path, &id); | |
| 243 | |
| 244 if (success && | |
| 245 hasId && | |
| 246 GetMostVisitedItemWithID(id, &item)) { | |
| 247 // Translate the resource URL replacing the id with the URL of the Most | |
| 248 // Visited item associated with it. The |params| variable will ensure | |
| 249 // that advanced favicon parameters are preserved, allowing high dpi | |
| 250 // favicons to be served from chrome-search schema. | |
| 251 GURL item_url = item.url; | |
| 252 std::string translated_url = transient_url.GetOrigin().spec() + params + | |
| 253 // TODO(pedrosimonetti): Do we have to URL-escape the |item_url|? | |
| 254 item_url.spec(); | |
| 255 *url = GURL(translated_url); | |
| 256 // TODO(pedrosimonetti): DO NOT SUBMIT | |
| 257 LOG(ERROR) << "OK: " << translated_url; | |
| 258 return true; | |
| 259 } else { | |
| 260 *url = GURL(chrome::kChromeSearchInvalidRequestUrl); | |
| 261 // TODO(pedrosimonetti): DO NOT SUBMIT | |
| 262 LOG(ERROR) << "FAIL: " << transient_url.spec(); | |
| 263 return true; | |
| 214 } | 264 } |
| 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 } | 265 } |
| 223 | 266 |
| 224 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 267 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
| 225 bool handled = true; | 268 bool handled = true; |
| 226 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 269 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
| 227 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 270 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
| 228 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) | 271 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) |
| 229 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) | 272 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) |
| 230 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize) | 273 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize) |
| 231 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxMarginChange, OnMarginChange) | 274 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxMarginChange, OnMarginChange) |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 497 InstantMostVisitedItem item; | 540 InstantMostVisitedItem item; |
| 498 return GetMostVisitedItemWithID(item_id, &item) ? item.url : GURL(); | 541 return GetMostVisitedItemWithID(item_id, &item) ? item.url : GURL(); |
| 499 } | 542 } |
| 500 | 543 |
| 501 void SearchBox::OnToggleVoiceSearch() { | 544 void SearchBox::OnToggleVoiceSearch() { |
| 502 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 545 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 503 extensions_v8::SearchBoxExtension::DispatchToggleVoiceSearch( | 546 extensions_v8::SearchBoxExtension::DispatchToggleVoiceSearch( |
| 504 render_view()->GetWebView()->mainFrame()); | 547 render_view()->GetWebView()->mainFrame()); |
| 505 } | 548 } |
| 506 } | 549 } |
| OLD | NEW |