OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/logging.h" |
13 #include "base/scoped_ptr.h" | 14 #include "base/scoped_ptr.h" |
14 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
16 #include "third_party/skia/include/core/SkColor.h" | 17 #include "third_party/skia/include/core/SkColor.h" |
17 | 18 |
18 namespace gfx { | 19 namespace gfx { |
19 class Canvas; | 20 class Canvas; |
20 class Rect; | 21 class Rect; |
21 } | 22 } |
22 | 23 |
| 24 // ExtensionAction encapsulates the state of a browser or page action. |
| 25 // Instances can have both global and per-tab state. If a property does not have |
| 26 // a per-tab value, the global value is used instead. |
| 27 // |
| 28 // TODO(aa): This should replace ExtensionAction and ExtensionActionState. |
23 class ExtensionAction { | 29 class ExtensionAction { |
24 public: | 30 public: |
25 ExtensionAction(); | 31 // Use this ID to indicate the default state for properties that take a tab_id |
26 virtual ~ExtensionAction(); | 32 // parameter. |
| 33 static const int kDefaultTabId; |
27 | 34 |
28 typedef enum { | 35 // extension id |
29 PAGE_ACTION = 0, | |
30 BROWSER_ACTION = 1, | |
31 } ExtensionActionType; | |
32 | |
33 std::string id() const { return id_; } | |
34 void set_id(const std::string& id) { id_ = id; } | |
35 | |
36 ExtensionActionType type() const { return type_; } | |
37 void set_type(ExtensionActionType type) { type_ = type; } | |
38 | |
39 std::string extension_id() const { return extension_id_; } | 36 std::string extension_id() const { return extension_id_; } |
40 void set_extension_id(const std::string& extension_id) { | 37 void set_extension_id(const std::string& extension_id) { |
41 extension_id_ = extension_id; | 38 extension_id_ = extension_id; |
42 } | 39 } |
43 | 40 |
44 std::string title() const { return title_; } | 41 // popup details |
45 void set_title(const std::string& title) { title_ = title; } | 42 const GURL& popup_url() const { return popup_url_; } |
| 43 void set_popup_url(const GURL& url) { popup_url_ = url; } |
| 44 bool has_popup() const { return !popup_url_.is_empty(); } |
46 | 45 |
47 const std::vector<std::string>& icon_paths() const { return icon_paths_; } | 46 // action id -- only used with legacy page actions API |
48 void AddIconPath(const std::string& icon_path) { | 47 std::string id() const { return id_; } |
49 icon_paths_.push_back(icon_path); | 48 void set_id(const std::string& id) { id_ = id; } |
| 49 |
| 50 // static icon paths from manifest -- only used with legacy page actions API. |
| 51 std::vector<std::string>* icon_paths() { return &icon_paths_; } |
| 52 |
| 53 // title |
| 54 void SetTitle(int tab_id, const std::string& title) { |
| 55 SetValue(&title_, tab_id, title); |
| 56 } |
| 57 std::string GetTitle(int tab_id) { return GetValue(&title_, tab_id); } |
| 58 |
| 59 // Icons are a bit different because the default value can be set to either a |
| 60 // bitmap or a path. However, conceptually, there is only one default icon. |
| 61 // Setting the default icon using a path clears the bitmap and vice-versa. |
| 62 // |
| 63 // To get the default icon, first check for the bitmap. If it is null, check |
| 64 // for the path. |
| 65 |
| 66 // Icon bitmap. |
| 67 void SetIcon(int tab_id, const SkBitmap& bitmap) { |
| 68 SetValue(&icon_, tab_id, bitmap); |
| 69 } |
| 70 SkBitmap GetIcon(int tab_id) { return GetValue(&icon_, tab_id); } |
| 71 |
| 72 // Icon index -- for use with icon_paths(), only used in page actions. |
| 73 void SetIconIndex(int tab_id, int index) { |
| 74 if (static_cast<size_t>(index) >= icon_paths_.size()) { |
| 75 NOTREACHED(); |
| 76 return; |
| 77 } |
| 78 SetValue(&icon_index_, tab_id, index); |
| 79 } |
| 80 int GetIconIndex(int tab_id) { |
| 81 return GetValue(&icon_index_, tab_id); |
50 } | 82 } |
51 | 83 |
52 const GURL& popup_url() const { return popup_url_; } | 84 // Non-tab-specific icon path. This is used to support the default_icon key of |
53 void set_popup_url(const GURL& url) { popup_url_ = url; } | 85 // page and browser actions. |
| 86 void set_default_icon_path(const std::string& path) { |
| 87 default_icon_path_ = path; |
| 88 } |
| 89 std::string default_icon_path() { |
| 90 return default_icon_path_; |
| 91 } |
54 | 92 |
55 bool is_popup() const { return !popup_url_.is_empty(); } | 93 // badge text |
| 94 void SetBadgeText(int tab_id, const std::string& text) { |
| 95 SetValue(&badge_text_, tab_id, text); |
| 96 } |
| 97 std::string GetBadgeText(int tab_id) { return GetValue(&badge_text_, tab_id);
} |
| 98 |
| 99 // badge text color |
| 100 void SetBadgeTextColor(int tab_id, const SkColor& text_color) { |
| 101 SetValue(&badge_text_color_, tab_id, text_color); |
| 102 } |
| 103 SkColor GetBadgeTextColor(int tab_id) { |
| 104 return GetValue(&badge_text_color_, tab_id); |
| 105 } |
| 106 |
| 107 // badge background color |
| 108 void SetBadgeBackgroundColor(int tab_id, const SkColor& color) { |
| 109 SetValue(&badge_background_color_, tab_id, color); |
| 110 } |
| 111 SkColor GetBadgeBackgroundColor(int tab_id) { |
| 112 return GetValue(&badge_background_color_, tab_id); |
| 113 } |
| 114 |
| 115 // visibility |
| 116 void SetIsVisible(int tab_id, bool value) { |
| 117 SetValue(&visible_, tab_id, value); |
| 118 } |
| 119 bool GetIsVisible(int tab_id) { |
| 120 return GetValue(&visible_, tab_id); |
| 121 } |
| 122 |
| 123 // Remove all tab-specific state. |
| 124 void ClearAllValuesForTab(int tab_id); |
| 125 |
| 126 // If the specified tab has a badge, paint it into the provided bounds. |
| 127 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id); |
56 | 128 |
57 private: | 129 private: |
58 // The id for the ExtensionAction, for example: "RssPageAction". | 130 template <class T> |
59 // For BrowserActions this is blank. | 131 struct ValueTraits { |
60 std::string id_; | 132 static T CreateEmpty() { |
| 133 return T(); |
| 134 } |
| 135 }; |
61 | 136 |
62 // The type of the ExtensionAction, either PageAction or BrowserAction. | 137 template<class T> |
63 ExtensionActionType type_; | 138 void SetValue(std::map<int, T>* map, int tab_id, T val) { |
| 139 (*map)[tab_id] = val; |
| 140 } |
64 | 141 |
65 // The id for the extension this ExtensionAction belongs to (as defined in | 142 template<class T> |
66 // the extension manifest). | 143 T GetValue(std::map<int, T>* map, int tab_id) { |
| 144 typename std::map<int, T>::iterator iter = map->find(tab_id); |
| 145 if (iter != map->end()) { |
| 146 return iter->second; |
| 147 } else { |
| 148 iter = map->find(kDefaultTabId); |
| 149 return iter != map->end() ? iter->second : ValueTraits<T>::CreateEmpty(); |
| 150 } |
| 151 } |
| 152 |
| 153 // The id for the extension this action belongs to (as defined in the |
| 154 // extension manifest). |
67 std::string extension_id_; | 155 std::string extension_id_; |
68 | 156 |
69 // The title text of the ExtensionAction. | 157 // Each of these data items can have both a global state (stored with the key |
70 std::string title_; | 158 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). |
| 159 std::map<int, std::string> title_; |
| 160 std::map<int, SkBitmap> icon_; |
| 161 std::map<int, int> icon_index_; // index into icon_paths_ |
| 162 std::map<int, std::string> badge_text_; |
| 163 std::map<int, SkColor> badge_background_color_; |
| 164 std::map<int, SkColor> badge_text_color_; |
| 165 std::map<int, bool> visible_; |
71 | 166 |
72 // The paths to the icons that this PageIcon can show. | 167 std::string default_icon_path_; |
73 std::vector<std::string> icon_paths_; | |
74 | 168 |
75 // If the action has a popup, it has a URL and a height. | 169 // If the action has a popup, it has a URL and a height. |
76 GURL popup_url_; | 170 GURL popup_url_; |
| 171 |
| 172 // The id for the ExtensionAction, for example: "RssPageAction". This is |
| 173 // needed for compat with an older version of the page actions API. |
| 174 std::string id_; |
| 175 |
| 176 // A list of paths to icons this action might show. This is needed to support |
| 177 // the legacy setIcon({iconIndex:...} method of the page actions API. |
| 178 std::vector<std::string> icon_paths_; |
77 }; | 179 }; |
78 | 180 |
79 typedef std::map<std::string, ExtensionAction*> ExtensionActionMap; | 181 template<> |
80 | 182 struct ExtensionAction::ValueTraits<int> { |
81 // This class keeps track of what values each tab uses to override the default | 183 static int CreateEmpty() { |
82 // values of the ExtensionAction. | 184 return -1; |
83 class ExtensionActionState { | |
84 public: | |
85 static void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, | |
86 const std::string& text, SkColor text_color, | |
87 SkColor background_color); | |
88 | |
89 ExtensionActionState(std::string title, int icon_index) | |
90 : hidden_(false), title_(title), icon_index_(icon_index), | |
91 badge_background_color_(SkColorSetARGB(255, 218, 0, 24)), | |
92 badge_text_color_(SK_ColorWHITE) { | |
93 } | 185 } |
94 | |
95 const std::string& title() const { return title_; } | |
96 void set_title(const std::string& title) { title_ = title; } | |
97 | |
98 const std::string& badge_text() const { return badge_text_; } | |
99 void set_badge_text(const std::string& badge_text) { | |
100 badge_text_ = badge_text; | |
101 } | |
102 | |
103 SkColor badge_background_color() const { | |
104 return badge_background_color_; | |
105 } | |
106 void set_badge_background_color(SkColor badge_background_color) { | |
107 badge_background_color_ = badge_background_color; | |
108 } | |
109 | |
110 SkColor badge_text_color() const { | |
111 return badge_text_color_; | |
112 } | |
113 void set_badge_text_color(SkColor badge_text_color) { | |
114 badge_text_color_ = badge_text_color; | |
115 } | |
116 | |
117 int icon_index() const { return icon_index_; } | |
118 void set_icon_index(int icon_index) { icon_index_ = icon_index; } | |
119 | |
120 SkBitmap* icon() const { return icon_.get(); } | |
121 void set_icon(SkBitmap* icon) { icon_.reset(icon); } | |
122 | |
123 bool hidden() const { return hidden_; } | |
124 void set_hidden(bool hidden) { hidden_ = hidden; } | |
125 | |
126 private: | |
127 // True if the action is in the hidden state. | |
128 bool hidden_; | |
129 | |
130 // The title text to use for tooltips and labels. | |
131 std::string title_; | |
132 | |
133 // The icon to use. | |
134 int icon_index_; | |
135 | |
136 // If non-NULL, overrides icon_index. | |
137 scoped_ptr<SkBitmap> icon_; | |
138 | |
139 // The badge text. | |
140 std::string badge_text_; | |
141 | |
142 // The background color for the badge. | |
143 SkColor badge_background_color_; | |
144 | |
145 // The text color for the badge. | |
146 SkColor badge_text_color_; | |
147 | |
148 DISALLOW_COPY_AND_ASSIGN(ExtensionActionState); | |
149 }; | 186 }; |
150 | 187 |
151 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ | 188 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION_H_ |
OLD | NEW |