Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / browser / api / serial / serial_event_dispatcher.h
blob502f57a26d16fad482e2cc93f9b83e94c5af0743
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_
8 #include <string>
9 #include <vector>
11 #include "extensions/browser/api/api_resource_manager.h"
12 #include "extensions/common/api/serial.h"
14 namespace content {
15 class BrowserContext;
18 namespace extensions {
20 struct Event;
21 class SerialConnection;
23 namespace api {
25 // Per-browser-context dispatcher for events on serial connections.
26 class SerialEventDispatcher : public BrowserContextKeyedAPI {
27 public:
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>*
38 GetFactoryInstance();
40 private:
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 {
50 ReceiveParams();
51 ~ReceiveParams();
53 content::BrowserThread::ID thread_id;
54 void* browser_context_id;
55 std::string extension_id;
56 scoped_refptr<ConnectionData> connections;
57 int connection_id;
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 api
80 } // namespace extensions
82 #endif // EXTENSIONS_BROWSER_API_SERIAL_SERIAL_EVENT_DISPATCHER_H_