Index: chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_event_dispatcher.cc |
diff --git a/chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_event_dispatcher.cc b/chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_event_dispatcher.cc |
index ae312fc74b3a5c7598e30ec3b1826e31c2eecfc4..47de7cbf18190fff130dbf6bc24259dfce9c63f8 100644 |
--- a/chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_event_dispatcher.cc |
+++ b/chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_event_dispatcher.cc |
@@ -7,6 +7,8 @@ |
#include "chrome/browser/browser_process.h" |
#include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h" |
#include "chrome/common/extensions/api/bluetooth_socket.h" |
+#include "device/bluetooth/bluetooth_device.h" |
+#include "device/bluetooth/bluetooth_socket.h" |
#include "extensions/browser/event_router.h" |
#include "net/base/io_buffer.h" |
#include "net/base/net_errors.h" |
@@ -18,7 +20,7 @@ using extensions::BluetoothApiSocket; |
int kDefaultBufferSize = 4096; |
-bluetooth_socket::ReceiveError MapErrorReason( |
+bluetooth_socket::ReceiveError MapReceiveErrorReason( |
BluetoothApiSocket::ErrorReason value) { |
switch (value) { |
case BluetoothApiSocket::kDisconnected: |
@@ -36,6 +38,20 @@ bluetooth_socket::ReceiveError MapErrorReason( |
} |
} |
+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 { |
@@ -78,43 +94,66 @@ BluetoothSocketEventDispatcher::BluetoothSocketEventDispatcher( |
BluetoothSocketEventDispatcher::~BluetoothSocketEventDispatcher() {} |
-BluetoothSocketEventDispatcher::ReceiveParams::ReceiveParams() {} |
+BluetoothSocketEventDispatcher::SocketParams::SocketParams() {} |
-BluetoothSocketEventDispatcher::ReceiveParams::~ReceiveParams() {} |
+BluetoothSocketEventDispatcher::SocketParams::~SocketParams() {} |
void BluetoothSocketEventDispatcher::OnSocketConnect( |
const std::string& extension_id, |
int socket_id) { |
DCHECK(BrowserThread::CurrentlyOn(thread_id_)); |
- StartSocketReceive(extension_id, socket_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::OnSocketResume( |
+void BluetoothSocketEventDispatcher::OnSocketListen( |
const std::string& extension_id, |
int socket_id) { |
DCHECK(BrowserThread::CurrentlyOn(thread_id_)); |
- StartSocketReceive(extension_id, socket_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::StartSocketReceive( |
+void BluetoothSocketEventDispatcher::OnSocketResume( |
const std::string& extension_id, |
int socket_id) { |
DCHECK(BrowserThread::CurrentlyOn(thread_id_)); |
- ReceiveParams params; |
+ 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); |
+ 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; |
+ } else if (socket->IsConnected()) { |
armansito
2014/05/08 22:00:16
nit: No need for else since the previous block ret
|
+ StartReceive(params); |
+ } else { |
+ StartAccept(params); |
+ } |
} |
// static |
-void BluetoothSocketEventDispatcher::StartReceive(const ReceiveParams& params) { |
+void BluetoothSocketEventDispatcher::StartReceive(const SocketParams& params) { |
DCHECK(BrowserThread::CurrentlyOn(params.thread_id)); |
BluetoothApiSocket* socket = |
@@ -143,7 +182,7 @@ void BluetoothSocketEventDispatcher::StartReceive(const ReceiveParams& params) { |
// static |
void BluetoothSocketEventDispatcher::ReceiveCallback( |
- const ReceiveParams& params, |
+ const SocketParams& params, |
int bytes_read, |
scoped_refptr<net::IOBuffer> io_buffer) { |
DCHECK(BrowserThread::CurrentlyOn(params.thread_id)); |
@@ -168,7 +207,7 @@ void BluetoothSocketEventDispatcher::ReceiveCallback( |
// static |
void BluetoothSocketEventDispatcher::ReceiveErrorCallback( |
- const ReceiveParams& params, |
+ const SocketParams& params, |
BluetoothApiSocket::ErrorReason error_reason, |
const std::string& error) { |
DCHECK(BrowserThread::CurrentlyOn(params.thread_id)); |
@@ -185,7 +224,7 @@ void BluetoothSocketEventDispatcher::ReceiveErrorCallback( |
bluetooth_socket::ReceiveErrorInfo receive_error_info; |
receive_error_info.socket_id = params.socket_id; |
receive_error_info.error_message = error; |
- receive_error_info.error = MapErrorReason(error_reason); |
+ receive_error_info.error = MapReceiveErrorReason(error_reason); |
scoped_ptr<base::ListValue> args = |
bluetooth_socket::OnReceiveError::Create(receive_error_info); |
scoped_ptr<Event> event( |
@@ -202,7 +241,102 @@ void BluetoothSocketEventDispatcher::ReceiveErrorCallback( |
} |
// static |
-void BluetoothSocketEventDispatcher::PostEvent(const ReceiveParams& params, |
+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(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 "acce0t" |
rpaquay
2014/05/09 16:09:53
accept?
keybuk
2014/05/09 18:28:24
Done.
|
+ // callback. We can safely ignore this error, as the application should not |
+ // care. |
+ return; |
+ } |
+ |
+ // Dispatch "onAcceptError" event but don't start another read to avoid |
rpaquay
2014/05/09 16:09:53
read => accept
keybuk
2014/05/09 18:28:24
Done.
|
+ // potential infinite reads if we have a persistent network error. |
rpaquay
2014/05/09 16:09:53
reads => accepts
keybuk
2014/05/09 18:28:24
Done.
|
+ 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(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)); |