• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2012 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.
4
5cr.define('options', function() {
6  /** @const */ var OptionsPage = options.OptionsPage;
7
8  /**
9   * Encapsulated handling of the Bluetooth options page.
10   * @constructor
11   */
12  function BluetoothOptions() {
13    OptionsPage.call(this,
14                     'bluetooth',
15                     loadTimeData.getString('bluetoothOptionsPageTabTitle'),
16                     'bluetooth-options');
17  }
18
19  cr.addSingletonGetter(BluetoothOptions);
20
21  BluetoothOptions.prototype = {
22    __proto__: OptionsPage.prototype,
23
24    /**
25     * The list of available (unpaired) bluetooth devices.
26     * @type {DeletableItemList}
27     * @private
28     */
29    deviceList_: null,
30
31    /** @override */
32    initializePage: function() {
33      OptionsPage.prototype.initializePage.call(this);
34      this.createDeviceList_();
35
36      $('bluetooth-add-device-cancel-button').onclick = function(event) {
37        chrome.send('stopBluetoothDeviceDiscovery');
38        OptionsPage.closeOverlay();
39      };
40
41      var self = this;
42      $('bluetooth-add-device-apply-button').onclick = function(event) {
43        var device = self.deviceList_.selectedItem;
44        var address = device.address;
45        chrome.send('stopBluetoothDeviceDiscovery');
46        OptionsPage.closeOverlay();
47        device.pairing = 'bluetoothStartConnecting';
48        options.BluetoothPairing.showDialog(device);
49        chrome.send('updateBluetoothDevice', [address, 'connect']);
50      };
51
52      $('bluetooth-unpaired-devices-list').addEventListener('change',
53                                                            function() {
54        var item = $('bluetooth-unpaired-devices-list').selectedItem;
55        // The "bluetooth-add-device-apply-button" should be enabled for devices
56        // that can be paired or remembered. Devices not supporting pairing will
57        // be just remembered and later reported as "item.paired" = true. The
58        // button should be disabled in any other case:
59        // * No item is selected (item is undefined).
60        // * Paired devices (item.paired is true) are already paired and a new
61        //   pairing attempt will fail. Paired devices could appear in this list
62        //   shortly after the pairing initiated in another window finishes.
63        // * "Connecting" devices (item.connecting is true) are in the process
64        //   of a pairing or connection. Another attempt to pair before the
65        //   ongoing pair finishes will fail, so the button should be disabled.
66        var disabled = !item || item.paired || item.connecting;
67        $('bluetooth-add-device-apply-button').disabled = disabled;
68      });
69    },
70
71    /**
72     * Creates, decorates and initializes the bluetooth device list.
73     * @private
74     */
75    createDeviceList_: function() {
76      this.deviceList_ = $('bluetooth-unpaired-devices-list');
77      options.system.bluetooth.BluetoothDeviceList.decorate(this.deviceList_);
78    }
79  };
80
81  /**
82   * Automatically start the device discovery process if the
83   * "Add device" dialog is visible.
84   */
85  BluetoothOptions.updateDiscovery = function() {
86    var page = BluetoothOptions.getInstance();
87    if (page && page.visible)
88      chrome.send('findBluetoothDevices');
89  }
90
91  // Export
92  return {
93    BluetoothOptions: BluetoothOptions
94  };
95});
96