Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: chrome/common/extensions/extension_action2.h

Issue 332021: Move page actions over to ExtensionAction2 (Closed)
Patch Set: Review feedback Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_ACTION2_H_ 5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION2_H_
6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION2_H_ 6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION2_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
19 namespace gfx {
20 class Canvas;
21 class Rect;
22 }
23
18 // ExtensionAction2 encapsulates the state of a browser or page action. 24 // ExtensionAction2 encapsulates the state of a browser or page action.
19 // Instances can have both global and per-tab state. If a property does not have 25 // Instances can have both global and per-tab state. If a property does not have
20 // a per-tab value, the global value is used instead. 26 // a per-tab value, the global value is used instead.
21 // 27 //
22 // TODO(aa): This should replace ExtensionAction and ExtensionActionState. 28 // TODO(aa): This should replace ExtensionAction and ExtensionActionState.
23 class ExtensionAction2 { 29 class ExtensionAction2 {
24 public: 30 public:
25 // Use this ID to indicate the default state for properties that take a tab_id 31 // Use this ID to indicate the default state for properties that take a tab_id
26 // parameter. 32 // parameter.
27 static const int kDefaultTabId; 33 static const int kDefaultTabId;
28 34
29 // extension id 35 // extension id
30 std::string extension_id() const { return extension_id_; } 36 std::string extension_id() const { return extension_id_; }
31 void set_extension_id(const std::string& extension_id) { 37 void set_extension_id(const std::string& extension_id) {
32 extension_id_ = extension_id; 38 extension_id_ = extension_id;
33 } 39 }
34 40
35 // popup details 41 // popup details
36 const GURL& popup_url() const { return popup_url_; } 42 const GURL& popup_url() const { return popup_url_; }
37 void set_popup_url(const GURL& url) { popup_url_ = url; } 43 void set_popup_url(const GURL& url) { popup_url_ = url; }
38 bool has_popup() const { return !popup_url_.is_empty(); } 44 bool has_popup() const { return !popup_url_.is_empty(); }
39 45
46 // action id -- only used with legacy page actions API
47 std::string id() const { return id_; }
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
40 // title 53 // title
41 void SetTitle(int tab_id, const std::string& title) { 54 void SetTitle(int tab_id, const std::string& title) {
42 SetValue(&title_, tab_id, title); 55 SetValue(&title_, tab_id, title);
43 } 56 }
44 std::string GetTitle(int tab_id) { return GetValue(&title_, tab_id); } 57 std::string GetTitle(int tab_id) { return GetValue(&title_, tab_id); }
45 58
46 // Icons are a bit different because the default value can be set to either a 59 // Icons are a bit different because the default value can be set to either a
47 // bitmap or a path. However, conceptually, there is only one default icon. 60 // bitmap or a path. However, conceptually, there is only one default icon.
48 // Setting the default icon using a path clears the bitmap and vice-versa. 61 // Setting the default icon using a path clears the bitmap and vice-versa.
49 // 62 //
50 // To get the default icon, first check for the bitmap. If it is null, check 63 // To get the default icon, first check for the bitmap. If it is null, check
51 // for the path. 64 // for the path.
52 65
53 // icon bitmap 66 // Icon bitmap.
54 void SetIcon(int tab_id, const SkBitmap& bitmap) { 67 void SetIcon(int tab_id, const SkBitmap& bitmap) {
55 SetValue(&icon_, tab_id, bitmap); 68 SetValue(&icon_, tab_id, bitmap);
56 if (tab_id == kDefaultTabId)
57 default_icon_path_.clear();
58 } 69 }
59 SkBitmap GetIcon(int tab_id) { return GetValue(&icon_, tab_id); } 70 SkBitmap GetIcon(int tab_id) { return GetValue(&icon_, tab_id); }
60 71
61 // icon path (relative to extension_id()'s root) 72 // Icon index -- for use with icon_paths(), only used in page actions.
62 // For legacy code, we also support setting the path as an index into 73 void SetIconIndex(int tab_id, int index) {
63 // icon_paths(). 74 if (static_cast<size_t>(index) >= icon_paths_.size()) {
64 void SetDefaultIcon(const std::string& path); 75 NOTREACHED();
65 void SetDefaultIcon(int icon_index); 76 return;
66 std::string GetDefaultIconPath() { 77 }
78 SetValue(&icon_index_, tab_id, index);
79 }
80 int GetIconIndex(int tab_id) {
81 return GetValue(&icon_index_, tab_id);
82 }
83
84 // Non-tab-specific icon path. This is used to support the default_icon key of
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() {
67 return default_icon_path_; 90 return default_icon_path_;
68 } 91 }
69 92
70 // badge text 93 // badge text
71 void SetBadgeText(int tab_id, const std::string& text) { 94 void SetBadgeText(int tab_id, const std::string& text) {
72 SetValue(&badge_text_, tab_id, text); 95 SetValue(&badge_text_, tab_id, text);
73 } 96 }
74 std::string GetBadgeText(int tab_id) { return GetValue(&badge_text_, tab_id); } 97 std::string GetBadgeText(int tab_id) { return GetValue(&badge_text_, tab_id); }
75 98
76 // badge text color 99 // badge text color
77 void SetBadgeTextColor(int tab_id, const SkColor& text_color) { 100 void SetBadgeTextColor(int tab_id, const SkColor& text_color) {
78 SetValue(&badge_text_color_, tab_id, text_color); 101 SetValue(&badge_text_color_, tab_id, text_color);
79 } 102 }
80 SkColor GetBadgeTextColor(int tab_id) { 103 SkColor GetBadgeTextColor(int tab_id) {
81 return GetValue(&badge_text_color_, tab_id); 104 return GetValue(&badge_text_color_, tab_id);
82 } 105 }
83 106
84 // badge background color 107 // badge background color
85 void SetBadgeBackgroundColor(int tab_id, const SkColor& color) { 108 void SetBadgeBackgroundColor(int tab_id, const SkColor& color) {
86 SetValue(&badge_background_color_, tab_id, color); 109 SetValue(&badge_background_color_, tab_id, color);
87 } 110 }
88 SkColor GetBadgeBackgroundColor(int tab_id) { 111 SkColor GetBadgeBackgroundColor(int tab_id) {
89 return GetValue(&badge_background_color_, tab_id); 112 return GetValue(&badge_background_color_, tab_id);
90 } 113 }
91 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
92 // Remove all tab-specific state. 123 // Remove all tab-specific state.
93 void ClearAllValuesForTab(int tab_id); 124 void ClearAllValuesForTab(int tab_id);
94 125
95 //--------------------------------------------------------------------------- 126 // If the specified tab has a badge, paint it into the provided bounds.
96 // Legacy support 127 void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds, int tab_id);
97
98 std::string id() const { return id_; }
99 void set_id(const std::string& id) { id_ = id; }
100
101 std::vector<std::string>* icon_paths() { return &icon_paths_; }
102 128
103 private: 129 private:
104 template <class T> 130 template <class T>
105 struct ValueTraits { 131 struct ValueTraits {
106 static T CreateEmpty() { 132 static T CreateEmpty() {
107 return T(); 133 return T();
108 } 134 }
109 }; 135 };
110 136
111 template<class T> 137 template<class T>
(...skipping 13 matching lines...) Expand all
125 } 151 }
126 152
127 // The id for the extension this action belongs to (as defined in the 153 // The id for the extension this action belongs to (as defined in the
128 // extension manifest). 154 // extension manifest).
129 std::string extension_id_; 155 std::string extension_id_;
130 156
131 // Each of these data items can have both a global state (stored with the key 157 // Each of these data items can have both a global state (stored with the key
132 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key). 158 // kDefaultTabId), or tab-specific state (stored with the tab_id as the key).
133 std::map<int, std::string> title_; 159 std::map<int, std::string> title_;
134 std::map<int, SkBitmap> icon_; 160 std::map<int, SkBitmap> icon_;
161 std::map<int, int> icon_index_; // index into icon_paths_
135 std::map<int, std::string> badge_text_; 162 std::map<int, std::string> badge_text_;
136 std::map<int, SkColor> badge_background_color_; 163 std::map<int, SkColor> badge_background_color_;
137 std::map<int, SkColor> badge_text_color_; 164 std::map<int, SkColor> badge_text_color_;
165 std::map<int, bool> visible_;
138 166
139 std::string default_icon_path_; 167 std::string default_icon_path_;
140 168
141 // 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.
142 GURL popup_url_; 170 GURL popup_url_;
143 171
144 //---------------------------------------------------------------------------
145 // Legacy support
146
147 // The id for the ExtensionAction2, for example: "RssPageAction". This is 172 // The id for the ExtensionAction2, for example: "RssPageAction". This is
148 // needed for compat with an older version of the page actions API. 173 // needed for compat with an older version of the page actions API.
149 std::string id_; 174 std::string id_;
150 175
151 // A list of paths to icons this action might show. This is needed to support 176 // A list of paths to icons this action might show. This is needed to support
152 // the setIcon({iconIndex:...} method. 177 // the legacy setIcon({iconIndex:...} method of the page actions API.
153 std::vector<std::string> icon_paths_; 178 std::vector<std::string> icon_paths_;
154 }; 179 };
155 180
156 template <> 181 template<>
157 struct ExtensionAction2::ValueTraits<SkColor> { 182 struct ExtensionAction2::ValueTraits<int> {
Matt Perry 2009/10/26 23:17:17 nit: can you move this inside the ExtensionAction2
158 static SkColor CreateEmpty() { 183 static int CreateEmpty() {
159 return 0x00000000; 184 return -1;
160 } 185 }
161 }; 186 };
162 187
163 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION2_H_ 188 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ACTION2_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698