| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT_DI
SPATCHER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT_DI
SPATCHER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT_DI
SPATCHER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT_DI
SPATCHER_H_ |
| 7 | 7 |
| 8 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h" | 8 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h" |
| 9 #include "extensions/browser/api/api_resource_manager.h" | 9 #include "extensions/browser/api/api_resource_manager.h" |
| 10 #include "extensions/browser/browser_context_keyed_api_factory.h" | 10 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 class BrowserContext; | 13 class BrowserContext; |
| 14 } | 14 } |
| 15 | 15 |
| 16 namespace device { |
| 17 class BluetoothDevice; |
| 18 class BluetoothSocket; |
| 19 } |
| 20 |
| 16 namespace extensions { | 21 namespace extensions { |
| 17 struct Event; | 22 struct Event; |
| 18 class BluetoothApiSocket; | 23 class BluetoothApiSocket; |
| 19 } | 24 } |
| 20 | 25 |
| 21 namespace extensions { | 26 namespace extensions { |
| 22 namespace api { | 27 namespace api { |
| 23 | 28 |
| 24 // Dispatch events related to "bluetooth" sockets from callback on native socket | 29 // Dispatch events related to "bluetooth" sockets from callback on native socket |
| 25 // instances. There is one instance per browser context. | 30 // instances. There is one instance per browser context. |
| 26 class BluetoothSocketEventDispatcher | 31 class BluetoothSocketEventDispatcher |
| 27 : public BrowserContextKeyedAPI, | 32 : public BrowserContextKeyedAPI, |
| 28 public base::SupportsWeakPtr<BluetoothSocketEventDispatcher> { | 33 public base::SupportsWeakPtr<BluetoothSocketEventDispatcher> { |
| 29 public: | 34 public: |
| 30 explicit BluetoothSocketEventDispatcher(content::BrowserContext* context); | 35 explicit BluetoothSocketEventDispatcher(content::BrowserContext* context); |
| 31 virtual ~BluetoothSocketEventDispatcher(); | 36 virtual ~BluetoothSocketEventDispatcher(); |
| 32 | 37 |
| 33 // Socket is active, start receiving data from it. | 38 // Socket is active, start receiving data from it. |
| 34 void OnSocketConnect(const std::string& extension_id, int socket_id); | 39 void OnSocketConnect(const std::string& extension_id, int socket_id); |
| 35 | 40 |
| 41 // Socket is active again, start accepting connections from it. |
| 42 void OnSocketListen(const std::string& extension_id, int socket_id); |
| 43 |
| 36 // Socket is active again, start receiving data from it. | 44 // Socket is active again, start receiving data from it. |
| 37 void OnSocketResume(const std::string& extension_id, int socket_id); | 45 void OnSocketResume(const std::string& extension_id, int socket_id); |
| 38 | 46 |
| 39 // BrowserContextKeyedAPI implementation. | 47 // BrowserContextKeyedAPI implementation. |
| 40 static BrowserContextKeyedAPIFactory<BluetoothSocketEventDispatcher>* | 48 static BrowserContextKeyedAPIFactory<BluetoothSocketEventDispatcher>* |
| 41 GetFactoryInstance(); | 49 GetFactoryInstance(); |
| 42 | 50 |
| 43 // Convenience method to get the SocketEventDispatcher for a profile. | 51 // Convenience method to get the SocketEventDispatcher for a profile. |
| 44 static BluetoothSocketEventDispatcher* Get(content::BrowserContext* context); | 52 static BluetoothSocketEventDispatcher* Get(content::BrowserContext* context); |
| 45 | 53 |
| 46 private: | 54 private: |
| 47 typedef ApiResourceManager<BluetoothApiSocket>::ApiResourceData SocketData; | 55 typedef ApiResourceManager<BluetoothApiSocket>::ApiResourceData SocketData; |
| 48 friend class BrowserContextKeyedAPIFactory<BluetoothSocketEventDispatcher>; | 56 friend class BrowserContextKeyedAPIFactory<BluetoothSocketEventDispatcher>; |
| 49 // BrowserContextKeyedAPI implementation. | 57 // BrowserContextKeyedAPI implementation. |
| 50 static const char* service_name() { return "BluetoothSocketEventDispatcher"; } | 58 static const char* service_name() { return "BluetoothSocketEventDispatcher"; } |
| 51 static const bool kServiceHasOwnInstanceInIncognito = true; | 59 static const bool kServiceHasOwnInstanceInIncognito = true; |
| 52 static const bool kServiceIsNULLWhileTesting = true; | 60 static const bool kServiceIsNULLWhileTesting = true; |
| 53 | 61 |
| 54 // base::Bind supports methods with up to 6 parameters. ReceiveParams is used | 62 // base::Bind supports methods with up to 6 parameters. SocketParams is used |
| 55 // as a workaround that limitation for invoking StartReceive. | 63 // as a workaround that limitation for invoking StartReceive() and |
| 56 struct ReceiveParams { | 64 // StartAccept(). |
| 57 ReceiveParams(); | 65 struct SocketParams { |
| 58 ~ReceiveParams(); | 66 SocketParams(); |
| 67 ~SocketParams(); |
| 59 | 68 |
| 60 content::BrowserThread::ID thread_id; | 69 content::BrowserThread::ID thread_id; |
| 61 void* browser_context_id; | 70 void* browser_context_id; |
| 62 std::string extension_id; | 71 std::string extension_id; |
| 63 scoped_refptr<SocketData> sockets; | 72 scoped_refptr<SocketData> sockets; |
| 64 int socket_id; | 73 int socket_id; |
| 65 }; | 74 }; |
| 66 | 75 |
| 67 // Start a receive and register a callback. | 76 // Start a receive and register a callback. |
| 68 void StartSocketReceive(const std::string& extension_id, int socket_id); | 77 static void StartReceive(const SocketParams& params); |
| 69 static void StartReceive(const ReceiveParams& params); | |
| 70 | 78 |
| 71 // Called when socket receive data. | 79 // Called when socket receive data. |
| 72 static void ReceiveCallback(const ReceiveParams& params, | 80 static void ReceiveCallback(const SocketParams& params, |
| 73 int bytes_read, | 81 int bytes_read, |
| 74 scoped_refptr<net::IOBuffer> io_buffer); | 82 scoped_refptr<net::IOBuffer> io_buffer); |
| 75 | 83 |
| 76 // Called when socket receive data. | 84 // Called when socket receive data. |
| 77 static void ReceiveErrorCallback(const ReceiveParams& params, | 85 static void ReceiveErrorCallback(const SocketParams& params, |
| 78 BluetoothApiSocket::ErrorReason error_reason, | 86 BluetoothApiSocket::ErrorReason error_reason, |
| 79 const std::string& error); | 87 const std::string& error); |
| 80 | 88 |
| 89 // Start an accept and register a callback. |
| 90 static void StartAccept(const SocketParams& params); |
| 91 |
| 92 // Called when socket accepts a client connection. |
| 93 static void AcceptCallback(const SocketParams& params, |
| 94 const device::BluetoothDevice* device, |
| 95 scoped_refptr<device::BluetoothSocket> socket); |
| 96 |
| 97 // Called when socket encounters an error while accepting a client connection. |
| 98 static void AcceptErrorCallback(const SocketParams& params, |
| 99 BluetoothApiSocket::ErrorReason error_reason, |
| 100 const std::string& error); |
| 101 |
| 81 // Post an extension event from IO to UI thread | 102 // Post an extension event from IO to UI thread |
| 82 static void PostEvent(const ReceiveParams& params, scoped_ptr<Event> event); | 103 static void PostEvent(const SocketParams& params, scoped_ptr<Event> event); |
| 83 | 104 |
| 84 // Dispatch an extension event on to EventRouter instance on UI thread. | 105 // Dispatch an extension event on to EventRouter instance on UI thread. |
| 85 static void DispatchEvent(void* browser_context_id, | 106 static void DispatchEvent(void* browser_context_id, |
| 86 const std::string& extension_id, | 107 const std::string& extension_id, |
| 87 scoped_ptr<Event> event); | 108 scoped_ptr<Event> event); |
| 88 | 109 |
| 89 // Usually FILE thread (except for unit testing). | 110 // Usually FILE thread (except for unit testing). |
| 90 content::BrowserThread::ID thread_id_; | 111 content::BrowserThread::ID thread_id_; |
| 91 content::BrowserContext* const browser_context_; | 112 content::BrowserContext* const browser_context_; |
| 92 scoped_refptr<SocketData> sockets_; | 113 scoped_refptr<SocketData> sockets_; |
| 93 }; | 114 }; |
| 94 | 115 |
| 95 } // namespace api | 116 } // namespace api |
| 96 } // namespace extensions | 117 } // namespace extensions |
| 97 | 118 |
| 98 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT
_DISPATCHER_H_ | 119 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT
_DISPATCHER_H_ |
| OLD | NEW |