OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/scoped_ptr.h" | 14 #include "base/scoped_ptr.h" |
15 #include "base/string16.h" | 15 #include "base/string16.h" |
16 #include "chrome/browser/extensions/extension_icon_manager.h" | 16 #include "chrome/browser/extensions/extension_icon_manager.h" |
| 17 #include "chrome/common/extensions/extension_extent.h" |
17 #include "chrome/common/notification_observer.h" | 18 #include "chrome/common/notification_observer.h" |
18 #include "chrome/common/notification_registrar.h" | 19 #include "chrome/common/notification_registrar.h" |
19 | 20 |
20 struct ContextMenuParams; | 21 struct ContextMenuParams; |
21 | 22 |
22 class Extension; | 23 class Extension; |
23 class ExtensionMessageService; | 24 class ExtensionMessageService; |
24 class Profile; | 25 class Profile; |
25 class SkBitmap; | 26 class SkBitmap; |
26 class TabContents; | 27 class TabContents; |
27 | 28 |
28 // Represents a menu item added by an extension. | 29 // Represents a menu item added by an extension. |
29 class ExtensionMenuItem { | 30 class ExtensionMenuItem { |
30 public: | 31 public: |
31 // A list of ExtensionMenuItem's. | 32 // A list of ExtensionMenuItem's. |
32 typedef std::vector<ExtensionMenuItem*> List; | 33 typedef std::vector<ExtensionMenuItem*> List; |
33 | 34 |
| 35 // An Id is a pair of |extension id|, |int| where the |int| is unique per |
| 36 // extension. |
| 37 typedef std::pair<std::string, int> Id; |
| 38 |
34 // For context menus, these are the contexts where an item can appear. | 39 // For context menus, these are the contexts where an item can appear. |
35 enum Context { | 40 enum Context { |
36 ALL = 1, | 41 ALL = 1, |
37 PAGE = 2, | 42 PAGE = 2, |
38 SELECTION = 4, | 43 SELECTION = 4, |
39 LINK = 8, | 44 LINK = 8, |
40 EDITABLE = 16, | 45 EDITABLE = 16, |
41 IMAGE = 32, | 46 IMAGE = 32, |
42 VIDEO = 64, | 47 VIDEO = 64, |
43 AUDIO = 128, | 48 AUDIO = 128, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 } | 80 } |
76 | 81 |
77 void Add(Context context) { | 82 void Add(Context context) { |
78 value_ |= context; | 83 value_ |= context; |
79 } | 84 } |
80 | 85 |
81 private: | 86 private: |
82 uint32 value_; // A bitmask of Context values. | 87 uint32 value_; // A bitmask of Context values. |
83 }; | 88 }; |
84 | 89 |
85 // An Id is a pair of |extension id|, |int| where the |int| is unique per | |
86 // extension. | |
87 typedef std::pair<std::string, int> Id; | |
88 | |
89 ExtensionMenuItem(const Id& id, std::string title, bool checked, Type type, | 90 ExtensionMenuItem(const Id& id, std::string title, bool checked, Type type, |
90 const ContextList& contexts); | 91 const ContextList& contexts); |
91 virtual ~ExtensionMenuItem(); | 92 virtual ~ExtensionMenuItem(); |
92 | 93 |
93 // Simple accessor methods. | 94 // Simple accessor methods. |
94 const std::string& extension_id() const { return id_.first; } | 95 const std::string& extension_id() const { return id_.first; } |
95 const std::string& title() const { return title_; } | 96 const std::string& title() const { return title_; } |
96 const List& children() { return children_; } | 97 const List& children() { return children_; } |
97 const Id& id() const { return id_; } | 98 const Id& id() const { return id_; } |
98 Id* parent_id() const { return parent_id_.get(); } | 99 Id* parent_id() const { return parent_id_.get(); } |
99 int child_count() const { return children_.size(); } | 100 int child_count() const { return children_.size(); } |
100 ContextList contexts() const { return contexts_; } | 101 ContextList contexts() const { return contexts_; } |
101 Type type() const { return type_; } | 102 Type type() const { return type_; } |
102 bool checked() const { return checked_; } | 103 bool checked() const { return checked_; } |
| 104 const ExtensionExtent& document_url_patterns() const { |
| 105 return document_url_patterns_; |
| 106 } |
| 107 const ExtensionExtent& target_url_patterns() const { |
| 108 return target_url_patterns_; |
| 109 } |
103 | 110 |
104 // Simple mutator methods. | 111 // Simple mutator methods. |
105 void set_title(std::string new_title) { title_ = new_title; } | 112 void set_title(std::string new_title) { title_ = new_title; } |
106 void set_contexts(ContextList contexts) { contexts_ = contexts; } | 113 void set_contexts(ContextList contexts) { contexts_ = contexts; } |
107 void set_type(Type type) { type_ = type; } | 114 void set_type(Type type) { type_ = type; } |
| 115 void set_document_url_patterns(const ExtensionExtent& patterns) { |
| 116 document_url_patterns_ = patterns; |
| 117 } |
| 118 void set_target_url_patterns(const ExtensionExtent& patterns) { |
| 119 target_url_patterns_ = patterns; |
| 120 } |
108 | 121 |
109 // Returns the title with any instances of %s replaced by |selection|. | 122 // Returns the title with any instances of %s replaced by |selection|. |
110 string16 TitleWithReplacement(const string16& selection) const; | 123 string16 TitleWithReplacement(const string16& selection) const; |
111 | 124 |
112 // Set the checked state to |checked|. Returns true if successful. | 125 // Set the checked state to |checked|. Returns true if successful. |
113 bool SetChecked(bool checked); | 126 bool SetChecked(bool checked); |
114 | 127 |
115 protected: | 128 protected: |
116 friend class ExtensionMenuManager; | 129 friend class ExtensionMenuManager; |
117 | 130 |
(...skipping 24 matching lines...) Expand all Loading... |
142 // This should only be true for items of type CHECKBOX or RADIO. | 155 // This should only be true for items of type CHECKBOX or RADIO. |
143 bool checked_; | 156 bool checked_; |
144 | 157 |
145 // In what contexts should the item be shown? | 158 // In what contexts should the item be shown? |
146 ContextList contexts_; | 159 ContextList contexts_; |
147 | 160 |
148 // If this item is a child of another item, the unique id of its parent. If | 161 // If this item is a child of another item, the unique id of its parent. If |
149 // this is a top-level item with no parent, this will be NULL. | 162 // this is a top-level item with no parent, this will be NULL. |
150 scoped_ptr<Id> parent_id_; | 163 scoped_ptr<Id> parent_id_; |
151 | 164 |
| 165 // Patterns for restricting what documents this item will appear for. This |
| 166 // applies to the frame where the click took place. |
| 167 ExtensionExtent document_url_patterns_; |
| 168 |
| 169 // Patterns for restricting where items appear based on the src/href |
| 170 // attribute of IMAGE/AUDIO/VIDEO/LINK tags. |
| 171 ExtensionExtent target_url_patterns_; |
| 172 |
152 // Any children this item may have. | 173 // Any children this item may have. |
153 List children_; | 174 List children_; |
154 | 175 |
155 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuItem); | 176 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuItem); |
156 }; | 177 }; |
157 | 178 |
158 // This class keeps track of menu items added by extensions. | 179 // This class keeps track of menu items added by extensions. |
159 class ExtensionMenuManager : public NotificationObserver { | 180 class ExtensionMenuManager : public NotificationObserver { |
160 public: | 181 public: |
| 182 // A bitmask of values from URLPattern::SchemeMasks indicating the schemes |
| 183 // of pages where we'll show extension menu items. |
| 184 static const int kAllowedSchemes; |
| 185 |
161 ExtensionMenuManager(); | 186 ExtensionMenuManager(); |
162 virtual ~ExtensionMenuManager(); | 187 virtual ~ExtensionMenuManager(); |
163 | 188 |
164 // Returns the ids of extensions which have menu items registered. | 189 // Returns the ids of extensions which have menu items registered. |
165 std::set<std::string> ExtensionIds(); | 190 std::set<std::string> ExtensionIds(); |
166 | 191 |
167 // Returns a list of all the *top-level* menu items (added via AddContextItem) | 192 // Returns a list of all the *top-level* menu items (added via AddContextItem) |
168 // for the given extension id, *not* including child items (added via | 193 // for the given extension id, *not* including child items (added via |
169 // AddChildItem); although those can be reached via the top-level items' | 194 // AddChildItem); although those can be reached via the top-level items' |
170 // children. A view can then decide how to display these, including whether to | 195 // children. A view can then decide how to display these, including whether to |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 | 232 |
208 // This returns a bitmap of width/height kFavIconSize, loaded either from an | 233 // This returns a bitmap of width/height kFavIconSize, loaded either from an |
209 // entry specified in the extension's 'icon' section of the manifest, or a | 234 // entry specified in the extension's 'icon' section of the manifest, or a |
210 // default extension icon. | 235 // default extension icon. |
211 const SkBitmap& GetIconForExtension(const std::string& extension_id); | 236 const SkBitmap& GetIconForExtension(const std::string& extension_id); |
212 | 237 |
213 // Implements the NotificationObserver interface. | 238 // Implements the NotificationObserver interface. |
214 virtual void Observe(NotificationType type, const NotificationSource& source, | 239 virtual void Observe(NotificationType type, const NotificationSource& source, |
215 const NotificationDetails& details); | 240 const NotificationDetails& details); |
216 | 241 |
| 242 // Returns true if |url| has an allowed scheme for extension context menu |
| 243 // items. This checks against kAllowedSchemes. |
| 244 static bool HasAllowedScheme(const GURL& url); |
| 245 |
217 private: | 246 private: |
218 // This is a helper function which takes care of de-selecting any other radio | 247 // This is a helper function which takes care of de-selecting any other radio |
219 // items in the same group (i.e. that are adjacent in the list). | 248 // items in the same group (i.e. that are adjacent in the list). |
220 void RadioItemSelected(ExtensionMenuItem* item); | 249 void RadioItemSelected(ExtensionMenuItem* item); |
221 | 250 |
222 // Returns true if item is a descendant of an item with id |ancestor_id|. | 251 // Returns true if item is a descendant of an item with id |ancestor_id|. |
223 bool DescendantOf(ExtensionMenuItem* item, | 252 bool DescendantOf(ExtensionMenuItem* item, |
224 const ExtensionMenuItem::Id& ancestor_id); | 253 const ExtensionMenuItem::Id& ancestor_id); |
225 | 254 |
226 // We keep items organized by mapping an extension id to a list of items. | 255 // We keep items organized by mapping an extension id to a list of items. |
227 typedef std::map<std::string, ExtensionMenuItem::List> MenuItemMap; | 256 typedef std::map<std::string, ExtensionMenuItem::List> MenuItemMap; |
228 MenuItemMap context_items_; | 257 MenuItemMap context_items_; |
229 | 258 |
230 // This lets us make lookup by id fast. It maps id to ExtensionMenuItem* for | 259 // This lets us make lookup by id fast. It maps id to ExtensionMenuItem* for |
231 // all items the menu manager knows about, including all children of top-level | 260 // all items the menu manager knows about, including all children of top-level |
232 // items. | 261 // items. |
233 std::map<ExtensionMenuItem::Id, ExtensionMenuItem*> items_by_id_; | 262 std::map<ExtensionMenuItem::Id, ExtensionMenuItem*> items_by_id_; |
234 | 263 |
235 NotificationRegistrar registrar_; | 264 NotificationRegistrar registrar_; |
236 | 265 |
237 ExtensionIconManager icon_manager_; | 266 ExtensionIconManager icon_manager_; |
238 | 267 |
239 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuManager); | 268 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuManager); |
240 }; | 269 }; |
241 | 270 |
242 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ | 271 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ |
OLD | NEW |