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