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 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ | |
6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/memory/linked_ptr.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/memory/weak_ptr.h" | |
16 #include "base/observer_list.h" | |
17 #include "third_party/skia/include/core/SkColor.h" | |
18 #include "ui/base/animation/linear_animation.h" | |
19 | |
20 class GURL; | |
21 class SkBitmap; | |
22 class SkDevice; | |
23 | |
24 namespace gfx { | |
25 class Canvas; | |
26 class Image; | |
27 class ImageSkia; | |
28 class Rect; | |
29 class Size; | |
30 } | |
31 | |
32 // ExtensionAction encapsulates the state of a browser action, page action, or | |
33 // script badge. | |
34 // Instances can have both global and per-tab state. If a property does not have | |
35 // a per-tab value, the global value is used instead. | |
36 class ExtensionAction { | |
37 public: | |
38 // Use this ID to indicate the default state for properties that take a tab_id | |
39 // parameter. | |
40 static const int kDefaultTabId; | |
41 | |
42 // The types of extension actions. | |
43 enum Type { | |
44 TYPE_BROWSER, | |
45 TYPE_PAGE, | |
46 TYPE_SCRIPT_BADGE, | |
47 }; | |
48 | |
49 enum Appearance { | |
50 // The action icon is hidden. | |
51 INVISIBLE, | |
52 // The action is trying to get the user's attention but isn't yet | |
53 // running on the page. Currently only used for script badges. | |
54 WANTS_ATTENTION, | |
55 // The action icon is visible with its normal appearance. | |
56 ACTIVE, | |
57 }; | |
58 | |
59 // A fade-in animation. | |
60 class IconAnimation : public ui::LinearAnimation { | |
61 public: | |
62 // Observes changes to icon animation state. | |
63 class Observer { | |
64 public: | |
65 virtual void OnIconChanged() = 0; | |
66 | |
67 protected: | |
68 virtual ~Observer() {} | |
69 }; | |
70 | |
71 // A holder for an IconAnimation with a scoped observer. | |
72 class ScopedObserver { | |
73 public: | |
74 ScopedObserver(const base::WeakPtr<IconAnimation>& icon_animation, | |
75 Observer* observer); | |
76 ~ScopedObserver(); | |
77 | |
78 // Gets the icon animation, or NULL if the reference has expired. | |
79 const IconAnimation* icon_animation() const { | |
80 return icon_animation_.get(); | |
81 } | |
82 | |
83 private: | |
84 base::WeakPtr<IconAnimation> icon_animation_; | |
85 Observer* observer_; | |
86 | |
87 DISALLOW_COPY_AND_ASSIGN(ScopedObserver); | |
88 }; | |
89 | |
90 virtual ~IconAnimation(); | |
91 | |
92 // Returns the icon derived from the current animation state applied to | |
93 // |icon|. Ownership remains with this. | |
94 const SkBitmap& Apply(const SkBitmap& icon) const; | |
95 | |
96 void AddObserver(Observer* observer); | |
97 void RemoveObserver(Observer* observer); | |
98 | |
99 private: | |
100 // Construct using ExtensionAction::RunIconAnimation(). | |
101 friend class ExtensionAction; | |
102 IconAnimation(); | |
103 | |
104 base::WeakPtr<IconAnimation> AsWeakPtr(); | |
105 | |
106 // ui::LinearAnimation implementation. | |
107 virtual void AnimateToState(double state) OVERRIDE; | |
108 | |
109 // Device we use to paint icons to. | |
110 mutable scoped_ptr<SkDevice> device_; | |
111 | |
112 ObserverList<Observer> observers_; | |
113 | |
114 base::WeakPtrFactory<IconAnimation> weak_ptr_factory_; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(IconAnimation); | |
117 }; | |
118 | |
119 ExtensionAction(const std::string& extension_id, Type action_type); | |
120 ~ExtensionAction(); | |
121 | |
122 // Gets a copy of this, ownership passed to caller. | |
123 // It doesn't make sense to copy of an ExtensionAction except in tests. | |
124 scoped_ptr<ExtensionAction> CopyForTest() const; | |
125 | |
126 // extension id | |
127 const std::string& extension_id() const { return extension_id_; } | |
128 | |
129 // What kind of action is this? | |
130 Type action_type() const { return action_type_; } | |
131 | |
132 // action id -- only used with legacy page actions API | |
133 std::string id() const { return id_; } | |
134 void set_id(const std::string& id) { id_ = id; } | |
135 | |
136 // static icon paths from manifest -- only used with legacy page actions API. | |
137 std::vector<std::string>* icon_paths() { return &icon_paths_; } | |
138 const std::vector<std::string>* icon_paths() const { return &icon_paths_; } | |
139 | |
140 bool has_changed() const { return has_changed_; } | |
141 void set_has_changed(bool value) { has_changed_ = value; } | |
142 | |
143 // Set the url which the popup will load when the user clicks this action's | |
144 // icon. Setting an empty URL will disable the popup for a given tab. | |
145 void SetPopupUrl(int tab_id, const GURL& url); | |
146 | |
147 // Use HasPopup() to see if a popup should be displayed. | |
148 bool HasPopup(int tab_id) const; | |
149 | |
150 // Get the URL to display in a popup. | |
151 GURL GetPopupUrl(int tab_id) const; | |
152 | |
153 // Set this action's title on a specific tab. | |
154 void SetTitle(int tab_id, const std::string& title) { | |
155 SetValue(&title_, tab_id, title); | |
156 } | |
157 | |
158 // If tab |tab_id| has a set title, return it. Otherwise, return | |
159 // the default title. | |
160 std::string GetTitle(int tab_id) const { return GetValue(&title_, tab_id); } | |
161 | |
162 // Icons are a bit different because the default value can be set to either a | |
163 // bitmap or a path. However, conceptually, there is only one default icon. | |
164 // Setting the default icon using a path clears the bitmap and vice-versa. | |
165 | |
166 // Since ExtensionAction, living in common/, can't interact with the browser | |
167 // to load images, the UI code needs to load the images for each path. For | |
168 // each path in default_icon_path() and icon_paths(), load the image there | |
169 // using an ImageLoadingTracker and call CacheIcon(path, image) with the | |
170 // result. | |
171 // | |
172 // If an image is cached redundantly, the first load will be used. | |
173 void CacheIcon(const std::string& path, const gfx::Image& icon); | |
174 | |
175 // Set this action's icon bitmap on a specific tab. | |
176 void SetIcon(int tab_id, const gfx::Image& image); | |
177 | |
178 // Get the icon for a tab, or the default if no icon was set for this tab, | |
179 // retrieving icons that have been specified by path from the previous | |
180 // arguments to CacheIcon(). If the default icon isn't found in the cache, | |
181 // returns the puzzle piece icon. | |
182 gfx::Image GetIcon(int tab_id) const; | |
183 | |
184 // Gets the icon that has been set using |SetIcon| for the tab. | |
185 gfx::ImageSkia GetExplicitlySetIcon(int tab_id) const; | |
186 | |
187 // Set this action's icon index for a specific tab. For use with | |
188 // icon_paths(), only used in page actions. | |
189 void SetIconIndex(int tab_id, int index); | |
190 | |
191 // Get this action's icon index for a tab, or the default if no icon index | |
192 // was set. | |
193 int GetIconIndex(int tab_id) const { | |
194 return GetValue(&icon_index_, tab_id); | |
195 } | |
196 | |
197 // Non-tab-specific icon path. This is used to support the default_icon key of | |
198 // page and browser actions. | |
199 void set_default_icon_path(const std::string& path) { | |
200 default_icon_path_ = path; | |
201 } | |
202 const std::string& default_icon_path() const { | |
203 return default_icon_path_; | |
204 } | |
205 | |
206 // Set this action's badge text on a specific tab. | |
207 void SetBadgeText(int tab_id, const std::string& text) { | |
208 SetValue(&badge_text_, tab_id, text); | |
209 } | |
210 // Get the badge text for a tab, or the default if no badge text was set. | |
211 std::string GetBadgeText(int tab_id) const { | |
212 return GetValue(&badge_text_, tab_id); | |
213 } | |
214 | |
215 // Set this action's badge text color on a specific tab. | |
216 void SetBadgeTextColor(int tab_id, SkColor text_color) { | |
217 SetValue(&badge_text_color_, tab_id, text_color); | |
218 } | |
219 // Get the text color for a tab, or the default color if no text color | |
220 // was set. | |
221 SkColor GetBadgeTextColor(int tab_id) const { | |
222 return GetValue(&badge_text_color_, tab_id); | |
223 } | |
224 | |
225 // Set this action's badge background color on a specific tab. | |
226 void SetBadgeBackgroundColor(int tab_id, SkColor color) { | |
227 SetValue(&badge_background_color_, tab_id, color); | |
228 } | |
229 // Get the badge background color for a tab, or the default if no color | |
230 // was set. | |
231 SkColor GetBadgeBackgroundColor(int tab_id) const { | |
232 return GetValue(&badge_background_color_, tab_id); | |
233 } | |
234 | |
235 // Set this action's badge visibility on a specific tab. This takes | |
236 // care of any appropriate transition animations. Returns true if | |
237 // the appearance has changed. | |
238 bool SetAppearance(int tab_id, Appearance value); | |
239 // Get the badge visibility for a tab, or the default badge visibility | |
240 // if none was set. | |
241 bool GetIsVisible(int tab_id) const { | |
242 return GetValue(&appearance_, tab_id) != INVISIBLE; | |
243 } | |
244 | |
245 // Remove all tab-specific state. | |
246 void ClearAllValuesForTab(int tab_id); | |
247 | |
248 // If the specified tab has a badge, paint it into the provided bounds. | |
249 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); | |
250 | |
251 // Returns icon image with badge for specified tab. | |
252 gfx::ImageSkia GetIconWithBadge(const gfx::ImageSkia& icon, | |
253 int tab_id, | |
254 const gfx::Size& spacing) const; | |
255 | |
256 // Gets a weak reference to the icon animation for a tab, if any. The | |
257 // reference will only have a value while the animation is running. | |
258 base::WeakPtr<IconAnimation> GetIconAnimation(int tab_id) const; | |
259 | |
260 private: | |
261 class IconWithBadgeImageSource; | |
262 | |
263 // Runs an animation on a tab. | |
264 void RunIconAnimation(int tab_id); | |
265 | |
266 // If the icon animation is running on tab |tab_id|, applies it to | |
267 // |orig| and returns the result. Otherwise, just returns |orig|. | |
268 gfx::ImageSkia ApplyIconAnimation(int tab_id, | |
269 const gfx::ImageSkia& orig) const; | |
270 | |
271 // Paints badge with specified parameters to |canvas|. | |
272 static void DoPaintBadge(gfx::Canvas* canvas, | |
273 const gfx::Rect& bounds, | |
274 const std::string& text, | |
275 const SkColor& text_color_in, | |
276 const SkColor& background_color_in, | |
277 int icon_width); | |
278 | |
279 template <class T> | |
280 struct ValueTraits { | |
281 static T CreateEmpty() { | |
282 return T(); | |
283 } | |
284 }; | |
285 | |
286 template<class T> | |
287 void SetValue(std::map<int, T>* map, int tab_id, const T& val) { | |
288 (*map)[tab_id] = val; | |
289 } | |
290 | |
291 template<class T> | |
292 T GetValue(const std::map<int, T>* map, int tab_id) const { | |
293 typename std::map<int, T>::const_iterator iter = map->find(tab_id); | |
294 if (iter != map->end()) { | |
295 return iter->second; | |
296 } else { | |
297 iter = map->find(kDefaultTabId); | |
298 return iter != map->end() ? iter->second : ValueTraits<T>::CreateEmpty(); | |
299 } | |
300 } | |
301 | |
302 // The id for the extension this action belongs to (as defined in the | |
303 // extension manifest). | |
304 const std::string extension_id_; | |
305 | |
306 const Type action_type_; | |
307 | |
308 // Each of these data items can have both a global state (stored with the key | |
309 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). | |
310 std::map<int, GURL> popup_url_; | |
311 std::map<int, std::string> title_; | |
312 std::map<int, gfx::ImageSkia> icon_; | |
313 std::map<int, int> icon_index_; // index into icon_paths_ | |
314 std::map<int, std::string> badge_text_; | |
315 std::map<int, SkColor> badge_background_color_; | |
316 std::map<int, SkColor> badge_text_color_; | |
317 std::map<int, Appearance> appearance_; | |
318 | |
319 // IconAnimations are destroyed by a delayed task on the UI message loop so | |
320 // that even if the Extension and ExtensionAction are destroyed on a non-UI | |
321 // thread, the animation will still only be touched from the UI thread. This | |
322 // causes the WeakPtr in this map to become NULL. GetIconAnimation() removes | |
323 // NULLs to prevent the map from growing without bound. | |
324 mutable std::map<int, base::WeakPtr<IconAnimation> > icon_animation_; | |
325 | |
326 std::string default_icon_path_; | |
327 | |
328 // The id for the ExtensionAction, for example: "RssPageAction". This is | |
329 // needed for compat with an older version of the page actions API. | |
330 std::string id_; | |
331 | |
332 // A list of paths to icons this action might show. This is needed to support | |
333 // the legacy setIcon({iconIndex:...} method of the page actions API. | |
334 std::vector<std::string> icon_paths_; | |
335 | |
336 // Saves the arguments from CacheIcon() calls. | |
337 std::map<std::string, gfx::ImageSkia> path_to_icon_cache_; | |
338 | |
339 // True if the ExtensionAction's settings have changed from what was | |
340 // specified in the manifest. | |
341 bool has_changed_; | |
342 | |
343 DISALLOW_COPY_AND_ASSIGN(ExtensionAction); | |
344 }; | |
345 | |
346 template<> | |
347 struct ExtensionAction::ValueTraits<int> { | |
348 static int CreateEmpty() { | |
349 return -1; | |
350 } | |
351 }; | |
352 | |
353 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ | |
OLD | NEW |