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 EXTENSIONS_BROWSER_API_SERIAL_SERIAL_EVENT_DISPATCHER_H_ 6 #define EXTENSIONS_BROWSER_API_SERIAL_SERIAL_EVENT_DISPATCHER_H_ 7 8 #include <string> 9 10 #include "extensions/browser/api/api_resource_manager.h" 11 #include "extensions/common/api/serial.h" 12 13 namespace content { 14 class BrowserContext; 15 } 16 17 namespace extensions { 18 19 struct Event; 20 class SerialConnection; 21 22 namespace core_api { 23 24 // Per-browser-context dispatcher for events on serial connections. 25 class SerialEventDispatcher : public BrowserContextKeyedAPI { 26 public: 27 explicit SerialEventDispatcher(content::BrowserContext* context); 28 virtual ~SerialEventDispatcher(); 29 30 // Start receiving data and firing events for a connection. 31 void PollConnection(const std::string& extension_id, int connection_id); 32 33 static SerialEventDispatcher* Get(content::BrowserContext* context); 34 35 // BrowserContextKeyedAPI implementation. 36 static BrowserContextKeyedAPIFactory<SerialEventDispatcher>* 37 GetFactoryInstance(); 38 39 private: 40 typedef ApiResourceManager<SerialConnection>::ApiResourceData ConnectionData; 41 friend class BrowserContextKeyedAPIFactory<SerialEventDispatcher>; 42 43 // BrowserContextKeyedAPI implementation. 44 static const char* service_name() { return "SerialEventDispatcher"; } 45 static const bool kServiceHasOwnInstanceInIncognito = true; 46 static const bool kServiceIsNULLWhileTesting = true; 47 48 struct ReceiveParams { 49 ReceiveParams(); 50 ~ReceiveParams(); 51 52 content::BrowserThread::ID thread_id; 53 void* browser_context_id; 54 std::string extension_id; 55 scoped_refptr<ConnectionData> connections; 56 int connection_id; 57 }; 58 59 static void StartReceive(const ReceiveParams& params); 60 61 static void ReceiveCallback(const ReceiveParams& params, 62 const std::string& data, 63 serial::ReceiveError error); 64 65 static void PostEvent(const ReceiveParams& params, 66 scoped_ptr<extensions::Event> event); 67 68 static void DispatchEvent(void* browser_context_id, 69 const std::string& extension_id, 70 scoped_ptr<extensions::Event> event); 71 72 content::BrowserThread::ID thread_id_; 73 content::BrowserContext* const context_; 74 scoped_refptr<ConnectionData> connections_; 75 }; 76 77 } // namespace core_api 78 79 } // namespace extensions 80 81 #endif // EXTENSIONS_BROWSER_API_SERIAL_SERIAL_EVENT_DISPATCHER_H_ 82