1 // Copyright 2015 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 PPAPI_PROXY_UDP_SOCKET_FILTER_H_
6 #define PPAPI_PROXY_UDP_SOCKET_FILTER_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/containers/scoped_ptr_hash_map.h"
14 #include "base/memory/ref_counted.h"
15 #include "ppapi/c/ppb_udp_socket.h"
16 #include "ppapi/c/private/ppb_net_address_private.h"
17 #include "ppapi/proxy/plugin_resource.h"
18 #include "ppapi/proxy/ppapi_proxy_export.h"
19 #include "ppapi/proxy/resource_message_filter.h"
20 #include "ppapi/shared_impl/tracked_callback.h"
25 class ResourceMessageReplyParams
;
27 // Handles receiving UDP packets on the IO thread so that when the recipient is
28 // not on the main thread, we can post directly to the appropriate thread.
29 class PPAPI_PROXY_EXPORT UDPSocketFilter
: public ResourceMessageFilter
{
33 // All these are called on whatever thread the plugin wants, while already
34 // holding the ppapi::ProxyLock. The "slot_available_callback" will be invoked
35 // whenever we detect that a slot is now available, so that the client can
36 // take appropriate action (like informing the host we can receive another
37 // buffer). It will always be run with the ProxyLock.
38 void AddUDPResource(PP_Instance instance
,
41 const base::Closure
& slot_available_callback
);
42 void RemoveUDPResource(PP_Resource resource
);
43 // Note, the slot_available_callback that was provided to AddUDPResource may
44 // be invoked during the RequestData call; this gives the client the
45 // opportunity to post a message to the host immediately.
46 int32_t RequestData(PP_Resource resource
,
50 const scoped_refptr
<TrackedCallback
>& callback
);
52 // ResourceMessageFilter implementation.
53 bool OnResourceReplyReceived(const ResourceMessageReplyParams
& reply_params
,
54 const IPC::Message
& nested_msg
) override
;
56 PP_NetAddress_Private
GetLastAddrPrivate(PP_Resource resource
) const;
58 // The maximum number of bytes that each
59 // PpapiPluginMsg_PPBUDPSocket_PushRecvResult message is allowed to carry.
60 static const int32_t kMaxReadSize
;
61 // The maximum number that we allow for setting
62 // PP_UDPSOCKET_OPTION_RECV_BUFFER_SIZE. This number is only for input
63 // argument sanity check, it doesn't mean the browser guarantees to support
64 // such a buffer size.
65 static const int32_t kMaxReceiveBufferSize
;
66 // The maximum number of received packets that we allow instances of this
68 static const size_t kPluginReceiveBufferSlots
;
71 // The queue of received data intended for 1 UDPSocketResourceBase. All usage
72 // must be protected by UDPSocketFilter::lock_.
75 explicit RecvQueue(PP_Instance instance
,
77 const base::Closure
& slot_available_callback
);
80 // Called on the IO thread when data is received. It will post |callback_|
81 // if it's valid, otherwise push the data on buffers_.
82 // The ppapi::ProxyLock should *not* be held, and won't be acquired.
83 void DataReceivedOnIOThread(int32_t result
,
85 const PP_NetAddress_Private
& addr
);
86 // Called on whatever thread the plugin chooses. Must already hold the
87 // PpapiProxyLock. Returns a code from pp_errors.h, or a positive number.
89 // Note, the out-params are owned by the plugin, and if the request can't be
90 // handled immediately, they will be written later just before the callback
92 int32_t RequestData(int32_t num_bytes
,
94 PP_Resource
* addr_out
,
95 const scoped_refptr
<TrackedCallback
>& callback
);
96 PP_NetAddress_Private
GetLastAddrPrivate() const;
102 PP_NetAddress_Private addr
;
104 std::queue
<RecvBuffer
> recv_buffers_
;
106 PP_Instance pp_instance_
;
107 scoped_refptr
<ppapi::TrackedCallback
> recvfrom_callback_
;
109 int32_t bytes_to_read_
;
110 PP_Resource
* recvfrom_addr_resource_
;
111 PP_NetAddress_Private last_recvfrom_addr_
;
113 // Callback to invoke when a UDP receive slot is available.
114 base::Closure slot_available_callback_
;
118 // This is deleted via RefCountedThreadSafe (see ResourceMessageFilter).
120 void OnPluginMsgPushRecvResult(const ResourceMessageReplyParams
& params
,
122 const std::string
& data
,
123 const PP_NetAddress_Private
& addr
);
125 // lock_ protects queues_.
127 // Lock order (if >1 acquired):
128 // 1 ppapi::ProxyLock
129 // \-->2 Filter lock_
130 mutable base::Lock lock_
;
131 base::ScopedPtrHashMap
<PP_Resource
, scoped_ptr
<RecvQueue
>> queues_
;
137 #endif // PPAPI_PROXY_UDP_SOCKET_FILTER_H_