- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / pepper / pepper_tcp_server_socket_message_filter.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 CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_TCP_SERVER_SOCKET_MESSAGE_FILTER_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_TCP_SERVER_SOCKET_MESSAGE_FILTER_H_
7
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "content/common/content_export.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/socket/tcp_socket.h"
15 #include "ppapi/c/pp_instance.h"
16 #include "ppapi/host/resource_message_filter.h"
17
18 struct PP_NetAddress_Private;
19
20 namespace ppapi {
21 namespace host {
22 class PpapiHost;
23 }
24 }
25
26 namespace content {
27
28 class BrowserPpapiHostImpl;
29 class ContentBrowserPepperHostFactory;
30
31 // TODO(yzshen): Remove this class entirely and let
32 // TCPServerSocketPrivateResource inherit TCPSocketResourceBase.
33 class CONTENT_EXPORT PepperTCPServerSocketMessageFilter
34     : public ppapi::host::ResourceMessageFilter {
35  public:
36   PepperTCPServerSocketMessageFilter(
37       ContentBrowserPepperHostFactory* factory,
38       BrowserPpapiHostImpl* host,
39       PP_Instance instance,
40       bool private_api);
41
42   static size_t GetNumInstances();
43
44  protected:
45   virtual ~PepperTCPServerSocketMessageFilter();
46
47  private:
48   enum State {
49     STATE_BEFORE_LISTENING,
50     STATE_LISTEN_IN_PROGRESS,
51     STATE_LISTENING,
52     STATE_ACCEPT_IN_PROGRESS,
53     STATE_CLOSED
54   };
55
56   // ppapi::host::ResourceMessageFilter overrides.
57   virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage(
58       const IPC::Message& message) OVERRIDE;
59   virtual int32_t OnResourceMessageReceived(
60       const IPC::Message& msg,
61       ppapi::host::HostMessageContext* context) OVERRIDE;
62
63   int32_t OnMsgListen(const ppapi::host::HostMessageContext* context,
64                       const PP_NetAddress_Private& addr,
65                       int32_t backlog);
66   int32_t OnMsgAccept(const ppapi::host::HostMessageContext* context);
67   int32_t OnMsgStopListening(const ppapi::host::HostMessageContext* context);
68
69   void DoListen(const ppapi::host::ReplyMessageContext& context,
70                 const PP_NetAddress_Private& addr,
71                 int32_t backlog);
72
73   void OnListenCompleted(const ppapi::host::ReplyMessageContext& context,
74                          int net_result);
75   void OnAcceptCompleted(const ppapi::host::ReplyMessageContext& context,
76                          int net_result);
77
78   void SendListenReply(const ppapi::host::ReplyMessageContext& context,
79                        int32_t pp_result,
80                        const PP_NetAddress_Private& local_addr);
81   void SendListenError(const ppapi::host::ReplyMessageContext& context,
82                        int32_t pp_result);
83   void SendAcceptReply(const ppapi::host::ReplyMessageContext& context,
84                        int32_t pp_result,
85                        int pending_resource_id,
86                        const PP_NetAddress_Private& local_addr,
87                        const PP_NetAddress_Private& remote_addr);
88   void SendAcceptError(const ppapi::host::ReplyMessageContext& context,
89                        int32_t pp_result);
90
91   // Following fields are initialized and used only on the IO thread.
92   // Non-owning ptr.
93   ppapi::host::PpapiHost* ppapi_host_;
94   // Non-owning ptr.
95   ContentBrowserPepperHostFactory* factory_;
96   PP_Instance instance_;
97
98   State state_;
99   scoped_ptr<net::TCPSocket> socket_;
100   scoped_ptr<net::TCPSocket> accepted_socket_;
101   net::IPEndPoint accepted_address_;
102
103   // Following fields are initialized on the IO thread but used only
104   // on the UI thread.
105   const bool external_plugin_;
106   const bool private_api_;
107   int render_process_id_;
108   int render_view_id_;
109
110   DISALLOW_COPY_AND_ASSIGN(PepperTCPServerSocketMessageFilter);
111 };
112
113 }  // namespace content
114
115 #endif  // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_TCP_SERVER_SOCKET_MESSAGE_FILTER_H_