Index: chrome/common/extensions/extension_action.h |
diff --git a/chrome/common/extensions/extension_action.h b/chrome/common/extensions/extension_action.h |
old mode 100644 |
new mode 100755 |
index 6eb66c1ac7dd931d5d4f98edd58b72099fc31d8b..7c7e11f81fc2e5b6e9d018a4ed667346c157c5f9 |
--- a/chrome/common/extensions/extension_action.h |
+++ b/chrome/common/extensions/extension_action.h |
@@ -10,6 +10,7 @@ |
#include <vector> |
#include "base/basictypes.h" |
+#include "base/logging.h" |
#include "base/scoped_ptr.h" |
#include "googleurl/src/gurl.h" |
#include "third_party/skia/include/core/SkBitmap.h" |
@@ -20,132 +21,168 @@ class Canvas; |
class Rect; |
} |
+// ExtensionAction encapsulates the state of a browser or page action. |
+// Instances can have both global and per-tab state. If a property does not have |
+// a per-tab value, the global value is used instead. |
+// |
+// TODO(aa): This should replace ExtensionAction and ExtensionActionState. |
class ExtensionAction { |
public: |
- ExtensionAction(); |
- virtual ~ExtensionAction(); |
- |
- typedef enum { |
- PAGE_ACTION = 0, |
- BROWSER_ACTION = 1, |
- } ExtensionActionType; |
- |
- std::string id() const { return id_; } |
- void set_id(const std::string& id) { id_ = id; } |
- |
- ExtensionActionType type() const { return type_; } |
- void set_type(ExtensionActionType type) { type_ = type; } |
+ // Use this ID to indicate the default state for properties that take a tab_id |
+ // parameter. |
+ static const int kDefaultTabId; |
+ // extension id |
std::string extension_id() const { return extension_id_; } |
void set_extension_id(const std::string& extension_id) { |
extension_id_ = extension_id; |
} |
- std::string title() const { return title_; } |
- void set_title(const std::string& title) { title_ = title; } |
- |
- const std::vector<std::string>& icon_paths() const { return icon_paths_; } |
- void AddIconPath(const std::string& icon_path) { |
- icon_paths_.push_back(icon_path); |
- } |
- |
+ // popup details |
const GURL& popup_url() const { return popup_url_; } |
void set_popup_url(const GURL& url) { popup_url_ = url; } |
+ bool has_popup() const { return !popup_url_.is_empty(); } |
- bool is_popup() const { return !popup_url_.is_empty(); } |
- |
- private: |
- // The id for the ExtensionAction, for example: "RssPageAction". |
- // For BrowserActions this is blank. |
- std::string id_; |
- |
- // The type of the ExtensionAction, either PageAction or BrowserAction. |
- ExtensionActionType type_; |
- |
- // The id for the extension this ExtensionAction belongs to (as defined in |
- // the extension manifest). |
- std::string extension_id_; |
- |
- // The title text of the ExtensionAction. |
- std::string title_; |
- |
- // The paths to the icons that this PageIcon can show. |
- std::vector<std::string> icon_paths_; |
- |
- // If the action has a popup, it has a URL and a height. |
- GURL popup_url_; |
-}; |
- |
-typedef std::map<std::string, ExtensionAction*> ExtensionActionMap; |
+ // action id -- only used with legacy page actions API |
+ std::string id() const { return id_; } |
+ void set_id(const std::string& id) { id_ = id; } |
-// This class keeps track of what values each tab uses to override the default |
-// values of the ExtensionAction. |
-class ExtensionActionState { |
- public: |
- static void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, |
- const std::string& text, SkColor text_color, |
- SkColor background_color); |
+ // static icon paths from manifest -- only used with legacy page actions API. |
+ std::vector<std::string>* icon_paths() { return &icon_paths_; } |
- ExtensionActionState(std::string title, int icon_index) |
- : hidden_(false), title_(title), icon_index_(icon_index), |
- badge_background_color_(SkColorSetARGB(255, 218, 0, 24)), |
- badge_text_color_(SK_ColorWHITE) { |
+ // title |
+ void SetTitle(int tab_id, const std::string& title) { |
+ SetValue(&title_, tab_id, title); |
+ } |
+ std::string GetTitle(int tab_id) { return GetValue(&title_, tab_id); } |
+ |
+ // Icons are a bit different because the default value can be set to either a |
+ // bitmap or a path. However, conceptually, there is only one default icon. |
+ // Setting the default icon using a path clears the bitmap and vice-versa. |
+ // |
+ // To get the default icon, first check for the bitmap. If it is null, check |
+ // for the path. |
+ |
+ // Icon bitmap. |
+ void SetIcon(int tab_id, const SkBitmap& bitmap) { |
+ SetValue(&icon_, tab_id, bitmap); |
+ } |
+ SkBitmap GetIcon(int tab_id) { return GetValue(&icon_, tab_id); } |
+ |
+ // Icon index -- for use with icon_paths(), only used in page actions. |
+ void SetIconIndex(int tab_id, int index) { |
+ if (static_cast<size_t>(index) >= icon_paths_.size()) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ SetValue(&icon_index_, tab_id, index); |
+ } |
+ int GetIconIndex(int tab_id) { |
+ return GetValue(&icon_index_, tab_id); |
} |
- const std::string& title() const { return title_; } |
- void set_title(const std::string& title) { title_ = title; } |
+ // Non-tab-specific icon path. This is used to support the default_icon key of |
+ // page and browser actions. |
+ void set_default_icon_path(const std::string& path) { |
+ default_icon_path_ = path; |
+ } |
+ std::string default_icon_path() { |
+ return default_icon_path_; |
+ } |
- const std::string& badge_text() const { return badge_text_; } |
- void set_badge_text(const std::string& badge_text) { |
- badge_text_ = badge_text; |
+ // badge text |
+ void SetBadgeText(int tab_id, const std::string& text) { |
+ SetValue(&badge_text_, tab_id, text); |
} |
+ std::string GetBadgeText(int tab_id) { return GetValue(&badge_text_, tab_id); } |
- SkColor badge_background_color() const { |
- return badge_background_color_; |
+ // badge text color |
+ void SetBadgeTextColor(int tab_id, const SkColor& text_color) { |
+ SetValue(&badge_text_color_, tab_id, text_color); |
} |
- void set_badge_background_color(SkColor badge_background_color) { |
- badge_background_color_ = badge_background_color; |
+ SkColor GetBadgeTextColor(int tab_id) { |
+ return GetValue(&badge_text_color_, tab_id); |
} |
- SkColor badge_text_color() const { |
- return badge_text_color_; |
+ // badge background color |
+ void SetBadgeBackgroundColor(int tab_id, const SkColor& color) { |
+ SetValue(&badge_background_color_, tab_id, color); |
} |
- void set_badge_text_color(SkColor badge_text_color) { |
- badge_text_color_ = badge_text_color; |
+ SkColor GetBadgeBackgroundColor(int tab_id) { |
+ return GetValue(&badge_background_color_, tab_id); |
} |
- int icon_index() const { return icon_index_; } |
- void set_icon_index(int icon_index) { icon_index_ = icon_index; } |
+ // visibility |
+ void SetIsVisible(int tab_id, bool value) { |
+ SetValue(&visible_, tab_id, value); |
+ } |
+ bool GetIsVisible(int tab_id) { |
+ return GetValue(&visible_, tab_id); |
+ } |
- SkBitmap* icon() const { return icon_.get(); } |
- void set_icon(SkBitmap* icon) { icon_.reset(icon); } |
+ // Remove all tab-specific state. |
+ void ClearAllValuesForTab(int tab_id); |
- bool hidden() const { return hidden_; } |
- void set_hidden(bool hidden) { hidden_ = hidden; } |
+ // If the specified tab has a badge, paint it into the provided bounds. |
+ void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); |
private: |
- // True if the action is in the hidden state. |
- bool hidden_; |
+ template <class T> |
+ struct ValueTraits { |
+ static T CreateEmpty() { |
+ return T(); |
+ } |
+ }; |
+ |
+ template<class T> |
+ void SetValue(std::map<int, T>* map, int tab_id, T val) { |
+ (*map)[tab_id] = val; |
+ } |
- // The title text to use for tooltips and labels. |
- std::string title_; |
+ template<class T> |
+ T GetValue(std::map<int, T>* map, int tab_id) { |
+ typename std::map<int, T>::iterator iter = map->find(tab_id); |
+ if (iter != map->end()) { |
+ return iter->second; |
+ } else { |
+ iter = map->find(kDefaultTabId); |
+ return iter != map->end() ? iter->second : ValueTraits<T>::CreateEmpty(); |
+ } |
+ } |
- // The icon to use. |
- int icon_index_; |
+ // The id for the extension this action belongs to (as defined in the |
+ // extension manifest). |
+ std::string extension_id_; |
- // If non-NULL, overrides icon_index. |
- scoped_ptr<SkBitmap> icon_; |
+ // Each of these data items can have both a global state (stored with the key |
+ // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). |
+ std::map<int, std::string> title_; |
+ std::map<int, SkBitmap> icon_; |
+ std::map<int, int> icon_index_; // index into icon_paths_ |
+ std::map<int, std::string> badge_text_; |
+ std::map<int, SkColor> badge_background_color_; |
+ std::map<int, SkColor> badge_text_color_; |
+ std::map<int, bool> visible_; |
- // The badge text. |
- std::string badge_text_; |
+ std::string default_icon_path_; |
+ |
+ // If the action has a popup, it has a URL and a height. |
+ GURL popup_url_; |
- // The background color for the badge. |
- SkColor badge_background_color_; |
+ // The id for the ExtensionAction, for example: "RssPageAction". This is |
+ // needed for compat with an older version of the page actions API. |
+ std::string id_; |
- // The text color for the badge. |
- SkColor badge_text_color_; |
+ // A list of paths to icons this action might show. This is needed to support |
+ // the legacy setIcon({iconIndex:...} method of the page actions API. |
+ std::vector<std::string> icon_paths_; |
+}; |
- DISALLOW_COPY_AND_ASSIGN(ExtensionActionState); |
+template<> |
+struct ExtensionAction::ValueTraits<int> { |
+ static int CreateEmpty() { |
+ return -1; |
+ } |
}; |
#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ |