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