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 |
27 namespace { | |
28 | |
29 struct DefaultFaviconResourceMap { | |
30 float scale; | |
31 int value; | |
32 }; | |
33 | |
34 const DefaultFaviconResourceMap kDefaultFaviconResources[] = { | |
35 {1.0f, IDR_DEFAULT_FAVICON}, | |
36 {2.0f, IDR_DEFAULT_FAVICON_32}, | |
37 {4.0f, IDR_DEFAULT_FAVICON_64}}; | |
38 | |
39 // Return the resource id of default favicon, whose scale is closet to | |
40 // |scale_factor|. | |
41 int FindBestMatchDefaultFaviconResourceIdForScaleFactor( | |
42 ui::ScaleFactor scale_factor) { | |
43 float scale = ui::GetScaleForScaleFactor(scale_factor); | |
44 float smallest_diff = std::numeric_limits<float>::max(); | |
45 int resource_id = IDR_DEFAULT_FAVICON; | |
46 | |
47 for (size_t i = 0; i < arraysize(kDefaultFaviconResources); ++i) { | |
48 float diff = std::abs(kDefaultFaviconResources[i].scale - scale); | |
49 if (diff < smallest_diff) { | |
50 resource_id = kDefaultFaviconResources[i].value; | |
51 smallest_diff = diff; | |
52 } | |
53 } | |
54 return resource_id; | |
55 } | |
56 | |
57 } // namespace | |
58 | |
28 FaviconSource::IconRequest::IconRequest() | 59 FaviconSource::IconRequest::IconRequest() |
29 : size_in_dip(gfx::kFaviconSize), device_scale_factor(1.0f) { | 60 : size_in_dip(gfx::kFaviconSize), device_scale_factor(1.0f) { |
30 } | 61 } |
31 | 62 |
32 FaviconSource::IconRequest::IconRequest( | 63 FaviconSource::IconRequest::IconRequest( |
33 const content::URLDataSource::GotDataCallback& cb, | 64 const content::URLDataSource::GotDataCallback& cb, |
34 const GURL& path, | 65 const GURL& path, |
35 int size, | 66 int size, |
36 float scale) | 67 float scale) |
37 : callback(cb), | 68 : callback(cb), |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 | 105 |
75 chrome::ParsedFaviconPath parsed; | 106 chrome::ParsedFaviconPath parsed; |
76 bool success = chrome::ParseFaviconPath(path, icon_types_, &parsed); | 107 bool success = chrome::ParseFaviconPath(path, icon_types_, &parsed); |
77 if (!success) { | 108 if (!success) { |
78 SendDefaultResponse(callback); | 109 SendDefaultResponse(callback); |
79 return; | 110 return; |
80 } | 111 } |
81 | 112 |
82 GURL url(parsed.url); | 113 GURL url(parsed.url); |
83 int desired_size_in_pixel = | 114 int desired_size_in_pixel = |
84 std::ceil(parsed.size_in_dip * parsed.device_scale_factor); | 115 gfx::ToCeiledInt(parsed.size_in_dip * parsed.device_scale_factor); |
85 | 116 |
86 if (parsed.is_icon_url) { | 117 if (parsed.is_icon_url) { |
87 // TODO(michaelbai): Change GetRawFavicon to support combination of | 118 // TODO(michaelbai): Change GetRawFavicon to support combination of |
88 // IconType. | 119 // IconType. |
89 favicon_service->GetRawFavicon( | 120 favicon_service->GetRawFavicon( |
90 url, | 121 url, |
91 favicon_base::FAVICON, | 122 favicon_base::FAVICON, |
92 desired_size_in_pixel, | 123 desired_size_in_pixel, |
93 base::Bind( | 124 base::Bind( |
94 &FaviconSource::OnFaviconDataAvailable, | 125 &FaviconSource::OnFaviconDataAvailable, |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 SendDefaultResponse(request); | 202 SendDefaultResponse(request); |
172 } | 203 } |
173 } | 204 } |
174 | 205 |
175 void FaviconSource::SendDefaultResponse( | 206 void FaviconSource::SendDefaultResponse( |
176 const content::URLDataSource::GotDataCallback& callback) { | 207 const content::URLDataSource::GotDataCallback& callback) { |
177 SendDefaultResponse(IconRequest(callback, GURL(), 16, 1.0f)); | 208 SendDefaultResponse(IconRequest(callback, GURL(), 16, 1.0f)); |
178 } | 209 } |
179 | 210 |
180 void FaviconSource::SendDefaultResponse(const IconRequest& icon_request) { | 211 void FaviconSource::SendDefaultResponse(const IconRequest& icon_request) { |
181 int favicon_index; | |
182 int resource_id; | 212 int resource_id; |
183 switch (icon_request.size_in_dip) { | 213 ui::ScaleFactor resource_scale_factor = ui::SCALE_FACTOR_NONE; |
214 | |
215 int desired_size_in_pixel = gfx::ToCeiledInt( | |
216 icon_request.size_in_dip * icon_request.device_scale_factor); | |
217 | |
218 switch (desired_size_in_pixel) { | |
184 case 64: | 219 case 64: |
185 favicon_index = SIZE_64; | |
186 resource_id = IDR_DEFAULT_FAVICON_64; | 220 resource_id = IDR_DEFAULT_FAVICON_64; |
187 break; | 221 break; |
188 case 32: | 222 case 32: |
189 favicon_index = SIZE_32; | |
190 resource_id = IDR_DEFAULT_FAVICON_32; | 223 resource_id = IDR_DEFAULT_FAVICON_32; |
191 break; | 224 break; |
192 default: | 225 case 16: |
193 favicon_index = SIZE_16; | |
194 resource_id = IDR_DEFAULT_FAVICON; | 226 resource_id = IDR_DEFAULT_FAVICON; |
195 break; | 227 break; |
228 default: | |
229 resource_scale_factor = | |
230 ui::GetSupportedScaleFactor(icon_request.device_scale_factor); | |
231 resource_id = FindBestMatchDefaultFaviconResourceIdForScaleFactor( | |
232 resource_scale_factor); | |
233 break; | |
196 } | 234 } |
235 | |
197 base::RefCountedMemory* default_favicon = | 236 base::RefCountedMemory* default_favicon = |
198 default_favicons_[favicon_index].get(); | 237 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( |
Peter Kasting
2016/12/01 07:17:46
oshima suggested that just removing this cache mig
minggang
2016/12/01 08:40:24
By removing the cache, we can fix this bug apparen
| |
199 | 238 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 | 239 |
209 icon_request.callback.Run(default_favicon); | 240 icon_request.callback.Run(default_favicon); |
210 } | 241 } |
OLD | NEW |