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 "chrome/browser/favicon/favicon_service_factory.h" | 9 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 10 #include "chrome/browser/history/top_sites.h" | 10 #include "chrome/browser/history/top_sites.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 FaviconSource::FaviconSource(Profile* profile, | 27 FaviconSource::FaviconSource(Profile* profile, |
| 28 IconType type, | 28 IconType type, |
| 29 const std::string& source_name) | 29 const std::string& source_name) |
| 30 : DataSource(source_name, MessageLoop::current()) { | 30 : DataSource(source_name, MessageLoop::current()) { |
| 31 Init(profile, type); | 31 Init(profile, type); |
| 32 } | 32 } |
| 33 | 33 |
| 34 FaviconSource::~FaviconSource() { | 34 FaviconSource::~FaviconSource() { |
| 35 } | 35 } |
| 36 | 36 |
| 37 void FaviconSource::Init(Profile* profile, IconType type) { | |
| 38 profile_ = profile->GetOriginalProfile(); | |
| 39 icon_types_ = type == FAVICON ? history::FAVICON : | |
| 40 history::TOUCH_PRECOMPOSED_ICON | history::TOUCH_ICON | | |
| 41 history::FAVICON; | |
| 42 } | |
| 43 | |
| 44 void FaviconSource::StartDataRequest(const std::string& path, | 37 void FaviconSource::StartDataRequest(const std::string& path, |
| 45 bool is_incognito, | 38 bool is_incognito, |
| 46 int request_id) { | 39 int request_id) { |
| 47 FaviconService* favicon_service = | 40 FaviconService* favicon_service = |
| 48 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 41 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 49 if (!favicon_service || path.empty()) { | 42 if (!favicon_service || path.empty()) { |
| 50 SendDefaultResponse(IconRequest(request_id, | 43 SendDefaultResponse(IconRequest(request_id, |
| 51 "", | 44 "", |
| 52 16, | 45 16, |
| 53 ui::SCALE_FACTOR_100P)); | 46 ui::SCALE_FACTOR_100P)); |
| 54 return; | 47 return; |
| 55 } | 48 } |
| 56 | 49 |
| 57 int size_in_dip = gfx::kFaviconSize; | 50 int size_in_dip = gfx::kFaviconSize; |
| 58 ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_100P; | 51 ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_100P; |
| 59 | 52 |
| 60 if (path.size() > 8 && | 53 size_t parsed_index = 0; |
|
Evan Stade
2013/01/07 20:23:56
can you add some docs about the expected format
| |
| 61 (path.substr(0, 8) == "iconurl/" || path.substr(0, 8) == "iconurl@")) { | 54 if (path.size() > 5 && path.substr(0, 5) == "size/") { |
|
Evan Stade
2013/01/07 20:23:56
this is a lot of 5s, can you pull it out into a co
| |
| 62 size_t prefix_length = 8; | 55 size_t slash = path.find("/", 5); |
| 63 // Optional scale factor appended to iconurl, which may be @1x or @2x. | 56 size_t scale_delimiter = path.find("@", 5); |
|
Evan Stade
2013/01/07 20:23:56
define variables as late as possible
| |
| 64 if (path.at(7) == '@') { | 57 std::string size = path.substr(5, slash - 5); |
| 65 size_t slash = path.find("/"); | 58 size_in_dip = atoi(size.c_str()); |
|
Evan Stade
2013/01/07 20:23:56
base::StringToInt
| |
| 66 std::string scale_str = path.substr(8, slash - 8); | 59 if (size_in_dip != 64 && size_in_dip != 32 && size_in_dip != 16) { |
| 67 web_ui_util::ParseScaleFactor(scale_str, &scale_factor); | 60 // Only 64x64, 32x32 and 16x16 icons are supported. |
| 68 prefix_length = slash + 1; | 61 size_in_dip = 16; |
| 69 } | 62 } |
| 63 // Optional scale factor. | |
| 64 if (scale_delimiter != std::string::npos && scale_delimiter < slash) { | |
| 65 DCHECK(size_in_dip == 16); | |
| 66 std::string scale_str = path.substr(scale_delimiter + 1, | |
| 67 slash - scale_delimiter - 1); | |
| 68 web_ui_util::ParseScaleFactor(scale_str, &scale_factor); | |
| 69 } | |
| 70 parsed_index = slash + 1; | |
| 71 } | |
| 72 | |
| 73 if (path.size() > parsed_index + 8 && | |
| 74 path.substr(parsed_index, 8) == "iconurl/") { | |
| 75 parsed_index += 8; | |
|
Evan Stade
2013/01/07 20:23:56
ditto with the 8s
| |
| 70 // TODO(michaelbai): Change GetRawFavicon to support combination of | 76 // TODO(michaelbai): Change GetRawFavicon to support combination of |
| 71 // IconType. | 77 // IconType. |
| 72 favicon_service->GetRawFavicon( | 78 favicon_service->GetRawFavicon( |
| 73 GURL(path.substr(prefix_length)), | 79 GURL(path.substr(parsed_index)), |
| 74 history::FAVICON, | 80 history::FAVICON, |
| 75 size_in_dip, | 81 size_in_dip, |
| 76 scale_factor, | 82 scale_factor, |
| 77 base::Bind(&FaviconSource::OnFaviconDataAvailable, | 83 base::Bind(&FaviconSource::OnFaviconDataAvailable, |
| 78 base::Unretained(this), | 84 base::Unretained(this), |
| 79 IconRequest(request_id, | 85 IconRequest(request_id, |
| 80 path.substr(prefix_length), | 86 path.substr(parsed_index), |
| 81 size_in_dip, | 87 size_in_dip, |
| 82 scale_factor)), | 88 scale_factor)), |
| 83 &cancelable_task_tracker_); | 89 &cancelable_task_tracker_); |
| 84 } else { | 90 } else { |
| 85 GURL url; | 91 GURL url; |
| 86 if (path.size() > 5 && path.substr(0, 5) == "size/") { | 92 |
| 87 size_t slash = path.find("/", 5); | 93 // URL requests prefixed with "origin/" are converted to a form with an |
| 88 size_t scale_delimiter = path.find("@", 5); | 94 // empty path and a valid scheme. (e.g., example.com --> |
| 89 std::string size = path.substr(5, slash - 5); | 95 // http://example.com/ or http://example.com/a --> http://example.com/) |
| 90 size_in_dip = atoi(size.c_str()); | 96 if (path.size() > parsed_index + 7 && |
| 91 if (size_in_dip != 64 && size_in_dip != 32 && size_in_dip != 16) { | 97 path.substr(parsed_index, 7) == "origin/") { |
| 92 // Only 64x64, 32x32 and 16x16 icons are supported. | 98 parsed_index += 7; |
|
Evan Stade
2013/01/07 20:23:56
ditto with the 7s
| |
| 93 size_in_dip = 16; | 99 std::string originalUrl = path.substr(parsed_index); |
|
Evan Stade
2013/01/07 20:23:56
original_url
| |
| 94 } | 100 |
| 95 // Optional scale factor. | 101 // If the original URL does not specify a scheme (e.g., example.com |
| 96 if (scale_delimiter != std::string::npos && scale_delimiter < slash) { | 102 // instead of http://example.com), add "http://" as a default. |
| 97 DCHECK(size_in_dip == 16); | 103 if (!GURL(originalUrl).has_scheme()) |
| 98 std::string scale_str = path.substr(scale_delimiter + 1, | 104 originalUrl = "http://" + originalUrl; |
| 99 slash - scale_delimiter - 1); | 105 |
| 100 web_ui_util::ParseScaleFactor(scale_str, &scale_factor); | 106 // Strip the path beyond the top-level domain. |
| 101 } | 107 url = GURL(originalUrl).GetOrigin(); |
| 102 url = GURL(path.substr(slash + 1)); | |
| 103 } else { | 108 } else { |
| 104 // URL requests prefixed with "origin/" are converted to a form with an | 109 url = GURL(path.substr(parsed_index)); |
| 105 // empty path and a valid scheme. (e.g., example.com --> | |
| 106 // http://example.com/ or http://example.com/a --> http://example.com/) | |
| 107 if (path.size() > 7 && path.substr(0, 7) == "origin/") { | |
| 108 std::string originalUrl = path.substr(7); | |
| 109 | |
| 110 // If the original URL does not specify a scheme (e.g., example.com | |
| 111 // instead of http://example.com), add "http://" as a default. | |
| 112 if (!GURL(originalUrl).has_scheme()) | |
| 113 originalUrl = "http://" + originalUrl; | |
| 114 | |
| 115 // Strip the path beyond the top-level domain. | |
| 116 url = GURL(originalUrl).GetOrigin(); | |
| 117 } else { | |
| 118 url = GURL(path); | |
| 119 } | |
| 120 } | 110 } |
| 121 | 111 |
| 122 // Intercept requests for prepopulated pages. | 112 // Intercept requests for prepopulated pages. |
| 123 for (size_t i = 0; i < arraysize(history::kPrepopulatedPages); i++) { | 113 for (size_t i = 0; i < arraysize(history::kPrepopulatedPages); i++) { |
| 124 if (url.spec() == | 114 if (url.spec() == |
| 125 l10n_util::GetStringUTF8(history::kPrepopulatedPages[i].url_id)) { | 115 l10n_util::GetStringUTF8(history::kPrepopulatedPages[i].url_id)) { |
| 126 SendResponse(request_id, | 116 SendResponse(request_id, |
| 127 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( | 117 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( |
| 128 history::kPrepopulatedPages[i].favicon_id, | 118 history::kPrepopulatedPages[i].favicon_id, |
| 129 scale_factor)); | 119 scale_factor)); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 156 // requests on the floor. | 146 // requests on the floor. |
| 157 return false; | 147 return false; |
| 158 } | 148 } |
| 159 | 149 |
| 160 bool FaviconSource::HandleMissingResource(const IconRequest& request) { | 150 bool FaviconSource::HandleMissingResource(const IconRequest& request) { |
| 161 // No additional checks to locate the favicon resource in the base | 151 // No additional checks to locate the favicon resource in the base |
| 162 // implementation. | 152 // implementation. |
| 163 return false; | 153 return false; |
| 164 } | 154 } |
| 165 | 155 |
| 156 void FaviconSource::Init(Profile* profile, IconType type) { | |
| 157 profile_ = profile->GetOriginalProfile(); | |
| 158 icon_types_ = type == FAVICON ? history::FAVICON : | |
| 159 history::TOUCH_PRECOMPOSED_ICON | history::TOUCH_ICON | | |
| 160 history::FAVICON; | |
| 161 } | |
| 162 | |
| 166 void FaviconSource::OnFaviconDataAvailable( | 163 void FaviconSource::OnFaviconDataAvailable( |
| 167 const IconRequest& request, | 164 const IconRequest& request, |
| 168 const history::FaviconBitmapResult& bitmap_result) { | 165 const history::FaviconBitmapResult& bitmap_result) { |
| 169 if (bitmap_result.is_valid()) { | 166 if (bitmap_result.is_valid()) { |
| 170 // Forward the data along to the networking system. | 167 // Forward the data along to the networking system. |
| 171 SendResponse(request.request_id, bitmap_result.bitmap_data); | 168 SendResponse(request.request_id, bitmap_result.bitmap_data); |
| 172 } else if (!HandleMissingResource(request)) { | 169 } else if (!HandleMissingResource(request)) { |
| 173 SendDefaultResponse(request); | 170 SendDefaultResponse(request); |
| 174 } | 171 } |
| 175 } | 172 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 195 | 192 |
| 196 if (!default_favicon) { | 193 if (!default_favicon) { |
| 197 ui::ScaleFactor scale_factor = icon_request.scale_factor; | 194 ui::ScaleFactor scale_factor = icon_request.scale_factor; |
| 198 default_favicon = ResourceBundle::GetSharedInstance() | 195 default_favicon = ResourceBundle::GetSharedInstance() |
| 199 .LoadDataResourceBytesForScale(resource_id, scale_factor); | 196 .LoadDataResourceBytesForScale(resource_id, scale_factor); |
| 200 default_favicons_[favicon_index] = default_favicon; | 197 default_favicons_[favicon_index] = default_favicon; |
| 201 } | 198 } |
| 202 | 199 |
| 203 SendResponse(icon_request.request_id, default_favicon); | 200 SendResponse(icon_request.request_id, default_favicon); |
| 204 } | 201 } |
| OLD | NEW |