1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/api/bluetooth_socket/bluetooth_api_socket.h"
#include "base/lazy_instance.h"
#include "device/bluetooth/bluetooth_socket.h"
#include "net/base/io_buffer.h"
namespace {
const char kSocketNotConnectedError[] = "Socket not connected";
const char kSocketNotListeningError[] = "Socket not listening";
} // namespace
namespace extensions {
// static
static base::LazyInstance<
BrowserContextKeyedAPIFactory<ApiResourceManager<BluetoothApiSocket> > >
g_server_factory = LAZY_INSTANCE_INITIALIZER;
// static
template <>
BrowserContextKeyedAPIFactory<ApiResourceManager<BluetoothApiSocket> >*
ApiResourceManager<BluetoothApiSocket>::GetFactoryInstance() {
return g_server_factory.Pointer();
}
BluetoothApiSocket::BluetoothApiSocket(const std::string& owner_extension_id)
: ApiResource(owner_extension_id),
persistent_(false),
buffer_size_(0),
paused_(false),
connected_(false) {
DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
}
BluetoothApiSocket::BluetoothApiSocket(
const std::string& owner_extension_id,
scoped_refptr<device::BluetoothSocket> socket,
const std::string& device_address,
const device::BluetoothUUID& uuid)
: ApiResource(owner_extension_id),
socket_(socket),
device_address_(device_address),
uuid_(uuid),
persistent_(false),
buffer_size_(0),
paused_(true),
connected_(true) {
DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
}
BluetoothApiSocket::~BluetoothApiSocket() {
DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
if (socket_.get())
socket_->Close();
}
void BluetoothApiSocket::AdoptConnectedSocket(
scoped_refptr<device::BluetoothSocket> socket,
const std::string& device_address,
const device::BluetoothUUID& uuid) {
DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
if (socket_.get())
socket_->Close();
socket_ = socket;
device_address_ = device_address;
uuid_ = uuid;
connected_ = true;
}
void BluetoothApiSocket::AdoptListeningSocket(
scoped_refptr<device::BluetoothSocket> socket,
const device::BluetoothUUID& uuid) {
DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
if (socket_.get())
socket_->Close();
socket_ = socket;
device_address_ = "";
uuid_ = uuid;
connected_ = false;
}
void BluetoothApiSocket::Disconnect(const base::Closure& callback) {
DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
if (!socket_.get()) {
callback.Run();
return;
}
connected_ = false;
socket_->Disconnect(callback);
}
bool BluetoothApiSocket::IsPersistent() const {
DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
return persistent_;
}
void BluetoothApiSocket::Receive(
int count,
const ReceiveCompletionCallback& success_callback,
const ErrorCompletionCallback& error_callback) {
DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
if (!socket_.get() || !IsConnected()) {
error_callback.Run(BluetoothApiSocket::kNotConnected,
kSocketNotConnectedError);
return;
}
socket_->Receive(count,
success_callback,
base::Bind(&OnSocketReceiveError, error_callback));
}
// static
void BluetoothApiSocket::OnSocketReceiveError(
const ErrorCompletionCallback& error_callback,
device::BluetoothSocket::ErrorReason reason,
const std::string& message) {
DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
BluetoothApiSocket::ErrorReason error_reason;
switch (reason) {
case device::BluetoothSocket::kIOPending:
error_reason = BluetoothApiSocket::kIOPending;
break;
case device::BluetoothSocket::kDisconnected:
error_reason = BluetoothApiSocket::kDisconnected;
break;
case device::BluetoothSocket::kSystemError:
error_reason = BluetoothApiSocket::kSystemError;
break;
}
error_callback.Run(error_reason, message);
}
void BluetoothApiSocket::Send(scoped_refptr<net::IOBuffer> buffer,
int buffer_size,
const SendCompletionCallback& success_callback,
const ErrorCompletionCallback& error_callback) {
DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
if (!socket_.get() || !IsConnected()) {
error_callback.Run(BluetoothApiSocket::kNotConnected,
kSocketNotConnectedError);
return;
}
socket_->Send(buffer,
buffer_size,
success_callback,
base::Bind(&OnSocketSendError, error_callback));
}
// static
void BluetoothApiSocket::OnSocketSendError(
const ErrorCompletionCallback& error_callback,
const std::string& message) {
DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
error_callback.Run(BluetoothApiSocket::kSystemError, message);
}
void BluetoothApiSocket::Accept(
const AcceptCompletionCallback& success_callback,
const ErrorCompletionCallback& error_callback) {
DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
if (!socket_.get() || IsConnected()) {
error_callback.Run(BluetoothApiSocket::kNotListening,
kSocketNotListeningError);
return;
}
socket_->Accept(success_callback,
base::Bind(&OnSocketAcceptError, error_callback));
}
// static
void BluetoothApiSocket::OnSocketAcceptError(
const ErrorCompletionCallback& error_callback,
const std::string& message) {
DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
error_callback.Run(BluetoothApiSocket::kSystemError, message);
}
} // namespace extensions
|