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

Side by Side Diff: ppapi/native_client/src/shared/ppapi_proxy/plugin_callback.cc

Issue 7740013: Cloning a bunch of stuff from the native_client repository at r6528 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 3 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 | Annotate | Revision Log
OLDNEW
(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/plugin_callback.h"
6 #include <string.h>
7 #include "native_client/src/shared/ppapi_proxy/utility.h"
8 #include "ppapi/c/pp_errors.h"
9 #include "srpcgen/ppp_rpc.h"
10
11 namespace ppapi_proxy {
12
13 int32_t MayForceCallback(PP_CompletionCallback callback, int32_t result) {
14 if (result == PP_OK_COMPLETIONPENDING)
15 return result;
16
17 if (callback.func == NULL ||
18 (callback.flags & PP_COMPLETIONCALLBACK_FLAG_OPTIONAL) != 0)
19 return result;
20
21 PPBCoreInterface()->CallOnMainThread(0, callback, result);
22 return PP_OK_COMPLETIONPENDING;
23 }
24
25 // Initialize static mutex used as critical section for all callback tables.
26 pthread_mutex_t CompletionCallbackTable::mutex_ = PTHREAD_MUTEX_INITIALIZER;
27
28 int32_t CompletionCallbackTable::AddCallback(
29 const PP_CompletionCallback& callback,
30 void* read_buffer) {
31 CallbackTableCriticalSection guard;
32 if (callback.func == NULL) {
33 DebugPrintf("CompletionCallbackTable attempted to add NULL func!!\n");
34 return 0;
35 }
36 int32_t callback_id = next_id_;
37 ++next_id_;
38 CallbackInfo info = { callback, read_buffer };
39 table_.insert(std::pair<int32_t, CallbackInfo>(callback_id, info));
40 return callback_id;
41 }
42
43 int32_t CompletionCallbackTable::AddCallback(
44 const PP_CompletionCallback& callback) {
45 return AddCallback(callback, NULL);
46 }
47
48 PP_CompletionCallback CompletionCallbackTable::RemoveCallback(
49 int32_t callback_id, void** read_buffer) {
50 CallbackTableCriticalSection guard;
51 CallbackTable::iterator it = table_.find(callback_id);
52 DebugPrintf("CompletionCallbackTable::RemoveCallback id: %"NACL_PRId32"\n",
53 callback_id);
54 if (table_.end() != it) {
55 CallbackInfo info = it->second;
56 table_.erase(it);
57 if (read_buffer != NULL)
58 *read_buffer = info.read_buffer;
59 return info.callback;
60 }
61 *read_buffer = NULL;
62 return PP_BlockUntilComplete();
63 }
64
65 } // namespace ppapi_proxy
66
67 // SRPC-abstraction wrapper around a PP_CompletionCallback.
68 void CompletionCallbackRpcServer::RunCompletionCallback(
69 NaClSrpcRpc* rpc,
70 NaClSrpcClosure* done,
71 // inputs
72 int32_t callback_id,
73 int32_t result,
74 // TODO(polina): use shm for read buffer
75 nacl_abi_size_t read_buffer_size, char* read_buffer) {
76 NaClSrpcClosureRunner runner(done);
77 rpc->result = NACL_SRPC_RESULT_APP_ERROR;
78
79 void* user_buffer;
80 PP_CompletionCallback callback =
81 ppapi_proxy::CompletionCallbackTable::Get()->RemoveCallback(
82 callback_id, &user_buffer);
83 if (callback.func == NULL) {
84 ppapi_proxy::DebugPrintf(
85 "CompletionCallbackRpcServer: id of %"NACL_PRId32" is NULL callback!\n",
86 callback_id);
87 return;
88 }
89
90 if (user_buffer != NULL && read_buffer_size > 0)
91 memcpy(user_buffer, read_buffer, read_buffer_size);
92 PP_RunCompletionCallback(&callback, result);
93
94 rpc->result = NACL_SRPC_RESULT_OK;
95 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698