| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.h" | 5 #include "chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" |
| 11 #include "chrome/browser/chromeos/system/runtime_environment.h" | 12 #include "chrome/browser/chromeos/system/runtime_environment.h" |
| 12 #include "chrome/browser/ui/webui/options/chromeos/system_settings_provider.h" | 13 #include "chrome/browser/ui/webui/options/chromeos/system_settings_provider.h" |
| 13 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 14 #include "grit/chromium_strings.h" | 15 #include "grit/chromium_strings.h" |
| 15 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 17 | 18 |
| 18 namespace chromeos { | 19 namespace chromeos { |
| 19 | 20 |
| 20 BluetoothOptionsHandler::BluetoothOptionsHandler() | 21 BluetoothOptionsHandler::BluetoothOptionsHandler() |
| 21 : chromeos::CrosOptionsPageUIHandler( | 22 : chromeos::CrosOptionsPageUIHandler( |
| 22 new chromeos::SystemSettingsProvider()) { | 23 new chromeos::SystemSettingsProvider()) { |
| 23 } | 24 } |
| 24 | 25 |
| 25 BluetoothOptionsHandler::~BluetoothOptionsHandler() { | 26 BluetoothOptionsHandler::~BluetoothOptionsHandler() { |
| 26 // TODO(kevers): Shutdown bluetooth. | 27 if (!CommandLine::ForCurrentProcess() |
| 28 ->HasSwitch(switches::kEnableBluetooth)) { |
| 29 return; |
| 30 } |
| 31 |
| 32 chromeos::BluetoothManager* bluetooth_manager = |
| 33 chromeos::BluetoothManager::GetInstance(); |
| 34 DCHECK(bluetooth_manager); |
| 35 |
| 36 chromeos::BluetoothAdapter* default_adapter = |
| 37 bluetooth_manager->DefaultAdapter(); |
| 38 |
| 39 if (default_adapter != NULL) { |
| 40 default_adapter->RemoveObserver(this); |
| 41 } |
| 42 |
| 43 bluetooth_manager->RemoveObserver(this); |
| 27 } | 44 } |
| 28 | 45 |
| 29 void BluetoothOptionsHandler::GetLocalizedValues( | 46 void BluetoothOptionsHandler::GetLocalizedValues( |
| 30 DictionaryValue* localized_strings) { | 47 DictionaryValue* localized_strings) { |
| 31 DCHECK(localized_strings); | 48 DCHECK(localized_strings); |
| 32 | 49 |
| 33 localized_strings->SetString("bluetooth", | 50 localized_strings->SetString("bluetooth", |
| 34 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_SECTION_TITLE_BLUETOOTH)); | 51 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_SECTION_TITLE_BLUETOOTH)); |
| 35 localized_strings->SetString("enableBluetooth", | 52 localized_strings->SetString("enableBluetooth", |
| 36 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_ENABLE)); | 53 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_ENABLE)); |
| 37 localized_strings->SetString("findBluetoothDevices", | 54 localized_strings->SetString("findBluetoothDevices", |
| 38 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_FIND_BLUETOOTH_DEVICES)); | 55 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_FIND_BLUETOOTH_DEVICES)); |
| 39 localized_strings->SetString("bluetoothScanning", | 56 localized_strings->SetString("bluetoothScanning", |
| 40 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_SCANNING)); | 57 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_SCANNING)); |
| 41 localized_strings->SetString("bluetoothDeviceConnected", | 58 localized_strings->SetString("bluetoothDeviceConnected", |
| 42 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_CONNECTED)); | 59 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_CONNECTED)); |
| 43 localized_strings->SetString("bluetoothDeviceNotPaired", | 60 localized_strings->SetString("bluetoothDeviceNotPaired", |
| 44 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_NOT_PAIRED)); | 61 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_NOT_PAIRED)); |
| 45 localized_strings->SetString("bluetoothConnectDevice", | 62 localized_strings->SetString("bluetoothConnectDevice", |
| 46 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_CONNECT)); | 63 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_CONNECT)); |
| 47 localized_strings->SetString("bluetoothDisconnectDevice", | 64 localized_strings->SetString("bluetoothDisconnectDevice", |
| 48 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_DISCONNECT)); | 65 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_DISCONNECT)); |
| 49 } | 66 } |
| 50 | 67 |
| 51 void BluetoothOptionsHandler::Initialize() { | 68 void BluetoothOptionsHandler::Initialize() { |
| 52 DCHECK(web_ui_); | 69 DCHECK(web_ui_); |
| 53 // Bluetooth support is a work in progress. Supress the feature unless | 70 // Bluetooth support is a work in progress. Supress the feature unless |
| 54 // explicitly enabled via a command line flag. | 71 // explicitly enabled via a command line flag. |
| 55 // TODO(kevers): Test for presence of bluetooth hardware. | |
| 56 if (!CommandLine::ForCurrentProcess() | 72 if (!CommandLine::ForCurrentProcess() |
| 57 ->HasSwitch(switches::kEnableBluetooth)) { | 73 ->HasSwitch(switches::kEnableBluetooth)) { |
| 58 return; | 74 return; |
| 59 } | 75 } |
| 76 |
| 60 web_ui_->CallJavascriptFunction( | 77 web_ui_->CallJavascriptFunction( |
| 61 "options.SystemOptions.showBluetoothSettings"); | 78 "options.SystemOptions.showBluetoothSettings"); |
| 62 // TODO(kevers): Initialize bluetooth. | 79 |
| 80 chromeos::BluetoothManager* bluetooth_manager = |
| 81 chromeos::BluetoothManager::GetInstance(); |
| 82 DCHECK(bluetooth_manager); |
| 83 bluetooth_manager->AddObserver(this); |
| 84 |
| 85 chromeos::BluetoothAdapter* default_adapter = |
| 86 bluetooth_manager->DefaultAdapter(); |
| 87 DefaultAdapterChanged(default_adapter); |
| 63 } | 88 } |
| 64 | 89 |
| 65 void BluetoothOptionsHandler::RegisterMessages() { | 90 void BluetoothOptionsHandler::RegisterMessages() { |
| 66 DCHECK(web_ui_); | 91 DCHECK(web_ui_); |
| 67 web_ui_->RegisterMessageCallback("bluetoothEnableChange", | 92 web_ui_->RegisterMessageCallback("bluetoothEnableChange", |
| 68 base::Bind(&BluetoothOptionsHandler::EnableChangeCallback, | 93 base::Bind(&BluetoothOptionsHandler::EnableChangeCallback, |
| 69 base::Unretained(this))); | 94 base::Unretained(this))); |
| 70 web_ui_->RegisterMessageCallback("findBluetoothDevices", | 95 web_ui_->RegisterMessageCallback("findBluetoothDevices", |
| 71 base::Bind(&BluetoothOptionsHandler::FindDevicesCallback, | 96 base::Bind(&BluetoothOptionsHandler::FindDevicesCallback, |
| 72 base::Unretained(this))); | 97 base::Unretained(this))); |
| 73 web_ui_->RegisterMessageCallback("updateBluetoothDevice", | 98 web_ui_->RegisterMessageCallback("updateBluetoothDevice", |
| 74 base::Bind(&BluetoothOptionsHandler::UpdateDeviceCallback, | 99 base::Bind(&BluetoothOptionsHandler::UpdateDeviceCallback, |
| 75 base::Unretained(this))); | 100 base::Unretained(this))); |
| 76 } | 101 } |
| 77 | 102 |
| 78 void BluetoothOptionsHandler::EnableChangeCallback( | 103 void BluetoothOptionsHandler::EnableChangeCallback( |
| 79 const ListValue* args) { | 104 const ListValue* args) { |
| 80 // TODO(kevers): Call Bluetooth API to enable or disable. | 105 // TODO(kevers): Call Bluetooth API to enable or disable. |
| 81 } | 106 } |
| 82 | 107 |
| 83 void BluetoothOptionsHandler::FindDevicesCallback( | 108 void BluetoothOptionsHandler::FindDevicesCallback( |
| 84 const ListValue* args) { | 109 const ListValue* args) { |
| 85 // We only initiate a scan if we're running on Chrome OS. Otherwise, we | 110 // We only initiate a scan if we're running on Chrome OS. Otherwise, we |
| 86 // generate a fake device list. | 111 // generate a fake device list. |
| 87 if (!chromeos::system::runtime_environment::IsRunningOnChromeOS()) { | 112 if (!chromeos::system::runtime_environment::IsRunningOnChromeOS()) { |
| 88 GenerateFakeDeviceList(); | 113 GenerateFakeDeviceList(); |
| 89 return; | 114 return; |
| 90 } | 115 } |
| 91 // TODO(kevers): Fetch real Bluetooth devices. | 116 |
| 117 chromeos::BluetoothManager* bluetooth_manager = |
| 118 chromeos::BluetoothManager::GetInstance(); |
| 119 DCHECK(bluetooth_manager); |
| 120 |
| 121 chromeos::BluetoothAdapter* default_adapter = |
| 122 bluetooth_manager->DefaultAdapter(); |
| 123 |
| 124 ValidateDefaultAdapter(default_adapter); |
| 125 |
| 126 if (default_adapter == NULL) { |
| 127 VLOG(1) << "FindDevicesCallback: no default adapter"; |
| 128 return; |
| 129 } |
| 130 |
| 131 default_adapter->StartDiscovery(); |
| 92 } | 132 } |
| 93 | 133 |
| 94 void BluetoothOptionsHandler::UpdateDeviceCallback( | 134 void BluetoothOptionsHandler::UpdateDeviceCallback( |
| 95 const ListValue* args) { | 135 const ListValue* args) { |
| 96 // TODO(kevers): Trigger connect/disconnect. | 136 // TODO(kevers): Trigger connect/disconnect. |
| 97 } | 137 } |
| 98 | 138 |
| 99 void BluetoothOptionsHandler::DeviceNotification( | 139 void BluetoothOptionsHandler::DeviceNotification( |
| 100 const DictionaryValue& device) { | 140 const DictionaryValue& device) { |
| 101 web_ui_->CallJavascriptFunction( | 141 web_ui_->CallJavascriptFunction( |
| 102 "options.SystemOptions.addBluetoothDevice", device); | 142 "options.SystemOptions.addBluetoothDevice", device); |
| 103 } | 143 } |
| 104 | 144 |
| 145 void BluetoothOptionsHandler::DefaultAdapterChanged( |
| 146 chromeos::BluetoothAdapter* adapter) { |
| 147 std::string old_default_adapter_id = default_adapter_id_; |
| 148 |
| 149 if (adapter == NULL) { |
| 150 default_adapter_id_.clear(); |
| 151 VLOG(2) << "DefaultAdapterChanged: no default bluetooth adapter"; |
| 152 } else { |
| 153 default_adapter_id_ = adapter->Id(); |
| 154 VLOG(2) << "DefaultAdapterChanged: " << default_adapter_id_; |
| 155 } |
| 156 |
| 157 if (default_adapter_id_ == old_default_adapter_id) { |
| 158 return; |
| 159 } |
| 160 |
| 161 if (adapter != NULL) { |
| 162 adapter->AddObserver(this); |
| 163 } |
| 164 |
| 165 // TODO(vlaviano): Respond to adapter change. |
| 166 } |
| 167 |
| 168 void BluetoothOptionsHandler::DiscoveryStarted(const std::string& adapter_id) { |
| 169 VLOG(2) << "Discovery started on " << adapter_id; |
| 170 } |
| 171 |
| 172 void BluetoothOptionsHandler::DiscoveryEnded(const std::string& adapter_id) { |
| 173 VLOG(2) << "Discovery ended on " << adapter_id; |
| 174 web_ui_->CallJavascriptFunction( |
| 175 "options.SystemOptions.notifyBluetoothSearchComplete"); |
| 176 |
| 177 // Stop the discovery session. |
| 178 // TODO(vlaviano): We may want to expose DeviceDisappeared, remove the |
| 179 // "Find devices" button, and let the discovery session continue throughout |
| 180 // the time that the page is visible rather than just doing a single discovery |
| 181 // cycle in response to a button click. |
| 182 chromeos::BluetoothManager* bluetooth_manager = |
| 183 chromeos::BluetoothManager::GetInstance(); |
| 184 DCHECK(bluetooth_manager); |
| 185 |
| 186 chromeos::BluetoothAdapter* default_adapter = |
| 187 bluetooth_manager->DefaultAdapter(); |
| 188 |
| 189 ValidateDefaultAdapter(default_adapter); |
| 190 |
| 191 if (default_adapter == NULL) { |
| 192 VLOG(1) << "DiscoveryEnded: no default adapter"; |
| 193 return; |
| 194 } |
| 195 |
| 196 default_adapter->StopDiscovery(); |
| 197 } |
| 198 |
| 199 void BluetoothOptionsHandler::DeviceFound(const std::string& adapter_id, |
| 200 chromeos::BluetoothDevice* device) { |
| 201 VLOG(2) << "Device found on " << adapter_id; |
| 202 DCHECK(device); |
| 203 |
| 204 // TODO(vlaviano): eliminate inconsistencies between the javascript rep and |
| 205 // BluetoothDevice so that we can use BluetoothDevice::AsDictionary() here. |
| 206 DictionaryValue device_js_rep; |
| 207 device_js_rep.SetString("deviceName", device->GetName()); |
| 208 device_js_rep.SetString("deviceId", device->GetAddress()); |
| 209 device_js_rep.SetString("deviceType", device->GetIcon()); |
| 210 if (device->IsPaired()) { |
| 211 device_js_rep.SetString("deviceStatus", "bluetoothDeviceConnected"); |
| 212 } else { |
| 213 device_js_rep.SetString("deviceStatus", "bluetoothDeviceNotPaired"); |
| 214 } |
| 215 |
| 216 web_ui_->CallJavascriptFunction( |
| 217 "options.SystemOptions.addBluetoothDevice", device_js_rep); |
| 218 } |
| 219 |
| 220 void BluetoothOptionsHandler::ValidateDefaultAdapter( |
| 221 chromeos::BluetoothAdapter* adapter) { |
| 222 if ((adapter == NULL && !default_adapter_id_.empty()) || |
| 223 (adapter != NULL && default_adapter_id_ != adapter->Id())) { |
| 224 VLOG(1) << "unexpected default adapter change from \"" |
| 225 << default_adapter_id_ << "\" to \"" << adapter->Id() << "\""; |
| 226 DefaultAdapterChanged(adapter); |
| 227 } |
| 228 } |
| 229 |
| 105 void BluetoothOptionsHandler::GenerateFakeDeviceList() { | 230 void BluetoothOptionsHandler::GenerateFakeDeviceList() { |
| 106 // TODO(kevers): Send notifications asynchronously simulating that the | 231 // TODO(kevers): Send notifications asynchronously simulating that the |
| 107 // process of discovering bluetooth devices takes time. | 232 // process of discovering bluetooth devices takes time. |
| 108 // Fire each notification using OneShotTimer with a | 233 // Fire each notification using OneShotTimer with a |
| 109 // varying delay. | 234 // varying delay. |
| 110 std::string data[9] = { | 235 std::string data[9] = { |
| 111 "Fake Wireless Keyboard", "01-02-03-04-05", "keyboard", | 236 "Fake Wireless Keyboard", "01-02-03-04-05", "keyboard", |
| 112 "Fake Wireless Mouse", "02-03-04-05-01", "mouse", | 237 "Fake Wireless Mouse", "02-03-04-05-01", "mouse", |
| 113 "Fake Wireless Headset", "03-04-05-01-02", "headset"}; | 238 "Fake Wireless Headset", "03-04-05-01-02", "headset"}; |
| 114 | 239 |
| 115 for (int i = 0; i < 3; i++) { | 240 for (int i = 0; i < 3; i++) { |
| 116 DictionaryValue device; | 241 DictionaryValue device; |
| 117 device.SetString("deviceName", data[3*i]); | 242 device.SetString("deviceName", data[3*i]); |
| 118 device.SetString("deviceId", data[3*i+1]); | 243 device.SetString("deviceId", data[3*i+1]); |
| 119 device.SetString("deviceType", data[3*i+2]); | 244 device.SetString("deviceType", data[3*i+2]); |
| 120 device.SetString("deviceStatus", "bluetoothDeviceNotPaired"); | 245 device.SetString("deviceStatus", "bluetoothDeviceNotPaired"); |
| 121 web_ui_->CallJavascriptFunction( | 246 web_ui_->CallJavascriptFunction( |
| 122 "options.SystemOptions.addBluetoothDevice", device); | 247 "options.SystemOptions.addBluetoothDevice", device); |
| 123 } | 248 } |
| 124 web_ui_->CallJavascriptFunction( | 249 web_ui_->CallJavascriptFunction( |
| 125 "options.SystemOptions.notifyBluetoothSearchComplete"); | 250 "options.SystemOptions.notifyBluetoothSearchComplete"); |
| 126 } | 251 } |
| 127 | 252 |
| 128 } | 253 } // namespace chromeos |
| 129 | |
| OLD | NEW |