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 Rect; | 27 class Rect; |
27 } | 28 } |
28 | 29 |
29 // ExtensionAction encapsulates the state of a browser action, page action, or | 30 // ExtensionAction encapsulates the state of a browser action, page action, or |
30 // script badge. | 31 // script badge. |
31 // 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 |
32 // a per-tab value, the global value is used instead. | 33 // a per-tab value, the global value is used instead. |
33 class ExtensionAction { | 34 class ExtensionAction { |
34 public: | 35 public: |
35 // 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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 | 113 |
113 // What kind of action is this? | 114 // What kind of action is this? |
114 Type action_type() const { return action_type_; } | 115 Type action_type() const { return action_type_; } |
115 | 116 |
116 // action id -- only used with legacy page actions API | 117 // action id -- only used with legacy page actions API |
117 std::string id() const { return id_; } | 118 std::string id() const { return id_; } |
118 void set_id(const std::string& id) { id_ = id; } | 119 void set_id(const std::string& id) { id_ = id; } |
119 | 120 |
120 // static icon paths from manifest -- only used with legacy page actions API. | 121 // static icon paths from manifest -- only used with legacy page actions API. |
121 std::vector<std::string>* icon_paths() { return &icon_paths_; } | 122 std::vector<std::string>* icon_paths() { return &icon_paths_; } |
123 const std::vector<std::string>* icon_paths() const { return &icon_paths_; } | |
122 | 124 |
123 // Set the url which the popup will load when the user clicks this action's | 125 // Set the url which the popup will load when the user clicks this action's |
124 // icon. Setting an empty URL will disable the popup for a given tab. | 126 // icon. Setting an empty URL will disable the popup for a given tab. |
125 void SetPopupUrl(int tab_id, const GURL& url); | 127 void SetPopupUrl(int tab_id, const GURL& url); |
126 | 128 |
127 // Use HasPopup() to see if a popup should be displayed. | 129 // Use HasPopup() to see if a popup should be displayed. |
128 bool HasPopup(int tab_id) const; | 130 bool HasPopup(int tab_id) const; |
129 | 131 |
130 // Get the URL to display in a popup. | 132 // Get the URL to display in a popup. |
131 GURL GetPopupUrl(int tab_id) const; | 133 GURL GetPopupUrl(int tab_id) const; |
(...skipping 10 matching lines...) Expand all Loading... | |
142 // Icons are a bit different because the default value can be set to either a | 144 // Icons are a bit different because the default value can be set to either a |
143 // bitmap or a path. However, conceptually, there is only one default icon. | 145 // bitmap or a path. However, conceptually, there is only one default icon. |
144 // Setting the default icon using a path clears the bitmap and vice-versa. | 146 // Setting the default icon using a path clears the bitmap and vice-versa. |
145 // | 147 // |
146 // To get the default icon, first check for the bitmap. If it is null, check | 148 // To get the default icon, first check for the bitmap. If it is null, check |
147 // for the path. | 149 // for the path. |
148 | 150 |
149 // Set this action's icon bitmap on a specific tab. | 151 // Set this action's icon bitmap on a specific tab. |
150 void SetIcon(int tab_id, const SkBitmap& bitmap); | 152 void SetIcon(int tab_id, const SkBitmap& bitmap); |
151 | 153 |
152 // Get the icon for a tab, or the default if no icon was set. | 154 // Maps paths to icons since ExtensionAction, living in common/, can't |
153 SkBitmap GetIcon(int tab_id) const; | 155 // interact with the browser to load them itself. Pre-populate this cache |
156 // with images from icon_paths() and default_icon_path(). | |
157 typedef std::map<std::string, gfx::Image> PathToIconCache; | |
158 | |
not at google - send to devlin
2012/07/27 03:16:12
I meant like
class ExtensionAction {
public:
c
Jeffrey Yasskin
2012/07/31 14:40:01
I've done this one.
| |
159 // Get the icon for a tab, or the default if no icon was set for this tab, | |
160 // retrieving icons that have been specified by path from 'cache'. If the | |
161 // default icon isn't found in 'cache', returns the puzzle piece icon. | |
162 gfx::Image GetIcon(int tab_id, const PathToIconCache& cache) const; | |
154 | 163 |
155 // Set this action's icon index for a specific tab. For use with | 164 // Set this action's icon index for a specific tab. For use with |
156 // icon_paths(), only used in page actions. | 165 // icon_paths(), only used in page actions. |
157 void SetIconIndex(int tab_id, int index); | 166 void SetIconIndex(int tab_id, int index); |
158 | 167 |
159 // Get this action's icon index for a tab, or the default if no icon index | 168 // Get this action's icon index for a tab, or the default if no icon index |
160 // was set. | 169 // was set. |
161 int GetIconIndex(int tab_id) const { | 170 int GetIconIndex(int tab_id) const { |
162 return GetValue(&icon_index_, tab_id); | 171 return GetValue(&icon_index_, tab_id); |
163 } | 172 } |
164 | 173 |
165 // Non-tab-specific icon path. This is used to support the default_icon key of | 174 // Non-tab-specific icon path. This is used to support the default_icon key of |
166 // page and browser actions. | 175 // page and browser actions. |
167 void set_default_icon_path(const std::string& path) { | 176 void set_default_icon_path(const std::string& path) { |
168 default_icon_path_ = path; | 177 default_icon_path_ = path; |
169 } | 178 } |
170 std::string default_icon_path() const { | 179 const std::string& default_icon_path() const { |
171 return default_icon_path_; | 180 return default_icon_path_; |
172 } | 181 } |
173 | 182 |
174 // Set this action's badge text on a specific tab. | 183 // Set this action's badge text on a specific tab. |
175 void SetBadgeText(int tab_id, const std::string& text) { | 184 void SetBadgeText(int tab_id, const std::string& text) { |
176 SetValue(&badge_text_, tab_id, text); | 185 SetValue(&badge_text_, tab_id, text); |
177 } | 186 } |
178 // Get the badge text for a tab, or the default if no badge text was set. | 187 // Get the badge text for a tab, or the default if no badge text was set. |
179 std::string GetBadgeText(int tab_id) const { | 188 std::string GetBadgeText(int tab_id) const { |
180 return GetValue(&badge_text_, tab_id); | 189 return GetValue(&badge_text_, tab_id); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); | 226 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); |
218 | 227 |
219 // Gets a weak reference to the icon animation for a tab, if any. The | 228 // Gets a weak reference to the icon animation for a tab, if any. The |
220 // reference will only have a value while the animation is running. | 229 // reference will only have a value while the animation is running. |
221 base::WeakPtr<IconAnimation> GetIconAnimation(int tab_id) const; | 230 base::WeakPtr<IconAnimation> GetIconAnimation(int tab_id) const; |
222 | 231 |
223 // Runs an animation on a tab. | 232 // Runs an animation on a tab. |
224 void RunIconAnimation(int tab_id); | 233 void RunIconAnimation(int tab_id); |
225 | 234 |
226 private: | 235 private: |
236 // If the icon animation is running on tab |tab_id|, applies it to | |
237 // |orig| and returns the result. Otherwise, just returns |orig|. | |
238 gfx::Image ApplyIconAnimation(int tab_id, const gfx::Image& orig) const; | |
239 | |
227 template <class T> | 240 template <class T> |
228 struct ValueTraits { | 241 struct ValueTraits { |
229 static T CreateEmpty() { | 242 static T CreateEmpty() { |
230 return T(); | 243 return T(); |
231 } | 244 } |
232 }; | 245 }; |
233 | 246 |
234 template<class T> | 247 template<class T> |
235 void SetValue(std::map<int, T>* map, int tab_id, const T& val) { | 248 void SetValue(std::map<int, T>* map, int tab_id, const T& val) { |
236 (*map)[tab_id] = val; | 249 (*map)[tab_id] = val; |
(...skipping 13 matching lines...) Expand all Loading... | |
250 // The id for the extension this action belongs to (as defined in the | 263 // The id for the extension this action belongs to (as defined in the |
251 // extension manifest). | 264 // extension manifest). |
252 const std::string extension_id_; | 265 const std::string extension_id_; |
253 | 266 |
254 const Type action_type_; | 267 const Type action_type_; |
255 | 268 |
256 // Each of these data items can have both a global state (stored with the key | 269 // Each of these data items can have both a global state (stored with the key |
257 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). | 270 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). |
258 std::map<int, GURL> popup_url_; | 271 std::map<int, GURL> popup_url_; |
259 std::map<int, std::string> title_; | 272 std::map<int, std::string> title_; |
260 std::map<int, SkBitmap> icon_; | 273 std::map<int, gfx::Image> icon_; |
261 std::map<int, int> icon_index_; // index into icon_paths_ | 274 std::map<int, int> icon_index_; // index into icon_paths_ |
262 std::map<int, std::string> badge_text_; | 275 std::map<int, std::string> badge_text_; |
263 std::map<int, SkColor> badge_background_color_; | 276 std::map<int, SkColor> badge_background_color_; |
264 std::map<int, SkColor> badge_text_color_; | 277 std::map<int, SkColor> badge_text_color_; |
265 std::map<int, bool> visible_; | 278 std::map<int, bool> visible_; |
266 | 279 |
267 class IconAnimationWrapper; | 280 class IconAnimationWrapper; |
268 std::map<int, linked_ptr<IconAnimationWrapper> > icon_animation_; | 281 std::map<int, linked_ptr<IconAnimationWrapper> > icon_animation_; |
269 | 282 |
270 std::string default_icon_path_; | 283 std::string default_icon_path_; |
(...skipping 10 matching lines...) Expand all Loading... | |
281 }; | 294 }; |
282 | 295 |
283 template<> | 296 template<> |
284 struct ExtensionAction::ValueTraits<int> { | 297 struct ExtensionAction::ValueTraits<int> { |
285 static int CreateEmpty() { | 298 static int CreateEmpty() { |
286 return -1; | 299 return -1; |
287 } | 300 } |
288 }; | 301 }; |
289 | 302 |
290 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ | 303 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ |
OLD | NEW |