OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "nacl_io/devfs/jspipe_event_emitter.h" | 5 #include "nacl_io/devfs/jspipe_event_emitter.h" |
6 | 6 |
7 #include <assert.h> | 7 #include <assert.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 | 12 |
13 #define TRACE(format, ...) \ | 13 #define TRACE(format, ...) \ |
14 LOG_TRACE("jspipe[%s]: " format, name_.c_str(), ##__VA_ARGS__) | 14 LOG_TRACE("jspipe[%s]: " format, name_.c_str(), ##__VA_ARGS__) |
15 #define ERROR(format, ...) \ | 15 #define ERROR(format, ...) \ |
16 LOG_ERROR("jspipe[%s]: " format, name_.c_str(), ##__VA_ARGS__) | 16 LOG_ERROR("jspipe[%s]: " format, name_.c_str(), ##__VA_ARGS__) |
17 | 17 |
18 #include "nacl_io/log.h" | 18 #include "nacl_io/log.h" |
19 #include "nacl_io/osinttypes.h" | 19 #include "nacl_io/osinttypes.h" |
20 #include "nacl_io/pepper_interface.h" | 20 #include "nacl_io/pepper_interface.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 const size_t kMaxPostMessageSize = 64*1024; | 23 const size_t kMaxPostMessageSize = 64 * 1024; |
24 const char* kDictKeyPipe = "pipe"; | 24 const char* kDictKeyPipe = "pipe"; |
25 const char* kDictKeyOperation = "operation"; | 25 const char* kDictKeyOperation = "operation"; |
26 const char* kDictKeyPayload = "payload"; | 26 const char* kDictKeyPayload = "payload"; |
27 const char* kOperationNameAck = "ack"; | 27 const char* kOperationNameAck = "ack"; |
28 const char* kOperationNameWrite = "write"; | 28 const char* kOperationNameWrite = "write"; |
29 } | 29 } |
30 | 30 |
31 namespace nacl_io { | 31 namespace nacl_io { |
32 | 32 |
33 JSPipeEventEmitter::JSPipeEventEmitter(PepperInterface* ppapi, size_t size) | 33 JSPipeEventEmitter::JSPipeEventEmitter(PepperInterface* ppapi, size_t size) |
34 : input_fifo_(size), | 34 : input_fifo_(size), |
35 post_message_buffer_size_(size), | 35 post_message_buffer_size_(size), |
36 bytes_sent_(0), | 36 bytes_sent_(0), |
37 bytes_acked_(0), | 37 bytes_acked_(0), |
38 bytes_read_(0), | 38 bytes_read_(0), |
39 ppapi_(ppapi), | 39 ppapi_(ppapi), |
40 messaging_iface_(NULL), | 40 messaging_iface_(NULL), |
41 var_iface_(NULL), | 41 var_iface_(NULL), |
42 array_iface_(NULL), | 42 array_iface_(NULL), |
43 buffer_iface_(NULL), | 43 buffer_iface_(NULL), |
44 dict_iface_(NULL), | 44 dict_iface_(NULL), |
45 pipe_name_var_(PP_MakeUndefined()), | 45 pipe_name_var_(PP_MakeUndefined()), |
46 pipe_key_(PP_MakeUndefined()), | 46 pipe_key_(PP_MakeUndefined()), |
47 operation_key_(PP_MakeUndefined()), | 47 operation_key_(PP_MakeUndefined()), |
48 payload_key_(PP_MakeUndefined()), | 48 payload_key_(PP_MakeUndefined()), |
49 write_var_(PP_MakeUndefined()), | 49 write_var_(PP_MakeUndefined()), |
50 ack_var_(PP_MakeUndefined()) { | 50 ack_var_(PP_MakeUndefined()) { |
51 UpdateStatus_Locked(); | 51 UpdateStatus_Locked(); |
52 if (ppapi == NULL) { | 52 if (ppapi == NULL) { |
53 TRACE("missing PPAPI provider"); | 53 TRACE("missing PPAPI provider"); |
54 return; | 54 return; |
55 } | 55 } |
56 messaging_iface_ = ppapi->GetMessagingInterface(); | 56 messaging_iface_ = ppapi->GetMessagingInterface(); |
57 var_iface_ = ppapi->GetVarInterface(); | 57 var_iface_ = ppapi->GetVarInterface(); |
58 array_iface_ = ppapi->GetVarArrayInterface(); | 58 array_iface_ = ppapi->GetVarArrayInterface(); |
59 buffer_iface_ = ppapi->GetVarArrayBufferInterface(); | 59 buffer_iface_ = ppapi->GetVarArrayBufferInterface(); |
60 dict_iface_ = ppapi->GetVarDictionaryInterface(); | 60 dict_iface_ = ppapi->GetVarDictionaryInterface(); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 Error err = SendAckMessage(bytes_read_); | 104 Error err = SendAckMessage(bytes_read_); |
105 if (err != 0) | 105 if (err != 0) |
106 ERROR("Sending ACK failed: %d\n", err.error); | 106 ERROR("Sending ACK failed: %d\n", err.error); |
107 } | 107 } |
108 | 108 |
109 UpdateStatus_Locked(); | 109 UpdateStatus_Locked(); |
110 return 0; | 110 return 0; |
111 } | 111 } |
112 | 112 |
113 Error JSPipeEventEmitter::SendWriteMessage(const void* buf, size_t count) { | 113 Error JSPipeEventEmitter::SendWriteMessage(const void* buf, size_t count) { |
114 TRACE("SendWriteMessage [%"PRIuS"] total=%"PRIuS, count, bytes_sent_); | 114 TRACE("SendWriteMessage [%" PRIuS "] total=%" PRIuS, count, bytes_sent_); |
115 if (!var_iface_ || !buffer_iface_) | 115 if (!var_iface_ || !buffer_iface_) |
116 return EIO; | 116 return EIO; |
117 | 117 |
118 // Copy payload data in a new ArrayBuffer | 118 // Copy payload data in a new ArrayBuffer |
119 PP_Var buffer = buffer_iface_->Create(count); | 119 PP_Var buffer = buffer_iface_->Create(count); |
120 memcpy(buffer_iface_->Map(buffer), buf, count); | 120 memcpy(buffer_iface_->Map(buffer), buf, count); |
121 buffer_iface_->Unmap(buffer); | 121 buffer_iface_->Unmap(buffer); |
122 | 122 |
123 Error rtn = SendMessageToJS(write_var_, buffer); | 123 Error rtn = SendMessageToJS(write_var_, buffer); |
124 var_iface_->Release(buffer); | 124 var_iface_->Release(buffer); |
(...skipping 11 matching lines...) Expand all Loading... |
136 // new name must not be empty | 136 // new name must not be empty |
137 if (!name || strlen(name) == 0) | 137 if (!name || strlen(name) == 0) |
138 return EIO; | 138 return EIO; |
139 | 139 |
140 TRACE("set name: %s", name); | 140 TRACE("set name: %s", name); |
141 name_ = name; | 141 name_ = name; |
142 pipe_name_var_ = VarFromCStr(name); | 142 pipe_name_var_ = VarFromCStr(name); |
143 return 0; | 143 return 0; |
144 } | 144 } |
145 | 145 |
146 Error JSPipeEventEmitter::SendMessageToJS(PP_Var operation, | 146 Error JSPipeEventEmitter::SendMessageToJS(PP_Var operation, PP_Var payload) { |
147 PP_Var payload) { | |
148 if (!ppapi_ || !messaging_iface_ || !var_iface_ || !dict_iface_) | 147 if (!ppapi_ || !messaging_iface_ || !var_iface_ || !dict_iface_) |
149 return EIO; | 148 return EIO; |
150 | 149 |
151 // Create dict object which will be sent to JavaScript. | 150 // Create dict object which will be sent to JavaScript. |
152 PP_Var dict = dict_iface_->Create(); | 151 PP_Var dict = dict_iface_->Create(); |
153 | 152 |
154 // Set try keys in the dictionaty: 'pipe', 'operation', and 'payload' | 153 // Set try keys in the dictionaty: 'pipe', 'operation', and 'payload' |
155 dict_iface_->Set(dict, pipe_key_, pipe_name_var_); | 154 dict_iface_->Set(dict, pipe_key_, pipe_name_var_); |
156 dict_iface_->Set(dict, operation_key_, operation); | 155 dict_iface_->Set(dict, operation_key_, operation); |
157 dict_iface_->Set(dict, payload_key_, payload); | 156 dict_iface_->Set(dict, payload_key_, payload); |
158 | 157 |
159 // Send the dict via PostMessage | 158 // Send the dict via PostMessage |
160 messaging_iface_->PostMessage(ppapi_->GetInstance(), dict); | 159 messaging_iface_->PostMessage(ppapi_->GetInstance(), dict); |
161 | 160 |
162 // Release the dict | 161 // Release the dict |
163 var_iface_->Release(dict); | 162 var_iface_->Release(dict); |
164 return 0; | 163 return 0; |
165 } | 164 } |
166 | 165 |
167 Error JSPipeEventEmitter::SendAckMessage(size_t byte_count) { | 166 Error JSPipeEventEmitter::SendAckMessage(size_t byte_count) { |
168 TRACE("SendAckMessage %"PRIuS, byte_count); | 167 TRACE("SendAckMessage %" PRIuS, byte_count); |
169 PP_Var payload; | 168 PP_Var payload; |
170 payload.type = PP_VARTYPE_INT32; | 169 payload.type = PP_VARTYPE_INT32; |
171 payload.value.as_int = (int32_t)byte_count; | 170 payload.value.as_int = (int32_t)byte_count; |
172 | 171 |
173 return SendMessageToJS(ack_var_, payload); | 172 return SendMessageToJS(ack_var_, payload); |
174 } | 173 } |
175 | 174 |
176 size_t JSPipeEventEmitter::HandleJSWrite(const char* data, size_t len) { | 175 size_t JSPipeEventEmitter::HandleJSWrite(const char* data, size_t len) { |
177 size_t out_len = input_fifo_.Write(data, len); | 176 size_t out_len = input_fifo_.Write(data, len); |
178 UpdateStatus_Locked(); | 177 UpdateStatus_Locked(); |
179 return out_len; | 178 return out_len; |
180 } | 179 } |
181 | 180 |
182 void JSPipeEventEmitter::HandleJSAck(size_t byte_count) { | 181 void JSPipeEventEmitter::HandleJSAck(size_t byte_count) { |
183 if (byte_count > bytes_sent_) { | 182 if (byte_count > bytes_sent_) { |
184 ERROR("HandleAck unexpected byte count: %"PRIuS, byte_count); | 183 ERROR("HandleAck unexpected byte count: %" PRIuS, byte_count); |
185 return; | 184 return; |
186 } | 185 } |
187 | 186 |
188 bytes_acked_ = byte_count; | 187 bytes_acked_ = byte_count; |
189 TRACE("HandleAck: %" SCNuS "/%" PRIuS, bytes_acked_, bytes_sent_); | 188 TRACE("HandleAck: %" SCNuS "/%" PRIuS, bytes_acked_, bytes_sent_); |
190 UpdateStatus_Locked(); | 189 UpdateStatus_Locked(); |
191 } | 190 } |
192 | 191 |
193 Error JSPipeEventEmitter::HandleJSWrite(struct PP_Var message) { | 192 Error JSPipeEventEmitter::HandleJSWrite(struct PP_Var message) { |
194 TRACE("HandleJSWrite"); | 193 TRACE("HandleJSWrite"); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 TRACE("Unknown message type: %s", message_type.c_str()); | 273 TRACE("Unknown message type: %s", message_type.c_str()); |
275 err = EINVAL; | 274 err = EINVAL; |
276 } | 275 } |
277 var_iface_->Release(payload); | 276 var_iface_->Release(payload); |
278 } | 277 } |
279 | 278 |
280 var_iface_->Release(operation_var); | 279 var_iface_->Release(operation_var); |
281 return err; | 280 return err; |
282 } | 281 } |
283 | 282 |
284 Error JSPipeEventEmitter::Write_Locked(const char* data, size_t len, | 283 Error JSPipeEventEmitter::Write_Locked(const char* data, |
| 284 size_t len, |
285 int* out_bytes) { | 285 int* out_bytes) { |
286 if (GetOSpace() == 0) { | 286 if (GetOSpace() == 0) { |
287 *out_bytes = 0; | 287 *out_bytes = 0; |
288 return 0; | 288 return 0; |
289 } | 289 } |
290 | 290 |
291 if (len > GetOSpace()) | 291 if (len > GetOSpace()) |
292 len = GetOSpace(); | 292 len = GetOSpace(); |
293 | 293 |
294 // Limit the size of the data we send with PostMessage to kMaxPostMessageSize | 294 // Limit the size of the data we send with PostMessage to kMaxPostMessageSize |
295 if (len > kMaxPostMessageSize) | 295 if (len > kMaxPostMessageSize) |
296 len = kMaxPostMessageSize; | 296 len = kMaxPostMessageSize; |
297 | 297 |
298 Error err = SendWriteMessage(data, len); | 298 Error err = SendWriteMessage(data, len); |
299 if (err != 0) | 299 if (err != 0) |
300 return err; | 300 return err; |
301 *out_bytes = len; | 301 *out_bytes = len; |
302 bytes_sent_ += len; | 302 bytes_sent_ += len; |
303 | 303 |
304 UpdateStatus_Locked(); | 304 UpdateStatus_Locked(); |
305 return 0; | 305 return 0; |
306 } | 306 } |
307 | 307 |
308 } // namespace nacl_io | 308 } // namespace nacl_io |
OLD | NEW |