Chromium Code Reviews| Index: chrome/renderer/resources/extensions/chrome_web_view_internal_custom_bindings.js |
| diff --git a/chrome/renderer/resources/extensions/chrome_web_view_internal_custom_bindings.js b/chrome/renderer/resources/extensions/chrome_web_view_internal_custom_bindings.js |
| index 9346c1177dbe88a32ad5c64036a2a86e348d9708..3694d796bd6e909b077b4d5ec7e7add5043efc12 100644 |
| --- a/chrome/renderer/resources/extensions/chrome_web_view_internal_custom_bindings.js |
| +++ b/chrome/renderer/resources/extensions/chrome_web_view_internal_custom_bindings.js |
| @@ -2,145 +2,25 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -// Custom binding for <webview> contextMenus API. |
| -// Note that this file mimics custom bindings for chrome.contextMenus API |
| -// which resides in context_menus_custom_bindings.js. The functions in this file |
| -// have an extra instanceId parameter in the beginning, which corresponds to the |
| -// id of the <webview>. |
| -// |
| -// TODO(lazyboy): Share common code /w context_menus_custom_bindings.js. |
| - |
| -var EventBindings = require('event_bindings'); |
| var binding = require('binding').Binding.create('chromeWebViewInternal'); |
| -var contextMenuNatives = requireNative('context_menus'); |
| -var sendRequest = require('sendRequest').sendRequest; |
| -var lastError = require('lastError'); |
| +var createContextMenusHandlers = require('contextMenusHandlers').create; |
|
not at google - send to devlin
2015/02/25 19:03:53
I'd rather imports import the module, not a method
robwu
2015/02/26 09:33:46
Done.
|
| binding.registerCustomHook(function(bindingsAPI) { |
| var apiFunctions = bindingsAPI.apiFunctions; |
| - var webviewContextMenus = {}; |
| - webviewContextMenus.generatedIdHandlers = {}; |
| - webviewContextMenus.stringIdHandlers = {}; |
| - |
| - // Per item event handler. |
| - var ename = 'webViewInternal.contextMenus'; |
| - webviewContextMenus.event = new EventBindings.Event(ename); |
| - |
| - webviewContextMenus.getIdFromCreateProperties = function(prop) { |
| - if (typeof(prop.id) !== 'undefined') |
| - return prop.id; |
| - return prop.generatedId; |
| - }; |
| - |
| - webviewContextMenus.handlersForId = function(instanceId, id) { |
| - if (typeof(id) === 'number') { |
| - if (!webviewContextMenus.generatedIdHandlers[instanceId]) { |
| - webviewContextMenus.generatedIdHandlers[instanceId] = {}; |
| - } |
| - return webviewContextMenus.generatedIdHandlers[instanceId]; |
| - } |
| - |
| - if (!webviewContextMenus.stringIdHandlers[instanceId]) { |
| - webviewContextMenus.stringIdHandlers[instanceId] = {}; |
| - } |
| - return webviewContextMenus.stringIdHandlers[instanceId]; |
| - }; |
| - |
| - webviewContextMenus.ensureListenerSetup = function() { |
| - if (webviewContextMenus.listening) { |
| - return; |
| - } |
| - webviewContextMenus.listening = true; |
| - webviewContextMenus.event.addListener(function() { |
| - // An extension context menu item has been clicked on - fire the onclick |
| - // if there is one. |
| - var id = arguments[0].menuItemId; |
| - var instanceId = arguments[0].webviewInstanceId; |
| - delete arguments[0].webviewInstanceId; |
| - var onclick = webviewContextMenus.handlersForId(instanceId, id)[id]; |
| - if (onclick) { |
| - $Function.apply(onclick, null, arguments); |
| - } |
| - }); |
| - }; |
| - |
| - apiFunctions.setHandleRequest('contextMenusCreate', function() { |
| - var args = arguments; |
| - var id = contextMenuNatives.GetNextContextMenuId(); |
| - args[1].generatedId = id; |
| - |
| - var optArgs = { |
| - customCallback: this.customCallback, |
| - }; |
| - |
| - sendRequest(this.name, args, this.definition.parameters, optArgs); |
| - return webviewContextMenus.getIdFromCreateProperties(args[1]); |
| - }); |
| + var impl = createContextMenusHandlers(/* isWebview = */ true); |
|
lazyboy
2015/02/25 17:58:42
nit: the most followed pattern here is:
createCont
robwu
2015/02/26 09:33:46
Done.
|
| - apiFunctions.setCustomCallback('contextMenusCreate', |
| - function(name, request, callback, response) { |
| - if (lastError.hasError(chrome)) { |
| - if (callback) |
| - callback(); |
| - return; |
| - } |
| + apiFunctions.setHandleRequest('contextMenusCreate', |
| + impl.requestHandlers.create); |
|
not at google - send to devlin
2015/02/25 19:03:53
Can we call this "handlers" rather than "impl"?
robwu
2015/02/26 09:33:46
Done.
|
| - var instanceId = request.args[0]; |
| - var id = webviewContextMenus.getIdFromCreateProperties(request.args[1]); |
| - var onclick = request.args.length ? request.args[1].onclick : null; |
| - if (onclick) { |
| - webviewContextMenus.ensureListenerSetup(); |
| - webviewContextMenus.handlersForId(instanceId, id)[id] = onclick; |
| - } |
| - if (callback) |
| - callback(); |
| - }); |
| + apiFunctions.setCustomCallback('contextMenusCreate', impl.callbacks.create); |
| - apiFunctions.setCustomCallback('contextMenusUpdate', |
| - function(name, request, callback, response) { |
| - if (lastError.hasError(chrome)) { |
| - if (callback) |
| - callback(); |
| - return; |
| - } |
| - var instanceId = request.args[0]; |
| - var id = request.args[1]; |
| - if (request.args[2].onclick) { |
| - webviewContextMenus.handlersForId(instanceId, id)[id] = |
| - request.args[2].onclick; |
| - } |
| - if (callback) |
| - callback(); |
| - }); |
| + apiFunctions.setCustomCallback('contextMenusUpdate', impl.callbacks.update); |
| - apiFunctions.setCustomCallback('contextMenusRemove', |
| - function(name, request, callback, response) { |
| - if (lastError.hasError(chrome)) { |
| - if (callback) |
| - callback(); |
| - return; |
| - } |
| - var instanceId = request.args[0]; |
| - var id = request.args[1]; |
| - delete webviewContextMenus.handlersForId(instanceId, id)[id]; |
| - if (callback) |
| - callback(); |
| - }); |
| + apiFunctions.setCustomCallback('contextMenusRemove', impl.callbacks.remove); |
| apiFunctions.setCustomCallback('contextMenusRemoveAll', |
| - function(name, request, callback, response) { |
| - if (lastError.hasError(chrome)) { |
| - if (callback) |
| - callback(); |
| - return; |
| - } |
| - var instanceId = request.args[0]; |
| - webviewContextMenus.stringIdHandlers[instanceId] = {}; |
| - webviewContextMenus.generatedIdHandlers[instanceId] = {}; |
| - if (callback) |
| - callback(); |
| - }); |
| + impl.callbacks.removeAll); |
| }); |