| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/pairing/bluetooth_host_pairing_controller.h" | 5 #include "components/pairing/bluetooth_host_pairing_controller.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/hash.h" |
| 8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 9 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 10 #include "components/pairing/bluetooth_pairing_constants.h" | 11 #include "components/pairing/bluetooth_pairing_constants.h" |
| 11 #include "components/pairing/pairing_api.pb.h" | 12 #include "components/pairing/pairing_api.pb.h" |
| 12 #include "components/pairing/proto_decoder.h" | 13 #include "components/pairing/proto_decoder.h" |
| 13 #include "device/bluetooth/bluetooth_adapter_factory.h" | 14 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 14 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 const int kReceiveSize = 16384; | 18 const int kReceiveSize = 16384; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 | 112 |
| 112 if (adapter_->IsPresent()) { | 113 if (adapter_->IsPresent()) { |
| 113 SetName(); | 114 SetName(); |
| 114 } else { | 115 } else { |
| 115 // Set the name once the adapter is present. | 116 // Set the name once the adapter is present. |
| 116 adapter_->AddObserver(this); | 117 adapter_->AddObserver(this); |
| 117 } | 118 } |
| 118 } | 119 } |
| 119 | 120 |
| 120 void BluetoothHostPairingController::SetName() { | 121 void BluetoothHostPairingController::SetName() { |
| 121 // TODO(zork): Make the device name prettier. (http://crbug.com/405774) | 122 // Hash the bluetooth address and take the lower 2 bytes to create a human |
| 122 device_name_ = base::StringPrintf("%s%s", kDeviceNamePrefix, | 123 // readable device name. |
| 123 adapter_->GetAddress().c_str()); | 124 const uint32 device_id = base::Hash(adapter_->GetAddress()) & 0xFFFF; |
| 125 device_name_ = base::StringPrintf("%s%04X", kDeviceNamePrefix, device_id); |
| 124 | 126 |
| 125 adapter_->SetName( | 127 adapter_->SetName( |
| 126 device_name_, | 128 device_name_, |
| 127 base::Bind(&BluetoothHostPairingController::OnSetName, | 129 base::Bind(&BluetoothHostPairingController::OnSetName, |
| 128 ptr_factory_.GetWeakPtr()), | 130 ptr_factory_.GetWeakPtr()), |
| 129 base::Bind(&BluetoothHostPairingController::OnSetError, | 131 base::Bind(&BluetoothHostPairingController::OnSetError, |
| 130 ptr_factory_.GetWeakPtr())); | 132 ptr_factory_.GetWeakPtr())); |
| 131 } | 133 } |
| 132 | 134 |
| 133 void BluetoothHostPairingController::OnSetName() { | 135 void BluetoothHostPairingController::OnSetName() { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 kReceiveSize, | 227 kReceiveSize, |
| 226 base::Bind(&BluetoothHostPairingController::OnReceiveComplete, | 228 base::Bind(&BluetoothHostPairingController::OnReceiveComplete, |
| 227 ptr_factory_.GetWeakPtr()), | 229 ptr_factory_.GetWeakPtr()), |
| 228 base::Bind(&BluetoothHostPairingController::OnReceiveError, | 230 base::Bind(&BluetoothHostPairingController::OnReceiveError, |
| 229 ptr_factory_.GetWeakPtr())); | 231 ptr_factory_.GetWeakPtr())); |
| 230 } | 232 } |
| 231 | 233 |
| 232 void BluetoothHostPairingController::OnCreateServiceError( | 234 void BluetoothHostPairingController::OnCreateServiceError( |
| 233 const std::string& message) { | 235 const std::string& message) { |
| 234 LOG(ERROR) << message; | 236 LOG(ERROR) << message; |
| 235 // TODO(zork): Add a stage for initialization error. (http://crbug.com/405744) | 237 ChangeStage(STAGE_INITIALIZATION_ERROR); |
| 236 ChangeStage(STAGE_NONE); | |
| 237 } | 238 } |
| 238 | 239 |
| 239 void BluetoothHostPairingController::OnSetError() { | 240 void BluetoothHostPairingController::OnSetError() { |
| 240 adapter_->RemovePairingDelegate(this); | 241 adapter_->RemovePairingDelegate(this); |
| 241 // TODO(zork): Add a stage for initialization error. (http://crbug.com/405744) | 242 ChangeStage(STAGE_INITIALIZATION_ERROR); |
| 242 ChangeStage(STAGE_NONE); | |
| 243 } | 243 } |
| 244 | 244 |
| 245 void BluetoothHostPairingController::OnAcceptError( | 245 void BluetoothHostPairingController::OnAcceptError( |
| 246 const std::string& error_message) { | 246 const std::string& error_message) { |
| 247 LOG(ERROR) << error_message; | 247 LOG(ERROR) << error_message; |
| 248 Reset(); | 248 Reset(); |
| 249 } | 249 } |
| 250 | 250 |
| 251 void BluetoothHostPairingController::OnSendError( | 251 void BluetoothHostPairingController::OnSendError( |
| 252 const std::string& error_message) { | 252 const std::string& error_message) { |
| 253 LOG(ERROR) << error_message; | 253 LOG(ERROR) << error_message; |
| 254 } | 254 } |
| 255 | 255 |
| 256 void BluetoothHostPairingController::OnReceiveError( | 256 void BluetoothHostPairingController::OnReceiveError( |
| 257 device::BluetoothSocket::ErrorReason reason, | 257 device::BluetoothSocket::ErrorReason reason, |
| 258 const std::string& error_message) { | 258 const std::string& error_message) { |
| 259 LOG(ERROR) << reason << ", " << error_message; | 259 LOG(ERROR) << reason << ", " << error_message; |
| 260 Reset(); | 260 Reset(); |
| 261 } | 261 } |
| 262 | 262 |
| 263 void BluetoothHostPairingController::OnHostStatusMessage( | 263 void BluetoothHostPairingController::OnHostStatusMessage( |
| 264 const pairing_api::HostStatus& message) { | 264 const pairing_api::HostStatus& message) { |
| 265 NOTREACHED(); | 265 NOTREACHED(); |
| 266 } | 266 } |
| 267 | 267 |
| 268 void BluetoothHostPairingController::OnConfigureHostMessage( | 268 void BluetoothHostPairingController::OnConfigureHostMessage( |
| 269 const pairing_api::ConfigureHost& message) { | 269 const pairing_api::ConfigureHost& message) { |
| 270 // TODO(zork): Add event to API to handle this case. (http://crbug.com/405744) | 270 FOR_EACH_OBSERVER(Observer, observers_, |
| 271 ConfigureHost(message.parameters().accepted_eula(), |
| 272 message.parameters().lang(), |
| 273 message.parameters().timezone(), |
| 274 message.parameters().send_reports(), |
| 275 message.parameters().keyboard_layout())); |
| 271 } | 276 } |
| 272 | 277 |
| 273 void BluetoothHostPairingController::OnPairDevicesMessage( | 278 void BluetoothHostPairingController::OnPairDevicesMessage( |
| 274 const pairing_api::PairDevices& message) { | 279 const pairing_api::PairDevices& message) { |
| 275 DCHECK(thread_checker_.CalledOnValidThread()); | 280 DCHECK(thread_checker_.CalledOnValidThread()); |
| 276 if (current_stage_ != STAGE_WAITING_FOR_CREDENTIALS) { | 281 if (current_stage_ != STAGE_WAITING_FOR_CREDENTIALS) { |
| 277 AbortWithError(PAIRING_ERROR_PAIRING_OR_ENROLLMENT, kErrorInvalidProtocol); | 282 AbortWithError(PAIRING_ERROR_PAIRING_OR_ENROLLMENT, kErrorInvalidProtocol); |
| 278 return; | 283 return; |
| 279 } | 284 } |
| 280 | 285 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 } | 335 } |
| 331 | 336 |
| 332 HostPairingController::Stage BluetoothHostPairingController::GetCurrentStage() { | 337 HostPairingController::Stage BluetoothHostPairingController::GetCurrentStage() { |
| 333 return current_stage_; | 338 return current_stage_; |
| 334 } | 339 } |
| 335 | 340 |
| 336 void BluetoothHostPairingController::StartPairing() { | 341 void BluetoothHostPairingController::StartPairing() { |
| 337 DCHECK_EQ(current_stage_, STAGE_NONE); | 342 DCHECK_EQ(current_stage_, STAGE_NONE); |
| 338 bool bluetooth_available = | 343 bool bluetooth_available = |
| 339 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); | 344 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); |
| 340 // TODO(zork): Add a stage for initialization error. (http://crbug.com/405744) | 345 if (!bluetooth_available) { |
| 341 if (!bluetooth_available) | 346 ChangeStage(STAGE_INITIALIZATION_ERROR); |
| 342 return; | 347 return; |
| 348 } |
| 343 | 349 |
| 344 device::BluetoothAdapterFactory::GetAdapter( | 350 device::BluetoothAdapterFactory::GetAdapter( |
| 345 base::Bind(&BluetoothHostPairingController::OnGetAdapter, | 351 base::Bind(&BluetoothHostPairingController::OnGetAdapter, |
| 346 ptr_factory_.GetWeakPtr())); | 352 ptr_factory_.GetWeakPtr())); |
| 347 } | 353 } |
| 348 | 354 |
| 349 std::string BluetoothHostPairingController::GetDeviceName() { | 355 std::string BluetoothHostPairingController::GetDeviceName() { |
| 350 return device_name_; | 356 return device_name_; |
| 351 } | 357 } |
| 352 | 358 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); | 411 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); |
| 406 } | 412 } |
| 407 | 413 |
| 408 void BluetoothHostPairingController::AuthorizePairing( | 414 void BluetoothHostPairingController::AuthorizePairing( |
| 409 device::BluetoothDevice* device) { | 415 device::BluetoothDevice* device) { |
| 410 // Disallow unknown device. | 416 // Disallow unknown device. |
| 411 device->RejectPairing(); | 417 device->RejectPairing(); |
| 412 } | 418 } |
| 413 | 419 |
| 414 } // namespace pairing_chromeos | 420 } // namespace pairing_chromeos |
| OLD | NEW |