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/favicon/favicon_service.h" | 5 #include "chrome/browser/favicon/favicon_service.h" |
6 | 6 |
| 7 #include "chrome/browser/favicon/select_favicon_frames.h" |
7 #include "chrome/browser/history/history.h" | 8 #include "chrome/browser/history/history.h" |
8 #include "chrome/browser/history/history_backend.h" | 9 #include "chrome/browser/history/history_backend.h" |
9 #include "chrome/browser/history/history_service_factory.h" | 10 #include "chrome/browser/history/history_service_factory.h" |
10 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" | 11 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" |
11 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 #include "ui/gfx/codec/png_codec.h" |
| 15 #include "ui/gfx/image/image_skia.h" |
12 | 16 |
13 FaviconService::FaviconService(HistoryService* history_service) | 17 FaviconService::FaviconService(HistoryService* history_service) |
14 : history_service_(history_service) { | 18 : history_service_(history_service) { |
15 } | 19 } |
16 | 20 |
| 21 FaviconService::Handle FaviconService::GetFaviconImage( |
| 22 const GURL& icon_url, |
| 23 history::IconType icon_type, |
| 24 int desired_size_in_dip, |
| 25 CancelableRequestConsumerBase* consumer, |
| 26 const FaviconImageCallback& callback) { |
| 27 GetFaviconRequest* request = new GetFaviconRequest(base::Bind( |
| 28 &FaviconService::GetFaviconImageCallback, |
| 29 base::Unretained(this), |
| 30 desired_size_in_dip, |
| 31 callback)); |
| 32 AddRequest(request, consumer); |
| 33 // TODO(pkotwicz): Pass in desired size and scale factors. |
| 34 if (history_service_) |
| 35 history_service_->GetFavicon(request, icon_url, icon_type); |
| 36 else |
| 37 ForwardEmptyResultAsync(request); |
| 38 return request->handle(); |
| 39 } |
| 40 |
| 41 FaviconService::Handle FaviconService::GetRawFavicon( |
| 42 const GURL& icon_url, |
| 43 history::IconType icon_type, |
| 44 int desired_size_in_dip, |
| 45 ui::ScaleFactor desired_scale_factor, |
| 46 CancelableRequestConsumerBase* consumer, |
| 47 const FaviconRawCallback& callback) { |
| 48 GetFaviconRequest* request = new GetFaviconRequest(base::Bind( |
| 49 &FaviconService::GetRawFaviconCallback, |
| 50 base::Unretained(this), |
| 51 desired_size_in_dip, |
| 52 desired_scale_factor, |
| 53 callback)); |
| 54 AddRequest(request, consumer); |
| 55 // TODO(pkotwicz): Pass in desired size and scale factor. |
| 56 if (history_service_) |
| 57 history_service_->GetFavicon(request, icon_url, icon_type); |
| 58 else |
| 59 ForwardEmptyResultAsync(request); |
| 60 return request->handle(); |
| 61 } |
| 62 |
17 FaviconService::Handle FaviconService::GetFavicon( | 63 FaviconService::Handle FaviconService::GetFavicon( |
18 const GURL& icon_url, | 64 const GURL& icon_url, |
19 history::IconType icon_type, | 65 history::IconType icon_type, |
| 66 int desired_size_in_dip, |
| 67 std::vector<ui::ScaleFactor> desired_scale_factors, |
20 CancelableRequestConsumerBase* consumer, | 68 CancelableRequestConsumerBase* consumer, |
21 const FaviconDataCallback& callback) { | 69 const FaviconResultsCallback& callback) { |
22 GetFaviconRequest* request = new GetFaviconRequest(callback); | 70 GetFaviconRequest* request = new GetFaviconRequest(callback); |
23 AddRequest(request, consumer); | 71 AddRequest(request, consumer); |
24 if (history_service_) | 72 if (history_service_) |
25 history_service_->GetFavicon(request, icon_url, icon_type); | 73 history_service_->GetFavicon(request, icon_url, icon_type); |
26 else | 74 else |
27 ForwardEmptyResultAsync(request); | 75 ForwardEmptyResultAsync(request); |
28 return request->handle(); | 76 return request->handle(); |
29 } | 77 } |
30 | 78 |
31 FaviconService::Handle FaviconService::UpdateFaviconMappingAndFetch( | 79 FaviconService::Handle FaviconService::UpdateFaviconMappingAndFetch( |
32 const GURL& page_url, | 80 const GURL& page_url, |
33 const GURL& icon_url, | 81 const GURL& icon_url, |
34 history::IconType icon_type, | 82 history::IconType icon_type, |
35 CancelableRequestConsumerBase* consumer, | 83 CancelableRequestConsumerBase* consumer, |
36 const FaviconDataCallback& callback) { | 84 const FaviconResultsCallback& callback) { |
37 GetFaviconRequest* request = new GetFaviconRequest(callback); | 85 GetFaviconRequest* request = new GetFaviconRequest(callback); |
38 AddRequest(request, consumer); | 86 AddRequest(request, consumer); |
39 if (history_service_) | 87 if (history_service_) |
40 history_service_->UpdateFaviconMappingAndFetch(request, page_url, | 88 history_service_->UpdateFaviconMappingAndFetch(request, page_url, |
41 icon_url, icon_type); | 89 icon_url, icon_type); |
42 else | 90 else |
43 ForwardEmptyResultAsync(request); | 91 ForwardEmptyResultAsync(request); |
44 return request->handle(); | 92 return request->handle(); |
45 } | 93 } |
46 | 94 |
| 95 FaviconService::Handle FaviconService::GetFaviconImageForURL( |
| 96 Profile* profile, |
| 97 const GURL& page_url, |
| 98 int icon_types, |
| 99 int desired_size_in_dip, |
| 100 CancelableRequestConsumerBase* consumer, |
| 101 const FaviconImageCallback& callback) { |
| 102 GetFaviconRequest* request = new GetFaviconRequest(base::Bind( |
| 103 &FaviconService::GetFaviconImageCallback, |
| 104 base::Unretained(this), |
| 105 desired_size_in_dip, |
| 106 callback)); |
| 107 |
| 108 std::vector<ui::ScaleFactor> desired_scale_factors = |
| 109 ui::GetSupportedScaleFactors(); |
| 110 return GetFaviconForURLImpl(profile, page_url, icon_types, |
| 111 desired_size_in_dip, desired_scale_factors, consumer, request); |
| 112 } |
| 113 |
| 114 FaviconService::Handle FaviconService::GetRawFaviconForURL( |
| 115 Profile* profile, |
| 116 const GURL& page_url, |
| 117 int icon_types, |
| 118 int desired_size_in_dip, |
| 119 ui::ScaleFactor desired_scale_factor, |
| 120 CancelableRequestConsumerBase* consumer, |
| 121 const FaviconRawCallback& callback) { |
| 122 GetFaviconRequest* request = new GetFaviconRequest(base::Bind( |
| 123 &FaviconService::GetRawFaviconCallback, |
| 124 base::Unretained(this), |
| 125 desired_size_in_dip, |
| 126 desired_scale_factor, |
| 127 callback)); |
| 128 |
| 129 std::vector<ui::ScaleFactor> desired_scale_factors; |
| 130 desired_scale_factors.push_back(desired_scale_factor); |
| 131 return GetFaviconForURLImpl(profile, page_url, icon_types, |
| 132 desired_size_in_dip, desired_scale_factors, consumer, request); |
| 133 } |
| 134 |
47 FaviconService::Handle FaviconService::GetFaviconForURL( | 135 FaviconService::Handle FaviconService::GetFaviconForURL( |
48 Profile* profile, | 136 Profile* profile, |
49 const GURL& page_url, | 137 const GURL& page_url, |
50 int icon_types, | 138 int icon_types, |
| 139 int desired_size_in_dip, |
| 140 std::vector<ui::ScaleFactor> desired_scale_factors, |
51 CancelableRequestConsumerBase* consumer, | 141 CancelableRequestConsumerBase* consumer, |
52 const FaviconDataCallback& callback) { | 142 const FaviconResultsCallback& callback) { |
53 GetFaviconRequest* request = new GetFaviconRequest(callback); | 143 GetFaviconRequest* request = new GetFaviconRequest(callback); |
54 AddRequest(request, consumer); | 144 return GetFaviconForURLImpl(profile, page_url, icon_types, |
55 FaviconService::Handle handle = request->handle(); | 145 desired_size_in_dip, desired_scale_factors, consumer, request); |
56 if (page_url.SchemeIs(chrome::kChromeUIScheme) || | |
57 page_url.SchemeIs(chrome::kExtensionScheme)) { | |
58 ChromeWebUIControllerFactory::GetInstance()->GetFaviconForURL( | |
59 profile, request, page_url); | |
60 } else { | |
61 if (history_service_) | |
62 history_service_->GetFaviconForURL(request, page_url, icon_types); | |
63 else | |
64 ForwardEmptyResultAsync(request); | |
65 } | |
66 return handle; | |
67 } | 146 } |
68 | 147 |
69 // Requests the favicon for |favicon_id|. The |consumer| is notified when the | |
70 // bits have been fetched. | |
71 FaviconService::Handle FaviconService::GetFaviconForID( | |
72 history::FaviconID favicon_id, | |
73 CancelableRequestConsumerBase* consumer, | |
74 const FaviconDataCallback& callback) { | |
75 GetFaviconRequest* request = new GetFaviconRequest(callback); | |
76 AddRequest(request, consumer); | |
77 FaviconService::Handle handle = request->handle(); | |
78 if (history_service_) | |
79 history_service_->GetFaviconForID(request, favicon_id); | |
80 else | |
81 ForwardEmptyResultAsync(request); | |
82 | |
83 return handle; | |
84 } | |
85 | |
86 | |
87 void FaviconService::SetFaviconOutOfDateForPage(const GURL& page_url) { | 148 void FaviconService::SetFaviconOutOfDateForPage(const GURL& page_url) { |
88 if (history_service_) | 149 if (history_service_) |
89 history_service_->SetFaviconOutOfDateForPage(page_url); | 150 history_service_->SetFaviconOutOfDateForPage(page_url); |
90 } | 151 } |
91 | 152 |
92 void FaviconService::CloneFavicon(const GURL& old_page_url, | 153 void FaviconService::CloneFavicon(const GURL& old_page_url, |
93 const GURL& new_page_url) { | 154 const GURL& new_page_url) { |
94 if (history_service_) | 155 if (history_service_) |
95 history_service_->CloneFavicon(old_page_url, new_page_url); | 156 history_service_->CloneFavicon(old_page_url, new_page_url); |
96 } | 157 } |
97 | 158 |
98 void FaviconService::SetImportedFavicons( | 159 void FaviconService::SetImportedFavicons( |
99 const std::vector<history::ImportedFaviconUsage>& favicon_usage) { | 160 const std::vector<history::ImportedFaviconUsage>& favicon_usage) { |
100 if (history_service_) | 161 if (history_service_) |
101 history_service_->SetImportedFavicons(favicon_usage); | 162 history_service_->SetImportedFavicons(favicon_usage); |
102 } | 163 } |
103 | 164 |
104 void FaviconService::SetFavicon(const GURL& page_url, | 165 void FaviconService::SetFavicon(const GURL& page_url, |
105 const GURL& icon_url, | 166 const GURL& icon_url, |
106 const std::vector<unsigned char>& image_data, | 167 const std::vector<unsigned char>& image_data, |
107 history::IconType icon_type) { | 168 history::IconType icon_type) { |
108 if (history_service_) | 169 if (history_service_) |
109 history_service_->SetFavicon(page_url, icon_url, image_data, icon_type); | 170 history_service_->SetFavicon(page_url, icon_url, image_data, icon_type); |
110 } | 171 } |
111 | 172 |
112 FaviconService::~FaviconService() { | 173 FaviconService::~FaviconService() { |
113 } | 174 } |
114 | 175 |
| 176 FaviconService::Handle FaviconService::GetFaviconForURLImpl( |
| 177 Profile* profile, |
| 178 const GURL& page_url, |
| 179 int icon_types, |
| 180 int desired_size_in_dip, |
| 181 std::vector<ui::ScaleFactor> desired_scale_factors, |
| 182 CancelableRequestConsumerBase* consumer, |
| 183 GetFaviconRequest* request) { |
| 184 AddRequest(request, consumer); |
| 185 FaviconService::Handle handle = request->handle(); |
| 186 if (page_url.SchemeIs(chrome::kChromeUIScheme) || |
| 187 page_url.SchemeIs(chrome::kExtensionScheme)) { |
| 188 // TODO(pkotwicz): Pass in desired size and desired scale factors. |
| 189 ChromeWebUIControllerFactory::GetInstance()->GetFaviconForURL( |
| 190 profile, request, page_url); |
| 191 } else { |
| 192 // TODO(pkotwicz): Pass in desired size and desired scale factors. |
| 193 if (history_service_) |
| 194 history_service_->GetFaviconForURL(request, page_url, icon_types); |
| 195 else |
| 196 ForwardEmptyResultAsync(request); |
| 197 } |
| 198 return handle; |
| 199 } |
| 200 |
| 201 void FaviconService::GetFaviconImageCallback( |
| 202 int desired_size_in_dip, |
| 203 FaviconImageCallback callback, |
| 204 Handle handle, |
| 205 std::vector<history::FaviconBitmapResult> favicon_bitmap_results, |
| 206 history::IconURLSizesMap icon_url_sizes_map) { |
| 207 std::vector<SkBitmap> sk_bitmaps; |
| 208 for (size_t i = 0; i < favicon_bitmap_results.size(); ++i) { |
| 209 if (favicon_bitmap_results[i].is_valid()) { |
| 210 scoped_refptr<base::RefCountedMemory> bitmap_data = |
| 211 favicon_bitmap_results[i].bitmap_data; |
| 212 SkBitmap out_bitmap; |
| 213 if (gfx::PNGCodec::Decode(bitmap_data->front(), bitmap_data->size(), |
| 214 &out_bitmap)) { |
| 215 sk_bitmaps.push_back(out_bitmap); |
| 216 } |
| 217 } |
| 218 } |
| 219 history::FaviconImageResult image_result; |
| 220 image_result.image = gfx::Image(SelectFaviconFrames( |
| 221 sk_bitmaps, ui::GetSupportedScaleFactors(), desired_size_in_dip, NULL)); |
| 222 image_result.icon_url = favicon_bitmap_results.empty() ? |
| 223 GURL() : favicon_bitmap_results[0].icon_url; |
| 224 |
| 225 callback.Run(handle, image_result); |
| 226 } |
| 227 |
| 228 void FaviconService::GetRawFaviconCallback( |
| 229 int desired_size_in_dip, |
| 230 ui::ScaleFactor desired_scale_factor, |
| 231 FaviconRawCallback callback, |
| 232 Handle handle, |
| 233 std::vector<history::FaviconBitmapResult> favicon_bitmap_results, |
| 234 history::IconURLSizesMap icon_url_sizes_map) { |
| 235 if (favicon_bitmap_results.empty() || !favicon_bitmap_results[0].is_valid()) { |
| 236 callback.Run(handle, history::FaviconBitmapResult()); |
| 237 return; |
| 238 } |
| 239 |
| 240 DCHECK_EQ(1u, favicon_bitmap_results.size()); |
| 241 history::FaviconBitmapResult bitmap_result = favicon_bitmap_results[0]; |
| 242 |
| 243 // If history bitmap is already desired pixel size, return early. |
| 244 float desired_scale = ui::GetScaleFactorScale(desired_scale_factor); |
| 245 int desired_edge_width_in_pixel = static_cast<int>( |
| 246 desired_size_in_dip * desired_scale + 0.5f); |
| 247 gfx::Size desired_size_in_pixel(desired_edge_width_in_pixel, |
| 248 desired_edge_width_in_pixel); |
| 249 if (bitmap_result.pixel_size == desired_size_in_pixel) { |
| 250 callback.Run(handle, bitmap_result); |
| 251 return; |
| 252 } |
| 253 |
| 254 // Convert raw bytes to SkBitmap, resize via SelectFaviconFrames(), then |
| 255 // convert back. |
| 256 SkBitmap bitmap; |
| 257 if (!gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), |
| 258 bitmap_result.bitmap_data->size(), |
| 259 &bitmap)) { |
| 260 callback.Run(handle, history::FaviconBitmapResult()); |
| 261 return; |
| 262 } |
| 263 |
| 264 std::vector<SkBitmap> bitmaps; |
| 265 bitmaps.push_back(bitmap); |
| 266 std::vector<ui::ScaleFactor> desired_scale_factors; |
| 267 desired_scale_factors.push_back(desired_scale_factor); |
| 268 gfx::ImageSkia resized_image = SelectFaviconFrames(bitmaps, |
| 269 desired_scale_factors, desired_size_in_dip, NULL); |
| 270 |
| 271 std::vector<unsigned char> resized_bitmap_data; |
| 272 if (!gfx::PNGCodec::EncodeBGRASkBitmap(*resized_image.bitmap(), false, |
| 273 &resized_bitmap_data)) { |
| 274 callback.Run(handle, history::FaviconBitmapResult()); |
| 275 return; |
| 276 } |
| 277 |
| 278 bitmap_result.bitmap_data = base::RefCountedBytes::TakeVector( |
| 279 &resized_bitmap_data); |
| 280 callback.Run(handle, bitmap_result); |
| 281 } |
| 282 |
115 void FaviconService::ForwardEmptyResultAsync(GetFaviconRequest* request) { | 283 void FaviconService::ForwardEmptyResultAsync(GetFaviconRequest* request) { |
116 request->ForwardResultAsync(request->handle(), history::FaviconData()); | 284 request->ForwardResultAsync(request->handle(), |
| 285 std::vector<history::FaviconBitmapResult>(), |
| 286 history::IconURLSizesMap()); |
117 } | 287 } |
OLD | NEW |