Index: chrome/browser/extensions/extension_action_icon_factory.cc |
diff --git a/chrome/browser/extensions/extension_action_icon_factory.cc b/chrome/browser/extensions/extension_action_icon_factory.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c54e4defc9e39829671325f0f5ad201015dea0d6 |
--- /dev/null |
+++ b/chrome/browser/extensions/extension_action_icon_factory.cc |
@@ -0,0 +1,94 @@ |
+// 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/extension_action_icon_factory.h" |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "chrome/browser/extensions/extension_icon_image.h" |
+#include "chrome/common/extensions/extension.h" |
+#include "chrome/common/extensions/extension_constants.h" |
+#include "chrome/common/extensions/extension_icon_set.h" |
+#include "grit/theme_resources.h" |
+#include "ui/base/resource/resource_bundle.h" |
+#include "ui/gfx/image/image_skia.h" |
+ |
+using extensions::Extension; |
+using extensions::IconImage; |
+ |
+namespace { |
+ |
+int GetDesiredSizeForActionType(ExtensionAction::Type type) { |
+ switch (type) { |
+ case ExtensionAction::TYPE_BROWSER: |
+ case ExtensionAction::TYPE_PAGE: |
+ return extension_misc::EXTENSION_ICON_ACTION; |
+ case ExtensionAction::TYPE_SCRIPT_BADGE: |
+ return extension_misc::EXTENSION_ICON_BITTY; |
+ default: |
+ NOTREACHED(); |
+ return 0; |
+ } |
+} |
+ |
+} // namespace |
+ |
+// extension::IconImage wrapper for extension action icons. We need this |
+// because ExtensionAction class cannot use extensions::IconImage directly. |
+class ExtensionActionIcon : public IconImage::Observer { |
+ public: |
+ // ExtensionActionIconFactory::Observer should outlive this. |
+ ExtensionActionIcon(const Extension* extension, |
+ const ExtensionIconSet& icon_set, |
+ int desired_size, |
+ ExtensionActionIconFactory::Observer* observer) |
+ : observer_(observer) { |
+ gfx::ImageSkia* favicon = |
+ ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
+ IDR_EXTENSIONS_FAVICON); |
+ |
+ icon_image_.reset(new IconImage(extension, icon_set, desired_size, *favicon, |
+ this)); |
+ } |
+ |
+ virtual ~ExtensionActionIcon() {} |
+ |
+ // extensions::IconImage::Observer overrides. |
+ virtual void OnExtensionIconImageChanged(IconImage* image) OVERRIDE { |
+ observer_->OnIconUpdated(); |
+ } |
+ |
+ const gfx::ImageSkia& image_skia() const { return icon_image_->image_skia(); } |
+ |
+ private: |
+ scoped_ptr<IconImage> icon_image_; |
+ ExtensionActionIconFactory::Observer* observer_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ExtensionActionIcon); |
+}; |
+ |
+ExtensionActionIconFactory::ExtensionActionIconFactory( |
+ const Extension* extension, Observer* observer) |
+ : extension_(extension), |
+ observer_(observer), |
+ last_icon_set_(NULL) { |
+} |
+ |
+ExtensionActionIconFactory::~ExtensionActionIconFactory() {} |
+ |
+gfx::ImageSkia ExtensionActionIconFactory::GetIcon( |
+ const ExtensionIconSet* icon_set, |
+ ExtensionAction::Type type) { |
+ // If icon set hasn't changed, return the present icon. |
+ if (icon_.get() && last_icon_set_ == icon_set) |
+ return icon_->image_skia(); |
+ |
+ // TODO(tbarzic): Cache old icon, so we don't have to reload it if gets |
+ // requested again. |
+ int desired_size = GetDesiredSizeForActionType(type); |
+ icon_.reset( |
+ new ExtensionActionIcon(extension_, *icon_set, desired_size, observer_)); |
+ last_icon_set_ = icon_set; |
+ |
+ return icon_->image_skia(); |
+} |