<webview>: Context menu API implementation CL.
Very Similar to chrome.contextMenus API, only applies to the <webview> that it is called upon.
var w = document.querySelector('webview');
w.contextMenus.OnClicked.addListener(function() { .. });
w.contextMenus.create({id: '1', title: 'one'});
w.contextMenus.update({id: '2', title: 'new', onclick: ...});
w.contextMenus.remove('1');
w.contextMenus.removeAll('1');
This CL adds the actual implementation to the previously exposed skeleton API.
Docs/snippets here:
https://docs.google.com/a/chromium.org/document/d/1AoMD6kF8ui1dikzTrwK-weVHegSqQaLV2zx4xJSh_fQ/edit
[email protected]
BUG=140315
Test=Can be tested inside a chrome apps <webview>.
<webview>.contextMenus.create(...) to create custom context menu items.
The API is similar to chrome.contextMenus API. Similarly .update,
.remove, .removeAll is also supported.
Click event handler can be specified as
<webview>.contextMenus.onClick.addListener(...).
Review URL: https://codereview.chromium.org/186213003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@255917 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/extensions/menu_manager.h b/chrome/browser/extensions/menu_manager.h
index ea6cecf9..f609dab 100644
--- a/chrome/browser/extensions/menu_manager.h
+++ b/chrome/browser/extensions/menu_manager.h
@@ -42,13 +42,32 @@
// A list of MenuItems.
typedef std::vector<MenuItem*> List;
+ // Key used to identify which extension a menu item belongs to.
+ // A menu item can also belong to a <webview> inside an extension,
+ // only in that case |webview_instance_id| would be
+ // non-zero (i.e. != guestview::kInstanceIDNone).
+ struct ExtensionKey {
+ std::string extension_id;
+ int webview_instance_id;
+
+ ExtensionKey();
+ ExtensionKey(const std::string& extension_id, int webview_instance_id);
+ explicit ExtensionKey(const std::string& extension_id);
+
+ bool operator==(const ExtensionKey& other) const;
+ bool operator!=(const ExtensionKey& other) const;
+ bool operator<(const ExtensionKey& other) const;
+
+ bool empty() const;
+ };
+
// An Id uniquely identifies a context menu item registered by an extension.
struct Id {
Id();
// Since the unique ID (uid or string_uid) is parsed from API arguments,
// the normal usage is to set the uid or string_uid immediately after
// construction.
- Id(bool incognito, const std::string& extension_id);
+ Id(bool incognito, const ExtensionKey& extension_key);
~Id();
bool operator==(const Id& other) const;
@@ -56,7 +75,7 @@
bool operator<(const Id& other) const;
bool incognito;
- std::string extension_id;
+ ExtensionKey extension_key;
// Only one of uid or string_uid will be defined.
int uid;
std::string string_uid;
@@ -138,7 +157,9 @@
// Simple accessor methods.
bool incognito() const { return id_.incognito; }
- const std::string& extension_id() const { return id_.extension_id; }
+ const std::string& extension_id() const {
+ return id_.extension_key.extension_id;
+ }
const std::string& title() const { return title_; }
const List& children() { return children_; }
const Id& id() const { return id_; }
@@ -254,15 +275,15 @@
// Convenience function to get the MenuManager for a Profile.
static MenuManager* Get(Profile* profile);
- // Returns the ids of extensions which have menu items registered.
- std::set<std::string> ExtensionIds();
+ // Returns the keys of extensions which have menu items registered.
+ std::set<MenuItem::ExtensionKey> ExtensionIds();
// Returns a list of all the *top-level* menu items (added via AddContextItem)
- // for the given extension id, *not* including child items (added via
- // AddChildItem); although those can be reached via the top-level items'
- // children. A view can then decide how to display these, including whether to
- // put them into a submenu if there are more than 1.
- const MenuItem::List* MenuItems(const std::string& extension_id);
+ // for the given extension specified by |extension_key|, *not* including child
+ // items (added via AddChildItem); although those can be reached via the
+ // top-level items' children. A view can then decide how to display these,
+ // including whether to put them into a submenu if there are more than 1.
+ const MenuItem::List* MenuItems(const MenuItem::ExtensionKey& extension_key);
// Adds a top-level menu item for an extension, requiring the |extension|
// pointer so it can load the icon for the extension. Takes ownership of
@@ -287,8 +308,8 @@
// and removed or false otherwise.
bool RemoveContextMenuItem(const MenuItem::Id& id);
- // Removes all items for the given extension id.
- void RemoveAllContextItems(const std::string& extension_id);
+ // Removes all items for the given extension specified by |extension_key|.
+ void RemoveAllContextItems(const MenuItem::ExtensionKey& extension_key);
// Returns the item with the given |id| or NULL.
MenuItem* GetItemById(const MenuItem::Id& id) const;
@@ -315,7 +336,8 @@
const content::NotificationDetails& details) OVERRIDE;
// Stores the menu items for the extension in the state storage.
- void WriteToStorage(const Extension* extension);
+ void WriteToStorage(const Extension* extension,
+ const MenuItem::ExtensionKey& extension_key);
// Reads menu items for the extension from the state storage. Any invalid
// items are ignored.
@@ -342,8 +364,8 @@
// Returns true if item is a descendant of an item with id |ancestor_id|.
bool DescendantOf(MenuItem* item, const MenuItem::Id& ancestor_id);
- // We keep items organized by mapping an extension id to a list of items.
- typedef std::map<std::string, MenuItem::List> MenuItemMap;
+ // We keep items organized by mapping ExtensionKey to a list of items.
+ typedef std::map<MenuItem::ExtensionKey, MenuItem::List> MenuItemMap;
MenuItemMap context_items_;
// This lets us make lookup by id fast. It maps id to MenuItem* for