Mark two tests in ImeTest.java as flaky
[chromium-blink-merge.git] / extensions / renderer / resources / serial_custom_bindings.js
blob8c943ed0162d2d0ac69c3fdd091aca3008259fe7
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.
5 /**
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
11 * the ID parameter.
14 var binding = require('binding').Binding.create('serial');
15 var context = requireNative('v8_context');
16 var eventBindings = require('event_bindings');
17 var utils = require('utils');
19 var serialServicePromise = function() {
20 // getBackgroundPage is not available in unit tests so fall back to the
21 // current page's serial_service module.
22 if (!chrome.runtime.getBackgroundPage)
23 return requireAsync('serial_service');
25 // Load the serial_service module from the background page if one exists. This
26 // is necessary for serial connections created in one window to be usable
27 // after that window is closed. This is necessary because the Mojo async
28 // waiter only functions while the v8 context remains.
29 return utils.promise(chrome.runtime.getBackgroundPage).then(function(bgPage) {
30 return context.GetModuleSystem(bgPage).requireAsync('serial_service');
31 }).catch(function() {
32 return requireAsync('serial_service');
33 });
34 }();
36 function forwardToConnection(methodName) {
37 return function(connectionId) {
38 var args = $Array.slice(arguments, 1);
39 return serialServicePromise.then(function(serialService) {
40 return serialService.getConnection(connectionId);
41 }).then(function(connection) {
42 return $Function.apply(connection[methodName], connection, args);
43 });
47 binding.registerCustomHook(function(bindingsAPI) {
48 var apiFunctions = bindingsAPI.apiFunctions;
49 apiFunctions.setHandleRequestWithPromise('getDevices', function() {
50 return serialServicePromise.then(function(serialService) {
51 return serialService.getDevices();
52 });
53 });
55 apiFunctions.setHandleRequestWithPromise('connect', function(path, options) {
56 return serialServicePromise.then(function(serialService) {
57 return serialService.createConnection(path, options);
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}]);
64 result.connection.onError = function(error) {
65 eventBindings.dispatchEvent(
66 'serial.onReceiveError', [{connectionId: id, error: error}]);
68 return result.info;
69 }).catch (function(e) {
70 throw new Error('Failed to connect to the port.');
71 });
72 });
74 apiFunctions.setHandleRequestWithPromise(
75 'disconnect', forwardToConnection('close'));
76 apiFunctions.setHandleRequestWithPromise(
77 'getInfo', forwardToConnection('getInfo'));
78 apiFunctions.setHandleRequestWithPromise(
79 'update', forwardToConnection('setOptions'));
80 apiFunctions.setHandleRequestWithPromise(
81 'getControlSignals', forwardToConnection('getControlSignals'));
82 apiFunctions.setHandleRequestWithPromise(
83 'setControlSignals', forwardToConnection('setControlSignals'));
84 apiFunctions.setHandleRequestWithPromise(
85 'flush', forwardToConnection('flush'));
86 apiFunctions.setHandleRequestWithPromise(
87 'setPaused', forwardToConnection('setPaused'));
88 apiFunctions.setHandleRequestWithPromise(
89 'send', forwardToConnection('send'));
91 apiFunctions.setHandleRequestWithPromise('getConnections', function() {
92 return serialServicePromise.then(function(serialService) {
93 return serialService.getConnections();
94 }).then(function(connections) {
95 var promises = [];
96 for (var connection of connections.values()) {
97 promises.push(connection.getInfo());
99 return Promise.all(promises);
104 exports.binding = binding.generate();