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/scoped_vector.h" |
15 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
16 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
| 18 #include "chrome/common/extensions/extension_icon_factory_delegate.h" |
| 19 #include "chrome/common/extensions/extension_icon_set.h" |
17 #include "third_party/skia/include/core/SkColor.h" | 20 #include "third_party/skia/include/core/SkColor.h" |
18 #include "ui/base/animation/linear_animation.h" | 21 #include "ui/base/animation/linear_animation.h" |
19 | 22 |
20 class GURL; | 23 class GURL; |
21 class SkBitmap; | 24 class SkBitmap; |
22 class SkDevice; | 25 class SkDevice; |
23 | 26 |
24 namespace gfx { | 27 namespace gfx { |
25 class Canvas; | 28 class Canvas; |
26 class Image; | 29 class Image; |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 const std::string& extension_id() const { return extension_id_; } | 130 const std::string& extension_id() const { return extension_id_; } |
128 | 131 |
129 // What kind of action is this? | 132 // What kind of action is this? |
130 Type action_type() const { return action_type_; } | 133 Type action_type() const { return action_type_; } |
131 | 134 |
132 // action id -- only used with legacy page actions API | 135 // action id -- only used with legacy page actions API |
133 std::string id() const { return id_; } | 136 std::string id() const { return id_; } |
134 void set_id(const std::string& id) { id_ = id; } | 137 void set_id(const std::string& id) { id_ = id; } |
135 | 138 |
136 // static icon paths from manifest -- only used with legacy page actions API. | 139 // static icon paths from manifest -- only used with legacy page actions API. |
137 std::vector<std::string>* icon_paths() { return &icon_paths_; } | 140 const std::vector<const ExtensionIconSet*>& page_action_icons() const { |
138 const std::vector<std::string>* icon_paths() const { return &icon_paths_; } | 141 return page_action_icons_.get(); |
| 142 } |
| 143 |
| 144 // Adds an IconSet for a new page action icon. |
| 145 void AddPageActionIcon(scoped_ptr<ExtensionIconSet> icon_set); |
| 146 |
| 147 // Checks if specified icon index is valid. |
| 148 bool IsValidIconIndex(int index) const; |
139 | 149 |
140 bool has_changed() const { return has_changed_; } | 150 bool has_changed() const { return has_changed_; } |
141 void set_has_changed(bool value) { has_changed_ = value; } | 151 void set_has_changed(bool value) { has_changed_ = value; } |
142 | 152 |
143 // Set the url which the popup will load when the user clicks this action's | 153 // Set the url which the popup will load when the user clicks this action's |
144 // icon. Setting an empty URL will disable the popup for a given tab. | 154 // icon. Setting an empty URL will disable the popup for a given tab. |
145 void SetPopupUrl(int tab_id, const GURL& url); | 155 void SetPopupUrl(int tab_id, const GURL& url); |
146 | 156 |
147 // Use HasPopup() to see if a popup should be displayed. | 157 // Use HasPopup() to see if a popup should be displayed. |
148 bool HasPopup(int tab_id) const; | 158 bool HasPopup(int tab_id) const; |
149 | 159 |
150 // Get the URL to display in a popup. | 160 // Get the URL to display in a popup. |
151 GURL GetPopupUrl(int tab_id) const; | 161 GURL GetPopupUrl(int tab_id) const; |
152 | 162 |
153 // Set this action's title on a specific tab. | 163 // Set this action's title on a specific tab. |
154 void SetTitle(int tab_id, const std::string& title) { | 164 void SetTitle(int tab_id, const std::string& title) { |
155 SetValue(&title_, tab_id, title); | 165 SetValue(&title_, tab_id, title); |
156 } | 166 } |
157 | 167 |
158 // If tab |tab_id| has a set title, return it. Otherwise, return | 168 // If tab |tab_id| has a set title, return it. Otherwise, return |
159 // the default title. | 169 // the default title. |
160 std::string GetTitle(int tab_id) const { return GetValue(&title_, tab_id); } | 170 std::string GetTitle(int tab_id) const { return GetValue(&title_, tab_id); } |
161 | 171 |
162 // Icons are a bit different because the default value can be set to either a | 172 // Icons are a bit different because the default value can be set to either a |
163 // bitmap or a path. However, conceptually, there is only one default icon. | 173 // bitmap or a path. However, conceptually, there is only one default icon. |
164 // Setting the default icon using a path clears the bitmap and vice-versa. | 174 // Setting the default icon using a path clears the bitmap and vice-versa. |
165 | 175 |
166 // Since ExtensionAction, living in common/, can't interact with the browser | |
167 // to load images, the UI code needs to load the images for each path. For | |
168 // each path in default_icon_path() and icon_paths(), load the image there | |
169 // using an ImageLoadingTracker and call CacheIcon(path, image) with the | |
170 // result. | |
171 // | |
172 // If an image is cached redundantly, the first load will be used. | |
173 void CacheIcon(const std::string& path, const gfx::Image& icon); | |
174 | |
175 // Set this action's icon bitmap on a specific tab. | 176 // Set this action's icon bitmap on a specific tab. |
176 void SetIcon(int tab_id, const gfx::Image& image); | 177 void SetIcon(int tab_id, const gfx::Image& image); |
177 | 178 |
178 // Get the icon for a tab, or the default if no icon was set for this tab, | 179 // Get the icon for a tab. If the default icon or pare action icon has to be |
179 // retrieving icons that have been specified by path from the previous | 180 // returned, |icon_factory| will be used to get the actual icon. |
180 // arguments to CacheIcon(). If the default icon isn't found in the cache, | 181 // |icon_factory| is used because the icon has to be loaded using |
181 // returns the puzzle piece icon. | 182 // extensions::IconImage, which cannot be done from chrome/common/. The |
182 gfx::Image GetIcon(int tab_id) const; | 183 // factory implementation should handle asynchronous icon loading. |
| 184 // If no icon is specified for the tab, returns the puzzle piece icon. |
| 185 gfx::Image GetIcon(int tab_id, |
| 186 ExtensionIconFactoryDelegate* icon_factory) const; |
183 | 187 |
184 // Gets the icon that has been set using |SetIcon| for the tab. | 188 // Gets the icon that has been set using |SetIcon| for the tab. |
185 gfx::ImageSkia GetExplicitlySetIcon(int tab_id) const; | 189 gfx::ImageSkia GetExplicitlySetIcon(int tab_id) const; |
186 | 190 |
187 // Set this action's icon index for a specific tab. For use with | 191 // Set this action's icon index for a specific tab. For use with |
188 // icon_paths(), only used in page actions. | 192 // icon_paths(), only used in page actions. |
189 void SetIconIndex(int tab_id, int index); | 193 void SetIconIndex(int tab_id, int index); |
190 | 194 |
191 // Get this action's icon index for a tab, or the default if no icon index | 195 // Get this action's icon index for a tab, or the default if no icon index |
192 // was set. | 196 // was set. |
193 int GetIconIndex(int tab_id) const { | 197 int GetIconIndex(int tab_id) const { |
194 return GetValue(&icon_index_, tab_id); | 198 return GetValue(&icon_index_, tab_id); |
195 } | 199 } |
196 | 200 |
197 // Non-tab-specific icon path. This is used to support the default_icon key of | 201 // Non-tab-specific icon path. This is used to support the default_icon key of |
198 // page and browser actions. | 202 // page and browser actions. |
199 void set_default_icon_path(const std::string& path) { | 203 void set_default_icon(scoped_ptr<ExtensionIconSet> icon_set) { |
200 default_icon_path_ = path; | 204 default_icon_ = icon_set.Pass(); |
201 } | 205 } |
202 const std::string& default_icon_path() const { | 206 |
203 return default_icon_path_; | 207 const ExtensionIconSet* default_icon() const { |
| 208 return default_icon_.get(); |
204 } | 209 } |
205 | 210 |
206 // Set this action's badge text on a specific tab. | 211 // Set this action's badge text on a specific tab. |
207 void SetBadgeText(int tab_id, const std::string& text) { | 212 void SetBadgeText(int tab_id, const std::string& text) { |
208 SetValue(&badge_text_, tab_id, text); | 213 SetValue(&badge_text_, tab_id, text); |
209 } | 214 } |
210 // Get the badge text for a tab, or the default if no badge text was set. | 215 // Get the badge text for a tab, or the default if no badge text was set. |
211 std::string GetBadgeText(int tab_id) const { | 216 std::string GetBadgeText(int tab_id) const { |
212 return GetValue(&badge_text_, tab_id); | 217 return GetValue(&badge_text_, tab_id); |
213 } | 218 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 class IconWithBadgeImageSource; | 266 class IconWithBadgeImageSource; |
262 | 267 |
263 // Runs an animation on a tab. | 268 // Runs an animation on a tab. |
264 void RunIconAnimation(int tab_id); | 269 void RunIconAnimation(int tab_id); |
265 | 270 |
266 // If the icon animation is running on tab |tab_id|, applies it to | 271 // If the icon animation is running on tab |tab_id|, applies it to |
267 // |orig| and returns the result. Otherwise, just returns |orig|. | 272 // |orig| and returns the result. Otherwise, just returns |orig|. |
268 gfx::ImageSkia ApplyIconAnimation(int tab_id, | 273 gfx::ImageSkia ApplyIconAnimation(int tab_id, |
269 const gfx::ImageSkia& orig) const; | 274 const gfx::ImageSkia& orig) const; |
270 | 275 |
| 276 // Returns width of the current icon for tab_id. |
| 277 int GetIconWidth(int tab_id) const; |
| 278 |
271 // Paints badge with specified parameters to |canvas|. | 279 // Paints badge with specified parameters to |canvas|. |
272 static void DoPaintBadge(gfx::Canvas* canvas, | 280 static void DoPaintBadge(gfx::Canvas* canvas, |
273 const gfx::Rect& bounds, | 281 const gfx::Rect& bounds, |
274 const std::string& text, | 282 const std::string& text, |
275 const SkColor& text_color_in, | 283 const SkColor& text_color_in, |
276 const SkColor& background_color_in, | 284 const SkColor& background_color_in, |
277 int icon_width); | 285 int icon_width); |
278 | 286 |
279 template <class T> | 287 template <class T> |
280 struct ValueTraits { | 288 struct ValueTraits { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 std::map<int, SkColor> badge_text_color_; | 324 std::map<int, SkColor> badge_text_color_; |
317 std::map<int, Appearance> appearance_; | 325 std::map<int, Appearance> appearance_; |
318 | 326 |
319 // IconAnimations are destroyed by a delayed task on the UI message loop so | 327 // IconAnimations are destroyed by a delayed task on the UI message loop so |
320 // that even if the Extension and ExtensionAction are destroyed on a non-UI | 328 // that even if the Extension and ExtensionAction are destroyed on a non-UI |
321 // thread, the animation will still only be touched from the UI thread. This | 329 // thread, the animation will still only be touched from the UI thread. This |
322 // causes the WeakPtr in this map to become NULL. GetIconAnimation() removes | 330 // causes the WeakPtr in this map to become NULL. GetIconAnimation() removes |
323 // NULLs to prevent the map from growing without bound. | 331 // NULLs to prevent the map from growing without bound. |
324 mutable std::map<int, base::WeakPtr<IconAnimation> > icon_animation_; | 332 mutable std::map<int, base::WeakPtr<IconAnimation> > icon_animation_; |
325 | 333 |
326 std::string default_icon_path_; | 334 // ExtensionIconSet containing paths to bitmaps from which default icon's |
| 335 // image representations will be selected. |
| 336 scoped_ptr<const ExtensionIconSet> default_icon_; |
| 337 |
| 338 // ExtensionIconSets with icon image representations for deprecated page |
| 339 // action icons. |
| 340 // ExtensionIconSet containing paths to bitmaps from which deprecated page |
| 341 // action icons' image representations will be selected. |
| 342 ScopedVector<const ExtensionIconSet> page_action_icons_; |
327 | 343 |
328 // The id for the ExtensionAction, for example: "RssPageAction". This is | 344 // The id for the ExtensionAction, for example: "RssPageAction". This is |
329 // needed for compat with an older version of the page actions API. | 345 // needed for compat with an older version of the page actions API. |
330 std::string id_; | 346 std::string id_; |
331 | 347 |
332 // A list of paths to icons this action might show. This is needed to support | |
333 // the legacy setIcon({iconIndex:...} method of the page actions API. | |
334 std::vector<std::string> icon_paths_; | |
335 | |
336 // Saves the arguments from CacheIcon() calls. | |
337 std::map<std::string, gfx::ImageSkia> path_to_icon_cache_; | |
338 | |
339 // True if the ExtensionAction's settings have changed from what was | 348 // True if the ExtensionAction's settings have changed from what was |
340 // specified in the manifest. | 349 // specified in the manifest. |
341 bool has_changed_; | 350 bool has_changed_; |
342 | 351 |
343 DISALLOW_COPY_AND_ASSIGN(ExtensionAction); | 352 DISALLOW_COPY_AND_ASSIGN(ExtensionAction); |
344 }; | 353 }; |
345 | 354 |
346 template<> | 355 template<> |
347 struct ExtensionAction::ValueTraits<int> { | 356 struct ExtensionAction::ValueTraits<int> { |
348 static int CreateEmpty() { | 357 static int CreateEmpty() { |
349 return -1; | 358 return -1; |
350 } | 359 } |
351 }; | 360 }; |
352 | 361 |
353 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ | 362 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ |
OLD | NEW |