void eUSPInstance::HandleMessage(const pp::Var& _m) { if(!_m.is_string()) return; string m = _m.AsString(); if(m == "run") { xPlatform::Handler()->OnInit(); inited = true; Update(); audio.Play(); PostMessage("ready"); } if(!inited) return; static const string open("open:"); static const string joystick("joystick:"); if(m.length() > open.length() && m.substr(0, open.length()) == open) { string url = m.substr(open.length()); new eURLLoader(this, url, this); } else if(m.length() > joystick.length() && m.substr(0, joystick.length()) == joystick) { using namespace xOptions; string joy = m.substr(joystick.length()); eOption<int>* op_joy = eOption<int>::Find("joystick"); SAFE_CALL(op_joy)->Value(joy.c_str()); } else if(m == "reset") { Handler()->OnAction(A_RESET); } }
/* when the browser does paintownModule.postMessage('run') this will * get executed. */ virtual void HandleMessage(const pp::Var& var_message){ if (!var_message.is_string()){ return; } std::string message = var_message.AsString(); if (message == "run"){ run(); } }
pp::Var WiScriptableObject::Call(const pp::Var& method, const std::vector<pp::Var>& args, pp::Var* exception) { if (!method.is_string()) { return pp::Var(); } std::string method_name = method.AsString(); // TODO(sdk_user): 4. Make this function call whatever method has method_name // as its method ID. return pp::Var(); }
bool WiScriptableObject::HasMethod(const pp::Var& method, pp::Var* exception) { if (!method.is_string()) { return false; } std::string method_name = method.AsString(); // TODO(sdk_user): 3. Make this function return true iff method_name is equal // to any of your method IDs. bool has_method = false; return has_method; }
/// Handler for messages coming in from the browser via postMessage(). The /// @a var_message can contain anything: a JSON string; a string that encodes /// method names and arguments; etc. For example, you could use /// JSON.stringify in the browser to create a message that contains a method /// name and some parameters, something like this: /// var json_message = JSON.stringify({ "myMethod" : "3.14159" }); /// nacl_module.postMessage(json_message); /// On receipt of this message in @a var_message, you could parse the JSON to /// retrieve the method name, match it to a function call, and then call it /// with the parameter. /// @param[in] var_message The message posted by the browser. virtual void HandleMessage(const pp::Var& var_message) { if (!var_message.is_string()) return; std::string message = var_message.AsString(); pp::Var var_reply; if (message == kBenchString) { bench(); var_reply = pp::Var(kReplyString); PostMessage(var_reply); } }
static Variant to_variant(const pp::Var& p_var) { if (p_var.is_undefined() || p_var.is_null()) return Variant(); if (p_var.is_bool()) return Variant(p_var.AsBool()); if (p_var.is_double()) return Variant(p_var.AsDouble()); if (p_var.is_int()) return Variant((int64_t)p_var.AsInt()); if (p_var.is_string()) return Variant(String::utf8(p_var.AsString().c_str())); return Variant(); };
void DiagrammarInterface::HandleMessage(const pp::Var& var_message) { // Ignore the message if it is not a string. if (!var_message.is_string()) return; // Get the string message and compare it to "hello". std::string message = var_message.AsString(); if (message == "hello") { // If it matches, send our response back to JavaScript. pp::Var var_reply("running"); PostMessage(var_reply); } if (message == "status?") { pp::Var var_reply(world_.now() - world_.simulation_time()); PostMessage(var_reply); } }
// hook from javascript into the CPP code. void MoonlightInstance::HandleMessage(const pp::Var& var_message) { // Ignore the message if it is not a string. if (!var_message.is_dictionary()) return; pp::VarDictionary msg(var_message); int32_t callbackId = msg.Get("callbackId").AsInt(); std::string method = msg.Get("method").AsString(); pp::VarArray params(msg.Get("params")); if (strcmp(method.c_str(), MSG_START_REQUEST) == 0) { HandleStartStream(callbackId, params); } else if (strcmp(method.c_str(), MSG_STOP_REQUEST) == 0) { HandleStopStream(callbackId, params); } else if (strcmp(method.c_str(), MSG_OPENURL) == 0) { HandleOpenURL(callbackId, params); } else if (strcmp(method.c_str(), "httpInit") == 0) { NvHTTPInit(callbackId, params); } else if (strcmp(method.c_str(), "makeCert") == 0) { MakeCert(callbackId, params); } else if (strcmp(method.c_str(), "pair") == 0) { HandlePair(callbackId, params); } else { pp::Var response("Unhandled message received: " + method); PostMessage(response); } }
// Handler for messages coming in from the browser via postMessage(). The // @a var_message can contain be any pp:Var type; for example int, string // Array or Dictinary. Please see the pp:Var documentation for more details. // @param[in] var_message The message posted by the browser. virtual void HandleMessage(const pp::Var& var_message) { /*if (!var_message.is_string()) return; std::string message = var_message.AsString(); pp::Var var_reply; if (message == kHelloString) { var_reply = pp::Var(kReplyString); PostMessage(var_reply); }*/ // Send integer events from JS straight to the CPU if (!var_message.is_int()) { uint32_t msgInt = var_message.AsInt(); cpu->OnExtMessage(msgInt); } }
void PpapiDeviceInfo::gotDeviceId(int32_t result, const pp::Var& deviceId) { assert(isMainThread()); ScopedMutex scopedMutex(mutex_); string rawDeviceIdStr; if (result == PP_OK && deviceId.is_string()) rawDeviceIdStr = deviceId.AsString(); // Google says the device ID from PPAPI, when supported, will always be 64 // chars: a string with the text value of the numerical ID. Clamp or extend // the actual received value to this length. const string::size_type SPEC_SIZE = 64; rawDeviceIdStr.resize(SPEC_SIZE, '0'); // Now convert the text numerical ID to a binary value deviceIdBin_ = hexTextToBin(rawDeviceIdStr); assert(deviceIdBin_.size() == 32); // We are now initialized condVar_.signal(); }
void GboxInstance::HandleMessage(const pp::Var& var_message) { if (!var_message.is_string()) return; std::string message = var_message.AsString(); pp::Var var_reply; if (message == kHelloString) { theLog.info("message: '%s'", message.c_str()); Hello(); } else if (message == kPaintMethodId) { Paint(); } else if (message == kClockMethodId) { Clock(); } else if (message == "quiet") { theLog.info("message: '%s'", message.c_str()); Quiet(); } else { theLog.info("other message: '%s'", message.c_str()); Other(message); } }
void HelloWorldInstance::HandleMessage(const pp::Var& var_message) { if (!var_message.is_string()) { return; } // Post the return result back to the browser. Note that HandleMessage() is // always called on the main thread, so it's OK to post the return message // directly from here. The return post is asynhronous: PostMessage returns // immediately. //PostMessage(return_var); }
void GodotInstance::HandleMessage(const pp::Var& var_message) { switch (state) { case STATE_METHOD: { ERR_FAIL_COND(!var_message.is_string()); sd->method = var_message.AsString().c_str(); state = STATE_PARAM_COUNT; } break; case STATE_PARAM_COUNT: { ERR_FAIL_COND(!var_message.is_number()); sd->arg_count = var_message.AsInt(); state = sd->arg_count>0?STATE_PARAMS:STATE_CALL; } break; case STATE_PARAMS: { Variant p = to_variant(var_message); sd->args.push_back(p); if (sd->args.size() >= sd->arg_count) state = STATE_CALL; } break; default: break; }; if (state == STATE_CALL) { // call state = STATE_METHOD; if (sd->method == "package_finished") { GetURLHandler::Status status = package_pending->get_status(); printf("status is %i, %i, %i\n", status, GetURLHandler::STATUS_ERROR, GetURLHandler::STATUS_COMPLETED); if (status == GetURLHandler::STATUS_ERROR) { printf("Error fetching package!\n"); }; if (status == GetURLHandler::STATUS_COMPLETED) { OSNacl* os = (OSNacl*)OS::get_singleton(); os->add_package(pkg_url, package_pending->get_data()); }; memdelete(package_pending); package_pending = NULL; package_loaded = true; opengl_context_->MakeContextCurrent(this); nacl_main(init_argc, (const char**)init_argn, (const char**)init_argv); for (uint32_t i=0; i<init_argc; i++) { memfree(init_argn[i]); memfree(init_argv[i]); }; }; if (sd->method == "get_package_status") { if (package_loaded) { // post "loaded" PostMessage("loaded"); } else if (package_pending == NULL) { // post "none" PostMessage("none"); } else { // post package_pending->get_bytes_read(); PostMessage(package_pending->get_bytes_read()); }; }; }; }
virtual void HandleMessage(const pp::Var& var_message) { if (!var_message.is_dictionary ()) { return; } pp::VarDictionary::VarDictionary dic = pp::VarDictionary::VarDictionary (var_message); pp::Var s_rgba= dic.Get("rgba"); pp::Var s_width = dic.Get("width"); pp::Var s_height = dic.Get("height"); pp::Var s_quality= dic.Get("quality"); pp::Var id = dic.Get("id"); if (!s_rgba.is_array_buffer()) { PostMessage(ErrorMsg("ArrayBuffer no find")); return; } if( !s_width.is_int ()) { PostMessage(ErrorMsg("width is not int")); return; } int width = s_width.AsInt (); if (!s_height.is_int ()) { PostMessage(ErrorMsg("height is not int")); return; } int height = s_height.AsInt (); double quality = 80.0f; bool lossless = false; if (s_quality.is_string()) { if (s_quality.AsString() == "lossless") { lossless = true; } }else if (s_quality.is_number()) { double q = s_quality.AsDouble(); if (q <= 100.0 && q >= 0 ) { quality = (float)q; } } std::unique_ptr<pp::VarArrayBuffer::VarArrayBuffer> array_buffer_var(new pp::VarArrayBuffer::VarArrayBuffer(s_rgba)); uint8_t* data = static_cast<uint8_t *>(array_buffer_var->Map()); int byte_length = static_cast<int>(array_buffer_var->ByteLength()); if (byte_length != (width * height * 4)) { PostMessage(ErrorMsg("ArrayBuffer length error :"+ std::to_string(byte_length))); return; } std::shared_ptr<uint8_t> outdata(new uint8_t[byte_length]()); uint8_t * r_outdata = outdata.get(); size_t ret; if (lossless) { ret = WebPEncodeLosslessRGBA(data,width,height,width*4,&r_outdata); }else{ ret = WebPEncodeRGBA(data,width,height,width*4,quality,&r_outdata); } if (ret == 0) { PostMessage(ErrorMsg("encoder error ")); return; } pp::VarArrayBuffer::VarArrayBuffer data_value = pp::VarArrayBuffer::VarArrayBuffer (ret); uint8_t* r_data_value = static_cast<uint8_t*>(data_value.Map()); for (uint32_t i = 0; i < ret; ++i) { r_data_value[i] = r_outdata[i]; } data_value.Unmap(); pp::VarDictionary::VarDictionary ret_msg = pp::VarDictionary::VarDictionary (); pp::Var id_key = pp::Var::Var("id"); ret_msg.Set (id_key,id); pp::Var data_key = pp::Var::Var("data"); ret_msg.Set (data_key,data_value); PostMessage(ret_msg); }
virtual void HandleMessage(const pp::Var& var_message) { // Receive a message from javascript. if (var_message.is_string()) { SkString msg(var_message.AsString().c_str()); if (msg.startsWith("init")) { } else if (msg.startsWith("LoadSKP")) { size_t startIndex = strlen("LoadSKP"); size_t dataSize = msg.size()/sizeof(char) - startIndex; SkBase64 decodedData; decodedData.decode(msg.c_str() + startIndex, dataSize); size_t decodedSize = 3 * (dataSize / 4); SkDebugf("Got size: %d\n", decodedSize); if (!decodedData.getData()) { SkDebugf("Failed to decode SKP\n"); return; } SkMemoryStream pictureStream(decodedData.getData(), decodedSize); SkPicture* picture = SkPicture::CreateFromStream(&pictureStream); if (NULL == picture) { SkDebugf("Failed to create SKP.\n"); return; } fDebugger.loadPicture(picture); picture->unref(); // Set up the command list. SkTArray<SkString>* commands = fDebugger.getDrawCommandsAsStrings(); PostMessage("ClearCommands"); for (int i = 0; i < commands->count(); ++i) { SkString addCommand("AddCommand:"); addCommand.append((*commands)[i]); PostMessage(addCommand.c_str()); } PostMessage("UpdateCommands"); // Set the overview text. SkString overviewText; fDebugger.getOverviewText(NULL, 0.0, &overviewText, 1); overviewText.prepend("SetOverview:"); PostMessage(overviewText.c_str()); // Draw the SKP. if (!fFlushLoopRunning) { Paint(); } } else if (msg.startsWith("CommandSelected:")) { size_t startIndex = strlen("CommandSelected:"); int index = atoi(msg.c_str() + startIndex); fDebugger.setIndex(index); if (!fFlushLoopRunning) { Paint(); } } else if (msg.startsWith("Rewind")) { fCanvas->clear(SK_ColorWHITE); fDebugger.setIndex(0); if (!fFlushLoopRunning) { Paint(); } } else if (msg.startsWith("StepBack")) { fCanvas->clear(SK_ColorWHITE); int currentIndex = fDebugger.index(); if (currentIndex > 1) { fDebugger.setIndex(currentIndex - 1); if (!fFlushLoopRunning) { Paint(); } } } else if (msg.startsWith("Pause")) { // TODO(borenet) } else if (msg.startsWith("StepForward")) { int currentIndex = fDebugger.index(); if (currentIndex < fDebugger.getSize() -1) { fDebugger.setIndex(currentIndex + 1); if (!fFlushLoopRunning) { Paint(); } } } else if (msg.startsWith("Play")) { fDebugger.setIndex(fDebugger.getSize() - 1); if (!fFlushLoopRunning) { Paint(); } } } }
virtual void dosomething(const pp::Var& var_message) { // std::cout << "HandleMessage... " << std::endl; int point_size(0); // printf( "handler thread id '%p'\n", pthread_self()); pp::VarDictionary dict; pp::Var command_name; if (var_message.is_dictionary()) { dict = pp::VarDictionary(var_message); if (!dict.HasKey("command")) { PostError("broker", "message JSON provided no 'command' member!", "null"); return; } command_name = dict.Get("command"); } else { PostError("broker", "No dictionary object was provided!", "null"); return; } if (!dict.HasKey("id")) { PostError("broker", "message JSON provided no 'id' member!", "null"); return; } if (!dict.Get("id").is_string()) { PostError("broker", "'id' member is not a string!", "null"); return; } std::string id = dict.Get("id").AsString(); if (boost::iequals(command_name.AsString(), "open")) { bool opened = open(var_message); if (!opened) { PostError("open","Unable to open file", id); return; } PostSuccess("open", pp::Var(pp::Var::Null()), id, "File opened successfully"); return; // open has set any errors } if (boost::iequals(command_name.AsString(), "close")) { int res = fclose(fp_); if (res) { std::ostringstream errors; errors << "fclose returned message " << res << " error: '" << strerror(errno) <<"'"; PostError("close", errors.str(), id); return; } header_ = LASHeader(); zip_ = LASzip(); unzipper_ = LASunzipper(); fp_ = NULL; bDidReadHeader_ = 0; pointIndex_ = 0; delete[] point_; point_ = 0; delete[] bytes_; bytes_ = 0; delete buffer_; buffer_ = 0; PostSuccess("close", pp::Var(pp::Var::Null()), id, "File closed successfully"); return; // close has set any errors } if (boost::iequals(command_name.AsString(), "getheader")) { if (!fp_) { PostError("broker", "No file is open!", id); return; } bDidReadHeader_ = readHeader(header_, var_message); if (!bDidReadHeader_) { PostError("getheader", "Header read failed!", id); return; } PostSuccess("getheader", header_.AsVar(), id, "Header read successful"); return; } // std::ostringstream errors; if (boost::iequals(command_name.AsString(), "read")) { if (!fp_) { PostError("read", "No file is open!", id); return; } if (!bDidReadHeader_) { PostError("read", "No header has been fetched!", id); return; } if (pointIndex_ == header_.point_count) { PostError("read", "All done reading. 'close' and 'open' the file again", id); return; } uint32_t count(header_.point_count); if (dict.HasKey("count")) { pp::Var cnt = dict.Get("count"); if (!cnt.is_int()) { PostError("read", "'count' is not an integer object!", id); return; } count = cnt.AsInt(); // std::cout << "Fetched count as " << count << std::endl; } #define MAX_POINT_COUNT 2000000 if (count > MAX_POINT_COUNT) { std::ostringstream errors; errors << "'count' is too large, choose a smaller value"; PostError("read", errors.str(), id); return; } uint32_t skip(0); if (dict.HasKey("skip")) { pp::Var s = dict.Get("skip"); if (!s.is_int()) { PostError("read", "'skip' is not an integer object!", id); return; } skip = s.AsInt(); } uint32_t start(0); if (dict.HasKey("start")) { pp::Var st = dict.Get("start"); if (!st.is_int()) { PostError("read", "'start' is not an integer object!", id); return; } start = st.AsInt(); } uint64_t num_left = (uint64_t)header_.point_count - (uint64_t)pointIndex_; uint64_t total_bytes = (uint64_t)count * (uint64_t)header_.point_record_length; if (!buffer_) { try { buffer_ = new pp::VarArrayBuffer(total_bytes); } catch (...) { std::ostringstream error; error << "Unable to allocate buffer of size " << total_bytes << " to read " << count << " points. please try a smaller buffer size" << std::endl; PostError("read", error.str(), id); return; } // std::cout << "making new VarArrayBuffer of size" << total_bytes << std::endl; } // std::cout << "allocated buffer" << std::endl; if (buffer_->ByteLength() < total_bytes) { delete buffer_; try { buffer_ = new pp::VarArrayBuffer(total_bytes); } catch (std::bad_alloc&) { std::ostringstream error; error << "Unable to allocate buffer of size " << total_bytes << " to read " << count << " points. please try a smaller buffer size" << std::endl; PostError("read", error.str(), id); return; } // std::cout << "buffer was wrong size " << buffer_->ByteLength() << ", making new VarArrayBuffer of size" << total_bytes << std::endl; } // std::cout << "mapping buffer" << std::endl; unsigned char* array_start; try { array_start = static_cast<unsigned char*>(buffer_->Map()); } catch (std::bad_alloc&) { std::ostringstream error; error << "Unable to allocate Map of size " << total_bytes << " to read " << count << " points. please try a smaller buffer size" << std::endl; PostError("read", error.str(), id); return; } // std::cout << "skip value: " << skip << std::endl; // std::cout << "start value: " << start << std::endl; unsigned char* data = array_start; uint32_t howManyToRead(count); uint32_t howManyRead(0); bool bHasMoreData(true); int i(0); while ( howManyRead != howManyToRead) // for (int i = howManyToRead; i != 0; --i) { bool bDoKeep(false); // fills in bytes_ bool ok = unzipper_.read(point_); if (!ok) { std::ostringstream error; error << "Unable to read point at index " << i << std::endl; error << "error msg: " << unzipper_.get_error() << std::endl; PostError("read", error.str(), id); return; } if (skip != 0) { if ((skip != 0) && ((pointIndex_ + start) % skip == 0) ) { bDoKeep = true; // std::cout << "keeping " << i <<"th point" << std::endl; } else { // std::cout << "skipping " << i << "th point" << std::endl; } } else { // No skip or start is set, we copy it all bDoKeep = true; } if (start != 0) { if (pointIndex_ < start) { bDoKeep = false; // std::cout << "reskipping " << i << "th point" << std::endl; } } if (bDoKeep) // if skip is 0, just copy all the time { std::copy(bytes_, bytes_ + header_.point_record_length, data); data += header_.point_record_length; howManyRead++; // std::cout << "keeping " << i <<"th point" << std::endl; } pointIndex_++; if (pointIndex_ == header_.point_count) { bHasMoreData = false; break; } i += 1; } // std::cout << "done reading" << std::endl; pp::VarDictionary dict; dict.Set("status", true); dict.Set("hasMoreData", bHasMoreData); dict.Set("message", "Done reading data"); try { dict.Set("result", *buffer_); } catch (std::bad_alloc&) { std::ostringstream error; error << "Unable to return ArrayBuffer of size " << total_bytes << " to read " << count << " points. please try a smaller buffer size" << std::endl; PostError("read", error.str(), id); return; } // PostSuccess("read", dict, id); dict.Set("error", false); dict.Set("method", "read"); dict.Set("id", id); dict.Set("count", (int32_t)howManyRead); dict.Set("skip", (int32_t)skip); dict.Set("start", (int32_t)start); try { // std::cout << "Posting message" << std::endl; PostMessage(dict); // std::cout << "Posted message" << std::endl; } catch (std::bad_alloc&) { std::ostringstream error; error << "Unable to post ArrayBuffer of size " << total_bytes << " to read " << count << " points. please try a smaller buffer size" << std::endl; PostError("read", error.str(), id); return; } // std::cout << "posted message" << std::endl; return; // int32_t x = *(int32_t*)&start[0]; // int32_t y = *(int32_t*)&start[4]; // int32_t z = *(int32_t*)&start[8]; // // double xs = applyScaling(x, header_.scale[0], header_.offset[0]); // double ys = applyScaling(y, header_.scale[1], header_.offset[1]); // double zs = applyScaling(z, header_.scale[2], header_.offset[2]); // // std::ostringstream oss; // oss << "scale[0]: " << header_.scale[0] <<std::endl; // oss << "scale[1]: " << header_.scale[1] <<std::endl; // oss << "scale[2]: " << header_.scale[2] <<std::endl; // oss << "offset[0]: " << header_.offset[0] <<std::endl; // oss << "offset[1]: " << header_.offset[1] <<std::endl; // oss << "offset[2]: " << header_.offset[2] <<std::endl; // oss << "min[0]: " << header_.mins[0] <<std::endl; // oss << "min[1]: " << header_.mins[1] <<std::endl; // oss << "min[2]: " << header_.mins[2] <<std::endl; // oss << "max[0]: " << header_.maxs[0] <<std::endl; // oss << "max[1]: " << header_.maxs[1] <<std::endl; // oss << "max[2]: " << header_.maxs[2] <<std::endl; // // oss << "x: " << x << std::endl; // oss << "y: " << y << std::endl; // oss << "z: " << z << std::endl; // // oss << "x: " << xs << std::endl; // oss << "y: " << ys << std::endl; // oss << "z: " << zs << std::endl; // PostMessage(oss.str()); // PostMessage(header_.AsVar()); // PostMessage(status(true, "Done reading data")); // return; } PostError("broker", "Command not found", id); return; // unsigned char** point; // bool ok = unzipper_.read(point_); // if (!ok) // { // errors << "Unable to read point!" << std::endl; // errors << "error msg: " << unzipper_.get_error() << std::endl; // PostError(errors.str()); // return; // } // // int32_t x = *(int32_t*)&bytes_[0]; // int32_t y = *(int32_t*)&bytes_[4]; // int32_t z = *(int32_t*)&bytes_[8]; // // double xs = applyScaling(x, header_.scale[0], header_.offset[0]); // double ys = applyScaling(y, header_.scale[1], header_.offset[1]); // double zs = applyScaling(z, header_.scale[2], header_.offset[2]); // // std::ostringstream oss; // oss << "scale[0]: " << header_.scale[0] <<std::endl; // oss << "scale[1]: " << header_.scale[1] <<std::endl; // oss << "scale[2]: " << header_.scale[2] <<std::endl; // oss << "offset[0]: " << header_.offset[0] <<std::endl; // oss << "offset[1]: " << header_.offset[1] <<std::endl; // oss << "offset[2]: " << header_.offset[2] <<std::endl; // oss << "min[0]: " << header_.mins[0] <<std::endl; // oss << "min[1]: " << header_.mins[1] <<std::endl; // oss << "min[2]: " << header_.mins[2] <<std::endl; // oss << "max[0]: " << header_.maxs[0] <<std::endl; // oss << "max[1]: " << header_.maxs[1] <<std::endl; // oss << "max[2]: " << header_.maxs[2] <<std::endl; // // oss << "x: " << x << std::endl; // oss << "y: " << y << std::endl; // oss << "z: " << z << std::endl; // // oss << "x: " << xs << std::endl; // oss << "y: " << ys << std::endl; // oss << "z: " << zs << std::endl; // PostMessage(header_.AsVar()); }