| 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; |
| 132 | 134 |
| 133 // Set this action's title on a specific tab. | 135 // Set this action's title on a specific tab. |
| 134 void SetTitle(int tab_id, const std::string& title) { | 136 void SetTitle(int tab_id, const std::string& title) { |
| 135 SetValue(&title_, tab_id, title); | 137 SetValue(&title_, tab_id, title); |
| 136 } | 138 } |
| 137 | 139 |
| 138 // If tab |tab_id| has a set title, return it. Otherwise, return | 140 // If tab |tab_id| has a set title, return it. Otherwise, return |
| 139 // the default title. | 141 // the default title. |
| 140 std::string GetTitle(int tab_id) const { return GetValue(&title_, tab_id); } | 142 std::string GetTitle(int tab_id) const { return GetValue(&title_, tab_id); } |
| 141 | 143 |
| 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. |
| 147 |
| 148 // Since ExtensionAction, living in common/, can't interact with the browser |
| 149 // to load images, the UI code needs to load the images for each path. For |
| 150 // each path in default_icon_path() and icon_paths(), load the image there |
| 151 // using an ImageLoadingTracker and call CacheIcon(path, image) with the |
| 152 // result. |
| 145 // | 153 // |
| 146 // To get the default icon, first check for the bitmap. If it is null, check | 154 // If an image is cached redundantly, the first load will be used. |
| 147 // for the path. | 155 void CacheIcon(const std::string& path, const gfx::Image& icon); |
| 148 | 156 |
| 149 // Set this action's icon bitmap on a specific tab. | 157 // Set this action's icon bitmap on a specific tab. |
| 150 void SetIcon(int tab_id, const SkBitmap& bitmap); | 158 void SetIcon(int tab_id, const SkBitmap& bitmap); |
| 151 | 159 |
| 152 // Get the icon for a tab, or the default if no icon was set. | 160 // Get the icon for a tab, or the default if no icon was set for this tab, |
| 153 SkBitmap GetIcon(int tab_id) const; | 161 // retrieving icons that have been specified by path from the previous |
| 162 // arguments to CacheIcon(). If the default icon isn't found in the cache, |
| 163 // returns the puzzle piece icon. |
| 164 gfx::Image GetIcon(int tab_id) const; |
| 154 | 165 |
| 155 // Set this action's icon index for a specific tab. For use with | 166 // Set this action's icon index for a specific tab. For use with |
| 156 // icon_paths(), only used in page actions. | 167 // icon_paths(), only used in page actions. |
| 157 void SetIconIndex(int tab_id, int index); | 168 void SetIconIndex(int tab_id, int index); |
| 158 | 169 |
| 159 // Get this action's icon index for a tab, or the default if no icon index | 170 // Get this action's icon index for a tab, or the default if no icon index |
| 160 // was set. | 171 // was set. |
| 161 int GetIconIndex(int tab_id) const { | 172 int GetIconIndex(int tab_id) const { |
| 162 return GetValue(&icon_index_, tab_id); | 173 return GetValue(&icon_index_, tab_id); |
| 163 } | 174 } |
| 164 | 175 |
| 165 // Non-tab-specific icon path. This is used to support the default_icon key of | 176 // Non-tab-specific icon path. This is used to support the default_icon key of |
| 166 // page and browser actions. | 177 // page and browser actions. |
| 167 void set_default_icon_path(const std::string& path) { | 178 void set_default_icon_path(const std::string& path) { |
| 168 default_icon_path_ = path; | 179 default_icon_path_ = path; |
| 169 } | 180 } |
| 170 std::string default_icon_path() const { | 181 const std::string& default_icon_path() const { |
| 171 return default_icon_path_; | 182 return default_icon_path_; |
| 172 } | 183 } |
| 173 | 184 |
| 174 // Set this action's badge text on a specific tab. | 185 // Set this action's badge text on a specific tab. |
| 175 void SetBadgeText(int tab_id, const std::string& text) { | 186 void SetBadgeText(int tab_id, const std::string& text) { |
| 176 SetValue(&badge_text_, tab_id, text); | 187 SetValue(&badge_text_, tab_id, text); |
| 177 } | 188 } |
| 178 // Get the badge text for a tab, or the default if no badge text was set. | 189 // Get the badge text for a tab, or the default if no badge text was set. |
| 179 std::string GetBadgeText(int tab_id) const { | 190 std::string GetBadgeText(int tab_id) const { |
| 180 return GetValue(&badge_text_, tab_id); | 191 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); | 228 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); |
| 218 | 229 |
| 219 // Gets a weak reference to the icon animation for a tab, if any. The | 230 // 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. | 231 // reference will only have a value while the animation is running. |
| 221 base::WeakPtr<IconAnimation> GetIconAnimation(int tab_id) const; | 232 base::WeakPtr<IconAnimation> GetIconAnimation(int tab_id) const; |
| 222 | 233 |
| 223 // Runs an animation on a tab. | 234 // Runs an animation on a tab. |
| 224 void RunIconAnimation(int tab_id); | 235 void RunIconAnimation(int tab_id); |
| 225 | 236 |
| 226 private: | 237 private: |
| 238 // If the icon animation is running on tab |tab_id|, applies it to |
| 239 // |orig| and returns the result. Otherwise, just returns |orig|. |
| 240 gfx::Image ApplyIconAnimation(int tab_id, const gfx::Image& orig) const; |
| 241 |
| 227 template <class T> | 242 template <class T> |
| 228 struct ValueTraits { | 243 struct ValueTraits { |
| 229 static T CreateEmpty() { | 244 static T CreateEmpty() { |
| 230 return T(); | 245 return T(); |
| 231 } | 246 } |
| 232 }; | 247 }; |
| 233 | 248 |
| 234 template<class T> | 249 template<class T> |
| 235 void SetValue(std::map<int, T>* map, int tab_id, const T& val) { | 250 void SetValue(std::map<int, T>* map, int tab_id, const T& val) { |
| 236 (*map)[tab_id] = val; | 251 (*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 | 265 // The id for the extension this action belongs to (as defined in the |
| 251 // extension manifest). | 266 // extension manifest). |
| 252 const std::string extension_id_; | 267 const std::string extension_id_; |
| 253 | 268 |
| 254 const Type action_type_; | 269 const Type action_type_; |
| 255 | 270 |
| 256 // Each of these data items can have both a global state (stored with the key | 271 // 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). | 272 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). |
| 258 std::map<int, GURL> popup_url_; | 273 std::map<int, GURL> popup_url_; |
| 259 std::map<int, std::string> title_; | 274 std::map<int, std::string> title_; |
| 260 std::map<int, SkBitmap> icon_; | 275 std::map<int, gfx::Image> icon_; |
| 261 std::map<int, int> icon_index_; // index into icon_paths_ | 276 std::map<int, int> icon_index_; // index into icon_paths_ |
| 262 std::map<int, std::string> badge_text_; | 277 std::map<int, std::string> badge_text_; |
| 263 std::map<int, SkColor> badge_background_color_; | 278 std::map<int, SkColor> badge_background_color_; |
| 264 std::map<int, SkColor> badge_text_color_; | 279 std::map<int, SkColor> badge_text_color_; |
| 265 std::map<int, bool> visible_; | 280 std::map<int, bool> visible_; |
| 266 | 281 |
| 267 class IconAnimationWrapper; | 282 class IconAnimationWrapper; |
| 268 std::map<int, linked_ptr<IconAnimationWrapper> > icon_animation_; | 283 std::map<int, linked_ptr<IconAnimationWrapper> > icon_animation_; |
| 269 | 284 |
| 270 std::string default_icon_path_; | 285 std::string default_icon_path_; |
| 271 | 286 |
| 272 // The id for the ExtensionAction, for example: "RssPageAction". This is | 287 // The id for the ExtensionAction, for example: "RssPageAction". This is |
| 273 // needed for compat with an older version of the page actions API. | 288 // needed for compat with an older version of the page actions API. |
| 274 std::string id_; | 289 std::string id_; |
| 275 | 290 |
| 276 // A list of paths to icons this action might show. This is needed to support | 291 // A list of paths to icons this action might show. This is needed to support |
| 277 // the legacy setIcon({iconIndex:...} method of the page actions API. | 292 // the legacy setIcon({iconIndex:...} method of the page actions API. |
| 278 std::vector<std::string> icon_paths_; | 293 std::vector<std::string> icon_paths_; |
| 279 | 294 |
| 295 // Saves the arguments from CacheIcon() calls. |
| 296 std::map<std::string, gfx::Image> path_to_icon_cache_; |
| 297 |
| 280 DISALLOW_COPY_AND_ASSIGN(ExtensionAction); | 298 DISALLOW_COPY_AND_ASSIGN(ExtensionAction); |
| 281 }; | 299 }; |
| 282 | 300 |
| 283 template<> | 301 template<> |
| 284 struct ExtensionAction::ValueTraits<int> { | 302 struct ExtensionAction::ValueTraits<int> { |
| 285 static int CreateEmpty() { | 303 static int CreateEmpty() { |
| 286 return -1; | 304 return -1; |
| 287 } | 305 } |
| 288 }; | 306 }; |
| 289 | 307 |
| 290 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ | 308 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ |
| OLD | NEW |