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 <cmath> | |
8 | |
9 #include "base/bind.h" | 7 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
11 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
12 #include "chrome/browser/favicon/favicon_service_factory.h" | 10 #include "chrome/browser/favicon/favicon_service_factory.h" |
13 #include "chrome/browser/history/top_sites_factory.h" | 11 #include "chrome/browser/history/top_sites_factory.h" |
14 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/search/instant_io_context.h" | 13 #include "chrome/browser/search/instant_io_context.h" |
16 #include "chrome/browser/sync/profile_sync_service_factory.h" | 14 #include "chrome/browser/sync/profile_sync_service_factory.h" |
17 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
18 #include "components/browser_sync/profile_sync_service.h" | 16 #include "components/browser_sync/profile_sync_service.h" |
19 #include "components/favicon_base/favicon_url_parser.h" | 17 #include "components/favicon_base/favicon_url_parser.h" |
20 #include "components/history/core/browser/top_sites.h" | 18 #include "components/history/core/browser/top_sites.h" |
21 #include "components/sync_sessions/open_tabs_ui_delegate.h" | 19 #include "components/sync_sessions/open_tabs_ui_delegate.h" |
22 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
23 #include "ui/base/layout.h" | 21 #include "ui/base/layout.h" |
24 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
25 #include "ui/base/webui/web_ui_util.h" | 23 #include "ui/base/webui/web_ui_util.h" |
24 #include "ui/gfx/geometry/safe_integer_conversions.h" | |
26 #include "ui/resources/grit/ui_resources.h" | 25 #include "ui/resources/grit/ui_resources.h" |
27 | 26 |
28 FaviconSource::IconRequest::IconRequest() | 27 FaviconSource::IconRequest::IconRequest() |
29 : size_in_dip(gfx::kFaviconSize), device_scale_factor(1.0f) { | 28 : size_in_dip(gfx::kFaviconSize), device_scale_factor(1.0f) { |
30 } | 29 } |
31 | 30 |
32 FaviconSource::IconRequest::IconRequest( | 31 FaviconSource::IconRequest::IconRequest( |
33 const content::URLDataSource::GotDataCallback& cb, | 32 const content::URLDataSource::GotDataCallback& cb, |
34 const GURL& path, | 33 const GURL& path, |
35 int size, | 34 int size, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 | 73 |
75 chrome::ParsedFaviconPath parsed; | 74 chrome::ParsedFaviconPath parsed; |
76 bool success = chrome::ParseFaviconPath(path, icon_types_, &parsed); | 75 bool success = chrome::ParseFaviconPath(path, icon_types_, &parsed); |
77 if (!success) { | 76 if (!success) { |
78 SendDefaultResponse(callback); | 77 SendDefaultResponse(callback); |
79 return; | 78 return; |
80 } | 79 } |
81 | 80 |
82 GURL url(parsed.url); | 81 GURL url(parsed.url); |
83 int desired_size_in_pixel = | 82 int desired_size_in_pixel = |
84 std::ceil(parsed.size_in_dip * parsed.device_scale_factor); | 83 gfx::ToCeiledInt(parsed.size_in_dip * parsed.device_scale_factor); |
85 | 84 |
86 if (parsed.is_icon_url) { | 85 if (parsed.is_icon_url) { |
87 // TODO(michaelbai): Change GetRawFavicon to support combination of | 86 // TODO(michaelbai): Change GetRawFavicon to support combination of |
88 // IconType. | 87 // IconType. |
89 favicon_service->GetRawFavicon( | 88 favicon_service->GetRawFavicon( |
90 url, | 89 url, |
91 favicon_base::FAVICON, | 90 favicon_base::FAVICON, |
92 desired_size_in_pixel, | 91 desired_size_in_pixel, |
93 base::Bind( | 92 base::Bind( |
94 &FaviconSource::OnFaviconDataAvailable, | 93 &FaviconSource::OnFaviconDataAvailable, |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 SendDefaultResponse(request); | 170 SendDefaultResponse(request); |
172 } | 171 } |
173 } | 172 } |
174 | 173 |
175 void FaviconSource::SendDefaultResponse( | 174 void FaviconSource::SendDefaultResponse( |
176 const content::URLDataSource::GotDataCallback& callback) { | 175 const content::URLDataSource::GotDataCallback& callback) { |
177 SendDefaultResponse(IconRequest(callback, GURL(), 16, 1.0f)); | 176 SendDefaultResponse(IconRequest(callback, GURL(), 16, 1.0f)); |
178 } | 177 } |
179 | 178 |
180 void FaviconSource::SendDefaultResponse(const IconRequest& icon_request) { | 179 void FaviconSource::SendDefaultResponse(const IconRequest& icon_request) { |
181 int favicon_index; | 180 int favicon_size; |
182 int resource_id; | 181 int resource_id; |
183 switch (icon_request.size_in_dip) { | 182 ui::ScaleFactor resource_scale_factor; |
184 case 64: | 183 |
185 favicon_index = SIZE_64; | 184 int desired_size_in_pixel = gfx::ToCeiledInt( |
185 icon_request.size_in_dip * icon_request.device_scale_factor); | |
186 | |
187 // Any desired size, which doesn't exist, will use the 16x16 default favicon | |
188 // to scale to that pixel size. | |
189 switch (desired_size_in_pixel) { | |
190 case SIZE_64: | |
186 resource_id = IDR_DEFAULT_FAVICON_64; | 191 resource_id = IDR_DEFAULT_FAVICON_64; |
192 favicon_size = SIZE_64; | |
Peter Kasting
2016/11/28 07:57:53
It seems like the only point of this variable is t
minggang
2016/11/28 08:40:47
Personally, I think that using enum is more meanin
Peter Kasting
2016/11/28 09:11:55
I don't understand what you're saying. Are you ac
minggang
2016/11/28 09:56:19
Considering to eliminate the unnecessary variable
| |
187 break; | 193 break; |
188 case 32: | 194 case SIZE_32: |
189 favicon_index = SIZE_32; | |
190 resource_id = IDR_DEFAULT_FAVICON_32; | 195 resource_id = IDR_DEFAULT_FAVICON_32; |
196 favicon_size = SIZE_32; | |
197 break; | |
198 case SIZE_16: | |
199 resource_id = IDR_DEFAULT_FAVICON; | |
200 favicon_size = SIZE_16; | |
191 break; | 201 break; |
192 default: | 202 default: |
193 favicon_index = SIZE_16; | |
194 resource_id = IDR_DEFAULT_FAVICON; | 203 resource_id = IDR_DEFAULT_FAVICON; |
204 favicon_size = SIZE_NOT_EXISTS; | |
195 break; | 205 break; |
196 } | 206 } |
207 | |
208 resource_scale_factor = | |
209 SIZE_NOT_EXISTS == favicon_size | |
210 ? ui::GetSupportedScaleFactor(icon_request.device_scale_factor) | |
211 : ui::SCALE_FACTOR_NONE; | |
212 | |
197 base::RefCountedMemory* default_favicon = | 213 base::RefCountedMemory* default_favicon = |
198 default_favicons_[favicon_index].get(); | 214 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( |
oshima
2016/11/30 18:04:38
I believe the problem is that cache doesn't take t
| |
199 | 215 resource_id, resource_scale_factor); |
200 if (!default_favicon) { | |
201 ui::ScaleFactor resource_scale_factor = | |
202 ui::GetSupportedScaleFactor(icon_request.device_scale_factor); | |
203 default_favicon = | |
204 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( | |
205 resource_id, resource_scale_factor); | |
206 default_favicons_[favicon_index] = default_favicon; | |
207 } | |
208 | 216 |
209 icon_request.callback.Run(default_favicon); | 217 icon_request.callback.Run(default_favicon); |
210 } | 218 } |
OLD | NEW |