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 374 375 376 377 378 379 380 381
|
// Copyright 2014 The Chromium Authors
// 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 <utility>
#include "base/containers/to_vector.h"
#include "base/functional/bind.h"
#include "base/lazy_instance.h"
#include "content/public/browser/browser_thread.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 "extensions/common/extension_id.h"
#include "net/base/io_buffer.h"
namespace {
namespace bluetooth_socket = extensions::api::bluetooth_socket;
using extensions::BluetoothApiSocket;
int kDefaultBufferSize = 4096;
bluetooth_socket::ReceiveError MapReceiveErrorReason(
BluetoothApiSocket::ErrorReason value) {
switch (value) {
case BluetoothApiSocket::kDisconnected:
return bluetooth_socket::ReceiveError::kDisconnected;
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::ReceiveError::kSystemError;
}
}
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::AcceptError::kSystemError;
}
}
} // namespace
namespace extensions {
namespace api {
using content::BrowserThread;
static base::LazyInstance<BrowserContextKeyedAPIFactory<
BluetoothSocketEventDispatcher>>::DestructorAtExit 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() = default;
BluetoothSocketEventDispatcher::SocketParams::SocketParams() = default;
BluetoothSocketEventDispatcher::SocketParams::SocketParams(
const SocketParams& other) = default;
BluetoothSocketEventDispatcher::SocketParams::~SocketParams() = default;
void BluetoothSocketEventDispatcher::OnSocketConnect(
const ExtensionId& extension_id,
int socket_id) {
DCHECK_CURRENTLY_ON(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 ExtensionId& extension_id,
int socket_id) {
DCHECK_CURRENTLY_ON(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 ExtensionId& extension_id,
int socket_id) {
DCHECK_CURRENTLY_ON(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_CURRENTLY_ON(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::BindOnce(&BluetoothSocketEventDispatcher::ReceiveCallback, params),
base::BindOnce(&BluetoothSocketEventDispatcher::ReceiveErrorCallback,
params));
}
// static
void BluetoothSocketEventDispatcher::ReceiveCallback(
const SocketParams& params,
int bytes_read,
scoped_refptr<net::IOBuffer> io_buffer) {
DCHECK_CURRENTLY_ON(params.thread_id);
// Dispatch "onReceive" event.
bluetooth_socket::ReceiveInfo receive_info;
receive_info.socket_id = params.socket_id;
receive_info.data =
base::ToVector(io_buffer->first(static_cast<size_t>(bytes_read)));
auto args = bluetooth_socket::OnReceive::Create(receive_info);
std::unique_ptr<Event> event(
new Event(events::BLUETOOTH_SOCKET_ON_RECEIVE,
bluetooth_socket::OnReceive::kEventName, std::move(args)));
PostEvent(params, std::move(event));
// Post a task to delay the read until the socket is available, as
// calling StartReceive at this point would error with ERR_IO_PENDING.
content::BrowserThread::GetTaskRunnerForThread(params.thread_id)
->PostTask(FROM_HERE,
base::BindOnce(&BluetoothSocketEventDispatcher::StartReceive,
params));
}
// static
void BluetoothSocketEventDispatcher::ReceiveErrorCallback(
const SocketParams& params,
BluetoothApiSocket::ErrorReason error_reason,
const std::string& error) {
DCHECK_CURRENTLY_ON(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);
auto args = bluetooth_socket::OnReceiveError::Create(receive_error_info);
std::unique_ptr<Event> event(
new Event(events::BLUETOOTH_SOCKET_ON_RECEIVE_ERROR,
bluetooth_socket::OnReceiveError::kEventName, std::move(args)));
PostEvent(params, std::move(event));
// 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_CURRENTLY_ON(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::BindOnce(&BluetoothSocketEventDispatcher::AcceptCallback, params),
base::BindOnce(&BluetoothSocketEventDispatcher::AcceptErrorCallback,
params));
}
// static
void BluetoothSocketEventDispatcher::AcceptCallback(
const SocketParams& params,
const device::BluetoothDevice* device,
scoped_refptr<device::BluetoothSocket> socket) {
DCHECK_CURRENTLY_ON(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;
auto args = bluetooth_socket::OnAccept::Create(accept_info);
std::unique_ptr<Event> event(new Event(events::BLUETOOTH_SOCKET_ON_ACCEPT,
bluetooth_socket::OnAccept::kEventName,
std::move(args)));
PostEvent(params, std::move(event));
// Post a task to delay the accept until the socket is available, as
// calling StartAccept at this point would error with ERR_IO_PENDING.
content::BrowserThread::GetTaskRunnerForThread(params.thread_id)
->PostTask(
FROM_HERE,
base::BindOnce(&BluetoothSocketEventDispatcher::StartAccept, params));
}
// static
void BluetoothSocketEventDispatcher::AcceptErrorCallback(
const SocketParams& params,
BluetoothApiSocket::ErrorReason error_reason,
const std::string& error) {
DCHECK_CURRENTLY_ON(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);
auto args = bluetooth_socket::OnAcceptError::Create(accept_error_info);
std::unique_ptr<Event> event(
new Event(events::BLUETOOTH_SOCKET_ON_ACCEPT_ERROR,
bluetooth_socket::OnAcceptError::kEventName, std::move(args)));
PostEvent(params, std::move(event));
// 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,
std::unique_ptr<Event> event) {
DCHECK_CURRENTLY_ON(params.thread_id);
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&DispatchEvent, params.browser_context_id,
params.extension_id, std::move(event)));
}
// static
void BluetoothSocketEventDispatcher::DispatchEvent(
void* browser_context_id,
const ExtensionId& extension_id,
std::unique_ptr<Event> event) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!ExtensionsBrowserClient::Get()->IsValidContext(browser_context_id)) {
return;
}
content::BrowserContext* context =
reinterpret_cast<content::BrowserContext*>(browser_context_id);
EventRouter* router = EventRouter::Get(context);
if (router) {
router->DispatchEventToExtension(extension_id, std::move(event));
}
}
} // namespace api
} // namespace extensions
|