Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / components / copresence_sockets / copresence_peer.cc
blobe4b5a96e8c359cab3dfb0c89e23b21ff1fdc0bb2
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.
5 #include "components/copresence_sockets/public/copresence_peer.h"
7 #include <stdint.h>
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 "components/copresence_sockets/transports/bluetooth/copresence_socket_bluetooth.h"
14 #include "device/bluetooth/bluetooth_adapter.h"
15 #include "device/bluetooth/bluetooth_adapter_factory.h"
16 #include "device/bluetooth/bluetooth_device.h"
17 #include "device/bluetooth/bluetooth_socket.h"
18 #include "device/bluetooth/bluetooth_uuid.h"
20 namespace {
22 const char kAdapterError[] = "NOADAPTER";
23 const char kSocketServiceUuid[] = "2491fb14-0077-4d4d-bd41-b18e9a570f56";
25 // This class will confirm pairing for a device that is expecting a pairing
26 // confirmation.
27 class DefaultApprovalDelegate
28 : public device::BluetoothDevice::PairingDelegate {
29 public:
30 DefaultApprovalDelegate() {}
31 ~DefaultApprovalDelegate() override {}
33 // device::BluetoothDevice::PairingDelegate overrides:
34 void RequestPinCode(device::BluetoothDevice* device) override {}
35 void RequestPasskey(device::BluetoothDevice* device) override {}
36 void DisplayPinCode(device::BluetoothDevice* device,
37 const std::string& pincode) override {}
38 void DisplayPasskey(device::BluetoothDevice* device,
39 uint32 passkey) override {}
40 void KeysEntered(device::BluetoothDevice* device, uint32 entered) override {}
41 void ConfirmPasskey(device::BluetoothDevice* device,
42 uint32 passkey) override {}
43 void AuthorizePairing(device::BluetoothDevice* device) override {
44 if (device->ExpectingConfirmation())
45 device->ConfirmPairing();
49 } // namespace
51 namespace copresence_sockets {
53 CopresencePeer::CopresencePeer(const CreatePeerCallback& create_callback,
54 const AcceptCallback& accept_callback)
55 : create_callback_(create_callback),
56 accept_callback_(accept_callback),
57 delegate_(nullptr),
58 weak_ptr_factory_(this) {
59 DCHECK(!create_callback.is_null());
60 DCHECK(!accept_callback.is_null());
62 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
63 create_callback_.Run(std::string());
64 return;
67 device::BluetoothAdapterFactory::GetAdapter(base::Bind(
68 &CopresencePeer::OnGetAdapter, weak_ptr_factory_.GetWeakPtr()));
71 std::string CopresencePeer::GetLocatorData() {
72 if (!adapter_.get())
73 return kAdapterError;
74 // TODO(rkc): Fix the "1." once we have finalized the locator format with
75 // other platforms. http://crbug.com/418616
76 return "1." + adapter_->GetAddress() + "." + kSocketServiceUuid;
79 CopresencePeer::~CopresencePeer() {
80 server_socket_->Disconnect(base::Bind(&base::DoNothing));
81 server_socket_->Close();
82 if (delegate_)
83 adapter_->RemovePairingDelegate(delegate_.get());
86 // Private methods.
88 void CopresencePeer::OnGetAdapter(
89 scoped_refptr<device::BluetoothAdapter> adapter) {
90 if (!adapter.get() || !adapter->IsPresent() || !adapter->IsPowered()) {
91 LOG(WARNING) << "Unable to use BT adapter";
92 create_callback_.Run(std::string());
93 return;
96 adapter_ = adapter;
97 delegate_ = make_scoped_ptr(new DefaultApprovalDelegate());
98 VLOG(2) << "Got Adapter, creating service with UUID: " << kSocketServiceUuid;
99 adapter_->AddPairingDelegate(
100 delegate_.get(),
101 device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
102 adapter_->CreateRfcommService(
103 device::BluetoothUUID(kSocketServiceUuid),
104 device::BluetoothAdapter::ServiceOptions(),
105 base::Bind(&CopresencePeer::OnCreateService,
106 weak_ptr_factory_.GetWeakPtr()),
107 base::Bind(&CopresencePeer::OnCreateServiceError,
108 weak_ptr_factory_.GetWeakPtr()));
111 void CopresencePeer::OnCreateService(
112 scoped_refptr<device::BluetoothSocket> socket) {
113 if (!socket.get()) {
114 LOG(WARNING) << "Couldn't create service!";
115 create_callback_.Run(std::string());
116 return;
119 VLOG(3) << "Starting Accept Socket.";
120 server_socket_ = socket;
121 create_callback_.Run(GetLocatorData());
122 server_socket_->Accept(
123 base::Bind(&CopresencePeer::OnAccept, weak_ptr_factory_.GetWeakPtr()),
124 base::Bind(&CopresencePeer::OnAcceptError,
125 weak_ptr_factory_.GetWeakPtr()));
128 void CopresencePeer::OnCreateServiceError(const std::string& message) {
129 LOG(WARNING) << "Couldn't create Bluetooth service: " << message;
130 create_callback_.Run(std::string());
133 void CopresencePeer::OnAccept(const device::BluetoothDevice* device,
134 scoped_refptr<device::BluetoothSocket> socket) {
135 if (!socket.get())
136 return;
137 VLOG(3) << "Accepted Socket.";
138 accept_callback_.Run(make_scoped_ptr(new CopresenceSocketBluetooth(socket)));
141 void CopresencePeer::OnAcceptError(const std::string& message) {
142 LOG(WARNING) << "Couldn't accept Bluetooth connection: " << message;
145 } // namespace copresence_sockets