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