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

Side by Side 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 unified diff | Download patch
OLDNEW
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 /**
6 * Custom bindings for the Serial API.
7 *
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
11 * the ID parameter.
12 */
13
5 var binding = require('binding').Binding.create('serial'); 14 var binding = require('binding').Binding.create('serial');
6 15
7 function createAsyncProxy(targetPromise, functionNames) { 16 function createAsyncProxy(targetPromise, functionNames) {
8 var functionProxies = {}; 17 var functionProxies = {};
9 $Array.forEach(functionNames, function(name) { 18 $Array.forEach(functionNames, function(name) {
10 functionProxies[name] = function() { 19 functionProxies[name] = function() {
11 var args = arguments; 20 var args = arguments;
12 return targetPromise.then(function(target) { 21 return targetPromise.then(function(target) {
13 return $Function.apply(target[name], target, args); 22 return $Function.apply(target[name], target, args);
14 }); 23 });
15 } 24 };
16 }); 25 });
17 return functionProxies; 26 return functionProxies;
18 } 27 }
19 28
20 var serialService = createAsyncProxy(requireAsync('serial_service'), [ 29 var serialService = createAsyncProxy(requireAsync('serial_service'), [
21 'getDevices', 30 'getDevices',
31 'createConnection',
32 'getConnection',
33 'getConnections',
22 ]); 34 ]);
23 35
36 function forwardToConnection(methodName) {
37 return function(connectionId) {
38 var args = $Array.slice(arguments, 1);
39 return serialService.getConnection(connectionId).then(function(connection) {
40 return $Function.apply(connection[methodName], connection, args);
41 });
42 };
43 }
44
24 binding.registerCustomHook(function(bindingsAPI) { 45 binding.registerCustomHook(function(bindingsAPI) {
25 var apiFunctions = bindingsAPI.apiFunctions; 46 var apiFunctions = bindingsAPI.apiFunctions;
26 apiFunctions.setHandleRequestWithPromise('getDevices', 47 apiFunctions.setHandleRequestWithPromise('getDevices',
27 serialService.getDevices); 48 serialService.getDevices);
49
50 apiFunctions.setHandleRequestWithPromise('connect', function(path, options) {
51 return serialService.createConnection(path, options).then(function(result) {
52 return result.info;
53 }).catch (function(e) {
54 throw new Error('Failed to connect to the port.');
55 });
56 });
57
58 apiFunctions.setHandleRequestWithPromise(
59 'disconnect', forwardToConnection('close'));
60 apiFunctions.setHandleRequestWithPromise(
61 'getInfo', forwardToConnection('getInfo'));
62 apiFunctions.setHandleRequestWithPromise(
63 'update', forwardToConnection('setOptions'));
64 apiFunctions.setHandleRequestWithPromise(
65 'getControlSignals', forwardToConnection('getControlSignals'));
66 apiFunctions.setHandleRequestWithPromise(
67 'setControlSignals', forwardToConnection('setControlSignals'));
68 apiFunctions.setHandleRequestWithPromise(
69 'flush', forwardToConnection('flush'));
70 apiFunctions.setHandleRequestWithPromise(
71 'setPaused', forwardToConnection('setPaused'));
72
73 apiFunctions.setHandleRequestWithPromise('getConnections', function() {
74 return serialService.getConnections().then(function(connections) {
75 var promises = [];
76 for (var id in connections) {
77 promises.push(connections[id].getInfo());
78 }
79 return Promise.all(promises);
80 });
81 });
28 }); 82 });
29 83
30 exports.binding = binding.generate(); 84 exports.binding = binding.generate();
OLDNEW
« 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