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.
6 * Custom bindings for the Serial API.
8 * The bindings are implemented by asynchronously delegating to the
9 * serial_service module. The functions that apply to a particular connection
10 * are delegated to the appropriate method on the Connection object specified by
14 var binding
= require('binding').Binding
.create('serial');
15 var context
= requireNative('v8_context');
16 var utils
= require('utils');
18 var serialServicePromise = function() {
19 // getBackgroundPage is not available in unit tests so fall back to the
20 // current page's serial_service module.
21 if (!chrome
.runtime
.getBackgroundPage
)
22 return requireAsync('serial_service');
24 // Load the serial_service module from the background page if one exists. This
25 // is necessary for serial connections created in one window to be usable
26 // after that window is closed. This is necessary because the Mojo async
27 // waiter only functions while the v8 context remains.
28 return utils
.promise(chrome
.runtime
.getBackgroundPage
).then(function(bgPage
) {
29 return context
.GetModuleSystem(bgPage
).requireAsync('serial_service');
31 return requireAsync('serial_service');
35 function forwardToConnection(methodName
) {
36 return function(connectionId
) {
37 var args
= $Array
.slice(arguments
, 1);
38 return serialServicePromise
.then(function(serialService
) {
39 return serialService
.getConnection(connectionId
);
40 }).then(function(connection
) {
41 return $Function
.apply(connection
[methodName
], connection
, args
);
46 binding
.registerCustomHook(function(bindingsAPI
) {
47 var apiFunctions
= bindingsAPI
.apiFunctions
;
48 apiFunctions
.setHandleRequestWithPromise('getDevices', function() {
49 return serialServicePromise
.then(function(serialService
) {
50 return serialService
.getDevices();
54 apiFunctions
.setHandleRequestWithPromise('connect', function(path
, options
) {
55 return serialServicePromise
.then(function(serialService
) {
56 return serialService
.createConnection(path
, options
);
57 }).then(function(result
) {
59 }).catch (function(e
) {
60 throw new Error('Failed to connect to the port.');
64 apiFunctions
.setHandleRequestWithPromise(
65 'disconnect', forwardToConnection('close'));
66 apiFunctions
.setHandleRequestWithPromise(
67 'getInfo', forwardToConnection('getInfo'));
68 apiFunctions
.setHandleRequestWithPromise(
69 'update', forwardToConnection('setOptions'));
70 apiFunctions
.setHandleRequestWithPromise(
71 'getControlSignals', forwardToConnection('getControlSignals'));
72 apiFunctions
.setHandleRequestWithPromise(
73 'setControlSignals', forwardToConnection('setControlSignals'));
74 apiFunctions
.setHandleRequestWithPromise(
75 'flush', forwardToConnection('flush'));
76 apiFunctions
.setHandleRequestWithPromise(
77 'setPaused', forwardToConnection('setPaused'));
79 apiFunctions
.setHandleRequestWithPromise('getConnections', function() {
80 return serialServicePromise
.then(function(serialService
) {
81 return serialService
.getConnections();
82 }).then(function(connections
) {
84 for (var id
in connections
) {
85 promises
.push(connections
[id
].getInfo());
87 return Promise
.all(promises
);
92 exports
.binding
= binding
.generate();