#ifndef NATIVE_CLIENT_SRC_SHARED_PPAPI_PROXY_PLUGIN_CALLBACK_H_
#define NATIVE_CLIENT_SRC_SHARED_PPAPI_PROXY_PLUGIN_CALLBACK_H_
#include <pthread.h>
#include <map>
#include "native_client/src/shared/ppapi_proxy/plugin_globals.h"
#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/pp_var.h"
namespace ppapi_proxy {
int32_t MayForceCallback(PP_CompletionCallback callback, int32_t result);
class CompletionCallbackTable {
public:
static CompletionCallbackTable* Get() {
static CompletionCallbackTable table;
return &table;
}
int32_t AddCallback(const PP_CompletionCallback& callback);
int32_t AddCallback(const PP_CompletionCallback& callback, void* read_buffer);
int32_t AddCallback(const PP_CompletionCallback& callback, PP_Var* read_var);
PP_CompletionCallback RemoveCallback(
int32_t callback_id, void** read_buffer, PP_Var** read_var);
private:
CompletionCallbackTable() : next_id_(1) { }
~CompletionCallbackTable() { }
int32_t AddCallback(const PP_CompletionCallback& callback,
void* read_buffer,
PP_Var* read_var);
struct CallbackInfo {
PP_CompletionCallback callback;
void* read_buffer;
PP_Var* read_var;
};
typedef std::map<int32_t, CallbackInfo> CallbackTable;
CallbackTable table_;
int32_t next_id_;
static pthread_mutex_t mutex_;
class CallbackTableCriticalSection {
public:
CallbackTableCriticalSection() { pthread_mutex_lock(&mutex_); }
~CallbackTableCriticalSection() { pthread_mutex_unlock(&mutex_); }
};
};
}
#endif