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
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/image_loader.h"
#include <stddef.h>
#include <map>
#include <utility>
#include <vector>
#include "base/compiler_specific.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/thread_pool.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/component_extension_resource_manager.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/image_loader_factory.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_handlers/icons_handler.h"
#include "skia/ext/image_operations.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/resource/resource_scale_factor.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image_family.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
using content::BrowserThread;
namespace extensions {
namespace {
bool ShouldResizeImageRepresentation(
ImageLoader::ImageRepresentation::ResizeCondition resize_method,
const gfx::Size& decoded_size,
const gfx::Size& desired_size) {
switch (resize_method) {
case ImageLoader::ImageRepresentation::ALWAYS_RESIZE:
return decoded_size != desired_size;
case ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER:
return decoded_size.width() > desired_size.width() ||
decoded_size.height() > desired_size.height();
case ImageLoader::ImageRepresentation::NEVER_RESIZE:
return false;
default:
NOTREACHED();
}
}
SkBitmap ResizeIfNeeded(const SkBitmap& bitmap,
const ImageLoader::ImageRepresentation& image_info) {
gfx::Size original_size(bitmap.width(), bitmap.height());
if (ShouldResizeImageRepresentation(image_info.resize_condition,
original_size,
image_info.desired_size)) {
return skia::ImageOperations::Resize(
bitmap, skia::ImageOperations::RESIZE_LANCZOS3,
image_info.desired_size.width(), image_info.desired_size.height());
}
return bitmap;
}
void LoadResourceOnUIThread(int resource_id, SkBitmap* bitmap) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
gfx::ImageSkia image(
*ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id));
image.MakeThreadSafe();
*bitmap = *image.bitmap();
}
SkBitmap LoadImageBlocking(const ImageLoader::ImageRepresentation& image_info) {
// Read the file from disk.
base::FilePath path = image_info.resource.GetFilePath();
if (path.empty()) {
return SkBitmap();
}
std::optional<std::vector<uint8_t>> file_contents =
base::ReadFileToBytes(path);
if (!file_contents) {
return SkBitmap();
}
// Note: This class only decodes bitmaps 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 bitmaps here that were generated by Chrome.
return gfx::PNGCodec::Decode(file_contents.value());
}
std::vector<SkBitmap> LoadResourceBitmaps(
const Extension* extension,
const std::vector<ImageLoader::ImageRepresentation>& info_list) {
// Loading resources has to happen on the UI thread. So do this first, and
// pass the rest of the work off as a blocking pool task.
std::vector<SkBitmap> bitmaps;
bitmaps.resize(info_list.size());
int i = 0;
for (auto it = info_list.cbegin(); it != info_list.cend(); ++it, ++i) {
DCHECK(it->resource.relative_path().empty() ||
extension->path() == it->resource.extension_root());
int resource_id = 0;
if (extension->location() == mojom::ManifestLocation::kComponent) {
const extensions::ComponentExtensionResourceManager* manager =
extensions::ExtensionsBrowserClient::Get()
->GetComponentExtensionResourceManager();
if (manager &&
manager->IsComponentExtensionResource(
extension->path(), it->resource.relative_path(), &resource_id)) {
DCHECK(!ui::ResourceBundle::GetSharedInstance().IsGzipped(resource_id));
LoadResourceOnUIThread(resource_id, &bitmaps[i]);
}
}
}
return bitmaps;
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// ImageLoader::ImageRepresentation
ImageLoader::ImageRepresentation::ImageRepresentation(
const ExtensionResource& resource,
ResizeCondition resize_condition,
const gfx::Size& desired_size,
float scale_factor)
: resource(resource),
resize_condition(resize_condition),
desired_size(desired_size),
scale_factor(scale_factor) {}
ImageLoader::ImageRepresentation::~ImageRepresentation() {
}
////////////////////////////////////////////////////////////////////////////////
// ImageLoader::LoadResult
struct ImageLoader::LoadResult {
LoadResult(const SkBitmap& bitmap,
const gfx::Size& original_size,
const ImageRepresentation& image_representation);
~LoadResult();
SkBitmap bitmap;
gfx::Size original_size;
ImageRepresentation image_representation;
};
ImageLoader::LoadResult::LoadResult(
const SkBitmap& bitmap,
const gfx::Size& original_size,
const ImageLoader::ImageRepresentation& image_representation)
: bitmap(bitmap),
original_size(original_size),
image_representation(image_representation) {
}
ImageLoader::LoadResult::~LoadResult() {
}
namespace {
// Need to be after ImageRepresentation and LoadResult are defined.
std::vector<ImageLoader::LoadResult> LoadImagesBlocking(
const std::vector<ImageLoader::ImageRepresentation>& info_list,
const std::vector<SkBitmap>& bitmaps) {
std::vector<ImageLoader::LoadResult> load_result;
for (size_t i = 0; i < info_list.size(); ++i) {
const ImageLoader::ImageRepresentation& image = info_list[i];
// If we don't have a path there isn't anything we can do, just skip it.
if (image.resource.relative_path().empty()) {
continue;
}
SkBitmap bitmap;
if (bitmaps[i].isNull()) {
bitmap = LoadImageBlocking(image);
} else {
bitmap = bitmaps[i];
}
// If the image failed to load, skip it.
if (bitmap.isNull() || bitmap.empty()) {
continue;
}
gfx::Size original_size(bitmap.width(), bitmap.height());
bitmap = ResizeIfNeeded(bitmap, image);
load_result.emplace_back(bitmap, original_size, image);
}
return load_result;
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// ImageLoader
ImageLoader::ImageLoader() = default;
ImageLoader::~ImageLoader() {
}
// static
ImageLoader* ImageLoader::Get(content::BrowserContext* context) {
return ImageLoaderFactory::GetForBrowserContext(context);
}
void ImageLoader::LoadImageAsync(const Extension* extension,
const ExtensionResource& resource,
const gfx::Size& max_size,
ImageLoaderImageCallback callback) {
std::vector<ImageRepresentation> info_list;
info_list.push_back(ImageRepresentation(
resource, ImageRepresentation::RESIZE_WHEN_LARGER, max_size, 1.f));
LoadImagesAsync(extension, info_list, std::move(callback));
}
void ImageLoader::LoadImageAtEveryScaleFactorAsync(
const Extension* extension,
const gfx::Size& dip_size,
ImageLoaderImageCallback callback) {
std::vector<ImageRepresentation> info_list;
std::set<float> scales;
for (const auto scale : ui::GetSupportedResourceScaleFactors()) {
scales.insert(ui::GetScaleForResourceScaleFactor(scale));
}
// There may not be a screen in unit tests.
auto* screen = display::Screen::GetScreen();
if (screen) {
for (const auto& display : screen->GetAllDisplays())
scales.insert(display.device_scale_factor());
}
for (const auto scale : scales) {
const gfx::Size px_size = gfx::ScaleToFlooredSize(dip_size, scale);
ExtensionResource image = IconsInfo::GetIconResource(
extension, px_size.width(), ExtensionIconSet::Match::kBigger);
info_list.emplace_back(image, ImageRepresentation::ALWAYS_RESIZE, px_size,
scale);
}
LoadImagesAsync(extension, info_list, std::move(callback));
}
void ImageLoader::LoadImagesAsync(
const Extension* extension,
const std::vector<ImageRepresentation>& info_list,
ImageLoaderImageCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
base::BindOnce(LoadImagesBlocking, info_list,
LoadResourceBitmaps(extension, info_list)),
base::BindOnce(&ImageLoader::ReplyBack, weak_ptr_factory_.GetWeakPtr(),
std::move(callback)));
}
void ImageLoader::LoadImageFamilyAsync(
const Extension* extension,
const std::vector<ImageRepresentation>& info_list,
ImageLoaderImageFamilyCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
base::BindOnce(LoadImagesBlocking, info_list,
LoadResourceBitmaps(extension, info_list)),
base::BindOnce(&ImageLoader::ReplyBackWithImageFamily,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void ImageLoader::ReplyBack(ImageLoaderImageCallback callback,
const std::vector<LoadResult>& load_result) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
gfx::ImageSkia image_skia;
for (auto it = load_result.cbegin(); it != load_result.cend(); ++it) {
const SkBitmap& bitmap = it->bitmap;
const ImageRepresentation& image_rep = it->image_representation;
image_skia.AddRepresentation(
gfx::ImageSkiaRep(bitmap, image_rep.scale_factor));
}
gfx::Image image;
if (!image_skia.isNull()) {
image_skia.MakeThreadSafe();
image = gfx::Image(image_skia);
}
std::move(callback).Run(image);
}
void ImageLoader::ReplyBackWithImageFamily(
ImageLoaderImageFamilyCallback callback,
const std::vector<LoadResult>& load_result) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
std::map<std::pair<int, int>, gfx::ImageSkia> image_skia_map;
gfx::ImageFamily image_family;
for (auto it = load_result.cbegin(); it != load_result.cend(); ++it) {
const SkBitmap& bitmap = it->bitmap;
const ImageRepresentation& image_rep = it->image_representation;
const std::pair<int, int> key = std::make_pair(
image_rep.desired_size.width(), image_rep.desired_size.height());
// Create a new ImageSkia for this width/height, or add a representation to
// an existing ImageSkia with the same width/height.
image_skia_map[key].AddRepresentation(
gfx::ImageSkiaRep(bitmap, image_rep.scale_factor));
}
for (auto it = image_skia_map.begin(); it != image_skia_map.end(); ++it) {
it->second.MakeThreadSafe();
image_family.Add(it->second);
}
std::move(callback).Run(std::move(image_family));
}
} // namespace extensions
|