Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/image_loader.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/file_util.h" | |
| 9 #include "base/path_service.h" | |
| 10 #include "base/string_number_conversions.h" | |
| 11 #include "base/threading/sequenced_worker_pool.h" | |
| 12 #include "chrome/browser/extensions/image_loader_factory.h" | |
| 13 #include "chrome/common/chrome_paths.h" | |
| 14 #include "chrome/common/extensions/extension.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "grit/chrome_unscaled_resources.h" | |
| 17 #include "grit/component_extension_resources_map.h" | |
| 18 #include "grit/theme_resources.h" | |
| 19 #include "skia/ext/image_operations.h" | |
| 20 #include "ui/base/resource/resource_bundle.h" | |
| 21 #include "ui/gfx/image/image_skia.h" | |
| 22 #include "webkit/glue/image_decoder.h" | |
| 23 | |
| 24 using content::BrowserThread; | |
| 25 using extensions::Extension; | |
| 26 using extensions::ImageLoader; | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 bool ShouldResizeImageRepresentation( | |
| 31 ImageLoader::ImageRepresentation::ResizeCondition resize_method, | |
| 32 const gfx::Size& decoded_size, | |
| 33 const gfx::Size& desired_size) { | |
| 34 switch (resize_method) { | |
| 35 case ImageLoader::ImageRepresentation::ALWAYS_RESIZE: | |
| 36 return decoded_size != desired_size; | |
| 37 case ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER: | |
| 38 return decoded_size.width() > desired_size.width() || | |
| 39 decoded_size.height() > desired_size.height(); | |
| 40 default: | |
| 41 NOTREACHED(); | |
| 42 return false; | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 SkBitmap ResizeIfNeeded(const SkBitmap& bitmap, | |
| 47 const ImageLoader::ImageRepresentation& image_info) { | |
| 48 gfx::Size original_size(bitmap.width(), bitmap.height()); | |
| 49 if (ShouldResizeImageRepresentation(image_info.resize_condition, | |
| 50 original_size, | |
| 51 image_info.desired_size)) { | |
| 52 return skia::ImageOperations::Resize( | |
| 53 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | |
| 54 image_info.desired_size.width(), image_info.desired_size.height()); | |
| 55 } | |
| 56 | |
| 57 return bitmap; | |
| 58 } | |
| 59 | |
| 60 void LoadResourceOnUIThread(int resource_id, SkBitmap* bitmap) { | |
| 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 62 | |
| 63 gfx::ImageSkia image( | |
| 64 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id)); | |
| 65 image.MakeThreadSafe(); | |
| 66 *bitmap = *image.bitmap(); | |
| 67 } | |
| 68 | |
| 69 void LoadImageOnBlockingPool(const ImageLoader::ImageRepresentation& image_info, | |
| 70 SkBitmap* bitmap) { | |
| 71 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 72 | |
| 73 // Read the file from disk. | |
| 74 std::string file_contents; | |
| 75 FilePath path = image_info.resource.GetFilePath(); | |
| 76 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 // Decode the bitmap using WebKit's image decoder. | |
| 81 const unsigned char* data = | |
| 82 reinterpret_cast<const unsigned char*>(file_contents.data()); | |
| 83 webkit_glue::ImageDecoder decoder; | |
| 84 // Note: This class only decodes bitmaps from extension resources. Chrome | |
| 85 // doesn't (for security reasons) directly load extension resources provided | |
| 86 // by the extension author, but instead decodes them in a separate | |
| 87 // locked-down utility process. Only if the decoding succeeds is the image | |
| 88 // saved from memory to disk and subsequently used in the Chrome UI. | |
| 89 // Chrome is therefore decoding bitmaps here that were generated by Chrome. | |
| 90 *bitmap = decoder.Decode(data, file_contents.length()); | |
| 91 } | |
| 92 | |
| 93 } // namespace | |
| 94 | |
| 95 namespace extensions { | |
| 96 | |
| 97 //////////////////////////////////////////////////////////////////////////////// | |
| 98 // ImageLoader::ImageRepresentation | |
| 99 | |
| 100 ImageLoader::ImageRepresentation::ImageRepresentation( | |
| 101 const ExtensionResource& resource, | |
| 102 ResizeCondition resize_condition, | |
| 103 const gfx::Size& desired_size, | |
| 104 ui::ScaleFactor scale_factor) | |
| 105 : resource(resource), | |
| 106 resize_condition(resize_condition), | |
| 107 desired_size(desired_size), | |
| 108 scale_factor(scale_factor) { | |
| 109 } | |
| 110 | |
| 111 ImageLoader::ImageRepresentation::~ImageRepresentation() { | |
| 112 } | |
| 113 | |
| 114 //////////////////////////////////////////////////////////////////////////////// | |
| 115 // ImageLoader::LoadResult | |
| 116 | |
| 117 struct ImageLoader::LoadResult { | |
| 118 LoadResult(const SkBitmap& bitmap, | |
| 119 const gfx::Size& original_size, | |
| 120 const ImageRepresentation& image_representation); | |
| 121 ~LoadResult(); | |
| 122 | |
| 123 SkBitmap bitmap; | |
| 124 gfx::Size original_size; | |
| 125 ImageRepresentation image_representation; | |
| 126 }; | |
| 127 | |
| 128 ImageLoader::LoadResult::LoadResult( | |
| 129 const SkBitmap& bitmap, | |
| 130 const gfx::Size& original_size, | |
| 131 const ImageLoader::ImageRepresentation& image_representation) | |
| 132 : bitmap(bitmap), | |
| 133 original_size(original_size), | |
| 134 image_representation(image_representation) { | |
| 135 } | |
| 136 | |
| 137 ImageLoader::LoadResult::~LoadResult() { | |
| 138 } | |
| 139 | |
| 140 //////////////////////////////////////////////////////////////////////////////// | |
| 141 // ImageLoader | |
| 142 | |
| 143 ImageLoader::ImageLoader() { | |
| 144 } | |
| 145 | |
| 146 ImageLoader::~ImageLoader() { | |
| 147 } | |
| 148 | |
| 149 // static | |
| 150 ImageLoader* ImageLoader::Get(Profile* profile) { | |
| 151 return ImageLoaderFactory::GetForProfile(profile); | |
| 152 } | |
| 153 | |
| 154 // static | |
| 155 bool ImageLoader::IsComponentExtensionResource(const Extension* extension, | |
| 156 const FilePath& resource_path, | |
| 157 int* resource_id) { | |
| 158 static const GritResourceMap kExtraComponentExtensionResources[] = { | |
| 159 {"web_store/webstore_icon_128.png", IDR_WEBSTORE_ICON}, | |
| 160 {"web_store/webstore_icon_16.png", IDR_WEBSTORE_ICON_16}, | |
| 161 {"chrome_app/product_logo_128.png", IDR_PRODUCT_LOGO_128}, | |
| 162 {"chrome_app/product_logo_16.png", IDR_PRODUCT_LOGO_16}, | |
| 163 {"settings_app/settings_app_icon_128.png", IDR_SETTINGS_APP_ICON_128}, | |
| 164 {"settings_app/settings_app_icon_16.png", IDR_SETTINGS_APP_ICON_16}, | |
| 165 }; | |
| 166 static const size_t kExtraComponentExtensionResourcesSize = | |
| 167 arraysize(kExtraComponentExtensionResources); | |
| 168 | |
| 169 if (extension->location() != Extension::COMPONENT) | |
| 170 return false; | |
| 171 | |
| 172 FilePath directory_path = extension->path(); | |
| 173 FilePath resources_dir; | |
| 174 FilePath relative_path; | |
| 175 if (!PathService::Get(chrome::DIR_RESOURCES, &resources_dir) || | |
| 176 !resources_dir.AppendRelativePath(directory_path, &relative_path)) { | |
| 177 return false; | |
| 178 } | |
| 179 relative_path = relative_path.Append(resource_path); | |
| 180 relative_path = relative_path.NormalizePathSeparators(); | |
| 181 | |
| 182 // TODO(tc): Make a map of FilePath -> resource ids so we don't have to | |
| 183 // covert to FilePaths all the time. This will be more useful as we add | |
| 184 // more resources. | |
| 185 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { | |
| 186 FilePath resource_path = | |
| 187 FilePath().AppendASCII(kComponentExtensionResources[i].name); | |
| 188 resource_path = resource_path.NormalizePathSeparators(); | |
| 189 | |
| 190 if (relative_path == resource_path) { | |
| 191 *resource_id = kComponentExtensionResources[i].value; | |
| 192 return true; | |
| 193 } | |
| 194 } | |
| 195 for (size_t i = 0; i < kExtraComponentExtensionResourcesSize; ++i) { | |
| 196 FilePath resource_path = | |
| 197 FilePath().AppendASCII(kExtraComponentExtensionResources[i].name); | |
| 198 resource_path = resource_path.NormalizePathSeparators(); | |
| 199 | |
| 200 if (relative_path == resource_path) { | |
| 201 *resource_id = kExtraComponentExtensionResources[i].value; | |
| 202 return true; | |
| 203 } | |
| 204 } | |
| 205 return false; | |
| 206 } | |
| 207 | |
| 208 void ImageLoader::LoadImageAsync( | |
| 209 const Extension* extension, | |
| 210 const ExtensionResource& resource, | |
| 211 const gfx::Size& max_size, | |
| 212 const base::Callback<void(const gfx::Image&)>& callback) { | |
| 213 std::vector<ImageRepresentation> info_list; | |
| 214 info_list.push_back(ImageRepresentation( | |
| 215 resource, | |
| 216 ImageRepresentation::RESIZE_WHEN_LARGER, | |
| 217 max_size, | |
| 218 ui::SCALE_FACTOR_100P)); | |
| 219 LoadImagesAsync(extension, info_list, callback); | |
| 220 } | |
| 221 | |
| 222 void ImageLoader::LoadImagesAsync( | |
| 223 const Extension* extension, | |
| 224 const std::vector<ImageRepresentation>& info_list, | |
| 225 const base::Callback<void(const gfx::Image&)>& callback) { | |
| 226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 227 | |
| 228 // Loading an image from the cache and loading resources have to happen | |
| 229 // on the UI thread. So do those two things first, and pass the rest of the | |
| 230 // work of as a blocking pool task. | |
| 231 | |
| 232 std::vector<SkBitmap> bitmaps; | |
| 233 bitmaps.resize(info_list.size()); | |
| 234 | |
| 235 int i = 0; | |
| 236 for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); | |
| 237 it != info_list.end(); ++it, ++i) { | |
| 238 DCHECK(it->resource.relative_path().empty() || | |
| 239 extension->path() == it->resource.extension_root()); | |
| 240 | |
| 241 int resource_id; | |
| 242 if (IsComponentExtensionResource(extension, it->resource.relative_path(), | |
| 243 &resource_id)) { | |
| 244 LoadResourceOnUIThread(resource_id, &bitmaps[i]); | |
| 245 } | |
| 246 } | |
| 247 | |
| 248 DCHECK(!BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 249 BrowserThread::PostBlockingPoolTask( | |
| 250 FROM_HERE, | |
| 251 base::Bind(&ImageLoader::LoadImagesOnBlockingPool, base::Unretained(this), | |
|
Aaron Boodman
2012/11/20 21:46:14
What prevents this callback from happening after t
Marijn Kruisselbrink
2012/11/20 21:51:03
I don't think weak pointers would work for cross-t
Aaron Boodman
2012/11/21 00:57:21
You can pass weakptrs to other threads, you just c
Marijn Kruisselbrink
2012/11/21 01:17:55
Ah yes, you're right (of course). If I make LoadIm
| |
| 252 info_list, bitmaps, callback)); | |
| 253 } | |
| 254 | |
| 255 void ImageLoader::LoadImagesOnBlockingPool( | |
| 256 const std::vector<ImageRepresentation>& info_list, | |
| 257 const std::vector<SkBitmap>& bitmaps, | |
| 258 const base::Callback<void(const gfx::Image&)>& callback) { | |
| 259 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 260 | |
| 261 gfx::ImageSkia image_skia; | |
| 262 | |
| 263 std::vector<LoadResult> load_result; | |
| 264 | |
| 265 int i = 0; | |
| 266 for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); | |
| 267 it != info_list.end(); ++it, ++i) { | |
| 268 // If we don't have a path there isn't anything we can do, just skip it. | |
| 269 if (it->resource.relative_path().empty()) | |
| 270 continue; | |
| 271 | |
| 272 SkBitmap bitmap; | |
| 273 if (!bitmaps[i].isNull()) { | |
| 274 bitmap = bitmaps[i]; | |
| 275 } else { | |
| 276 LoadImageOnBlockingPool(*it, &bitmap); | |
| 277 } | |
| 278 | |
| 279 // If the image failed to load, skip it. | |
| 280 if (bitmap.isNull() || bitmap.empty()) | |
| 281 continue; | |
| 282 | |
| 283 gfx::Size original_size(bitmap.width(), bitmap.height()); | |
| 284 bitmap = ResizeIfNeeded(bitmap, *it); | |
| 285 | |
| 286 load_result.push_back(LoadResult(bitmap, original_size, *it)); | |
| 287 } | |
| 288 | |
| 289 gfx::Image image; | |
| 290 | |
| 291 if (!image_skia.isNull()) { | |
| 292 image_skia.MakeThreadSafe(); | |
| 293 image = gfx::Image(image_skia); | |
| 294 } | |
| 295 | |
| 296 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 297 base::Bind(&ImageLoader::ReplyBack, | |
| 298 base::Unretained(this), load_result, | |
| 299 callback)); | |
| 300 } | |
| 301 | |
| 302 void ImageLoader::ReplyBack( | |
| 303 const std::vector<LoadResult>& load_result, | |
| 304 const base::Callback<void(const gfx::Image&)>& callback) { | |
| 305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 306 | |
| 307 gfx::ImageSkia image_skia; | |
| 308 | |
| 309 for (std::vector<LoadResult>::const_iterator it = load_result.begin(); | |
| 310 it != load_result.end(); ++it) { | |
| 311 const SkBitmap& bitmap = it->bitmap; | |
| 312 const ImageRepresentation& image_rep = it->image_representation; | |
| 313 | |
| 314 image_skia.AddRepresentation(gfx::ImageSkiaRep( | |
| 315 bitmap, image_rep.scale_factor)); | |
| 316 } | |
| 317 | |
| 318 gfx::Image image; | |
| 319 if (!image_skia.isNull()) { | |
| 320 image_skia.MakeThreadSafe(); | |
| 321 image = gfx::Image(image_skia); | |
| 322 } | |
| 323 | |
| 324 callback.Run(image); | |
| 325 } | |
| 326 | |
| 327 } // namespace extensions | |
| OLD | NEW |