| 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/logging.h" |
| 9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/favicon/fallback_icon_url_parser.h" |
| 13 #include "chrome/common/favicon/favicon_url_parser.h" | 15 #include "chrome/common/favicon/favicon_url_parser.h" |
| 16 #include "chrome/common/favicon/large_icon_url_parser.h" |
| 14 #include "chrome/common/omnibox_focus_state.h" | 17 #include "chrome/common/omnibox_focus_state.h" |
| 15 #include "chrome/common/render_messages.h" | 18 #include "chrome/common/render_messages.h" |
| 16 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
| 17 #include "chrome/renderer/searchbox/searchbox_extension.h" | 20 #include "chrome/renderer/searchbox/searchbox_extension.h" |
| 18 #include "components/favicon_base/favicon_types.h" | 21 #include "components/favicon_base/favicon_types.h" |
| 19 #include "content/public/renderer/render_view.h" | 22 #include "content/public/renderer/render_view.h" |
| 20 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
| 21 #include "third_party/WebKit/public/web/WebDocument.h" | 24 #include "third_party/WebKit/public/web/WebDocument.h" |
| 22 #include "third_party/WebKit/public/web/WebFrame.h" | 25 #include "third_party/WebKit/public/web/WebFrame.h" |
| 23 #include "third_party/WebKit/public/web/WebView.h" | 26 #include "third_party/WebKit/public/web/WebView.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 38 | 41 |
| 39 for (size_t i = 0; i < new_items.size(); ++i) { | 42 for (size_t i = 0; i < new_items.size(); ++i) { |
| 40 if (new_items[i].url != old_item_id_pairs[i].second.url || | 43 if (new_items[i].url != old_item_id_pairs[i].second.url || |
| 41 new_items[i].title != old_item_id_pairs[i].second.title) { | 44 new_items[i].title != old_item_id_pairs[i].second.title) { |
| 42 return false; | 45 return false; |
| 43 } | 46 } |
| 44 } | 47 } |
| 45 return true; | 48 return true; |
| 46 } | 49 } |
| 47 | 50 |
| 51 const char* GetIconTypeUrlHost(SearchBox::ImageSourceType type) { |
| 52 switch (type) { |
| 53 case SearchBox::FAVICON: |
| 54 return "favicon"; |
| 55 case SearchBox::LARGE_ICON: |
| 56 return "large-icon"; |
| 57 case SearchBox::FALLBACK_ICON: |
| 58 return "fallback-icon"; |
| 59 case SearchBox::THUMB: |
| 60 return "thumb"; |
| 61 default: |
| 62 NOTREACHED(); |
| 63 } |
| 64 return nullptr; |
| 65 } |
| 66 |
| 67 // Given |path| from an image URL, returns starting index of the page URL, |
| 68 // depending on |type| of image URL. Returns -1 if parse fails. |
| 69 int GetImagePathStartOfPageURL(SearchBox::ImageSourceType type, |
| 70 const std::string& path) { |
| 71 // TODO(huangs): Refactor this: http://crbug.com/468320. |
| 72 switch (type) { |
| 73 case SearchBox::FAVICON: { |
| 74 chrome::ParsedFaviconPath parsed; |
| 75 return chrome::ParseFaviconPath( |
| 76 path, favicon_base::FAVICON, &parsed) ? parsed.path_index : -1; |
| 77 } |
| 78 case SearchBox::LARGE_ICON: { |
| 79 LargeIconUrlParser parser; |
| 80 return parser.Parse(path) ? parser.path_index() : -1; |
| 81 } |
| 82 case SearchBox::FALLBACK_ICON: { |
| 83 chrome::ParsedFallbackIconPath parser; |
| 84 return parser.Parse(path) ? parser.path_index() : -1; |
| 85 } |
| 86 case SearchBox::THUMB: { |
| 87 return 0; |
| 88 } |
| 89 default: { |
| 90 NOTREACHED(); |
| 91 break; |
| 92 } |
| 93 } |
| 94 return -1; |
| 95 } |
| 96 |
| 97 // Helper for SearchBox::GenerateImageURLFromTransientURL(). |
| 98 class SearchBoxIconURLHelper: public SearchBox::IconURLHelper { |
| 99 public: |
| 100 explicit SearchBoxIconURLHelper(const SearchBox* search_box); |
| 101 ~SearchBoxIconURLHelper() override; |
| 102 int GetViewID() const override; |
| 103 std::string GetURLStringFromRestrictedID(InstantRestrictedID rid) const |
| 104 override; |
| 105 |
| 106 private: |
| 107 const SearchBox* search_box_; |
| 108 }; |
| 109 |
| 110 SearchBoxIconURLHelper::SearchBoxIconURLHelper(const SearchBox* search_box) |
| 111 : search_box_(search_box) { |
| 112 } |
| 113 |
| 114 SearchBoxIconURLHelper::~SearchBoxIconURLHelper() { |
| 115 } |
| 116 |
| 117 int SearchBoxIconURLHelper::GetViewID() const { |
| 118 return search_box_->render_view()->GetRoutingID(); |
| 119 } |
| 120 |
| 121 std::string SearchBoxIconURLHelper::GetURLStringFromRestrictedID( |
| 122 InstantRestrictedID rid) const { |
| 123 InstantMostVisitedItem item; |
| 124 if (!search_box_->GetMostVisitedItemWithID(rid, &item)) |
| 125 return std::string(); |
| 126 |
| 127 return item.url.spec(); |
| 128 } |
| 129 |
| 48 } // namespace | 130 } // namespace |
| 49 | 131 |
| 50 namespace internal { // for testing | 132 namespace internal { // for testing |
| 51 | 133 |
| 52 // Parses |path| and fills in |id| with the InstantRestrictedID obtained from | 134 // Parses "<view_id>/<restricted_id>". If successful, assigns |
| 53 // the |path|. |render_view_id| is the ID of the associated RenderView. | 135 // |*view_id| := "<view_id>", |*rid| := "<restricted_id>", and returns true. |
| 54 // | 136 bool ParseViewIdAndRestrictedId(const std::string id_part, |
| 55 // |path| is a pair of |render_view_id| and |restricted_id|, and it is | 137 int* view_id_out, |
| 56 // contained in Instant Extended URLs. A valid |path| is in the form: | 138 InstantRestrictedID* rid_out) { |
| 57 // <render_view_id>/<restricted_id> | 139 DCHECK(view_id_out); |
| 58 // | 140 DCHECK(rid_out); |
| 59 // If the |path| is valid, returns true and fills in |id| with restricted_id | |
| 60 // value. If the |path| is invalid, returns false and |id| is not set. | |
| 61 bool GetInstantRestrictedIDFromPath(int render_view_id, | |
| 62 const std::string& path, | |
| 63 InstantRestrictedID* id) { | |
| 64 // Check that the path is of Most visited item ID form. | 141 // Check that the path is of Most visited item ID form. |
| 65 std::vector<std::string> tokens; | 142 std::vector<std::string> tokens; |
| 66 if (Tokenize(path, "/", &tokens) != 2) | 143 if (Tokenize(id_part, "/", &tokens) != 2) |
| 67 return false; | 144 return false; |
| 68 | 145 |
| 69 int view_id = 0; | 146 int view_id; |
| 70 if (!base::StringToInt(tokens[0], &view_id) || view_id != render_view_id) | 147 InstantRestrictedID rid; |
| 148 if (!base::StringToInt(tokens[0], &view_id) || view_id < 0 || |
| 149 !base::StringToInt(tokens[1], &rid) || rid < 0) |
| 71 return false; | 150 return false; |
| 72 return base::StringToInt(tokens[1], id); | 151 |
| 152 *view_id_out = view_id; |
| 153 *rid_out = rid; |
| 154 return true; |
| 73 } | 155 } |
| 74 | 156 |
| 75 bool GetRestrictedIDFromFaviconUrl(int render_view_id, | 157 // Takes icon |url| of given |type|, e.g., FAVICON looking like |
| 76 const GURL& url, | 158 // |
| 77 std::string* favicon_params, | 159 // chrome-search://favicon/<view_id>/<restricted_id> |
| 78 InstantRestrictedID* rid) { | 160 // chrome-search://favicon/<parameters>/<view_id>/<restricted_id> |
| 161 // |
| 162 // If successful, assigns |*param_part| := "" or "<parameters>/" (note trailing |
| 163 // slash), |*view_id| := "<view_id>", |*rid| := "rid", and returns true. |
| 164 bool ParseIconRestrictedUrl(const GURL& url, |
| 165 SearchBox::ImageSourceType type, |
| 166 std::string* param_part, |
| 167 int* view_id, |
| 168 InstantRestrictedID* rid) { |
| 169 DCHECK(param_part); |
| 170 DCHECK(view_id); |
| 171 DCHECK(rid); |
| 79 // Strip leading slash. | 172 // Strip leading slash. |
| 80 std::string raw_path = url.path(); | 173 std::string raw_path = url.path(); |
| 81 DCHECK_GT(raw_path.length(), (size_t) 0); | 174 DCHECK_GT(raw_path.length(), (size_t) 0); |
| 82 DCHECK_EQ(raw_path[0], '/'); | 175 DCHECK_EQ(raw_path[0], '/'); |
| 83 raw_path = raw_path.substr(1); | 176 raw_path = raw_path.substr(1); |
| 84 | 177 |
| 85 chrome::ParsedFaviconPath parsed; | 178 int path_index = GetImagePathStartOfPageURL(type, raw_path); |
| 86 if (!chrome::ParseFaviconPath(raw_path, favicon_base::FAVICON, &parsed)) | 179 if (path_index < 0) |
| 87 return false; | 180 return false; |
| 88 | 181 |
| 89 // The part of the URL which details the favicon parameters should be returned | 182 std::string id_part = raw_path.substr(path_index); |
| 90 // so the favicon URL can be reconstructed, by replacing the restricted_id | 183 if (!ParseViewIdAndRestrictedId(id_part, view_id, rid)) |
| 91 // with the actual URL from which the favicon is being requested. | 184 return false; |
| 92 *favicon_params = raw_path.substr(0, parsed.path_index); | |
| 93 | 185 |
| 94 // The part of the favicon URL which is supposed to contain the URL from | 186 *param_part = raw_path.substr(0, path_index); |
| 95 // which the favicon is being requested (i.e., the page's URL) actually | |
| 96 // contains a pair in the format "<view_id>/<restricted_id>". If the page's | |
| 97 // URL is not in the expected format then the execution must be stopped, | |
| 98 // returning |true|, indicating that the favicon URL should be translated | |
| 99 // without the page's URL part, to prevent search providers from spoofing | |
| 100 // the user's browsing history. For example, the following favicon URL | |
| 101 // "chrome-search://favicon/http://www.secretsite.com" it is not in the | |
| 102 // expected format "chrome-search://favicon/<view_id>/<restricted_id>" so | |
| 103 // the pages's URL part ("http://www.secretsite.com") should be removed | |
| 104 // entirely from the translated URL otherwise the search engine would know | |
| 105 // if the user has visited that page (by verifying whether the favicon URL | |
| 106 // returns an image for a particular page's URL); the translated URL in this | |
| 107 // case would be "chrome-search://favicon/" which would simply return the | |
| 108 // default favicon. | |
| 109 std::string id_part = raw_path.substr(parsed.path_index); | |
| 110 InstantRestrictedID id; | |
| 111 if (!GetInstantRestrictedIDFromPath(render_view_id, id_part, &id)) | |
| 112 return true; | |
| 113 | |
| 114 *rid = id; | |
| 115 return true; | 187 return true; |
| 116 } | 188 } |
| 117 | 189 |
| 118 // Parses a thumbnail |url| and fills in |id| with the InstantRestrictedID | 190 bool TranslateIconRestrictedUrl(const GURL& transient_url, |
| 119 // obtained from the |url|. |render_view_id| is the ID of the associated | 191 SearchBox::ImageSourceType type, |
| 120 // RenderView. | 192 const SearchBox::IconURLHelper& helper, |
| 121 // | 193 GURL* url) { |
| 122 // Valid |url| forms: | 194 std::string params; |
| 123 // chrome-search://thumb/<view_id>/<restricted_id> | 195 int view_id = -1; |
| 124 // | 196 InstantRestrictedID rid = -1; |
| 125 // If the |url| is valid, returns true and fills in |id| with restricted_id | |
| 126 // value. If the |url| is invalid, returns false and |id| is not set. | |
| 127 bool GetRestrictedIDFromThumbnailUrl(int render_view_id, | |
| 128 const GURL& url, | |
| 129 InstantRestrictedID* id) { | |
| 130 // Strip leading slash. | |
| 131 std::string path = url.path(); | |
| 132 DCHECK_GT(path.length(), (size_t) 0); | |
| 133 DCHECK_EQ(path[0], '/'); | |
| 134 path = path.substr(1); | |
| 135 | 197 |
| 136 return GetInstantRestrictedIDFromPath(render_view_id, path, id); | 198 if (!internal::ParseIconRestrictedUrl( |
| 199 transient_url, type, ¶ms, &view_id, &rid) || |
| 200 view_id != helper.GetViewID()) { |
| 201 if (type == SearchBox::FAVICON) { |
| 202 *url = GURL(base::StringPrintf("chrome-search://%s/", |
| 203 GetIconTypeUrlHost(SearchBox::FAVICON))); |
| 204 return true; |
| 205 } |
| 206 return false; |
| 207 } |
| 208 |
| 209 std::string item_url = helper.GetURLStringFromRestrictedID(rid); |
| 210 *url = GURL(base::StringPrintf("chrome-search://%s/%s%s", |
| 211 GetIconTypeUrlHost(type), |
| 212 params.c_str(), |
| 213 item_url.c_str())); |
| 214 return true; |
| 137 } | 215 } |
| 138 | 216 |
| 139 } // namespace internal | 217 } // namespace internal |
| 140 | 218 |
| 219 SearchBox::IconURLHelper::IconURLHelper() { |
| 220 } |
| 221 |
| 222 SearchBox::IconURLHelper::~IconURLHelper() { |
| 223 } |
| 224 |
| 141 SearchBox::SearchBox(content::RenderView* render_view) | 225 SearchBox::SearchBox(content::RenderView* render_view) |
| 142 : content::RenderViewObserver(render_view), | 226 : content::RenderViewObserver(render_view), |
| 143 content::RenderViewObserverTracker<SearchBox>(render_view), | 227 content::RenderViewObserverTracker<SearchBox>(render_view), |
| 144 page_seq_no_(0), | 228 page_seq_no_(0), |
| 145 app_launcher_enabled_(false), | 229 app_launcher_enabled_(false), |
| 146 is_focused_(false), | 230 is_focused_(false), |
| 147 is_input_in_progress_(false), | 231 is_input_in_progress_(false), |
| 148 is_key_capture_enabled_(false), | 232 is_key_capture_enabled_(false), |
| 149 display_instant_results_(false), | 233 display_instant_results_(false), |
| 150 most_visited_items_cache_(kMaxInstantMostVisitedItemCacheSize), | 234 most_visited_items_cache_(kMaxInstantMostVisitedItemCacheSize), |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 } | 267 } |
| 184 | 268 |
| 185 void SearchBox::DeleteMostVisitedItem( | 269 void SearchBox::DeleteMostVisitedItem( |
| 186 InstantRestrictedID most_visited_item_id) { | 270 InstantRestrictedID most_visited_item_id) { |
| 187 render_view()->Send(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem( | 271 render_view()->Send(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem( |
| 188 render_view()->GetRoutingID(), | 272 render_view()->GetRoutingID(), |
| 189 page_seq_no_, | 273 page_seq_no_, |
| 190 GetURLForMostVisitedItem(most_visited_item_id))); | 274 GetURLForMostVisitedItem(most_visited_item_id))); |
| 191 } | 275 } |
| 192 | 276 |
| 193 bool SearchBox::GenerateFaviconURLFromTransientURL(const GURL& transient_url, | 277 bool SearchBox::GenerateImageURLFromTransientURL(const GURL& transient_url, |
| 194 GURL* url) const { | 278 ImageSourceType type, |
| 195 std::string favicon_params; | 279 GURL* url) const { |
| 196 InstantRestrictedID rid = -1; | 280 SearchBoxIconURLHelper helper(this); |
| 197 bool success = internal::GetRestrictedIDFromFaviconUrl( | 281 return internal::TranslateIconRestrictedUrl(transient_url, type, helper, url); |
| 198 render_view()->GetRoutingID(), transient_url, &favicon_params, &rid); | |
| 199 if (!success) | |
| 200 return false; | |
| 201 | |
| 202 InstantMostVisitedItem item; | |
| 203 std::string item_url; | |
| 204 if (rid != -1 && GetMostVisitedItemWithID(rid, &item)) | |
| 205 item_url = item.url.spec(); | |
| 206 | |
| 207 *url = GURL(base::StringPrintf("chrome-search://favicon/%s%s", | |
| 208 favicon_params.c_str(), | |
| 209 item_url.c_str())); | |
| 210 return true; | |
| 211 } | |
| 212 | |
| 213 bool SearchBox::GenerateThumbnailURLFromTransientURL(const GURL& transient_url, | |
| 214 GURL* url) const { | |
| 215 InstantRestrictedID rid = 0; | |
| 216 if (!internal::GetRestrictedIDFromThumbnailUrl(render_view()->GetRoutingID(), | |
| 217 transient_url, &rid)) { | |
| 218 return false; | |
| 219 } | |
| 220 | |
| 221 GURL most_visited_item_url(GetURLForMostVisitedItem(rid)); | |
| 222 if (most_visited_item_url.is_empty()) | |
| 223 return false; | |
| 224 *url = GURL(base::StringPrintf("chrome-search://thumb/%s", | |
| 225 most_visited_item_url.spec().c_str())); | |
| 226 return true; | |
| 227 } | 282 } |
| 228 | 283 |
| 229 void SearchBox::GetMostVisitedItems( | 284 void SearchBox::GetMostVisitedItems( |
| 230 std::vector<InstantMostVisitedItemIDPair>* items) const { | 285 std::vector<InstantMostVisitedItemIDPair>* items) const { |
| 231 return most_visited_items_cache_.GetCurrentItems(items); | 286 return most_visited_items_cache_.GetCurrentItems(items); |
| 232 } | 287 } |
| 233 | 288 |
| 234 bool SearchBox::GetMostVisitedItemWithID( | 289 bool SearchBox::GetMostVisitedItemWithID( |
| 235 InstantRestrictedID most_visited_item_id, | 290 InstantRestrictedID most_visited_item_id, |
| 236 InstantMostVisitedItem* item) const { | 291 InstantMostVisitedItem* item) const { |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 | 538 |
| 484 void SearchBox::Reset() { | 539 void SearchBox::Reset() { |
| 485 query_.clear(); | 540 query_.clear(); |
| 486 embedded_search_request_params_ = EmbeddedSearchRequestParams(); | 541 embedded_search_request_params_ = EmbeddedSearchRequestParams(); |
| 487 suggestion_ = InstantSuggestion(); | 542 suggestion_ = InstantSuggestion(); |
| 488 start_margin_ = 0; | 543 start_margin_ = 0; |
| 489 is_focused_ = false; | 544 is_focused_ = false; |
| 490 is_key_capture_enabled_ = false; | 545 is_key_capture_enabled_ = false; |
| 491 theme_info_ = ThemeBackgroundInfo(); | 546 theme_info_ = ThemeBackgroundInfo(); |
| 492 } | 547 } |
| OLD | NEW |