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 #ifndef NATIVE_CLIENT_SRC_SHARED_PPAPI_PROXY_PLUGIN_CALLBACK_H_ |
| 6 #define NATIVE_CLIENT_SRC_SHARED_PPAPI_PROXY_PLUGIN_CALLBACK_H_ |
| 7 |
| 8 #include <pthread.h> |
| 9 #include <map> |
| 10 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h" |
| 11 #include "ppapi/c/pp_completion_callback.h" |
| 12 |
| 13 namespace ppapi_proxy { |
| 14 |
| 15 // Skips callback invocation and returns |result| if callback function is NULL |
| 16 // or PP_COMPLETIONCALLBACK_FLAG_OPTIONAL is set. Otherwise, schedules the |
| 17 // callback with |result| as an argument and returns PP_OK_COMPLETIONPENDING. |
| 18 int32_t MayForceCallback(PP_CompletionCallback callback, int32_t result); |
| 19 |
| 20 // Maintains a table of PP_CompletionCallback objects and their respective |
| 21 // identifiers that can be used to retrieve the objects. |
| 22 class CompletionCallbackTable { |
| 23 public: |
| 24 // Return a singleton instance. |
| 25 static CompletionCallbackTable* Get() { |
| 26 static CompletionCallbackTable table; |
| 27 return &table; |
| 28 } |
| 29 |
| 30 // Adds the given |callback| and optionally the associated |read_buffer|, |
| 31 // generating and returning an identifier for it. |
| 32 // If |callback| is NULL, then returns 0. |
| 33 int32_t AddCallback(const PP_CompletionCallback& callback); |
| 34 int32_t AddCallback(const PP_CompletionCallback& callback, void* read_buffer); |
| 35 // Removes and returns the callback and optionally the associated |
| 36 // |read_buffer| corresponding to the given |callback_id|. |
| 37 // If no callback is found, returns a NULL callback. |
| 38 PP_CompletionCallback RemoveCallback(int32_t callback_id, void** read_buffer); |
| 39 |
| 40 private: |
| 41 // Currently implemented as singleton, so use a private constructor. |
| 42 CompletionCallbackTable() : next_id_(1) { } |
| 43 ~CompletionCallbackTable() { } |
| 44 |
| 45 struct CallbackInfo { |
| 46 PP_CompletionCallback callback; |
| 47 void* read_buffer; // To be used with callbacks invoked on byte reads. |
| 48 }; |
| 49 |
| 50 typedef std::map<int32_t, CallbackInfo> CallbackTable; |
| 51 CallbackTable table_; |
| 52 int32_t next_id_; |
| 53 |
| 54 // Single static mutex used as critical section for all callback tables. |
| 55 static pthread_mutex_t mutex_; |
| 56 class CallbackTableCriticalSection { |
| 57 public: |
| 58 CallbackTableCriticalSection() { pthread_mutex_lock(&mutex_); } |
| 59 ~CallbackTableCriticalSection() { pthread_mutex_unlock(&mutex_); } |
| 60 }; |
| 61 }; |
| 62 |
| 63 } // namespace ppapi_proxy |
| 64 |
| 65 #endif // NATIVE_CLIENT_SRC_SHARED_PPAPI_PROXY_PLUGIN_CALLBACK_H_ |
OLD | NEW |