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 CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_UDP_SOCKET_MESSAGE_FILTER_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_UDP_SOCKET_MESSAGE_FILTER_H_
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"
30 struct PP_NetAddress_Private;
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)
39 class IOBufferWithSize;
44 class SocketOptionData;
47 struct ReplyMessageContext;
50 #if defined(TIZEN_PEPPER_EXTENSIONS)
52 struct SerializedHandle;
54 #endif // defined(TIZEN_PEPPER_EXTENSIONS)
59 class BrowserPpapiHostImpl;
61 class CONTENT_EXPORT PepperUDPSocketMessageFilter
62 : public ppapi::host::ResourceMessageFilter {
64 PepperUDPSocketMessageFilter(BrowserPpapiHostImpl* host,
68 static size_t GetNumInstances();
71 ~PepperUDPSocketMessageFilter() override;
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
84 PendingSend(const net::IPAddress& address,
86 const scoped_refptr<net::IOBufferWithSize>& buffer,
87 const ppapi::host::ReplyMessageContext& context);
88 PendingSend(const PendingSend& other);
91 net::IPAddress address;
93 scoped_refptr<net::IOBufferWithSize> buffer;
94 ppapi::host::ReplyMessageContext context;
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;
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);
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)
132 void DoSendTo(const ppapi::host::ReplyMessageContext& context,
133 const std::string& data,
134 const PP_NetAddress_Private& addr);
135 int StartPendingSend();
138 void OnRecvFromCompleted(int net_result);
139 void OnSendToCompleted(int net_result);
140 void FinishPendingSend(int net_result);
142 void SendBindReply(const ppapi::host::ReplyMessageContext& context,
144 #if defined(TIZEN_PEPPER_EXTENSIONS)
145 const ppapi::proxy::SerializedHandle& handle,
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,
153 int32_t bytes_written);
155 void SendBindError(const ppapi::host::ReplyMessageContext& context,
157 void SendRecvFromError(int32_t result);
158 void SendSendToError(const ppapi::host::ReplyMessageContext& context,
161 int32_t CanUseMulticastAPI(const PP_NetAddress_Private& addr);
163 // Bitwise-or of SocketOption flags. This stores the state about whether
164 // each option is set before Bind() is called.
167 // Locally cached value of buffer size.
168 int32_t rcvbuf_size_;
169 int32_t sndbuf_size_;
171 // Multicast options, if socket hasn't been bound
173 int32_t can_use_multicast_;
175 std::unique_ptr<net::UDPSocket> socket_;
177 #if defined(OS_CHROMEOS)
178 std::unique_ptr<chromeos::FirewallHole,
179 content::BrowserThread::DeleteOnUIThread>
181 #endif // defined(OS_CHROMEOS)
183 scoped_refptr<net::IOBuffer> recvfrom_buffer_;
185 std::queue<PendingSend> pending_sends_;
187 net::IPEndPoint recvfrom_address_;
189 size_t remaining_recv_slots_;
191 bool external_plugin_;
194 int render_process_id_;
195 int render_frame_id_;
197 const bool is_potentially_secure_plugin_context_;
199 DISALLOW_COPY_AND_ASSIGN(PepperUDPSocketMessageFilter);
202 } // namespace content
204 #endif // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_UDP_SOCKET_MESSAGE_FILTER_H_