22 #ifdef __native_client__
26 #include <ppapi_simple/ps.h>
27 #include <ppapi_simple/ps_event.h>
28 #include <ppapi/cpp/instance.h>
29 #include <ppapi/cpp/var.h>
32 #include <condition_variable>
36 struct NaclMessageHandle
final
50 std::condition_variable condv;
53 void naclPostMessage(
const std::string &type,
54 const std::string &message)
56 pp::Var msgVar = pp::Var(std::string(type).append(
":").append(message));
57 pp::Instance(PSGetInstanceId()).PostMessage(msgVar);
60 static void naclMessageHandlerFunc(
struct PP_Var key,
64 NaclMessageHandle *handle =
reinterpret_cast<NaclMessageHandle *
>(
67 if (key.type != PP_VARTYPE_STRING || value.type != PP_VARTYPE_STRING)
69 if (pp::Var(key).AsString() != handle->type)
72 handle->handled =
true;
73 handle->message = pp::Var(value).AsString();
75 handle->condv.notify_one();
78 NaclMessageHandle *naclRegisterMessageHandler(
const std::string &type)
80 NaclMessageHandle *handle =
new NaclMessageHandle;
81 handle->handled =
false;
84 PSEventRegisterMessageHandler(type.c_str(),
85 naclMessageHandlerFunc,
86 reinterpret_cast<void *
>(handle));
90 void naclUnregisterMessageHandler(NaclMessageHandle *handle)
92 PSEventRegisterMessageHandler(handle->type.c_str(),
98 std::string naclWaitForMessage(NaclMessageHandle *handle)
101 std::unique_lock <std::mutex> lck(mtx);
103 while (!handle->handled)
104 handle->condv.wait(lck);
106 handle->handled =
false;
107 return handle->message;
#define A_DELETE_COPY(func)