summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/bluetooth_internals/adapter_broker.js
blob: 1ca5ea685c51a96555e3773251991e0cfada7e56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * Javascript for AdapterBroker, served from
 *     chrome://bluetooth-internals/.
 */
cr.define('adapter_broker', function() {
  /** @typedef {bluetooth.mojom.AdapterRemote} */
  let AdapterRemote;
  /** @typedef {bluetooth.mojom.DeviceRemote} */
  let DeviceRemote;
  /** @typedef {bluetooth.mojom.DiscoverySessionRemote} */
  let DiscoverySessionRemote;

  /**
   * Enum of adapter property names. Used for adapterchanged events.
   * @enum {string}
   */
  const AdapterProperty = {
    DISCOVERABLE: 'discoverable',
    DISCOVERING: 'discovering',
    POWERED: 'powered',
    PRESENT: 'present',
  };

  /**
   * The proxy class of an adapter and router of adapter events.
   * Exposes an EventTarget interface that allows other object to subscribe to
   * to specific AdapterClient events.
   * Provides remote access to Adapter functions. Converts parameters to Mojo
   * handles and back when necessary.
   *
   * @implements {bluetooth.mojom.AdapterClientInterface}
   */
  class AdapterBroker extends cr.EventTarget {
    /** @param {!AdapterRemote} adapter */
    constructor(adapter) {
      super();
      this.adapterClientReceiver_ =
          new bluetooth.mojom.AdapterClientReceiver(this);
      this.adapter_ = adapter;
      this.adapter_.setClient(
          this.adapterClientReceiver_.$.bindNewPipeAndPassRemote());
    }

    presentChanged(present) {
      this.dispatchEvent(new CustomEvent('adapterchanged', {
        detail: {
          property: AdapterProperty.PRESENT,
          value: present,
        }
      }));
    }

    poweredChanged(powered) {
      this.dispatchEvent(new CustomEvent('adapterchanged', {
        detail: {
          property: AdapterProperty.POWERED,
          value: powered,
        }
      }));
    }

    discoverableChanged(discoverable) {
      this.dispatchEvent(new CustomEvent('adapterchanged', {
        detail: {
          property: AdapterProperty.DISCOVERABLE,
          value: discoverable,
        }
      }));
    }

    discoveringChanged(discovering) {
      this.dispatchEvent(new CustomEvent('adapterchanged', {
        detail: {
          property: AdapterProperty.DISCOVERING,
          value: discovering,
        }
      }));
    }

    deviceAdded(device) {
      this.dispatchEvent(
          new CustomEvent('deviceadded', {detail: {deviceInfo: device}}));
    }

    deviceChanged(device) {
      this.dispatchEvent(
          new CustomEvent('devicechanged', {detail: {deviceInfo: device}}));
    }

    deviceRemoved(device) {
      this.dispatchEvent(
          new CustomEvent('deviceremoved', {detail: {deviceInfo: device}}));
    }

    /**
     * Creates a GATT connection to the device with |address|.
     * @param {string} address
     * @return {!Promise<!bluetooth.mojom.Device>}
     */
    connectToDevice(address) {
      return this.adapter_.connectToDevice(address).then(function(response) {
        if (response.result != bluetooth.mojom.ConnectResult.SUCCESS) {
          // TODO(crbug.com/663394): Replace with more descriptive error
          // messages.
          const ConnectResult = bluetooth.mojom.ConnectResult;
          const errorString = Object.keys(ConnectResult).find(function(key) {
            return ConnectResult[key] === response.result;
          });

          throw new Error(errorString);
        }

        return response.device;
      });
    }

    /**
     * Gets an array of currently detectable devices from the Adapter service.
     * @return {Promise<{devices: Array<!bluetooth.mojom.DeviceInfo>}>}
     */
    getDevices() {
      return this.adapter_.getDevices();
    }

    /**
     * Gets the current state of the Adapter.
     * @return {Promise<{info: bluetooth.mojom.AdapterInfo}>}
     */
    getInfo() {
      return this.adapter_.getInfo();
    }


    /**
     * Requests the adapter to start a new discovery session.
     * @return {!Promise<!bluetooth.mojom.DiscoverySessionRemote>}
     */
    startDiscoverySession() {
      return this.adapter_.startDiscoverySession().then(function(response) {
        if (!response.session) {
          throw new Error('Discovery session failed to start');
        }

        return response.session;
      });
    }
  }

  let adapterBroker = null;

  /**
   * Initializes an AdapterBroker if one doesn't exist.
   * @param {!mojom.BluetoothInternalsHandlerRemote=}
   *     opt_bluetoothInternalsHandler
   * @return {!Promise<!adapter_broker.AdapterBroker>} resolves with
   *     AdapterBroker, rejects if Bluetooth is not supported.
   */
  function getAdapterBroker(opt_bluetoothInternalsHandler) {
    if (adapterBroker) {
      return Promise.resolve(adapterBroker);
    }

    const bluetoothInternalsHandler = opt_bluetoothInternalsHandler ?
        opt_bluetoothInternalsHandler :
        mojom.BluetoothInternalsHandler.getRemote();

    // Get an Adapter service.
    return bluetoothInternalsHandler.getAdapter().then(function(response) {
      if (!response.adapter) {
        throw new Error('Bluetooth Not Supported on this platform.');
      }

      adapterBroker = new AdapterBroker(response.adapter);
      return adapterBroker;
    });
  }

  return {
    AdapterBroker: AdapterBroker,
    AdapterProperty: AdapterProperty,
    getAdapterBroker: getAdapterBroker,
  };
});