OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/browser_callback.h" | 5 #include "native_client/src/shared/ppapi_proxy/browser_callback.h" |
6 | 6 |
7 #include <new> | 7 #include <new> |
8 | 8 |
9 #include "native_client/src/include/nacl_macros.h" | 9 #include "native_client/src/include/nacl_macros.h" |
10 #include "native_client/src/include/nacl_scoped_ptr.h" | 10 #include "native_client/src/include/nacl_scoped_ptr.h" |
11 #include "native_client/src/shared/platform/nacl_check.h" | 11 #include "native_client/src/shared/platform/nacl_check.h" |
12 #include "native_client/src/shared/ppapi_proxy/browser_globals.h" | 12 #include "native_client/src/shared/ppapi_proxy/browser_globals.h" |
13 #include "native_client/src/shared/ppapi_proxy/browser_ppp.h" | 13 #include "native_client/src/shared/ppapi_proxy/browser_ppp.h" |
| 14 #include "native_client/src/shared/ppapi_proxy/object_serialize.h" |
14 #include "native_client/src/shared/ppapi_proxy/utility.h" | 15 #include "native_client/src/shared/ppapi_proxy/utility.h" |
15 #include "native_client/src/shared/srpc/nacl_srpc.h" | 16 #include "native_client/src/shared/srpc/nacl_srpc.h" |
16 #include "ppapi/c/pp_completion_callback.h" | 17 #include "ppapi/c/pp_completion_callback.h" |
17 #include "native_client/src/trusted/plugin/plugin.h" | 18 #include "native_client/src/trusted/plugin/plugin.h" |
18 #include "srpcgen/ppp_rpc.h" | 19 #include "srpcgen/ppp_rpc.h" |
19 | 20 |
20 namespace ppapi_proxy { | 21 namespace ppapi_proxy { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
(...skipping 18 matching lines...) Expand all Loading... |
42 // depending on how the callback operation was initiated, and | 43 // depending on how the callback operation was initiated, and |
43 // |check_result_func| provides an abstraction to the semantics. | 44 // |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 // |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 // number of bytes read. The way the number of bytes read |
46 // retrieved/calculated may be different depending on how the callback was | 47 // retrieved/calculated may be different depending on how the callback was |
47 // initiated, and |get_size_read_func| provides the indirection. | 48 // initiated, and |get_size_read_func| provides the indirection. |
48 struct RemoteCallbackInfo { | 49 struct RemoteCallbackInfo { |
49 NaClSrpcChannel* srpc_channel; | 50 NaClSrpcChannel* srpc_channel; |
50 int32_t callback_id; | 51 int32_t callback_id; |
51 char* read_buffer; | 52 char* read_buffer; |
| 53 PP_Var read_var; |
52 CheckResultFunc check_result_func; | 54 CheckResultFunc check_result_func; |
53 GetReadSizeFunc get_size_read_func; | 55 GetReadSizeFunc get_size_read_func; |
54 }; | 56 }; |
55 | 57 |
56 // Calls the remote implementation of a callback on the plugin side. | 58 // 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 | 59 // Implements a PP_CompletionCallback_Func type that can be used along with an |
58 // instance of a RemoteCallbackInfo as |user_data| to provide a | 60 // instance of a RemoteCallbackInfo as |user_data| to provide a |
59 // PP_CompletionCallback to browser functions. | 61 // PP_CompletionCallback to browser functions. |
60 // | 62 // |
61 // |remote_callback| is a pointer to a RemoteCallbackInfo, | 63 // |remote_callback| is a pointer to a RemoteCallbackInfo, |
(...skipping 14 matching lines...) Expand all Loading... |
76 if (LookupBrowserPppForInstance(instance) == NULL) { | 78 if (LookupBrowserPppForInstance(instance) == NULL) { |
77 DebugPrintf("RunRemoteCallback: proxy=NULL\n", result); | 79 DebugPrintf("RunRemoteCallback: proxy=NULL\n", result); |
78 return; | 80 return; |
79 } | 81 } |
80 | 82 |
81 nacl_abi_size_t read_buffer_size = 0; | 83 nacl_abi_size_t read_buffer_size = 0; |
82 CheckResultFunc check_result_func = remote_callback->check_result_func; | 84 CheckResultFunc check_result_func = remote_callback->check_result_func; |
83 GetReadSizeFunc get_size_read_func = remote_callback->get_size_read_func; | 85 GetReadSizeFunc get_size_read_func = remote_callback->get_size_read_func; |
84 if ((*check_result_func)(result) && remote_callback->read_buffer != NULL) | 86 if ((*check_result_func)(result) && remote_callback->read_buffer != NULL) |
85 read_buffer_size = (*get_size_read_func)(result); | 87 read_buffer_size = (*get_size_read_func)(result); |
| 88 if (remote_callback->read_var.type != PP_VARTYPE_NULL) { |
| 89 read_buffer_size = kMaxReturnVarSize; |
| 90 read_buffer.reset( |
| 91 Serialize(&remote_callback->read_var, 1, &read_buffer_size)); |
| 92 } |
86 | 93 |
87 NaClSrpcError srpc_result = | 94 NaClSrpcError srpc_result = |
88 CompletionCallbackRpcClient::RunCompletionCallback( | 95 CompletionCallbackRpcClient::RunCompletionCallback( |
89 remote_callback->srpc_channel, | 96 remote_callback->srpc_channel, |
90 remote_callback->callback_id, | 97 remote_callback->callback_id, |
91 result, | 98 result, |
92 read_buffer_size, | 99 read_buffer_size, |
93 read_buffer.get()); | 100 read_buffer.get()); |
94 DebugPrintf("RunRemoteCallback: %s\n", | 101 DebugPrintf("RunRemoteCallback: %s\n", |
95 NaClSrpcErrorString(srpc_result)); | 102 NaClSrpcErrorString(srpc_result)); |
96 if (srpc_result == NACL_SRPC_RESULT_INTERNAL) | 103 if (srpc_result == NACL_SRPC_RESULT_INTERNAL) |
97 CleanUpAfterDeadNexe(instance); | 104 CleanUpAfterDeadNexe(instance); |
98 } | 105 } |
99 | 106 |
100 } // namespace | 107 } // namespace |
101 | 108 |
102 // Builds a RemoteCallbackInfo and returns PP_CompletionCallback corresponding | 109 // Builds a RemoteCallbackInfo and returns PP_CompletionCallback corresponding |
103 // to RunRemoteCallback or NULL on failure. | 110 // to RunRemoteCallback or NULL on failure. |
104 struct PP_CompletionCallback MakeRemoteCompletionCallback( | 111 struct PP_CompletionCallback MakeRemoteCompletionCallback( |
105 NaClSrpcChannel* srpc_channel, | 112 NaClSrpcChannel* srpc_channel, |
106 int32_t callback_id, | 113 int32_t callback_id, |
107 int32_t bytes_to_read, | 114 int32_t bytes_to_read, |
108 char** buffer, | 115 char** buffer, |
| 116 PP_Var** var, |
109 CheckResultFunc check_result_func, | 117 CheckResultFunc check_result_func, |
110 GetReadSizeFunc get_size_read_func) { | 118 GetReadSizeFunc get_size_read_func) { |
111 RemoteCallbackInfo* remote_callback = new(std::nothrow) RemoteCallbackInfo; | 119 nacl::scoped_ptr<RemoteCallbackInfo> remote_callback( |
112 if (remote_callback == NULL) // new failed. | 120 new(std::nothrow) RemoteCallbackInfo); |
| 121 if (remote_callback.get() == NULL) // new failed. |
113 return PP_BlockUntilComplete(); | 122 return PP_BlockUntilComplete(); |
114 remote_callback->srpc_channel = srpc_channel; | 123 remote_callback->srpc_channel = srpc_channel; |
115 remote_callback->callback_id = callback_id; | 124 remote_callback->callback_id = callback_id; |
116 remote_callback->read_buffer = NULL; | 125 remote_callback->read_buffer = NULL; |
| 126 remote_callback->read_var = PP_MakeNull(); |
117 remote_callback->check_result_func = check_result_func; | 127 remote_callback->check_result_func = check_result_func; |
118 remote_callback->get_size_read_func = get_size_read_func; | 128 remote_callback->get_size_read_func = get_size_read_func; |
119 | 129 |
120 if (bytes_to_read > 0 && buffer != NULL) { | 130 if (bytes_to_read > 0 && buffer != NULL) { |
121 *buffer = new(std::nothrow) char[bytes_to_read]; | 131 *buffer = new(std::nothrow) char[bytes_to_read]; |
122 if (*buffer == NULL) // new failed. | 132 if (*buffer == NULL) // new failed. |
123 return PP_BlockUntilComplete(); | 133 return PP_BlockUntilComplete(); |
124 remote_callback->read_buffer = *buffer; | 134 remote_callback->read_buffer = *buffer; |
125 } | 135 } |
| 136 if (var) |
| 137 *var = &remote_callback->read_var; |
126 | 138 |
127 return PP_MakeOptionalCompletionCallback( | 139 return PP_MakeOptionalCompletionCallback( |
128 RunRemoteCallback, remote_callback); | 140 RunRemoteCallback, remote_callback.release()); |
129 } | 141 } |
130 | 142 |
131 struct PP_CompletionCallback MakeRemoteCompletionCallback( | 143 struct PP_CompletionCallback MakeRemoteCompletionCallback( |
| 144 NaClSrpcChannel* srpc_channel, |
| 145 int32_t callback_id, |
| 146 int32_t bytes_to_read, |
| 147 char** buffer, |
| 148 CheckResultFunc check_result_func, |
| 149 GetReadSizeFunc get_size_read_func) { |
| 150 return MakeRemoteCompletionCallback(srpc_channel, callback_id, bytes_to_read, |
| 151 buffer, NULL, check_result_func, |
| 152 get_size_read_func); |
| 153 } |
| 154 |
| 155 struct PP_CompletionCallback MakeRemoteCompletionCallback( |
132 NaClSrpcChannel* srpc_channel, | 156 NaClSrpcChannel* srpc_channel, |
133 int32_t callback_id, | 157 int32_t callback_id, |
134 int32_t bytes_to_read, | 158 int32_t bytes_to_read, |
135 char** buffer) { | 159 char** buffer) { |
136 return MakeRemoteCompletionCallback(srpc_channel, callback_id, bytes_to_read, | 160 return MakeRemoteCompletionCallback(srpc_channel, callback_id, bytes_to_read, |
137 buffer, BytesWereRead, CastToNaClAbiSize); | 161 buffer, BytesWereRead, CastToNaClAbiSize); |
138 } | 162 } |
139 | 163 |
140 struct PP_CompletionCallback MakeRemoteCompletionCallback( | 164 struct PP_CompletionCallback MakeRemoteCompletionCallback( |
141 NaClSrpcChannel* srpc_channel, | 165 NaClSrpcChannel* srpc_channel, |
| 166 int32_t callback_id, |
| 167 PP_Var** var) { |
| 168 return MakeRemoteCompletionCallback(srpc_channel, callback_id, 0, NULL, var, |
| 169 BytesWereRead, CastToNaClAbiSize); |
| 170 } |
| 171 |
| 172 struct PP_CompletionCallback MakeRemoteCompletionCallback( |
| 173 NaClSrpcChannel* srpc_channel, |
142 int32_t callback_id) { | 174 int32_t callback_id) { |
143 return MakeRemoteCompletionCallback(srpc_channel, callback_id, 0, NULL); | 175 return MakeRemoteCompletionCallback(srpc_channel, callback_id, 0, NULL); |
144 } | 176 } |
145 | 177 |
146 void DeleteRemoteCallbackInfo(struct PP_CompletionCallback callback) { | 178 void DeleteRemoteCallbackInfo(struct PP_CompletionCallback callback) { |
147 nacl::scoped_ptr<RemoteCallbackInfo> remote_callback( | 179 nacl::scoped_ptr<RemoteCallbackInfo> remote_callback( |
148 reinterpret_cast<RemoteCallbackInfo*>(callback.user_data)); | 180 reinterpret_cast<RemoteCallbackInfo*>(callback.user_data)); |
149 nacl::scoped_array<char> read_buffer(remote_callback->read_buffer); | 181 nacl::scoped_array<char> read_buffer(remote_callback->read_buffer); |
150 } | 182 } |
151 | 183 |
152 } // namespace ppapi_proxy | 184 } // namespace ppapi_proxy |
OLD | NEW |