[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <set> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "base/basictypes.h" |
[email protected] | 1790275 | 2011-08-31 22:52:54 | [diff] [blame] | 14 | #include "base/compiler_specific.h" |
[email protected] | 2b07c93f | 2010-08-02 23:13:04 | [diff] [blame] | 15 | #include "base/gtest_prod_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/scoped_ptr.h" |
[email protected] | bec6455 | 2012-06-13 20:25:49 | [diff] [blame] | 17 | #include "base/memory/weak_ptr.h" |
[email protected] | 46acbf1 | 2013-06-10 18:43:42 | [diff] [blame^] | 18 | #include "base/strings/string16.h" |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 19 | #include "base/values.h" |
[email protected] | b671760b | 2010-07-15 21:13:47 | [diff] [blame] | 20 | #include "chrome/browser/extensions/extension_icon_manager.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 21 | #include "content/public/browser/notification_observer.h" |
| 22 | #include "content/public/browser/notification_registrar.h" |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 23 | #include "extensions/common/url_pattern_set.h" |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 24 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 25 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 26 | class Profile; |
[email protected] | b671760b | 2010-07-15 21:13:47 | [diff] [blame] | 27 | class SkBitmap; |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 28 | |
| 29 | namespace content { |
| 30 | class WebContents; |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 31 | struct ContextMenuParams; |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 32 | } |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 33 | |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 34 | namespace extensions { |
| 35 | class Extension; |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 36 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 37 | // Represents a menu item added by an extension. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 38 | class MenuItem { |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 39 | public: |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 40 | // A list of MenuItems. |
| 41 | typedef std::vector<MenuItem*> List; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 42 | |
[email protected] | 5a7b5eaf | 2010-11-02 20:52:19 | [diff] [blame] | 43 | // An Id uniquely identifies a context menu item registered by an extension. |
| 44 | struct Id { |
| 45 | Id(); |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 46 | // Since the unique ID (uid or string_uid) is parsed from API arguments, |
| 47 | // the normal usage is to set the uid or string_uid immediately after |
| 48 | // construction. |
[email protected] | 9a4cb81 | 2012-05-23 20:32:32 | [diff] [blame] | 49 | Id(bool incognito, const std::string& extension_id); |
[email protected] | 5a7b5eaf | 2010-11-02 20:52:19 | [diff] [blame] | 50 | ~Id(); |
| 51 | |
| 52 | bool operator==(const Id& other) const; |
| 53 | bool operator!=(const Id& other) const; |
| 54 | bool operator<(const Id& other) const; |
| 55 | |
[email protected] | 9a4cb81 | 2012-05-23 20:32:32 | [diff] [blame] | 56 | bool incognito; |
[email protected] | 5a7b5eaf | 2010-11-02 20:52:19 | [diff] [blame] | 57 | std::string extension_id; |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 58 | // Only one of uid or string_uid will be defined. |
[email protected] | 5a7b5eaf | 2010-11-02 20:52:19 | [diff] [blame] | 59 | int uid; |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 60 | std::string string_uid; |
[email protected] | 5a7b5eaf | 2010-11-02 20:52:19 | [diff] [blame] | 61 | }; |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 62 | |
[email protected] | a11fa34 | 2010-07-09 16:56:00 | [diff] [blame] | 63 | // For context menus, these are the contexts where an item can appear. |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 64 | enum Context { |
| 65 | ALL = 1, |
| 66 | PAGE = 2, |
| 67 | SELECTION = 4, |
| 68 | LINK = 8, |
| 69 | EDITABLE = 16, |
| 70 | IMAGE = 32, |
| 71 | VIDEO = 64, |
| 72 | AUDIO = 128, |
[email protected] | 0e5e8d5 | 2011-04-06 21:02:51 | [diff] [blame] | 73 | FRAME = 256, |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 74 | LAUNCHER = 512 |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | // An item can be only one of these types. |
| 78 | enum Type { |
| 79 | NORMAL, |
| 80 | CHECKBOX, |
| 81 | RADIO, |
| 82 | SEPARATOR |
| 83 | }; |
| 84 | |
[email protected] | a11fa34 | 2010-07-09 16:56:00 | [diff] [blame] | 85 | // A list of Contexts for an item. |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 86 | class ContextList { |
| 87 | public: |
| 88 | ContextList() : value_(0) {} |
| 89 | explicit ContextList(Context context) : value_(context) {} |
| 90 | ContextList(const ContextList& other) : value_(other.value_) {} |
| 91 | |
| 92 | void operator=(const ContextList& other) { |
| 93 | value_ = other.value_; |
| 94 | } |
| 95 | |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 96 | bool operator==(const ContextList& other) const { |
| 97 | return value_ == other.value_; |
| 98 | } |
| 99 | |
| 100 | bool operator!=(const ContextList& other) const { |
| 101 | return !(*this == other); |
| 102 | } |
| 103 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 104 | bool Contains(Context context) const { |
| 105 | return (value_ & context) > 0; |
| 106 | } |
| 107 | |
| 108 | void Add(Context context) { |
| 109 | value_ |= context; |
| 110 | } |
| 111 | |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 112 | scoped_ptr<Value> ToValue() const { |
| 113 | return scoped_ptr<Value>(Value::CreateIntegerValue(value_)); |
| 114 | } |
| 115 | |
| 116 | bool Populate(const Value& value) { |
| 117 | int int_value; |
| 118 | if (!value.GetAsInteger(&int_value) || int_value < 0) |
| 119 | return false; |
| 120 | value_ = int_value; |
| 121 | return true; |
| 122 | } |
| 123 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 124 | private: |
| 125 | uint32 value_; // A bitmask of Context values. |
| 126 | }; |
| 127 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 128 | MenuItem(const Id& id, |
| 129 | const std::string& title, |
| 130 | bool checked, |
| 131 | bool enabled, |
| 132 | Type type, |
| 133 | const ContextList& contexts); |
| 134 | virtual ~MenuItem(); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 135 | |
| 136 | // Simple accessor methods. |
[email protected] | 9a4cb81 | 2012-05-23 20:32:32 | [diff] [blame] | 137 | bool incognito() const { return id_.incognito; } |
[email protected] | 5a7b5eaf | 2010-11-02 20:52:19 | [diff] [blame] | 138 | const std::string& extension_id() const { return id_.extension_id; } |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 139 | const std::string& title() const { return title_; } |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 140 | const List& children() { return children_; } |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 141 | const Id& id() const { return id_; } |
| 142 | Id* parent_id() const { return parent_id_.get(); } |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 143 | int child_count() const { return children_.size(); } |
| 144 | ContextList contexts() const { return contexts_; } |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 145 | Type type() const { return type_; } |
| 146 | bool checked() const { return checked_; } |
[email protected] | d4a8b7a | 2012-04-03 07:27:13 | [diff] [blame] | 147 | bool enabled() const { return enabled_; } |
[email protected] | cced75a | 2011-05-20 08:31:12 | [diff] [blame] | 148 | const URLPatternSet& document_url_patterns() const { |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 149 | return document_url_patterns_; |
| 150 | } |
[email protected] | cced75a | 2011-05-20 08:31:12 | [diff] [blame] | 151 | const URLPatternSet& target_url_patterns() const { |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 152 | return target_url_patterns_; |
| 153 | } |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 154 | |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 155 | // Simple mutator methods. |
[email protected] | 48329f9 | 2011-03-28 19:38:22 | [diff] [blame] | 156 | void set_title(const std::string& new_title) { title_ = new_title; } |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 157 | void set_contexts(ContextList contexts) { contexts_ = contexts; } |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 158 | void set_type(Type type) { type_ = type; } |
[email protected] | d4a8b7a | 2012-04-03 07:27:13 | [diff] [blame] | 159 | void set_enabled(bool enabled) { enabled_ = enabled; } |
[email protected] | cced75a | 2011-05-20 08:31:12 | [diff] [blame] | 160 | void set_document_url_patterns(const URLPatternSet& patterns) { |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 161 | document_url_patterns_ = patterns; |
| 162 | } |
[email protected] | cced75a | 2011-05-20 08:31:12 | [diff] [blame] | 163 | void set_target_url_patterns(const URLPatternSet& patterns) { |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 164 | target_url_patterns_ = patterns; |
| 165 | } |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 166 | |
[email protected] | 745feedb | 2010-08-02 04:08:07 | [diff] [blame] | 167 | // Returns the title with any instances of %s replaced by |selection|. The |
| 168 | // result will be no longer than |max_length|. |
| 169 | string16 TitleWithReplacement(const string16& selection, |
| 170 | size_t max_length) const; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 171 | |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 172 | // Sets the checked state to |checked|. Returns true if successful. |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 173 | bool SetChecked(bool checked); |
| 174 | |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 175 | // Converts to Value for serialization to preferences. |
| 176 | scoped_ptr<base::DictionaryValue> ToValue() const; |
| 177 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 178 | // Returns a new MenuItem created from |value|, or NULL if there is |
| 179 | // an error. The caller takes ownership of the MenuItem. |
| 180 | static MenuItem* Populate(const std::string& extension_id, |
| 181 | const DictionaryValue& value, |
| 182 | std::string* error); |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 183 | |
| 184 | // Sets any document and target URL patterns from |properties|. |
[email protected] | 8af81c0 | 2012-08-14 23:06:15 | [diff] [blame] | 185 | bool PopulateURLPatterns(std::vector<std::string>* document_url_patterns, |
| 186 | std::vector<std::string>* target_url_patterns, |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 187 | std::string* error); |
| 188 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 189 | protected: |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 190 | friend class MenuManager; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 191 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 192 | // Takes ownership of |item| and sets its parent_id_. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 193 | void AddChild(MenuItem* item); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 194 | |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 195 | // Takes the child item from this parent. The item is returned and the caller |
| 196 | // then owns the pointer. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 197 | MenuItem* ReleaseChild(const Id& child_id, bool recursive); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 198 | |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 199 | // Recursively appends all descendant items (children, grandchildren, etc.) |
| 200 | // to the output |list|. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 201 | void GetFlattenedSubtree(MenuItem::List* list); |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 202 | |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 203 | // Recursively removes all descendant items (children, grandchildren, etc.), |
| 204 | // returning the ids of the removed items. |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 205 | std::set<Id> RemoveAllDescendants(); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 206 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 207 | private: |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 208 | // The unique id for this item. |
| 209 | Id id_; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 210 | |
| 211 | // What gets shown in the menu for this item. |
| 212 | std::string title_; |
| 213 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 214 | Type type_; |
| 215 | |
| 216 | // This should only be true for items of type CHECKBOX or RADIO. |
| 217 | bool checked_; |
| 218 | |
[email protected] | d4a8b7a | 2012-04-03 07:27:13 | [diff] [blame] | 219 | // If the item is enabled or not. |
| 220 | bool enabled_; |
| 221 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 222 | // In what contexts should the item be shown? |
| 223 | ContextList contexts_; |
| 224 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 225 | // If this item is a child of another item, the unique id of its parent. If |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 226 | // this is a top-level item with no parent, this will be NULL. |
| 227 | scoped_ptr<Id> parent_id_; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 228 | |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 229 | // Patterns for restricting what documents this item will appear for. This |
| 230 | // applies to the frame where the click took place. |
[email protected] | cced75a | 2011-05-20 08:31:12 | [diff] [blame] | 231 | URLPatternSet document_url_patterns_; |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 232 | |
| 233 | // Patterns for restricting where items appear based on the src/href |
| 234 | // attribute of IMAGE/AUDIO/VIDEO/LINK tags. |
[email protected] | cced75a | 2011-05-20 08:31:12 | [diff] [blame] | 235 | URLPatternSet target_url_patterns_; |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 236 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 237 | // Any children this item may have. |
| 238 | List children_; |
| 239 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 240 | DISALLOW_COPY_AND_ASSIGN(MenuItem); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | // This class keeps track of menu items added by extensions. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 244 | class MenuManager : public content::NotificationObserver, |
| 245 | public base::SupportsWeakPtr<MenuManager> { |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 246 | public: |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 247 | explicit MenuManager(Profile* profile); |
| 248 | virtual ~MenuManager(); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 249 | |
| 250 | // Returns the ids of extensions which have menu items registered. |
| 251 | std::set<std::string> ExtensionIds(); |
| 252 | |
| 253 | // Returns a list of all the *top-level* menu items (added via AddContextItem) |
| 254 | // for the given extension id, *not* including child items (added via |
| 255 | // AddChildItem); although those can be reached via the top-level items' |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 256 | // children. A view can then decide how to display these, including whether to |
| 257 | // put them into a submenu if there are more than 1. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 258 | const MenuItem::List* MenuItems(const std::string& extension_id); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 259 | |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 260 | // Adds a top-level menu item for an extension, requiring the |extension| |
| 261 | // pointer so it can load the icon for the extension. Takes ownership of |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 262 | // |item|. Returns a boolean indicating success or failure. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 263 | bool AddContextItem(const Extension* extension, MenuItem* item); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 264 | |
| 265 | // Add an item as a child of another item which has been previously added, and |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 266 | // takes ownership of |item|. Returns a boolean indicating success or failure. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 267 | bool AddChildItem(const MenuItem::Id& parent_id, |
| 268 | MenuItem* child); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 269 | |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 270 | // Makes existing item with |child_id| a child of the item with |parent_id|. |
| 271 | // If the child item was already a child of another parent, this will remove |
| 272 | // it from that parent first. It is an error to try and move an item to be a |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 273 | // child of one of its own descendants. It is legal to pass NULL for |
| 274 | // |parent_id|, which means the item should be moved to the top-level. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 275 | bool ChangeParent(const MenuItem::Id& child_id, |
| 276 | const MenuItem::Id* parent_id); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 277 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 278 | // Removes a context menu item with the given id (whether it is a top-level |
| 279 | // item or a child of some other item), returning true if the item was found |
| 280 | // and removed or false otherwise. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 281 | bool RemoveContextMenuItem(const MenuItem::Id& id); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 282 | |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 283 | // Removes all items for the given extension id. |
[email protected] | 48329f9 | 2011-03-28 19:38:22 | [diff] [blame] | 284 | void RemoveAllContextItems(const std::string& extension_id); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 285 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 286 | // Returns the item with the given |id| or NULL. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 287 | MenuItem* GetItemById(const MenuItem::Id& id) const; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 288 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 289 | // Notify the MenuManager that an item has been updated not through |
| 290 | // an explicit call into MenuManager. For example, if an item is |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 291 | // acquired by a call to GetItemById and changed, then this should be called. |
| 292 | // Returns true if the item was found or false otherwise. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 293 | bool ItemUpdated(const MenuItem::Id& id); |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 294 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 295 | // Called when a menu item is clicked on by the user. |
[email protected] | 33a86dbd | 2012-06-20 03:20:18 | [diff] [blame] | 296 | void ExecuteCommand(Profile* profile, |
| 297 | content::WebContents* web_contents, |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 298 | const content::ContextMenuParams& params, |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 299 | const MenuItem::Id& menu_item_id); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 300 | |
[email protected] | f23a8c1 | 2011-03-15 14:43:43 | [diff] [blame] | 301 | // This returns a bitmap of width/height kFaviconSize, loaded either from an |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 302 | // entry specified in the extension's 'icon' section of the manifest, or a |
| 303 | // default extension icon. |
| 304 | const SkBitmap& GetIconForExtension(const std::string& extension_id); |
| 305 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 306 | // Implements the content::NotificationObserver interface. |
| 307 | virtual void Observe(int type, const content::NotificationSource& source, |
| 308 | const content::NotificationDetails& details) OVERRIDE; |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 309 | |
[email protected] | bec6455 | 2012-06-13 20:25:49 | [diff] [blame] | 310 | // Stores the menu items for the extension in the state storage. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 311 | void WriteToStorage(const Extension* extension); |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 312 | |
[email protected] | bec6455 | 2012-06-13 20:25:49 | [diff] [blame] | 313 | // Reads menu items for the extension from the state storage. Any invalid |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 314 | // items are ignored. |
[email protected] | bec6455 | 2012-06-13 20:25:49 | [diff] [blame] | 315 | void ReadFromStorage(const std::string& extension_id, |
| 316 | scoped_ptr<base::Value> value); |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 317 | |
[email protected] | 6350346 | 2012-10-30 22:14:31 | [diff] [blame] | 318 | // Removes all "incognito" "split" mode context items. |
| 319 | void RemoveAllIncognitoContextItems(); |
| 320 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 321 | private: |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 322 | FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, DeleteParent); |
| 323 | FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, RemoveOneByOne); |
[email protected] | 2b07c93f | 2010-08-02 23:13:04 | [diff] [blame] | 324 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 325 | // This is a helper function which takes care of de-selecting any other radio |
| 326 | // items in the same group (i.e. that are adjacent in the list). |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 327 | void RadioItemSelected(MenuItem* item); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 328 | |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 329 | // Make sure that there is only one radio item selected at once in any run. |
| 330 | // If there are no radio items selected, then the first item in the run |
| 331 | // will get selected. If there are multiple radio items selected, then only |
| 332 | // the last one will get selcted. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 333 | void SanitizeRadioList(const MenuItem::List& item_list); |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 334 | |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 335 | // Returns true if item is a descendant of an item with id |ancestor_id|. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 336 | bool DescendantOf(MenuItem* item, const MenuItem::Id& ancestor_id); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 337 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 338 | // We keep items organized by mapping an extension id to a list of items. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 339 | typedef std::map<std::string, MenuItem::List> MenuItemMap; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 340 | MenuItemMap context_items_; |
| 341 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 342 | // This lets us make lookup by id fast. It maps id to MenuItem* for |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 343 | // all items the menu manager knows about, including all children of top-level |
| 344 | // items. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 345 | std::map<MenuItem::Id, MenuItem*> items_by_id_; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 346 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 347 | content::NotificationRegistrar registrar_; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 348 | |
[email protected] | b671760b | 2010-07-15 21:13:47 | [diff] [blame] | 349 | ExtensionIconManager icon_manager_; |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 350 | |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 351 | Profile* profile_; |
| 352 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 353 | DISALLOW_COPY_AND_ASSIGN(MenuManager); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 354 | }; |
| 355 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 356 | } // namespace extensions |
| 357 | |
| 358 | #endif // CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ |