Reland the ULONG -> SIZE_T change from 317177
[chromium-blink-merge.git] / extensions / renderer / resources / context_menus_custom_bindings.js
blob0e82711b5c41fb47b4c8a998e609d576330fe303
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')
24 return prop.id;
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) {
34 return;
36 contextMenus.listening = true;
37 contextMenus.event.addListener(function() {
38 // An extension context menu item has been clicked on - fire the onclick
39 // if there is one.
40 var id = arguments[0].menuItemId;
41 var onclick = contextMenus.handlersForId(id)[id];
42 if (onclick) {
43 $Function.apply(onclick, null, arguments);
45 });
48 apiFunctions.setHandleRequest('create', function() {
49 var args = arguments;
50 var id = contextMenuNatives.GetNextContextMenuId();
51 args[0].generatedId = id;
52 var optArgs = {
53 customCallback: this.customCallback,
55 sendRequest(this.name, args, this.definition.parameters, optArgs);
56 return contextMenus.getIdFromCreateProperties(args[0]);
57 });
59 apiFunctions.setCustomCallback('create',
60 function(name, request, callback, response) {
61 if (lastError.hasError(chrome)) {
62 if (callback)
63 callback();
64 return;
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;
71 if (onclick) {
72 contextMenus.ensureListenerSetup();
73 contextMenus.handlersForId(id)[id] = onclick;
75 if (callback)
76 callback();
77 });
79 apiFunctions.setCustomCallback('remove',
80 function(name, request, callback, response) {
81 if (lastError.hasError(chrome)) {
82 if (callback)
83 callback();
84 return;
86 var id = request.args[0];
87 delete contextMenus.handlersForId(id)[id];
88 if (callback)
89 callback();
90 });
92 apiFunctions.setCustomCallback('update',
93 function(name, request, callback, response) {
94 if (lastError.hasError(chrome)) {
95 if (callback)
96 callback();
97 return;
99 var id = request.args[0];
100 if (request.args[1].onclick) {
101 contextMenus.handlersForId(id)[id] = request.args[1].onclick;
103 if (callback)
104 callback();
107 apiFunctions.setCustomCallback('removeAll',
108 function(name, request, callback, response) {
109 if (lastError.hasError(chrome)) {
110 if (callback)
111 callback();
112 return;
114 contextMenus.generatedIdHandlers = {};
115 contextMenus.stringIdHandlers = {};
116 if (callback)
117 callback();
121 exports.binding = binding.generate();