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
;
13 binding
.registerCustomHook(function(bindingsAPI
) {
14 var apiFunctions
= bindingsAPI
.apiFunctions
;
16 var contextMenus
= {};
17 contextMenus
.generatedIdHandlers
= {};
18 contextMenus
.stringIdHandlers
= {};
19 var eventName
= 'contextMenus';
20 contextMenus
.event
= new Event(eventName
);
21 contextMenus
.getIdFromCreateProperties = function(prop
) {
22 if (typeof(prop
.id
) !== 'undefined')
24 return prop
.generatedId
;
26 contextMenus
.handlersForId = function(id
) {
27 if (typeof(id
) === 'number')
28 return contextMenus
.generatedIdHandlers
;
29 return contextMenus
.stringIdHandlers
;
31 contextMenus
.ensureListenerSetup = function() {
32 if (contextMenus
.listening
) {
35 contextMenus
.listening
= true;
36 contextMenus
.event
.addListener(function() {
37 // An extension context menu item has been clicked on - fire the onclick
39 var id
= arguments
[0].menuItemId
;
40 var onclick
= contextMenus
.handlersForId(id
)[id
];
42 $Function
.apply(onclick
, null, arguments
);
47 apiFunctions
.setHandleRequest('create', function() {
49 var id
= contextMenuNatives
.GetNextContextMenuId();
50 args
[0].generatedId
= id
;
52 customCallback
: this.customCallback
,
54 sendRequest(this.name
, args
, this.definition
.parameters
, optArgs
);
55 return contextMenus
.getIdFromCreateProperties(args
[0]);
58 apiFunctions
.setCustomCallback('create', function(name
, request
, response
) {
59 if (chrome
.runtime
.lastError
) {
63 var id
= contextMenus
.getIdFromCreateProperties(request
.args
[0]);
65 // Set up the onclick handler if we were passed one in the request.
66 var onclick
= request
.args
.length
? request
.args
[0].onclick
: null;
68 contextMenus
.ensureListenerSetup();
69 contextMenus
.handlersForId(id
)[id
] = onclick
;
73 apiFunctions
.setCustomCallback('remove', function(name
, request
, response
) {
74 if (chrome
.runtime
.lastError
) {
77 var id
= request
.args
[0];
78 delete contextMenus
.handlersForId(id
)[id
];
81 apiFunctions
.setCustomCallback('update', function(name
, request
, response
) {
82 if (chrome
.runtime
.lastError
) {
85 var id
= request
.args
[0];
86 if (request
.args
[1].onclick
) {
87 contextMenus
.handlersForId(id
)[id
] = request
.args
[1].onclick
;
91 apiFunctions
.setCustomCallback('removeAll',
92 function(name
, request
, response
) {
93 if (chrome
.runtime
.lastError
) {
96 contextMenus
.generatedIdHandlers
= {};
97 contextMenus
.stringIdHandlers
= {};
101 exports
.binding
= binding
.generate();