Finish refactoring of DomCodeToUsLayoutKeyboardCode().
[chromium-blink-merge.git] / extensions / browser / api / sockets_tcp_server / tcp_server_socket_event_dispatcher.h
blobbddfb8c42c386e7d14e1e2ffc978dbb9f03305f4
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_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT_DISPATCHER_H_
6 #define EXTENSIONS_BROWSER_API_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT_DISPATCHER_H_
8 #include "extensions/browser/api/api_resource_manager.h"
9 #include "extensions/browser/api/sockets_tcp/sockets_tcp_api.h"
10 #include "extensions/browser/api/sockets_tcp_server/sockets_tcp_server_api.h"
12 namespace content {
13 class BrowserContext;
16 namespace extensions {
17 struct Event;
18 class ResumableTCPSocket;
21 namespace extensions {
22 namespace core_api {
24 // Dispatch events related to "sockets.tcp" sockets from callback on native
25 // socket instances. There is one instance per profile.
26 class TCPServerSocketEventDispatcher
27 : public BrowserContextKeyedAPI,
28 public base::SupportsWeakPtr<TCPServerSocketEventDispatcher> {
29 public:
30 explicit TCPServerSocketEventDispatcher(content::BrowserContext* context);
31 ~TCPServerSocketEventDispatcher() override;
33 // Server socket is active, start accepting connections from it.
34 void OnServerSocketListen(const std::string& extension_id, int socket_id);
36 // Server socket is active again, start accepting connections from it.
37 void OnServerSocketResume(const std::string& extension_id, int socket_id);
39 // BrowserContextKeyedAPI implementation.
40 static BrowserContextKeyedAPIFactory<TCPServerSocketEventDispatcher>*
41 GetFactoryInstance();
43 // Convenience method to get the SocketEventDispatcher for a profile.
44 static TCPServerSocketEventDispatcher* Get(content::BrowserContext* context);
46 private:
47 typedef ApiResourceManager<ResumableTCPServerSocket>::ApiResourceData
48 ServerSocketData;
49 typedef ApiResourceManager<ResumableTCPSocket>::ApiResourceData
50 ClientSocketData;
51 friend class BrowserContextKeyedAPIFactory<TCPServerSocketEventDispatcher>;
52 // BrowserContextKeyedAPI implementation.
53 static const char* service_name() { return "TCPServerSocketEventDispatcher"; }
54 static const bool kServiceHasOwnInstanceInIncognito = true;
55 static const bool kServiceIsNULLWhileTesting = true;
57 // base::Bind supports methods with up to 6 parameters. AcceptParams is used
58 // as a workaround that limitation for invoking StartAccept.
59 struct AcceptParams {
60 AcceptParams();
61 ~AcceptParams();
63 content::BrowserThread::ID thread_id;
64 void* browser_context_id;
65 std::string extension_id;
66 scoped_refptr<ServerSocketData> server_sockets;
67 scoped_refptr<ClientSocketData> client_sockets;
68 int socket_id;
71 // Start an accept and register a callback.
72 void StartSocketAccept(const std::string& extension_id, int socket_id);
74 // Start an accept and register a callback.
75 static void StartAccept(const AcceptParams& params);
77 // Called when socket accepts a new connection.
78 static void AcceptCallback(const AcceptParams& params,
79 int result_code,
80 net::TCPClientSocket* socket);
82 // Post an extension event from |thread_id| to UI thread
83 static void PostEvent(const AcceptParams& params, scoped_ptr<Event> event);
85 // Dispatch an extension event on to EventRouter instance on UI thread.
86 static void DispatchEvent(void* browser_context_id,
87 const std::string& extension_id,
88 scoped_ptr<Event> event);
90 // Usually IO thread (except for unit testing).
91 content::BrowserThread::ID thread_id_;
92 content::BrowserContext* const browser_context_;
93 scoped_refptr<ServerSocketData> server_sockets_;
94 scoped_refptr<ClientSocketData> client_sockets_;
97 } // namespace core_api
98 } // namespace extensions
100 #endif // EXTENSIONS_BROWSER_API_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT_DISPATCHER_H_