| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #include "chrome/browser/ui/webui/options2/chromeos/bluetooth_options_handler.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/callback.h" | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/string_number_conversions.h" | |
| 12 #include "base/utf_string_conversions.h" | |
| 13 #include "base/values.h" | |
| 14 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | |
| 15 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" | |
| 16 #include "content/public/browser/web_ui.h" | |
| 17 #include "grit/chromium_strings.h" | |
| 18 #include "grit/generated_resources.h" | |
| 19 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 20 #include "ui/base/l10n/l10n_util.h" | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // |UpdateDeviceCallback| takes a variable length list as an argument. The | |
| 25 // value stored in each list element is indicated by the following constants. | |
| 26 const int kUpdateDeviceAddressIndex = 0; | |
| 27 const int kUpdateDeviceCommandIndex = 1; | |
| 28 const int kUpdateDeviceAuthTokenIndex = 2; | |
| 29 | |
| 30 // |UpdateDeviceCallback| provides a command value of one of the following | |
| 31 // constants that indicates what update it is providing to us. | |
| 32 const char kConnectCommand[] = "connect"; | |
| 33 const char kCancelCommand[] = "cancel"; | |
| 34 const char kAcceptCommand[] = "accept"; | |
| 35 const char kRejectCommand[] = "reject"; | |
| 36 const char kDisconnectCommand[] = "disconnect"; | |
| 37 const char kForgetCommand[] = "forget"; | |
| 38 | |
| 39 // |SendDeviceNotification| may include a pairing parameter whose value | |
| 40 // is one of the following constants instructing the UI to perform a certain | |
| 41 // action. | |
| 42 const char kEnterPinCode[] = "bluetoothEnterPinCode"; | |
| 43 const char kEnterPasskey[] = "bluetoothEnterPasskey"; | |
| 44 const char kRemotePinCode[] = "bluetoothRemotePinCode"; | |
| 45 const char kRemotePasskey[] = "bluetoothRemotePasskey"; | |
| 46 const char kConfirmPasskey[] = "bluetoothConfirmPasskey"; | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 namespace chromeos { | |
| 51 namespace options { | |
| 52 | |
| 53 BluetoothOptionsHandler::BluetoothOptionsHandler() : weak_ptr_factory_(this) { | |
| 54 } | |
| 55 | |
| 56 BluetoothOptionsHandler::~BluetoothOptionsHandler() { | |
| 57 if (adapter_.get()) | |
| 58 adapter_->RemoveObserver(this); | |
| 59 } | |
| 60 | |
| 61 void BluetoothOptionsHandler::GetLocalizedValues( | |
| 62 DictionaryValue* localized_strings) { | |
| 63 DCHECK(localized_strings); | |
| 64 | |
| 65 static OptionsStringResource resources[] = { | |
| 66 { "bluetooth", IDS_OPTIONS_SETTINGS_SECTION_TITLE_BLUETOOTH }, | |
| 67 { "disableBluetooth", IDS_OPTIONS_SETTINGS_BLUETOOTH_DISABLE }, | |
| 68 { "enableBluetooth", IDS_OPTIONS_SETTINGS_BLUETOOTH_ENABLE }, | |
| 69 { "addBluetoothDevice", IDS_OPTIONS_SETTINGS_ADD_BLUETOOTH_DEVICE }, | |
| 70 { "bluetoothAddDeviceTitle", | |
| 71 IDS_OPTIONS_SETTINGS_BLUETOOTH_ADD_DEVICE_TITLE }, | |
| 72 { "bluetoothOptionsPageTabTitle", | |
| 73 IDS_OPTIONS_SETTINGS_BLUETOOTH_ADD_DEVICE_TITLE }, | |
| 74 { "findBluetoothDevices", IDS_OPTIONS_SETTINGS_FIND_BLUETOOTH_DEVICES }, | |
| 75 { "bluetoothNoDevices", IDS_OPTIONS_SETTINGS_BLUETOOTH_NO_DEVICES }, | |
| 76 { "bluetoothNoDevicesFound", | |
| 77 IDS_OPTIONS_SETTINGS_BLUETOOTH_NO_DEVICES_FOUND }, | |
| 78 {"bluetoothScanning", IDS_OPTIONS_SETTINGS_BLUETOOTH_SCANNING }, | |
| 79 {"bluetoothDeviceConnected", IDS_OPTIONS_SETTINGS_BLUETOOTH_CONNECTED }, | |
| 80 { "bluetoothDeviceNotConnected", | |
| 81 IDS_OPTIONS_SETTINGS_BLUETOOTH_NOT_CONNECTED }, | |
| 82 { "bluetoothConnectDevice", IDS_OPTIONS_SETTINGS_BLUETOOTH_CONNECT }, | |
| 83 { "bluetoothDisconnectDevice", IDS_OPTIONS_SETTINGS_BLUETOOTH_DISCONNECT }, | |
| 84 { "bluetoothForgetDevice", IDS_OPTIONS_SETTINGS_BLUETOOTH_FORGET }, | |
| 85 { "bluetoothCancel", IDS_OPTIONS_SETTINGS_BLUETOOTH_CANCEL }, | |
| 86 { "bluetoothEnterKey", IDS_OPTIONS_SETTINGS_BLUETOOTH_ENTER_KEY }, | |
| 87 { "bluetoothDismissError", IDS_OPTIONS_SETTINGS_BLUETOOTH_DISMISS_ERROR }, | |
| 88 | |
| 89 // Device connecting and pairing. | |
| 90 { "bluetoothStartConnecting", | |
| 91 IDS_OPTIONS_SETTINGS_BLUETOOTH_START_CONNECTING }, | |
| 92 { "bluetoothAcceptPasskey", | |
| 93 IDS_OPTIONS_SETTINGS_BLUETOOTH_ACCEPT_PASSKEY }, | |
| 94 { "bluetoothRejectPasskey", | |
| 95 IDS_OPTIONS_SETTINGS_BLUETOOTH_REJECT_PASSKEY }, | |
| 96 { "bluetoothEnterPinCode", | |
| 97 IDS_OPTIONS_SETTINGS_BLUETOOTH_ENTER_PIN_CODE_REQUEST }, | |
| 98 { "bluetoothEnterPasskey", | |
| 99 IDS_OPTIONS_SETTINGS_BLUETOOTH_ENTER_PASSKEY_REQUEST }, | |
| 100 { "bluetoothRemotePinCode", | |
| 101 IDS_OPTIONS_SETTINGS_BLUETOOTH_REMOTE_PIN_CODE_REQUEST }, | |
| 102 { "bluetoothRemotePasskey", | |
| 103 IDS_OPTIONS_SETTINGS_BLUETOOTH_REMOTE_PASSKEY_REQUEST }, | |
| 104 { "bluetoothConfirmPasskey", | |
| 105 IDS_OPTIONS_SETTINGS_BLUETOOTH_CONFIRM_PASSKEY_REQUEST }, | |
| 106 | |
| 107 // Error messages. | |
| 108 { "bluetoothStartDiscoveryFailed", | |
| 109 IDS_OPTIONS_SETTINGS_BLUETOOTH_START_DISCOVERY_FAILED }, | |
| 110 { "bluetoothStopDiscoveryFailed", | |
| 111 IDS_OPTIONS_SETTINGS_BLUETOOTH_STOP_DISCOVERY_FAILED }, | |
| 112 { "bluetoothChangePowerFailed", | |
| 113 IDS_OPTIONS_SETTINGS_BLUETOOTH_CHANGE_POWER_FAILED }, | |
| 114 { "bluetoothConnectFailed", | |
| 115 IDS_OPTIONS_SETTINGS_BLUETOOTH_CONNECT_FAILED }, | |
| 116 { "bluetoothDisconnectFailed", | |
| 117 IDS_OPTIONS_SETTINGS_BLUETOOTH_DISCONNECT_FAILED }, | |
| 118 { "bluetoothForgetFailed", | |
| 119 IDS_OPTIONS_SETTINGS_BLUETOOTH_FORGET_FAILED }}; | |
| 120 | |
| 121 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
| 122 } | |
| 123 | |
| 124 // TODO(kevers): Reorder methods to match ordering in the header file. | |
| 125 | |
| 126 void BluetoothOptionsHandler::AdapterPresentChanged(BluetoothAdapter* adapter, | |
| 127 bool present) { | |
| 128 DCHECK(adapter == adapter_.get()); | |
| 129 if (present) { | |
| 130 web_ui()->CallJavascriptFunction( | |
| 131 "options.BrowserOptions.showBluetoothSettings"); | |
| 132 | |
| 133 // Update the checkbox and visibility based on the powered state of the | |
| 134 // new adapter. | |
| 135 AdapterPoweredChanged(adapter_.get(), adapter_->IsPowered()); | |
| 136 } else { | |
| 137 web_ui()->CallJavascriptFunction( | |
| 138 "options.BrowserOptions.hideBluetoothSettings"); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void BluetoothOptionsHandler::AdapterPoweredChanged(BluetoothAdapter* adapter, | |
| 143 bool powered) { | |
| 144 DCHECK(adapter == adapter_.get()); | |
| 145 base::FundamentalValue checked(powered); | |
| 146 web_ui()->CallJavascriptFunction( | |
| 147 "options.BrowserOptions.setBluetoothState", checked); | |
| 148 } | |
| 149 | |
| 150 void BluetoothOptionsHandler::RegisterMessages() { | |
| 151 web_ui()->RegisterMessageCallback("bluetoothEnableChange", | |
| 152 base::Bind(&BluetoothOptionsHandler::EnableChangeCallback, | |
| 153 base::Unretained(this))); | |
| 154 web_ui()->RegisterMessageCallback("findBluetoothDevices", | |
| 155 base::Bind(&BluetoothOptionsHandler::FindDevicesCallback, | |
| 156 base::Unretained(this))); | |
| 157 web_ui()->RegisterMessageCallback("updateBluetoothDevice", | |
| 158 base::Bind(&BluetoothOptionsHandler::UpdateDeviceCallback, | |
| 159 base::Unretained(this))); | |
| 160 web_ui()->RegisterMessageCallback("stopBluetoothDeviceDiscovery", | |
| 161 base::Bind(&BluetoothOptionsHandler::StopDiscoveryCallback, | |
| 162 base::Unretained(this))); | |
| 163 web_ui()->RegisterMessageCallback("getPairedBluetoothDevices", | |
| 164 base::Bind(&BluetoothOptionsHandler::GetPairedDevicesCallback, | |
| 165 base::Unretained(this))); | |
| 166 } | |
| 167 | |
| 168 void BluetoothOptionsHandler::InitializeHandler() { | |
| 169 adapter_ = BluetoothAdapter::DefaultAdapter(); | |
| 170 adapter_->AddObserver(this); | |
| 171 } | |
| 172 | |
| 173 void BluetoothOptionsHandler::InitializePage() { | |
| 174 // Show or hide the bluetooth settings and update the checkbox based | |
| 175 // on the current present/powered state. | |
| 176 AdapterPresentChanged(adapter_.get(), adapter_->IsPresent()); | |
| 177 // Automatically start device discovery if the "Add Bluetooth Device" | |
| 178 // overlay is visible. | |
| 179 web_ui()->CallJavascriptFunction( | |
| 180 "options.BluetoothOptions.updateDiscovery"); | |
| 181 } | |
| 182 | |
| 183 void BluetoothOptionsHandler::EnableChangeCallback( | |
| 184 const ListValue* args) { | |
| 185 bool bluetooth_enabled; | |
| 186 args->GetBoolean(0, &bluetooth_enabled); | |
| 187 | |
| 188 adapter_->SetPowered(bluetooth_enabled, | |
| 189 base::Bind(&base::DoNothing), | |
| 190 base::Bind(&BluetoothOptionsHandler::EnableChangeError, | |
| 191 weak_ptr_factory_.GetWeakPtr())); | |
| 192 } | |
| 193 | |
| 194 void BluetoothOptionsHandler::EnableChangeError() { | |
| 195 DVLOG(1) << "Failed to change power state."; | |
| 196 ReportError("bluetoothChangePowerFailed", std::string()); | |
| 197 } | |
| 198 | |
| 199 void BluetoothOptionsHandler::FindDevicesCallback( | |
| 200 const ListValue* args) { | |
| 201 adapter_->SetDiscovering( | |
| 202 true, | |
| 203 base::Bind(&base::DoNothing), | |
| 204 base::Bind(&BluetoothOptionsHandler::FindDevicesError, | |
| 205 weak_ptr_factory_.GetWeakPtr())); | |
| 206 } | |
| 207 | |
| 208 void BluetoothOptionsHandler::FindDevicesError() { | |
| 209 DVLOG(1) << "Failed to start discovery."; | |
| 210 ReportError("bluetoothStartDiscoveryFailed", std::string()); | |
| 211 } | |
| 212 | |
| 213 void BluetoothOptionsHandler::UpdateDeviceCallback( | |
| 214 const ListValue* args) { | |
| 215 std::string address; | |
| 216 args->GetString(kUpdateDeviceAddressIndex, &address); | |
| 217 | |
| 218 BluetoothDevice* device = adapter_->GetDevice(address); | |
| 219 if (!device) | |
| 220 return; | |
| 221 | |
| 222 std::string command; | |
| 223 args->GetString(kUpdateDeviceCommandIndex, &command); | |
| 224 | |
| 225 if (command == kConnectCommand) { | |
| 226 int size = args->GetSize(); | |
| 227 if (size > kUpdateDeviceAuthTokenIndex) { | |
| 228 // PIN code or Passkey entry during the pairing process. | |
| 229 std::string auth_token; | |
| 230 args->GetString(kUpdateDeviceAuthTokenIndex, &auth_token); | |
| 231 | |
| 232 if (device->ExpectingPinCode()) { | |
| 233 // PIN Code is an array of 1 to 16 8-bit bytes, the usual | |
| 234 // interpretation, and the one shared by BlueZ, is a UTF-8 string | |
| 235 // of as many characters that will fit in that space, thus we | |
| 236 // can use the auth token from JavaScript unmodified. | |
| 237 DVLOG(1) << "PIN Code supplied: " << address << ": " << auth_token; | |
| 238 device->SetPinCode(auth_token); | |
| 239 } else if (device->ExpectingPasskey()) { | |
| 240 // Passkey is a numeric in the range 0-999999, in this case the | |
| 241 // JavaScript code should have ensured the auth token string only | |
| 242 // contains digits so a simple conversion is sufficient. In the | |
| 243 // failure case, just use 0 since that's the most likely Passkey | |
| 244 // anyway, and if it's refused the device will request a new one. | |
| 245 unsigned passkey = 0; | |
| 246 base::StringToUint(auth_token, &passkey); | |
| 247 | |
| 248 DVLOG(1) << "Passkey supplied: " << address << ": " << passkey; | |
| 249 device->SetPasskey(passkey); | |
| 250 } else { | |
| 251 LOG(WARNING) << "Auth token supplied after pairing ended: " << address | |
| 252 << ": " << auth_token; | |
| 253 } | |
| 254 } else { | |
| 255 // Connection request. | |
| 256 DVLOG(1) << "Connect: " << address; | |
| 257 device->Connect( | |
| 258 this, | |
| 259 base::Bind(&base::DoNothing), | |
| 260 base::Bind(&BluetoothOptionsHandler::ConnectError, | |
| 261 weak_ptr_factory_.GetWeakPtr(), | |
| 262 device->address())); | |
| 263 } | |
| 264 } else if (command == kCancelCommand) { | |
| 265 // Cancel pairing. | |
| 266 DVLOG(1) << "Cancel pairing: " << address; | |
| 267 device->CancelPairing(); | |
| 268 } else if (command == kAcceptCommand) { | |
| 269 // Confirm displayed Passkey. | |
| 270 DVLOG(1) << "Confirm pairing: " << address; | |
| 271 device->ConfirmPairing(); | |
| 272 } else if (command == kRejectCommand) { | |
| 273 // Reject displayed Passkey. | |
| 274 DVLOG(1) << "Reject pairing: " << address; | |
| 275 device->RejectPairing(); | |
| 276 } else if (command == kDisconnectCommand) { | |
| 277 // Disconnect from device. | |
| 278 DVLOG(1) << "Disconnect device: " << address; | |
| 279 device->Disconnect( | |
| 280 base::Bind(&base::DoNothing), | |
| 281 base::Bind(&BluetoothOptionsHandler::DisconnectError, | |
| 282 weak_ptr_factory_.GetWeakPtr(), | |
| 283 device->address())); | |
| 284 } else if (command == kForgetCommand) { | |
| 285 // Disconnect from device and delete pairing information. | |
| 286 DVLOG(1) << "Forget device: " << address; | |
| 287 device->Forget(base::Bind(&BluetoothOptionsHandler::ForgetError, | |
| 288 weak_ptr_factory_.GetWeakPtr(), | |
| 289 device->address())); | |
| 290 } else { | |
| 291 LOG(WARNING) << "Unknown updateBluetoothDevice command: " << command; | |
| 292 } | |
| 293 } | |
| 294 | |
| 295 void BluetoothOptionsHandler::ConnectError(const std::string& address) { | |
| 296 DVLOG(1) << "Failed to connect to device: " << address; | |
| 297 ReportError("bluetoothConnectFailed", address); | |
| 298 } | |
| 299 | |
| 300 void BluetoothOptionsHandler::DisconnectError(const std::string& address) { | |
| 301 DVLOG(1) << "Failed to disconnect from device: " << address; | |
| 302 ReportError("bluetoothDisconnectFailed", address); | |
| 303 } | |
| 304 | |
| 305 void BluetoothOptionsHandler::ForgetError(const std::string& address) { | |
| 306 DVLOG(1) << "Failed to disconnect and unpair device: " << address; | |
| 307 ReportError("bluetoothForgetFailed", address); | |
| 308 } | |
| 309 | |
| 310 void BluetoothOptionsHandler::StopDiscoveryCallback( | |
| 311 const ListValue* args) { | |
| 312 adapter_->SetDiscovering( | |
| 313 false, | |
| 314 base::Bind(&base::DoNothing), | |
| 315 base::Bind(&BluetoothOptionsHandler::StopDiscoveryError, | |
| 316 weak_ptr_factory_.GetWeakPtr())); | |
| 317 } | |
| 318 | |
| 319 void BluetoothOptionsHandler::StopDiscoveryError() { | |
| 320 DVLOG(1) << "Failed to stop discovery."; | |
| 321 ReportError("bluetoothStopDiscoveryFailed", std::string()); | |
| 322 } | |
| 323 | |
| 324 void BluetoothOptionsHandler::GetPairedDevicesCallback( | |
| 325 const ListValue* args) { | |
| 326 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | |
| 327 | |
| 328 for (BluetoothAdapter::DeviceList::iterator iter = devices.begin(); | |
| 329 iter != devices.end(); ++iter) | |
| 330 SendDeviceNotification(*iter, NULL); | |
| 331 } | |
| 332 | |
| 333 void BluetoothOptionsHandler::SendDeviceNotification( | |
| 334 const BluetoothDevice* device, | |
| 335 base::DictionaryValue* params) { | |
| 336 base::DictionaryValue js_properties; | |
| 337 js_properties.SetString("name", device->GetName()); | |
| 338 js_properties.SetString("address", device->address()); | |
| 339 js_properties.SetBoolean("paired", device->IsPaired()); | |
| 340 js_properties.SetBoolean("bonded", device->IsBonded()); | |
| 341 js_properties.SetBoolean("connected", device->IsConnected()); | |
| 342 if (params) { | |
| 343 js_properties.MergeDictionary(params); | |
| 344 } | |
| 345 web_ui()->CallJavascriptFunction( | |
| 346 "options.BrowserOptions.addBluetoothDevice", | |
| 347 js_properties); | |
| 348 } | |
| 349 | |
| 350 void BluetoothOptionsHandler::RequestPinCode(BluetoothDevice* device) { | |
| 351 DictionaryValue params; | |
| 352 params.SetString("pairing", kEnterPinCode); | |
| 353 SendDeviceNotification(device, ¶ms); | |
| 354 } | |
| 355 | |
| 356 void BluetoothOptionsHandler::RequestPasskey(BluetoothDevice* device) { | |
| 357 DictionaryValue params; | |
| 358 params.SetString("pairing", kEnterPasskey); | |
| 359 SendDeviceNotification(device, ¶ms); | |
| 360 } | |
| 361 | |
| 362 void BluetoothOptionsHandler::DisplayPinCode(BluetoothDevice* device, | |
| 363 const std::string& pincode) { | |
| 364 DictionaryValue params; | |
| 365 params.SetString("pairing", kRemotePinCode); | |
| 366 params.SetString("pincode", pincode); | |
| 367 SendDeviceNotification(device, ¶ms); | |
| 368 } | |
| 369 | |
| 370 void BluetoothOptionsHandler::DisplayPasskey(BluetoothDevice* device, | |
| 371 uint32 passkey) { | |
| 372 DictionaryValue params; | |
| 373 params.SetString("pairing", kRemotePasskey); | |
| 374 params.SetInteger("passkey", passkey); | |
| 375 SendDeviceNotification(device, ¶ms); | |
| 376 } | |
| 377 | |
| 378 void BluetoothOptionsHandler::ConfirmPasskey(BluetoothDevice* device, | |
| 379 uint32 passkey) { | |
| 380 DictionaryValue params; | |
| 381 params.SetString("pairing", kConfirmPasskey); | |
| 382 params.SetInteger("passkey", passkey); | |
| 383 SendDeviceNotification(device, ¶ms); | |
| 384 } | |
| 385 | |
| 386 void BluetoothOptionsHandler::DismissDisplayOrConfirm() { | |
| 387 web_ui()->CallJavascriptFunction( | |
| 388 "options.BluetoothPairing.dismissDialog"); | |
| 389 } | |
| 390 | |
| 391 void BluetoothOptionsHandler::ReportError( | |
| 392 const std::string& error, | |
| 393 const std::string& address) { | |
| 394 base::DictionaryValue properties; | |
| 395 properties.SetString("label", error); | |
| 396 properties.SetString("address", address); | |
| 397 web_ui()->CallJavascriptFunction( | |
| 398 "options.BluetoothPairing.showMessage", | |
| 399 properties); | |
| 400 } | |
| 401 | |
| 402 void BluetoothOptionsHandler::DeviceAdded(BluetoothAdapter* adapter, | |
| 403 BluetoothDevice* device) { | |
| 404 DCHECK(adapter == adapter_.get()); | |
| 405 DCHECK(device); | |
| 406 SendDeviceNotification(device, NULL); | |
| 407 } | |
| 408 | |
| 409 void BluetoothOptionsHandler::DeviceChanged(BluetoothAdapter* adapter, | |
| 410 BluetoothDevice* device) { | |
| 411 DCHECK(adapter == adapter_.get()); | |
| 412 DCHECK(device); | |
| 413 SendDeviceNotification(device, NULL); | |
| 414 } | |
| 415 | |
| 416 void BluetoothOptionsHandler::DeviceRemoved(BluetoothAdapter* adapter, | |
| 417 BluetoothDevice* device) { | |
| 418 DCHECK(adapter == adapter_.get()); | |
| 419 DCHECK(device); | |
| 420 | |
| 421 base::StringValue address(device->address()); | |
| 422 web_ui()->CallJavascriptFunction( | |
| 423 "options.BrowserOptions.removeBluetoothDevice", | |
| 424 address); | |
| 425 } | |
| 426 | |
| 427 } // namespace options | |
| 428 } // namespace chromeos | |
| OLD | NEW |