OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PPAPI_PROXY_UDP_SOCKET_RESOURCE_BASE_H_ | 5 #ifndef PPAPI_PROXY_UDP_SOCKET_RESOURCE_BASE_H_ |
6 #define PPAPI_PROXY_UDP_SOCKET_RESOURCE_BASE_H_ | 6 #define PPAPI_PROXY_UDP_SOCKET_RESOURCE_BASE_H_ |
7 | 7 |
| 8 #include <queue> |
8 #include <string> | 9 #include <string> |
9 | 10 |
10 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 #include "ppapi/c/ppb_udp_socket.h" | 14 #include "ppapi/c/ppb_udp_socket.h" |
14 #include "ppapi/c/private/ppb_net_address_private.h" | 15 #include "ppapi/c/private/ppb_net_address_private.h" |
15 #include "ppapi/proxy/plugin_resource.h" | 16 #include "ppapi/proxy/plugin_resource.h" |
16 #include "ppapi/proxy/ppapi_proxy_export.h" | 17 #include "ppapi/proxy/ppapi_proxy_export.h" |
17 #include "ppapi/shared_impl/tracked_callback.h" | 18 #include "ppapi/shared_impl/tracked_callback.h" |
18 | 19 |
19 namespace ppapi { | 20 namespace ppapi { |
20 namespace proxy { | 21 namespace proxy { |
21 | 22 |
22 class ResourceMessageReplyParams; | 23 class ResourceMessageReplyParams; |
23 | 24 |
24 class PPAPI_PROXY_EXPORT UDPSocketResourceBase: public PluginResource { | 25 class PPAPI_PROXY_EXPORT UDPSocketResourceBase: public PluginResource { |
25 public: | 26 public: |
26 // The maximum number of bytes that each PpapiHostMsg_PPBUDPSocket_RecvFrom | 27 // The maximum number of bytes that each |
27 // message is allowed to request. | 28 // PpapiPluginMsg_PPBUDPSocket_PushRecvResult message is allowed to carry. |
28 static const int32_t kMaxReadSize; | 29 static const int32_t kMaxReadSize; |
29 // The maximum number of bytes that each PpapiHostMsg_PPBUDPSocket_SendTo | 30 // The maximum number of bytes that each PpapiHostMsg_PPBUDPSocket_SendTo |
30 // message is allowed to carry. | 31 // message is allowed to carry. |
31 static const int32_t kMaxWriteSize; | 32 static const int32_t kMaxWriteSize; |
32 | 33 |
33 // The maximum number that we allow for setting | 34 // The maximum number that we allow for setting |
34 // PP_UDPSOCKET_OPTION_SEND_BUFFER_SIZE. This number is only for input | 35 // 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 // argument sanity check, it doesn't mean the browser guarantees to support |
36 // such a buffer size. | 37 // such a buffer size. |
37 static const int32_t kMaxSendBufferSize; | 38 static const int32_t kMaxSendBufferSize; |
38 // The maximum number that we allow for setting | 39 // The maximum number that we allow for setting |
39 // PP_UDPSOCKET_OPTION_RECV_BUFFER_SIZE. This number is only for input | 40 // 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 // argument sanity check, it doesn't mean the browser guarantees to support |
41 // such a buffer size. | 42 // such a buffer size. |
42 static const int32_t kMaxReceiveBufferSize; | 43 static const int32_t kMaxReceiveBufferSize; |
43 | 44 |
| 45 // The maximum number of received packets that we allow instances of this |
| 46 // class to buffer. |
| 47 static const size_t kPluginReceiveBufferSlots; |
| 48 |
44 protected: | 49 protected: |
45 UDPSocketResourceBase(Connection connection, | 50 UDPSocketResourceBase(Connection connection, |
46 PP_Instance instance, | 51 PP_Instance instance, |
47 bool private_api); | 52 bool private_api); |
48 virtual ~UDPSocketResourceBase(); | 53 virtual ~UDPSocketResourceBase(); |
49 | 54 |
50 int32_t SetOptionImpl(PP_UDPSocket_Option name, | 55 int32_t SetOptionImpl(PP_UDPSocket_Option name, |
51 const PP_Var& value, | 56 const PP_Var& value, |
52 scoped_refptr<TrackedCallback> callback); | 57 scoped_refptr<TrackedCallback> callback); |
53 int32_t BindImpl(const PP_NetAddress_Private* addr, | 58 int32_t BindImpl(const PP_NetAddress_Private* addr, |
54 scoped_refptr<TrackedCallback> callback); | 59 scoped_refptr<TrackedCallback> callback); |
55 PP_Bool GetBoundAddressImpl(PP_NetAddress_Private* addr); | 60 PP_Bool GetBoundAddressImpl(PP_NetAddress_Private* addr); |
56 // |addr| could be NULL to indicate that an output value is not needed. | 61 // |addr| could be NULL to indicate that an output value is not needed. |
57 int32_t RecvFromImpl(char* buffer, | 62 int32_t RecvFromImpl(char* buffer, |
58 int32_t num_bytes, | 63 int32_t num_bytes, |
59 PP_Resource* addr, | 64 PP_Resource* addr, |
60 scoped_refptr<TrackedCallback> callback); | 65 scoped_refptr<TrackedCallback> callback); |
61 PP_Bool GetRecvFromAddressImpl(PP_NetAddress_Private* addr); | 66 PP_Bool GetRecvFromAddressImpl(PP_NetAddress_Private* addr); |
62 int32_t SendToImpl(const char* buffer, | 67 int32_t SendToImpl(const char* buffer, |
63 int32_t num_bytes, | 68 int32_t num_bytes, |
64 const PP_NetAddress_Private* addr, | 69 const PP_NetAddress_Private* addr, |
65 scoped_refptr<TrackedCallback> callback); | 70 scoped_refptr<TrackedCallback> callback); |
66 void CloseImpl(); | 71 void CloseImpl(); |
67 | 72 |
68 private: | 73 private: |
| 74 struct RecvBuffer { |
| 75 int32_t result; |
| 76 std::string data; |
| 77 PP_NetAddress_Private addr; |
| 78 }; |
| 79 |
| 80 // Resource overrides. |
| 81 virtual void OnReplyReceived(const ResourceMessageReplyParams& params, |
| 82 const IPC::Message& msg) OVERRIDE; |
| 83 |
69 void PostAbortIfNecessary(scoped_refptr<TrackedCallback>* callback); | 84 void PostAbortIfNecessary(scoped_refptr<TrackedCallback>* callback); |
70 | 85 |
71 // IPC message handlers. | 86 // IPC message handlers. |
72 void OnPluginMsgSetOptionReply(scoped_refptr<TrackedCallback> callback, | 87 void OnPluginMsgSetOptionReply(scoped_refptr<TrackedCallback> callback, |
73 const ResourceMessageReplyParams& params); | 88 const ResourceMessageReplyParams& params); |
74 void OnPluginMsgBindReply(const ResourceMessageReplyParams& params, | 89 void OnPluginMsgBindReply(const ResourceMessageReplyParams& params, |
75 const PP_NetAddress_Private& bound_addr); | 90 const PP_NetAddress_Private& bound_addr); |
76 void OnPluginMsgRecvFromReply(PP_Resource* output_addr, | 91 void OnPluginMsgPushRecvResult(const ResourceMessageReplyParams& params, |
77 const ResourceMessageReplyParams& params, | 92 int32_t result, |
78 const std::string& data, | 93 const std::string& data, |
79 const PP_NetAddress_Private& addr); | 94 const PP_NetAddress_Private& addr); |
80 void OnPluginMsgSendToReply(const ResourceMessageReplyParams& params, | 95 void OnPluginMsgSendToReply(const ResourceMessageReplyParams& params, |
81 int32_t bytes_written); | 96 int32_t bytes_written); |
82 | 97 |
83 void RunCallback(scoped_refptr<TrackedCallback> callback, int32_t pp_result); | 98 void RunCallback(scoped_refptr<TrackedCallback> callback, int32_t pp_result); |
84 | 99 |
| 100 // Callers must ensure that |output_buffer| is big enough to store |data|. |
| 101 int32_t SetRecvFromOutput(int32_t browser_result, |
| 102 const std::string& data, |
| 103 const PP_NetAddress_Private& addr, |
| 104 char* output_buffer, |
| 105 int32_t num_bytes, |
| 106 PP_Resource* output_addr); |
| 107 |
85 bool private_api_; | 108 bool private_api_; |
86 bool bound_; | 109 bool bound_; |
87 bool closed_; | 110 bool closed_; |
88 | 111 |
89 scoped_refptr<TrackedCallback> bind_callback_; | 112 scoped_refptr<TrackedCallback> bind_callback_; |
90 scoped_refptr<TrackedCallback> recvfrom_callback_; | 113 scoped_refptr<TrackedCallback> recvfrom_callback_; |
91 scoped_refptr<TrackedCallback> sendto_callback_; | 114 scoped_refptr<TrackedCallback> sendto_callback_; |
92 | 115 |
93 char* read_buffer_; | 116 char* read_buffer_; |
94 int32_t bytes_to_read_; | 117 int32_t bytes_to_read_; |
| 118 PP_Resource* recvfrom_addr_resource_; |
95 | 119 |
96 PP_NetAddress_Private recvfrom_addr_; | 120 PP_NetAddress_Private recvfrom_addr_; |
97 PP_NetAddress_Private bound_addr_; | 121 PP_NetAddress_Private bound_addr_; |
98 | 122 |
| 123 std::queue<RecvBuffer> recv_buffers_; |
| 124 |
99 DISALLOW_COPY_AND_ASSIGN(UDPSocketResourceBase); | 125 DISALLOW_COPY_AND_ASSIGN(UDPSocketResourceBase); |
100 }; | 126 }; |
101 | 127 |
102 } // namespace proxy | 128 } // namespace proxy |
103 } // namespace ppapi | 129 } // namespace ppapi |
104 | 130 |
105 #endif // PPAPI_PROXY_UDP_SOCKET_RESOURCE_BASE_H_ | 131 #endif // PPAPI_PROXY_UDP_SOCKET_RESOURCE_BASE_H_ |
OLD | NEW |