void network_error() { Json::Value writerRoot; writerRoot["result"] = 1; writerRoot["type"] = "network error"; Json::StyledWriter writer; std::string msg(writer.write(writerRoot)); struct PP_Var var = PSInterfaceVar()->VarFromUtf8(msg.data(), msg.length()); PSInterfaceMessaging()->PostMessage(PSGetInstanceId(), var); PSInterfaceVar()->Release(var); }
struct PP_Var nspawn_send_request(struct PP_Var req_var) { /* * naclprocess.js is required in order send requests to JavaScript. * If NACL_PROCESS is not set in the environment then we assume it is * not present and exit early. Without this check we would block forever * waiting for a response for the JavaScript side. * * Only check this once per process, as some programs (emacs) * engage in manipulation of the environment that may not be safely * read at all times. */ static int checked_for_nacl_process = 0; if (!checked_for_nacl_process) { const char* naclprocess = getenv("NACL_PROCESS"); if (naclprocess == NULL) { fprintf(stderr, "nspawn_send_request called without NACL_PROCESS set\n"); return PP_MakeNull(); } checked_for_nacl_process = 1; } int64_t id = get_request_id(); char req_id[64]; sprintf(req_id, "%lld", id); nspawn_dict_setstring(req_var, "id", req_id); struct NaClSpawnReply reply; pthread_mutex_init(&reply.mu, NULL); pthread_cond_init(&reply.cond, NULL); PSEventRegisterMessageHandler(req_id, &handle_reply, &reply); PSInterfaceMessaging()->PostMessage(PSGetInstanceId(), req_var); nspawn_var_release(req_var); pthread_mutex_lock(&reply.mu); /* * Wait for response for JavaScript. This can block for an unbounded amount * of time (e.g. waiting for a response to waitpid). */ int error = pthread_cond_wait(&reply.cond, &reply.mu); pthread_mutex_unlock(&reply.mu); pthread_cond_destroy(&reply.cond); pthread_mutex_destroy(&reply.mu); PSEventRegisterMessageHandler(req_id, NULL, &reply); if (error != 0) { fprintf(stderr, "nspawn_send_request: pthread_cond_timedwait: %s\n", strerror(error)); return PP_MakeNull(); } return reply.result_var; }
static void PostMessageString(const char* message) { struct PP_Var message_var = PSInterfaceVar()->VarFromUtf8(message, strlen(message)); PSInterfaceMessaging()->PostMessage(g_ps_instance, message_var); PSInterfaceVar()->Release(message_var); }
// PostUpdateMessage() helper function for sendimg small messages to JS. void SimpleTemplate::PostUpdateMessage(const char* message_name, double value) { pp::VarDictionary message; message.Set("message", message_name); message.Set("value", value); PSInterfaceMessaging()->PostMessage(PSGetInstanceId(), message.pp_var()); }