OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/copresence_sockets/public/copresence_peer.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/format_macros.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/rand_util.h" |
| 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/stringprintf.h" |
| 16 #include "components/copresence_sockets/transports/bluetooth/copresence_socket_b
luetooth.h" |
| 17 #include "device/bluetooth/bluetooth_adapter.h" |
| 18 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 19 #include "device/bluetooth/bluetooth_device.h" |
| 20 #include "device/bluetooth/bluetooth_socket.h" |
| 21 #include "device/bluetooth/bluetooth_uuid.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 const char kAdapterError[] = "NOADAPTER"; |
| 26 |
| 27 device::BluetoothUUID GenerateRandomUuid() { |
| 28 // Random hex string of the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. |
| 29 return device::BluetoothUUID(base::StringPrintf( |
| 30 "%08" PRIx64 "-%04" PRIx64 "-%04" PRIx64 "-%04" PRIx64 "-%012" PRIx64, |
| 31 base::RandGenerator(UINT32_MAX), |
| 32 base::RandGenerator(UINT16_MAX), |
| 33 base::RandGenerator(UINT16_MAX), |
| 34 base::RandGenerator(UINT16_MAX), |
| 35 base::RandGenerator(static_cast<uint64>(UINT16_MAX) + UINT32_MAX))); |
| 36 } |
| 37 |
| 38 // This class will confirm pairing for a device that is expecting a pairing |
| 39 // confirmation. |
| 40 class DefaultApprovalDelegate |
| 41 : public device::BluetoothDevice::PairingDelegate { |
| 42 public: |
| 43 DefaultApprovalDelegate() {} |
| 44 virtual ~DefaultApprovalDelegate() {} |
| 45 |
| 46 // device::BluetoothDevice::PairingDelegate overrides: |
| 47 virtual void RequestPinCode(device::BluetoothDevice* device) override {} |
| 48 virtual void RequestPasskey(device::BluetoothDevice* device) override {} |
| 49 virtual void DisplayPinCode(device::BluetoothDevice* device, |
| 50 const std::string& pincode) override {} |
| 51 virtual void DisplayPasskey(device::BluetoothDevice* device, |
| 52 uint32 passkey) override {} |
| 53 virtual void KeysEntered(device::BluetoothDevice* device, |
| 54 uint32 entered) override {} |
| 55 virtual void ConfirmPasskey(device::BluetoothDevice* device, |
| 56 uint32 passkey) override {} |
| 57 virtual void AuthorizePairing(device::BluetoothDevice* device) override { |
| 58 if (device->ExpectingConfirmation()) |
| 59 device->ConfirmPairing(); |
| 60 } |
| 61 }; |
| 62 |
| 63 } // namespace |
| 64 |
| 65 namespace copresence_sockets { |
| 66 |
| 67 CopresencePeer::CopresencePeer(const CreatePeerCallback& create_callback, |
| 68 const AcceptCallback& accept_callback) |
| 69 : create_callback_(create_callback), |
| 70 accept_callback_(accept_callback), |
| 71 delegate_(nullptr), |
| 72 weak_ptr_factory_(this) { |
| 73 DCHECK(!create_callback.is_null()); |
| 74 DCHECK(!accept_callback.is_null()); |
| 75 |
| 76 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| 77 create_callback_.Run(std::string()); |
| 78 return; |
| 79 } |
| 80 |
| 81 device::BluetoothAdapterFactory::GetAdapter(base::Bind( |
| 82 &CopresencePeer::OnGetAdapter, weak_ptr_factory_.GetWeakPtr())); |
| 83 } |
| 84 |
| 85 std::string CopresencePeer::GetLocatorData() { |
| 86 if (!adapter_.get()) |
| 87 return kAdapterError; |
| 88 // TODO(rkc): Fix the "1." once we have finalized the locator format with |
| 89 // other platforms. http://crbug.com/418616 |
| 90 return "1." + adapter_->GetAddress() + "." + service_uuid_.value(); |
| 91 } |
| 92 |
| 93 CopresencePeer::~CopresencePeer() { |
| 94 server_socket_->Disconnect(base::Bind(&base::DoNothing)); |
| 95 server_socket_->Close(); |
| 96 if (delegate_) |
| 97 adapter_->RemovePairingDelegate(delegate_.get()); |
| 98 } |
| 99 |
| 100 // Private methods. |
| 101 |
| 102 void CopresencePeer::OnGetAdapter( |
| 103 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 104 if (!adapter.get() || !adapter->IsPresent() || !adapter->IsPowered()) { |
| 105 create_callback_.Run(std::string()); |
| 106 return; |
| 107 } |
| 108 |
| 109 adapter_ = adapter; |
| 110 service_uuid_ = GenerateRandomUuid(); |
| 111 |
| 112 delegate_ = make_scoped_ptr(new DefaultApprovalDelegate()); |
| 113 VLOG(2) << "Creating service with UUID: " << service_uuid_.value(); |
| 114 adapter_->AddPairingDelegate( |
| 115 delegate_.get(), |
| 116 device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH); |
| 117 adapter_->CreateRfcommService( |
| 118 service_uuid_, |
| 119 device::BluetoothAdapter::ServiceOptions(), |
| 120 base::Bind(&CopresencePeer::OnCreateService, |
| 121 weak_ptr_factory_.GetWeakPtr()), |
| 122 base::Bind(&CopresencePeer::OnCreateServiceError, |
| 123 weak_ptr_factory_.GetWeakPtr())); |
| 124 } |
| 125 |
| 126 void CopresencePeer::OnCreateService( |
| 127 scoped_refptr<device::BluetoothSocket> socket) { |
| 128 if (!socket.get()) { |
| 129 create_callback_.Run(std::string()); |
| 130 return; |
| 131 } |
| 132 |
| 133 server_socket_ = socket; |
| 134 create_callback_.Run(GetLocatorData()); |
| 135 server_socket_->Accept( |
| 136 base::Bind(&CopresencePeer::OnAccept, weak_ptr_factory_.GetWeakPtr()), |
| 137 base::Bind(&CopresencePeer::OnAcceptError, |
| 138 weak_ptr_factory_.GetWeakPtr())); |
| 139 } |
| 140 |
| 141 void CopresencePeer::OnCreateServiceError(const std::string& message) { |
| 142 LOG(WARNING) << "Couldn't create Bluetooth service: " << message; |
| 143 create_callback_.Run(std::string()); |
| 144 } |
| 145 |
| 146 void CopresencePeer::OnAccept(const device::BluetoothDevice* device, |
| 147 scoped_refptr<device::BluetoothSocket> socket) { |
| 148 if (!socket.get()) |
| 149 return; |
| 150 accept_callback_.Run(make_scoped_ptr(new CopresenceSocketBluetooth(socket))); |
| 151 } |
| 152 |
| 153 void CopresencePeer::OnAcceptError(const std::string& message) { |
| 154 LOG(WARNING) << "Couldn't accept Bluetooth connection: " << message; |
| 155 } |
| 156 |
| 157 } // namespace copresence_sockets |
OLD | NEW |