OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 // Implementation of custom bindings for the contextMenus API. | 5 // Implementation of custom bindings for the contextMenus API. |
6 // This is used to implement the contextMenus API for extensions and for the | 6 // This is used to implement the contextMenus API for extensions and for the |
7 // <webview> tag (see chrome_web_view_experimental.js). | 7 // <webview> tag (see chrome_web_view_experimental.js). |
8 | 8 |
9 var contextMenuNatives = requireNative('context_menus'); | 9 var contextMenuNatives = requireNative('context_menus'); |
10 var sendRequest = require('sendRequest').sendRequest; | 10 var sendRequest = bindingUtil ? |
11 var Event = require('event_bindings').Event; | 11 $Function.bind(bindingUtil.sendRequest, bindingUtil) : |
12 var lastError = require('lastError'); | 12 require('sendRequest').sendRequest; |
| 13 var hasLastError = bindingUtil ? |
| 14 $Function.bind(bindingUtil.hasLastError, bindingUtil) : |
| 15 require('lastError').hasError; |
| 16 |
| 17 var jsEvent; |
| 18 function createNewEvent(name) { |
| 19 if (bindingUtil) { |
| 20 // Native custom events ignore schema. |
| 21 return bindingUtil.createCustomEvent(name, undefined, undefined); |
| 22 } |
| 23 if (!jsEvent) |
| 24 jsEvent = require('event_bindings').Event; |
| 25 return new jsEvent(name); |
| 26 } |
13 | 27 |
14 // Add the bindings to the contextMenus API. | 28 // Add the bindings to the contextMenus API. |
15 function createContextMenusHandlers(isWebview) { | 29 function createContextMenusHandlers(isWebview) { |
16 var eventName = isWebview ? 'webViewInternal.contextMenus' : 'contextMenus'; | 30 var eventName = isWebview ? 'webViewInternal.contextMenus' : 'contextMenus'; |
17 // Some dummy value for chrome.contextMenus instances. | 31 // Some dummy value for chrome.contextMenus instances. |
18 // Webviews use positive integers, and 0 to denote an invalid webview ID. | 32 // Webviews use positive integers, and 0 to denote an invalid webview ID. |
19 // The following constant is -1 to avoid any conflicts between webview IDs and | 33 // The following constant is -1 to avoid any conflicts between webview IDs and |
20 // extensions. | 34 // extensions. |
21 var INSTANCEID_NON_WEBVIEW = -1; | 35 var INSTANCEID_NON_WEBVIEW = -1; |
22 | 36 |
23 // Generates a customCallback for a given method. |handleCallback| will be | 37 // Generates a customCallback for a given method. |handleCallback| will be |
24 // invoked with the same arguments this function is called with. | 38 // invoked with the same arguments this function is called with. |
25 function getCallback(handleCallback) { | 39 function getCallback(handleCallback) { |
26 return function() { | 40 return function() { |
27 var extensionCallback = arguments[arguments.length - 1]; | 41 var extensionCallback = arguments[arguments.length - 1]; |
28 if (lastError.hasError(chrome)) { | 42 if (hasLastError(bindingUtil ? undefined : chrome)) { |
29 if (extensionCallback) | 43 if (extensionCallback) |
30 extensionCallback(); | 44 extensionCallback(); |
31 return; | 45 return; |
32 } | 46 } |
33 | 47 |
34 $Function.apply(handleCallback, null, arguments); | 48 $Function.apply(handleCallback, null, arguments); |
35 if (extensionCallback) | 49 if (extensionCallback) |
36 extensionCallback(); | 50 extensionCallback(); |
37 }; | 51 }; |
38 } | 52 } |
39 | 53 |
40 var contextMenus = { __proto__: null }; | 54 var contextMenus = { __proto__: null }; |
41 contextMenus.handlers = { __proto__: null }; | 55 contextMenus.handlers = { __proto__: null }; |
42 contextMenus.event = new Event(eventName); | 56 contextMenus.event = createNewEvent(eventName); |
43 | 57 |
44 contextMenus.getIdFromCreateProperties = function(createProperties) { | 58 contextMenus.getIdFromCreateProperties = function(createProperties) { |
45 if (typeof createProperties.id !== 'undefined') | 59 if (typeof createProperties.id !== 'undefined') |
46 return createProperties.id; | 60 return createProperties.id; |
47 return createProperties.generatedId; | 61 return createProperties.generatedId; |
48 }; | 62 }; |
49 | 63 |
50 contextMenus.handlersForId = function(instanceId, id) { | 64 contextMenus.handlersForId = function(instanceId, id) { |
51 if (!contextMenus.handlers[instanceId]) { | 65 if (!contextMenus.handlers[instanceId]) { |
52 contextMenus.handlers[instanceId] = { | 66 contextMenus.handlers[instanceId] = { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 createProperties.generatedId = contextMenuNatives.GetNextContextMenuId(); | 109 createProperties.generatedId = contextMenuNatives.GetNextContextMenuId(); |
96 var id = contextMenus.getIdFromCreateProperties(createProperties); | 110 var id = contextMenus.getIdFromCreateProperties(createProperties); |
97 var instanceId = isWebview ? arguments[0] : INSTANCEID_NON_WEBVIEW; | 111 var instanceId = isWebview ? arguments[0] : INSTANCEID_NON_WEBVIEW; |
98 var onclick = createProperties.onclick; | 112 var onclick = createProperties.onclick; |
99 | 113 |
100 var optArgs = { | 114 var optArgs = { |
101 __proto__: null, | 115 __proto__: null, |
102 customCallback: getCallback($Function.bind(createCallback, null, | 116 customCallback: getCallback($Function.bind(createCallback, null, |
103 instanceId, id, onclick)), | 117 instanceId, id, onclick)), |
104 }; | 118 }; |
105 sendRequest(this.name, $Array.from(arguments), | 119 var name = isWebview ? |
106 this.definition.parameters, optArgs); | 120 'chromeWebViewInternal.contextMenusCreate' : 'contextMenus.create'; |
| 121 sendRequest(name, $Array.from(arguments), |
| 122 bindingUtil ? undefined : this.definition.parameters, |
| 123 optArgs); |
107 return id; | 124 return id; |
108 }; | 125 }; |
109 | 126 |
110 function removeCallback(instanceId, id) { | 127 function removeCallback(instanceId, id) { |
111 delete contextMenus.handlersForId(instanceId, id)[id]; | 128 delete contextMenus.handlersForId(instanceId, id)[id]; |
112 } | 129 } |
113 | 130 |
114 requestHandlers.remove = function() { | 131 requestHandlers.remove = function() { |
115 var instanceId = isWebview ? arguments[0] : INSTANCEID_NON_WEBVIEW; | 132 var instanceId = isWebview ? arguments[0] : INSTANCEID_NON_WEBVIEW; |
116 var id = isWebview ? arguments[1] : arguments[0]; | 133 var id = isWebview ? arguments[1] : arguments[0]; |
117 var optArgs = { | 134 var optArgs = { |
118 __proto__: null, | 135 __proto__: null, |
119 customCallback: getCallback($Function.bind(removeCallback, null, | 136 customCallback: getCallback($Function.bind(removeCallback, null, |
120 instanceId, id)), | 137 instanceId, id)), |
121 }; | 138 }; |
122 sendRequest(this.name, $Array.from(arguments), | 139 var name = isWebview ? |
123 this.definition.parameters, optArgs); | 140 'chromeWebViewInternal.contextMenusRemove' : 'contextMenus.remove'; |
| 141 sendRequest(name, $Array.from(arguments), |
| 142 bindingUtil ? undefined : this.definition.parameters, |
| 143 optArgs); |
124 }; | 144 }; |
125 | 145 |
126 function updateCallback(instanceId, id, onclick) { | 146 function updateCallback(instanceId, id, onclick) { |
127 if (onclick) { | 147 if (onclick) { |
128 contextMenus.ensureListenerSetup(); | 148 contextMenus.ensureListenerSetup(); |
129 contextMenus.handlersForId(instanceId, id)[id] = onclick; | 149 contextMenus.handlersForId(instanceId, id)[id] = onclick; |
130 } else if (onclick === null) { | 150 } else if (onclick === null) { |
131 // When onclick is explicitly set to null, remove the event listener. | 151 // When onclick is explicitly set to null, remove the event listener. |
132 delete contextMenus.handlersForId(instanceId, id)[id]; | 152 delete contextMenus.handlersForId(instanceId, id)[id]; |
133 } | 153 } |
134 } | 154 } |
135 | 155 |
136 requestHandlers.update = function() { | 156 requestHandlers.update = function() { |
137 var instanceId = isWebview ? arguments[0] : INSTANCEID_NON_WEBVIEW; | 157 var instanceId = isWebview ? arguments[0] : INSTANCEID_NON_WEBVIEW; |
138 var id = isWebview ? arguments[1] : arguments[0]; | 158 var id = isWebview ? arguments[1] : arguments[0]; |
139 var updateProperties = isWebview ? arguments[2] : arguments[1]; | 159 var updateProperties = isWebview ? arguments[2] : arguments[1]; |
140 var onclick = updateProperties.onclick; | 160 var onclick = updateProperties.onclick; |
141 var optArgs = { | 161 var optArgs = { |
142 __proto__: null, | 162 __proto__: null, |
143 customCallback: getCallback($Function.bind(updateCallback, null, | 163 customCallback: getCallback($Function.bind(updateCallback, null, |
144 instanceId, id, onclick)), | 164 instanceId, id, onclick)), |
145 }; | 165 }; |
146 | 166 |
147 sendRequest(this.name, $Array.from(arguments), | 167 var name = isWebview ? |
148 this.definition.parameters, optArgs); | 168 'chromeWebViewInternal.contextMenusUpdate' : |
| 169 'contextMenus.update'; |
| 170 sendRequest(name, $Array.from(arguments), |
| 171 bindingUtil ? undefined : this.definition.parameters, optArgs); |
149 }; | 172 }; |
150 | 173 |
151 function removeAllCallback(instanceId) { | 174 function removeAllCallback(instanceId) { |
152 delete contextMenus.handlers[instanceId]; | 175 delete contextMenus.handlers[instanceId]; |
153 } | 176 } |
154 | 177 |
155 requestHandlers.removeAll = function() { | 178 requestHandlers.removeAll = function() { |
156 var instanceId = isWebview ? arguments[0] : INSTANCEID_NON_WEBVIEW; | 179 var instanceId = isWebview ? arguments[0] : INSTANCEID_NON_WEBVIEW; |
157 var optArgs = { | 180 var optArgs = { |
158 __proto__: null, | 181 __proto__: null, |
159 customCallback: getCallback($Function.bind(removeAllCallback, null, | 182 customCallback: getCallback($Function.bind(removeAllCallback, null, |
160 instanceId)), | 183 instanceId)), |
161 }; | 184 }; |
162 | 185 |
163 sendRequest(this.name, $Array.from(arguments), | 186 var name = isWebview ? |
| 187 'chromeWebViewInternal.contextMenusRemoveAll' : |
| 188 'contextMenus.removeAll'; |
| 189 sendRequest(name, $Array.from(arguments), |
164 this.definition.parameters, optArgs); | 190 this.definition.parameters, optArgs); |
165 }; | 191 }; |
166 | 192 |
167 return { | 193 return { |
168 requestHandlers: requestHandlers, | 194 requestHandlers: requestHandlers, |
169 }; | 195 }; |
170 } | 196 } |
171 | 197 |
172 exports.$set('create', createContextMenusHandlers); | 198 exports.$set('create', createContextMenusHandlers); |
OLD | NEW |