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 the contextMenus API.
7 var binding
= require('binding').Binding
.create('contextMenus');
9 var contextMenuNatives
= requireNative('context_menus');
10 var sendRequest
= require('sendRequest').sendRequest
;
11 var Event
= require('event_bindings').Event
;
12 var lastError
= require('lastError');
14 binding
.registerCustomHook(function(bindingsAPI
) {
15 var apiFunctions
= bindingsAPI
.apiFunctions
;
17 var contextMenus
= {};
18 contextMenus
.generatedIdHandlers
= {};
19 contextMenus
.stringIdHandlers
= {};
20 var eventName
= 'contextMenus';
21 contextMenus
.event
= new Event(eventName
);
22 contextMenus
.getIdFromCreateProperties = function(prop
) {
23 if (typeof(prop
.id
) !== 'undefined')
25 return prop
.generatedId
;
27 contextMenus
.handlersForId = function(id
) {
28 if (typeof(id
) === 'number')
29 return contextMenus
.generatedIdHandlers
;
30 return contextMenus
.stringIdHandlers
;
32 contextMenus
.ensureListenerSetup = function() {
33 if (contextMenus
.listening
) {
36 contextMenus
.listening
= true;
37 contextMenus
.event
.addListener(function() {
38 // An extension context menu item has been clicked on - fire the onclick
40 var id
= arguments
[0].menuItemId
;
41 var onclick
= contextMenus
.handlersForId(id
)[id
];
43 $Function
.apply(onclick
, null, arguments
);
48 apiFunctions
.setHandleRequest('create', function() {
50 var id
= contextMenuNatives
.GetNextContextMenuId();
51 args
[0].generatedId
= id
;
53 customCallback
: this.customCallback
,
55 sendRequest(this.name
, args
, this.definition
.parameters
, optArgs
);
56 return contextMenus
.getIdFromCreateProperties(args
[0]);
59 apiFunctions
.setCustomCallback('create',
60 function(name
, request
, callback
, response
) {
61 if (lastError
.hasError(chrome
)) {
67 var id
= contextMenus
.getIdFromCreateProperties(request
.args
[0]);
69 // Set up the onclick handler if we were passed one in the request.
70 var onclick
= request
.args
.length
? request
.args
[0].onclick
: null;
72 contextMenus
.ensureListenerSetup();
73 contextMenus
.handlersForId(id
)[id
] = onclick
;
79 apiFunctions
.setCustomCallback('remove',
80 function(name
, request
, callback
, response
) {
81 if (lastError
.hasError(chrome
)) {
86 var id
= request
.args
[0];
87 delete contextMenus
.handlersForId(id
)[id
];
92 apiFunctions
.setCustomCallback('update',
93 function(name
, request
, callback
, response
) {
94 if (lastError
.hasError(chrome
)) {
99 var id
= request
.args
[0];
100 if (request
.args
[1].onclick
) {
101 contextMenus
.handlersForId(id
)[id
] = request
.args
[1].onclick
;
107 apiFunctions
.setCustomCallback('removeAll',
108 function(name
, request
, callback
, response
) {
109 if (lastError
.hasError(chrome
)) {
114 contextMenus
.generatedIdHandlers
= {};
115 contextMenus
.stringIdHandlers
= {};
121 exports
.binding
= binding
.generate();