Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/browser/ui/webui/favicon_source.h" | 5 #include "chrome/browser/ui/webui/favicon_source.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "chrome/browser/favicon/favicon_service_factory.h" | 10 #include "chrome/browser/favicon/favicon_service_factory.h" |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 } | 154 } |
| 155 | 155 |
| 156 bool FaviconSource::ShouldReplaceExistingSource() const { | 156 bool FaviconSource::ShouldReplaceExistingSource() const { |
| 157 // Leave the existing DataSource in place, otherwise we'll drop any pending | 157 // Leave the existing DataSource in place, otherwise we'll drop any pending |
| 158 // requests on the floor. | 158 // requests on the floor. |
| 159 return false; | 159 return false; |
| 160 } | 160 } |
| 161 | 161 |
| 162 bool FaviconSource::ShouldServiceRequest(const net::URLRequest* request) const { | 162 bool FaviconSource::ShouldServiceRequest(const net::URLRequest* request) const { |
| 163 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { | 163 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { |
| 164 return InstantService::IsInstantPath(request->url()) && | 164 // Strip leading slash. |
| 165 InstantIOContext::ShouldServiceRequest(request); | 165 std::string path = request->url().path().substr(1); |
|
Jered
2013/05/31 17:34:33
Why do the !path.empty() check here? I think we ca
kmadhusu
2013/06/04 02:36:01
It was just a safety check. Removed.
| |
| 166 return !path.empty() && InstantIOContext::ShouldServiceRequest(request); | |
| 166 } | 167 } |
| 167 return URLDataSource::ShouldServiceRequest(request); | 168 return URLDataSource::ShouldServiceRequest(request); |
| 168 } | 169 } |
| 169 | 170 |
| 170 bool FaviconSource::HandleMissingResource(const IconRequest& request) { | 171 bool FaviconSource::HandleMissingResource(const IconRequest& request) { |
| 171 // No additional checks to locate the favicon resource in the base | 172 // No additional checks to locate the favicon resource in the base |
| 172 // implementation. | 173 // implementation. |
| 173 return false; | 174 return false; |
| 174 } | 175 } |
| 175 | 176 |
| 176 bool FaviconSource::ParsePath(const std::string& raw_path, | 177 bool FaviconSource::ParsePath(const std::string& raw_path, |
|
Jered
2013/05/31 17:34:33
Here and elsewhere, change "raw_path" to "path".
kmadhusu
2013/06/04 02:36:01
Done.
| |
| 177 bool* is_icon_url, | 178 bool* is_icon_url, |
| 178 GURL* url, | 179 GURL* url, |
| 179 int* size_in_dip, | 180 int* size_in_dip, |
| 180 ui::ScaleFactor* scale_factor) const { | 181 ui::ScaleFactor* scale_factor) const { |
| 181 DCHECK_EQ(16, gfx::kFaviconSize); | 182 DCHECK_EQ(16, gfx::kFaviconSize); |
| 182 | 183 |
| 183 *is_icon_url = false; | 184 *is_icon_url = false; |
| 184 *url = GURL(); | 185 *url = GURL(); |
| 185 *size_in_dip = 16; | 186 *size_in_dip = 16; |
| 186 *scale_factor = ui::SCALE_FACTOR_100P; | 187 *scale_factor = ui::SCALE_FACTOR_100P; |
| 187 | 188 |
| 188 if (raw_path.empty()) | 189 if (raw_path.empty()) |
| 189 return false; | 190 return false; |
| 190 | 191 |
| 191 // Translate to regular path if |raw_path| is of the form | |
| 192 // chrome-search://favicon/<most_visited_item_id>, where | |
| 193 // "most_visited_item_id" is a uint64. | |
| 194 std::string path = InstantService::MaybeTranslateInstantPathOnUI(profile_, | |
| 195 raw_path); | |
| 196 size_t parsed_index = 0; | 192 size_t parsed_index = 0; |
| 197 if (HasSubstringAt(path, parsed_index, kLargestParameter)) { | 193 if (HasSubstringAt(raw_path, parsed_index, kLargestParameter)) { |
| 198 parsed_index += strlen(kLargestParameter); | 194 parsed_index += strlen(kLargestParameter); |
| 199 *size_in_dip = 0; | 195 *size_in_dip = 0; |
| 200 } else if (HasSubstringAt(path, parsed_index, kSizeParameter)) { | 196 } else if (HasSubstringAt(raw_path, parsed_index, kSizeParameter)) { |
| 201 parsed_index += strlen(kSizeParameter); | 197 parsed_index += strlen(kSizeParameter); |
| 202 | 198 |
| 203 size_t slash = path.find("/", parsed_index); | 199 size_t slash = raw_path.find("/", parsed_index); |
| 204 if (slash == std::string::npos) | 200 if (slash == std::string::npos) |
| 205 return false; | 201 return false; |
| 206 | 202 |
| 207 size_t scale_delimiter = path.find("@", parsed_index); | 203 size_t scale_delimiter = raw_path.find("@", parsed_index); |
| 208 std::string size_str; | 204 std::string size_str; |
| 209 std::string scale_str; | 205 std::string scale_str; |
| 210 if (scale_delimiter == std::string::npos) { | 206 if (scale_delimiter == std::string::npos) { |
| 211 // Support the legacy size format of 'size/aa/' where 'aa' is the desired | 207 // Support the legacy size format of 'size/aa/' where 'aa' is the desired |
| 212 // size in DIP for the sake of not regressing the extensions which use it. | 208 // size in DIP for the sake of not regressing the extensions which use it. |
| 213 size_str = path.substr(parsed_index, slash - parsed_index); | 209 size_str = raw_path.substr(parsed_index, slash - parsed_index); |
| 214 } else { | 210 } else { |
| 215 size_str = path.substr(parsed_index, scale_delimiter - parsed_index); | 211 size_str = raw_path.substr(parsed_index, scale_delimiter - parsed_index); |
| 216 scale_str = path.substr(scale_delimiter + 1, | 212 scale_str = raw_path.substr(scale_delimiter + 1, |
| 217 slash - scale_delimiter - 1); | 213 slash - scale_delimiter - 1); |
| 218 } | 214 } |
| 219 | 215 |
| 220 if (!base::StringToInt(size_str, size_in_dip)) | 216 if (!base::StringToInt(size_str, size_in_dip)) |
| 221 return false; | 217 return false; |
| 222 | 218 |
| 223 if (*size_in_dip != 64 && *size_in_dip != 32) { | 219 if (*size_in_dip != 64 && *size_in_dip != 32) { |
| 224 // Only 64x64, 32x32 and 16x16 icons are supported. | 220 // Only 64x64, 32x32 and 16x16 icons are supported. |
| 225 *size_in_dip = 16; | 221 *size_in_dip = 16; |
| 226 } | 222 } |
| 227 | 223 |
| 228 if (!scale_str.empty()) | 224 if (!scale_str.empty()) |
| 229 webui::ParseScaleFactor(scale_str, scale_factor); | 225 webui::ParseScaleFactor(scale_str, scale_factor); |
| 230 | 226 |
| 231 // Return the default favicon (as opposed to a resized favicon) for | 227 // Return the default favicon (as opposed to a resized favicon) for |
| 232 // favicon sizes which are not cached by the favicon service. | 228 // favicon sizes which are not cached by the favicon service. |
| 233 // Currently the favicon service caches: | 229 // Currently the favicon service caches: |
| 234 // - favicons of sizes "16 * scale factor" px of type FAVICON | 230 // - favicons of sizes "16 * scale factor" px of type FAVICON |
| 235 // where scale factor is one of FaviconUtil::GetFaviconScaleFactors(). | 231 // where scale factor is one of FaviconUtil::GetFaviconScaleFactors(). |
| 236 // - the largest TOUCH_ICON / TOUCH_PRECOMPOSED_ICON | 232 // - the largest TOUCH_ICON / TOUCH_PRECOMPOSED_ICON |
| 237 if (*size_in_dip != 16 && icon_types_ == chrome::FAVICON) | 233 if (*size_in_dip != 16 && icon_types_ == chrome::FAVICON) |
| 238 return false; | 234 return false; |
| 239 | 235 |
| 240 parsed_index = slash + 1; | 236 parsed_index = slash + 1; |
| 241 } | 237 } |
| 242 | 238 |
| 243 if (HasSubstringAt(path, parsed_index, kIconURLParameter)) { | 239 if (HasSubstringAt(raw_path, parsed_index, kIconURLParameter)) { |
| 244 parsed_index += strlen(kIconURLParameter); | 240 parsed_index += strlen(kIconURLParameter); |
| 245 *is_icon_url = true; | 241 *is_icon_url = true; |
| 246 *url = GURL(path.substr(parsed_index)); | 242 *url = GURL(raw_path.substr(parsed_index)); |
| 247 } else { | 243 } else { |
| 248 // URL requests prefixed with "origin/" are converted to a form with an | 244 // URL requests prefixed with "origin/" are converted to a form with an |
| 249 // empty path and a valid scheme. (e.g., example.com --> | 245 // empty path and a valid scheme. (e.g., example.com --> |
| 250 // http://example.com/ or http://example.com/a --> http://example.com/) | 246 // http://example.com/ or http://example.com/a --> http://example.com/) |
| 251 if (HasSubstringAt(path, parsed_index, kOriginParameter)) { | 247 if (HasSubstringAt(raw_path, parsed_index, kOriginParameter)) { |
| 252 parsed_index += strlen(kOriginParameter); | 248 parsed_index += strlen(kOriginParameter); |
| 253 std::string possibly_invalid_url = path.substr(parsed_index); | 249 std::string possibly_invalid_url = raw_path.substr(parsed_index); |
| 254 | 250 |
| 255 // If the URL does not specify a scheme (e.g., example.com instead of | 251 // If the URL does not specify a scheme (e.g., example.com instead of |
| 256 // http://example.com), add "http://" as a default. | 252 // http://example.com), add "http://" as a default. |
| 257 if (!GURL(possibly_invalid_url).has_scheme()) | 253 if (!GURL(possibly_invalid_url).has_scheme()) |
| 258 possibly_invalid_url = "http://" + possibly_invalid_url; | 254 possibly_invalid_url = "http://" + possibly_invalid_url; |
| 259 | 255 |
| 260 // Strip the path beyond the top-level domain. | 256 // Strip the path beyond the top-level domain. |
| 261 *url = GURL(possibly_invalid_url).GetOrigin(); | 257 *url = GURL(possibly_invalid_url).GetOrigin(); |
| 262 } else { | 258 } else { |
| 263 *url = GURL(path.substr(parsed_index)); | 259 *url = GURL(raw_path.substr(parsed_index)); |
| 264 } | 260 } |
| 265 } | 261 } |
| 266 return true; | 262 return true; |
| 267 } | 263 } |
| 268 | 264 |
| 269 void FaviconSource::OnFaviconDataAvailable( | 265 void FaviconSource::OnFaviconDataAvailable( |
| 270 const IconRequest& request, | 266 const IconRequest& request, |
| 271 const chrome::FaviconBitmapResult& bitmap_result) { | 267 const chrome::FaviconBitmapResult& bitmap_result) { |
| 272 if (bitmap_result.is_valid()) { | 268 if (bitmap_result.is_valid()) { |
| 273 // Forward the data along to the networking system. | 269 // Forward the data along to the networking system. |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 304 | 300 |
| 305 if (!default_favicon) { | 301 if (!default_favicon) { |
| 306 ui::ScaleFactor scale_factor = icon_request.scale_factor; | 302 ui::ScaleFactor scale_factor = icon_request.scale_factor; |
| 307 default_favicon = ResourceBundle::GetSharedInstance() | 303 default_favicon = ResourceBundle::GetSharedInstance() |
| 308 .LoadDataResourceBytesForScale(resource_id, scale_factor); | 304 .LoadDataResourceBytesForScale(resource_id, scale_factor); |
| 309 default_favicons_[favicon_index] = default_favicon; | 305 default_favicons_[favicon_index] = default_favicon; |
| 310 } | 306 } |
| 311 | 307 |
| 312 icon_request.callback.Run(default_favicon); | 308 icon_request.callback.Run(default_favicon); |
| 313 } | 309 } |
| OLD | NEW |