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
351
352
353
354
355
356
357
358
359
360
361
|
// 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/extensions/image_loading_tracker.h"
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/file_util.h"
#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_resource.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "grit/component_extension_resources_map.h"
#include "grit/theme_resources.h"
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "webkit/glue/image_decoder.h"
using content::BrowserThread;
using extensions::Extension;
////////////////////////////////////////////////////////////////////////////////
// ImageLoadingTracker::Observer
ImageLoadingTracker::Observer::~Observer() {}
////////////////////////////////////////////////////////////////////////////////
// ImageLoadingTracker::ImageInfo
ImageLoadingTracker::ImageInfo::ImageInfo(
const ExtensionResource& resource, gfx::Size max_size)
: resource(resource), max_size(max_size) {
}
ImageLoadingTracker::ImageInfo::~ImageInfo() {
}
////////////////////////////////////////////////////////////////////////////////
// ImageLoadingTracker::PendingLoadInfo
ImageLoadingTracker::PendingLoadInfo::PendingLoadInfo()
: extension(NULL),
cache(CACHE),
pending_count(0) {
}
ImageLoadingTracker::PendingLoadInfo::~PendingLoadInfo() {}
////////////////////////////////////////////////////////////////////////////////
// ImageLoadingTracker::ImageLoader
// A RefCounted class for loading images on the File thread and reporting back
// on the UI thread.
class ImageLoadingTracker::ImageLoader
: public base::RefCountedThreadSafe<ImageLoader> {
public:
explicit ImageLoader(ImageLoadingTracker* tracker)
: tracker_(tracker) {
CHECK(BrowserThread::GetCurrentThreadIdentifier(&callback_thread_id_));
DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE));
}
// Lets this class know that the tracker is no longer interested in the
// results.
void StopTracking() {
tracker_ = NULL;
}
// Instructs the loader to load a task on the File thread.
void LoadImage(const ExtensionResource& resource,
const gfx::Size& max_size,
int id) {
DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE));
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(&ImageLoader::LoadOnFileThread, this, resource,
max_size, id));
}
void LoadOnFileThread(const ExtensionResource& resource,
const gfx::Size& max_size,
int id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
// Read the file from disk.
std::string file_contents;
FilePath path = resource.GetFilePath();
if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) {
ReportBack(NULL, resource, gfx::Size(), id);
return;
}
// Decode the image using WebKit's image decoder.
const unsigned char* data =
reinterpret_cast<const unsigned char*>(file_contents.data());
webkit_glue::ImageDecoder decoder;
scoped_ptr<SkBitmap> decoded(new SkBitmap());
// Note: This class only decodes images from extension resources. Chrome
// doesn't (for security reasons) directly load extension resources provided
// by the extension author, but instead decodes them in a separate
// locked-down utility process. Only if the decoding succeeds is the image
// saved from memory to disk and subsequently used in the Chrome UI.
// Chrome is therefore decoding images here that were generated by Chrome.
*decoded = decoder.Decode(data, file_contents.length());
if (decoded->empty()) {
ReportBack(NULL, resource, gfx::Size(), id);
return; // Unable to decode.
}
gfx::Size original_size(decoded->width(), decoded->height());
if (decoded->width() > max_size.width() ||
decoded->height() > max_size.height()) {
// The bitmap is too big, re-sample.
*decoded = skia::ImageOperations::Resize(
*decoded, skia::ImageOperations::RESIZE_LANCZOS3,
max_size.width(), max_size.height());
}
ReportBack(decoded.release(), resource, original_size, id);
}
// Instructs the loader to load a resource on the File thread.
void LoadResource(const ExtensionResource& resource,
const gfx::Size& max_size,
int id,
int resource_id) {
DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE));
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(&ImageLoader::LoadResourceOnFileThread, this, resource,
max_size, id, resource_id));
}
void LoadResourceOnFileThread(const ExtensionResource& resource,
const gfx::Size& max_size,
int id,
int resource_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
SkBitmap* image = ExtensionIconSource::LoadImageByResourceId(
resource_id);
ReportBack(image, resource, max_size, id);
}
void ReportBack(SkBitmap* image, const ExtensionResource& resource,
const gfx::Size& original_size, int id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
BrowserThread::PostTask(
callback_thread_id_, FROM_HERE,
base::Bind(&ImageLoader::ReportOnUIThread, this,
image, resource, original_size, id));
}
void ReportOnUIThread(SkBitmap* image, const ExtensionResource& resource,
const gfx::Size& original_size, int id) {
DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE));
if (tracker_)
tracker_->OnImageLoaded(image, resource, original_size, id, true);
delete image;
}
private:
friend class base::RefCountedThreadSafe<ImageLoader>;
~ImageLoader() {}
// The tracker we are loading the image for. If NULL, it means the tracker is
// no longer interested in the reply.
ImageLoadingTracker* tracker_;
// The thread that we need to call back on to report that we are done.
BrowserThread::ID callback_thread_id_;
DISALLOW_COPY_AND_ASSIGN(ImageLoader);
};
////////////////////////////////////////////////////////////////////////////////
// ImageLoadingTracker
ImageLoadingTracker::ImageLoadingTracker(Observer* observer)
: observer_(observer),
next_id_(0) {
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
content::NotificationService::AllSources());
}
ImageLoadingTracker::~ImageLoadingTracker() {
// The loader is created lazily and is NULL if the tracker is destroyed before
// any valid image load tasks have been posted.
if (loader_)
loader_->StopTracking();
}
void ImageLoadingTracker::LoadImage(const Extension* extension,
const ExtensionResource& resource,
const gfx::Size& max_size,
CacheParam cache) {
std::vector<ImageInfo> info_list;
info_list.push_back(ImageInfo(resource, max_size));
LoadImages(extension, info_list, cache);
}
void ImageLoadingTracker::LoadImages(const Extension* extension,
const std::vector<ImageInfo>& info_list,
CacheParam cache) {
PendingLoadInfo load_info;
load_info.extension = extension;
load_info.cache = cache;
load_info.extension_id = extension->id();
load_info.pending_count = info_list.size();
int id = next_id_++;
load_map_[id] = load_info;
for (std::vector<ImageInfo>::const_iterator it = info_list.begin();
it != info_list.end(); ++it) {
// Load resources for special component extensions.
if (load_info.extension_id == extension_misc::kWebStoreAppId) {
if (!loader_)
loader_ = new ImageLoader(this);
loader_->LoadResource(it->resource, it->max_size, id, IDR_WEBSTORE_ICON);
continue;
} else if (load_info.extension_id == extension_misc::kChromeAppId) {
if (!loader_)
loader_ = new ImageLoader(this);
loader_->LoadResource(it->resource,
it->max_size,
id,
IDR_PRODUCT_LOGO_128);
continue;
}
// If we don't have a path we don't need to do any further work, just
// respond back.
if (it->resource.relative_path().empty()) {
OnImageLoaded(NULL, it->resource, it->max_size, id, false);
continue;
}
DCHECK(extension->path() == it->resource.extension_root());
// See if the extension has the image already.
if (extension->HasCachedImage(it->resource, it->max_size)) {
SkBitmap image = extension->GetCachedImage(it->resource, it->max_size);
OnImageLoaded(&image, it->resource, it->max_size, id, false);
continue;
}
// Instruct the ImageLoader to load this on the File thread. LoadImage and
// LoadResource do not block.
if (!loader_)
loader_ = new ImageLoader(this);
int resource_id;
if (IsComponentExtensionResource(extension, it->resource, resource_id))
loader_->LoadResource(it->resource, it->max_size, id, resource_id);
else
loader_->LoadImage(it->resource, it->max_size, id);
}
}
bool ImageLoadingTracker::IsComponentExtensionResource(
const Extension* extension,
const ExtensionResource& resource,
int& resource_id) const {
if (extension->location() != Extension::COMPONENT)
return false;
FilePath directory_path = extension->path();
FilePath relative_path = directory_path.BaseName().Append(
resource.relative_path());
for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) {
FilePath resource_path =
FilePath().AppendASCII(kComponentExtensionResources[i].name);
resource_path = resource_path.NormalizePathSeparators();
if (relative_path == resource_path) {
resource_id = kComponentExtensionResources[i].value;
return true;
}
}
return false;
}
void ImageLoadingTracker::OnImageLoaded(
SkBitmap* image,
const ExtensionResource& resource,
const gfx::Size& original_size,
int id,
bool should_cache) {
LoadMap::iterator load_map_it = load_map_.find(id);
DCHECK(load_map_it != load_map_.end());
PendingLoadInfo* info = &load_map_it->second;
// Save the pending results.
DCHECK(info->pending_count > 0);
info->pending_count--;
if (image)
info->bitmaps.push_back(*image);
// Add to the extension's image cache if requested.
DCHECK(info->cache != CACHE || info->extension);
if (should_cache && info->cache == CACHE && !resource.empty() &&
!info->extension->HasCachedImage(resource, original_size)) {
info->extension->SetCachedImage(resource, image ? *image : SkBitmap(),
original_size);
}
// If all pending images are done then report back.
if (info->pending_count == 0) {
gfx::Image image;
std::string extension_id = info->extension_id;
if (info->bitmaps.size() > 0) {
gfx::ImageSkia image_skia;
for (std::vector<SkBitmap>::const_iterator it = info->bitmaps.begin();
it != info->bitmaps.end(); ++it) {
// TODO(pkotwicz): Do something better but ONLY when DIP is enabled.
image_skia.AddRepresentation(
gfx::ImageSkiaRep(*it, ui::SCALE_FACTOR_100P));
}
image = gfx::Image(image_skia);
}
load_map_.erase(load_map_it);
// ImageLoadingTracker might be deleted after the callback so don't
// anything after this statement.
observer_->OnImageLoaded(image, extension_id, id);
}
}
void ImageLoadingTracker::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED);
const Extension* extension =
content::Details<extensions::UnloadedExtensionInfo>(details)->extension;
// Remove reference to this extension from all pending load entries. This
// ensures we don't attempt to cache the image when the load completes.
for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) {
PendingLoadInfo* info = &i->second;
if (info->extension == extension) {
info->extension = NULL;
info->cache = DONT_CACHE;
}
}
}
|