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 /** | 5 /** |
6 * Custom bindings for the Serial API. | 6 * Custom bindings for the Serial API. |
7 * | 7 * |
8 * The bindings are implemented by asynchronously delegating to the | 8 * The bindings are implemented by asynchronously delegating to the |
9 * serial_service module. The functions that apply to a particular connection | 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 | 10 * are delegated to the appropriate method on the Connection object specified by |
11 * the ID parameter. | 11 * the ID parameter. |
12 */ | 12 */ |
13 | 13 |
14 var binding = require('binding').Binding.create('serial'); | 14 var binding = require('binding').Binding.create('serial'); |
| 15 var utils = require('utils'); |
15 | 16 |
16 function createAsyncProxy(targetPromise, functionNames) { | 17 var serialService = utils.createAsyncProxy( |
17 var functionProxies = {}; | 18 utils.requireAsyncFromBackgroundPage('serial_service'), [ |
18 $Array.forEach(functionNames, function(name) { | |
19 functionProxies[name] = function() { | |
20 var args = arguments; | |
21 return targetPromise.then(function(target) { | |
22 return $Function.apply(target[name], target, args); | |
23 }); | |
24 }; | |
25 }); | |
26 return functionProxies; | |
27 } | |
28 | |
29 var serialService = createAsyncProxy(requireAsync('serial_service'), [ | |
30 'getDevices', | 19 'getDevices', |
31 'createConnection', | 20 'createConnection', |
32 'getConnection', | 21 'getConnection', |
33 'getConnections', | 22 'getConnections', |
34 ]); | 23 ]); |
35 | 24 |
36 function forwardToConnection(methodName) { | 25 function forwardToConnection(methodName) { |
37 return function(connectionId) { | 26 return function(connectionId) { |
38 var args = $Array.slice(arguments, 1); | 27 var args = $Array.slice(arguments, 1); |
39 return serialService.getConnection(connectionId).then(function(connection) { | 28 return serialService.getConnection(connectionId).then(function(connection) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 var promises = []; | 64 var promises = []; |
76 for (var id in connections) { | 65 for (var id in connections) { |
77 promises.push(connections[id].getInfo()); | 66 promises.push(connections[id].getInfo()); |
78 } | 67 } |
79 return Promise.all(promises); | 68 return Promise.all(promises); |
80 }); | 69 }); |
81 }); | 70 }); |
82 }); | 71 }); |
83 | 72 |
84 exports.binding = binding.generate(); | 73 exports.binding = binding.generate(); |
OLD | NEW |