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_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT_DISPATCHER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT_DISPATCHER_H_
8 #include "chrome/browser/extensions/api/api_resource_manager.h"
9 #include "chrome/browser/extensions/api/sockets_tcp/sockets_tcp_api.h"
10 #include "chrome/browser/extensions/api/sockets_tcp_server/sockets_tcp_server_api.h"
12 namespace extensions {
14 class ResumableTCPSocket;
17 namespace extensions {
20 // Dispatch events related to "sockets.tcp" sockets from callback on native
21 // socket instances. There is one instance per profile.
22 class TCPServerSocketEventDispatcher
23 : public ProfileKeyedAPI,
24 public base::SupportsWeakPtr<TCPServerSocketEventDispatcher> {
26 explicit TCPServerSocketEventDispatcher(Profile* profile);
27 virtual ~TCPServerSocketEventDispatcher();
29 // Server socket is active, start accepting connections from it.
30 void OnServerSocketListen(const std::string& extension_id, int socket_id);
32 // Server socket is active again, start accepting connections from it.
33 void OnServerSocketResume(const std::string& extension_id, int socket_id);
35 // ProfileKeyedAPI implementation.
36 static ProfileKeyedAPIFactory<TCPServerSocketEventDispatcher>*
39 // Convenience method to get the SocketEventDispatcher for a profile.
40 static TCPServerSocketEventDispatcher* Get(Profile* profile);
43 typedef ApiResourceManager<ResumableTCPServerSocket>::ApiResourceData
45 typedef ApiResourceManager<ResumableTCPSocket>::ApiResourceData
47 friend class ProfileKeyedAPIFactory<TCPServerSocketEventDispatcher>;
48 // ProfileKeyedAPI implementation.
49 static const char* service_name() {
50 return "TCPServerSocketEventDispatcher";
52 static const bool kServiceHasOwnInstanceInIncognito = true;
53 static const bool kServiceIsNULLWhileTesting = true;
55 // base::Bind supports methods with up to 6 parameters. AcceptParams is used
56 // as a workaround that limitation for invoking StartAccept.
61 content::BrowserThread::ID thread_id;
63 std::string extension_id;
64 scoped_refptr<ServerSocketData> server_sockets;
65 scoped_refptr<ClientSocketData> client_sockets;
69 // Start an accept and register a callback.
70 void StartSocketAccept(const std::string& extension_id, int socket_id);
72 // Start an accept and register a callback.
73 static void StartAccept(const AcceptParams& params);
75 // Called when socket accepts a new connection.
76 static void AcceptCallback(const AcceptParams& params,
78 net::TCPClientSocket *socket);
80 // Post an extension event from |thread_id| to UI thread
81 static void PostEvent(const AcceptParams& params,
82 scoped_ptr<Event> event);
84 // Dispatch an extension event on to EventRouter instance on UI thread.
85 static void DispatchEvent(void* profile_id,
86 const std::string& extension_id,
87 scoped_ptr<Event> event);
89 // Usually IO thread (except for unit testing).
90 content::BrowserThread::ID thread_id_;
91 Profile* const profile_;
92 scoped_refptr<ServerSocketData> server_sockets_;
93 scoped_refptr<ClientSocketData> client_sockets_;
97 } // namespace extensions
99 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT_DISPATCHER_H_