[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 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 11 | #include <map> |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 12 | #include <memory> |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 13 | #include <set> |
| 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
[email protected] | 1790275 | 2011-08-31 22:52:54 | [diff] [blame] | 17 | #include "base/compiler_specific.h" |
[email protected] | 2b07c93f | 2010-08-02 23:13:04 | [diff] [blame] | 18 | #include "base/gtest_prod_util.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 19 | #include "base/macros.h" |
[email protected] | bec6455 | 2012-06-13 20:25:49 | [diff] [blame] | 20 | #include "base/memory/weak_ptr.h" |
[email protected] | 00ee2f5 | 2014-04-25 09:25:52 | [diff] [blame] | 21 | #include "base/scoped_observer.h" |
[email protected] | 46acbf1 | 2013-06-10 18:43:42 | [diff] [blame] | 22 | #include "base/strings/string16.h" |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 23 | #include "base/values.h" |
[email protected] | b671760b | 2010-07-15 21:13:47 | [diff] [blame] | 24 | #include "chrome/browser/extensions/extension_icon_manager.h" |
[email protected] | b33f0b11 | 2014-03-13 17:05:30 | [diff] [blame] | 25 | #include "components/keyed_service/core/keyed_service.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 26 | #include "content/public/browser/notification_observer.h" |
| 27 | #include "content/public/browser/notification_registrar.h" |
Evan Stade | 75872a6 | 2019-09-06 21:17:38 | [diff] [blame] | 28 | #include "extensions/browser/extension_registry.h" |
[email protected] | 00ee2f5 | 2014-04-25 09:25:52 | [diff] [blame] | 29 | #include "extensions/browser/extension_registry_observer.h" |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 30 | #include "extensions/common/url_pattern_set.h" |
estade | 32426e0 | 2016-12-18 01:26:17 | [diff] [blame] | 31 | #include "ui/gfx/image/image.h" |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 32 | |
| 33 | namespace content { |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 34 | class BrowserContext; |
rob | cbe35ba | 2016-03-10 01:20:49 | [diff] [blame] | 35 | class RenderFrameHost; |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 36 | class WebContents; |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 37 | struct ContextMenuParams; |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 38 | } |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 39 | |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 40 | namespace extensions { |
| 41 | class Extension; |
[email protected] | b673a594 | 2013-11-14 11:14:19 | [diff] [blame] | 42 | class StateStore; |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 43 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 44 | // Represents a menu item added by an extension. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 45 | class MenuItem { |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 46 | public: |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 47 | using List = std::vector<MenuItem*>; |
| 48 | using OwnedList = std::vector<std::unique_ptr<MenuItem>>; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 49 | |
paulmeyer | ed43726 | 2015-07-09 16:05:29 | [diff] [blame] | 50 | // Key used to identify which extension a menu item belongs to. A menu item |
| 51 | // can also belong to a <webview>, in which case |webview_embedder_process_id| |
| 52 | // and |webview_instance_id| will be non-zero. When two ExtensionKeys are |
| 53 | // compared, an empty |extension_id| will match any other extension ID. This |
| 54 | // allows menu items belonging to webviews to be found with only the two |
| 55 | // webview IDs when the extension ID is not known. This is currently done from |
| 56 | // ChromeExtensionsBrowserClient::CleanUpWebView(). |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 57 | struct ExtensionKey { |
| 58 | std::string extension_id; |
paulmeyer | ed43726 | 2015-07-09 16:05:29 | [diff] [blame] | 59 | int webview_embedder_process_id; |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 60 | int webview_instance_id; |
| 61 | |
| 62 | ExtensionKey(); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 63 | explicit ExtensionKey(const std::string& extension_id); |
paulmeyer | ed43726 | 2015-07-09 16:05:29 | [diff] [blame] | 64 | ExtensionKey(const std::string& extension_id, |
| 65 | int webview_embedder_process_id, |
| 66 | int webview_instance_id); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 67 | |
| 68 | bool operator==(const ExtensionKey& other) const; |
| 69 | bool operator!=(const ExtensionKey& other) const; |
| 70 | bool operator<(const ExtensionKey& other) const; |
| 71 | |
| 72 | bool empty() const; |
| 73 | }; |
| 74 | |
[email protected] | 5a7b5eaf | 2010-11-02 20:52:19 | [diff] [blame] | 75 | // An Id uniquely identifies a context menu item registered by an extension. |
| 76 | struct Id { |
| 77 | Id(); |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 78 | // Since the unique ID (uid or string_uid) is parsed from API arguments, |
| 79 | // the normal usage is to set the uid or string_uid immediately after |
| 80 | // construction. |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 81 | Id(bool incognito, const ExtensionKey& extension_key); |
[email protected] | 5a7b5eaf | 2010-11-02 20:52:19 | [diff] [blame] | 82 | ~Id(); |
| 83 | |
| 84 | bool operator==(const Id& other) const; |
| 85 | bool operator!=(const Id& other) const; |
| 86 | bool operator<(const Id& other) const; |
| 87 | |
[email protected] | 9a4cb81 | 2012-05-23 20:32:32 | [diff] [blame] | 88 | bool incognito; |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 89 | ExtensionKey extension_key; |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 90 | // Only one of uid or string_uid will be defined. |
[email protected] | 5a7b5eaf | 2010-11-02 20:52:19 | [diff] [blame] | 91 | int uid; |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 92 | std::string string_uid; |
[email protected] | 5a7b5eaf | 2010-11-02 20:52:19 | [diff] [blame] | 93 | }; |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 94 | |
[email protected] | a11fa34 | 2010-07-09 16:56:00 | [diff] [blame] | 95 | // For context menus, these are the contexts where an item can appear. |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 96 | enum Context { |
| 97 | ALL = 1, |
| 98 | PAGE = 2, |
| 99 | SELECTION = 4, |
| 100 | LINK = 8, |
| 101 | EDITABLE = 16, |
| 102 | IMAGE = 32, |
| 103 | VIDEO = 64, |
| 104 | AUDIO = 128, |
[email protected] | 0e5e8d5 | 2011-04-06 21:02:51 | [diff] [blame] | 105 | FRAME = 256, |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 106 | LAUNCHER = 512, |
| 107 | BROWSER_ACTION = 1024, |
| 108 | PAGE_ACTION = 2048 |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | // An item can be only one of these types. |
| 112 | enum Type { |
| 113 | NORMAL, |
| 114 | CHECKBOX, |
| 115 | RADIO, |
| 116 | SEPARATOR |
| 117 | }; |
| 118 | |
[email protected] | a11fa34 | 2010-07-09 16:56:00 | [diff] [blame] | 119 | // A list of Contexts for an item. |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 120 | class ContextList { |
| 121 | public: |
| 122 | ContextList() : value_(0) {} |
| 123 | explicit ContextList(Context context) : value_(context) {} |
| 124 | ContextList(const ContextList& other) : value_(other.value_) {} |
| 125 | |
| 126 | void operator=(const ContextList& other) { |
| 127 | value_ = other.value_; |
| 128 | } |
| 129 | |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 130 | bool operator==(const ContextList& other) const { |
| 131 | return value_ == other.value_; |
| 132 | } |
| 133 | |
| 134 | bool operator!=(const ContextList& other) const { |
| 135 | return !(*this == other); |
| 136 | } |
| 137 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 138 | bool Contains(Context context) const { |
| 139 | return (value_ & context) > 0; |
| 140 | } |
| 141 | |
| 142 | void Add(Context context) { |
| 143 | value_ |= context; |
| 144 | } |
| 145 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 146 | std::unique_ptr<base::Value> ToValue() const { |
| 147 | return std::unique_ptr<base::Value>( |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 148 | new base::Value(static_cast<int>(value_))); |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 149 | } |
| 150 | |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 151 | bool Populate(const base::Value& value) { |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 152 | int int_value; |
| 153 | if (!value.GetAsInteger(&int_value) || int_value < 0) |
| 154 | return false; |
| 155 | value_ = int_value; |
| 156 | return true; |
| 157 | } |
| 158 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 159 | private: |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 160 | uint32_t value_; // A bitmask of Context values. |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 161 | }; |
| 162 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 163 | MenuItem(const Id& id, |
| 164 | const std::string& title, |
| 165 | bool checked, |
catmullings | ff2cdbc | 2017-08-22 22:07:02 | [diff] [blame] | 166 | bool visible, |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 167 | bool enabled, |
| 168 | Type type, |
| 169 | const ContextList& contexts); |
| 170 | virtual ~MenuItem(); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 171 | |
| 172 | // Simple accessor methods. |
[email protected] | 9a4cb81 | 2012-05-23 20:32:32 | [diff] [blame] | 173 | bool incognito() const { return id_.incognito; } |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 174 | const std::string& extension_id() const { |
| 175 | return id_.extension_key.extension_id; |
| 176 | } |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 177 | const std::string& title() const { return title_; } |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 178 | const OwnedList& children() { return children_; } |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 179 | const Id& id() const { return id_; } |
| 180 | Id* parent_id() const { return parent_id_.get(); } |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 181 | const ContextList& contexts() const { return contexts_; } |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 182 | Type type() const { return type_; } |
| 183 | bool checked() const { return checked_; } |
catmullings | ff2cdbc | 2017-08-22 22:07:02 | [diff] [blame] | 184 | bool visible() const { return visible_; } |
[email protected] | d4a8b7a | 2012-04-03 07:27:13 | [diff] [blame] | 185 | bool enabled() const { return enabled_; } |
[email protected] | cced75a | 2011-05-20 08:31:12 | [diff] [blame] | 186 | const URLPatternSet& document_url_patterns() const { |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 187 | return document_url_patterns_; |
| 188 | } |
[email protected] | cced75a | 2011-05-20 08:31:12 | [diff] [blame] | 189 | const URLPatternSet& target_url_patterns() const { |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 190 | return target_url_patterns_; |
| 191 | } |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 192 | |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 193 | // Simple mutator methods. |
[email protected] | 48329f9 | 2011-03-28 19:38:22 | [diff] [blame] | 194 | void set_title(const std::string& new_title) { title_ = new_title; } |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 195 | void set_contexts(ContextList contexts) { contexts_ = contexts; } |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 196 | void set_type(Type type) { type_ = type; } |
catmullings | ff2cdbc | 2017-08-22 22:07:02 | [diff] [blame] | 197 | void set_visible(bool visible) { visible_ = visible; } |
[email protected] | d4a8b7a | 2012-04-03 07:27:13 | [diff] [blame] | 198 | void set_enabled(bool enabled) { enabled_ = enabled; } |
[email protected] | cced75a | 2011-05-20 08:31:12 | [diff] [blame] | 199 | void set_document_url_patterns(const URLPatternSet& patterns) { |
Devlin Cronin | 11860c98 | 2018-12-14 20:18:27 | [diff] [blame] | 200 | document_url_patterns_ = patterns.Clone(); |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 201 | } |
[email protected] | cced75a | 2011-05-20 08:31:12 | [diff] [blame] | 202 | void set_target_url_patterns(const URLPatternSet& patterns) { |
Devlin Cronin | 11860c98 | 2018-12-14 20:18:27 | [diff] [blame] | 203 | target_url_patterns_ = patterns.Clone(); |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 204 | } |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 205 | |
[email protected] | 745feedb | 2010-08-02 04:08:07 | [diff] [blame] | 206 | // Returns the title with any instances of %s replaced by |selection|. The |
| 207 | // result will be no longer than |max_length|. |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 208 | base::string16 TitleWithReplacement(const base::string16& selection, |
[email protected] | 745feedb | 2010-08-02 04:08:07 | [diff] [blame] | 209 | size_t max_length) const; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 210 | |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 211 | // Sets the checked state to |checked|. Returns true if successful. |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 212 | bool SetChecked(bool checked); |
| 213 | |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 214 | // Converts to Value for serialization to preferences. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 215 | std::unique_ptr<base::DictionaryValue> ToValue() const; |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 216 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 217 | // Returns a new MenuItem created from |value|, or NULL if there is |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 218 | // an error. |
| 219 | static std::unique_ptr<MenuItem> Populate(const std::string& extension_id, |
| 220 | const base::DictionaryValue& value, |
| 221 | std::string* error); |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 222 | |
| 223 | // Sets any document and target URL patterns from |properties|. |
[email protected] | 8af81c0 | 2012-08-14 23:06:15 | [diff] [blame] | 224 | bool PopulateURLPatterns(std::vector<std::string>* document_url_patterns, |
| 225 | std::vector<std::string>* target_url_patterns, |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 226 | std::string* error); |
| 227 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 228 | protected: |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 229 | friend class MenuManager; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 230 | |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 231 | // Adds |item| and sets its parent_id_. |
| 232 | void AddChild(std::unique_ptr<MenuItem> item); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 233 | |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 234 | // Removes the child item from this parent and returns it. |
| 235 | std::unique_ptr<MenuItem> ReleaseChild(const Id& child_id, bool recursive); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 236 | |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 237 | // Recursively appends all descendant items (children, grandchildren, etc.) |
| 238 | // to the output |list|. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 239 | void GetFlattenedSubtree(MenuItem::List* list); |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 240 | |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 241 | // Recursively removes all descendant items (children, grandchildren, etc.), |
| 242 | // returning the ids of the removed items. |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 243 | std::set<Id> RemoveAllDescendants(); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 244 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 245 | private: |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 246 | // The unique id for this item. |
| 247 | Id id_; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 248 | |
| 249 | // What gets shown in the menu for this item. |
| 250 | std::string title_; |
| 251 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 252 | Type type_; |
| 253 | |
| 254 | // This should only be true for items of type CHECKBOX or RADIO. |
| 255 | bool checked_; |
| 256 | |
catmullings | ff2cdbc | 2017-08-22 22:07:02 | [diff] [blame] | 257 | // If the item is visible (shown or hidden) in the menu. |
| 258 | bool visible_; |
| 259 | |
[email protected] | d4a8b7a | 2012-04-03 07:27:13 | [diff] [blame] | 260 | // If the item is enabled or not. |
| 261 | bool enabled_; |
| 262 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 263 | // In what contexts should the item be shown? |
| 264 | ContextList contexts_; |
| 265 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 266 | // 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] | 267 | // this is a top-level item with no parent, this will be NULL. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 268 | std::unique_ptr<Id> parent_id_; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 269 | |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 270 | // Patterns for restricting what documents this item will appear for. This |
| 271 | // applies to the frame where the click took place. |
[email protected] | cced75a | 2011-05-20 08:31:12 | [diff] [blame] | 272 | URLPatternSet document_url_patterns_; |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 273 | |
| 274 | // Patterns for restricting where items appear based on the src/href |
| 275 | // attribute of IMAGE/AUDIO/VIDEO/LINK tags. |
[email protected] | cced75a | 2011-05-20 08:31:12 | [diff] [blame] | 276 | URLPatternSet target_url_patterns_; |
[email protected] | 9e9d791 | 2010-07-18 21:05:28 | [diff] [blame] | 277 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 278 | // Any children this item may have. |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 279 | OwnedList children_; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 280 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 281 | DISALLOW_COPY_AND_ASSIGN(MenuItem); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 282 | }; |
| 283 | |
| 284 | // This class keeps track of menu items added by extensions. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 285 | class MenuManager : public content::NotificationObserver, |
[email protected] | b673a594 | 2013-11-14 11:14:19 | [diff] [blame] | 286 | public base::SupportsWeakPtr<MenuManager>, |
[email protected] | 00ee2f5 | 2014-04-25 09:25:52 | [diff] [blame] | 287 | public KeyedService, |
| 288 | public ExtensionRegistryObserver { |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 289 | public: |
[email protected] | db6bf7f | 2014-03-27 15:49:26 | [diff] [blame] | 290 | static const char kOnContextMenus[]; |
| 291 | static const char kOnWebviewContextMenus[]; |
| 292 | |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 293 | MenuManager(content::BrowserContext* context, StateStore* store_); |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 294 | ~MenuManager() override; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 295 | |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 296 | // Convenience function to get the MenuManager for a browser context. |
| 297 | static MenuManager* Get(content::BrowserContext* context); |
[email protected] | b673a594 | 2013-11-14 11:14:19 | [diff] [blame] | 298 | |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 299 | // Returns the keys of extensions which have menu items registered. |
| 300 | std::set<MenuItem::ExtensionKey> ExtensionIds(); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 301 | |
| 302 | // Returns a list of all the *top-level* menu items (added via AddContextItem) |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 303 | // for the given extension specified by |extension_key|, *not* including child |
| 304 | // items (added via AddChildItem); although those can be reached via the |
| 305 | // top-level items' children. A view can then decide how to display these, |
| 306 | // including whether to put them into a submenu if there are more than 1. |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 307 | const MenuItem::OwnedList* MenuItems( |
| 308 | const MenuItem::ExtensionKey& extension_key); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 309 | |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 310 | // Adds a top-level menu item for an extension, requiring the |extension| |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 311 | // pointer so it can load the icon for the extension. Returns a boolean |
| 312 | // indicating success or failure. |
| 313 | bool AddContextItem(const Extension* extension, |
| 314 | std::unique_ptr<MenuItem> item); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 315 | |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 316 | // Add an item as a child of another item which has been previously added. |
| 317 | // Returns a boolean indicating success or failure. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 318 | bool AddChildItem(const MenuItem::Id& parent_id, |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 319 | std::unique_ptr<MenuItem> child); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 320 | |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 321 | // Makes existing item with |child_id| a child of the item with |parent_id|. |
| 322 | // If the child item was already a child of another parent, this will remove |
| 323 | // 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] | 324 | // child of one of its own descendants. It is legal to pass NULL for |
| 325 | // |parent_id|, which means the item should be moved to the top-level. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 326 | bool ChangeParent(const MenuItem::Id& child_id, |
| 327 | const MenuItem::Id* parent_id); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 328 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 329 | // Removes a context menu item with the given id (whether it is a top-level |
| 330 | // item or a child of some other item), returning true if the item was found |
| 331 | // and removed or false otherwise. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 332 | bool RemoveContextMenuItem(const MenuItem::Id& id); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 333 | |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 334 | // Removes all items for the given extension specified by |extension_key|. |
| 335 | void RemoveAllContextItems(const MenuItem::ExtensionKey& extension_key); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 336 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 337 | // Returns the item with the given |id| or NULL. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 338 | MenuItem* GetItemById(const MenuItem::Id& id) const; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 339 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 340 | // Notify the MenuManager that an item has been updated not through |
| 341 | // an explicit call into MenuManager. For example, if an item is |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 342 | // acquired by a call to GetItemById and changed, then this should be called. |
| 343 | // Returns true if the item was found or false otherwise. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 344 | bool ItemUpdated(const MenuItem::Id& id); |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 345 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 346 | // Called when a menu item is clicked on by the user. |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 347 | void ExecuteCommand(content::BrowserContext* context, |
[email protected] | 33a86dbd | 2012-06-20 03:20:18 | [diff] [blame] | 348 | content::WebContents* web_contents, |
rob | cbe35ba | 2016-03-10 01:20:49 | [diff] [blame] | 349 | content::RenderFrameHost* render_frame_host, |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 350 | const content::ContextMenuParams& params, |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 351 | const MenuItem::Id& menu_item_id); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 352 | |
estade | 32426e0 | 2016-12-18 01:26:17 | [diff] [blame] | 353 | // This returns a image of width/height kFaviconSize, loaded either from an |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 354 | // entry specified in the extension's 'icon' section of the manifest, or a |
| 355 | // default extension icon. |
estade | 32426e0 | 2016-12-18 01:26:17 | [diff] [blame] | 356 | gfx::Image GetIconForExtension(const std::string& extension_id); |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 357 | |
[email protected] | 00ee2f5 | 2014-04-25 09:25:52 | [diff] [blame] | 358 | // content::NotificationObserver implementation. |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 359 | void Observe(int type, |
| 360 | const content::NotificationSource& source, |
| 361 | const content::NotificationDetails& details) override; |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 362 | |
[email protected] | 00ee2f5 | 2014-04-25 09:25:52 | [diff] [blame] | 363 | // ExtensionRegistryObserver implementation. |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 364 | void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 365 | const Extension* extension) override; |
| 366 | void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 367 | const Extension* extension, |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 368 | UnloadedExtensionReason reason) override; |
[email protected] | 00ee2f5 | 2014-04-25 09:25:52 | [diff] [blame] | 369 | |
[email protected] | bec6455 | 2012-06-13 20:25:49 | [diff] [blame] | 370 | // Stores the menu items for the extension in the state storage. |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 371 | void WriteToStorage(const Extension* extension, |
| 372 | const MenuItem::ExtensionKey& extension_key); |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 373 | |
[email protected] | bec6455 | 2012-06-13 20:25:49 | [diff] [blame] | 374 | // Reads menu items for the extension from the state storage. Any invalid |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 375 | // items are ignored. |
[email protected] | bec6455 | 2012-06-13 20:25:49 | [diff] [blame] | 376 | void ReadFromStorage(const std::string& extension_id, |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 377 | std::unique_ptr<base::Value> value); |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 378 | |
[email protected] | 6350346 | 2012-10-30 22:14:31 | [diff] [blame] | 379 | // Removes all "incognito" "split" mode context items. |
| 380 | void RemoveAllIncognitoContextItems(); |
| 381 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 382 | private: |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 383 | FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, DeleteParent); |
| 384 | FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, RemoveOneByOne); |
[email protected] | 2b07c93f | 2010-08-02 23:13:04 | [diff] [blame] | 385 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 386 | // This is a helper function which takes care of de-selecting any other radio |
| 387 | // items in the same group (i.e. that are adjacent in the list). |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 388 | void RadioItemSelected(MenuItem* item); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 389 | |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 390 | // Make sure that there is only one radio item selected at once in any run. |
| 391 | // If there are no radio items selected, then the first item in the run |
| 392 | // will get selected. If there are multiple radio items selected, then only |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 393 | // the last one will get selected. |
Catherine Mullings | 97a2769d | 2017-08-04 17:55:20 | [diff] [blame] | 394 | void SanitizeRadioListsInMenu(const MenuItem::OwnedList& item_list); |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 395 | |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 396 | // 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] | 397 | bool DescendantOf(MenuItem* item, const MenuItem::Id& ancestor_id); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 398 | |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 399 | // We keep items organized by mapping ExtensionKey to a list of items. |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 400 | std::map<MenuItem::ExtensionKey, MenuItem::OwnedList> context_items_; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 401 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 402 | // 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] | 403 | // all items the menu manager knows about, including all children of top-level |
| 404 | // items. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 405 | std::map<MenuItem::Id, MenuItem*> items_by_id_; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 406 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 407 | content::NotificationRegistrar registrar_; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 408 | |
[email protected] | 00ee2f5 | 2014-04-25 09:25:52 | [diff] [blame] | 409 | // Listen to extension load, unloaded notifications. |
| 410 | ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
Evan Stade | 75872a6 | 2019-09-06 21:17:38 | [diff] [blame] | 411 | extension_registry_observer_{this}; |
[email protected] | 00ee2f5 | 2014-04-25 09:25:52 | [diff] [blame] | 412 | |
[email protected] | b671760b | 2010-07-15 21:13:47 | [diff] [blame] | 413 | ExtensionIconManager icon_manager_; |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 414 | |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 415 | content::BrowserContext* browser_context_; |
[email protected] | a001147 | 2012-05-30 17:15:40 | [diff] [blame] | 416 | |
[email protected] | b673a594 | 2013-11-14 11:14:19 | [diff] [blame] | 417 | // Owned by ExtensionSystem. |
| 418 | StateStore* store_; |
| 419 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 420 | DISALLOW_COPY_AND_ASSIGN(MenuManager); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 421 | }; |
| 422 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 423 | } // namespace extensions |
| 424 | |
| 425 | #endif // CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ |