| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Custom binding for the contextMenus API. | 5 // Custom binding for the contextMenus API. |
| 6 | 6 |
| 7 var binding = require('binding').Binding.create('contextMenus'); | 7 var binding = require('binding').Binding.create('contextMenus'); |
| 8 | 8 var createContextMenusHandlers = require('contextMenusHandlers').create; |
| 9 var contextMenuNatives = requireNative('context_menus'); | |
| 10 var sendRequest = require('sendRequest').sendRequest; | |
| 11 var Event = require('event_bindings').Event; | |
| 12 var lastError = require('lastError'); | |
| 13 | 9 |
| 14 binding.registerCustomHook(function(bindingsAPI) { | 10 binding.registerCustomHook(function(bindingsAPI) { |
| 15 var apiFunctions = bindingsAPI.apiFunctions; | 11 var apiFunctions = bindingsAPI.apiFunctions; |
| 16 | 12 |
| 17 var contextMenus = {}; | 13 var impl = createContextMenusHandlers(/* isWebview = */ false); |
| 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; | |
| 26 }; | |
| 27 contextMenus.handlersForId = function(id) { | |
| 28 if (typeof(id) === 'number') | |
| 29 return contextMenus.generatedIdHandlers; | |
| 30 return contextMenus.stringIdHandlers; | |
| 31 }; | |
| 32 contextMenus.ensureListenerSetup = function() { | |
| 33 if (contextMenus.listening) { | |
| 34 return; | |
| 35 } | |
| 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); | |
| 44 } | |
| 45 }); | |
| 46 }; | |
| 47 | 14 |
| 48 apiFunctions.setHandleRequest('create', function() { | 15 apiFunctions.setHandleRequest('create', impl.requestHandlers.create); |
| 49 var args = arguments; | |
| 50 var id = contextMenuNatives.GetNextContextMenuId(); | |
| 51 args[0].generatedId = id; | |
| 52 var optArgs = { | |
| 53 customCallback: this.customCallback, | |
| 54 }; | |
| 55 sendRequest(this.name, args, this.definition.parameters, optArgs); | |
| 56 return contextMenus.getIdFromCreateProperties(args[0]); | |
| 57 }); | |
| 58 | 16 |
| 59 apiFunctions.setCustomCallback('create', | 17 apiFunctions.setCustomCallback('create', impl.callbacks.create); |
| 60 function(name, request, callback, response) { | |
| 61 if (lastError.hasError(chrome)) { | |
| 62 if (callback) | |
| 63 callback(); | |
| 64 return; | |
| 65 } | |
| 66 | 18 |
| 67 var id = contextMenus.getIdFromCreateProperties(request.args[0]); | 19 apiFunctions.setCustomCallback('remove', impl.callbacks.remove); |
| 68 | 20 |
| 69 // Set up the onclick handler if we were passed one in the request. | 21 apiFunctions.setCustomCallback('update', impl.callbacks.update); |
| 70 var onclick = request.args.length ? request.args[0].onclick : null; | |
| 71 if (onclick) { | |
| 72 contextMenus.ensureListenerSetup(); | |
| 73 contextMenus.handlersForId(id)[id] = onclick; | |
| 74 } | |
| 75 if (callback) | |
| 76 callback(); | |
| 77 }); | |
| 78 | 22 |
| 79 apiFunctions.setCustomCallback('remove', | 23 apiFunctions.setCustomCallback('removeAll', impl.callbacks.removeAll); |
| 80 function(name, request, callback, response) { | |
| 81 if (lastError.hasError(chrome)) { | |
| 82 if (callback) | |
| 83 callback(); | |
| 84 return; | |
| 85 } | |
| 86 var id = request.args[0]; | |
| 87 delete contextMenus.handlersForId(id)[id]; | |
| 88 if (callback) | |
| 89 callback(); | |
| 90 }); | |
| 91 | |
| 92 apiFunctions.setCustomCallback('update', | |
| 93 function(name, request, callback, response) { | |
| 94 if (lastError.hasError(chrome)) { | |
| 95 if (callback) | |
| 96 callback(); | |
| 97 return; | |
| 98 } | |
| 99 var id = request.args[0]; | |
| 100 if (request.args[1].onclick) { | |
| 101 contextMenus.handlersForId(id)[id] = request.args[1].onclick; | |
| 102 } | |
| 103 if (callback) | |
| 104 callback(); | |
| 105 }); | |
| 106 | |
| 107 apiFunctions.setCustomCallback('removeAll', | |
| 108 function(name, request, callback, response) { | |
| 109 if (lastError.hasError(chrome)) { | |
| 110 if (callback) | |
| 111 callback(); | |
| 112 return; | |
| 113 } | |
| 114 contextMenus.generatedIdHandlers = {}; | |
| 115 contextMenus.stringIdHandlers = {}; | |
| 116 if (callback) | |
| 117 callback(); | |
| 118 }); | |
| 119 }); | 24 }); |
| 120 | 25 |
| 121 exports.binding = binding.generate(); | 26 exports.binding = binding.generate(); |
| OLD | NEW |