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 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
17 #include "third_party/skia/include/core/SkColor.h" | 17 #include "third_party/skia/include/core/SkColor.h" |
18 #include "ui/base/animation/linear_animation.h" | 18 #include "ui/base/animation/linear_animation.h" |
19 | 19 |
20 class GURL; | 20 class GURL; |
21 class SkBitmap; | 21 class SkBitmap; |
22 class SkDevice; | 22 class SkDevice; |
23 | 23 |
24 namespace gfx { | 24 namespace gfx { |
25 class Canvas; | 25 class Canvas; |
26 class Image; | 26 class ImageSkia; |
27 class Rect; | 27 class Rect; |
| 28 class Size; |
28 } | 29 } |
29 | 30 |
30 // ExtensionAction encapsulates the state of a browser action, page action, or | 31 // ExtensionAction encapsulates the state of a browser action, page action, or |
31 // script badge. | 32 // script badge. |
32 // Instances can have both global and per-tab state. If a property does not have | 33 // Instances can have both global and per-tab state. If a property does not have |
33 // a per-tab value, the global value is used instead. | 34 // a per-tab value, the global value is used instead. |
34 class ExtensionAction { | 35 class ExtensionAction { |
35 public: | 36 public: |
36 // Use this ID to indicate the default state for properties that take a tab_id | 37 // Use this ID to indicate the default state for properties that take a tab_id |
37 // parameter. | 38 // parameter. |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 // bitmap or a path. However, conceptually, there is only one default icon. | 156 // bitmap or a path. However, conceptually, there is only one default icon. |
156 // Setting the default icon using a path clears the bitmap and vice-versa. | 157 // Setting the default icon using a path clears the bitmap and vice-versa. |
157 | 158 |
158 // Since ExtensionAction, living in common/, can't interact with the browser | 159 // Since ExtensionAction, living in common/, can't interact with the browser |
159 // to load images, the UI code needs to load the images for each path. For | 160 // to load images, the UI code needs to load the images for each path. For |
160 // each path in default_icon_path() and icon_paths(), load the image there | 161 // each path in default_icon_path() and icon_paths(), load the image there |
161 // using an ImageLoadingTracker and call CacheIcon(path, image) with the | 162 // using an ImageLoadingTracker and call CacheIcon(path, image) with the |
162 // result. | 163 // result. |
163 // | 164 // |
164 // If an image is cached redundantly, the first load will be used. | 165 // If an image is cached redundantly, the first load will be used. |
165 void CacheIcon(const std::string& path, const gfx::Image& icon); | 166 void CacheIcon(const std::string& path, const gfx::ImageSkia& icon); |
166 | 167 |
167 // Set this action's icon bitmap on a specific tab. | 168 // Set this action's icon bitmap on a specific tab. |
168 void SetIcon(int tab_id, const SkBitmap& bitmap); | 169 void SetIcon(int tab_id, const gfx::ImageSkia& bitmap); |
169 | 170 |
170 // Get the icon for a tab, or the default if no icon was set for this tab, | 171 // Get the icon for a tab, or the default if no icon was set for this tab, |
171 // retrieving icons that have been specified by path from the previous | 172 // retrieving icons that have been specified by path from the previous |
172 // arguments to CacheIcon(). If the default icon isn't found in the cache, | 173 // arguments to CacheIcon(). If the default icon isn't found in the cache, |
173 // returns the puzzle piece icon. | 174 // returns the puzzle piece icon. |
174 gfx::Image GetIcon(int tab_id) const; | 175 gfx::ImageSkia GetIcon(int tab_id) const; |
175 | 176 |
176 // Set this action's icon index for a specific tab. For use with | 177 // Set this action's icon index for a specific tab. For use with |
177 // icon_paths(), only used in page actions. | 178 // icon_paths(), only used in page actions. |
178 void SetIconIndex(int tab_id, int index); | 179 void SetIconIndex(int tab_id, int index); |
179 | 180 |
180 // Get this action's icon index for a tab, or the default if no icon index | 181 // Get this action's icon index for a tab, or the default if no icon index |
181 // was set. | 182 // was set. |
182 int GetIconIndex(int tab_id) const { | 183 int GetIconIndex(int tab_id) const { |
183 return GetValue(&icon_index_, tab_id); | 184 return GetValue(&icon_index_, tab_id); |
184 } | 185 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 bool GetIsVisible(int tab_id) const { | 231 bool GetIsVisible(int tab_id) const { |
231 return GetValue(&appearance_, tab_id) != INVISIBLE; | 232 return GetValue(&appearance_, tab_id) != INVISIBLE; |
232 } | 233 } |
233 | 234 |
234 // Remove all tab-specific state. | 235 // Remove all tab-specific state. |
235 void ClearAllValuesForTab(int tab_id); | 236 void ClearAllValuesForTab(int tab_id); |
236 | 237 |
237 // If the specified tab has a badge, paint it into the provided bounds. | 238 // If the specified tab has a badge, paint it into the provided bounds. |
238 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); | 239 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); |
239 | 240 |
| 241 // Returns icon image with badge for specified tab. |
| 242 gfx::ImageSkia GetIconWithBadge(const gfx::ImageSkia& icon, |
| 243 int tab_id, |
| 244 const gfx::Size& spacing) const; |
| 245 |
240 // Gets a weak reference to the icon animation for a tab, if any. The | 246 // Gets a weak reference to the icon animation for a tab, if any. The |
241 // reference will only have a value while the animation is running. | 247 // reference will only have a value while the animation is running. |
242 base::WeakPtr<IconAnimation> GetIconAnimation(int tab_id) const; | 248 base::WeakPtr<IconAnimation> GetIconAnimation(int tab_id) const; |
243 | 249 |
244 private: | 250 private: |
245 // Runs an animation on a tab. | 251 // Runs an animation on a tab. |
246 void RunIconAnimation(int tab_id); | 252 void RunIconAnimation(int tab_id); |
247 | 253 |
| 254 private: |
248 class IconAnimationWrapper; | 255 class IconAnimationWrapper; |
| 256 class AnimatedIconImageSource; |
| 257 class IconWithBadgeImageSource; |
249 | 258 |
250 // Finds the icon animation wrapper for a tab, if any. If the animation for | 259 // Finds the icon animation wrapper for a tab, if any. If the animation for |
251 // this tab has recently completed, also removes up any other dead wrappers | 260 // this tab has recently completed, also removes up any other dead wrappers |
252 // from the map. | 261 // from the map. |
253 IconAnimationWrapper* GetIconAnimationWrapper(int tab_id) const; | 262 IconAnimationWrapper* GetIconAnimationWrapper(int tab_id) const; |
254 | 263 |
255 // If the icon animation is running on tab |tab_id|, applies it to | 264 // If the icon animation is running on tab |tab_id|, applies it to |
256 // |orig| and returns the result. Otherwise, just returns |orig|. | 265 // |orig| and returns the result. Otherwise, just returns |orig|. |
257 gfx::Image ApplyIconAnimation(int tab_id, const gfx::Image& orig) const; | 266 gfx::ImageSkia ApplyIconAnimation(int tab_id, |
| 267 const gfx::ImageSkia& orig) const; |
| 268 |
| 269 // Paints badge with specified parameters to |canvas|. |
| 270 static void DoPaintBadge(gfx::Canvas* canvas, |
| 271 const gfx::Rect& bounds, |
| 272 const std::string& text, |
| 273 const SkColor& text_color_in, |
| 274 const SkColor& background_color_in, |
| 275 int icon_width); |
258 | 276 |
259 template <class T> | 277 template <class T> |
260 struct ValueTraits { | 278 struct ValueTraits { |
261 static T CreateEmpty() { | 279 static T CreateEmpty() { |
262 return T(); | 280 return T(); |
263 } | 281 } |
264 }; | 282 }; |
265 | 283 |
266 template<class T> | 284 template<class T> |
267 void SetValue(std::map<int, T>* map, int tab_id, const T& val) { | 285 void SetValue(std::map<int, T>* map, int tab_id, const T& val) { |
(...skipping 14 matching lines...) Expand all Loading... |
282 // The id for the extension this action belongs to (as defined in the | 300 // The id for the extension this action belongs to (as defined in the |
283 // extension manifest). | 301 // extension manifest). |
284 const std::string extension_id_; | 302 const std::string extension_id_; |
285 | 303 |
286 const Type action_type_; | 304 const Type action_type_; |
287 | 305 |
288 // Each of these data items can have both a global state (stored with the key | 306 // Each of these data items can have both a global state (stored with the key |
289 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). | 307 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). |
290 std::map<int, GURL> popup_url_; | 308 std::map<int, GURL> popup_url_; |
291 std::map<int, std::string> title_; | 309 std::map<int, std::string> title_; |
292 std::map<int, gfx::Image> icon_; | 310 std::map<int, gfx::ImageSkia> icon_; |
293 std::map<int, int> icon_index_; // index into icon_paths_ | 311 std::map<int, int> icon_index_; // index into icon_paths_ |
294 std::map<int, std::string> badge_text_; | 312 std::map<int, std::string> badge_text_; |
295 std::map<int, SkColor> badge_background_color_; | 313 std::map<int, SkColor> badge_background_color_; |
296 std::map<int, SkColor> badge_text_color_; | 314 std::map<int, SkColor> badge_text_color_; |
297 std::map<int, Appearance> appearance_; | 315 std::map<int, Appearance> appearance_; |
298 | 316 |
299 // IconAnimationWrappers own themselves so that even if the Extension and | 317 // IconAnimationWrappers own themselves so that even if the Extension and |
300 // ExtensionAction are destroyed on a non-UI thread, the animation will still | 318 // ExtensionAction are destroyed on a non-UI thread, the animation will still |
301 // only be touched from the UI thread. When an animation finishes, it deletes | 319 // only be touched from the UI thread. When an animation finishes, it deletes |
302 // itself, which causes the WeakPtr in this map to become NULL. | 320 // itself, which causes the WeakPtr in this map to become NULL. |
303 // GetIconAnimationWrapper() removes NULLs to prevent the map from growing | 321 // GetIconAnimationWrapper() removes NULLs to prevent the map from growing |
304 // without bound. | 322 // without bound. |
305 mutable std::map<int, base::WeakPtr<IconAnimationWrapper> > icon_animation_; | 323 mutable std::map<int, base::WeakPtr<IconAnimationWrapper> > icon_animation_; |
306 | 324 |
307 std::string default_icon_path_; | 325 std::string default_icon_path_; |
308 | 326 |
309 // The id for the ExtensionAction, for example: "RssPageAction". This is | 327 // The id for the ExtensionAction, for example: "RssPageAction". This is |
310 // needed for compat with an older version of the page actions API. | 328 // needed for compat with an older version of the page actions API. |
311 std::string id_; | 329 std::string id_; |
312 | 330 |
313 // A list of paths to icons this action might show. This is needed to support | 331 // A list of paths to icons this action might show. This is needed to support |
314 // the legacy setIcon({iconIndex:...} method of the page actions API. | 332 // the legacy setIcon({iconIndex:...} method of the page actions API. |
315 std::vector<std::string> icon_paths_; | 333 std::vector<std::string> icon_paths_; |
316 | 334 |
317 // Saves the arguments from CacheIcon() calls. | 335 // Saves the arguments from CacheIcon() calls. |
318 std::map<std::string, gfx::Image> path_to_icon_cache_; | 336 std::map<std::string, gfx::ImageSkia> path_to_icon_cache_; |
319 | 337 |
320 DISALLOW_COPY_AND_ASSIGN(ExtensionAction); | 338 DISALLOW_COPY_AND_ASSIGN(ExtensionAction); |
321 }; | 339 }; |
322 | 340 |
323 template<> | 341 template<> |
324 struct ExtensionAction::ValueTraits<int> { | 342 struct ExtensionAction::ValueTraits<int> { |
325 static int CreateEmpty() { | 343 static int CreateEmpty() { |
326 return -1; | 344 return -1; |
327 } | 345 } |
328 }; | 346 }; |
329 | 347 |
330 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ | 348 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ |
OLD | NEW |