1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/memory/ref_counted_memory.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
#include "chrome/common/url_constants.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/image_loader.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_resource.h"
#include "extensions/common/manifest_handlers/icons_handler.h"
#include "extensions/grit/extensions_browser_resources.h"
#include "grit/component_extension_resources_map.h"
#include "skia/ext/image_operations.h"
#include "ui/base/layout.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/favicon_size.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/skbitmap_operations.h"
#include "url/gurl.h"
namespace extensions {
namespace {
scoped_refptr<base::RefCountedMemory> BitmapToMemory(const SkBitmap* image) {
base::RefCountedBytes* image_bytes = new base::RefCountedBytes;
gfx::PNGCodec::EncodeBGRASkBitmap(*image, false, &image_bytes->data());
return image_bytes;
}
SkBitmap DesaturateImage(const SkBitmap* image) {
color_utils::HSL shift = {-1, 0, 0.6};
return SkBitmapOperations::CreateHSLShiftedBitmap(*image, shift);
}
SkBitmap* ToBitmap(const unsigned char* data, size_t size) {
SkBitmap* decoded = new SkBitmap();
bool success = gfx::PNGCodec::Decode(data, size, decoded);
DCHECK(success);
return decoded;
}
} // namespace
ExtensionIconSource::ExtensionIconSource(Profile* profile) : profile_(profile) {
}
struct ExtensionIconSource::ExtensionIconRequest {
content::URLDataSource::GotDataCallback callback;
scoped_refptr<const Extension> extension;
bool grayscale;
int size;
ExtensionIconSet::MatchType match;
};
// static
GURL ExtensionIconSource::GetIconURL(const Extension* extension,
int icon_size,
ExtensionIconSet::MatchType match,
bool grayscale,
bool* exists) {
if (exists) {
*exists =
IconsInfo::GetIconURL(extension, icon_size, match) != GURL::EmptyGURL();
}
GURL icon_url(base::StringPrintf("%s%s/%d/%d%s",
chrome::kChromeUIExtensionIconURL,
extension->id().c_str(),
icon_size,
match,
grayscale ? "?grayscale=true" : ""));
CHECK(icon_url.is_valid());
return icon_url;
}
// static
SkBitmap* ExtensionIconSource::LoadImageByResourceId(int resource_id) {
std::string contents = ResourceBundle::GetSharedInstance()
.GetRawDataResourceForScale(resource_id,
ui::SCALE_FACTOR_100P).as_string();
// Convert and return it.
const unsigned char* data =
reinterpret_cast<const unsigned char*>(contents.data());
return ToBitmap(data, contents.length());
}
std::string ExtensionIconSource::GetSource() const {
return chrome::kChromeUIExtensionIconHost;
}
std::string ExtensionIconSource::GetMimeType(const std::string&) const {
// We need to explicitly return a mime type, otherwise if the user tries to
// drag the image they get no extension.
return "image/png";
}
void ExtensionIconSource::StartDataRequest(
const std::string& path,
int render_process_id,
int render_frame_id,
const content::URLDataSource::GotDataCallback& callback) {
// This is where everything gets started. First, parse the request and make
// the request data available for later.
static int next_id = 0;
if (!ParseData(path, ++next_id, callback)) {
// If the request data cannot be parsed, request parameters will not be
// added to |request_map_|.
// Send back the default application icon (not resized or desaturated) as
// the default response.
callback.Run(BitmapToMemory(GetDefaultAppImage()).get());
return;
}
ExtensionIconRequest* request = GetData(next_id);
ExtensionResource icon = IconsInfo::GetIconResource(
request->extension.get(), request->size, request->match);
if (icon.relative_path().empty()) {
LoadIconFailed(next_id);
} else {
LoadExtensionImage(icon, next_id);
}
}
ExtensionIconSource::~ExtensionIconSource() {
// Clean up all the temporary data we're holding for requests.
STLDeleteValues(&request_map_);
}
const SkBitmap* ExtensionIconSource::GetDefaultAppImage() {
if (!default_app_data_.get())
default_app_data_.reset(LoadImageByResourceId(IDR_APP_DEFAULT_ICON));
return default_app_data_.get();
}
const SkBitmap* ExtensionIconSource::GetDefaultExtensionImage() {
if (!default_extension_data_.get()) {
default_extension_data_.reset(
LoadImageByResourceId(IDR_EXTENSION_DEFAULT_ICON));
}
return default_extension_data_.get();
}
void ExtensionIconSource::FinalizeImage(const SkBitmap* image,
int request_id) {
SkBitmap bitmap;
ExtensionIconRequest* request = GetData(request_id);
if (request->grayscale)
bitmap = DesaturateImage(image);
else
bitmap = *image;
request->callback.Run(BitmapToMemory(&bitmap).get());
ClearData(request_id);
}
void ExtensionIconSource::LoadDefaultImage(int request_id) {
ExtensionIconRequest* request = GetData(request_id);
const SkBitmap* default_image = NULL;
if (request->extension->is_app())
default_image = GetDefaultAppImage();
else
default_image = GetDefaultExtensionImage();
SkBitmap resized_image(skia::ImageOperations::Resize(
*default_image, skia::ImageOperations::RESIZE_LANCZOS3,
request->size, request->size));
// There are cases where Resize returns an empty bitmap, for example if you
// ask for an image too large. In this case it is better to return the default
// image than returning nothing at all.
if (resized_image.empty())
resized_image = *default_image;
FinalizeImage(&resized_image, request_id);
}
void ExtensionIconSource::LoadExtensionImage(const ExtensionResource& icon,
int request_id) {
ExtensionIconRequest* request = GetData(request_id);
ImageLoader::Get(profile_)->LoadImageAsync(
request->extension.get(),
icon,
gfx::Size(request->size, request->size),
base::Bind(&ExtensionIconSource::OnImageLoaded, AsWeakPtr(), request_id));
}
void ExtensionIconSource::LoadFaviconImage(int request_id) {
FaviconService* favicon_service =
FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
// Fall back to the default icons if the service isn't available.
if (favicon_service == NULL) {
LoadDefaultImage(request_id);
return;
}
GURL favicon_url =
AppLaunchInfo::GetFullLaunchURL(GetData(request_id)->extension.get());
favicon_service->GetRawFaviconForPageURL(
favicon_url,
favicon_base::FAVICON,
gfx::kFaviconSize,
base::Bind(&ExtensionIconSource::OnFaviconDataAvailable,
base::Unretained(this),
request_id),
&cancelable_task_tracker_);
}
void ExtensionIconSource::OnFaviconDataAvailable(
int request_id,
const favicon_base::FaviconRawBitmapResult& bitmap_result) {
ExtensionIconRequest* request = GetData(request_id);
// Fallback to the default icon if there wasn't a favicon.
if (!bitmap_result.is_valid()) {
LoadDefaultImage(request_id);
return;
}
if (!request->grayscale) {
// If we don't need a grayscale image, then we can bypass FinalizeImage
// to avoid unnecessary conversions.
request->callback.Run(bitmap_result.bitmap_data.get());
ClearData(request_id);
} else {
FinalizeImage(ToBitmap(bitmap_result.bitmap_data->front(),
bitmap_result.bitmap_data->size()), request_id);
}
}
void ExtensionIconSource::OnImageLoaded(int request_id,
const gfx::Image& image) {
if (image.IsEmpty())
LoadIconFailed(request_id);
else
FinalizeImage(image.ToSkBitmap(), request_id);
}
void ExtensionIconSource::LoadIconFailed(int request_id) {
ExtensionIconRequest* request = GetData(request_id);
ExtensionResource icon = IconsInfo::GetIconResource(
request->extension.get(), request->size, request->match);
if (request->size == extension_misc::EXTENSION_ICON_BITTY)
LoadFaviconImage(request_id);
else
LoadDefaultImage(request_id);
}
bool ExtensionIconSource::ParseData(
const std::string& path,
int request_id,
const content::URLDataSource::GotDataCallback& callback) {
// Extract the parameters from the path by lower casing and splitting.
std::string path_lower = base::StringToLowerASCII(path);
std::vector<std::string> path_parts;
base::SplitString(path_lower, '/', &path_parts);
if (path_lower.empty() || path_parts.size() < 3)
return false;
std::string size_param = path_parts.at(1);
std::string match_param = path_parts.at(2);
match_param = match_param.substr(0, match_param.find('?'));
int size;
if (!base::StringToInt(size_param, &size))
return false;
if (size <= 0 || size > extension_misc::EXTENSION_ICON_GIGANTOR)
return false;
ExtensionIconSet::MatchType match_type;
int match_num;
if (!base::StringToInt(match_param, &match_num))
return false;
match_type = static_cast<ExtensionIconSet::MatchType>(match_num);
if (!(match_type == ExtensionIconSet::MATCH_EXACTLY ||
match_type == ExtensionIconSet::MATCH_SMALLER ||
match_type == ExtensionIconSet::MATCH_BIGGER))
match_type = ExtensionIconSet::MATCH_EXACTLY;
std::string extension_id = path_parts.at(0);
const Extension* extension = ExtensionSystem::Get(profile_)->
extension_service()->GetInstalledExtension(extension_id);
if (!extension)
return false;
bool grayscale = path_lower.find("grayscale=true") != std::string::npos;
SetData(request_id, callback, extension, grayscale, size, match_type);
return true;
}
void ExtensionIconSource::SetData(
int request_id,
const content::URLDataSource::GotDataCallback& callback,
const Extension* extension,
bool grayscale,
int size,
ExtensionIconSet::MatchType match) {
ExtensionIconRequest* request = new ExtensionIconRequest();
request->callback = callback;
request->extension = extension;
request->grayscale = grayscale;
request->size = size;
request->match = match;
request_map_[request_id] = request;
}
ExtensionIconSource::ExtensionIconRequest* ExtensionIconSource::GetData(
int request_id) {
return request_map_[request_id];
}
void ExtensionIconSource::ClearData(int request_id) {
std::map<int, ExtensionIconRequest*>::iterator i =
request_map_.find(request_id);
if (i == request_map_.end())
return;
delete i->second;
request_map_.erase(i);
}
} // namespace extensions
|