1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Custom binding for <webview> contextMenus API.
6 // Note that this file mimics custom bindings for chrome.contextMenus API
7 // which resides in context_menus_custom_bindings.js. The functions in this file
8 // have an extra instanceId parameter in the beginning, which corresponds to the
9 // id of the <webview>.
11 // TODO(lazyboy): Share common code /w context_menus_custom_bindings.js.
13 var EventBindings = require('event_bindings');
14 var binding = require('binding').Binding.create('chromeWebViewInternal');
15 var contextMenuNatives = requireNative('context_menus');
16 var sendRequest = require('sendRequest').sendRequest;
18 binding.registerCustomHook(function(bindingsAPI) {
19 var apiFunctions = bindingsAPI.apiFunctions;
21 var webviewContextMenus = {};
22 webviewContextMenus.generatedIdHandlers = {};
23 webviewContextMenus.stringIdHandlers = {};
25 // Per item event handler.
26 var ename = 'webViewInternal.contextMenus';
27 webviewContextMenus.event = new EventBindings.Event(ename);
29 webviewContextMenus.getIdFromCreateProperties = function(prop) {
30 if (typeof(prop.id) !== 'undefined')
32 return prop.generatedId;
35 webviewContextMenus.handlersForId = function(instanceId, id) {
36 if (typeof(id) === 'number') {
37 if (!webviewContextMenus.generatedIdHandlers[instanceId]) {
38 webviewContextMenus.generatedIdHandlers[instanceId] = {};
40 return webviewContextMenus.generatedIdHandlers[instanceId];
43 if (!webviewContextMenus.stringIdHandlers[instanceId]) {
44 webviewContextMenus.stringIdHandlers[instanceId] = {};
46 return webviewContextMenus.stringIdHandlers[instanceId];
49 webviewContextMenus.ensureListenerSetup = function() {
50 if (webviewContextMenus.listening) {
53 webviewContextMenus.listening = true;
54 webviewContextMenus.event.addListener(function() {
55 // An extension context menu item has been clicked on - fire the onclick
57 var id = arguments[0].menuItemId;
58 var instanceId = arguments[0].webviewInstanceId;
59 delete arguments[0].webviewInstanceId;
60 var onclick = webviewContextMenus.handlersForId(instanceId, id)[id];
62 $Function.apply(onclick, null, arguments);
67 apiFunctions.setHandleRequest('contextMenusCreate', function() {
69 var id = contextMenuNatives.GetNextContextMenuId();
70 args[1].generatedId = id;
73 customCallback: this.customCallback,
76 sendRequest(this.name, args, this.definition.parameters, optArgs);
77 return webviewContextMenus.getIdFromCreateProperties(args[1]);
80 apiFunctions.setCustomCallback('contextMenusCreate',
81 function(name, request, response) {
82 if (chrome.runtime.lastError) {
86 var instanceId = request.args[0];
87 var id = webviewContextMenus.getIdFromCreateProperties(request.args[1]);
88 var onclick = request.args.length ? request.args[1].onclick : null;
90 webviewContextMenus.ensureListenerSetup();
91 webviewContextMenus.handlersForId(instanceId, id)[id] = onclick;
95 apiFunctions.setCustomCallback('contextMenusUpdate',
96 function(name, request, response) {
97 if (chrome.runtime.lastError) {
100 var instanceId = request.args[0];
101 var id = request.args[1];
102 if (request.args[2].onclick) {
103 webviewContextMenus.handlersForId(instanceId, id)[id] =
104 request.args[2].onclick;
108 apiFunctions.setCustomCallback('contextMenusRemove',
109 function(name, request, response) {
110 if (chrome.runtime.lastError) {
113 var instanceId = request.args[0];
114 var id = request.args[1];
115 delete webviewContextMenus.handlersForId(instanceId, id)[id];
118 apiFunctions.setCustomCallback('contextMenusRemoveAll',
119 function(name, request, response) {
120 if (chrome.runtime.lastError) {
123 var instanceId = request.args[0];
124 webviewContextMenus.stringIdHandlers[instanceId] = {};
125 webviewContextMenus.generatedIdHandlers[instanceId] = {};
130 exports.ChromeWebView = binding.generate();