- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / sockets_tcp_server / tcp_server_socket_event_dispatcher.h
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.
4
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_
7
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"
11
12 namespace extensions {
13 struct Event;
14 class ResumableTCPSocket;
15 }
16
17 namespace extensions {
18 namespace api {
19
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> {
25  public:
26   explicit TCPServerSocketEventDispatcher(Profile* profile);
27   virtual ~TCPServerSocketEventDispatcher();
28
29   // Server socket is active, start accepting connections from it.
30   void OnServerSocketListen(const std::string& extension_id, int socket_id);
31
32   // Server socket is active again, start accepting connections from it.
33   void OnServerSocketResume(const std::string& extension_id, int socket_id);
34
35   // ProfileKeyedAPI implementation.
36   static ProfileKeyedAPIFactory<TCPServerSocketEventDispatcher>*
37       GetFactoryInstance();
38
39   // Convenience method to get the SocketEventDispatcher for a profile.
40   static TCPServerSocketEventDispatcher* Get(Profile* profile);
41
42  private:
43   typedef ApiResourceManager<ResumableTCPServerSocket>::ApiResourceData
44       ServerSocketData;
45   typedef ApiResourceManager<ResumableTCPSocket>::ApiResourceData
46       ClientSocketData;
47   friend class ProfileKeyedAPIFactory<TCPServerSocketEventDispatcher>;
48   // ProfileKeyedAPI implementation.
49   static const char* service_name() {
50     return "TCPServerSocketEventDispatcher";
51   }
52   static const bool kServiceHasOwnInstanceInIncognito = true;
53   static const bool kServiceIsNULLWhileTesting = true;
54
55   // base::Bind supports methods with up to 6 parameters. AcceptParams is used
56   // as a workaround that limitation for invoking StartAccept.
57   struct AcceptParams {
58     AcceptParams();
59     ~AcceptParams();
60
61     content::BrowserThread::ID thread_id;
62     void* profile_id;
63     std::string extension_id;
64     scoped_refptr<ServerSocketData> server_sockets;
65     scoped_refptr<ClientSocketData> client_sockets;
66     int socket_id;
67   };
68
69   // Start an accept and register a callback.
70   void StartSocketAccept(const std::string& extension_id, int socket_id);
71
72   // Start an accept and register a callback.
73   static void StartAccept(const AcceptParams& params);
74
75   // Called when socket accepts a new connection.
76   static void AcceptCallback(const AcceptParams& params,
77                              int result_code,
78                              net::TCPClientSocket *socket);
79
80   // Post an extension event from |thread_id| to UI thread
81   static void PostEvent(const AcceptParams& params,
82                         scoped_ptr<Event> event);
83
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);
88
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_;
94 };
95
96 }  // namespace api
97 }  // namespace extensions
98
99 #endif  // CHROME_BROWSER_EXTENSIONS_API_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT_DISPATCHER_H_