blob: ea6cecf90de2aec43d6ac8c8f05304b7d76a279c [file] [log] [blame]
[email protected]7dddebc32012-01-11 22:01:031// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]2e3b5202010-03-23 06:52:412// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]5aeeae12012-07-05 19:13:115#ifndef CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_
6#define CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_
[email protected]2e3b5202010-03-23 06:52:417
8#include <map>
9#include <set>
10#include <string>
11#include <vector>
12
13#include "base/basictypes.h"
[email protected]17902752011-08-31 22:52:5414#include "base/compiler_specific.h"
[email protected]2b07c93f2010-08-02 23:13:0415#include "base/gtest_prod_util.h"
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/scoped_ptr.h"
[email protected]bec64552012-06-13 20:25:4917#include "base/memory/weak_ptr.h"
[email protected]46acbf12013-06-10 18:43:4218#include "base/strings/string16.h"
[email protected]a0011472012-05-30 17:15:4019#include "base/values.h"
[email protected]b671760b2010-07-15 21:13:4720#include "chrome/browser/extensions/extension_icon_manager.h"
[email protected]b673a5942013-11-14 11:14:1921#include "components/browser_context_keyed_service/browser_context_keyed_service.h"
[email protected]6c2381d2011-10-19 02:52:5322#include "content/public/browser/notification_observer.h"
23#include "content/public/browser/notification_registrar.h"
[email protected]e9f541a2012-11-19 21:52:3124#include "extensions/common/url_pattern_set.h"
[email protected]2e3b5202010-03-23 06:52:4125
[email protected]2e3b5202010-03-23 06:52:4126
[email protected]2e3b5202010-03-23 06:52:4127class Profile;
[email protected]b671760b2010-07-15 21:13:4728class SkBitmap;
[email protected]ea049a02011-12-25 21:37:0929
30namespace content {
31class WebContents;
[email protected]35be7ec2012-02-12 20:42:5132struct ContextMenuParams;
[email protected]ea049a02011-12-25 21:37:0933}
[email protected]2e3b5202010-03-23 06:52:4134
[email protected]1c321ee52012-05-21 03:02:3435namespace extensions {
36class Extension;
[email protected]b673a5942013-11-14 11:14:1937class StateStore;
[email protected]1c321ee52012-05-21 03:02:3438
[email protected]2e3b5202010-03-23 06:52:4139// Represents a menu item added by an extension.
[email protected]5aeeae12012-07-05 19:13:1140class MenuItem {
[email protected]2e3b5202010-03-23 06:52:4141 public:
[email protected]5aeeae12012-07-05 19:13:1142 // A list of MenuItems.
43 typedef std::vector<MenuItem*> List;
[email protected]2e3b5202010-03-23 06:52:4144
[email protected]5a7b5eaf2010-11-02 20:52:1945 // An Id uniquely identifies a context menu item registered by an extension.
46 struct Id {
47 Id();
[email protected]619c5dba2012-05-16 00:44:4848 // Since the unique ID (uid or string_uid) is parsed from API arguments,
49 // the normal usage is to set the uid or string_uid immediately after
50 // construction.
[email protected]9a4cb812012-05-23 20:32:3251 Id(bool incognito, const std::string& extension_id);
[email protected]5a7b5eaf2010-11-02 20:52:1952 ~Id();
53
54 bool operator==(const Id& other) const;
55 bool operator!=(const Id& other) const;
56 bool operator<(const Id& other) const;
57
[email protected]9a4cb812012-05-23 20:32:3258 bool incognito;
[email protected]5a7b5eaf2010-11-02 20:52:1959 std::string extension_id;
[email protected]619c5dba2012-05-16 00:44:4860 // Only one of uid or string_uid will be defined.
[email protected]5a7b5eaf2010-11-02 20:52:1961 int uid;
[email protected]619c5dba2012-05-16 00:44:4862 std::string string_uid;
[email protected]5a7b5eaf2010-11-02 20:52:1963 };
[email protected]9e9d7912010-07-18 21:05:2864
[email protected]a11fa342010-07-09 16:56:0065 // For context menus, these are the contexts where an item can appear.
[email protected]2e3b5202010-03-23 06:52:4166 enum Context {
67 ALL = 1,
68 PAGE = 2,
69 SELECTION = 4,
70 LINK = 8,
71 EDITABLE = 16,
72 IMAGE = 32,
73 VIDEO = 64,
74 AUDIO = 128,
[email protected]0e5e8d52011-04-06 21:02:5175 FRAME = 256,
[email protected]4f8a4d12012-09-28 19:23:0976 LAUNCHER = 512
[email protected]2e3b5202010-03-23 06:52:4177 };
78
79 // An item can be only one of these types.
80 enum Type {
81 NORMAL,
82 CHECKBOX,
83 RADIO,
84 SEPARATOR
85 };
86
[email protected]a11fa342010-07-09 16:56:0087 // A list of Contexts for an item.
[email protected]2e3b5202010-03-23 06:52:4188 class ContextList {
89 public:
90 ContextList() : value_(0) {}
91 explicit ContextList(Context context) : value_(context) {}
92 ContextList(const ContextList& other) : value_(other.value_) {}
93
94 void operator=(const ContextList& other) {
95 value_ = other.value_;
96 }
97
[email protected]66dbfb2c2010-05-12 20:20:1598 bool operator==(const ContextList& other) const {
99 return value_ == other.value_;
100 }
101
102 bool operator!=(const ContextList& other) const {
103 return !(*this == other);
104 }
105
[email protected]2e3b5202010-03-23 06:52:41106 bool Contains(Context context) const {
107 return (value_ & context) > 0;
108 }
109
110 void Add(Context context) {
111 value_ |= context;
112 }
113
[email protected]aeca23f2013-06-21 22:34:41114 scoped_ptr<base::Value> ToValue() const {
[email protected]aa15e2e2013-08-14 02:13:58115 return scoped_ptr<base::Value>(
116 new base::FundamentalValue(static_cast<int>(value_)));
[email protected]a0011472012-05-30 17:15:40117 }
118
[email protected]aeca23f2013-06-21 22:34:41119 bool Populate(const base::Value& value) {
[email protected]a0011472012-05-30 17:15:40120 int int_value;
121 if (!value.GetAsInteger(&int_value) || int_value < 0)
122 return false;
123 value_ = int_value;
124 return true;
125 }
126
[email protected]2e3b5202010-03-23 06:52:41127 private:
128 uint32 value_; // A bitmask of Context values.
129 };
130
[email protected]5aeeae12012-07-05 19:13:11131 MenuItem(const Id& id,
132 const std::string& title,
133 bool checked,
134 bool enabled,
135 Type type,
136 const ContextList& contexts);
137 virtual ~MenuItem();
[email protected]2e3b5202010-03-23 06:52:41138
139 // Simple accessor methods.
[email protected]9a4cb812012-05-23 20:32:32140 bool incognito() const { return id_.incognito; }
[email protected]5a7b5eaf2010-11-02 20:52:19141 const std::string& extension_id() const { return id_.extension_id; }
[email protected]2e3b5202010-03-23 06:52:41142 const std::string& title() const { return title_; }
[email protected]63a414b52010-06-03 23:20:49143 const List& children() { return children_; }
[email protected]f4f04592010-07-14 20:40:13144 const Id& id() const { return id_; }
145 Id* parent_id() const { return parent_id_.get(); }
[email protected]2e3b5202010-03-23 06:52:41146 int child_count() const { return children_.size(); }
147 ContextList contexts() const { return contexts_; }
[email protected]2e3b5202010-03-23 06:52:41148 Type type() const { return type_; }
149 bool checked() const { return checked_; }
[email protected]d4a8b7a2012-04-03 07:27:13150 bool enabled() const { return enabled_; }
[email protected]cced75a2011-05-20 08:31:12151 const URLPatternSet& document_url_patterns() const {
[email protected]9e9d7912010-07-18 21:05:28152 return document_url_patterns_;
153 }
[email protected]cced75a2011-05-20 08:31:12154 const URLPatternSet& target_url_patterns() const {
[email protected]9e9d7912010-07-18 21:05:28155 return target_url_patterns_;
156 }
[email protected]2e3b5202010-03-23 06:52:41157
[email protected]66dbfb2c2010-05-12 20:20:15158 // Simple mutator methods.
[email protected]48329f92011-03-28 19:38:22159 void set_title(const std::string& new_title) { title_ = new_title; }
[email protected]66dbfb2c2010-05-12 20:20:15160 void set_contexts(ContextList contexts) { contexts_ = contexts; }
[email protected]66dbfb2c2010-05-12 20:20:15161 void set_type(Type type) { type_ = type; }
[email protected]d4a8b7a2012-04-03 07:27:13162 void set_enabled(bool enabled) { enabled_ = enabled; }
[email protected]cced75a2011-05-20 08:31:12163 void set_document_url_patterns(const URLPatternSet& patterns) {
[email protected]9e9d7912010-07-18 21:05:28164 document_url_patterns_ = patterns;
165 }
[email protected]cced75a2011-05-20 08:31:12166 void set_target_url_patterns(const URLPatternSet& patterns) {
[email protected]9e9d7912010-07-18 21:05:28167 target_url_patterns_ = patterns;
168 }
[email protected]66dbfb2c2010-05-12 20:20:15169
[email protected]745feedb2010-08-02 04:08:07170 // Returns the title with any instances of %s replaced by |selection|. The
171 // result will be no longer than |max_length|.
[email protected]439f1e32013-12-09 20:09:09172 base::string16 TitleWithReplacement(const base::string16& selection,
[email protected]745feedb2010-08-02 04:08:07173 size_t max_length) const;
[email protected]2e3b5202010-03-23 06:52:41174
[email protected]a0011472012-05-30 17:15:40175 // Sets the checked state to |checked|. Returns true if successful.
[email protected]66dbfb2c2010-05-12 20:20:15176 bool SetChecked(bool checked);
177
[email protected]a0011472012-05-30 17:15:40178 // Converts to Value for serialization to preferences.
179 scoped_ptr<base::DictionaryValue> ToValue() const;
180
[email protected]5aeeae12012-07-05 19:13:11181 // Returns a new MenuItem created from |value|, or NULL if there is
182 // an error. The caller takes ownership of the MenuItem.
183 static MenuItem* Populate(const std::string& extension_id,
[email protected]aeca23f2013-06-21 22:34:41184 const base::DictionaryValue& value,
[email protected]5aeeae12012-07-05 19:13:11185 std::string* error);
[email protected]a0011472012-05-30 17:15:40186
187 // Sets any document and target URL patterns from |properties|.
[email protected]8af81c02012-08-14 23:06:15188 bool PopulateURLPatterns(std::vector<std::string>* document_url_patterns,
189 std::vector<std::string>* target_url_patterns,
[email protected]a0011472012-05-30 17:15:40190 std::string* error);
191
[email protected]2e3b5202010-03-23 06:52:41192 protected:
[email protected]5aeeae12012-07-05 19:13:11193 friend class MenuManager;
[email protected]2e3b5202010-03-23 06:52:41194
[email protected]2e3b5202010-03-23 06:52:41195 // Takes ownership of |item| and sets its parent_id_.
[email protected]5aeeae12012-07-05 19:13:11196 void AddChild(MenuItem* item);
[email protected]2e3b5202010-03-23 06:52:41197
[email protected]66dbfb2c2010-05-12 20:20:15198 // Takes the child item from this parent. The item is returned and the caller
199 // then owns the pointer.
[email protected]5aeeae12012-07-05 19:13:11200 MenuItem* ReleaseChild(const Id& child_id, bool recursive);
[email protected]66dbfb2c2010-05-12 20:20:15201
[email protected]a0011472012-05-30 17:15:40202 // Recursively appends all descendant items (children, grandchildren, etc.)
203 // to the output |list|.
[email protected]5aeeae12012-07-05 19:13:11204 void GetFlattenedSubtree(MenuItem::List* list);
[email protected]a0011472012-05-30 17:15:40205
[email protected]66dbfb2c2010-05-12 20:20:15206 // Recursively removes all descendant items (children, grandchildren, etc.),
207 // returning the ids of the removed items.
[email protected]f4f04592010-07-14 20:40:13208 std::set<Id> RemoveAllDescendants();
[email protected]66dbfb2c2010-05-12 20:20:15209
[email protected]2e3b5202010-03-23 06:52:41210 private:
[email protected]f4f04592010-07-14 20:40:13211 // The unique id for this item.
212 Id id_;
[email protected]2e3b5202010-03-23 06:52:41213
214 // What gets shown in the menu for this item.
215 std::string title_;
216
[email protected]2e3b5202010-03-23 06:52:41217 Type type_;
218
219 // This should only be true for items of type CHECKBOX or RADIO.
220 bool checked_;
221
[email protected]d4a8b7a2012-04-03 07:27:13222 // If the item is enabled or not.
223 bool enabled_;
224
[email protected]2e3b5202010-03-23 06:52:41225 // In what contexts should the item be shown?
226 ContextList contexts_;
227
[email protected]2e3b5202010-03-23 06:52:41228 // If this item is a child of another item, the unique id of its parent. If
[email protected]f4f04592010-07-14 20:40:13229 // this is a top-level item with no parent, this will be NULL.
230 scoped_ptr<Id> parent_id_;
[email protected]2e3b5202010-03-23 06:52:41231
[email protected]9e9d7912010-07-18 21:05:28232 // Patterns for restricting what documents this item will appear for. This
233 // applies to the frame where the click took place.
[email protected]cced75a2011-05-20 08:31:12234 URLPatternSet document_url_patterns_;
[email protected]9e9d7912010-07-18 21:05:28235
236 // Patterns for restricting where items appear based on the src/href
237 // attribute of IMAGE/AUDIO/VIDEO/LINK tags.
[email protected]cced75a2011-05-20 08:31:12238 URLPatternSet target_url_patterns_;
[email protected]9e9d7912010-07-18 21:05:28239
[email protected]2e3b5202010-03-23 06:52:41240 // Any children this item may have.
241 List children_;
242
[email protected]5aeeae12012-07-05 19:13:11243 DISALLOW_COPY_AND_ASSIGN(MenuItem);
[email protected]2e3b5202010-03-23 06:52:41244};
245
246// This class keeps track of menu items added by extensions.
[email protected]5aeeae12012-07-05 19:13:11247class MenuManager : public content::NotificationObserver,
[email protected]b673a5942013-11-14 11:14:19248 public base::SupportsWeakPtr<MenuManager>,
249 public BrowserContextKeyedService {
[email protected]2e3b5202010-03-23 06:52:41250 public:
[email protected]b673a5942013-11-14 11:14:19251 MenuManager(Profile* profile, StateStore* store_);
[email protected]5aeeae12012-07-05 19:13:11252 virtual ~MenuManager();
[email protected]2e3b5202010-03-23 06:52:41253
[email protected]b673a5942013-11-14 11:14:19254 // Convenience function to get the MenuManager for a Profile.
255 static MenuManager* Get(Profile* profile);
256
[email protected]2e3b5202010-03-23 06:52:41257 // Returns the ids of extensions which have menu items registered.
258 std::set<std::string> ExtensionIds();
259
260 // Returns a list of all the *top-level* menu items (added via AddContextItem)
261 // for the given extension id, *not* including child items (added via
262 // AddChildItem); although those can be reached via the top-level items'
[email protected]63a414b52010-06-03 23:20:49263 // children. A view can then decide how to display these, including whether to
264 // put them into a submenu if there are more than 1.
[email protected]5aeeae12012-07-05 19:13:11265 const MenuItem::List* MenuItems(const std::string& extension_id);
[email protected]2e3b5202010-03-23 06:52:41266
[email protected]052c92702010-06-25 07:25:52267 // Adds a top-level menu item for an extension, requiring the |extension|
268 // pointer so it can load the icon for the extension. Takes ownership of
[email protected]f4f04592010-07-14 20:40:13269 // |item|. Returns a boolean indicating success or failure.
[email protected]5aeeae12012-07-05 19:13:11270 bool AddContextItem(const Extension* extension, MenuItem* item);
[email protected]2e3b5202010-03-23 06:52:41271
272 // Add an item as a child of another item which has been previously added, and
[email protected]f4f04592010-07-14 20:40:13273 // takes ownership of |item|. Returns a boolean indicating success or failure.
[email protected]5aeeae12012-07-05 19:13:11274 bool AddChildItem(const MenuItem::Id& parent_id,
275 MenuItem* child);
[email protected]2e3b5202010-03-23 06:52:41276
[email protected]66dbfb2c2010-05-12 20:20:15277 // Makes existing item with |child_id| a child of the item with |parent_id|.
278 // If the child item was already a child of another parent, this will remove
279 // it from that parent first. It is an error to try and move an item to be a
[email protected]f4f04592010-07-14 20:40:13280 // child of one of its own descendants. It is legal to pass NULL for
281 // |parent_id|, which means the item should be moved to the top-level.
[email protected]5aeeae12012-07-05 19:13:11282 bool ChangeParent(const MenuItem::Id& child_id,
283 const MenuItem::Id* parent_id);
[email protected]66dbfb2c2010-05-12 20:20:15284
[email protected]2e3b5202010-03-23 06:52:41285 // Removes a context menu item with the given id (whether it is a top-level
286 // item or a child of some other item), returning true if the item was found
287 // and removed or false otherwise.
[email protected]5aeeae12012-07-05 19:13:11288 bool RemoveContextMenuItem(const MenuItem::Id& id);
[email protected]2e3b5202010-03-23 06:52:41289
[email protected]66dbfb2c2010-05-12 20:20:15290 // Removes all items for the given extension id.
[email protected]48329f92011-03-28 19:38:22291 void RemoveAllContextItems(const std::string& extension_id);
[email protected]66dbfb2c2010-05-12 20:20:15292
[email protected]2e3b5202010-03-23 06:52:41293 // Returns the item with the given |id| or NULL.
[email protected]5aeeae12012-07-05 19:13:11294 MenuItem* GetItemById(const MenuItem::Id& id) const;
[email protected]2e3b5202010-03-23 06:52:41295
[email protected]5aeeae12012-07-05 19:13:11296 // Notify the MenuManager that an item has been updated not through
297 // an explicit call into MenuManager. For example, if an item is
[email protected]7dddebc32012-01-11 22:01:03298 // acquired by a call to GetItemById and changed, then this should be called.
299 // Returns true if the item was found or false otherwise.
[email protected]5aeeae12012-07-05 19:13:11300 bool ItemUpdated(const MenuItem::Id& id);
[email protected]7dddebc32012-01-11 22:01:03301
[email protected]2e3b5202010-03-23 06:52:41302 // Called when a menu item is clicked on by the user.
[email protected]33a86dbd2012-06-20 03:20:18303 void ExecuteCommand(Profile* profile,
304 content::WebContents* web_contents,
[email protected]35be7ec2012-02-12 20:42:51305 const content::ContextMenuParams& params,
[email protected]5aeeae12012-07-05 19:13:11306 const MenuItem::Id& menu_item_id);
[email protected]2e3b5202010-03-23 06:52:41307
[email protected]f23a8c12011-03-15 14:43:43308 // This returns a bitmap of width/height kFaviconSize, loaded either from an
[email protected]052c92702010-06-25 07:25:52309 // entry specified in the extension's 'icon' section of the manifest, or a
310 // default extension icon.
311 const SkBitmap& GetIconForExtension(const std::string& extension_id);
312
[email protected]6c2381d2011-10-19 02:52:53313 // Implements the content::NotificationObserver interface.
314 virtual void Observe(int type, const content::NotificationSource& source,
315 const content::NotificationDetails& details) OVERRIDE;
[email protected]052c92702010-06-25 07:25:52316
[email protected]bec64552012-06-13 20:25:49317 // Stores the menu items for the extension in the state storage.
[email protected]5aeeae12012-07-05 19:13:11318 void WriteToStorage(const Extension* extension);
[email protected]a0011472012-05-30 17:15:40319
[email protected]bec64552012-06-13 20:25:49320 // Reads menu items for the extension from the state storage. Any invalid
[email protected]a0011472012-05-30 17:15:40321 // items are ignored.
[email protected]bec64552012-06-13 20:25:49322 void ReadFromStorage(const std::string& extension_id,
323 scoped_ptr<base::Value> value);
[email protected]a0011472012-05-30 17:15:40324
[email protected]63503462012-10-30 22:14:31325 // Removes all "incognito" "split" mode context items.
326 void RemoveAllIncognitoContextItems();
327
[email protected]2e3b5202010-03-23 06:52:41328 private:
[email protected]5aeeae12012-07-05 19:13:11329 FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, DeleteParent);
330 FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, RemoveOneByOne);
[email protected]2b07c93f2010-08-02 23:13:04331
[email protected]2e3b5202010-03-23 06:52:41332 // This is a helper function which takes care of de-selecting any other radio
333 // items in the same group (i.e. that are adjacent in the list).
[email protected]5aeeae12012-07-05 19:13:11334 void RadioItemSelected(MenuItem* item);
[email protected]2e3b5202010-03-23 06:52:41335
[email protected]7dddebc32012-01-11 22:01:03336 // Make sure that there is only one radio item selected at once in any run.
337 // If there are no radio items selected, then the first item in the run
338 // will get selected. If there are multiple radio items selected, then only
339 // the last one will get selcted.
[email protected]5aeeae12012-07-05 19:13:11340 void SanitizeRadioList(const MenuItem::List& item_list);
[email protected]7dddebc32012-01-11 22:01:03341
[email protected]66dbfb2c2010-05-12 20:20:15342 // Returns true if item is a descendant of an item with id |ancestor_id|.
[email protected]5aeeae12012-07-05 19:13:11343 bool DescendantOf(MenuItem* item, const MenuItem::Id& ancestor_id);
[email protected]66dbfb2c2010-05-12 20:20:15344
[email protected]2e3b5202010-03-23 06:52:41345 // We keep items organized by mapping an extension id to a list of items.
[email protected]5aeeae12012-07-05 19:13:11346 typedef std::map<std::string, MenuItem::List> MenuItemMap;
[email protected]2e3b5202010-03-23 06:52:41347 MenuItemMap context_items_;
348
[email protected]5aeeae12012-07-05 19:13:11349 // This lets us make lookup by id fast. It maps id to MenuItem* for
[email protected]2e3b5202010-03-23 06:52:41350 // all items the menu manager knows about, including all children of top-level
351 // items.
[email protected]5aeeae12012-07-05 19:13:11352 std::map<MenuItem::Id, MenuItem*> items_by_id_;
[email protected]2e3b5202010-03-23 06:52:41353
[email protected]6c2381d2011-10-19 02:52:53354 content::NotificationRegistrar registrar_;
[email protected]2e3b5202010-03-23 06:52:41355
[email protected]b671760b2010-07-15 21:13:47356 ExtensionIconManager icon_manager_;
[email protected]052c92702010-06-25 07:25:52357
[email protected]a0011472012-05-30 17:15:40358 Profile* profile_;
359
[email protected]b673a5942013-11-14 11:14:19360 // Owned by ExtensionSystem.
361 StateStore* store_;
362
[email protected]5aeeae12012-07-05 19:13:11363 DISALLOW_COPY_AND_ASSIGN(MenuManager);
[email protected]2e3b5202010-03-23 06:52:41364};
365
[email protected]5aeeae12012-07-05 19:13:11366} // namespace extensions
367
368#endif // CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_