Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(542)

Unified Diff: chrome/browser/extensions/image_utils.cc

Issue 11027044: Add a class to replace ImageLoadingTracker with a nicer API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/image_utils.cc
diff --git a/chrome/browser/extensions/image_utils.cc b/chrome/browser/extensions/image_utils.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a86fee177f47c07d4206fc85ce90aa8a557a7fd5
--- /dev/null
+++ b/chrome/browser/extensions/image_utils.cc
@@ -0,0 +1,210 @@
+// 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_utils.h"
+
+#include "base/callback.h"
+#include "base/file_util.h"
+#include "base/threading/sequenced_worker_pool.h"
+#include "chrome/common/chrome_notification_types.h"
+#include "chrome/common/extensions/extension.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/notification_observer.h"
+#include "content/public/browser/notification_registrar.h"
+#include "content/public/browser/notification_service.h"
+#include "grit/component_extension_resources_map.h"
+#include "skia/ext/image_operations.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "ui/base/resource/resource_bundle.h"
+#include "ui/gfx/image/image.h"
+#include "ui/gfx/image/image_skia.h"
+#include "webkit/glue/image_decoder.h"
+
+using content::BrowserThread;
+using extension_image_utils::ImageRepresentation;
+using extensions::Extension;
+
+namespace {
+
+// TODO: adapt to my refactoring
+bool IsComponentExtensionResource(
Finnur 2012/10/05 14:41:33 This is copied from the ImageLoadingTracker. Did y
Marijn Kruisselbrink 2012/10/05 18:00:08 I'm using the git workflow, so no svn copy; I coul
+ Extension::Location extension_location,
+ const ExtensionResource& resource,
+ int& resource_id) {
Finnur 2012/10/05 14:41:33 You should use int* resource_id, not int&.
Marijn Kruisselbrink 2012/10/05 18:00:08 Ah yes, I fixed that in ILT in another CL but appa
+ if (extension_location != Extension::COMPONENT)
+ return false;
+
+ FilePath directory_path = resource.extension_root();
+ 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;
+}
+
+bool ShouldResizeImageRepresentation(
+ ImageRepresentation::ResizeCondition resize_method,
+ const gfx::Size& decoded_size,
+ const gfx::Size& desired_size) {
+ switch (resize_method) {
+ case ImageRepresentation::ALWAYS_RESIZE:
+ return decoded_size != desired_size;
+ case ImageRepresentation::RESIZE_WHEN_LARGER:
+ return decoded_size.width() > desired_size.width() ||
+ decoded_size.height() > desired_size.height();
+ default:
+ NOTREACHED();
+ return false;
+ }
+}
+
+SkBitmap ResizeIfNeeded(const SkBitmap& bitmap,
+ const 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 LoadResourceOnBlockingPool(int resource_id,
+ SkBitmap* bitmap) {
+ DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
+
+ gfx::ImageSkia image(
+ *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id));
+ image.MakeThreadSafe();
+ *bitmap = *image.bitmap();
+}
+
+void LoadImageOnBlockingPool(const ImageRepresentation& image_info,
+ SkBitmap* bitmap) {
+ DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
+
+ // Read the file from disk.
+ std::string file_contents;
+ FilePath path = image_info.resource.GetFilePath();
+ if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) {
+ return;
+ }
+
+ // Decode the bitmap using WebKit's image decoder.
+ const unsigned char* data =
+ reinterpret_cast<const unsigned char*>(file_contents.data());
+ webkit_glue::ImageDecoder decoder;
+ // 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.
+ *bitmap = decoder.Decode(data, file_contents.length());
+}
+
+void LoadImagesOnBlockingPool(
+ const std::vector<ImageRepresentation>& info_list,
+ Extension::Location extension_location,
+ const base::Callback<void(const gfx::Image&)>& callback,
+ BrowserThread::ID thread_id) {
+ DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
+
+ gfx::ImageSkia image_skia;
+
+ for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin();
+ it != info_list.end(); ++it) {
+ // If we don't have a path there isn't anything we can do, just skip it.
+ if (it->resource.relative_path().empty())
+ continue;
Finnur 2012/10/05 14:41:33 We used to let the caller know when we were done,
Marijn Kruisselbrink 2012/10/05 18:00:08 ILT only internally needed to send itself a null b
+
+ int resource_id;
+ SkBitmap bitmap;
+ if (IsComponentExtensionResource(extension_location, it->resource,
+ resource_id))
+ LoadResourceOnBlockingPool(resource_id, &bitmap);
+ else
+ LoadImageOnBlockingPool(*it, &bitmap);
+
+ // If the image failed to load, skip it.
+ if (bitmap.isNull() || bitmap.empty())
+ continue;
+
+ bitmap = ResizeIfNeeded(bitmap, *it);
+
+ image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap, it->scale_factor));
+ }
+
+ gfx::Image image;
+
+ if (!image_skia.isNull()) {
+ image_skia.MakeThreadSafe();
+ image = gfx::Image(image_skia);
+ }
+
+ BrowserThread::PostTask(thread_id, FROM_HERE, base::Bind(callback, image));
+}
+
+} // namespace
+
+namespace extension_image_utils {
+
+ImageRepresentation::ImageRepresentation(
+ const ExtensionResource& resource,
+ ResizeCondition resize_condition,
+ const gfx::Size& desired_size,
+ ui::ScaleFactor scale_factor)
+ : resource(resource),
+ resize_condition(resize_condition),
+ desired_size(desired_size),
+ scale_factor(scale_factor) {
+}
+
+ImageRepresentation::~ImageRepresentation() {
+}
+
+void LoadImageAsync(const Extension* extension,
+ const ExtensionResource& resource,
+ const gfx::Size& max_size,
+ const base::Callback<void(const gfx::Image&)>& callback) {
+ std::vector<ImageRepresentation> info_list;
+ info_list.push_back(ImageRepresentation(
+ resource,
+ ImageRepresentation::RESIZE_WHEN_LARGER,
+ max_size,
+ ui::SCALE_FACTOR_100P));
+ LoadImagesAsync(extension, info_list, callback);
+}
+
+void LoadImagesAsync(const Extension* extension,
+ const std::vector<ImageRepresentation>& info_list,
+ const base::Callback<void(const gfx::Image&)>& callback) {
+ for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin();
+ it != info_list.end(); ++it) {
+ DCHECK(it->resource.relative_path().empty() ||
+ extension->path() == it->resource.extension_root());
+ }
+
+ BrowserThread::ID thread_id;
+ CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_id));
+ DCHECK(!BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
+ BrowserThread::PostBlockingPoolTask(
+ FROM_HERE,
+ base::Bind(&LoadImagesOnBlockingPool, info_list,
+ extension->location(), callback, thread_id));
+}
+
+} // namespace extension_image_utils

Powered by Google App Engine
This is Rietveld 408576698