Index: extensions/renderer/resources/context_menus_handlers.js |
diff --git a/extensions/renderer/resources/context_menus_handlers.js b/extensions/renderer/resources/context_menus_handlers.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..863d378200dbbecc4998b56faf172225e3675fee |
--- /dev/null |
+++ b/extensions/renderer/resources/context_menus_handlers.js |
@@ -0,0 +1,135 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Implementation of custom bindings for the contextMenus API. |
lazyboy
2015/02/25 17:58:42
A little bit detail here would help.
Note that we
robwu
2015/02/26 00:15:11
Done.
|
+ |
+var contextMenuNatives = requireNative('context_menus'); |
+var sendRequest = require('sendRequest').sendRequest; |
+var Event = require('event_bindings').Event; |
+var lastError = require('lastError'); |
+ |
+// Add the bindings to the contextMenus API. |
+function createContextMenusHandlers(isWebview) { |
+ var eventName = isWebview ? 'webViewInternal.contextMenus' : 'contextMenus'; |
+ // Some dummy value for chrome.contextMenus instances. |
+ var INSTANCEID_NONWEBVIEW = -1; |
lazyboy
2015/02/25 17:58:42
Most places in the code base refers to this as kIn
robwu
2015/02/25 18:25:30
I've picked -1 because "0" could be a value associ
robwu
2015/02/26 00:15:11
I've added a comment to clarify my intention.
|
+ |
+ // Generates a customCallback for a given method. |handleCallback| will be |
+ // invoked with |request.args| as parameters. |
+ function createCustomCallback(handleCallback) { |
+ return function(name, request, callback) { |
+ if (lastError.hasError(chrome)) { |
+ if (callback) |
+ callback(); |
+ return; |
+ } |
+ var args = request.args; |
+ if (!isWebview) { |
+ // <webviews> have an extra item in front of the parameter list, which |
lazyboy
2015/02/25 22:52:48
nit: <webview>s ...
robwu
2015/02/26 00:15:11
Done.
|
+ // specifies the ID of the webview. This is used to hide context menu |
lazyboy
2015/02/25 22:52:47
s/ID/viewInstanceId
robwu
2015/02/26 00:15:11
Done.
|
+ // events in one webview from another. |
+ // The non-webview chrome.contextMenus API is not called with such an |
+ // ID, so we prepend an ID to match the function signature. |
+ args = $Array.concat([INSTANCEID_NONWEBVIEW], args); |
+ } |
+ $Function.apply(handleCallback, null, args); |
+ if (callback) |
+ callback(); |
+ }; |
+ } |
+ |
+ var contextMenus = {}; |
+ contextMenus.handlers = {}; |
+ contextMenus.event = new Event(eventName); |
+ |
+ contextMenus.getIdFromCreateProperties = function(createProperties) { |
+ if (typeof createProperties.id !== 'undefined') |
+ return createProperties.id; |
+ return createProperties.generatedId; |
+ }; |
+ |
+ contextMenus.handlersForId = function(instanceid, id) { |
lazyboy
2015/02/25 22:52:47
here and in all other places:
s/instanceid/instanc
robwu
2015/02/26 00:15:11
Done.
|
+ if (!contextMenus.handlers[instanceid]) { |
+ contextMenus.handlers[instanceid] = { |
+ generated: {}, |
+ string: {} |
+ }; |
+ } |
+ if (typeof id === 'number') |
+ return contextMenus.handlers[instanceid].generated; |
+ return contextMenus.handlers[instanceid].string; |
+ }; |
+ |
+ contextMenus.ensureListenerSetup = function() { |
+ if (contextMenus.listening) { |
+ return; |
+ } |
+ contextMenus.listening = true; |
+ contextMenus.event.addListener(function(info) { |
+ var instanceid = INSTANCEID_NONWEBVIEW; |
+ if (isWebview) { |
+ instanceid = info.webviewInstanceId; |
+ delete info.webviewInstanceId; |
lazyboy
2015/02/25 22:52:47
Add a note here saying we don't expose "webviewIns
robwu
2015/02/26 00:15:11
Done.
|
+ } |
+ |
+ var id = info.menuItemId; |
+ var onclick = contextMenus.handlersForId(instanceid, id)[id]; |
+ if (onclick) { |
+ $Function.apply(onclick, null, arguments); |
+ } |
+ }); |
+ }; |
+ |
+ // To be used with apiFunctions.setHandleRequest |
+ var requestHandlers = {}; |
+ // To be used with apiFunctions.setCustomCallback |
+ var callbacks = {}; |
+ |
+ requestHandlers.create = function() { |
+ var createProperties = isWebview ? arguments[1] : arguments[0]; |
+ createProperties.generatedId = contextMenuNatives.GetNextContextMenuId(); |
+ var optArgs = { |
+ customCallback: this.customCallback, |
+ }; |
+ sendRequest(this.name, arguments, this.definition.parameters, optArgs); |
+ return contextMenus.getIdFromCreateProperties(createProperties); |
+ }; |
+ |
+ callbacks.create = |
+ createCustomCallback(function(instanceid, createProperties) { |
+ var id = contextMenus.getIdFromCreateProperties(createProperties); |
+ var onclick = createProperties.onclick; |
+ if (onclick) { |
+ contextMenus.ensureListenerSetup(); |
+ contextMenus.handlersForId(instanceid, id)[id] = onclick; |
+ } |
+ }); |
+ |
+ callbacks.remove = createCustomCallback(function(instanceid, id) { |
+ delete contextMenus.handlersForId(instanceid, id)[id]; |
+ }); |
+ |
+ callbacks.update = |
+ createCustomCallback(function(instanceid, id, updateProperties) { |
+ var onclick = updateProperties.onclick; |
+ if (onclick) { |
+ contextMenus.ensureListenerSetup(); |
+ contextMenus.handlersForId(instanceid, id)[id] = onclick; |
+ } else if (onclick === null) { |
+ // When onclick is explicitly set to null, remove the event listener. |
lazyboy
2015/02/25 17:58:42
Great, can you file a bug for this and add it to t
robwu
2015/02/25 18:25:30
Done. https://crbug.com/461869
lazyboy
2015/02/25 22:52:47
Please write a test for this behavior :)
robwu
2015/02/26 00:15:11
Done.
|
+ delete contextMenus.handlersForId(instanceid, id)[id]; |
+ } |
+ }); |
+ |
+ callbacks.removeAll = createCustomCallback(function(instanceid) { |
+ delete contextMenus.handlers[instanceid]; |
+ }); |
+ |
+ return { |
lazyboy
2015/02/25 17:58:42
This is a bit brittle, the json schema compiler do
robwu
2015/02/25 18:25:30
This return value is not validated at all. This me
lazyboy
2015/02/25 22:52:47
Not significantly different, but there are differe
robwu
2015/02/26 00:15:11
If that's all, then merging would make maintenance
|
+ requestHandlers: requestHandlers, |
+ callbacks: callbacks |
+ }; |
+} |
+ |
+exports.create = createContextMenusHandlers; |