summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/image_loading_tracker.cc
diff options
context:
space:
mode:
authorcira@chromium.org <cira@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-07 22:49:10 +0000
committercira@chromium.org <cira@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-07 22:49:10 +0000
commitecabe6eed156a36238888bfd2fdb96ec4906f0a4 (patch)
tree9419d9a349fc940d45fa450b3a75169fdf48b39f /chrome/browser/extensions/image_loading_tracker.cc
parent7050861ff094fd74d155264b6da3ca3795543870 (diff)
downloadchromium_src-ecabe6eed156a36238888bfd2fdb96ec4906f0a4.zip
chromium_src-ecabe6eed156a36238888bfd2fdb96ec4906f0a4.tar.gz
chromium_src-ecabe6eed156a36238888bfd2fdb96ec4906f0a4.tar.bz2
Loads local resources from current locale subtree if available, if not it falls back to extension subtree.
We look for ext_root/foo/bar.js under ext_root/_locales/fr/foo/bar.js if current locale is fr. If there is no fr specific resource we load ext_root/foo/bar.js instead. Lots of small refactoring to replace FilePath with ExtensionResource. BUG=12131 TEST=See unittest for sample tree. Review URL: http://codereview.chromium.org/256022 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@28333 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/image_loading_tracker.cc')
-rw-r--r--chrome/browser/extensions/image_loading_tracker.cc115
1 files changed, 115 insertions, 0 deletions
diff --git a/chrome/browser/extensions/image_loading_tracker.cc b/chrome/browser/extensions/image_loading_tracker.cc
new file mode 100644
index 0000000..e141391
--- /dev/null
+++ b/chrome/browser/extensions/image_loading_tracker.cc
@@ -0,0 +1,115 @@
+// Copyright (c) 2009 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 "app/gfx/favicon_size.h"
+#include "base/file_util.h"
+#include "base/logging.h"
+#include "base/message_loop.h"
+#include "base/scoped_ptr.h"
+#include "base/task.h"
+#include "base/thread.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/common/extensions/extension_resource.h"
+#include "skia/ext/image_operations.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "webkit/glue/image_decoder.h"
+
+////////////////////////////////////////////////////////////////////////////////
+// ImageLoadingTracker::LoadImageTask
+
+// The LoadImageTask is for asynchronously loading the image on the file thread.
+// If the image is successfully loaded and decoded it will report back on the
+// |callback_loop| to let the caller know the image is done loading.
+class ImageLoadingTracker::LoadImageTask : public Task {
+ public:
+ // Constructor for the LoadImageTask class. |tracker| is the object that
+ // we use to communicate back to the entity that wants the image after we
+ // decode it. |path| is the path to load the image from. |index| is an
+ // identifier for the image that we pass back to the caller.
+ LoadImageTask(ImageLoadingTracker* tracker,
+ const ExtensionResource& resource,
+ size_t index)
+ : callback_loop_(MessageLoop::current()),
+ tracker_(tracker),
+ resource_(resource),
+ index_(index) {}
+
+ void ReportBack(SkBitmap* image) {
+ DCHECK(image);
+ callback_loop_->PostTask(FROM_HERE, NewRunnableMethod(tracker_,
+ &ImageLoadingTracker::OnImageLoaded,
+ image,
+ index_));
+ }
+
+ virtual void Run() {
+ // Read the file from disk.
+ std::string file_contents;
+ FilePath path = resource_.GetFilePath();
+ if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) {
+ ReportBack(NULL);
+ 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(gfx::Size(kFavIconSize, kFavIconSize));
+ scoped_ptr<SkBitmap> decoded(new SkBitmap());
+ *decoded = decoder.Decode(data, file_contents.length());
+ if (decoded->empty()) {
+ ReportBack(NULL);
+ return; // Unable to decode.
+ }
+
+ if (decoded->width() != kFavIconSize || decoded->height() != kFavIconSize) {
+ // The bitmap is not the correct size, re-sample.
+ int new_width = decoded->width();
+ int new_height = decoded->height();
+ // Calculate what dimensions to use within the constraints (16x16 max).
+ calc_favicon_target_size(&new_width, &new_height);
+ *decoded = skia::ImageOperations::Resize(
+ *decoded, skia::ImageOperations::RESIZE_LANCZOS3,
+ new_width, new_height);
+ }
+
+ ReportBack(decoded.release());
+ }
+
+ private:
+ // The message loop that we need to call back on to report that we are done.
+ MessageLoop* callback_loop_;
+
+ // The object that is waiting for us to respond back.
+ ImageLoadingTracker* tracker_;
+
+ // The image resource to load asynchronously.
+ ExtensionResource resource_;
+
+ // The index of the icon being loaded.
+ size_t index_;
+};
+
+////////////////////////////////////////////////////////////////////////////////
+// ImageLoadingTracker
+
+void ImageLoadingTracker::PostLoadImageTask(const ExtensionResource& resource) {
+ MessageLoop* file_loop = g_browser_process->file_thread()->message_loop();
+ file_loop->PostTask(FROM_HERE, new LoadImageTask(this, resource,
+ posted_count_++));
+}
+
+void ImageLoadingTracker::OnImageLoaded(SkBitmap* image, size_t index) {
+ if (image == NULL) {
+ NOTREACHED() << "Image failed to decode.";
+ image = new SkBitmap();
+ }
+ if (observer_)
+ observer_->OnImageLoaded(image, index);
+ delete image;
+ if (--image_count_ == 0)
+ Release(); // We are no longer needed.
+}