Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(216)

Side by Side Diff: chrome/renderer/searchbox/searchbox.cc

Issue 1010783002: [Icons NTP] Working prototype to fetch, store, and display big icons. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tweaks and unit test fix. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/renderer/searchbox/searchbox.h ('k') | chrome/renderer/searchbox/searchbox_extension.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/time/time.h" 13 #include "base/time/time.h"
13 #include "chrome/common/chrome_switches.h" 14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/favicon/fallback_icon_url_parser.h"
14 #include "chrome/common/favicon/favicon_url_parser.h" 16 #include "chrome/common/favicon/favicon_url_parser.h"
17 #include "chrome/common/favicon/large_icon_url_parser.h"
15 #include "chrome/common/omnibox_focus_state.h" 18 #include "chrome/common/omnibox_focus_state.h"
16 #include "chrome/common/render_messages.h" 19 #include "chrome/common/render_messages.h"
17 #include "chrome/common/url_constants.h" 20 #include "chrome/common/url_constants.h"
18 #include "chrome/renderer/searchbox/searchbox_extension.h" 21 #include "chrome/renderer/searchbox/searchbox_extension.h"
19 #include "components/favicon_base/favicon_types.h" 22 #include "components/favicon_base/favicon_types.h"
20 #include "content/public/renderer/render_frame.h" 23 #include "content/public/renderer/render_frame.h"
21 #include "content/public/renderer/render_view.h" 24 #include "content/public/renderer/render_view.h"
22 #include "net/base/escape.h" 25 #include "net/base/escape.h"
23 #include "third_party/WebKit/public/web/WebDocument.h" 26 #include "third_party/WebKit/public/web/WebDocument.h"
24 #include "third_party/WebKit/public/web/WebFrame.h" 27 #include "third_party/WebKit/public/web/WebFrame.h"
(...skipping 17 matching lines...) Expand all
42 45
43 for (size_t i = 0; i < new_items.size(); ++i) { 46 for (size_t i = 0; i < new_items.size(); ++i) {
44 if (new_items[i].url != old_item_id_pairs[i].second.url || 47 if (new_items[i].url != old_item_id_pairs[i].second.url ||
45 new_items[i].title != old_item_id_pairs[i].second.title) { 48 new_items[i].title != old_item_id_pairs[i].second.title) {
46 return false; 49 return false;
47 } 50 }
48 } 51 }
49 return true; 52 return true;
50 } 53 }
51 54
55 const char* GetIconTypeUrlHost(SearchBox::ImageSourceType type) {
56 switch (type) {
57 case SearchBox::FAVICON:
58 return "favicon";
59 case SearchBox::LARGE_ICON:
60 return "large-icon";
61 case SearchBox::FALLBACK_ICON:
62 return "fallback-icon";
63 case SearchBox::THUMB:
64 return "thumb";
65 default:
66 NOTREACHED();
67 }
68 return nullptr;
69 }
70
71 // Given |path| from an image URL, returns starting index of the page URL,
72 // depending on |type| of image URL. Returns -1 if parse fails.
73 int GetImagePathStartOfPageURL(SearchBox::ImageSourceType type,
74 const std::string& path) {
75 // TODO(huangs): Refactor this: http://crbug.com/468320.
76 switch (type) {
77 case SearchBox::FAVICON: {
78 chrome::ParsedFaviconPath parsed;
79 return chrome::ParseFaviconPath(
80 path, favicon_base::FAVICON, &parsed) ? parsed.path_index : -1;
81 }
82 case SearchBox::LARGE_ICON: {
83 LargeIconUrlParser parser;
84 return parser.Parse(path) ? parser.path_index() : -1;
85 }
86 case SearchBox::FALLBACK_ICON: {
87 chrome::ParsedFallbackIconPath parser;
88 return parser.Parse(path) ? parser.path_index() : -1;
89 }
90 case SearchBox::THUMB: {
91 return 0;
92 }
93 default: {
94 NOTREACHED();
95 break;
96 }
97 }
98 return -1;
99 }
100
101 // Helper for SearchBox::GenerateImageURLFromTransientURL().
102 class SearchBoxIconURLHelper: public SearchBox::IconURLHelper {
103 public:
104 explicit SearchBoxIconURLHelper(const SearchBox* search_box);
105 ~SearchBoxIconURLHelper() override;
106 int GetViewID() const override;
107 std::string GetURLStringFromRestrictedID(InstantRestrictedID rid) const
108 override;
109
110 private:
111 const SearchBox* search_box_;
112 };
113
114 SearchBoxIconURLHelper::SearchBoxIconURLHelper(const SearchBox* search_box)
115 : search_box_(search_box) {
116 }
117
118 SearchBoxIconURLHelper::~SearchBoxIconURLHelper() {
119 }
120
121 int SearchBoxIconURLHelper::GetViewID() const {
122 return search_box_->render_view()->GetRoutingID();
123 }
124
125 std::string SearchBoxIconURLHelper::GetURLStringFromRestrictedID(
126 InstantRestrictedID rid) const {
127 InstantMostVisitedItem item;
128 if (!search_box_->GetMostVisitedItemWithID(rid, &item))
129 return std::string();
130
131 return item.url.spec();
132 }
133
52 } // namespace 134 } // namespace
53 135
54 namespace internal { // for testing 136 namespace internal { // for testing
55 137
56 // Parses |path| and fills in |id| with the InstantRestrictedID obtained from 138 // Parses "<view_id>/<restricted_id>". If successful, assigns
57 // the |path|. |render_view_id| is the ID of the associated RenderView. 139 // |*view_id| := "<view_id>", |*rid| := "<restricted_id>", and returns true.
58 // 140 bool ParseViewIdAndRestrictedId(const std::string id_part,
59 // |path| is a pair of |render_view_id| and |restricted_id|, and it is 141 int* view_id_out,
60 // contained in Instant Extended URLs. A valid |path| is in the form: 142 InstantRestrictedID* rid_out) {
61 // <render_view_id>/<restricted_id> 143 DCHECK(view_id_out);
62 // 144 DCHECK(rid_out);
63 // If the |path| is valid, returns true and fills in |id| with restricted_id
64 // value. If the |path| is invalid, returns false and |id| is not set.
65 bool GetInstantRestrictedIDFromPath(int render_view_id,
66 const std::string& path,
67 InstantRestrictedID* id) {
68 // Check that the path is of Most visited item ID form. 145 // Check that the path is of Most visited item ID form.
69 std::vector<std::string> tokens; 146 std::vector<std::string> tokens;
70 if (Tokenize(path, "/", &tokens) != 2) 147 if (Tokenize(id_part, "/", &tokens) != 2)
71 return false; 148 return false;
72 149
73 int view_id = 0; 150 int view_id;
74 if (!base::StringToInt(tokens[0], &view_id) || view_id != render_view_id) 151 InstantRestrictedID rid;
152 if (!base::StringToInt(tokens[0], &view_id) || view_id < 0 ||
153 !base::StringToInt(tokens[1], &rid) || rid < 0)
75 return false; 154 return false;
76 return base::StringToInt(tokens[1], id); 155
156 *view_id_out = view_id;
157 *rid_out = rid;
158 return true;
77 } 159 }
78 160
79 bool GetRestrictedIDFromFaviconUrl(int render_view_id, 161 // Takes icon |url| of given |type|, e.g., FAVICON looking like
80 const GURL& url, 162 //
81 std::string* favicon_params, 163 // chrome-search://favicon/<view_id>/<restricted_id>
82 InstantRestrictedID* rid) { 164 // chrome-search://favicon/<parameters>/<view_id>/<restricted_id>
165 //
166 // If successful, assigns |*param_part| := "" or "<parameters>/" (note trailing
167 // slash), |*view_id| := "<view_id>", |*rid| := "rid", and returns true.
168 bool ParseIconRestrictedUrl(const GURL& url,
169 SearchBox::ImageSourceType type,
170 std::string* param_part,
171 int* view_id,
172 InstantRestrictedID* rid) {
173 DCHECK(param_part);
174 DCHECK(view_id);
175 DCHECK(rid);
83 // Strip leading slash. 176 // Strip leading slash.
84 std::string raw_path = url.path(); 177 std::string raw_path = url.path();
85 DCHECK_GT(raw_path.length(), (size_t) 0); 178 DCHECK_GT(raw_path.length(), (size_t) 0);
86 DCHECK_EQ(raw_path[0], '/'); 179 DCHECK_EQ(raw_path[0], '/');
87 raw_path = raw_path.substr(1); 180 raw_path = raw_path.substr(1);
88 181
89 chrome::ParsedFaviconPath parsed; 182 int path_index = GetImagePathStartOfPageURL(type, raw_path);
90 if (!chrome::ParseFaviconPath(raw_path, favicon_base::FAVICON, &parsed)) 183 if (path_index < 0)
91 return false; 184 return false;
92 185
93 // The part of the URL which details the favicon parameters should be returned 186 std::string id_part = raw_path.substr(path_index);
94 // so the favicon URL can be reconstructed, by replacing the restricted_id 187 if (!ParseViewIdAndRestrictedId(id_part, view_id, rid))
95 // with the actual URL from which the favicon is being requested. 188 return false;
96 *favicon_params = raw_path.substr(0, parsed.path_index);
97 189
98 // The part of the favicon URL which is supposed to contain the URL from 190 *param_part = raw_path.substr(0, path_index);
99 // which the favicon is being requested (i.e., the page's URL) actually
100 // contains a pair in the format "<view_id>/<restricted_id>". If the page's
101 // URL is not in the expected format then the execution must be stopped,
102 // returning |true|, indicating that the favicon URL should be translated
103 // without the page's URL part, to prevent search providers from spoofing
104 // the user's browsing history. For example, the following favicon URL
105 // "chrome-search://favicon/http://www.secretsite.com" it is not in the
106 // expected format "chrome-search://favicon/<view_id>/<restricted_id>" so
107 // the pages's URL part ("http://www.secretsite.com") should be removed
108 // entirely from the translated URL otherwise the search engine would know
109 // if the user has visited that page (by verifying whether the favicon URL
110 // returns an image for a particular page's URL); the translated URL in this
111 // case would be "chrome-search://favicon/" which would simply return the
112 // default favicon.
113 std::string id_part = raw_path.substr(parsed.path_index);
114 InstantRestrictedID id;
115 if (!GetInstantRestrictedIDFromPath(render_view_id, id_part, &id))
116 return true;
117
118 *rid = id;
119 return true; 191 return true;
120 } 192 }
121 193
122 // Parses a thumbnail |url| and fills in |id| with the InstantRestrictedID 194 bool TranslateIconRestrictedUrl(const GURL& transient_url,
123 // obtained from the |url|. |render_view_id| is the ID of the associated 195 SearchBox::ImageSourceType type,
124 // RenderView. 196 const SearchBox::IconURLHelper& helper,
125 // 197 GURL* url) {
126 // Valid |url| forms: 198 std::string params;
127 // chrome-search://thumb/<view_id>/<restricted_id> 199 int view_id = -1;
128 // 200 InstantRestrictedID rid = -1;
129 // If the |url| is valid, returns true and fills in |id| with restricted_id
130 // value. If the |url| is invalid, returns false and |id| is not set.
131 bool GetRestrictedIDFromThumbnailUrl(int render_view_id,
132 const GURL& url,
133 InstantRestrictedID* id) {
134 // Strip leading slash.
135 std::string path = url.path();
136 DCHECK_GT(path.length(), (size_t) 0);
137 DCHECK_EQ(path[0], '/');
138 path = path.substr(1);
139 201
140 return GetInstantRestrictedIDFromPath(render_view_id, path, id); 202 if (!internal::ParseIconRestrictedUrl(
203 transient_url, type, &params, &view_id, &rid) ||
204 view_id != helper.GetViewID()) {
205 if (type == SearchBox::FAVICON) {
206 *url = GURL(base::StringPrintf("chrome-search://%s/",
207 GetIconTypeUrlHost(SearchBox::FAVICON)));
208 return true;
209 }
210 return false;
211 }
212
213 std::string item_url = helper.GetURLStringFromRestrictedID(rid);
214 *url = GURL(base::StringPrintf("chrome-search://%s/%s%s",
215 GetIconTypeUrlHost(type),
216 params.c_str(),
217 item_url.c_str()));
218 return true;
141 } 219 }
142 220
143 } // namespace internal 221 } // namespace internal
144 222
223 SearchBox::IconURLHelper::IconURLHelper() {
224 }
225
226 SearchBox::IconURLHelper::~IconURLHelper() {
227 }
228
145 SearchBox::SearchBox(content::RenderView* render_view) 229 SearchBox::SearchBox(content::RenderView* render_view)
146 : content::RenderViewObserver(render_view), 230 : content::RenderViewObserver(render_view),
147 content::RenderViewObserverTracker<SearchBox>(render_view), 231 content::RenderViewObserverTracker<SearchBox>(render_view),
148 page_seq_no_(0), 232 page_seq_no_(0),
149 app_launcher_enabled_(false), 233 app_launcher_enabled_(false),
150 is_focused_(false), 234 is_focused_(false),
151 is_input_in_progress_(false), 235 is_input_in_progress_(false),
152 is_key_capture_enabled_(false), 236 is_key_capture_enabled_(false),
153 display_instant_results_(false), 237 display_instant_results_(false),
154 most_visited_items_cache_(kMaxInstantMostVisitedItemCacheSize), 238 most_visited_items_cache_(kMaxInstantMostVisitedItemCacheSize),
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 } 278 }
195 279
196 void SearchBox::DeleteMostVisitedItem( 280 void SearchBox::DeleteMostVisitedItem(
197 InstantRestrictedID most_visited_item_id) { 281 InstantRestrictedID most_visited_item_id) {
198 render_view()->Send(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem( 282 render_view()->Send(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
199 render_view()->GetRoutingID(), 283 render_view()->GetRoutingID(),
200 page_seq_no_, 284 page_seq_no_,
201 GetURLForMostVisitedItem(most_visited_item_id))); 285 GetURLForMostVisitedItem(most_visited_item_id)));
202 } 286 }
203 287
204 bool SearchBox::GenerateFaviconURLFromTransientURL(const GURL& transient_url, 288 bool SearchBox::GenerateImageURLFromTransientURL(const GURL& transient_url,
205 GURL* url) const { 289 ImageSourceType type,
206 std::string favicon_params; 290 GURL* url) const {
207 InstantRestrictedID rid = -1; 291 SearchBoxIconURLHelper helper(this);
208 bool success = internal::GetRestrictedIDFromFaviconUrl( 292 return internal::TranslateIconRestrictedUrl(transient_url, type, helper, url);
209 render_view()->GetRoutingID(), transient_url, &favicon_params, &rid);
210 if (!success)
211 return false;
212
213 InstantMostVisitedItem item;
214 std::string item_url;
215 if (rid != -1 && GetMostVisitedItemWithID(rid, &item))
216 item_url = item.url.spec();
217
218 *url = GURL(base::StringPrintf("chrome-search://favicon/%s%s",
219 favicon_params.c_str(),
220 item_url.c_str()));
221 return true;
222 }
223
224 bool SearchBox::GenerateThumbnailURLFromTransientURL(const GURL& transient_url,
225 GURL* url) const {
226 InstantRestrictedID rid = 0;
227 if (!internal::GetRestrictedIDFromThumbnailUrl(render_view()->GetRoutingID(),
228 transient_url, &rid)) {
229 return false;
230 }
231
232 GURL most_visited_item_url(GetURLForMostVisitedItem(rid));
233 if (most_visited_item_url.is_empty())
234 return false;
235 *url = GURL(base::StringPrintf("chrome-search://thumb/%s",
236 most_visited_item_url.spec().c_str()));
237 return true;
238 } 293 }
239 294
240 void SearchBox::GetMostVisitedItems( 295 void SearchBox::GetMostVisitedItems(
241 std::vector<InstantMostVisitedItemIDPair>* items) const { 296 std::vector<InstantMostVisitedItemIDPair>* items) const {
242 return most_visited_items_cache_.GetCurrentItems(items); 297 return most_visited_items_cache_.GetCurrentItems(items);
243 } 298 }
244 299
245 bool SearchBox::GetMostVisitedItemWithID( 300 bool SearchBox::GetMostVisitedItemWithID(
246 InstantRestrictedID most_visited_item_id, 301 InstantRestrictedID most_visited_item_id,
247 InstantMostVisitedItem* item) const { 302 InstantMostVisitedItem* item) const {
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 549
495 void SearchBox::Reset() { 550 void SearchBox::Reset() {
496 query_.clear(); 551 query_.clear();
497 embedded_search_request_params_ = EmbeddedSearchRequestParams(); 552 embedded_search_request_params_ = EmbeddedSearchRequestParams();
498 suggestion_ = InstantSuggestion(); 553 suggestion_ = InstantSuggestion();
499 start_margin_ = 0; 554 start_margin_ = 0;
500 is_focused_ = false; 555 is_focused_ = false;
501 is_key_capture_enabled_ = false; 556 is_key_capture_enabled_ = false;
502 theme_info_ = ThemeBackgroundInfo(); 557 theme_info_ = ThemeBackgroundInfo();
503 } 558 }
OLDNEW
« no previous file with comments | « chrome/renderer/searchbox/searchbox.h ('k') | chrome/renderer/searchbox/searchbox_extension.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698