OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ | 5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ |
6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ | 6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/memory/linked_ptr.h" |
| 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/observer_list.h" |
14 #include "third_party/skia/include/core/SkColor.h" | 18 #include "third_party/skia/include/core/SkColor.h" |
| 19 #include "ui/base/animation/linear_animation.h" |
| 20 |
| 21 class GURL; |
| 22 class SkBitmap; |
| 23 class SkDevice; |
15 | 24 |
16 namespace gfx { | 25 namespace gfx { |
17 class Canvas; | 26 class Canvas; |
18 class Rect; | 27 class Rect; |
19 } | 28 } |
20 | 29 |
21 class GURL; | |
22 class SkBitmap; | |
23 | |
24 // ExtensionAction encapsulates the state of a browser action, page action, or | 30 // ExtensionAction encapsulates the state of a browser action, page action, or |
25 // script badge. | 31 // script badge. |
26 // Instances can have both global and per-tab state. If a property does not have | 32 // Instances can have both global and per-tab state. If a property does not have |
27 // a per-tab value, the global value is used instead. | 33 // a per-tab value, the global value is used instead. |
28 class ExtensionAction { | 34 class ExtensionAction { |
29 public: | 35 public: |
30 // Use this ID to indicate the default state for properties that take a tab_id | 36 // Use this ID to indicate the default state for properties that take a tab_id |
31 // parameter. | 37 // parameter. |
32 static const int kDefaultTabId; | 38 static const int kDefaultTabId; |
33 | 39 |
34 // The types of extension actions. | 40 // The types of extension actions. |
35 enum Type { | 41 enum Type { |
36 TYPE_BROWSER, | 42 TYPE_BROWSER, |
37 TYPE_PAGE, | 43 TYPE_PAGE, |
38 TYPE_SCRIPT_BADGE, | 44 TYPE_SCRIPT_BADGE, |
39 }; | 45 }; |
40 | 46 |
| 47 // A fade-in animation. |
| 48 class IconAnimation : public ui::LinearAnimation, |
| 49 public base::SupportsWeakPtr<IconAnimation> { |
| 50 public: |
| 51 // Observes changes to icon animation state. |
| 52 class Observer { |
| 53 public: |
| 54 virtual void OnIconChanged(const IconAnimation& animation) = 0; |
| 55 |
| 56 protected: |
| 57 virtual ~Observer() {} |
| 58 }; |
| 59 |
| 60 // A holder for an IconAnimation with a scoped observer. |
| 61 class ScopedObserver { |
| 62 public: |
| 63 ScopedObserver(const base::WeakPtr<IconAnimation>& icon_animation, |
| 64 Observer* observer); |
| 65 ~ScopedObserver(); |
| 66 |
| 67 // Gets the icon animation, or NULL if the reference has expired. |
| 68 const IconAnimation* icon_animation() const { |
| 69 return icon_animation_.get(); |
| 70 } |
| 71 |
| 72 private: |
| 73 base::WeakPtr<IconAnimation> icon_animation_; |
| 74 Observer* observer_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(ScopedObserver); |
| 77 }; |
| 78 |
| 79 virtual ~IconAnimation(); |
| 80 |
| 81 // Returns the icon derived from the current animation state applied to |
| 82 // |icon|. Ownership remains with this. |
| 83 const SkBitmap& Apply(const SkBitmap& icon) const; |
| 84 |
| 85 void AddObserver(Observer* observer); |
| 86 void RemoveObserver(Observer* observer); |
| 87 |
| 88 private: |
| 89 // Construct using ExtensionAction::RunIconAnimation(). |
| 90 friend class ExtensionAction; |
| 91 explicit IconAnimation(ui::AnimationDelegate* delegate); |
| 92 |
| 93 // ui::LinearAnimation implementation. |
| 94 virtual void AnimateToState(double state) OVERRIDE; |
| 95 |
| 96 // Device we use to paint icons to. |
| 97 mutable scoped_ptr<SkDevice> device_; |
| 98 |
| 99 ObserverList<Observer> observers_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(IconAnimation); |
| 102 }; |
| 103 |
41 ExtensionAction(const std::string& extension_id, Type action_type); | 104 ExtensionAction(const std::string& extension_id, Type action_type); |
42 ~ExtensionAction(); | 105 ~ExtensionAction(); |
43 | 106 |
| 107 // Gets a copy of this, ownership passed to caller. |
| 108 // It doesn't make sense to copy of an ExtensionAction except in tests. |
| 109 scoped_ptr<ExtensionAction> CopyForTest() const; |
| 110 |
44 // extension id | 111 // extension id |
45 const std::string& extension_id() const { return extension_id_; } | 112 const std::string& extension_id() const { return extension_id_; } |
46 | 113 |
47 // What kind of action is this? | 114 // What kind of action is this? |
48 Type action_type() const { return action_type_; } | 115 Type action_type() const { return action_type_; } |
49 | 116 |
50 // action id -- only used with legacy page actions API | 117 // action id -- only used with legacy page actions API |
51 std::string id() const { return id_; } | 118 std::string id() const { return id_; } |
52 void set_id(const std::string& id) { id_ = id; } | 119 void set_id(const std::string& id) { id_ = id; } |
53 | 120 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 bool GetIsVisible(int tab_id) const { | 210 bool GetIsVisible(int tab_id) const { |
144 return GetValue(&visible_, tab_id); | 211 return GetValue(&visible_, tab_id); |
145 } | 212 } |
146 | 213 |
147 // Remove all tab-specific state. | 214 // Remove all tab-specific state. |
148 void ClearAllValuesForTab(int tab_id); | 215 void ClearAllValuesForTab(int tab_id); |
149 | 216 |
150 // If the specified tab has a badge, paint it into the provided bounds. | 217 // If the specified tab has a badge, paint it into the provided bounds. |
151 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); | 218 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); |
152 | 219 |
| 220 // Gets a weak reference to the icon animation for a tab, if any. The |
| 221 // reference will only have a value while the animation is running. |
| 222 base::WeakPtr<IconAnimation> GetIconAnimation(int tab_id) const; |
| 223 |
| 224 // Runs an animation on a tab. |
| 225 void RunIconAnimation(int tab_id); |
| 226 |
153 private: | 227 private: |
154 template <class T> | 228 template <class T> |
155 struct ValueTraits { | 229 struct ValueTraits { |
156 static T CreateEmpty() { | 230 static T CreateEmpty() { |
157 return T(); | 231 return T(); |
158 } | 232 } |
159 }; | 233 }; |
160 | 234 |
161 template<class T> | 235 template<class T> |
162 void SetValue(std::map<int, T>* map, int tab_id, const T& val) { | 236 void SetValue(std::map<int, T>* map, int tab_id, const T& val) { |
(...skipping 21 matching lines...) Expand all Loading... |
184 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). | 258 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). |
185 std::map<int, GURL> popup_url_; | 259 std::map<int, GURL> popup_url_; |
186 std::map<int, std::string> title_; | 260 std::map<int, std::string> title_; |
187 std::map<int, SkBitmap> icon_; | 261 std::map<int, SkBitmap> icon_; |
188 std::map<int, int> icon_index_; // index into icon_paths_ | 262 std::map<int, int> icon_index_; // index into icon_paths_ |
189 std::map<int, std::string> badge_text_; | 263 std::map<int, std::string> badge_text_; |
190 std::map<int, SkColor> badge_background_color_; | 264 std::map<int, SkColor> badge_background_color_; |
191 std::map<int, SkColor> badge_text_color_; | 265 std::map<int, SkColor> badge_text_color_; |
192 std::map<int, bool> visible_; | 266 std::map<int, bool> visible_; |
193 | 267 |
| 268 class IconAnimationWrapper; |
| 269 std::map<int, linked_ptr<IconAnimationWrapper> > icon_animation_; |
| 270 |
194 std::string default_icon_path_; | 271 std::string default_icon_path_; |
195 | 272 |
196 // The id for the ExtensionAction, for example: "RssPageAction". This is | 273 // The id for the ExtensionAction, for example: "RssPageAction". This is |
197 // needed for compat with an older version of the page actions API. | 274 // needed for compat with an older version of the page actions API. |
198 std::string id_; | 275 std::string id_; |
199 | 276 |
200 // A list of paths to icons this action might show. This is needed to support | 277 // A list of paths to icons this action might show. This is needed to support |
201 // the legacy setIcon({iconIndex:...} method of the page actions API. | 278 // the legacy setIcon({iconIndex:...} method of the page actions API. |
202 std::vector<std::string> icon_paths_; | 279 std::vector<std::string> icon_paths_; |
| 280 |
| 281 DISALLOW_COPY_AND_ASSIGN(ExtensionAction); |
203 }; | 282 }; |
204 | 283 |
205 template<> | 284 template<> |
206 struct ExtensionAction::ValueTraits<int> { | 285 struct ExtensionAction::ValueTraits<int> { |
207 static int CreateEmpty() { | 286 static int CreateEmpty() { |
208 return -1; | 287 return -1; |
209 } | 288 } |
210 }; | 289 }; |
211 | 290 |
212 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ | 291 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ |
OLD | NEW |