OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef PPAPI_PROXY_UDP_SOCKET_FILTER_H_ |
| 6 #define PPAPI_PROXY_UDP_SOCKET_FILTER_H_ |
| 7 |
| 8 #include <queue> |
| 9 #include <string> |
| 10 |
| 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" |
| 21 |
| 22 namespace ppapi { |
| 23 namespace proxy { |
| 24 |
| 25 class ResourceMessageReplyParams; |
| 26 |
| 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 { |
| 30 public: |
| 31 UDPSocketFilter(); |
| 32 |
| 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 // on the IO thread without the ProxyLock, so that callback is responsible |
| 36 // for posting to another thread and acquiring the ProxyLock if necessary. |
| 37 void AddUDPResource(PP_Instance instance, |
| 38 PP_Resource resource, |
| 39 bool private_api, |
| 40 const base::Closure& slot_available_callback); |
| 41 void RemoveUDPResource(PP_Resource resource); |
| 42 int32_t RequestData(PP_Resource resource, |
| 43 int32_t num_bytes, |
| 44 char* buffer, |
| 45 PP_Resource* addr, |
| 46 const scoped_refptr<TrackedCallback>& callback); |
| 47 |
| 48 // ResourceMessageFilter implementation. |
| 49 bool OnResourceReplyReceived(const ResourceMessageReplyParams& reply_params, |
| 50 const IPC::Message& nested_msg) override; |
| 51 |
| 52 PP_NetAddress_Private GetLastAddrPrivate(PP_Resource resource) const; |
| 53 |
| 54 // The maximum number of bytes that each |
| 55 // PpapiPluginMsg_PPBUDPSocket_PushRecvResult message is allowed to carry. |
| 56 static const int32_t kMaxReadSize; |
| 57 // The maximum number that we allow for setting |
| 58 // PP_UDPSOCKET_OPTION_RECV_BUFFER_SIZE. This number is only for input |
| 59 // argument sanity check, it doesn't mean the browser guarantees to support |
| 60 // such a buffer size. |
| 61 static const int32_t kMaxReceiveBufferSize; |
| 62 // The maximum number of received packets that we allow instances of this |
| 63 // class to buffer. |
| 64 static const size_t kPluginReceiveBufferSlots; |
| 65 |
| 66 private: |
| 67 // The queue of received data intended for 1 UDPSocketResourceBase. All usage |
| 68 // must be protected by UDPSocketFilter::lock_. |
| 69 class RecvQueue { |
| 70 public: |
| 71 explicit RecvQueue(PP_Instance instance, |
| 72 bool private_api, |
| 73 const base::Closure& slot_available_callback); |
| 74 ~RecvQueue(); |
| 75 |
| 76 // Called on the IO thread when data is received. It will post |callback_| |
| 77 // if it's valid, otherwise push the data on buffers_. |
| 78 // The ppapi::ProxyLock should *not* be held, and won't be acquired. |
| 79 void DataReceivedOnIOThread(int32_t result, |
| 80 const std::string& d, |
| 81 const PP_NetAddress_Private& addr); |
| 82 // Called on whatever thread the plugin chooses. Must already hold the |
| 83 // PpapiProxyLock. Returns a code from pp_errors.h, or a positive number. |
| 84 // |
| 85 // Note, the out-params are owned by the plugin, and if the request can't be |
| 86 // handled immediately, they will be written later just before the callback |
| 87 // is invoked. |
| 88 int32_t RequestData(int32_t num_bytes, |
| 89 char* buffer_out, |
| 90 PP_Resource* addr_out, |
| 91 const scoped_refptr<TrackedCallback>& callback); |
| 92 PP_NetAddress_Private GetLastAddrPrivate() const; |
| 93 |
| 94 private: |
| 95 struct RecvBuffer { |
| 96 int32_t result; |
| 97 std::string data; |
| 98 PP_NetAddress_Private addr; |
| 99 }; |
| 100 std::queue<RecvBuffer> recv_buffers_; |
| 101 |
| 102 PP_Instance pp_instance_; |
| 103 scoped_refptr<ppapi::TrackedCallback> recvfrom_callback_; |
| 104 char* read_buffer_; |
| 105 int32_t bytes_to_read_; |
| 106 PP_Resource* recvfrom_addr_resource_; |
| 107 PP_NetAddress_Private last_recvfrom_addr_; |
| 108 bool private_api_; |
| 109 // Callback to invoke when a UDP receive slot is available. |
| 110 base::Closure slot_available_callback_; |
| 111 }; |
| 112 |
| 113 private: |
| 114 // This is deleted via RefCountedThreadSafe (see ResourceMessageFilter). |
| 115 ~UDPSocketFilter(); |
| 116 void OnPluginMsgPushRecvResult(const ResourceMessageReplyParams& params, |
| 117 int32_t result, |
| 118 const std::string& data, |
| 119 const PP_NetAddress_Private& addr); |
| 120 |
| 121 // lock_ protects queues_. |
| 122 // |
| 123 // Lock order (if >1 acquired): |
| 124 // 1 ppapi::ProxyLock |
| 125 // \-->2 Filter lock_ |
| 126 mutable base::Lock lock_; |
| 127 base::ScopedPtrHashMap<PP_Resource, RecvQueue> queues_; |
| 128 }; |
| 129 |
| 130 } // namespace proxy |
| 131 } // namespace ppapi |
| 132 |
| 133 #endif // PPAPI_PROXY_UDP_SOCKET_FILTER_H_ |
OLD | NEW |