• 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 Page = cr.ui.pageManager.Page;
7  /** @const */ var PageManager = cr.ui.pageManager.PageManager;
8
9  /**
10   * Encapsulated handling of the Bluetooth options page.
11   * @constructor
12   * @extends {cr.ui.pageManager.Page}
13   */
14  function BluetoothOptions() {
15    Page.call(this, 'bluetooth',
16              loadTimeData.getString('bluetoothOptionsPageTabTitle'),
17              'bluetooth-options');
18  }
19
20  cr.addSingletonGetter(BluetoothOptions);
21
22  BluetoothOptions.prototype = {
23    __proto__: Page.prototype,
24
25    /**
26     * The list of available (unpaired) bluetooth devices.
27     * @type {options.DeletableItemList}
28     * @private
29     */
30    deviceList_: null,
31
32    /** @override */
33    initializePage: function() {
34      Page.prototype.initializePage.call(this);
35      this.createDeviceList_();
36
37      BluetoothOptions.updateDiscoveryState(true);
38
39      $('bluetooth-add-device-cancel-button').onclick = function(event) {
40        PageManager.closeOverlay();
41      };
42
43      var self = this;
44      $('bluetooth-add-device-apply-button').onclick = function(event) {
45        var device = self.deviceList_.selectedItem;
46        var address = device.address;
47        PageManager.closeOverlay();
48        device.pairing = 'bluetoothStartConnecting';
49        options.BluetoothPairing.showDialog(device);
50        chrome.send('updateBluetoothDevice', [address, 'connect']);
51      };
52
53      $('bluetooth-unpaired-devices-list').addEventListener('change',
54                                                            function() {
55        var item = $('bluetooth-unpaired-devices-list').selectedItem;
56        // The "bluetooth-add-device-apply-button" should be enabled for devices
57        // that can be paired or remembered. Devices not supporting pairing will
58        // be just remembered and later reported as "item.paired" = true. The
59        // button should be disabled in any other case:
60        // * No item is selected (item is undefined).
61        // * Paired devices (item.paired is true) are already paired and a new
62        //   pairing attempt will fail. Paired devices could appear in this list
63        //   shortly after the pairing initiated in another window finishes.
64        // * "Connecting" devices (item.connecting is true) are in the process
65        //   of a pairing or connection. Another attempt to pair before the
66        //   ongoing pair finishes will fail, so the button should be disabled.
67        var disabled = !item || item.paired || item.connecting;
68        $('bluetooth-add-device-apply-button').disabled = disabled;
69      });
70    },
71
72    /** @override */
73    didClosePage: function() {
74      chrome.send('stopBluetoothDeviceDiscovery');
75    },
76
77    /**
78     * Creates, decorates and initializes the bluetooth device list.
79     * @private
80     */
81    createDeviceList_: function() {
82      var deviceList = $('bluetooth-unpaired-devices-list');
83      options.system.bluetooth.BluetoothDeviceList.decorate(deviceList);
84      this.deviceList_ = assertInstanceof(deviceList,
85                                          options.DeletableItemList);
86    }
87  };
88
89  /**
90   * Automatically start the device discovery process if the
91   * "Add device" dialog is visible.
92   */
93  BluetoothOptions.startDeviceDiscovery = function() {
94    var page = BluetoothOptions.getInstance();
95    if (page && page.visible)
96      chrome.send('findBluetoothDevices');
97  };
98
99  /**
100   * Updates the dialog to show that device discovery has stopped. Updates the
101   * label text and hides/unhides the spinner. based on discovery state.
102   */
103  BluetoothOptions.updateDiscoveryState = function(discovering) {
104    $('bluetooth-scanning-label').hidden = !discovering;
105    $('bluetooth-scanning-icon').hidden = !discovering;
106    $('bluetooth-scan-stopped-label').hidden = discovering;
107  };
108
109  /**
110   * If the "Add device" dialog is visible, dismiss it.
111   */
112  BluetoothOptions.dismissOverlay = function() {
113    var page = BluetoothOptions.getInstance();
114    if (page && page.visible)
115      PageManager.closeOverlay();
116  };
117
118  // Export
119  return {
120    BluetoothOptions: BluetoothOptions
121  };
122});
123