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