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_utils.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/file_util.h" | |
| 9 #include "base/path_service.h" | |
| 10 #include "base/threading/sequenced_worker_pool.h" | |
| 11 #include "chrome/common/chrome_notification_types.h" | |
| 12 #include "chrome/common/chrome_paths.h" | |
| 13 #include "chrome/common/extensions/extension.h" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "content/public/browser/notification_observer.h" | |
| 16 #include "content/public/browser/notification_registrar.h" | |
| 17 #include "content/public/browser/notification_service.h" | |
| 18 #include "grit/component_extension_resources_map.h" | |
| 19 #include "grit/theme_resources.h" | |
| 20 #include "skia/ext/image_operations.h" | |
| 21 #include "third_party/skia/include/core/SkBitmap.h" | |
| 22 #include "ui/base/resource/resource_bundle.h" | |
| 23 #include "ui/gfx/image/image.h" | |
| 24 #include "ui/gfx/image/image_skia.h" | |
| 25 #include "webkit/glue/image_decoder.h" | |
| 26 | |
| 27 using content::BrowserThread; | |
| 28 using extension_image_utils::ImageRepresentation; | |
| 29 using extensions::Extension; | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 bool ShouldResizeImageRepresentation( | |
| 34 ImageRepresentation::ResizeCondition resize_method, | |
| 35 const gfx::Size& decoded_size, | |
| 36 const gfx::Size& desired_size) { | |
| 37 switch (resize_method) { | |
| 38 case ImageRepresentation::ALWAYS_RESIZE: | |
| 39 return decoded_size != desired_size; | |
| 40 case ImageRepresentation::RESIZE_WHEN_LARGER: | |
| 41 return decoded_size.width() > desired_size.width() || | |
| 42 decoded_size.height() > desired_size.height(); | |
| 43 default: | |
| 44 NOTREACHED(); | |
| 45 return false; | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 SkBitmap ResizeIfNeeded(const SkBitmap& bitmap, | |
| 50 const ImageRepresentation& image_info) { | |
| 51 gfx::Size original_size(bitmap.width(), bitmap.height()); | |
| 52 if (ShouldResizeImageRepresentation(image_info.resize_condition, | |
| 53 original_size, | |
| 54 image_info.desired_size)) { | |
| 55 return skia::ImageOperations::Resize( | |
| 56 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | |
| 57 image_info.desired_size.width(), image_info.desired_size.height()); | |
| 58 } | |
| 59 | |
| 60 return bitmap; | |
| 61 } | |
| 62 | |
| 63 void LoadResourceOnUIThread(int resource_id, SkBitmap* bitmap) { | |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 65 | |
| 66 gfx::ImageSkia image( | |
| 67 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id)); | |
| 68 image.MakeThreadSafe(); | |
| 69 *bitmap = *image.bitmap(); | |
| 70 } | |
| 71 | |
| 72 void LoadImageOnBlockingPool(const ImageRepresentation& image_info, | |
| 73 SkBitmap* bitmap) { | |
| 74 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 75 | |
| 76 // Read the file from disk. | |
| 77 std::string file_contents; | |
| 78 FilePath path = image_info.resource.GetFilePath(); | |
| 79 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { | |
| 80 return; | |
| 81 } | |
| 82 | |
| 83 // Decode the bitmap using WebKit's image decoder. | |
| 84 const unsigned char* data = | |
| 85 reinterpret_cast<const unsigned char*>(file_contents.data()); | |
| 86 webkit_glue::ImageDecoder decoder; | |
| 87 // Note: This class only decodes bitmaps from extension resources. Chrome | |
| 88 // doesn't (for security reasons) directly load extension resources provided | |
| 89 // by the extension author, but instead decodes them in a separate | |
| 90 // locked-down utility process. Only if the decoding succeeds is the image | |
| 91 // saved from memory to disk and subsequently used in the Chrome UI. | |
| 92 // Chrome is therefore decoding bitmaps here that were generated by Chrome. | |
| 93 *bitmap = decoder.Decode(data, file_contents.length()); | |
| 94 } | |
| 95 | |
| 96 void LoadImagesOnBlockingPool( | |
| 97 const std::vector<ImageRepresentation>& info_list, | |
| 98 const std::vector<SkBitmap*>& bitmaps, | |
| 99 const base::Callback<void(const gfx::Image&)>& callback, | |
| 100 BrowserThread::ID thread_id) { | |
| 101 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 102 | |
| 103 gfx::ImageSkia image_skia; | |
| 104 | |
| 105 int i = 0; | |
| 106 for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); | |
| 107 it != info_list.end(); ++it, ++i) { | |
| 108 // If we don't have a path there isn't anything we can do, just skip it. | |
| 109 if (it->resource.relative_path().empty()) | |
| 110 continue; | |
| 111 | |
| 112 SkBitmap bitmap; | |
| 113 if (bitmaps[i]) { | |
| 114 bitmap = *bitmaps[i]; | |
| 115 delete bitmaps[i]; | |
| 116 } else { | |
| 117 LoadImageOnBlockingPool(*it, &bitmap); | |
| 118 } | |
| 119 | |
| 120 // If the image failed to load, skip it. | |
| 121 if (bitmap.isNull() || bitmap.empty()) | |
| 122 continue; | |
| 123 | |
| 124 bitmap = ResizeIfNeeded(bitmap, *it); | |
| 125 | |
| 126 image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap, it->scale_factor)); | |
| 127 } | |
| 128 | |
| 129 gfx::Image image; | |
| 130 | |
| 131 if (!image_skia.isNull()) { | |
| 132 image_skia.MakeThreadSafe(); | |
| 133 image = gfx::Image(image_skia); | |
| 134 } | |
| 135 | |
| 136 BrowserThread::PostTask(thread_id, FROM_HERE, base::Bind(callback, image)); | |
| 137 } | |
| 138 | |
| 139 } // namespace | |
| 140 | |
| 141 namespace extension_image_utils { | |
| 142 | |
| 143 bool IsComponentExtensionResource(const Extension* extension, | |
| 144 const FilePath& resource_path, | |
| 145 int* resource_id) { | |
| 146 static const GritResourceMap kExtraComponentExtensionResources[] = { | |
| 147 {"web_store/webstore_icon_128.png", IDR_WEBSTORE_ICON}, | |
| 148 {"web_store/webstore_icon_16.png", IDR_WEBSTORE_ICON_16}, | |
| 149 {"chrome_app/product_logo_128.png", IDR_PRODUCT_LOGO_128}, | |
| 150 {"chrome_app/product_logo_16.png", IDR_PRODUCT_LOGO_16}, | |
| 151 }; | |
| 152 static const size_t kExtraComponentExtensionResourcesSize = | |
| 153 arraysize(kExtraComponentExtensionResources); | |
| 154 | |
| 155 if (extension->location() != Extension::COMPONENT) | |
|
Aaron Boodman
2012/10/08 19:00:31
If the location is component, then we should never
Marijn Kruisselbrink
2012/10/08 20:24:52
So should all the branches that return false have
| |
| 156 return false; | |
| 157 | |
| 158 FilePath directory_path = extension->path(); | |
| 159 FilePath resources_dir; | |
| 160 FilePath relative_path; | |
| 161 if (!PathService::Get(chrome::DIR_RESOURCES, &resources_dir) || | |
| 162 !resources_dir.AppendRelativePath(directory_path, &relative_path)) { | |
| 163 return false; | |
| 164 } | |
| 165 relative_path = relative_path.Append(resource_path); | |
| 166 relative_path = relative_path.NormalizePathSeparators(); | |
| 167 | |
| 168 // TODO(tc): Make a map of FilePath -> resource ids so we don't have to | |
| 169 // covert to FilePaths all the time. This will be more useful as we add | |
| 170 // more resources. | |
| 171 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { | |
| 172 FilePath resource_path = | |
| 173 FilePath().AppendASCII(kComponentExtensionResources[i].name); | |
| 174 resource_path = resource_path.NormalizePathSeparators(); | |
| 175 | |
| 176 if (relative_path == resource_path) { | |
| 177 *resource_id = kComponentExtensionResources[i].value; | |
| 178 return true; | |
| 179 } | |
| 180 } | |
| 181 for (size_t i = 0; i < kExtraComponentExtensionResourcesSize; ++i) { | |
| 182 FilePath resource_path = | |
| 183 FilePath().AppendASCII(kExtraComponentExtensionResources[i].name); | |
| 184 resource_path = resource_path.NormalizePathSeparators(); | |
| 185 | |
| 186 if (relative_path == resource_path) { | |
| 187 *resource_id = kExtraComponentExtensionResources[i].value; | |
| 188 return true; | |
| 189 } | |
| 190 } | |
| 191 return false; | |
| 192 } | |
| 193 | |
| 194 ImageRepresentation::ImageRepresentation( | |
| 195 const ExtensionResource& resource, | |
| 196 ResizeCondition resize_condition, | |
| 197 const gfx::Size& desired_size, | |
| 198 ui::ScaleFactor scale_factor) | |
| 199 : resource(resource), | |
| 200 resize_condition(resize_condition), | |
| 201 desired_size(desired_size), | |
| 202 scale_factor(scale_factor) { | |
| 203 } | |
| 204 | |
| 205 ImageRepresentation::~ImageRepresentation() { | |
| 206 } | |
| 207 | |
| 208 void LoadImageAsync(const Extension* extension, | |
| 209 const ExtensionResource& resource, | |
| 210 const gfx::Size& max_size, | |
| 211 const base::Callback<void(const gfx::Image&)>& callback) { | |
| 212 std::vector<ImageRepresentation> info_list; | |
| 213 info_list.push_back(ImageRepresentation( | |
| 214 resource, | |
| 215 ImageRepresentation::RESIZE_WHEN_LARGER, | |
| 216 max_size, | |
| 217 ui::SCALE_FACTOR_100P)); | |
| 218 LoadImagesAsync(extension, info_list, callback); | |
| 219 } | |
| 220 | |
| 221 void LoadImagesAsync(const Extension* extension, | |
| 222 const std::vector<ImageRepresentation>& info_list, | |
| 223 const base::Callback<void(const gfx::Image&)>& callback) { | |
| 224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 225 | |
| 226 // Loading an image from the cache and loading resources have to happen | |
| 227 // on the UI thread. So do those two things first, and pass the rest of the | |
| 228 // work of as a blocking pool task. | |
| 229 std::vector<SkBitmap*> bitmaps; | |
| 230 bitmaps.resize(info_list.size()); | |
| 231 int i = 0; | |
| 232 for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); | |
| 233 it != info_list.end(); ++it, ++i) { | |
| 234 DCHECK(it->resource.relative_path().empty() || | |
| 235 extension->path() == it->resource.extension_root()); | |
| 236 | |
| 237 if (extension->HasCachedImage(it->resource, it->desired_size)) { | |
| 238 bitmaps[i] = new SkBitmap(extension->GetCachedImage(it->resource, | |
| 239 it->desired_size)); | |
| 240 continue; | |
| 241 } | |
| 242 | |
| 243 int resource_id; | |
| 244 if (IsComponentExtensionResource(extension, it->resource.relative_path(), | |
| 245 &resource_id)) { | |
| 246 bitmaps[i] = new SkBitmap; | |
| 247 LoadResourceOnUIThread(resource_id, bitmaps[i]); | |
| 248 } | |
| 249 } | |
|
Aaron Boodman
2012/10/08 19:00:31
If we're done at this point, it would be nice to c
Marijn Kruisselbrink
2012/10/08 20:24:52
Done. I wasn't sure if it made sense to complicate
| |
| 250 | |
| 251 BrowserThread::ID thread_id; | |
| 252 CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_id)); | |
| 253 DCHECK(!BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 254 BrowserThread::PostBlockingPoolTask( | |
| 255 FROM_HERE, | |
| 256 base::Bind(&LoadImagesOnBlockingPool, info_list, | |
| 257 bitmaps, callback, thread_id)); | |
| 258 } | |
| 259 | |
| 260 } // namespace extension_image_utils | |
| OLD | NEW |