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 context = requireNative('v8_context'); | 15 var context = requireNative('v8_context'); |
| 16 var eventBindings = require('event_bindings'); |
16 var utils = require('utils'); | 17 var utils = require('utils'); |
17 | 18 |
18 var serialServicePromise = function() { | 19 var serialServicePromise = function() { |
19 // getBackgroundPage is not available in unit tests so fall back to the | 20 // getBackgroundPage is not available in unit tests so fall back to the |
20 // current page's serial_service module. | 21 // current page's serial_service module. |
21 if (!chrome.runtime.getBackgroundPage) | 22 if (!chrome.runtime.getBackgroundPage) |
22 return requireAsync('serial_service'); | 23 return requireAsync('serial_service'); |
23 | 24 |
24 // Load the serial_service module from the background page if one exists. This | 25 // 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 // is necessary for serial connections created in one window to be usable |
(...skipping 15 matching lines...) Expand all Loading... |
41 return $Function.apply(connection[methodName], connection, args); | 42 return $Function.apply(connection[methodName], connection, args); |
42 }); | 43 }); |
43 }; | 44 }; |
44 } | 45 } |
45 | 46 |
46 binding.registerCustomHook(function(bindingsAPI) { | 47 binding.registerCustomHook(function(bindingsAPI) { |
47 var apiFunctions = bindingsAPI.apiFunctions; | 48 var apiFunctions = bindingsAPI.apiFunctions; |
48 apiFunctions.setHandleRequestWithPromise('getDevices', function() { | 49 apiFunctions.setHandleRequestWithPromise('getDevices', function() { |
49 return serialServicePromise.then(function(serialService) { | 50 return serialServicePromise.then(function(serialService) { |
50 return serialService.getDevices(); | 51 return serialService.getDevices(); |
51 }) | 52 }); |
52 }); | 53 }); |
53 | 54 |
54 apiFunctions.setHandleRequestWithPromise('connect', function(path, options) { | 55 apiFunctions.setHandleRequestWithPromise('connect', function(path, options) { |
55 return serialServicePromise.then(function(serialService) { | 56 return serialServicePromise.then(function(serialService) { |
56 return serialService.createConnection(path, options); | 57 return serialService.createConnection(path, options); |
57 }).then(function(result) { | 58 }).then(function(result) { |
| 59 var id = result.info.connectionId; |
| 60 result.connection.onData = function(data) { |
| 61 eventBindings.dispatchEvent( |
| 62 'serial.onReceive', [{connectionId: id, data: data}]); |
| 63 }; |
| 64 result.connection.onError = function(error) { |
| 65 eventBindings.dispatchEvent( |
| 66 'serial.onReceiveError', [{connectionId: id, error: error}]); |
| 67 }; |
58 return result.info; | 68 return result.info; |
59 }).catch (function(e) { | 69 }).catch (function(e) { |
60 throw new Error('Failed to connect to the port.'); | 70 throw new Error('Failed to connect to the port.'); |
61 }); | 71 }); |
62 }); | 72 }); |
63 | 73 |
64 apiFunctions.setHandleRequestWithPromise( | 74 apiFunctions.setHandleRequestWithPromise( |
65 'disconnect', forwardToConnection('close')); | 75 'disconnect', forwardToConnection('close')); |
66 apiFunctions.setHandleRequestWithPromise( | 76 apiFunctions.setHandleRequestWithPromise( |
67 'getInfo', forwardToConnection('getInfo')); | 77 'getInfo', forwardToConnection('getInfo')); |
68 apiFunctions.setHandleRequestWithPromise( | 78 apiFunctions.setHandleRequestWithPromise( |
69 'update', forwardToConnection('setOptions')); | 79 'update', forwardToConnection('setOptions')); |
70 apiFunctions.setHandleRequestWithPromise( | 80 apiFunctions.setHandleRequestWithPromise( |
71 'getControlSignals', forwardToConnection('getControlSignals')); | 81 'getControlSignals', forwardToConnection('getControlSignals')); |
72 apiFunctions.setHandleRequestWithPromise( | 82 apiFunctions.setHandleRequestWithPromise( |
73 'setControlSignals', forwardToConnection('setControlSignals')); | 83 'setControlSignals', forwardToConnection('setControlSignals')); |
74 apiFunctions.setHandleRequestWithPromise( | 84 apiFunctions.setHandleRequestWithPromise( |
75 'flush', forwardToConnection('flush')); | 85 'flush', forwardToConnection('flush')); |
76 apiFunctions.setHandleRequestWithPromise( | 86 apiFunctions.setHandleRequestWithPromise( |
77 'setPaused', forwardToConnection('setPaused')); | 87 'setPaused', forwardToConnection('setPaused')); |
| 88 apiFunctions.setHandleRequestWithPromise( |
| 89 'send', forwardToConnection('send')); |
78 | 90 |
79 apiFunctions.setHandleRequestWithPromise('getConnections', function() { | 91 apiFunctions.setHandleRequestWithPromise('getConnections', function() { |
80 return serialServicePromise.then(function(serialService) { | 92 return serialServicePromise.then(function(serialService) { |
81 return serialService.getConnections(); | 93 return serialService.getConnections(); |
82 }).then(function(connections) { | 94 }).then(function(connections) { |
83 var promises = []; | 95 var promises = []; |
84 for (var id in connections) { | 96 for (var id in connections) { |
85 promises.push(connections[id].getInfo()); | 97 promises.push(connections[id].getInfo()); |
86 } | 98 } |
87 return Promise.all(promises); | 99 return Promise.all(promises); |
88 }); | 100 }); |
89 }); | 101 }); |
90 }); | 102 }); |
91 | 103 |
92 exports.binding = binding.generate(); | 104 exports.binding = binding.generate(); |
OLD | NEW |