| 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 "grit/renderer_resources.h" | 20 #include "grit/renderer_resources.h" |
| 19 #include "net/base/escape.h" | 21 #include "net/base/escape.h" |
| 20 #include "third_party/WebKit/public/web/WebDocument.h" | 22 #include "third_party/WebKit/public/web/WebDocument.h" |
| 21 #include "third_party/WebKit/public/web/WebFrame.h" | 23 #include "third_party/WebKit/public/web/WebFrame.h" |
| 22 #include "third_party/WebKit/public/web/WebView.h" | 24 #include "third_party/WebKit/public/web/WebView.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 // Parses |path| and fills in |id| with the InstantRestrictedID obtained from |
| 53 // |url|. |render_view_id| is the ID of the associated RenderView. | 55 // the |path|. |render_view_id| is the ID of the associated RenderView. |
| 54 // | 56 // |
| 55 // Valid |url| forms: | 57 // |path| is a pair of |render_view_id| and |restricted_id|, and it is |
| 56 // chrome-search://favicon/<view_id>/<restricted_id> | 58 // contained in Instant Extended URLs. A valid |path| is in the form: |
| 57 // chrome-search://thumb/<view_id>/<restricted_id> | 59 // <render_view_id>/<restricted_id> |
| 58 // | 60 // |
| 59 // If the |url| is valid, returns true and fills in |id| with restricted_id | 61 // If the |path| 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. | 62 // value. If the |path| is invalid, returns false and |id| is not set. |
| 61 bool GetInstantRestrictedIDFromURL(int render_view_id, | 63 bool GetInstantRestrictedIDFromPath(int render_view_id, |
| 62 const GURL& url, | 64 const std::string& path, |
| 63 InstantRestrictedID* id) { | 65 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. | 66 // Check that the path is of Most visited item ID form. |
| 68 std::vector<std::string> tokens; | 67 std::vector<std::string> tokens; |
| 69 if (Tokenize(path, "/", &tokens) != 2) | 68 if (Tokenize(path, "/", &tokens) != 2) |
| 70 return false; | 69 return false; |
| 71 | 70 |
| 72 int view_id = 0; | 71 int view_id = 0; |
| 73 if (!base::StringToInt(tokens[0], &view_id) || view_id != render_view_id) | 72 if (!base::StringToInt(tokens[0], &view_id) || view_id != render_view_id) |
| 74 return false; | 73 return false; |
| 75 return base::StringToInt(tokens[1], id); | 74 return base::StringToInt(tokens[1], id); |
| 76 } | 75 } |
| 77 | 76 |
| 77 bool GetRestrictedIDFromFaviconUrl(int render_view_id, |
| 78 const GURL& url, |
| 79 std::string* favicon_params, |
| 80 InstantRestrictedID* rid) { |
| 81 // Strip leading slash. |
| 82 std::string raw_path = url.path(); |
| 83 DCHECK_GT(raw_path.length(), (size_t) 0); |
| 84 DCHECK_EQ(raw_path[0], '/'); |
| 85 raw_path = raw_path.substr(1); |
| 86 |
| 87 chrome::ParsedFaviconPath parsed; |
| 88 if (!chrome::ParseFaviconPath(raw_path, chrome::FAVICON, &parsed)) |
| 89 return false; |
| 90 |
| 91 // The part of the URL which details the favicon parameters should be returned |
| 92 // so the favicon URL can be reconstructed, by replacing the restricted_id |
| 93 // with the actual URL from which the favicon is being requested. |
| 94 *favicon_params = raw_path.substr(0, parsed.path_index); |
| 95 |
| 96 // The part of the favicon URL which is supposed to contain the URL from |
| 97 // which the favicon is being requested (i.e., the page's URL) actually |
| 98 // contains a pair in the format "<view_id>/<restricted_id>". If the page's |
| 99 // URL is not in the expected format then the execution must be stopped, |
| 100 // returning |true|, indicating that the favicon URL should be translated |
| 101 // without the page's URL part, to prevent search providers from spoofing |
| 102 // the user's browsing history. For example, the following favicon URL |
| 103 // "chrome-search://favicon/http://www.secretsite.com" it is not in the |
| 104 // expected format "chrome-search://favicon/<view_id>/<restricted_id>" so |
| 105 // the pages's URL part ("http://www.secretsite.com") should be removed |
| 106 // entirely from the translated URL otherwise the search engine would know |
| 107 // if the user has visited that page (by verifying whether the favicon URL |
| 108 // returns an image for a particular page's URL); the translated URL in this |
| 109 // case would be "chrome-search://favicon/" which would simply return the |
| 110 // default favicon. |
| 111 std::string id_part = raw_path.substr(parsed.path_index); |
| 112 InstantRestrictedID id; |
| 113 if (!GetInstantRestrictedIDFromPath(render_view_id, id_part, &id)) |
| 114 return true; |
| 115 |
| 116 *rid = id; |
| 117 return true; |
| 118 } |
| 119 |
| 120 // Parses a thumbnail |url| and fills in |id| with the InstantRestrictedID |
| 121 // obtained from the |url|. |render_view_id| is the ID of the associated |
| 122 // RenderView. |
| 123 // |
| 124 // Valid |url| forms: |
| 125 // chrome-search://thumb/<view_id>/<restricted_id> |
| 126 // |
| 127 // If the |url| is valid, returns true and fills in |id| with restricted_id |
| 128 // value. If the |url| is invalid, returns false and |id| is not set. |
| 129 bool GetRestrictedIDFromThumbnailUrl(int render_view_id, |
| 130 const GURL& url, |
| 131 InstantRestrictedID* id) { |
| 132 // Strip leading slash. |
| 133 std::string path = url.path(); |
| 134 DCHECK_GT(path.length(), (size_t) 0); |
| 135 DCHECK_EQ(path[0], '/'); |
| 136 path = path.substr(1); |
| 137 |
| 138 return GetInstantRestrictedIDFromPath(render_view_id, path, id); |
| 139 } |
| 140 |
| 78 } // namespace internal | 141 } // namespace internal |
| 79 | 142 |
| 80 SearchBox::SearchBox(content::RenderView* render_view) | 143 SearchBox::SearchBox(content::RenderView* render_view) |
| 81 : content::RenderViewObserver(render_view), | 144 : content::RenderViewObserver(render_view), |
| 82 content::RenderViewObserverTracker<SearchBox>(render_view), | 145 content::RenderViewObserverTracker<SearchBox>(render_view), |
| 83 app_launcher_enabled_(false), | 146 app_launcher_enabled_(false), |
| 84 is_focused_(false), | 147 is_focused_(false), |
| 85 is_input_in_progress_(false), | 148 is_input_in_progress_(false), |
| 86 is_key_capture_enabled_(false), | 149 is_key_capture_enabled_(false), |
| 87 most_visited_items_cache_(kMaxInstantMostVisitedItemCacheSize), | 150 most_visited_items_cache_(kMaxInstantMostVisitedItemCacheSize), |
| (...skipping 14 matching lines...) Expand all Loading... |
| 102 | 165 |
| 103 void SearchBox::DeleteMostVisitedItem( | 166 void SearchBox::DeleteMostVisitedItem( |
| 104 InstantRestrictedID most_visited_item_id) { | 167 InstantRestrictedID most_visited_item_id) { |
| 105 render_view()->Send(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem( | 168 render_view()->Send(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem( |
| 106 render_view()->GetRoutingID(), render_view()->GetPageId(), | 169 render_view()->GetRoutingID(), render_view()->GetPageId(), |
| 107 GetURLForMostVisitedItem(most_visited_item_id))); | 170 GetURLForMostVisitedItem(most_visited_item_id))); |
| 108 } | 171 } |
| 109 | 172 |
| 110 bool SearchBox::GenerateFaviconURLFromTransientURL(const GURL& transient_url, | 173 bool SearchBox::GenerateFaviconURLFromTransientURL(const GURL& transient_url, |
| 111 GURL* url) const { | 174 GURL* url) const { |
| 112 InstantRestrictedID rid = 0; | 175 std::string favicon_params; |
| 113 if (!internal::GetInstantRestrictedIDFromURL(render_view()->GetRoutingID(), | 176 InstantRestrictedID rid = -1; |
| 114 transient_url, &rid)) { | 177 bool success = internal::GetRestrictedIDFromFaviconUrl( |
| 178 render_view()->GetRoutingID(), transient_url, &favicon_params, &rid); |
| 179 if (!success) |
| 115 return false; | 180 return false; |
| 116 } | |
| 117 | 181 |
| 118 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); | 182 InstantMostVisitedItem item; |
| 119 if (most_visited_item_url.is_empty()) | 183 std::string item_url; |
| 120 return false; | 184 if (rid != -1 && GetMostVisitedItemWithID(rid, &item)) |
| 121 *url = GURL(base::StringPrintf("chrome-search://favicon/%s", | 185 item_url = item.url.spec(); |
| 122 most_visited_item_url.spec().c_str())); | 186 |
| 187 *url = GURL(base::StringPrintf("chrome-search://favicon/%s%s", |
| 188 favicon_params.c_str(), |
| 189 item_url.c_str())); |
| 123 return true; | 190 return true; |
| 124 } | 191 } |
| 125 | 192 |
| 126 bool SearchBox::GenerateThumbnailURLFromTransientURL(const GURL& transient_url, | 193 bool SearchBox::GenerateThumbnailURLFromTransientURL(const GURL& transient_url, |
| 127 GURL* url) const { | 194 GURL* url) const { |
| 128 InstantRestrictedID rid = 0; | 195 InstantRestrictedID rid = 0; |
| 129 if (!internal::GetInstantRestrictedIDFromURL(render_view()->GetRoutingID(), | 196 if (!internal::GetRestrictedIDFromThumbnailUrl(render_view()->GetRoutingID(), |
| 130 transient_url, &rid)) { | 197 transient_url, &rid)) { |
| 131 return false; | 198 return false; |
| 132 } | 199 } |
| 133 | 200 |
| 134 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); | 201 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); |
| 135 if (most_visited_item_url.is_empty()) | 202 if (most_visited_item_url.is_empty()) |
| 136 return false; | 203 return false; |
| 137 *url = GURL(base::StringPrintf("chrome-search://thumb/%s", | 204 *url = GURL(base::StringPrintf("chrome-search://thumb/%s", |
| 138 most_visited_item_url.spec().c_str())); | 205 most_visited_item_url.spec().c_str())); |
| 139 return true; | 206 return true; |
| 140 } | 207 } |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 } | 416 } |
| 350 | 417 |
| 351 void SearchBox::Reset() { | 418 void SearchBox::Reset() { |
| 352 query_.clear(); | 419 query_.clear(); |
| 353 start_margin_ = 0; | 420 start_margin_ = 0; |
| 354 width_ = 0; | 421 width_ = 0; |
| 355 is_focused_ = false; | 422 is_focused_ = false; |
| 356 is_key_capture_enabled_ = false; | 423 is_key_capture_enabled_ = false; |
| 357 theme_info_ = ThemeBackgroundInfo(); | 424 theme_info_ = ThemeBackgroundInfo(); |
| 358 } | 425 } |
| OLD | NEW |