1 // Copyright 2013 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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_EVENT_DISPATCHER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_EVENT_DISPATCHER_H_
8 #include "chrome/common/extensions/api/serial.h"
9 #include "extensions/browser/api/api_resource_manager.h"
13 namespace extensions {
16 class SerialConnection;
20 // Per-profile dispatcher for events on serial connections.
21 class SerialEventDispatcher : public BrowserContextKeyedAPI {
23 explicit SerialEventDispatcher(content::BrowserContext* context);
24 virtual ~SerialEventDispatcher();
26 // Start receiving data and firing events for a connection.
27 void PollConnection(const std::string& extension_id, int connection_id);
29 static SerialEventDispatcher* Get(content::BrowserContext* context);
31 // BrowserContextKeyedAPI implementation.
32 static BrowserContextKeyedAPIFactory<SerialEventDispatcher>*
36 typedef ApiResourceManager<SerialConnection>::ApiResourceData ConnectionData;
37 friend class BrowserContextKeyedAPIFactory<SerialEventDispatcher>;
39 // BrowserContextKeyedAPI implementation.
40 static const char* service_name() { return "SerialEventDispatcher"; }
41 static const bool kServiceHasOwnInstanceInIncognito = true;
42 static const bool kServiceIsNULLWhileTesting = true;
44 struct ReceiveParams {
48 content::BrowserThread::ID thread_id;
50 std::string extension_id;
51 scoped_refptr<ConnectionData> connections;
55 static void StartReceive(const ReceiveParams& params);
57 static void ReceiveCallback(const ReceiveParams& params,
58 const std::string& data,
59 serial::ReceiveError error);
61 static void PostEvent(const ReceiveParams& params,
62 scoped_ptr<extensions::Event> event);
64 static void DispatchEvent(void* profile_id,
65 const std::string& extension_id,
66 scoped_ptr<extensions::Event> event);
68 content::BrowserThread::ID thread_id_;
69 Profile* const profile_;
70 scoped_refptr<ConnectionData> connections_;
75 } // namespace extensions
77 #endif // CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_EVENT_DISPATCHER_H_