Index: extensions/renderer/resources/serial_custom_bindings.js |
diff --git a/extensions/renderer/resources/serial_custom_bindings.js b/extensions/renderer/resources/serial_custom_bindings.js |
index 3d15acff1c7fca3f4a1e9420bc53a5fd8162414e..31f6daec467a7a6e59a9692e57d26d0088ff8f0f 100644 |
--- a/extensions/renderer/resources/serial_custom_bindings.js |
+++ b/extensions/renderer/resources/serial_custom_bindings.js |
@@ -2,6 +2,15 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+/** |
+ * Custom bindings for the Serial API. |
+ * |
+ * The bindings are implemented by asynchronously delegating to the |
+ * serial_service module. The functions that apply to a particular connection |
+ * are delegated to the appropriate method on the Connection object specified by |
+ * the ID parameter. |
+ */ |
+ |
var binding = require('binding').Binding.create('serial'); |
function createAsyncProxy(targetPromise, functionNames) { |
@@ -12,19 +21,64 @@ function createAsyncProxy(targetPromise, functionNames) { |
return targetPromise.then(function(target) { |
return $Function.apply(target[name], target, args); |
}); |
- } |
+ }; |
}); |
return functionProxies; |
} |
var serialService = createAsyncProxy(requireAsync('serial_service'), [ |
'getDevices', |
+ 'createConnection', |
+ 'getConnection', |
+ 'getConnections', |
]); |
+function forwardToConnection(methodName) { |
+ return function(connectionId) { |
+ var args = $Array.slice(arguments, 1); |
+ return serialService.getConnection(connectionId).then(function(connection) { |
+ return $Function.apply(connection[methodName], connection, args); |
+ }); |
+ }; |
+} |
+ |
binding.registerCustomHook(function(bindingsAPI) { |
var apiFunctions = bindingsAPI.apiFunctions; |
apiFunctions.setHandleRequestWithPromise('getDevices', |
serialService.getDevices); |
+ |
+ apiFunctions.setHandleRequestWithPromise('connect', function(path, options) { |
+ return serialService.createConnection(path, options).then(function(result) { |
+ return result.info; |
+ }).catch (function(e) { |
+ throw new Error('Failed to connect to the port.'); |
+ }); |
+ }); |
+ |
+ apiFunctions.setHandleRequestWithPromise( |
+ 'disconnect', forwardToConnection('close')); |
+ apiFunctions.setHandleRequestWithPromise( |
+ 'getInfo', forwardToConnection('getInfo')); |
+ apiFunctions.setHandleRequestWithPromise( |
+ 'update', forwardToConnection('setOptions')); |
+ apiFunctions.setHandleRequestWithPromise( |
+ 'getControlSignals', forwardToConnection('getControlSignals')); |
+ apiFunctions.setHandleRequestWithPromise( |
+ 'setControlSignals', forwardToConnection('setControlSignals')); |
+ apiFunctions.setHandleRequestWithPromise( |
+ 'flush', forwardToConnection('flush')); |
+ apiFunctions.setHandleRequestWithPromise( |
+ 'setPaused', forwardToConnection('setPaused')); |
+ |
+ apiFunctions.setHandleRequestWithPromise('getConnections', function() { |
+ return serialService.getConnections().then(function(connections) { |
+ var promises = []; |
+ for (var id in connections) { |
+ promises.push(connections[id].getInfo()); |
+ } |
+ return Promise.all(promises); |
+ }); |
+ }); |
}); |
exports.binding = binding.generate(); |