OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Native Client 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 "native_client/src/shared/ppapi_proxy/browser_callback.h" |
| 6 |
| 7 #include <new> |
| 8 |
| 9 #include "native_client/src/include/nacl_macros.h" |
| 10 #include "native_client/src/include/nacl_scoped_ptr.h" |
| 11 #include "native_client/src/shared/platform/nacl_check.h" |
| 12 #include "native_client/src/shared/ppapi_proxy/browser_globals.h" |
| 13 #include "native_client/src/shared/ppapi_proxy/browser_ppp.h" |
| 14 #include "native_client/src/shared/ppapi_proxy/utility.h" |
| 15 #include "native_client/src/shared/srpc/nacl_srpc.h" |
| 16 #include "ppapi/c/pp_completion_callback.h" |
| 17 #include "native_client/src/trusted/plugin/plugin.h" |
| 18 #include "srpcgen/ppp_rpc.h" |
| 19 |
| 20 namespace ppapi_proxy { |
| 21 |
| 22 namespace { |
| 23 |
| 24 bool BytesWereRead(int32_t num_bytes) { |
| 25 return (num_bytes > 0); |
| 26 } |
| 27 |
| 28 nacl_abi_size_t CastToNaClAbiSize(int32_t result) { |
| 29 return static_cast<nacl_abi_size_t>(result); |
| 30 } |
| 31 |
| 32 // Data structure used on the browser side to invoke a completion callback |
| 33 // on the plugin side. |
| 34 // |
| 35 // A plugin-side callback is proxied over to the browser side using |
| 36 // a |callback_id|. This id is then paired with an |srpc_channel| listened to |
| 37 // by the nexe that supplied the callback. |
| 38 // |
| 39 // |read_buffer| is used with callbacks that are invoked on byte reads. |
| 40 // |check_result_func| is a pointer to a function used to check the |
| 41 // result of the operation. The semantics of the result value may be different |
| 42 // depending on how the callback operation was initiated, and |
| 43 // |check_result_func| provides an abstraction to the semantics. |
| 44 // |get_size_read_func| is a pointer to a function used to get the |
| 45 // number of bytes read. The way the number of bytes read |
| 46 // retrieved/calculated may be different depending on how the callback was |
| 47 // initiated, and |get_size_read_func| provides the indirection. |
| 48 struct RemoteCallbackInfo { |
| 49 NaClSrpcChannel* srpc_channel; |
| 50 int32_t callback_id; |
| 51 char* read_buffer; |
| 52 CheckResultFunc check_result_func; |
| 53 GetReadSizeFunc get_size_read_func; |
| 54 }; |
| 55 |
| 56 // Calls the remote implementation of a callback on the plugin side. |
| 57 // Implements a PP_CompletionCallback_Func type that can be used along with an |
| 58 // instance of a RemoteCallbackInfo as |user_data| to provide a |
| 59 // PP_CompletionCallback to browser functions. |
| 60 // |
| 61 // |remote_callback| is a pointer to a RemoteCallbackInfo, |
| 62 // deleted after rpc via scoped_ptr. The associated |read_buffer| is also |
| 63 // deleted. |
| 64 // |result| is passed by the callback invoker to indicate success or error. |
| 65 // It is passed as-is to the plugin side callback. |
| 66 void RunRemoteCallback(void* user_data, int32_t result) { |
| 67 CHECK(PPBCoreInterface()->IsMainThread()); |
| 68 DebugPrintf("RunRemoteCallback: result=%"NACL_PRId32"\n", result); |
| 69 nacl::scoped_ptr<RemoteCallbackInfo> remote_callback( |
| 70 reinterpret_cast<RemoteCallbackInfo*>(user_data)); |
| 71 nacl::scoped_array<char> read_buffer(remote_callback->read_buffer); |
| 72 |
| 73 // If the proxy is down, the channel is no longer usable for remote calls. |
| 74 PP_Instance instance = |
| 75 LookupInstanceIdForSrpcChannel(remote_callback->srpc_channel); |
| 76 if (LookupBrowserPppForInstance(instance) == NULL) { |
| 77 DebugPrintf("RunRemoteCallback: proxy=NULL\n", result); |
| 78 return; |
| 79 } |
| 80 |
| 81 nacl_abi_size_t read_buffer_size = 0; |
| 82 CheckResultFunc check_result_func = remote_callback->check_result_func; |
| 83 GetReadSizeFunc get_size_read_func = remote_callback->get_size_read_func; |
| 84 if ((*check_result_func)(result) && remote_callback->read_buffer != NULL) |
| 85 read_buffer_size = (*get_size_read_func)(result); |
| 86 |
| 87 NaClSrpcError srpc_result = |
| 88 CompletionCallbackRpcClient::RunCompletionCallback( |
| 89 remote_callback->srpc_channel, |
| 90 remote_callback->callback_id, |
| 91 result, |
| 92 read_buffer_size, |
| 93 read_buffer.get()); |
| 94 DebugPrintf("RunRemoteCallback: %s\n", |
| 95 NaClSrpcErrorString(srpc_result)); |
| 96 if (srpc_result == NACL_SRPC_RESULT_INTERNAL) |
| 97 CleanUpAfterDeadNexe(instance); |
| 98 } |
| 99 |
| 100 } // namespace |
| 101 |
| 102 // Builds a RemoteCallbackInfo and returns PP_CompletionCallback corresponding |
| 103 // to RunRemoteCallback or NULL on failure. |
| 104 struct PP_CompletionCallback MakeRemoteCompletionCallback( |
| 105 NaClSrpcChannel* srpc_channel, |
| 106 int32_t callback_id, |
| 107 int32_t bytes_to_read, |
| 108 char** buffer, |
| 109 CheckResultFunc check_result_func, |
| 110 GetReadSizeFunc get_size_read_func) { |
| 111 RemoteCallbackInfo* remote_callback = new(std::nothrow) RemoteCallbackInfo; |
| 112 if (remote_callback == NULL) // new failed. |
| 113 return PP_BlockUntilComplete(); |
| 114 remote_callback->srpc_channel = srpc_channel; |
| 115 remote_callback->callback_id = callback_id; |
| 116 remote_callback->read_buffer = NULL; |
| 117 remote_callback->check_result_func = check_result_func; |
| 118 remote_callback->get_size_read_func = get_size_read_func; |
| 119 |
| 120 if (bytes_to_read > 0 && buffer != NULL) { |
| 121 *buffer = new(std::nothrow) char[bytes_to_read]; |
| 122 if (*buffer == NULL) // new failed. |
| 123 return PP_BlockUntilComplete(); |
| 124 remote_callback->read_buffer = *buffer; |
| 125 } |
| 126 |
| 127 return PP_MakeOptionalCompletionCallback( |
| 128 RunRemoteCallback, remote_callback); |
| 129 } |
| 130 |
| 131 struct PP_CompletionCallback MakeRemoteCompletionCallback( |
| 132 NaClSrpcChannel* srpc_channel, |
| 133 int32_t callback_id, |
| 134 int32_t bytes_to_read, |
| 135 char** buffer) { |
| 136 return MakeRemoteCompletionCallback(srpc_channel, callback_id, bytes_to_read, |
| 137 buffer, BytesWereRead, CastToNaClAbiSize); |
| 138 } |
| 139 |
| 140 struct PP_CompletionCallback MakeRemoteCompletionCallback( |
| 141 NaClSrpcChannel* srpc_channel, |
| 142 int32_t callback_id) { |
| 143 return MakeRemoteCompletionCallback(srpc_channel, callback_id, 0, NULL); |
| 144 } |
| 145 |
| 146 void DeleteRemoteCallbackInfo(struct PP_CompletionCallback callback) { |
| 147 nacl::scoped_ptr<RemoteCallbackInfo> remote_callback( |
| 148 reinterpret_cast<RemoteCallbackInfo*>(callback.user_data)); |
| 149 nacl::scoped_array<char> read_buffer(remote_callback->read_buffer); |
| 150 } |
| 151 |
| 152 } // namespace ppapi_proxy |
OLD | NEW |