diff options
Diffstat (limited to 'src/utils/process.cpp')
-rw-r--r-- | src/utils/process.cpp | 74 |
1 files changed, 69 insertions, 5 deletions
diff --git a/src/utils/process.cpp b/src/utils/process.cpp index 74ac20b53..d3d154270 100644 --- a/src/utils/process.cpp +++ b/src/utils/process.cpp @@ -269,13 +269,9 @@ bool openBrowser(std::string url) return execFile("/usr/bin/xdg-open", "/usr/bin/xdg-open", url, ""); } #elif defined __native_client__ -#include <ppapi_simple/ps.h> -#include <ppapi/cpp/instance.h> -#include <ppapi/cpp/var.h> bool openBrowser(std::string url) { - pp::Var msgVar = pp::Var(std::string("open-browser: ").append(url)); - pp::Instance(PSGetInstanceId()).PostMessage(msgVar); + naclPostMessage("open-browser", url); return true; } #else @@ -300,3 +296,71 @@ void setPriority(const bool big A_UNUSED) { } #endif + +#ifdef __native_client__ +#include <ppapi_simple/ps.h> +#include <ppapi_simple/ps_event.h> +#include <ppapi/cpp/instance.h> +#include <ppapi/cpp/var.h> + +#include <mutex> +#include <condition_variable> + +struct _NaclMessageHandle { + bool handled; + std::string type; + std::string message; + std::condition_variable condv; +}; + +void naclPostMessage(const std::string &type, const std::string &message) +{ + pp::Var msgVar = pp::Var(std::string(type).append(":").append(message)); + pp::Instance(PSGetInstanceId()).PostMessage(msgVar); +} + +void naclMessageHandlerFunc(struct PP_Var key, + struct PP_Var value, + void* user_data) +{ + NaclMessageHandle *handle = (NaclMessageHandle *)user_data; + + if (key.type != PP_VARTYPE_STRING || value.type != PP_VARTYPE_STRING) + return; + if (pp::Var(key).AsString() != handle->type) + return; + + handle->handled = true; + handle->message = pp::Var(value).AsString(); + + handle->condv.notify_one(); +} + +NaclMessageHandle *naclRegisterMessageHandler(const std::string &type) +{ + NaclMessageHandle *handle = new NaclMessageHandle; + handle->handled = false; + handle->type = type; + + PSEventRegisterMessageHandler(type.c_str(), naclMessageHandlerFunc, (void *)handle); + return handle; +} + +void naclUnregisterMessageHandler(NaclMessageHandle *handle) +{ + PSEventRegisterMessageHandler(handle->type.c_str(), NULL, NULL); + delete handle; +} + +std::string naclWaitForMessage(NaclMessageHandle *handle) +{ + std::mutex mtx; + std::unique_lock <std::mutex> lck(mtx); + + while (!handle->handled) + handle->condv.wait(lck); + + handle->handled = false; + return handle->message; +} +#endif |