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_BROWSER_EXTENSIONS_EXTENSION_ACTION_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_H_ | 6 #define CHROME_BROWSER_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" | |
13 #include "base/memory/linked_ptr.h" | |
14 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/scoped_vector.h" | 13 #include "base/stl_util.h" |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/observer_list.h" | |
18 #include "chrome/common/extensions/api/extension_action/action_info.h" | 14 #include "chrome/common/extensions/api/extension_action/action_info.h" |
19 #include "extensions/common/extension_icon_set.h" | |
20 #include "third_party/skia/include/core/SkColor.h" | 15 #include "third_party/skia/include/core/SkColor.h" |
21 // TODO(robertphillips): change this to "class SkBaseDevice;" | |
22 #include "third_party/skia/include/core/SkDevice.h" | |
23 #include "ui/gfx/animation/linear_animation.h" | |
24 | 16 |
25 class GURL; | 17 class GURL; |
26 class SkBitmap; | 18 |
| 19 namespace content { |
| 20 class BrowserContext; |
| 21 } |
| 22 |
| 23 namespace extensions { |
| 24 class Extension; |
| 25 class IconImage; |
| 26 } |
27 | 27 |
28 namespace gfx { | 28 namespace gfx { |
29 class Canvas; | 29 class Canvas; |
30 class Image; | 30 class Image; |
31 class ImageSkia; | 31 class ImageSkia; |
32 class Rect; | 32 class Rect; |
33 class Size; | 33 class Size; |
34 } | 34 } |
35 | 35 |
36 // ExtensionAction encapsulates the state of a browser action or page action. | 36 // ExtensionAction encapsulates the state of a browser action or page action. |
37 // Instances can have both global and per-tab state. If a property does not have | 37 // Instances can have both global and per-tab state. If a property does not have |
38 // a per-tab value, the global value is used instead. | 38 // a per-tab value, the global value is used instead. |
39 class ExtensionAction { | 39 class ExtensionAction { |
40 public: | 40 public: |
41 // The action that the UI should take after the ExtensionAction is clicked. | 41 // The action that the UI should take after the ExtensionAction is clicked. |
42 enum ShowAction { | 42 enum ShowAction { |
43 ACTION_NONE, | 43 ACTION_NONE, |
44 ACTION_SHOW_POPUP, | 44 ACTION_SHOW_POPUP, |
45 // We don't need a SHOW_CONTEXT_MENU because that's handled separately in | 45 // We don't need a SHOW_CONTEXT_MENU because that's handled separately in |
46 // the UI. | 46 // the UI. |
47 }; | 47 }; |
48 | 48 |
49 // Use this ID to indicate the default state for properties that take a tab_id | 49 // Use this ID to indicate the default state for properties that take a tab_id |
50 // parameter. | 50 // parameter. |
51 static const int kDefaultTabId; | 51 static const int kDefaultTabId; |
52 | 52 |
53 // Max size (both dimensions) for page actions. | 53 // Max size (both dimensions) for page actions. |
54 static const int kPageActionIconMaxSize; | 54 static const int kPageActionIconMaxSize; |
55 | 55 |
56 ExtensionAction(const std::string& extension_id, | 56 ExtensionAction(const extensions::Extension& extension, |
57 extensions::ActionInfo::Type action_type, | 57 extensions::ActionInfo::Type action_type, |
58 const extensions::ActionInfo& manifest_data); | 58 const extensions::ActionInfo& manifest_data); |
59 ~ExtensionAction(); | 59 ~ExtensionAction(); |
60 | 60 |
61 // Gets a copy of this, ownership passed to caller. | |
62 // It doesn't make sense to copy of an ExtensionAction except in tests. | |
63 scoped_ptr<ExtensionAction> CopyForTest() const; | |
64 | |
65 // Given the extension action type, returns the size the extension action icon | |
66 // should have. The icon should be square, so only one dimension is | |
67 // returned. | |
68 static int GetIconSizeForType(extensions::ActionInfo::Type type); | |
69 | |
70 // extension id | 61 // extension id |
71 const std::string& extension_id() const { return extension_id_; } | 62 const std::string& extension_id() const { return extension_id_; } |
72 | 63 |
73 // What kind of action is this? | 64 // What kind of action is this? |
74 extensions::ActionInfo::Type action_type() const { | 65 extensions::ActionInfo::Type action_type() const { |
75 return action_type_; | 66 return action_type_; |
76 } | 67 } |
77 | 68 |
78 // action id -- only used with legacy page actions API | 69 // action id -- only used with legacy page actions API |
79 std::string id() const { return id_; } | 70 std::string id() const { return id_; } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 105 |
115 // Gets the icon that has been set using |SetIcon| for the tab. | 106 // Gets the icon that has been set using |SetIcon| for the tab. |
116 gfx::ImageSkia GetExplicitlySetIcon(int tab_id) const; | 107 gfx::ImageSkia GetExplicitlySetIcon(int tab_id) const; |
117 | 108 |
118 // Sets the icon for a tab, in a way that can't be read by the extension's | 109 // Sets the icon for a tab, in a way that can't be read by the extension's |
119 // javascript. Multiple icons can be set at the same time; some icon with the | 110 // javascript. Multiple icons can be set at the same time; some icon with the |
120 // highest priority will be used. | 111 // highest priority will be used. |
121 void DeclarativeSetIcon(int tab_id, int priority, const gfx::Image& icon); | 112 void DeclarativeSetIcon(int tab_id, int priority, const gfx::Image& icon); |
122 void UndoDeclarativeSetIcon(int tab_id, int priority, const gfx::Image& icon); | 113 void UndoDeclarativeSetIcon(int tab_id, int priority, const gfx::Image& icon); |
123 | 114 |
124 // Non-tab-specific icon path. This is used to support the default_icon key of | |
125 // page and browser actions. | |
126 void set_default_icon(scoped_ptr<ExtensionIconSet> icon_set) { | |
127 default_icon_ = icon_set.Pass(); | |
128 } | |
129 | |
130 const ExtensionIconSet* default_icon() const { | 115 const ExtensionIconSet* default_icon() const { |
131 return default_icon_.get(); | 116 return default_icon_.get(); |
132 } | 117 } |
133 | 118 |
134 // Set this action's badge text on a specific tab. | 119 // Set this action's badge text on a specific tab. |
135 void SetBadgeText(int tab_id, const std::string& text) { | 120 void SetBadgeText(int tab_id, const std::string& text) { |
136 SetValue(&badge_text_, tab_id, text); | 121 SetValue(&badge_text_, tab_id, text); |
137 } | 122 } |
138 // Get the badge text for a tab, or the default if no badge text was set. | 123 // Get the badge text for a tab, or the default if no badge text was set. |
139 std::string GetBadgeText(int tab_id) const { | 124 std::string GetBadgeText(int tab_id) const { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 void ClearAllValuesForTab(int tab_id); | 180 void ClearAllValuesForTab(int tab_id); |
196 | 181 |
197 // If the specified tab has a badge, paint it into the provided bounds. | 182 // If the specified tab has a badge, paint it into the provided bounds. |
198 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); | 183 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); |
199 | 184 |
200 // Returns icon image with badge for specified tab. | 185 // Returns icon image with badge for specified tab. |
201 gfx::ImageSkia GetIconWithBadge(const gfx::ImageSkia& icon, | 186 gfx::ImageSkia GetIconWithBadge(const gfx::ImageSkia& icon, |
202 int tab_id, | 187 int tab_id, |
203 const gfx::Size& spacing) const; | 188 const gfx::Size& spacing) const; |
204 | 189 |
| 190 // Lazily loads and returns the default icon image, if one exists for the |
| 191 // action. |
| 192 extensions::IconImage* LoadDefaultIconImage( |
| 193 const extensions::Extension& extension, |
| 194 content::BrowserContext* browser_context); |
| 195 |
| 196 // Returns the image to use as the default icon for the action. Can only be |
| 197 // called after LoadDefaultIconImage(). |
| 198 gfx::ImageSkia GetDefaultIconImage() const; |
| 199 |
205 // Determine whether or not the ExtensionAction has a value set for the given | 200 // Determine whether or not the ExtensionAction has a value set for the given |
206 // |tab_id| for each property. | 201 // |tab_id| for each property. |
207 bool HasPopupUrl(int tab_id) const; | 202 bool HasPopupUrl(int tab_id) const; |
208 bool HasTitle(int tab_id) const; | 203 bool HasTitle(int tab_id) const; |
209 bool HasBadgeText(int tab_id) const; | 204 bool HasBadgeText(int tab_id) const; |
210 bool HasBadgeBackgroundColor(int tab_id) const; | 205 bool HasBadgeBackgroundColor(int tab_id) const; |
211 bool HasBadgeTextColor(int tab_id) const; | 206 bool HasBadgeTextColor(int tab_id) const; |
212 bool HasIsVisible(int tab_id) const; | 207 bool HasIsVisible(int tab_id) const; |
213 bool HasIcon(int tab_id) const; | 208 bool HasIcon(int tab_id) const; |
214 | 209 |
| 210 void SetDefaultIconForTest(scoped_ptr<ExtensionIconSet> default_icon); |
| 211 |
215 private: | 212 private: |
| 213 // Populates the action from the |extension| and |manifest_data|, filling in |
| 214 // any missing values (like title or icons) as possible. |
| 215 void Populate(const extensions::Extension& extension, |
| 216 const extensions::ActionInfo& manifest_data); |
| 217 |
216 // Returns width of the current icon for tab_id. | 218 // Returns width of the current icon for tab_id. |
217 // TODO(tbarzic): The icon selection is done in ExtensionActionIconFactory. | 219 // TODO(tbarzic): The icon selection is done in ExtensionActionIconFactory. |
218 // We should probably move this there too. | 220 // We should probably move this there too. |
219 int GetIconWidth(int tab_id) const; | 221 int GetIconWidth(int tab_id) const; |
220 | 222 |
221 template <class T> | 223 template <class T> |
222 struct ValueTraits { | 224 struct ValueTraits { |
223 static T CreateEmpty() { | 225 static T CreateEmpty() { |
224 return T(); | 226 return T(); |
225 } | 227 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 // Maps tab_id to the number of active (applied-but-not-reverted) | 279 // Maps tab_id to the number of active (applied-but-not-reverted) |
278 // declarativeContent.ShowPageAction actions. | 280 // declarativeContent.ShowPageAction actions. |
279 std::map<int, int> declarative_show_count_; | 281 std::map<int, int> declarative_show_count_; |
280 | 282 |
281 // declarative_icon_[tab_id][declarative_rule_priority] is a vector of icon | 283 // declarative_icon_[tab_id][declarative_rule_priority] is a vector of icon |
282 // images that are currently in effect | 284 // images that are currently in effect |
283 std::map<int, std::map<int, std::vector<gfx::Image> > > declarative_icon_; | 285 std::map<int, std::map<int, std::vector<gfx::Image> > > declarative_icon_; |
284 | 286 |
285 // ExtensionIconSet containing paths to bitmaps from which default icon's | 287 // ExtensionIconSet containing paths to bitmaps from which default icon's |
286 // image representations will be selected. | 288 // image representations will be selected. |
287 scoped_ptr<const ExtensionIconSet> default_icon_; | 289 scoped_ptr<ExtensionIconSet> default_icon_; |
| 290 |
| 291 // The default icon image, if |default_icon_| exists. |
| 292 // Lazily initialized via LoadDefaultIconImage(). |
| 293 scoped_ptr<extensions::IconImage> default_icon_image_; |
288 | 294 |
289 // The id for the ExtensionAction, for example: "RssPageAction". This is | 295 // The id for the ExtensionAction, for example: "RssPageAction". This is |
290 // needed for compat with an older version of the page actions API. | 296 // needed for compat with an older version of the page actions API. |
291 std::string id_; | 297 std::string id_; |
292 | 298 |
293 DISALLOW_COPY_AND_ASSIGN(ExtensionAction); | 299 DISALLOW_COPY_AND_ASSIGN(ExtensionAction); |
294 }; | 300 }; |
295 | 301 |
296 template<> | 302 template<> |
297 struct ExtensionAction::ValueTraits<int> { | 303 struct ExtensionAction::ValueTraits<int> { |
298 static int CreateEmpty() { | 304 static int CreateEmpty() { |
299 return -1; | 305 return -1; |
300 } | 306 } |
301 }; | 307 }; |
302 | 308 |
303 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_H_ | 309 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTION_H_ |
OLD | NEW |