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 Image; |
27 class ImageSkia; | |
27 class Rect; | 28 class Rect; |
29 class Size; | |
28 } | 30 } |
29 | 31 |
30 // ExtensionAction encapsulates the state of a browser action, page action, or | 32 // ExtensionAction encapsulates the state of a browser action, page action, or |
31 // script badge. | 33 // script badge. |
32 // Instances can have both global and per-tab state. If a property does not have | 34 // 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. | 35 // a per-tab value, the global value is used instead. |
34 class ExtensionAction { | 36 class ExtensionAction { |
35 public: | 37 public: |
36 // Use this ID to indicate the default state for properties that take a tab_id | 38 // Use this ID to indicate the default state for properties that take a tab_id |
37 // parameter. | 39 // parameter. |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
158 // Since ExtensionAction, living in common/, can't interact with the browser | 160 // 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 | 161 // 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 | 162 // each path in default_icon_path() and icon_paths(), load the image there |
161 // using an ImageLoadingTracker and call CacheIcon(path, image) with the | 163 // using an ImageLoadingTracker and call CacheIcon(path, image) with the |
162 // result. | 164 // result. |
163 // | 165 // |
164 // If an image is cached redundantly, the first load will be used. | 166 // If an image is cached redundantly, the first load will be used. |
165 void CacheIcon(const std::string& path, const gfx::Image& icon); | 167 void CacheIcon(const std::string& path, const gfx::Image& icon); |
166 | 168 |
167 // Set this action's icon bitmap on a specific tab. | 169 // Set this action's icon bitmap on a specific tab. |
168 void SetIcon(int tab_id, const SkBitmap& bitmap); | 170 void SetIcon(int tab_id, const gfx::Image& bitmap); |
169 | 171 |
170 // Get the icon for a tab, or the default if no icon was set for this tab, | 172 // 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 | 173 // 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, | 174 // arguments to CacheIcon(). If the default icon isn't found in the cache, |
173 // returns the puzzle piece icon. | 175 // returns the puzzle piece icon. |
174 gfx::Image GetIcon(int tab_id) const; | 176 gfx::Image GetIcon(int tab_id) const; |
175 | 177 |
176 // Set this action's icon index for a specific tab. For use with | 178 // Set this action's icon index for a specific tab. For use with |
177 // icon_paths(), only used in page actions. | 179 // icon_paths(), only used in page actions. |
178 void SetIconIndex(int tab_id, int index); | 180 void SetIconIndex(int tab_id, int index); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 // Get the badge visibility for a tab, or the default badge visibility | 230 // Get the badge visibility for a tab, or the default badge visibility |
229 // if none was set. | 231 // if none was set. |
230 bool GetIsVisible(int tab_id) const { | 232 bool GetIsVisible(int tab_id) const { |
231 return GetValue(&appearance_, tab_id) != INVISIBLE; | 233 return GetValue(&appearance_, tab_id) != INVISIBLE; |
232 } | 234 } |
233 | 235 |
234 // Remove all tab-specific state. | 236 // Remove all tab-specific state. |
235 void ClearAllValuesForTab(int tab_id); | 237 void ClearAllValuesForTab(int tab_id); |
236 | 238 |
237 // If the specified tab has a badge, paint it into the provided bounds. | 239 // 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); | 240 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); |
Jeffrey Yasskin
2012/08/09 21:39:04
Do the new functions let you make this private or
tbarzic
2012/08/10 06:24:08
we still need this..
| |
239 | 241 |
242 // Returns icon image with badge for specified tab. | |
243 gfx::ImageSkia GetIconWithBadge(const gfx::ImageSkia& icon, | |
244 int tab_id, | |
245 const gfx::Size& spacing) const; | |
246 | |
240 // Gets a weak reference to the icon animation for a tab, if any. The | 247 // 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. | 248 // reference will only have a value while the animation is running. |
242 base::WeakPtr<IconAnimation> GetIconAnimation(int tab_id) const; | 249 base::WeakPtr<IconAnimation> GetIconAnimation(int tab_id) const; |
243 | 250 |
244 private: | 251 private: |
245 // Runs an animation on a tab. | 252 // Runs an animation on a tab. |
246 void RunIconAnimation(int tab_id); | 253 void RunIconAnimation(int tab_id); |
247 | 254 |
255 private: | |
248 class IconAnimationWrapper; | 256 class IconAnimationWrapper; |
257 class AnimatedIconImageSource; | |
Jeffrey Yasskin
2012/08/09 21:39:04
Do these need to be members of ExtensionAction, or
tbarzic
2012/08/10 06:24:08
IconWithBadgeImageSource uses private DoPaintBadge
| |
258 class IconWithBadgeImageSource; | |
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; | |
269 | |
270 // Paints badge with specified parameters to |canvas|. | |
271 static void DoPaintBadge(gfx::Canvas* canvas, | |
272 const gfx::Rect& bounds, | |
273 const std::string& text, | |
274 const SkColor& text_color_in, | |
275 const SkColor& background_color_in, | |
276 int icon_width); | |
258 | 277 |
259 template <class T> | 278 template <class T> |
260 struct ValueTraits { | 279 struct ValueTraits { |
261 static T CreateEmpty() { | 280 static T CreateEmpty() { |
262 return T(); | 281 return T(); |
263 } | 282 } |
264 }; | 283 }; |
265 | 284 |
266 template<class T> | 285 template<class T> |
267 void SetValue(std::map<int, T>* map, int tab_id, const T& val) { | 286 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 | 301 // The id for the extension this action belongs to (as defined in the |
283 // extension manifest). | 302 // extension manifest). |
284 const std::string extension_id_; | 303 const std::string extension_id_; |
285 | 304 |
286 const Type action_type_; | 305 const Type action_type_; |
287 | 306 |
288 // Each of these data items can have both a global state (stored with the key | 307 // 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). | 308 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). |
290 std::map<int, GURL> popup_url_; | 309 std::map<int, GURL> popup_url_; |
291 std::map<int, std::string> title_; | 310 std::map<int, std::string> title_; |
292 std::map<int, gfx::Image> icon_; | 311 std::map<int, gfx::ImageSkia> icon_; |
Jeffrey Yasskin
2012/08/09 21:39:04
Why ImageSkia instead of Image? It would be good t
tbarzic
2012/08/10 06:24:08
we are using ImageSkia internally inside this clas
Jeffrey Yasskin
2012/08/10 07:40:35
I know less about this than you do. :)
| |
293 std::map<int, int> icon_index_; // index into icon_paths_ | 312 std::map<int, int> icon_index_; // index into icon_paths_ |
294 std::map<int, std::string> badge_text_; | 313 std::map<int, std::string> badge_text_; |
295 std::map<int, SkColor> badge_background_color_; | 314 std::map<int, SkColor> badge_background_color_; |
296 std::map<int, SkColor> badge_text_color_; | 315 std::map<int, SkColor> badge_text_color_; |
297 std::map<int, Appearance> appearance_; | 316 std::map<int, Appearance> appearance_; |
298 | 317 |
299 // IconAnimationWrappers own themselves so that even if the Extension and | 318 // IconAnimationWrappers own themselves so that even if the Extension and |
300 // ExtensionAction are destroyed on a non-UI thread, the animation will still | 319 // 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 | 320 // 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. | 321 // itself, which causes the WeakPtr in this map to become NULL. |
303 // GetIconAnimationWrapper() removes NULLs to prevent the map from growing | 322 // GetIconAnimationWrapper() removes NULLs to prevent the map from growing |
304 // without bound. | 323 // without bound. |
305 mutable std::map<int, base::WeakPtr<IconAnimationWrapper> > icon_animation_; | 324 mutable std::map<int, base::WeakPtr<IconAnimationWrapper> > icon_animation_; |
306 | 325 |
307 std::string default_icon_path_; | 326 std::string default_icon_path_; |
308 | 327 |
309 // The id for the ExtensionAction, for example: "RssPageAction". This is | 328 // The id for the ExtensionAction, for example: "RssPageAction". This is |
310 // needed for compat with an older version of the page actions API. | 329 // needed for compat with an older version of the page actions API. |
311 std::string id_; | 330 std::string id_; |
312 | 331 |
313 // A list of paths to icons this action might show. This is needed to support | 332 // 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. | 333 // the legacy setIcon({iconIndex:...} method of the page actions API. |
315 std::vector<std::string> icon_paths_; | 334 std::vector<std::string> icon_paths_; |
316 | 335 |
317 // Saves the arguments from CacheIcon() calls. | 336 // Saves the arguments from CacheIcon() calls. |
318 std::map<std::string, gfx::Image> path_to_icon_cache_; | 337 std::map<std::string, gfx::ImageSkia> path_to_icon_cache_; |
319 | 338 |
320 DISALLOW_COPY_AND_ASSIGN(ExtensionAction); | 339 DISALLOW_COPY_AND_ASSIGN(ExtensionAction); |
321 }; | 340 }; |
322 | 341 |
323 template<> | 342 template<> |
324 struct ExtensionAction::ValueTraits<int> { | 343 struct ExtensionAction::ValueTraits<int> { |
325 static int CreateEmpty() { | 344 static int CreateEmpty() { |
326 return -1; | 345 return -1; |
327 } | 346 } |
328 }; | 347 }; |
329 | 348 |
330 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ | 349 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ |
OLD | NEW |