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" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
| 13 #include "chrome/browser/ui/webui/web_ui_util.h" | 12 #include "chrome/browser/ui/webui/web_ui_util.h" |
| 14 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
| 15 #include "grit/locale_settings.h" | 14 #include "grit/locale_settings.h" |
| 16 #include "grit/ui_resources.h" | 15 #include "grit/ui_resources.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/layout.h" | 17 #include "ui/base/layout.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
| 20 | 19 |
| 20 FaviconSource::IconRequest::IconRequest() | |
| 21 : request_path(""), | |
|
Evan Stade
2013/01/15 20:49:34
why is this necessary?
jam
2013/01/15 21:09:08
I had moved this from the header, i'll take this o
| |
| 22 size_in_dip(gfx::kFaviconSize), | |
| 23 scale_factor(ui::SCALE_FACTOR_NONE) { | |
| 24 } | |
| 25 | |
| 26 FaviconSource::IconRequest::IconRequest( | |
| 27 const content::URLDataSource::GotDataCallback& cb, | |
| 28 const std::string& path, | |
| 29 int size, | |
| 30 ui::ScaleFactor scale) | |
| 31 : callback(cb), | |
| 32 request_path(path), | |
| 33 size_in_dip(size), | |
| 34 scale_factor(scale) { | |
| 35 } | |
| 36 | |
| 37 FaviconSource::IconRequest::~IconRequest() { | |
| 38 } | |
| 39 | |
| 21 FaviconSource::FaviconSource(Profile* profile, IconType type) | 40 FaviconSource::FaviconSource(Profile* profile, IconType type) |
| 22 : profile_(profile->GetOriginalProfile()), | 41 : profile_(profile->GetOriginalProfile()), |
| 23 icon_types_(type == FAVICON ? history::FAVICON : | 42 icon_types_(type == FAVICON ? history::FAVICON : |
| 24 history::TOUCH_PRECOMPOSED_ICON | history::TOUCH_ICON | | 43 history::TOUCH_PRECOMPOSED_ICON | history::TOUCH_ICON | |
| 25 history::FAVICON) { | 44 history::FAVICON) { |
| 26 } | 45 } |
| 27 | 46 |
| 28 FaviconSource::~FaviconSource() { | 47 FaviconSource::~FaviconSource() { |
| 29 } | 48 } |
| 30 | 49 |
| 31 std::string FaviconSource::GetSource() { | 50 std::string FaviconSource::GetSource() { |
| 32 return icon_types_ == history::FAVICON ? | 51 return icon_types_ == history::FAVICON ? |
| 33 chrome::kChromeUIFaviconHost : chrome::kChromeUITouchIconHost; | 52 chrome::kChromeUIFaviconHost : chrome::kChromeUITouchIconHost; |
| 34 } | 53 } |
| 35 | 54 |
| 36 void FaviconSource::StartDataRequest(const std::string& path, | 55 void FaviconSource::StartDataRequest( |
| 37 bool is_incognito, | 56 const std::string& path, |
| 38 int request_id) { | 57 bool is_incognito, |
| 58 const content::URLDataSource::GotDataCallback& callback) { | |
| 39 FaviconService* favicon_service = | 59 FaviconService* favicon_service = |
| 40 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 60 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 41 if (!favicon_service || path.empty()) { | 61 if (!favicon_service || path.empty()) { |
| 42 SendDefaultResponse(IconRequest(request_id, | 62 SendDefaultResponse(IconRequest(callback, |
| 43 "", | 63 "", |
| 44 16, | 64 16, |
| 45 ui::SCALE_FACTOR_100P)); | 65 ui::SCALE_FACTOR_100P)); |
| 46 return; | 66 return; |
| 47 } | 67 } |
| 48 | 68 |
| 49 int size_in_dip = gfx::kFaviconSize; | 69 int size_in_dip = gfx::kFaviconSize; |
| 50 ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_100P; | 70 ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_100P; |
| 51 | 71 |
| 52 if (path.size() > 8 && | 72 if (path.size() > 8 && |
| 53 (path.substr(0, 8) == "iconurl/" || path.substr(0, 8) == "iconurl@")) { | 73 (path.substr(0, 8) == "iconurl/" || path.substr(0, 8) == "iconurl@")) { |
| 54 size_t prefix_length = 8; | 74 size_t prefix_length = 8; |
| 55 // Optional scale factor appended to iconurl, which may be @1x or @2x. | 75 // Optional scale factor appended to iconurl, which may be @1x or @2x. |
| 56 if (path.at(7) == '@') { | 76 if (path.at(7) == '@') { |
| 57 size_t slash = path.find("/"); | 77 size_t slash = path.find("/"); |
| 58 std::string scale_str = path.substr(8, slash - 8); | 78 std::string scale_str = path.substr(8, slash - 8); |
| 59 web_ui_util::ParseScaleFactor(scale_str, &scale_factor); | 79 web_ui_util::ParseScaleFactor(scale_str, &scale_factor); |
| 60 prefix_length = slash + 1; | 80 prefix_length = slash + 1; |
| 61 } | 81 } |
| 62 // TODO(michaelbai): Change GetRawFavicon to support combination of | 82 // TODO(michaelbai): Change GetRawFavicon to support combination of |
| 63 // IconType. | 83 // IconType. |
| 64 favicon_service->GetRawFavicon( | 84 favicon_service->GetRawFavicon( |
| 65 GURL(path.substr(prefix_length)), | 85 GURL(path.substr(prefix_length)), |
| 66 history::FAVICON, | 86 history::FAVICON, |
| 67 size_in_dip, | 87 size_in_dip, |
| 68 scale_factor, | 88 scale_factor, |
| 69 base::Bind(&FaviconSource::OnFaviconDataAvailable, | 89 base::Bind(&FaviconSource::OnFaviconDataAvailable, |
| 70 base::Unretained(this), | 90 base::Unretained(this), |
| 71 IconRequest(request_id, | 91 IconRequest(callback, |
| 72 path.substr(prefix_length), | 92 path.substr(prefix_length), |
| 73 size_in_dip, | 93 size_in_dip, |
| 74 scale_factor)), | 94 scale_factor)), |
| 75 &cancelable_task_tracker_); | 95 &cancelable_task_tracker_); |
| 76 } else { | 96 } else { |
| 77 GURL url; | 97 GURL url; |
| 78 if (path.size() > 5 && path.substr(0, 5) == "size/") { | 98 if (path.size() > 5 && path.substr(0, 5) == "size/") { |
| 79 size_t slash = path.find("/", 5); | 99 size_t slash = path.find("/", 5); |
| 80 size_t scale_delimiter = path.find("@", 5); | 100 size_t scale_delimiter = path.find("@", 5); |
| 81 std::string size = path.substr(5, slash - 5); | 101 std::string size = path.substr(5, slash - 5); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 108 url = GURL(originalUrl).GetOrigin(); | 128 url = GURL(originalUrl).GetOrigin(); |
| 109 } else { | 129 } else { |
| 110 url = GURL(path); | 130 url = GURL(path); |
| 111 } | 131 } |
| 112 } | 132 } |
| 113 | 133 |
| 114 // Intercept requests for prepopulated pages. | 134 // Intercept requests for prepopulated pages. |
| 115 for (size_t i = 0; i < arraysize(history::kPrepopulatedPages); i++) { | 135 for (size_t i = 0; i < arraysize(history::kPrepopulatedPages); i++) { |
| 116 if (url.spec() == | 136 if (url.spec() == |
| 117 l10n_util::GetStringUTF8(history::kPrepopulatedPages[i].url_id)) { | 137 l10n_util::GetStringUTF8(history::kPrepopulatedPages[i].url_id)) { |
| 118 url_data_source()->SendResponse(request_id, | 138 callback.Run( |
| 119 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( | 139 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( |
| 120 history::kPrepopulatedPages[i].favicon_id, | 140 history::kPrepopulatedPages[i].favicon_id, |
| 121 scale_factor)); | 141 scale_factor)); |
| 122 return; | 142 return; |
| 123 } | 143 } |
| 124 } | 144 } |
| 125 | 145 |
| 126 favicon_service->GetRawFaviconForURL( | 146 favicon_service->GetRawFaviconForURL( |
| 127 FaviconService::FaviconForURLParams( | 147 FaviconService::FaviconForURLParams( |
| 128 profile_, url, icon_types_, size_in_dip), | 148 profile_, url, icon_types_, size_in_dip), |
| 129 scale_factor, | 149 scale_factor, |
| 130 base::Bind(&FaviconSource::OnFaviconDataAvailable, | 150 base::Bind(&FaviconSource::OnFaviconDataAvailable, |
| 131 base::Unretained(this), | 151 base::Unretained(this), |
| 132 IconRequest(request_id, | 152 IconRequest(callback, |
| 133 url.spec(), | 153 url.spec(), |
| 134 size_in_dip, | 154 size_in_dip, |
| 135 scale_factor)), | 155 scale_factor)), |
| 136 &cancelable_task_tracker_); | 156 &cancelable_task_tracker_); |
| 137 } | 157 } |
| 138 } | 158 } |
| 139 | 159 |
| 140 std::string FaviconSource::GetMimeType(const std::string&) const { | 160 std::string FaviconSource::GetMimeType(const std::string&) const { |
| 141 // We need to explicitly return a mime type, otherwise if the user tries to | 161 // We need to explicitly return a mime type, otherwise if the user tries to |
| 142 // drag the image they get no extension. | 162 // drag the image they get no extension. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 153 // No additional checks to locate the favicon resource in the base | 173 // No additional checks to locate the favicon resource in the base |
| 154 // implementation. | 174 // implementation. |
| 155 return false; | 175 return false; |
| 156 } | 176 } |
| 157 | 177 |
| 158 void FaviconSource::OnFaviconDataAvailable( | 178 void FaviconSource::OnFaviconDataAvailable( |
| 159 const IconRequest& request, | 179 const IconRequest& request, |
| 160 const history::FaviconBitmapResult& bitmap_result) { | 180 const history::FaviconBitmapResult& bitmap_result) { |
| 161 if (bitmap_result.is_valid()) { | 181 if (bitmap_result.is_valid()) { |
| 162 // Forward the data along to the networking system. | 182 // Forward the data along to the networking system. |
| 163 url_data_source()->SendResponse( | 183 request.callback.Run(bitmap_result.bitmap_data); |
| 164 request.request_id, bitmap_result.bitmap_data); | |
| 165 } else if (!HandleMissingResource(request)) { | 184 } else if (!HandleMissingResource(request)) { |
| 166 SendDefaultResponse(request); | 185 SendDefaultResponse(request); |
| 167 } | 186 } |
| 168 } | 187 } |
| 169 | 188 |
| 170 void FaviconSource::SendDefaultResponse(const IconRequest& icon_request) { | 189 void FaviconSource::SendDefaultResponse(const IconRequest& icon_request) { |
| 171 int favicon_index; | 190 int favicon_index; |
| 172 int resource_id; | 191 int resource_id; |
| 173 switch (icon_request.size_in_dip) { | 192 switch (icon_request.size_in_dip) { |
| 174 case 64: | 193 case 64: |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 186 } | 205 } |
| 187 base::RefCountedMemory* default_favicon = default_favicons_[favicon_index]; | 206 base::RefCountedMemory* default_favicon = default_favicons_[favicon_index]; |
| 188 | 207 |
| 189 if (!default_favicon) { | 208 if (!default_favicon) { |
| 190 ui::ScaleFactor scale_factor = icon_request.scale_factor; | 209 ui::ScaleFactor scale_factor = icon_request.scale_factor; |
| 191 default_favicon = ResourceBundle::GetSharedInstance() | 210 default_favicon = ResourceBundle::GetSharedInstance() |
| 192 .LoadDataResourceBytesForScale(resource_id, scale_factor); | 211 .LoadDataResourceBytesForScale(resource_id, scale_factor); |
| 193 default_favicons_[favicon_index] = default_favicon; | 212 default_favicons_[favicon_index] = default_favicon; |
| 194 } | 213 } |
| 195 | 214 |
| 196 url_data_source()->SendResponse(icon_request.request_id, default_favicon); | 215 icon_request.callback.Run(default_favicon); |
| 197 } | 216 } |
| OLD | NEW |