| 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" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/webui/web_ui_util.h" |
| 12 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
| 13 #include "grit/locale_settings.h" | 14 #include "grit/locale_settings.h" |
| 14 #include "grit/ui_resources.h" | 15 #include "grit/ui_resources.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 16 #include "ui/base/layout.h" | 17 #include "ui/base/layout.h" |
| 17 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
| 18 #include "ui/gfx/favicon_size.h" | 19 #include "ui/gfx/favicon_size.h" |
| 19 | 20 |
| 20 FaviconSource::FaviconSource(Profile* profile, IconType type) | 21 FaviconSource::FaviconSource(Profile* profile, IconType type) |
| 21 : DataSource(type == FAVICON ? chrome::kChromeUIFaviconHost : | 22 : DataSource(type == FAVICON ? chrome::kChromeUIFaviconHost : |
| (...skipping 18 matching lines...) Expand all Loading... |
| 40 history::TOUCH_PRECOMPOSED_ICON | history::TOUCH_ICON | | 41 history::TOUCH_PRECOMPOSED_ICON | history::TOUCH_ICON | |
| 41 history::FAVICON; | 42 history::FAVICON; |
| 42 } | 43 } |
| 43 | 44 |
| 44 void FaviconSource::StartDataRequest(const std::string& path, | 45 void FaviconSource::StartDataRequest(const std::string& path, |
| 45 bool is_incognito, | 46 bool is_incognito, |
| 46 int request_id) { | 47 int request_id) { |
| 47 FaviconService* favicon_service = | 48 FaviconService* favicon_service = |
| 48 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 49 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 49 if (!favicon_service || path.empty()) { | 50 if (!favicon_service || path.empty()) { |
| 50 SendDefaultResponse(request_id); | 51 SendDefaultResponse(IconRequest(request_id, 16, ui::SCALE_FACTOR_NONE)); |
| 51 return; | 52 return; |
| 52 } | 53 } |
| 53 | 54 |
| 55 int pixel_size = gfx::kFaviconSize; |
| 56 ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_NONE; |
| 57 |
| 54 FaviconService::Handle handle; | 58 FaviconService::Handle handle; |
| 55 if (path.size() > 8 && path.substr(0, 8) == "iconurl/") { | 59 if (path.size() > 8 && |
| 60 (path.substr(0, 8) == "iconurl/" || path.substr(0, 8) == "iconurl@")) { |
| 61 size_t prefix_length = 8; |
| 62 // Optional scale factor appended to iconurl, which may be @1x or @2x. |
| 63 if (path.at(7) == '@') { |
| 64 size_t slash = path.find("/"); |
| 65 std::string scale_str = path.substr(8, slash - 8); |
| 66 web_ui_util::ParseScaleFactor(scale_str, &scale_factor); |
| 67 prefix_length = slash + 1; |
| 68 } |
| 56 // TODO : Change GetFavicon to support combination of IconType. | 69 // TODO : Change GetFavicon to support combination of IconType. |
| 57 handle = favicon_service->GetRawFavicon( | 70 handle = favicon_service->GetRawFavicon( |
| 58 GURL(path.substr(8)), | 71 GURL(path.substr(prefix_length)), |
| 59 history::FAVICON, | 72 history::FAVICON, |
| 60 gfx::kFaviconSize, | 73 pixel_size, |
| 61 ui::SCALE_FACTOR_100P, | 74 scale_factor, |
| 62 &cancelable_consumer_, | 75 &cancelable_consumer_, |
| 63 base::Bind(&FaviconSource::OnFaviconDataAvailable, | 76 base::Bind(&FaviconSource::OnFaviconDataAvailable, |
| 64 base::Unretained(this))); | 77 base::Unretained(this))); |
| 65 } else { | 78 } else { |
| 66 GURL url; | 79 GURL url; |
| 67 | |
| 68 if (path.size() > 5 && path.substr(0, 5) == "size/") { | 80 if (path.size() > 5 && path.substr(0, 5) == "size/") { |
| 69 size_t slash = path.find("/", 5); | 81 size_t slash = path.find("/", 5); |
| 82 size_t scale_delimiter = path.find("@", 5); |
| 70 std::string size = path.substr(5, slash - 5); | 83 std::string size = path.substr(5, slash - 5); |
| 71 int pixel_size = atoi(size.c_str()); | 84 pixel_size = atoi(size.c_str()); |
| 72 CHECK(pixel_size == 32 || pixel_size == 16) << | 85 CHECK(pixel_size == 32 || pixel_size == 16) << |
| 73 "only 32x32 and 16x16 icons are supported"; | 86 "only 32x32 and 16x16 icons are supported"; |
| 74 request_size_map_[request_id] = pixel_size; | 87 // Optional scale factor. |
| 88 if (scale_delimiter != std::string::npos && scale_delimiter < slash) { |
| 89 std::string scale_str = path.substr(scale_delimiter + 1, |
| 90 slash - scale_delimiter - 1); |
| 91 web_ui_util::ParseScaleFactor(scale_str, &scale_factor); |
| 92 } |
| 75 url = GURL(path.substr(slash + 1)); | 93 url = GURL(path.substr(slash + 1)); |
| 76 } else { | 94 } else { |
| 77 // URL requests prefixed with "origin/" are converted to a form with an | 95 // URL requests prefixed with "origin/" are converted to a form with an |
| 78 // empty path and a valid scheme. (e.g., example.com --> | 96 // empty path and a valid scheme. (e.g., example.com --> |
| 79 // http://example.com/ or http://example.com/a --> http://example.com/) | 97 // http://example.com/ or http://example.com/a --> http://example.com/) |
| 80 if (path.size() > 7 && path.substr(0, 7) == "origin/") { | 98 if (path.size() > 7 && path.substr(0, 7) == "origin/") { |
| 81 std::string originalUrl = path.substr(7); | 99 std::string originalUrl = path.substr(7); |
| 82 | 100 |
| 83 // If the original URL does not specify a scheme (e.g., example.com | 101 // If the original URL does not specify a scheme (e.g., example.com |
| 84 // instead of http://example.com), add "http://" as a default. | 102 // instead of http://example.com), add "http://" as a default. |
| 85 if (!GURL(originalUrl).has_scheme()) | 103 if (!GURL(originalUrl).has_scheme()) |
| 86 originalUrl = "http://" + originalUrl; | 104 originalUrl = "http://" + originalUrl; |
| 87 | 105 |
| 88 // Strip the path beyond the top-level domain. | 106 // Strip the path beyond the top-level domain. |
| 89 url = GURL(originalUrl).GetOrigin(); | 107 url = GURL(originalUrl).GetOrigin(); |
| 90 } else { | 108 } else { |
| 91 url = GURL(path); | 109 url = GURL(path); |
| 92 } | 110 } |
| 93 | |
| 94 request_size_map_[request_id] = 16; | |
| 95 } | 111 } |
| 96 | 112 |
| 97 // Intercept requests for prepopulated pages. | 113 // Intercept requests for prepopulated pages. |
| 98 for (size_t i = 0; i < arraysize(history::kPrepopulatedPages); i++) { | 114 for (size_t i = 0; i < arraysize(history::kPrepopulatedPages); i++) { |
| 99 if (url.spec() == | 115 if (url.spec() == |
| 100 l10n_util::GetStringUTF8(history::kPrepopulatedPages[i].url_id)) { | 116 l10n_util::GetStringUTF8(history::kPrepopulatedPages[i].url_id)) { |
| 101 request_size_map_.erase(request_id); | |
| 102 SendResponse(request_id, | 117 SendResponse(request_id, |
| 103 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | 118 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
| 104 history::kPrepopulatedPages[i].favicon_id, | 119 history::kPrepopulatedPages[i].favicon_id, |
| 105 ui::SCALE_FACTOR_100P)); | 120 scale_factor)); |
| 106 return; | 121 return; |
| 107 } | 122 } |
| 108 } | 123 } |
| 109 | 124 |
| 110 // TODO(estade): fetch the requested size. | |
| 111 handle = favicon_service->GetRawFaviconForURL( | 125 handle = favicon_service->GetRawFaviconForURL( |
| 112 FaviconService::FaviconForURLParams( | 126 FaviconService::FaviconForURLParams( |
| 113 profile_, | 127 profile_, |
| 114 url, | 128 url, |
| 115 icon_types_, | 129 icon_types_, |
| 116 gfx::kFaviconSize, | 130 pixel_size, |
| 117 &cancelable_consumer_), | 131 &cancelable_consumer_), |
| 118 ui::SCALE_FACTOR_100P, | 132 scale_factor, |
| 119 base::Bind(&FaviconSource::OnFaviconDataAvailable, | 133 base::Bind(&FaviconSource::OnFaviconDataAvailable, |
| 120 base::Unretained(this))); | 134 base::Unretained(this))); |
| 121 } | 135 } |
| 122 | 136 |
| 123 // Attach the ChromeURLDataManager request ID to the history request. | 137 // Attach the ChromeURLDataManager request ID to the history request. |
| 124 cancelable_consumer_.SetClientData(favicon_service, handle, request_id); | 138 cancelable_consumer_.SetClientData(favicon_service, |
| 139 handle, |
| 140 IconRequest(request_id, |
| 141 pixel_size, |
| 142 scale_factor)); |
| 125 } | 143 } |
| 126 | 144 |
| 127 std::string FaviconSource::GetMimeType(const std::string&) const { | 145 std::string FaviconSource::GetMimeType(const std::string&) const { |
| 128 // We need to explicitly return a mime type, otherwise if the user tries to | 146 // We need to explicitly return a mime type, otherwise if the user tries to |
| 129 // drag the image they get no extension. | 147 // drag the image they get no extension. |
| 130 return "image/png"; | 148 return "image/png"; |
| 131 } | 149 } |
| 132 | 150 |
| 133 bool FaviconSource::ShouldReplaceExistingSource() const { | 151 bool FaviconSource::ShouldReplaceExistingSource() const { |
| 134 // Leave the existing DataSource in place, otherwise we'll drop any pending | 152 // Leave the existing DataSource in place, otherwise we'll drop any pending |
| 135 // requests on the floor. | 153 // requests on the floor. |
| 136 return false; | 154 return false; |
| 137 } | 155 } |
| 138 | 156 |
| 139 void FaviconSource::OnFaviconDataAvailable( | 157 void FaviconSource::OnFaviconDataAvailable( |
| 140 FaviconService::Handle request_handle, | 158 FaviconService::Handle request_handle, |
| 141 const history::FaviconBitmapResult& bitmap_result) { | 159 const history::FaviconBitmapResult& bitmap_result) { |
| 142 FaviconService* favicon_service = | 160 FaviconService* favicon_service = |
| 143 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 161 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 144 int request_id = cancelable_consumer_.GetClientData(favicon_service, | 162 const IconRequest& request = |
| 145 request_handle); | 163 cancelable_consumer_.GetClientData(favicon_service, |
| 164 request_handle); |
| 146 | 165 |
| 147 if (bitmap_result.is_valid()) { | 166 if (bitmap_result.is_valid()) { |
| 148 // Forward the data along to the networking system. | 167 // Forward the data along to the networking system. |
| 149 SendResponse(request_id, bitmap_result.bitmap_data); | 168 SendResponse(request.request_id, bitmap_result.bitmap_data); |
| 150 } else { | 169 } else { |
| 151 SendDefaultResponse(request_id); | 170 SendDefaultResponse(request); |
| 152 } | 171 } |
| 153 } | 172 } |
| 154 | 173 |
| 155 void FaviconSource::SendDefaultResponse(int request_id) { | 174 void FaviconSource::SendDefaultResponse(const IconRequest& icon_request) { |
| 156 base::RefCountedMemory* bytes = NULL; | 175 base::RefCountedMemory* bytes = NULL; |
| 157 if (request_size_map_[request_id] == 32) { | 176 ui::ScaleFactor scale_factor = icon_request.scale_factor; |
| 177 |
| 178 if (icon_request.pixel_size == 32) { |
| 158 if (!default_favicon_large_.get()) { | 179 if (!default_favicon_large_.get()) { |
| 159 default_favicon_large_ = | 180 default_favicon_large_ = |
| 160 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | 181 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
| 161 IDR_DEFAULT_LARGE_FAVICON, ui::SCALE_FACTOR_100P); | 182 IDR_DEFAULT_LARGE_FAVICON, scale_factor); |
| 162 } | 183 } |
| 163 bytes = default_favicon_large_; | 184 bytes = default_favicon_large_; |
| 164 } else { | 185 } else { |
| 165 if (!default_favicon_.get()) { | 186 if (!default_favicon_.get()) { |
| 166 default_favicon_ = | 187 default_favicon_ = |
| 167 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | 188 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
| 168 IDR_DEFAULT_FAVICON, ui::SCALE_FACTOR_100P); | 189 IDR_DEFAULT_FAVICON, scale_factor); |
| 169 } | 190 } |
| 170 bytes = default_favicon_; | 191 bytes = default_favicon_; |
| 171 } | 192 } |
| 172 request_size_map_.erase(request_id); | 193 SendResponse(icon_request.request_id, bytes); |
| 173 | |
| 174 SendResponse(request_id, bytes); | |
| 175 } | 194 } |
| OLD | NEW |