| OLD | NEW |
| 1 // Copyright (c) 2010 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2010 The Native Client 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 "native_client/src/shared/ppapi_proxy/plugin_callback.h" | 5 #include "native_client/src/shared/ppapi_proxy/plugin_callback.h" |
| 6 #include <string.h> |
| 6 #include "srpcgen/ppp_rpc.h" | 7 #include "srpcgen/ppp_rpc.h" |
| 7 | 8 |
| 8 namespace ppapi_proxy { | 9 namespace ppapi_proxy { |
| 9 | 10 |
| 10 CompletionCallbackTable::CompletionCallbackTable() | 11 CompletionCallbackTable::CompletionCallbackTable() |
| 11 : next_id_(1) { | 12 : next_id_(1) { |
| 12 } | 13 } |
| 13 | 14 |
| 14 int32_t CompletionCallbackTable::AddCallback( | 15 int32_t CompletionCallbackTable::AddCallback( |
| 16 const PP_CompletionCallback& callback, |
| 17 char* read_buffer) { |
| 18 if (callback.func == NULL) |
| 19 return 0; |
| 20 int32_t callback_id = next_id_; |
| 21 ++next_id_; |
| 22 CallbackInfo info = { callback, read_buffer }; |
| 23 table_[callback_id] = info; |
| 24 return callback_id; |
| 25 } |
| 26 |
| 27 int32_t CompletionCallbackTable::AddCallback( |
| 15 const PP_CompletionCallback& callback) { | 28 const PP_CompletionCallback& callback) { |
| 16 if (callback.func == NULL) | 29 return AddCallback(callback, NULL); |
| 17 return 0; | |
| 18 table_[next_id_] = callback; | |
| 19 return next_id_++; | |
| 20 } | 30 } |
| 21 | 31 |
| 22 PP_CompletionCallback CompletionCallbackTable::RemoveCallback( | 32 PP_CompletionCallback CompletionCallbackTable::RemoveCallback( |
| 23 int32_t callback_id) { | 33 int32_t callback_id, char** read_buffer) { |
| 24 CallbackTable::iterator it = table_.find(callback_id); | 34 CallbackTable::iterator it = table_.find(callback_id); |
| 25 if (table_.end() != it) { | 35 if (table_.end() != it) { |
| 26 PP_CompletionCallback callback = it->second; | 36 CallbackInfo info = it->second; |
| 27 table_.erase(it); | 37 table_.erase(it); |
| 28 return callback; | 38 if (read_buffer != NULL) |
| 39 *read_buffer = info.read_buffer; |
| 40 return info.callback; |
| 29 } | 41 } |
| 42 *read_buffer = NULL; |
| 30 return PP_BlockUntilComplete(); | 43 return PP_BlockUntilComplete(); |
| 31 } | 44 } |
| 32 | 45 |
| 33 } // namespace ppapi_proxy | 46 } // namespace ppapi_proxy |
| 34 | 47 |
| 35 // SRPC-abstraction wrapper around a PP_CompletionCallback. | 48 // SRPC-abstraction wrapper around a PP_CompletionCallback. |
| 36 void CompletionCallbackRpcServer::RunCompletionCallback( | 49 void CompletionCallbackRpcServer::RunCompletionCallback( |
| 37 NaClSrpcRpc* rpc, | 50 NaClSrpcRpc* rpc, |
| 38 NaClSrpcClosure* done, | 51 NaClSrpcClosure* done, |
| 52 // inputs |
| 39 int32_t callback_id, | 53 int32_t callback_id, |
| 40 int32_t result) { | 54 int32_t result, |
| 55 // TODO(polina): use shm for read buffer |
| 56 nacl_abi_size_t read_buffer_size, char* read_buffer) { |
| 41 NaClSrpcClosureRunner runner(done); | 57 NaClSrpcClosureRunner runner(done); |
| 42 rpc->result = NACL_SRPC_RESULT_APP_ERROR; | 58 rpc->result = NACL_SRPC_RESULT_APP_ERROR; |
| 59 |
| 60 char* user_buffer; |
| 43 PP_CompletionCallback callback = | 61 PP_CompletionCallback callback = |
| 44 ppapi_proxy::CompletionCallbackTable::Get()->RemoveCallback(callback_id); | 62 ppapi_proxy::CompletionCallbackTable::Get()->RemoveCallback( |
| 63 callback_id, &user_buffer); |
| 45 if (callback.func == NULL) | 64 if (callback.func == NULL) |
| 46 return; | 65 return; |
| 66 |
| 67 if (user_buffer != NULL && read_buffer_size > 0) |
| 68 memcpy(user_buffer, read_buffer, read_buffer_size); |
| 47 PP_RunCompletionCallback(&callback, result); | 69 PP_RunCompletionCallback(&callback, result); |
| 70 |
| 48 rpc->result = NACL_SRPC_RESULT_OK; | 71 rpc->result = NACL_SRPC_RESULT_OK; |
| 49 } | 72 } |
| OLD | NEW |