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.
5 cr.define('options', function() {
6 /** @const */ var Page = cr.ui.pageManager.Page;
7 /** @const */ var PageManager = cr.ui.pageManager.PageManager;
10 * Encapsulated handling of the Bluetooth options page.
12 * @extends {cr.ui.pageManager.Page}
14 function BluetoothOptions() {
15 Page.call(this, 'bluetooth',
16 loadTimeData.getString('bluetoothOptionsPageTabTitle'),
20 cr.addSingletonGetter(BluetoothOptions);
22 BluetoothOptions.prototype = {
23 __proto__: Page.prototype,
26 * The list of available (unpaired) bluetooth devices.
27 * @type {options.DeletableItemList}
33 initializePage: function() {
34 Page.prototype.initializePage.call(this);
35 this.createDeviceList_();
37 BluetoothOptions.updateDiscoveryState(true);
39 $('bluetooth-add-device-cancel-button').onclick = function(event) {
40 PageManager.closeOverlay();
44 $('bluetooth-add-device-apply-button').onclick = function(event) {
45 chrome.send('coreOptionsUserMetricsAction',
46 ['Options_BluetoothConnectNewDevice']);
47 var device = self.deviceList_.selectedItem;
48 var address = device.address;
49 PageManager.closeOverlay();
50 device.pairing = 'bluetoothStartConnecting';
51 options.BluetoothPairing.showDialog(device);
52 chrome.send('updateBluetoothDevice', [address, 'connect']);
55 $('bluetooth-unpaired-devices-list').addEventListener('change',
57 var item = $('bluetooth-unpaired-devices-list').selectedItem;
58 // The "bluetooth-add-device-apply-button" should be enabled for devices
59 // that can be paired or remembered. Devices not supporting pairing will
60 // be just remembered and later reported as "item.paired" = true. The
61 // button should be disabled in any other case:
62 // * No item is selected (item is undefined).
63 // * Paired devices (item.paired is true) are already paired and a new
64 // pairing attempt will fail. Paired devices could appear in this list
65 // shortly after the pairing initiated in another window finishes.
66 // * "Connecting" devices (item.connecting is true) are in the process
67 // of a pairing or connection. Another attempt to pair before the
68 // ongoing pair finishes will fail, so the button should be disabled.
69 var disabled = !item || item.paired || item.connecting;
70 $('bluetooth-add-device-apply-button').disabled = disabled;
75 didClosePage: function() {
76 chrome.send('stopBluetoothDeviceDiscovery');
80 * Creates, decorates and initializes the bluetooth device list.
83 createDeviceList_: function() {
84 var deviceList = $('bluetooth-unpaired-devices-list');
85 options.system.bluetooth.BluetoothDeviceList.decorate(deviceList);
86 this.deviceList_ = assertInstanceof(deviceList,
87 options.DeletableItemList);
92 * Automatically start the device discovery process if the
93 * "Add device" dialog is visible.
95 BluetoothOptions.startDeviceDiscovery = function() {
96 var page = BluetoothOptions.getInstance();
97 if (page && page.visible)
98 chrome.send('findBluetoothDevices');
102 * Updates the dialog to show that device discovery has stopped. Updates the
103 * label text and hides/unhides the spinner. based on discovery state.
105 BluetoothOptions.updateDiscoveryState = function(discovering) {
106 $('bluetooth-scanning-label').hidden = !discovering;
107 $('bluetooth-scanning-icon').hidden = !discovering;
108 $('bluetooth-scan-stopped-label').hidden = discovering;
112 * If the "Add device" dialog is visible, dismiss it.
114 BluetoothOptions.dismissOverlay = function() {
115 var page = BluetoothOptions.getInstance();
116 if (page && page.visible)
117 PageManager.closeOverlay();
122 BluetoothOptions: BluetoothOptions