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.
5 #ifndef EXTENSIONS_BROWSER_API_SERIAL_SERIAL_EVENT_DISPATCHER_H_
6 #define EXTENSIONS_BROWSER_API_SERIAL_SERIAL_EVENT_DISPATCHER_H_
11 #include "extensions/browser/api/api_resource_manager.h"
12 #include "extensions/common/api/serial.h"
18 namespace extensions
{
21 class SerialConnection
;
25 // Per-browser-context dispatcher for events on serial connections.
26 class SerialEventDispatcher
: public BrowserContextKeyedAPI
{
28 explicit SerialEventDispatcher(content::BrowserContext
* context
);
29 ~SerialEventDispatcher() override
;
31 // Start receiving data and firing events for a connection.
32 void PollConnection(const std::string
& extension_id
, int connection_id
);
34 static SerialEventDispatcher
* Get(content::BrowserContext
* context
);
36 // BrowserContextKeyedAPI implementation.
37 static BrowserContextKeyedAPIFactory
<SerialEventDispatcher
>*
41 typedef ApiResourceManager
<SerialConnection
>::ApiResourceData ConnectionData
;
42 friend class BrowserContextKeyedAPIFactory
<SerialEventDispatcher
>;
44 // BrowserContextKeyedAPI implementation.
45 static const char* service_name() { return "SerialEventDispatcher"; }
46 static const bool kServiceHasOwnInstanceInIncognito
= true;
47 static const bool kServiceIsNULLWhileTesting
= true;
49 struct ReceiveParams
{
53 content::BrowserThread::ID thread_id
;
54 void* browser_context_id
;
55 std::string extension_id
;
56 scoped_refptr
<ConnectionData
> connections
;
60 static void StartReceive(const ReceiveParams
& params
);
62 static void ReceiveCallback(const ReceiveParams
& params
,
63 const std::vector
<char>& data
,
64 serial::ReceiveError error
);
66 static void PostEvent(const ReceiveParams
& params
,
67 scoped_ptr
<extensions::Event
> event
);
69 static void DispatchEvent(void* browser_context_id
,
70 const std::string
& extension_id
,
71 scoped_ptr
<extensions::Event
> event
);
73 content::BrowserThread::ID thread_id_
;
74 content::BrowserContext
* const context_
;
75 scoped_refptr
<ConnectionData
> connections_
;
78 } // namespace core_api
80 } // namespace extensions
82 #endif // EXTENSIONS_BROWSER_API_SERIAL_SERIAL_EVENT_DISPATCHER_H_