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/extension_action_icon_factory.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "chrome/browser/extensions/extension_icon_image.h" | |
9 #include "chrome/common/extensions/extension.h" | |
10 #include "chrome/common/extensions/extension_constants.h" | |
11 #include "chrome/common/extensions/extension_icon_set.h" | |
12 #include "grit/theme_resources.h" | |
13 #include "ui/base/resource/resource_bundle.h" | |
14 #include "ui/gfx/image/image_skia.h" | |
15 | |
16 using extensions::Extension; | |
17 using extensions::IconImage; | |
18 | |
19 namespace { | |
20 | |
21 int GetDesiredSizeForActionType(ExtensionAction::Type type) { | |
22 switch (type) { | |
23 case ExtensionAction::TYPE_BROWSER: | |
24 case ExtensionAction::TYPE_PAGE: | |
25 return extension_misc::EXTENSION_ICON_ACTION; | |
26 case ExtensionAction::TYPE_SCRIPT_BADGE: | |
27 return extension_misc::EXTENSION_ICON_BITTY; | |
28 default: | |
29 NOTREACHED(); | |
30 return 0; | |
31 } | |
32 } | |
33 | |
34 } // namespace | |
Jeffrey Yasskin
2012/09/01 03:54:33
.cc-local classes should go in anonymous namespace
tbarzic
2012/09/05 01:05:29
Done.
| |
35 | |
36 // extension::IconImage wrapper for extension action icons. We need this | |
Jeffrey Yasskin
2012/09/01 03:54:33
Why is this a separate class from the browser-leve
tbarzic
2012/09/05 01:05:29
Done.
| |
37 // because ExtensionAction class cannot use extensions::IconImage directly. | |
38 class ExtensionActionIcon : public IconImage::Observer { | |
39 public: | |
40 // ExtensionActionIconFactory::Observer should outlive this. | |
41 ExtensionActionIcon(const Extension* extension, | |
42 const ExtensionIconSet& icon_set, | |
43 int desired_size, | |
44 ExtensionActionIconFactory::Observer* observer) | |
45 : observer_(observer) { | |
46 gfx::ImageSkia* favicon = | |
47 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
48 IDR_EXTENSIONS_FAVICON); | |
49 | |
50 icon_image_.reset(new IconImage(extension, icon_set, desired_size, *favicon, | |
51 this)); | |
52 } | |
53 | |
54 virtual ~ExtensionActionIcon() {} | |
55 | |
56 // extensions::IconImage::Observer overrides. | |
57 virtual void OnExtensionIconImageChanged(IconImage* image) OVERRIDE { | |
58 observer_->OnIconUpdated(); | |
59 } | |
60 | |
61 const gfx::ImageSkia& image_skia() const { return icon_image_->image_skia(); } | |
62 | |
63 private: | |
64 scoped_ptr<IconImage> icon_image_; | |
65 ExtensionActionIconFactory::Observer* observer_; | |
66 | |
67 DISALLOW_COPY_AND_ASSIGN(ExtensionActionIcon); | |
68 }; | |
69 | |
70 ExtensionActionIconFactory::ExtensionActionIconFactory( | |
71 const Extension* extension, Observer* observer) | |
72 : extension_(extension), | |
73 observer_(observer), | |
74 last_icon_set_(NULL) { | |
75 } | |
76 | |
77 ExtensionActionIconFactory::~ExtensionActionIconFactory() {} | |
78 | |
79 gfx::ImageSkia ExtensionActionIconFactory::GetIcon( | |
80 const ExtensionIconSet* icon_set, | |
81 ExtensionAction::Type type) { | |
82 // If icon set hasn't changed, return the present icon. | |
83 if (icon_.get() && last_icon_set_ == icon_set) | |
84 return icon_->image_skia(); | |
85 | |
86 // TODO(tbarzic): Cache old icon, so we don't have to reload it if gets | |
87 // requested again. | |
88 int desired_size = GetDesiredSizeForActionType(type); | |
89 icon_.reset( | |
90 new ExtensionActionIcon(extension_, *icon_set, desired_size, observer_)); | |
91 last_icon_set_ = icon_set; | |
92 | |
93 return icon_->image_skia(); | |
94 } | |
OLD | NEW |