Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: ppapi/shared_impl/private/ppb_tcp_server_socket_shared.cc

Issue 9283022: Exposed Listen and Accept methods to plugin. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Added Destroy message for initialized but cancelled sockets. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 #include "ppapi/shared_impl/private/ppb_tcp_server_socket_shared.h"
6
7 #include <cstddef>
8 #include <cstring>
9
10 #include "base/logging.h"
11 #include "base/message_loop.h"
12 #include "ppapi/c/pp_errors.h"
13
14 namespace ppapi {
15
16 PPB_TCPServerSocket_Shared::PPB_TCPServerSocket_Shared(PP_Instance instance)
17 : Resource(instance), state_(NOT_INITIALIZED), tcp_socket_buffer_(NULL) {
18 }
19
20 PPB_TCPServerSocket_Shared::PPB_TCPServerSocket_Shared(
21 const HostResource& resource)
22 : Resource(resource), state_(NOT_INITIALIZED), tcp_socket_buffer_(NULL) {
23 }
24
25 PPB_TCPServerSocket_Shared::~PPB_TCPServerSocket_Shared() {
26 }
27
28 thunk::PPB_TCPServerSocket_Private_API*
29 PPB_TCPServerSocket_Shared::AsPPB_TCPServerSocket_Private_API() {
30 return this;
31 }
32
33 int32_t PPB_TCPServerSocket_Shared::Listen(const PP_NetAddress_Private* addr,
34 int32_t backlog,
35 PP_CompletionCallback callback) {
36 if (!addr)
37 return PP_ERROR_BADARGUMENT;
38 if (!callback.func)
39 return PP_ERROR_BLOCKS_MAIN_THREAD;
40 if (state_ != NOT_INITIALIZED &&
yzshen1 2012/02/10 22:40:15 Why we have to init and listen as two separate ste
ygorshenin1 2012/02/13 14:59:46 Because of efficiency. When user creates socket, i
41 state_ != INITIALIZING &&
42 state_ != BEFORE_LISTENING) {
43 return PP_ERROR_FAILED;
44 }
45 if (TrackedCallback::IsPending(listen_callback_))
46 return PP_ERROR_INPROGRESS; // Can only have one pending request.
47
48 if (state_ == NOT_INITIALIZED || state_ == INITIALIZING) {
brettw 2012/02/10 21:43:32 I don't see you using the INITIALIZING state anywh
ygorshenin1 2012/02/13 14:59:46 It was used in webkit/plugin/ppapi/ppb_tcp_server_
49 // Store args for lazy Listen call. Listen will be invoked when
50 // initialization is completed.
51 memmove(&listen_addr_, addr, sizeof(*addr));
52 listen_backlog_ = backlog;
53 listen_callback_ = new TrackedCallback(this, callback);
54
55 if (state_ == NOT_INITIALIZED) {
brettw 2012/02/10 21:43:32 This should also be able to be simplified if we ca
ygorshenin1 2012/02/13 14:59:46 Done.
56 state_ = INITIALIZING_PENDING_LISTEN_REQUEST;
57 SendInitialize();
58 } else {
59 state_ = INITIALIZING_PENDING_LISTEN_REQUEST;
60 }
61 } else { // state_ == BEFORE_LISTENING
brettw 2012/02/10 21:43:32 I also don't understand this case. Can't you only
ygorshenin1 2012/02/13 14:59:46 OK, code is simplified. On 2012/02/10 21:43:32, b
62 listen_callback_ = new TrackedCallback(this, callback);
63 // Send the request, the browser will call us back via ListenACK
64 SendListen(*addr, backlog);
65 }
66 return PP_OK_COMPLETIONPENDING;
67 }
68
69 int32_t PPB_TCPServerSocket_Shared::Accept(PP_Resource* tcp_socket,
70 PP_CompletionCallback callback) {
71 if (!tcp_socket)
72 return PP_ERROR_BADARGUMENT;
73 if (!callback.func)
74 return PP_ERROR_BLOCKS_MAIN_THREAD;
75
76 if (state_ != LISTENING)
77 return PP_ERROR_FAILED;
78 if (TrackedCallback::IsPending(accept_callback_))
79 return PP_ERROR_INPROGRESS;
80
81 tcp_socket_buffer_ = tcp_socket;
82 accept_callback_ = new TrackedCallback(this, callback);
83
84 SendAccept();
85 return PP_OK_COMPLETIONPENDING;
86 }
87
88 void PPB_TCPServerSocket_Shared::StopListening() {
89 if (state_ != LISTENING)
90 return;
91 state_ = CLOSED;
92
93 SendStopListening();
94 socket_id_ = 0;
95
96 PostAbortAndClearIfNecessary(&listen_callback_);
brettw 2012/02/10 21:43:32 Not necessary, but did you consider just inlining
ygorshenin1 2012/02/13 14:59:46 Done.
97 PostAbortAndClearIfNecessary(&accept_callback_);
98 tcp_socket_buffer_ = NULL;
99 }
100
101 void PPB_TCPServerSocket_Shared::OnInitializeCompleted(uint32 socket_id) {
102 if (state_ != INITIALIZING && state_ != INITIALIZING_PENDING_LISTEN_REQUEST) {
103 NOTREACHED();
104 return;
105 }
106
107 bool is_pending_listen_request =
108 state_ == INITIALIZING_PENDING_LISTEN_REQUEST;
109
110 if (socket_id == 0) {
111 state_ = NOT_INITIALIZED;
112 } else {
113 state_ = BEFORE_LISTENING;
114 socket_id_ = socket_id;
115 }
116
117 if (is_pending_listen_request) {
118 if (state_ == NOT_INITIALIZED) {
119 // Initialization is failed due to full socket table.
120 TrackedCallback::ClearAndRun(&listen_callback_, PP_ERROR_NOSPACE);
121 } else {
122 SendListen(listen_addr_, listen_backlog_);
123 }
124 }
125 }
126
127 void PPB_TCPServerSocket_Shared::OnListenCompleted(bool succeeded) {
128 if (state_ != BEFORE_LISTENING ||
129 !TrackedCallback::IsPending(listen_callback_)) {
130 NOTREACHED();
131 return;
132 }
133
134 if (succeeded)
135 state_ = LISTENING;
136
137 TrackedCallback::ClearAndRun(&listen_callback_,
138 succeeded ? PP_OK : PP_ERROR_FAILED);
139 }
140
141 void PPB_TCPServerSocket_Shared::PostAbortAndClearIfNecessary(
142 scoped_refptr<TrackedCallback>* callback) {
143 if (callback->get())
144 (*callback)->PostAbort();
145 }
146
147 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698