c92f1b305e58d65d10cbc4ce31377c1d439a183b
[platform/framework/web/crosswalk-tizen.git] /
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_UDP_SOCKET_MESSAGE_FILTER_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_UDP_SOCKET_MESSAGE_FILTER_H_
7
8 #include <stddef.h>
9
10 #include <memory>
11 #include <queue>
12 #include <string>
13
14 #include "base/callback.h"
15 #include "base/compiler_specific.h"
16 #include "base/macros.h"
17 #include "base/memory/ref_counted.h"
18 #include "build/build_config.h"
19 #include "content/common/content_export.h"
20 #include "content/public/common/process_type.h"
21 #include "net/base/completion_callback.h"
22 #include "net/base/ip_address.h"
23 #include "net/base/ip_endpoint.h"
24 #include "net/socket/udp_socket.h"
25 #include "ppapi/c/pp_instance.h"
26 #include "ppapi/c/pp_stdint.h"
27 #include "ppapi/c/ppb_udp_socket.h"
28 #include "ppapi/host/resource_message_filter.h"
29
30 struct PP_NetAddress_Private;
31
32 #if defined(OS_CHROMEOS)
33 #include "chromeos/network/firewall_hole.h"
34 #include "content/public/browser/browser_thread.h"
35 #endif  // defined(OS_CHROMEOS)
36
37 namespace net {
38 class IOBuffer;
39 class IOBufferWithSize;
40 }
41
42 namespace ppapi {
43
44 class SocketOptionData;
45
46 namespace host {
47 struct ReplyMessageContext;
48 }
49
50 #if defined(TIZEN_PEPPER_EXTENSIONS)
51 namespace proxy {
52 struct SerializedHandle;
53 }
54 #endif  // defined(TIZEN_PEPPER_EXTENSIONS)
55 }
56
57 namespace content {
58
59 class BrowserPpapiHostImpl;
60
61 class CONTENT_EXPORT PepperUDPSocketMessageFilter
62     : public ppapi::host::ResourceMessageFilter {
63  public:
64   PepperUDPSocketMessageFilter(BrowserPpapiHostImpl* host,
65                                PP_Instance instance,
66                                bool private_api);
67
68   static size_t GetNumInstances();
69
70  protected:
71   ~PepperUDPSocketMessageFilter() override;
72
73  private:
74   enum SocketOption {
75     SOCKET_OPTION_ADDRESS_REUSE = 1 << 0,
76     SOCKET_OPTION_BROADCAST = 1 << 1,
77     SOCKET_OPTION_RCVBUF_SIZE = 1 << 2,
78     SOCKET_OPTION_SNDBUF_SIZE = 1 << 3,
79     SOCKET_OPTION_MULTICAST_LOOP = 1 << 4,
80     SOCKET_OPTION_MULTICAST_TTL = 1 << 5
81   };
82
83   struct PendingSend {
84     PendingSend(const net::IPAddress& address,
85                 int port,
86                 const scoped_refptr<net::IOBufferWithSize>& buffer,
87                 const ppapi::host::ReplyMessageContext& context);
88     PendingSend(const PendingSend& other);
89     ~PendingSend();
90
91     net::IPAddress address;
92     int port;
93     scoped_refptr<net::IOBufferWithSize> buffer;
94     ppapi::host::ReplyMessageContext context;
95   };
96
97   // ppapi::host::ResourceMessageFilter overrides.
98   scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage(
99       const IPC::Message& message) override;
100   int32_t OnResourceMessageReceived(
101       const IPC::Message& msg,
102       ppapi::host::HostMessageContext* context) override;
103
104   int32_t OnMsgSetOption(const ppapi::host::HostMessageContext* context,
105                          PP_UDPSocket_Option name,
106                          const ppapi::SocketOptionData& value);
107   int32_t OnMsgBind(const ppapi::host::HostMessageContext* context,
108                     const PP_NetAddress_Private& addr);
109   int32_t OnMsgSendTo(const ppapi::host::HostMessageContext* context,
110                       const std::string& data,
111                       const PP_NetAddress_Private& addr);
112   int32_t OnMsgClose(const ppapi::host::HostMessageContext* context);
113   int32_t OnMsgRecvSlotAvailable(
114       const ppapi::host::HostMessageContext* context);
115   int32_t OnMsgJoinGroup(const ppapi::host::HostMessageContext* context,
116                          const PP_NetAddress_Private& addr);
117   int32_t OnMsgLeaveGroup(const ppapi::host::HostMessageContext* context,
118                           const PP_NetAddress_Private& addr);
119
120   void DoBind(const ppapi::host::ReplyMessageContext& context,
121               const PP_NetAddress_Private& addr);
122   void OnBindComplete(std::unique_ptr<net::UDPSocket> socket,
123                       const ppapi::host::ReplyMessageContext& context,
124                       const PP_NetAddress_Private& net_address);
125 #if defined(OS_CHROMEOS)
126   void OpenFirewallHole(const net::IPEndPoint& local_address,
127                         base::Closure bind_complete);
128   void OnFirewallHoleOpened(base::Closure bind_complete,
129                             std::unique_ptr<chromeos::FirewallHole> hole);
130 #endif  // defined(OS_CHROMEOS)
131   void DoRecvFrom();
132   void DoSendTo(const ppapi::host::ReplyMessageContext& context,
133                 const std::string& data,
134                 const PP_NetAddress_Private& addr);
135   int StartPendingSend();
136   void Close();
137
138   void OnRecvFromCompleted(int net_result);
139   void OnSendToCompleted(int net_result);
140   void FinishPendingSend(int net_result);
141
142   void SendBindReply(const ppapi::host::ReplyMessageContext& context,
143                      int32_t result,
144 #if defined(TIZEN_PEPPER_EXTENSIONS)
145                      const ppapi::proxy::SerializedHandle& handle,
146 #endif
147                      const PP_NetAddress_Private& addr);
148   void SendRecvFromResult(int32_t result,
149                           const std::string& data,
150                           const PP_NetAddress_Private& addr);
151   void SendSendToReply(const ppapi::host::ReplyMessageContext& context,
152                        int32_t result,
153                        int32_t bytes_written);
154
155   void SendBindError(const ppapi::host::ReplyMessageContext& context,
156                      int32_t result);
157   void SendRecvFromError(int32_t result);
158   void SendSendToError(const ppapi::host::ReplyMessageContext& context,
159                        int32_t result);
160
161   int32_t CanUseMulticastAPI(const PP_NetAddress_Private& addr);
162
163   // Bitwise-or of SocketOption flags. This stores the state about whether
164   // each option is set before Bind() is called.
165   int socket_options_;
166
167   // Locally cached value of buffer size.
168   int32_t rcvbuf_size_;
169   int32_t sndbuf_size_;
170
171   // Multicast options, if socket hasn't been bound
172   int multicast_ttl_;
173   int32_t can_use_multicast_;
174
175   std::unique_ptr<net::UDPSocket> socket_;
176   bool closed_;
177 #if defined(OS_CHROMEOS)
178   std::unique_ptr<chromeos::FirewallHole,
179                   content::BrowserThread::DeleteOnUIThread>
180       firewall_hole_;
181 #endif  // defined(OS_CHROMEOS)
182
183   scoped_refptr<net::IOBuffer> recvfrom_buffer_;
184
185   std::queue<PendingSend> pending_sends_;
186
187   net::IPEndPoint recvfrom_address_;
188
189   size_t remaining_recv_slots_;
190
191   bool external_plugin_;
192   bool private_api_;
193
194   int render_process_id_;
195   int render_frame_id_;
196
197   const bool is_potentially_secure_plugin_context_;
198
199   DISALLOW_COPY_AND_ASSIGN(PepperUDPSocketMessageFilter);
200 };
201
202 }  // namespace content
203
204 #endif  // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_UDP_SOCKET_MESSAGE_FILTER_H_