Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Unified Diff: extensions/renderer/resources/serial_custom_bindings.js

Issue 423403002: Implement more of chrome.serial on the Mojo SerialConnection. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@js-serial
Patch Set: add missed GN change Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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();
« no previous file with comments | « extensions/renderer/api/serial/serial_api_unittest.cc ('k') | extensions/renderer/resources/serial_service.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698