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 PPAPI_PROXY_UDP_SOCKET_RESOURCE_BASE_H_ 6 #define PPAPI_PROXY_UDP_SOCKET_RESOURCE_BASE_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/compiler_specific.h" 12 #include "base/memory/ref_counted.h" 13 #include "ppapi/c/ppb_udp_socket.h" 14 #include "ppapi/c/private/ppb_net_address_private.h" 15 #include "ppapi/proxy/plugin_resource.h" 16 #include "ppapi/proxy/ppapi_proxy_export.h" 17 #include "ppapi/shared_impl/tracked_callback.h" 18 19 namespace ppapi { 20 namespace proxy { 21 22 class ResourceMessageReplyParams; 23 24 class PPAPI_PROXY_EXPORT UDPSocketResourceBase: public PluginResource { 25 public: 26 // The maximum number of bytes that each PpapiHostMsg_PPBUDPSocket_RecvFrom 27 // message is allowed to request. 28 static const int32_t kMaxReadSize; 29 // The maximum number of bytes that each PpapiHostMsg_PPBUDPSocket_SendTo 30 // message is allowed to carry. 31 static const int32_t kMaxWriteSize; 32 33 // The maximum number that we allow for setting 34 // PP_UDPSOCKET_OPTION_SEND_BUFFER_SIZE. This number is only for input 35 // argument sanity check, it doesn't mean the browser guarantees to support 36 // such a buffer size. 37 static const int32_t kMaxSendBufferSize; 38 // The maximum number that we allow for setting 39 // PP_UDPSOCKET_OPTION_RECV_BUFFER_SIZE. This number is only for input 40 // argument sanity check, it doesn't mean the browser guarantees to support 41 // such a buffer size. 42 static const int32_t kMaxReceiveBufferSize; 43 44 protected: 45 UDPSocketResourceBase(Connection connection, 46 PP_Instance instance, 47 bool private_api); 48 virtual ~UDPSocketResourceBase(); 49 50 int32_t SetOptionImpl(PP_UDPSocket_Option name, 51 const PP_Var& value, 52 scoped_refptr<TrackedCallback> callback); 53 int32_t BindImpl(const PP_NetAddress_Private* addr, 54 scoped_refptr<TrackedCallback> callback); 55 PP_Bool GetBoundAddressImpl(PP_NetAddress_Private* addr); 56 // |addr| could be NULL to indicate that an output value is not needed. 57 int32_t RecvFromImpl(char* buffer, 58 int32_t num_bytes, 59 PP_Resource* addr, 60 scoped_refptr<TrackedCallback> callback); 61 PP_Bool GetRecvFromAddressImpl(PP_NetAddress_Private* addr); 62 int32_t SendToImpl(const char* buffer, 63 int32_t num_bytes, 64 const PP_NetAddress_Private* addr, 65 scoped_refptr<TrackedCallback> callback); 66 void CloseImpl(); 67 68 private: 69 void PostAbortIfNecessary(scoped_refptr<TrackedCallback>* callback); 70 71 // IPC message handlers. 72 void OnPluginMsgSetOptionReply(scoped_refptr<TrackedCallback> callback, 73 const ResourceMessageReplyParams& params); 74 void OnPluginMsgBindReply(const ResourceMessageReplyParams& params, 75 const PP_NetAddress_Private& bound_addr); 76 void OnPluginMsgRecvFromReply(PP_Resource* output_addr, 77 const ResourceMessageReplyParams& params, 78 const std::string& data, 79 const PP_NetAddress_Private& addr); 80 void OnPluginMsgSendToReply(const ResourceMessageReplyParams& params, 81 int32_t bytes_written); 82 83 bool private_api_; 84 bool bound_; 85 bool closed_; 86 87 scoped_refptr<TrackedCallback> bind_callback_; 88 scoped_refptr<TrackedCallback> recvfrom_callback_; 89 scoped_refptr<TrackedCallback> sendto_callback_; 90 91 char* read_buffer_; 92 int32_t bytes_to_read_; 93 94 PP_NetAddress_Private recvfrom_addr_; 95 PP_NetAddress_Private bound_addr_; 96 97 DISALLOW_COPY_AND_ASSIGN(UDPSocketResourceBase); 98 }; 99 100 } // namespace proxy 101 } // namespace ppapi 102 103 #endif // PPAPI_PROXY_UDP_SOCKET_RESOURCE_BASE_H_ 104