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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
// 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_socket_event_dispatcher.h"
#include "base/lazy_instance.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_socket.h"
#include "extensions/browser/api/bluetooth_socket/bluetooth_api_socket.h"
#include "extensions/browser/event_router.h"
#include "extensions/common/api/bluetooth_socket.h"
#include "net/base/io_buffer.h"
namespace {
namespace bluetooth_socket = extensions::core_api::bluetooth_socket;
using extensions::BluetoothApiSocket;
int kDefaultBufferSize = 4096;
bluetooth_socket::ReceiveError MapReceiveErrorReason(
BluetoothApiSocket::ErrorReason value) {
switch (value) {
case BluetoothApiSocket::kDisconnected:
return bluetooth_socket::RECEIVE_ERROR_DISCONNECTED;
case BluetoothApiSocket::kNotConnected:
// kNotConnected is impossible since a socket has to be connected to be
// able to call Receive() on it.
// fallthrough
case BluetoothApiSocket::kIOPending:
// kIOPending is not relevant to apps, as BluetoothSocketEventDispatcher
// handles this specific error.
// fallthrough
default:
return bluetooth_socket::RECEIVE_ERROR_SYSTEM_ERROR;
}
}
bluetooth_socket::AcceptError MapAcceptErrorReason(
BluetoothApiSocket::ErrorReason value) {
// TODO(keybuk): All values are system error, we may want to seperate these
// out to more discrete reasons.
switch (value) {
case BluetoothApiSocket::kNotListening:
// kNotListening is impossible since a socket has to be listening to be
// able to call Accept() on it.
// fallthrough
default:
return bluetooth_socket::ACCEPT_ERROR_SYSTEM_ERROR;
}
}
} // namespace
namespace extensions {
namespace core_api {
using content::BrowserThread;
static base::LazyInstance<
BrowserContextKeyedAPIFactory<BluetoothSocketEventDispatcher> > g_factory =
LAZY_INSTANCE_INITIALIZER;
// static
BrowserContextKeyedAPIFactory<BluetoothSocketEventDispatcher>*
BluetoothSocketEventDispatcher::GetFactoryInstance() {
return g_factory.Pointer();
}
// static
BluetoothSocketEventDispatcher* BluetoothSocketEventDispatcher::Get(
content::BrowserContext* context) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
return BrowserContextKeyedAPIFactory<BluetoothSocketEventDispatcher>::Get(
context);
}
BluetoothSocketEventDispatcher::BluetoothSocketEventDispatcher(
content::BrowserContext* context)
: thread_id_(BluetoothApiSocket::kThreadId),
browser_context_(context) {
ApiResourceManager<BluetoothApiSocket>* manager =
ApiResourceManager<BluetoothApiSocket>::Get(browser_context_);
DCHECK(manager)
<< "There is no socket manager. "
"If this assertion is failing during a test, then it is likely that "
"TestExtensionSystem is failing to provide an instance of "
"ApiResourceManager<BluetoothApiSocket>.";
sockets_ = manager->data_;
}
BluetoothSocketEventDispatcher::~BluetoothSocketEventDispatcher() {}
BluetoothSocketEventDispatcher::SocketParams::SocketParams() {}
BluetoothSocketEventDispatcher::SocketParams::~SocketParams() {}
void BluetoothSocketEventDispatcher::OnSocketConnect(
const std::string& extension_id,
int socket_id) {
DCHECK(BrowserThread::CurrentlyOn(thread_id_));
SocketParams params;
params.thread_id = thread_id_;
params.browser_context_id = browser_context_;
params.extension_id = extension_id;
params.sockets = sockets_;
params.socket_id = socket_id;
StartReceive(params);
}
void BluetoothSocketEventDispatcher::OnSocketListen(
const std::string& extension_id,
int socket_id) {
DCHECK(BrowserThread::CurrentlyOn(thread_id_));
SocketParams params;
params.thread_id = thread_id_;
params.browser_context_id = browser_context_;
params.extension_id = extension_id;
params.sockets = sockets_;
params.socket_id = socket_id;
StartAccept(params);
}
void BluetoothSocketEventDispatcher::OnSocketResume(
const std::string& extension_id,
int socket_id) {
DCHECK(BrowserThread::CurrentlyOn(thread_id_));
SocketParams params;
params.thread_id = thread_id_;
params.browser_context_id = browser_context_;
params.extension_id = extension_id;
params.sockets = sockets_;
params.socket_id = socket_id;
BluetoothApiSocket* socket =
params.sockets->Get(params.extension_id, params.socket_id);
if (!socket) {
// This can happen if the socket is closed while our callback is active.
return;
}
if (socket->IsConnected()) {
StartReceive(params);
} else {
StartAccept(params);
}
}
// static
void BluetoothSocketEventDispatcher::StartReceive(const SocketParams& params) {
DCHECK(BrowserThread::CurrentlyOn(params.thread_id));
BluetoothApiSocket* socket =
params.sockets->Get(params.extension_id, params.socket_id);
if (!socket) {
// This can happen if the socket is closed while our callback is active.
return;
}
DCHECK(params.extension_id == socket->owner_extension_id())
<< "Socket has wrong owner.";
// Don't start another read if the socket has been paused.
if (socket->paused())
return;
int buffer_size = socket->buffer_size();
if (buffer_size <= 0)
buffer_size = kDefaultBufferSize;
socket->Receive(
buffer_size,
base::Bind(
&BluetoothSocketEventDispatcher::ReceiveCallback, params),
base::Bind(
&BluetoothSocketEventDispatcher::ReceiveErrorCallback, params));
}
// static
void BluetoothSocketEventDispatcher::ReceiveCallback(
const SocketParams& params,
int bytes_read,
scoped_refptr<net::IOBuffer> io_buffer) {
DCHECK(BrowserThread::CurrentlyOn(params.thread_id));
// Dispatch "onReceive" event.
bluetooth_socket::ReceiveInfo receive_info;
receive_info.socket_id = params.socket_id;
receive_info.data.assign(io_buffer->data(), io_buffer->data() + bytes_read);
scoped_ptr<base::ListValue> args =
bluetooth_socket::OnReceive::Create(receive_info);
scoped_ptr<Event> event(new Event(
events::UNKNOWN, bluetooth_socket::OnReceive::kEventName, args.Pass()));
PostEvent(params, event.Pass());
// Post a task to delay the read until the socket is available, as
// calling StartReceive at this point would error with ERR_IO_PENDING.
BrowserThread::PostTask(
params.thread_id,
FROM_HERE,
base::Bind(&BluetoothSocketEventDispatcher::StartReceive, params));
}
// static
void BluetoothSocketEventDispatcher::ReceiveErrorCallback(
const SocketParams& params,
BluetoothApiSocket::ErrorReason error_reason,
const std::string& error) {
DCHECK(BrowserThread::CurrentlyOn(params.thread_id));
if (error_reason == BluetoothApiSocket::kIOPending) {
// This happens when resuming a socket which already had an active "read"
// callback. We can safely ignore this error, as the application should not
// care.
return;
}
// Dispatch "onReceiveError" event but don't start another read to avoid
// potential infinite reads if we have a persistent network error.
bluetooth_socket::ReceiveErrorInfo receive_error_info;
receive_error_info.socket_id = params.socket_id;
receive_error_info.error_message = error;
receive_error_info.error = MapReceiveErrorReason(error_reason);
scoped_ptr<base::ListValue> args =
bluetooth_socket::OnReceiveError::Create(receive_error_info);
scoped_ptr<Event> event(
new Event(events::UNKNOWN, bluetooth_socket::OnReceiveError::kEventName,
args.Pass()));
PostEvent(params, event.Pass());
// Since we got an error, the socket is now "paused" until the application
// "resumes" it.
BluetoothApiSocket* socket =
params.sockets->Get(params.extension_id, params.socket_id);
if (socket) {
socket->set_paused(true);
}
}
// static
void BluetoothSocketEventDispatcher::StartAccept(const SocketParams& params) {
DCHECK(BrowserThread::CurrentlyOn(params.thread_id));
BluetoothApiSocket* socket =
params.sockets->Get(params.extension_id, params.socket_id);
if (!socket) {
// This can happen if the socket is closed while our callback is active.
return;
}
DCHECK(params.extension_id == socket->owner_extension_id())
<< "Socket has wrong owner.";
// Don't start another accept if the socket has been paused.
if (socket->paused())
return;
socket->Accept(
base::Bind(
&BluetoothSocketEventDispatcher::AcceptCallback, params),
base::Bind(
&BluetoothSocketEventDispatcher::AcceptErrorCallback, params));
}
// static
void BluetoothSocketEventDispatcher::AcceptCallback(
const SocketParams& params,
const device::BluetoothDevice* device,
scoped_refptr<device::BluetoothSocket> socket) {
DCHECK(BrowserThread::CurrentlyOn(params.thread_id));
BluetoothApiSocket* server_api_socket =
params.sockets->Get(params.extension_id, params.socket_id);
DCHECK(server_api_socket);
BluetoothApiSocket* client_api_socket = new BluetoothApiSocket(
params.extension_id,
socket,
device->GetAddress(),
server_api_socket->uuid());
int client_socket_id = params.sockets->Add(client_api_socket);
// Dispatch "onAccept" event.
bluetooth_socket::AcceptInfo accept_info;
accept_info.socket_id = params.socket_id;
accept_info.client_socket_id = client_socket_id;
scoped_ptr<base::ListValue> args =
bluetooth_socket::OnAccept::Create(accept_info);
scoped_ptr<Event> event(new Event(
events::UNKNOWN, bluetooth_socket::OnAccept::kEventName, args.Pass()));
PostEvent(params, event.Pass());
// Post a task to delay the accept until the socket is available, as
// calling StartAccept at this point would error with ERR_IO_PENDING.
BrowserThread::PostTask(
params.thread_id,
FROM_HERE,
base::Bind(&BluetoothSocketEventDispatcher::StartAccept, params));
}
// static
void BluetoothSocketEventDispatcher::AcceptErrorCallback(
const SocketParams& params,
BluetoothApiSocket::ErrorReason error_reason,
const std::string& error) {
DCHECK(BrowserThread::CurrentlyOn(params.thread_id));
if (error_reason == BluetoothApiSocket::kIOPending) {
// This happens when resuming a socket which already had an active "accept"
// callback. We can safely ignore this error, as the application should not
// care.
return;
}
// Dispatch "onAcceptError" event but don't start another accept to avoid
// potential infinite accepts if we have a persistent network error.
bluetooth_socket::AcceptErrorInfo accept_error_info;
accept_error_info.socket_id = params.socket_id;
accept_error_info.error_message = error;
accept_error_info.error = MapAcceptErrorReason(error_reason);
scoped_ptr<base::ListValue> args =
bluetooth_socket::OnAcceptError::Create(accept_error_info);
scoped_ptr<Event> event(new Event(events::UNKNOWN,
bluetooth_socket::OnAcceptError::kEventName,
args.Pass()));
PostEvent(params, event.Pass());
// Since we got an error, the socket is now "paused" until the application
// "resumes" it.
BluetoothApiSocket* socket =
params.sockets->Get(params.extension_id, params.socket_id);
if (socket) {
socket->set_paused(true);
}
}
// static
void BluetoothSocketEventDispatcher::PostEvent(const SocketParams& params,
scoped_ptr<Event> event) {
DCHECK(BrowserThread::CurrentlyOn(params.thread_id));
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&DispatchEvent,
params.browser_context_id,
params.extension_id,
base::Passed(event.Pass())));
}
// static
void BluetoothSocketEventDispatcher::DispatchEvent(
void* browser_context_id,
const std::string& extension_id,
scoped_ptr<Event> event) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
content::BrowserContext* context =
reinterpret_cast<content::BrowserContext*>(browser_context_id);
if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context))
return;
EventRouter* router = EventRouter::Get(context);
if (router)
router->DispatchEventToExtension(extension_id, event.Pass());
}
} // namespace core_api
} // namespace extensions
|