Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(454)

Side by Side Diff: components/copresence_sockets/copresence_peer.cc

Issue 610633002: Prototype for copresenceSockets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/rand_util.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "components/copresence_sockets/mediums/bluetooth/copresence_socket_blue tooth.h"
12 #include "device/bluetooth/bluetooth_adapter.h"
13 #include "device/bluetooth/bluetooth_adapter_factory.h"
14 #include "device/bluetooth/bluetooth_device.h"
15 #include "device/bluetooth/bluetooth_socket.h"
16 #include "device/bluetooth/bluetooth_uuid.h"
17
18 namespace {
19
20 std::string RandomHexBytes(size_t length) {
21 static const char kHexChars[] = "0123456789ABCDEF";
22
23 std::string rand_string;
24 for (size_t i = 0; i < length; ++i)
25 rand_string += kHexChars[base::RandInt(0, 15)];
26 return rand_string;
27 }
28
29 device::BluetoothUUID GenerateRandomUuid() {
30 // Random hex string of the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
31 return device::BluetoothUUID(RandomHexBytes(8) + "-" + RandomHexBytes(4) +
Ken Rockot(use gerrit already) 2014/09/30 17:26:04 nit: Use StringPrintf instead?
rkc 2014/10/01 19:08:23 Done.
32 "-" + RandomHexBytes(4) + "-" +
33 RandomHexBytes(4) + "-" + RandomHexBytes(12));
34 }
35
36 class DefaultApprovalDelegate
37 : public device::BluetoothDevice::PairingDelegate {
38 public:
39 DefaultApprovalDelegate() {}
40 virtual ~DefaultApprovalDelegate() {}
41
42 // device::PairingDelegate overrides:
Ken Rockot(use gerrit already) 2014/09/30 17:26:04 nit: device::BluetoothDevice::PairingDelegate
rkc 2014/10/01 19:08:23 Done.
43 virtual void RequestPinCode(device::BluetoothDevice* device) override {}
44 virtual void RequestPasskey(device::BluetoothDevice* device) override {}
45 virtual void DisplayPinCode(device::BluetoothDevice* device,
46 const std::string& pincode) override {}
47 virtual void DisplayPasskey(device::BluetoothDevice* device,
48 uint32 passkey) override {}
49 virtual void KeysEntered(device::BluetoothDevice* device,
50 uint32 entered) override {}
51 virtual void ConfirmPasskey(device::BluetoothDevice* device,
52 uint32 passkey) override {}
53 virtual void AuthorizePairing(device::BluetoothDevice* device) override {
54 if (device->ExpectingConfirmation())
55 device->ConfirmPairing();
56 }
57 };
58
59 } // namespace
60
61 namespace copresence_sockets {
62
63 // Public methods.
64
65 CopresencePeer::CopresencePeer(CreatePeerCallback create_callback,
66 AcceptCallback accept_callback)
67 : create_callback_(create_callback),
68 accept_callback_(accept_callback),
armansito 2014/09/30 20:37:58 It looks like |accept_callback_| and |create_callb
rkc 2014/10/01 19:08:23 I had it that way earlier, but it seemed simpler t
69 delegate_(nullptr) {
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(
79 base::Bind(&CopresencePeer::OnGetAdapter, AsWeakPtr()));
Ken Rockot(use gerrit already) 2014/09/30 17:26:04 Here and elsewhere it looks like you only use a We
rkc 2014/10/01 19:08:23 Done.
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_);
93 delete delegate_;
94 }
95 }
96
97 // Private methods.
98
99 void CopresencePeer::OnGetAdapter(
100 scoped_refptr<device::BluetoothAdapter> adapter) {
101 if (!adapter.get()) {
102 create_callback_.Run(std::string());
103 return;
104 }
105
106 adapter_ = adapter;
107 service_uuid_ = GenerateRandomUuid();
armansito 2014/09/30 20:37:58 I'm not sure about using a random UUID here. If th
rkc 2014/10/01 19:08:23 This is how the Android side is currently implemen
108
109 delegate_ = new DefaultApprovalDelegate();
110 VLOG(2) << "Creating service with UUID: " << service_uuid_.value();
111 adapter_->AddPairingDelegate(
112 delegate_, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
113 adapter_->CreateRfcommService(
114 service_uuid_,
115 device::BluetoothAdapter::ServiceOptions(),
116 base::Bind(&CopresencePeer::OnCreateService, AsWeakPtr()),
117 base::Bind(&CopresencePeer::OnCreateServiceError, AsWeakPtr()));
118 }
119
120 void CopresencePeer::OnCreateService(
121 scoped_refptr<device::BluetoothSocket> socket) {
122 if (!socket.get()) {
123 create_callback_.Run(std::string());
124 return;
125 }
126
127 server_socket_ = socket;
128 create_callback_.Run(GetLocatorData());
129 server_socket_->Accept(
130 base::Bind(&CopresencePeer::OnAccept, AsWeakPtr()),
131 base::Bind(&CopresencePeer::OnAcceptError, AsWeakPtr()));
132 }
133
134 void CopresencePeer::OnCreateServiceError(const std::string& message) {
135 LOG(WARNING) << "Couldn't create Bluetooth service: " << message;
136 create_callback_.Run(std::string());
137 }
138
139 void CopresencePeer::OnAccept(const device::BluetoothDevice* device,
140 scoped_refptr<device::BluetoothSocket> socket) {
141 if (!socket.get())
142 return;
143 CopresenceSocketBluetooth* copresence_socket =
144 new CopresenceSocketBluetooth(socket);
145 accept_callback_.Run(copresence_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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698