Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1182)

Unified Diff: chrome/browser/extensions/menu_manager.cc

Issue 186213003: <webview>: Context menu API implementation CL. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/menu_manager.cc
diff --git a/chrome/browser/extensions/menu_manager.cc b/chrome/browser/extensions/menu_manager.cc
index 424366390b34035298c5790bd32d417cd7ed67a8..bb8ead316c5e1eedc8f1506e751d01e566f7e3d9 100644
--- a/chrome/browser/extensions/menu_manager.cc
+++ b/chrome/browser/extensions/menu_manager.cc
@@ -19,6 +19,7 @@
#include "chrome/browser/extensions/menu_manager_factory.h"
#include "chrome/browser/extensions/state_store.h"
#include "chrome/browser/extensions/tab_helper.h"
+#include "chrome/browser/guestview/guestview.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/context_menus.h"
#include "content/public/browser/notification_details.h"
@@ -509,6 +510,49 @@ bool MenuManager::RemoveContextMenuItem(const MenuItem::Id& id) {
return result;
}
+void MenuManager::RemoveAllWebviewContextItems(const std::string& extension_id,
+ int webview_instance_id) {
+ MenuItem::List::iterator i;
+ for (i = context_items_[extension_id].begin();
Fady Samuel 2014/03/04 01:56:46 I think it would be much cleaner if context items
lazyboy 2014/03/04 16:11:31 Hmmm, for most cases, webview_instance_id is 0, ad
Fady Samuel 2014/03/04 16:36:00 There is no additional indirection being added in
lazyboy 2014/03/04 20:50:24 Included instance id to the key, called MenuItem::
+ i != context_items_[extension_id].end();
+ ++i) {
+ MenuItem* item = *i;
+ if (item->id().webview_instance_id != webview_instance_id)
+ continue;
+ items_by_id_.erase(item->id());
+
+ // Remove descendants from this item and erase them from the lookup cache.
+ std::set<MenuItem::Id> removed_ids = item->RemoveAllDescendants();
+ std::set<MenuItem::Id>::const_iterator j;
+ for (j = removed_ids.begin(); j != removed_ids.end(); ++j)
+ items_by_id_.erase(*j);
+ }
+
+ // TODO(lazyboy): Consider better data structure. We are naivly iterating
+ // through each items in the extension and remvoving the ones that belong
+ // to the webview.
+ MenuItem::List& items = context_items_[extension_id];
+ i = items.begin();
+ MenuItem::List::iterator check_iter = items.begin();
+ MenuItem::List::iterator insert_iter = items.begin();
+
+ while (check_iter != items.end()) {
+ int item_webview_instance_id = (*check_iter)->id().webview_instance_id;
+ if (item_webview_instance_id != webview_instance_id) {
+ *insert_iter = *check_iter;
+ ++insert_iter;
+ } else {
+ delete *check_iter;
+ }
+ ++check_iter;
+ }
+
+ // Shrink.
+ items.resize(insert_iter - items.begin());
+ if (!items.size())
+ context_items_.erase(extension_id);
+}
+
void MenuManager::RemoveAllContextItems(const std::string& extension_id) {
MenuItem::List::iterator i;
for (i = context_items_[extension_id].begin();
@@ -646,6 +690,10 @@ void MenuManager::ExecuteCommand(Profile* profile,
properties->SetBoolean("editable", params.is_editable);
+ GuestView* guest_view = GuestView::FromWebContents(web_contents);
+ if (guest_view)
+ properties->SetInteger("webviewInstanceId", guest_view->view_instance_id());
Fady Samuel 2014/03/04 01:56:46 What is this?
lazyboy 2014/03/04 16:11:31 This is a property on the onClick event.
Fady Samuel 2014/03/04 16:36:00 Is this visible to the developer? What's it used f
lazyboy 2014/03/04 20:50:24 Yes. This is to indicate which webview the event c
lazyboy 2014/03/04 21:13:49 As discussed offline, I've removed exposing webvie
+
args->Append(properties);
// Add the tab info to the argument list.
@@ -683,6 +731,7 @@ void MenuManager::ExecuteCommand(Profile* profile,
}
{
+ // Dispatch to menu item's .onclick handler.
scoped_ptr<Event> event(new Event(
event_names::kOnContextMenus,
scoped_ptr<base::ListValue>(args->DeepCopy())));
@@ -691,10 +740,15 @@ void MenuManager::ExecuteCommand(Profile* profile,
event_router->DispatchEventToExtension(item->extension_id(), event.Pass());
}
{
- scoped_ptr<Event> event(new Event(context_menus::OnClicked::kEventName,
- args.Pass()));
+ // Dispatch to .contextMenus.onClicked handler.
+ scoped_ptr<Event> event(
+ new Event(guest_view ? event_names::kOnWebviewContextMenus
+ : context_menus::OnClicked::kEventName,
+ args.Pass()));
event->restrict_to_browser_context = profile;
event->user_gesture = EventRouter::USER_GESTURE_ENABLED;
+ if (guest_view)
+ event->filter_info.SetInstanceID(guest_view->view_instance_id());
event_router->DispatchEventToExtension(item->extension_id(), event.Pass());
}
}
@@ -763,13 +817,16 @@ void MenuManager::WriteToStorage(const Extension* extension) {
if (top_items) {
for (MenuItem::List::const_iterator i = top_items->begin();
i != top_items->end(); ++i) {
+ if ((*i)->id().webview_instance_id)
+ continue;
(*i)->GetFlattenedSubtree(&all_items);
}
}
- if (store_)
+ if (store_) {
store_->SetExtensionValue(extension->id(), kContextMenusKey,
MenuItemsToValue(all_items));
+ }
}
void MenuManager::ReadFromStorage(const std::string& extension_id,
@@ -859,19 +916,21 @@ void MenuManager::RemoveAllIncognitoContextItems() {
RemoveContextMenuItem(*remove_iter);
}
-MenuItem::Id::Id() : incognito(false), uid(0) {}
+MenuItem::Id::Id() : incognito(false), uid(0), webview_instance_id(0) {}
MenuItem::Id::Id(bool incognito, const std::string& extension_id)
- : incognito(incognito), extension_id(extension_id), uid(0) {}
+ : incognito(incognito),
+ extension_id(extension_id),
+ uid(0),
+ webview_instance_id(0) {}
MenuItem::Id::~Id() {
}
bool MenuItem::Id::operator==(const Id& other) const {
- return (incognito == other.incognito &&
- extension_id == other.extension_id &&
- uid == other.uid &&
- string_uid == other.string_uid);
+ return (incognito == other.incognito && extension_id == other.extension_id &&
+ uid == other.uid && string_uid == other.string_uid &&
+ webview_instance_id == other.webview_instance_id);
}
bool MenuItem::Id::operator!=(const Id& other) const {
@@ -885,10 +944,15 @@ bool MenuItem::Id::operator<(const Id& other) const {
if (extension_id < other.extension_id)
return true;
if (extension_id == other.extension_id) {
- if (uid < other.uid)
+ if (webview_instance_id < other.webview_instance_id)
return true;
- if (uid == other.uid)
- return string_uid < other.string_uid;
+
+ if (webview_instance_id == other.webview_instance_id) {
+ if (uid < other.uid)
+ return true;
+ if (uid == other.uid)
+ return string_uid < other.string_uid;
+ }
}
}
return false;

Powered by Google App Engine
This is Rietveld 408576698