Chromium Code Reviews| 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 uint32 device_id = base::Hash(adapter_->GetAddress()) & 0xFFFF; |
|
achuithb
2014/09/18 06:59:17
A comment would help here. I'm guessing you're mas
Zachary Kuznia
2014/09/18 23:48:17
Done.
| |
| 122 device_name_ = base::StringPrintf("%s%s", kDeviceNamePrefix, | 123 |
| 123 adapter_->GetAddress().c_str()); | 124 device_name_ = base::StringPrintf("%s%04X", kDeviceNamePrefix, device_id); |
| 124 | 125 |
| 125 adapter_->SetName( | 126 adapter_->SetName( |
| 126 device_name_, | 127 device_name_, |
| 127 base::Bind(&BluetoothHostPairingController::OnSetName, | 128 base::Bind(&BluetoothHostPairingController::OnSetName, |
| 128 ptr_factory_.GetWeakPtr()), | 129 ptr_factory_.GetWeakPtr()), |
| 129 base::Bind(&BluetoothHostPairingController::OnSetError, | 130 base::Bind(&BluetoothHostPairingController::OnSetError, |
| 130 ptr_factory_.GetWeakPtr())); | 131 ptr_factory_.GetWeakPtr())); |
| 131 } | 132 } |
| 132 | 133 |
| 133 void BluetoothHostPairingController::OnSetName() { | 134 void BluetoothHostPairingController::OnSetName() { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 kReceiveSize, | 226 kReceiveSize, |
| 226 base::Bind(&BluetoothHostPairingController::OnReceiveComplete, | 227 base::Bind(&BluetoothHostPairingController::OnReceiveComplete, |
| 227 ptr_factory_.GetWeakPtr()), | 228 ptr_factory_.GetWeakPtr()), |
| 228 base::Bind(&BluetoothHostPairingController::OnReceiveError, | 229 base::Bind(&BluetoothHostPairingController::OnReceiveError, |
| 229 ptr_factory_.GetWeakPtr())); | 230 ptr_factory_.GetWeakPtr())); |
| 230 } | 231 } |
| 231 | 232 |
| 232 void BluetoothHostPairingController::OnCreateServiceError( | 233 void BluetoothHostPairingController::OnCreateServiceError( |
| 233 const std::string& message) { | 234 const std::string& message) { |
| 234 LOG(ERROR) << message; | 235 LOG(ERROR) << message; |
| 235 // TODO(zork): Add a stage for initialization error. (http://crbug.com/405744) | 236 ChangeStage(STAGE_INITIALIZATION_ERROR); |
| 236 ChangeStage(STAGE_NONE); | |
| 237 } | 237 } |
| 238 | 238 |
| 239 void BluetoothHostPairingController::OnSetError() { | 239 void BluetoothHostPairingController::OnSetError() { |
| 240 adapter_->RemovePairingDelegate(this); | 240 adapter_->RemovePairingDelegate(this); |
| 241 // TODO(zork): Add a stage for initialization error. (http://crbug.com/405744) | 241 ChangeStage(STAGE_INITIALIZATION_ERROR); |
| 242 ChangeStage(STAGE_NONE); | |
| 243 } | 242 } |
| 244 | 243 |
| 245 void BluetoothHostPairingController::OnAcceptError( | 244 void BluetoothHostPairingController::OnAcceptError( |
| 246 const std::string& error_message) { | 245 const std::string& error_message) { |
| 247 LOG(ERROR) << error_message; | 246 LOG(ERROR) << error_message; |
| 248 Reset(); | 247 Reset(); |
| 249 } | 248 } |
| 250 | 249 |
| 251 void BluetoothHostPairingController::OnSendError( | 250 void BluetoothHostPairingController::OnSendError( |
| 252 const std::string& error_message) { | 251 const std::string& error_message) { |
| 253 LOG(ERROR) << error_message; | 252 LOG(ERROR) << error_message; |
| 254 } | 253 } |
| 255 | 254 |
| 256 void BluetoothHostPairingController::OnReceiveError( | 255 void BluetoothHostPairingController::OnReceiveError( |
| 257 device::BluetoothSocket::ErrorReason reason, | 256 device::BluetoothSocket::ErrorReason reason, |
| 258 const std::string& error_message) { | 257 const std::string& error_message) { |
| 259 LOG(ERROR) << reason << ", " << error_message; | 258 LOG(ERROR) << reason << ", " << error_message; |
| 260 Reset(); | 259 Reset(); |
| 261 } | 260 } |
| 262 | 261 |
| 263 void BluetoothHostPairingController::OnHostStatusMessage( | 262 void BluetoothHostPairingController::OnHostStatusMessage( |
| 264 const pairing_api::HostStatus& message) { | 263 const pairing_api::HostStatus& message) { |
| 265 NOTREACHED(); | 264 NOTREACHED(); |
| 266 } | 265 } |
| 267 | 266 |
| 268 void BluetoothHostPairingController::OnConfigureHostMessage( | 267 void BluetoothHostPairingController::OnConfigureHostMessage( |
| 269 const pairing_api::ConfigureHost& message) { | 268 const pairing_api::ConfigureHost& message) { |
| 270 // TODO(zork): Add event to API to handle this case. (http://crbug.com/405744) | 269 FOR_EACH_OBSERVER(Observer, observers_, |
| 270 ConfigureHost(message.parameters().accepted_eula(), | |
| 271 message.parameters().lang(), | |
| 272 message.parameters().timezone(), | |
| 273 message.parameters().send_reports(), | |
| 274 message.parameters().keyboard_layout())); | |
| 271 } | 275 } |
| 272 | 276 |
| 273 void BluetoothHostPairingController::OnPairDevicesMessage( | 277 void BluetoothHostPairingController::OnPairDevicesMessage( |
| 274 const pairing_api::PairDevices& message) { | 278 const pairing_api::PairDevices& message) { |
| 275 DCHECK(thread_checker_.CalledOnValidThread()); | 279 DCHECK(thread_checker_.CalledOnValidThread()); |
| 276 if (current_stage_ != STAGE_WAITING_FOR_CREDENTIALS) { | 280 if (current_stage_ != STAGE_WAITING_FOR_CREDENTIALS) { |
| 277 AbortWithError(PAIRING_ERROR_PAIRING_OR_ENROLLMENT, kErrorInvalidProtocol); | 281 AbortWithError(PAIRING_ERROR_PAIRING_OR_ENROLLMENT, kErrorInvalidProtocol); |
| 278 return; | 282 return; |
| 279 } | 283 } |
| 280 | 284 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 330 } | 334 } |
| 331 | 335 |
| 332 HostPairingController::Stage BluetoothHostPairingController::GetCurrentStage() { | 336 HostPairingController::Stage BluetoothHostPairingController::GetCurrentStage() { |
| 333 return current_stage_; | 337 return current_stage_; |
| 334 } | 338 } |
| 335 | 339 |
| 336 void BluetoothHostPairingController::StartPairing() { | 340 void BluetoothHostPairingController::StartPairing() { |
| 337 DCHECK_EQ(current_stage_, STAGE_NONE); | 341 DCHECK_EQ(current_stage_, STAGE_NONE); |
| 338 bool bluetooth_available = | 342 bool bluetooth_available = |
| 339 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); | 343 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); |
| 340 // TODO(zork): Add a stage for initialization error. (http://crbug.com/405744) | 344 if (!bluetooth_available) { |
| 341 if (!bluetooth_available) | 345 ChangeStage(STAGE_INITIALIZATION_ERROR); |
| 342 return; | 346 return; |
| 347 } | |
| 343 | 348 |
| 344 device::BluetoothAdapterFactory::GetAdapter( | 349 device::BluetoothAdapterFactory::GetAdapter( |
| 345 base::Bind(&BluetoothHostPairingController::OnGetAdapter, | 350 base::Bind(&BluetoothHostPairingController::OnGetAdapter, |
| 346 ptr_factory_.GetWeakPtr())); | 351 ptr_factory_.GetWeakPtr())); |
| 347 } | 352 } |
| 348 | 353 |
| 349 std::string BluetoothHostPairingController::GetDeviceName() { | 354 std::string BluetoothHostPairingController::GetDeviceName() { |
| 350 return device_name_; | 355 return device_name_; |
| 351 } | 356 } |
| 352 | 357 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 405 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); | 410 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); |
| 406 } | 411 } |
| 407 | 412 |
| 408 void BluetoothHostPairingController::AuthorizePairing( | 413 void BluetoothHostPairingController::AuthorizePairing( |
| 409 device::BluetoothDevice* device) { | 414 device::BluetoothDevice* device) { |
| 410 // Disallow unknown device. | 415 // Disallow unknown device. |
| 411 device->RejectPairing(); | 416 device->RejectPairing(); |
| 412 } | 417 } |
| 413 | 418 |
| 414 } // namespace pairing_chromeos | 419 } // namespace pairing_chromeos |
| OLD | NEW |