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_set.h" |
17 #include "third_party/skia/include/core/SkColor.h" | 19 #include "third_party/skia/include/core/SkColor.h" |
18 #include "ui/base/animation/linear_animation.h" | 20 #include "ui/base/animation/linear_animation.h" |
19 | 21 |
20 class GURL; | 22 class GURL; |
21 class SkBitmap; | 23 class SkBitmap; |
22 class SkDevice; | 24 class SkDevice; |
23 | 25 |
24 namespace gfx { | 26 namespace gfx { |
25 class Canvas; | 27 class Canvas; |
26 class Image; | 28 class Image; |
(...skipping 22 matching lines...) Expand all Loading... |
49 enum Appearance { | 51 enum Appearance { |
50 // The action icon is hidden. | 52 // The action icon is hidden. |
51 INVISIBLE, | 53 INVISIBLE, |
52 // The action is trying to get the user's attention but isn't yet | 54 // The action is trying to get the user's attention but isn't yet |
53 // running on the page. Currently only used for script badges. | 55 // running on the page. Currently only used for script badges. |
54 WANTS_ATTENTION, | 56 WANTS_ATTENTION, |
55 // The action icon is visible with its normal appearance. | 57 // The action icon is visible with its normal appearance. |
56 ACTIVE, | 58 ACTIVE, |
57 }; | 59 }; |
58 | 60 |
| 61 // Since ExtensionAction, living in common/, can't interact with the browser |
| 62 // to load images, the UI code will have to load the images for each path for |
| 63 // us by implementing this interface and pass it to |GetIcon| method. |
| 64 // Whenever icon to return is defined by its path (stored in ExtensionIconSet) |
| 65 // We will call Icon::Factory::GetIcon to get actual icon. |
| 66 class IconFactory { |
| 67 public: |
| 68 virtual ~IconFactory() {} |
| 69 |
| 70 virtual gfx::ImageSkia GetIcon(const ExtensionIconSet* paths, |
| 71 Type action_type) = 0; |
| 72 }; |
| 73 |
59 // A fade-in animation. | 74 // A fade-in animation. |
60 class IconAnimation : public ui::LinearAnimation, | 75 class IconAnimation : public ui::LinearAnimation, |
61 public base::SupportsWeakPtr<IconAnimation> { | 76 public base::SupportsWeakPtr<IconAnimation> { |
62 public: | 77 public: |
63 // Observes changes to icon animation state. | 78 // Observes changes to icon animation state. |
64 class Observer { | 79 class Observer { |
65 public: | 80 public: |
66 virtual void OnIconChanged(const IconAnimation& animation) = 0; | 81 virtual void OnIconChanged(const IconAnimation& animation) = 0; |
67 | 82 |
68 protected: | 83 protected: |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 // extension id | 138 // extension id |
124 const std::string& extension_id() const { return extension_id_; } | 139 const std::string& extension_id() const { return extension_id_; } |
125 | 140 |
126 // What kind of action is this? | 141 // What kind of action is this? |
127 Type action_type() const { return action_type_; } | 142 Type action_type() const { return action_type_; } |
128 | 143 |
129 // action id -- only used with legacy page actions API | 144 // action id -- only used with legacy page actions API |
130 std::string id() const { return id_; } | 145 std::string id() const { return id_; } |
131 void set_id(const std::string& id) { id_ = id; } | 146 void set_id(const std::string& id) { id_ = id; } |
132 | 147 |
133 // static icon paths from manifest -- only used with legacy page actions API. | 148 const std::vector<const ExtensionIconSet*>& page_action_icons() const { |
134 std::vector<std::string>* icon_paths() { return &icon_paths_; } | 149 return page_action_icons_.get(); |
135 const std::vector<std::string>* icon_paths() const { return &icon_paths_; } | 150 } |
| 151 |
| 152 // Adds an IconSet for a new page action icon. |
| 153 void AddPageActionIcon(const ExtensionIconSet* icon_set); |
| 154 |
| 155 // Checks if specified icon index is valid. |
| 156 bool IsValidIconIndex(int index) const; |
136 | 157 |
137 bool has_changed() const { return has_changed_; } | 158 bool has_changed() const { return has_changed_; } |
138 void set_has_changed(bool value) { has_changed_ = value; } | 159 void set_has_changed(bool value) { has_changed_ = value; } |
139 | 160 |
140 // Set the url which the popup will load when the user clicks this action's | 161 // Set the url which the popup will load when the user clicks this action's |
141 // icon. Setting an empty URL will disable the popup for a given tab. | 162 // icon. Setting an empty URL will disable the popup for a given tab. |
142 void SetPopupUrl(int tab_id, const GURL& url); | 163 void SetPopupUrl(int tab_id, const GURL& url); |
143 | 164 |
144 // Use HasPopup() to see if a popup should be displayed. | 165 // Use HasPopup() to see if a popup should be displayed. |
145 bool HasPopup(int tab_id) const; | 166 bool HasPopup(int tab_id) const; |
146 | 167 |
147 // Get the URL to display in a popup. | 168 // Get the URL to display in a popup. |
148 GURL GetPopupUrl(int tab_id) const; | 169 GURL GetPopupUrl(int tab_id) const; |
149 | 170 |
150 // Set this action's title on a specific tab. | 171 // Set this action's title on a specific tab. |
151 void SetTitle(int tab_id, const std::string& title) { | 172 void SetTitle(int tab_id, const std::string& title) { |
152 SetValue(&title_, tab_id, title); | 173 SetValue(&title_, tab_id, title); |
153 } | 174 } |
154 | 175 |
155 // If tab |tab_id| has a set title, return it. Otherwise, return | 176 // If tab |tab_id| has a set title, return it. Otherwise, return |
156 // the default title. | 177 // the default title. |
157 std::string GetTitle(int tab_id) const { return GetValue(&title_, tab_id); } | 178 std::string GetTitle(int tab_id) const { return GetValue(&title_, tab_id); } |
158 | 179 |
159 // Icons are a bit different because the default value can be set to either a | 180 // Icons are a bit different because the default value can be set to either a |
160 // bitmap or a path. However, conceptually, there is only one default icon. | 181 // bitmap or a path. However, conceptually, there is only one default icon. |
161 // Setting the default icon using a path clears the bitmap and vice-versa. | 182 // Setting the default icon using a path clears the bitmap and vice-versa. |
162 | 183 |
163 // Since ExtensionAction, living in common/, can't interact with the browser | |
164 // to load images, the UI code needs to load the images for each path. For | |
165 // each path in default_icon_path() and icon_paths(), load the image there | |
166 // using an ImageLoadingTracker and call CacheIcon(path, image) with the | |
167 // result. | |
168 // | |
169 // If an image is cached redundantly, the first load will be used. | |
170 void CacheIcon(const std::string& path, const gfx::Image& icon); | |
171 | |
172 // Set this action's icon bitmap on a specific tab. | 184 // Set this action's icon bitmap on a specific tab. |
173 void SetIcon(int tab_id, const gfx::Image& image); | 185 void SetIcon(int tab_id, const gfx::Image& image); |
174 | 186 |
175 // Get the icon for a tab, or the default if no icon was set for this tab, | 187 // Get the icon for a tab. If icon to return is defined by its path, |
176 // retrieving icons that have been specified by path from the previous | 188 // |icon_factory| will be used to get the actual icon. |
177 // arguments to CacheIcon(). If the default icon isn't found in the cache, | 189 // If no icon is specified for the tab, returns the puzzle piece icon. |
178 // returns the puzzle piece icon. | 190 gfx::Image GetIcon(int tab_id, IconFactory* icon_factory) const; |
179 gfx::Image GetIcon(int tab_id) const; | |
180 | 191 |
181 // Set this action's icon index for a specific tab. For use with | 192 // Set this action's icon index for a specific tab. For use with |
182 // icon_paths(), only used in page actions. | 193 // icon_paths(), only used in page actions. |
183 void SetIconIndex(int tab_id, int index); | 194 void SetIconIndex(int tab_id, int index); |
184 | 195 |
185 // Get this action's icon index for a tab, or the default if no icon index | 196 // Get this action's icon index for a tab, or the default if no icon index |
186 // was set. | 197 // was set. |
187 int GetIconIndex(int tab_id) const { | 198 int GetIconIndex(int tab_id) const { |
188 return GetValue(&icon_index_, tab_id); | 199 return GetValue(&icon_index_, tab_id); |
189 } | 200 } |
190 | 201 |
191 // Non-tab-specific icon path. This is used to support the default_icon key of | 202 // Non-tab-specific icon path. This is used to support the default_icon key of |
192 // page and browser actions. | 203 // page and browser actions. |
193 void set_default_icon_path(const std::string& path) { | 204 void set_default_icon(const ExtensionIconSet* icon_set) { |
194 default_icon_path_ = path; | 205 default_icon_.reset(icon_set); |
195 } | 206 } |
196 const std::string& default_icon_path() const { | 207 |
197 return default_icon_path_; | 208 const ExtensionIconSet* default_icon() const { |
| 209 return default_icon_.get(); |
198 } | 210 } |
199 | 211 |
200 // Set this action's badge text on a specific tab. | 212 // Set this action's badge text on a specific tab. |
201 void SetBadgeText(int tab_id, const std::string& text) { | 213 void SetBadgeText(int tab_id, const std::string& text) { |
202 SetValue(&badge_text_, tab_id, text); | 214 SetValue(&badge_text_, tab_id, text); |
203 } | 215 } |
204 // Get the badge text for a tab, or the default if no badge text was set. | 216 // Get the badge text for a tab, or the default if no badge text was set. |
205 std::string GetBadgeText(int tab_id) const { | 217 std::string GetBadgeText(int tab_id) const { |
206 return GetValue(&badge_text_, tab_id); | 218 return GetValue(&badge_text_, tab_id); |
207 } | 219 } |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 std::map<int, Appearance> appearance_; | 329 std::map<int, Appearance> appearance_; |
318 | 330 |
319 // IconAnimationWrappers own themselves so that even if the Extension and | 331 // IconAnimationWrappers own themselves so that even if the Extension and |
320 // ExtensionAction are destroyed on a non-UI thread, the animation will still | 332 // ExtensionAction are destroyed on a non-UI thread, the animation will still |
321 // only be touched from the UI thread. When an animation finishes, it deletes | 333 // only be touched from the UI thread. When an animation finishes, it deletes |
322 // itself, which causes the WeakPtr in this map to become NULL. | 334 // itself, which causes the WeakPtr in this map to become NULL. |
323 // GetIconAnimationWrapper() removes NULLs to prevent the map from growing | 335 // GetIconAnimationWrapper() removes NULLs to prevent the map from growing |
324 // without bound. | 336 // without bound. |
325 mutable std::map<int, base::WeakPtr<IconAnimationWrapper> > icon_animation_; | 337 mutable std::map<int, base::WeakPtr<IconAnimationWrapper> > icon_animation_; |
326 | 338 |
327 std::string default_icon_path_; | 339 // ExtensionIconSet with icon image representations for default icon. |
| 340 scoped_ptr<const ExtensionIconSet> default_icon_; |
| 341 |
| 342 // ExtensionIconSets with icon image representations for deprecated page |
| 343 // action icons. |
| 344 ScopedVector<const ExtensionIconSet> page_action_icons_; |
328 | 345 |
329 // The id for the ExtensionAction, for example: "RssPageAction". This is | 346 // The id for the ExtensionAction, for example: "RssPageAction". This is |
330 // needed for compat with an older version of the page actions API. | 347 // needed for compat with an older version of the page actions API. |
331 std::string id_; | 348 std::string id_; |
332 | 349 |
333 // A list of paths to icons this action might show. This is needed to support | |
334 // the legacy setIcon({iconIndex:...} method of the page actions API. | |
335 std::vector<std::string> icon_paths_; | |
336 | |
337 // Saves the arguments from CacheIcon() calls. | |
338 std::map<std::string, gfx::ImageSkia> path_to_icon_cache_; | |
339 | |
340 // True if the ExtensionAction's settings have changed from what was | 350 // True if the ExtensionAction's settings have changed from what was |
341 // specified in the manifest. | 351 // specified in the manifest. |
342 bool has_changed_; | 352 bool has_changed_; |
343 | 353 |
344 DISALLOW_COPY_AND_ASSIGN(ExtensionAction); | 354 DISALLOW_COPY_AND_ASSIGN(ExtensionAction); |
345 }; | 355 }; |
346 | 356 |
347 template<> | 357 template<> |
348 struct ExtensionAction::ValueTraits<int> { | 358 struct ExtensionAction::ValueTraits<int> { |
349 static int CreateEmpty() { | 359 static int CreateEmpty() { |
350 return -1; | 360 return -1; |
351 } | 361 } |
352 }; | 362 }; |
353 | 363 |
354 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ | 364 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ |
OLD | NEW |