| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | |
| 15 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter_factory.h" | |
| 16 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" | |
| 17 #include "content/public/browser/web_ui.h" | 14 #include "content/public/browser/web_ui.h" |
| 15 #include "device/bluetooth/bluetooth_adapter.h" |
| 16 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 17 #include "device/bluetooth/bluetooth_device.h" |
| 18 #include "grit/chromium_strings.h" | 18 #include "grit/chromium_strings.h" |
| 19 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
| 20 #include "third_party/cros_system_api/dbus/service_constants.h" | 20 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 // |UpdateDeviceCallback| takes a variable length list as an argument. The | 25 // |UpdateDeviceCallback| takes a variable length list as an argument. The |
| 26 // value stored in each list element is indicated by the following constants. | 26 // value stored in each list element is indicated by the following constants. |
| 27 const int kUpdateDeviceAddressIndex = 0; | 27 const int kUpdateDeviceAddressIndex = 0; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 { "bluetoothDisconnectFailed", | 117 { "bluetoothDisconnectFailed", |
| 118 IDS_OPTIONS_SETTINGS_BLUETOOTH_DISCONNECT_FAILED }, | 118 IDS_OPTIONS_SETTINGS_BLUETOOTH_DISCONNECT_FAILED }, |
| 119 { "bluetoothForgetFailed", | 119 { "bluetoothForgetFailed", |
| 120 IDS_OPTIONS_SETTINGS_BLUETOOTH_FORGET_FAILED }}; | 120 IDS_OPTIONS_SETTINGS_BLUETOOTH_FORGET_FAILED }}; |
| 121 | 121 |
| 122 RegisterStrings(localized_strings, resources, arraysize(resources)); | 122 RegisterStrings(localized_strings, resources, arraysize(resources)); |
| 123 } | 123 } |
| 124 | 124 |
| 125 // TODO(kevers): Reorder methods to match ordering in the header file. | 125 // TODO(kevers): Reorder methods to match ordering in the header file. |
| 126 | 126 |
| 127 void BluetoothOptionsHandler::AdapterPresentChanged(BluetoothAdapter* adapter, | 127 void BluetoothOptionsHandler::AdapterPresentChanged( |
| 128 bool present) { | 128 device::BluetoothAdapter* adapter, |
| 129 bool present) { |
| 129 DCHECK(adapter == adapter_.get()); | 130 DCHECK(adapter == adapter_.get()); |
| 130 if (present) { | 131 if (present) { |
| 131 web_ui()->CallJavascriptFunction( | 132 web_ui()->CallJavascriptFunction( |
| 132 "options.BrowserOptions.showBluetoothSettings"); | 133 "options.BrowserOptions.showBluetoothSettings"); |
| 133 | 134 |
| 134 // Update the checkbox and visibility based on the powered state of the | 135 // Update the checkbox and visibility based on the powered state of the |
| 135 // new adapter. | 136 // new adapter. |
| 136 AdapterPoweredChanged(adapter_.get(), adapter_->IsPowered()); | 137 AdapterPoweredChanged(adapter_.get(), adapter_->IsPowered()); |
| 137 } else { | 138 } else { |
| 138 web_ui()->CallJavascriptFunction( | 139 web_ui()->CallJavascriptFunction( |
| 139 "options.BrowserOptions.hideBluetoothSettings"); | 140 "options.BrowserOptions.hideBluetoothSettings"); |
| 140 } | 141 } |
| 141 } | 142 } |
| 142 | 143 |
| 143 void BluetoothOptionsHandler::AdapterPoweredChanged(BluetoothAdapter* adapter, | 144 void BluetoothOptionsHandler::AdapterPoweredChanged( |
| 144 bool powered) { | 145 device::BluetoothAdapter* adapter, |
| 146 bool powered) { |
| 145 DCHECK(adapter == adapter_.get()); | 147 DCHECK(adapter == adapter_.get()); |
| 146 base::FundamentalValue checked(powered); | 148 base::FundamentalValue checked(powered); |
| 147 web_ui()->CallJavascriptFunction( | 149 web_ui()->CallJavascriptFunction( |
| 148 "options.BrowserOptions.setBluetoothState", checked); | 150 "options.BrowserOptions.setBluetoothState", checked); |
| 149 } | 151 } |
| 150 | 152 |
| 151 void BluetoothOptionsHandler::RegisterMessages() { | 153 void BluetoothOptionsHandler::RegisterMessages() { |
| 152 web_ui()->RegisterMessageCallback("bluetoothEnableChange", | 154 web_ui()->RegisterMessageCallback("bluetoothEnableChange", |
| 153 base::Bind(&BluetoothOptionsHandler::EnableChangeCallback, | 155 base::Bind(&BluetoothOptionsHandler::EnableChangeCallback, |
| 154 base::Unretained(this))); | 156 base::Unretained(this))); |
| 155 web_ui()->RegisterMessageCallback("findBluetoothDevices", | 157 web_ui()->RegisterMessageCallback("findBluetoothDevices", |
| 156 base::Bind(&BluetoothOptionsHandler::FindDevicesCallback, | 158 base::Bind(&BluetoothOptionsHandler::FindDevicesCallback, |
| 157 base::Unretained(this))); | 159 base::Unretained(this))); |
| 158 web_ui()->RegisterMessageCallback("updateBluetoothDevice", | 160 web_ui()->RegisterMessageCallback("updateBluetoothDevice", |
| 159 base::Bind(&BluetoothOptionsHandler::UpdateDeviceCallback, | 161 base::Bind(&BluetoothOptionsHandler::UpdateDeviceCallback, |
| 160 base::Unretained(this))); | 162 base::Unretained(this))); |
| 161 web_ui()->RegisterMessageCallback("stopBluetoothDeviceDiscovery", | 163 web_ui()->RegisterMessageCallback("stopBluetoothDeviceDiscovery", |
| 162 base::Bind(&BluetoothOptionsHandler::StopDiscoveryCallback, | 164 base::Bind(&BluetoothOptionsHandler::StopDiscoveryCallback, |
| 163 base::Unretained(this))); | 165 base::Unretained(this))); |
| 164 web_ui()->RegisterMessageCallback("getPairedBluetoothDevices", | 166 web_ui()->RegisterMessageCallback("getPairedBluetoothDevices", |
| 165 base::Bind(&BluetoothOptionsHandler::GetPairedDevicesCallback, | 167 base::Bind(&BluetoothOptionsHandler::GetPairedDevicesCallback, |
| 166 base::Unretained(this))); | 168 base::Unretained(this))); |
| 167 } | 169 } |
| 168 | 170 |
| 169 void BluetoothOptionsHandler::InitializeHandler() { | 171 void BluetoothOptionsHandler::InitializeHandler() { |
| 170 adapter_ = BluetoothAdapterFactory::DefaultAdapter(); | 172 adapter_ = device::BluetoothAdapterFactory::DefaultAdapter(); |
| 173 DCHECK(adapter_.get()); |
| 171 adapter_->AddObserver(this); | 174 adapter_->AddObserver(this); |
| 172 } | 175 } |
| 173 | 176 |
| 174 void BluetoothOptionsHandler::InitializePage() { | 177 void BluetoothOptionsHandler::InitializePage() { |
| 175 // Show or hide the bluetooth settings and update the checkbox based | 178 // Show or hide the bluetooth settings and update the checkbox based |
| 176 // on the current present/powered state. | 179 // on the current present/powered state. |
| 177 AdapterPresentChanged(adapter_.get(), adapter_->IsPresent()); | 180 AdapterPresentChanged(adapter_.get(), adapter_->IsPresent()); |
| 178 // Automatically start device discovery if the "Add Bluetooth Device" | 181 // Automatically start device discovery if the "Add Bluetooth Device" |
| 179 // overlay is visible. | 182 // overlay is visible. |
| 180 web_ui()->CallJavascriptFunction( | 183 web_ui()->CallJavascriptFunction( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 209 void BluetoothOptionsHandler::FindDevicesError() { | 212 void BluetoothOptionsHandler::FindDevicesError() { |
| 210 DVLOG(1) << "Failed to start discovery."; | 213 DVLOG(1) << "Failed to start discovery."; |
| 211 ReportError("bluetoothStartDiscoveryFailed", std::string()); | 214 ReportError("bluetoothStartDiscoveryFailed", std::string()); |
| 212 } | 215 } |
| 213 | 216 |
| 214 void BluetoothOptionsHandler::UpdateDeviceCallback( | 217 void BluetoothOptionsHandler::UpdateDeviceCallback( |
| 215 const ListValue* args) { | 218 const ListValue* args) { |
| 216 std::string address; | 219 std::string address; |
| 217 args->GetString(kUpdateDeviceAddressIndex, &address); | 220 args->GetString(kUpdateDeviceAddressIndex, &address); |
| 218 | 221 |
| 219 BluetoothDevice* device = adapter_->GetDevice(address); | 222 device::BluetoothDevice* device = adapter_->GetDevice(address); |
| 220 if (!device) | 223 if (!device) |
| 221 return; | 224 return; |
| 222 | 225 |
| 223 std::string command; | 226 std::string command; |
| 224 args->GetString(kUpdateDeviceCommandIndex, &command); | 227 args->GetString(kUpdateDeviceCommandIndex, &command); |
| 225 | 228 |
| 226 if (command == kConnectCommand) { | 229 if (command == kConnectCommand) { |
| 227 int size = args->GetSize(); | 230 int size = args->GetSize(); |
| 228 if (size > kUpdateDeviceAuthTokenIndex) { | 231 if (size > kUpdateDeviceAuthTokenIndex) { |
| 229 // PIN code or Passkey entry during the pairing process. | 232 // PIN code or Passkey entry during the pairing process. |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 weak_ptr_factory_.GetWeakPtr())); | 320 weak_ptr_factory_.GetWeakPtr())); |
| 318 } | 321 } |
| 319 | 322 |
| 320 void BluetoothOptionsHandler::StopDiscoveryError() { | 323 void BluetoothOptionsHandler::StopDiscoveryError() { |
| 321 DVLOG(1) << "Failed to stop discovery."; | 324 DVLOG(1) << "Failed to stop discovery."; |
| 322 ReportError("bluetoothStopDiscoveryFailed", std::string()); | 325 ReportError("bluetoothStopDiscoveryFailed", std::string()); |
| 323 } | 326 } |
| 324 | 327 |
| 325 void BluetoothOptionsHandler::GetPairedDevicesCallback( | 328 void BluetoothOptionsHandler::GetPairedDevicesCallback( |
| 326 const ListValue* args) { | 329 const ListValue* args) { |
| 327 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | 330 device::BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 328 | 331 |
| 329 for (BluetoothAdapter::DeviceList::iterator iter = devices.begin(); | 332 for (device::BluetoothAdapter::DeviceList::iterator iter = devices.begin(); |
| 330 iter != devices.end(); ++iter) | 333 iter != devices.end(); ++iter) |
| 331 SendDeviceNotification(*iter, NULL); | 334 SendDeviceNotification(*iter, NULL); |
| 332 } | 335 } |
| 333 | 336 |
| 334 void BluetoothOptionsHandler::SendDeviceNotification( | 337 void BluetoothOptionsHandler::SendDeviceNotification( |
| 335 const BluetoothDevice* device, | 338 const device::BluetoothDevice* device, |
| 336 base::DictionaryValue* params) { | 339 base::DictionaryValue* params) { |
| 337 base::DictionaryValue js_properties; | 340 base::DictionaryValue js_properties; |
| 338 js_properties.SetString("name", device->GetName()); | 341 js_properties.SetString("name", device->GetName()); |
| 339 js_properties.SetString("address", device->address()); | 342 js_properties.SetString("address", device->address()); |
| 340 js_properties.SetBoolean("paired", device->IsPaired()); | 343 js_properties.SetBoolean("paired", device->IsPaired()); |
| 341 js_properties.SetBoolean("bonded", device->IsBonded()); | 344 js_properties.SetBoolean("bonded", device->IsBonded()); |
| 342 js_properties.SetBoolean("connected", device->IsConnected()); | 345 js_properties.SetBoolean("connected", device->IsConnected()); |
| 343 if (params) { | 346 if (params) { |
| 344 js_properties.MergeDictionary(params); | 347 js_properties.MergeDictionary(params); |
| 345 } | 348 } |
| 346 web_ui()->CallJavascriptFunction( | 349 web_ui()->CallJavascriptFunction( |
| 347 "options.BrowserOptions.addBluetoothDevice", | 350 "options.BrowserOptions.addBluetoothDevice", |
| 348 js_properties); | 351 js_properties); |
| 349 } | 352 } |
| 350 | 353 |
| 351 void BluetoothOptionsHandler::RequestPinCode(BluetoothDevice* device) { | 354 void BluetoothOptionsHandler::RequestPinCode(device::BluetoothDevice* device) { |
| 352 DictionaryValue params; | 355 DictionaryValue params; |
| 353 params.SetString("pairing", kEnterPinCode); | 356 params.SetString("pairing", kEnterPinCode); |
| 354 SendDeviceNotification(device, ¶ms); | 357 SendDeviceNotification(device, ¶ms); |
| 355 } | 358 } |
| 356 | 359 |
| 357 void BluetoothOptionsHandler::RequestPasskey(BluetoothDevice* device) { | 360 void BluetoothOptionsHandler::RequestPasskey(device::BluetoothDevice* device) { |
| 358 DictionaryValue params; | 361 DictionaryValue params; |
| 359 params.SetString("pairing", kEnterPasskey); | 362 params.SetString("pairing", kEnterPasskey); |
| 360 SendDeviceNotification(device, ¶ms); | 363 SendDeviceNotification(device, ¶ms); |
| 361 } | 364 } |
| 362 | 365 |
| 363 void BluetoothOptionsHandler::DisplayPinCode(BluetoothDevice* device, | 366 void BluetoothOptionsHandler::DisplayPinCode(device::BluetoothDevice* device, |
| 364 const std::string& pincode) { | 367 const std::string& pincode) { |
| 365 DictionaryValue params; | 368 DictionaryValue params; |
| 366 params.SetString("pairing", kRemotePinCode); | 369 params.SetString("pairing", kRemotePinCode); |
| 367 params.SetString("pincode", pincode); | 370 params.SetString("pincode", pincode); |
| 368 SendDeviceNotification(device, ¶ms); | 371 SendDeviceNotification(device, ¶ms); |
| 369 } | 372 } |
| 370 | 373 |
| 371 void BluetoothOptionsHandler::DisplayPasskey(BluetoothDevice* device, | 374 void BluetoothOptionsHandler::DisplayPasskey(device::BluetoothDevice* device, |
| 372 uint32 passkey) { | 375 uint32 passkey) { |
| 373 DictionaryValue params; | 376 DictionaryValue params; |
| 374 params.SetString("pairing", kRemotePasskey); | 377 params.SetString("pairing", kRemotePasskey); |
| 375 params.SetInteger("passkey", passkey); | 378 params.SetInteger("passkey", passkey); |
| 376 SendDeviceNotification(device, ¶ms); | 379 SendDeviceNotification(device, ¶ms); |
| 377 } | 380 } |
| 378 | 381 |
| 379 void BluetoothOptionsHandler::ConfirmPasskey(BluetoothDevice* device, | 382 void BluetoothOptionsHandler::ConfirmPasskey(device::BluetoothDevice* device, |
| 380 uint32 passkey) { | 383 uint32 passkey) { |
| 381 DictionaryValue params; | 384 DictionaryValue params; |
| 382 params.SetString("pairing", kConfirmPasskey); | 385 params.SetString("pairing", kConfirmPasskey); |
| 383 params.SetInteger("passkey", passkey); | 386 params.SetInteger("passkey", passkey); |
| 384 SendDeviceNotification(device, ¶ms); | 387 SendDeviceNotification(device, ¶ms); |
| 385 } | 388 } |
| 386 | 389 |
| 387 void BluetoothOptionsHandler::DismissDisplayOrConfirm() { | 390 void BluetoothOptionsHandler::DismissDisplayOrConfirm() { |
| 388 web_ui()->CallJavascriptFunction( | 391 web_ui()->CallJavascriptFunction( |
| 389 "options.BluetoothPairing.dismissDialog"); | 392 "options.BluetoothPairing.dismissDialog"); |
| 390 } | 393 } |
| 391 | 394 |
| 392 void BluetoothOptionsHandler::ReportError( | 395 void BluetoothOptionsHandler::ReportError( |
| 393 const std::string& error, | 396 const std::string& error, |
| 394 const std::string& address) { | 397 const std::string& address) { |
| 395 base::DictionaryValue properties; | 398 base::DictionaryValue properties; |
| 396 properties.SetString("label", error); | 399 properties.SetString("label", error); |
| 397 properties.SetString("address", address); | 400 properties.SetString("address", address); |
| 398 web_ui()->CallJavascriptFunction( | 401 web_ui()->CallJavascriptFunction( |
| 399 "options.BluetoothPairing.showMessage", | 402 "options.BluetoothPairing.showMessage", |
| 400 properties); | 403 properties); |
| 401 } | 404 } |
| 402 | 405 |
| 403 void BluetoothOptionsHandler::DeviceAdded(BluetoothAdapter* adapter, | 406 void BluetoothOptionsHandler::DeviceAdded(device::BluetoothAdapter* adapter, |
| 404 BluetoothDevice* device) { | 407 device::BluetoothDevice* device) { |
| 405 DCHECK(adapter == adapter_.get()); | 408 DCHECK(adapter == adapter_.get()); |
| 406 DCHECK(device); | 409 DCHECK(device); |
| 407 SendDeviceNotification(device, NULL); | 410 SendDeviceNotification(device, NULL); |
| 408 } | 411 } |
| 409 | 412 |
| 410 void BluetoothOptionsHandler::DeviceChanged(BluetoothAdapter* adapter, | 413 void BluetoothOptionsHandler::DeviceChanged(device::BluetoothAdapter* adapter, |
| 411 BluetoothDevice* device) { | 414 device::BluetoothDevice* device) { |
| 412 DCHECK(adapter == adapter_.get()); | 415 DCHECK(adapter == adapter_.get()); |
| 413 DCHECK(device); | 416 DCHECK(device); |
| 414 SendDeviceNotification(device, NULL); | 417 SendDeviceNotification(device, NULL); |
| 415 } | 418 } |
| 416 | 419 |
| 417 void BluetoothOptionsHandler::DeviceRemoved(BluetoothAdapter* adapter, | 420 void BluetoothOptionsHandler::DeviceRemoved(device::BluetoothAdapter* adapter, |
| 418 BluetoothDevice* device) { | 421 device::BluetoothDevice* device) { |
| 419 DCHECK(adapter == adapter_.get()); | 422 DCHECK(adapter == adapter_.get()); |
| 420 DCHECK(device); | 423 DCHECK(device); |
| 421 | 424 |
| 422 base::StringValue address(device->address()); | 425 base::StringValue address(device->address()); |
| 423 web_ui()->CallJavascriptFunction( | 426 web_ui()->CallJavascriptFunction( |
| 424 "options.BrowserOptions.removeBluetoothDevice", | 427 "options.BrowserOptions.removeBluetoothDevice", |
| 425 address); | 428 address); |
| 426 } | 429 } |
| 427 | 430 |
| 428 } // namespace options | 431 } // namespace options |
| 429 } // namespace chromeos | 432 } // namespace chromeos |
| OLD | NEW |