void UIBinding::Log(Logger::Level level, std::string& message) { if (level > Logger::LWARN) return; std::string methodName("warn"); if (level < Logger::LWARN) methodName = "error"; std::string origMethodName(methodName); origMethodName.append("_orig"); std::vector<AutoUserWindow>& openWindows = UIBinding::GetInstance()->GetOpenWindows(); for (size_t i = 0; i < openWindows.size(); i++) { KObjectRef domWindow = openWindows[i]->GetDOMWindow(); if (domWindow.isNull()) continue; KObjectRef console = domWindow->GetObject("console", 0); if (console.isNull()) continue; KMethodRef method = console->GetMethod(origMethodName.c_str(), 0); if (method.isNull()) method = console->GetMethod(methodName.c_str(), 0); RunOnMainThread(method, ValueList(Value::NewString(message)), false); } }
KValueRef Script::Evaluate(const char *mimeType, const char *name, const char *code, KObjectRef scope) { KObjectRef evaluator = this->FindEvaluatorWithMethod("canEvaluate", mimeType); if (!evaluator.isNull()) { KMethodRef evaluate = evaluator->GetMethod("evaluate"); if (!evaluate.isNull()) { ValueList args; args.push_back(Value::NewString(mimeType)); args.push_back(Value::NewString(name)); args.push_back(Value::NewString(code)); args.push_back(Value::NewObject(scope)); return evaluate->Call(args); } else { throw ValueException::FromFormat( "Error evaluating: No \"evaluate\" method found on evaluator for mimeType: \"%s\"", mimeType); } } else { throw ValueException::FromFormat("Error evaluating: No evaluator found for mimeType: \"%s\"", mimeType); } }
void Notification::Configure(KObjectRef properties) { this->title = properties->GetString("title"); this->message = properties->GetString("message"); this->iconURL = properties->GetString("icon"); this->timeout = properties->GetInt("timeout", -1); this->clickedCallback = properties->GetMethod("callback"); }
void Network::_GetHostByAddress(const ValueList& args, KValueRef result) { if (args.at(0)->IsObject()) { KObjectRef obj = args.at(0)->ToObject(); AutoPtr<IPAddress> b = obj.cast<IPAddress>(); if (!b.isNull()) { // in this case, they've passed us an IPAddressBinding // object, which we can just retrieve the ipaddress // instance and resolving using it Poco::Net::IPAddress addr(b->GetAddress()->toString()); AutoPtr<Host> binding = new Host(addr); if (binding->IsInvalid()) { throw ValueException::FromString("Could not resolve address"); } result->SetObject(binding); return; } else { KMethodRef toStringMethod = obj->GetMethod("toString"); if (toStringMethod.isNull()) throw ValueException::FromString("Unknown object passed"); result->SetObject(GetHostBinding(toStringMethod->Call()->ToString())); return; } } else if (args.at(0)->IsString()) { // in this case, they just passed in a string so resolve as normal result->SetObject(GetHostBinding(args.GetString(0))); } }
AutoPtr<PreprocessData> Script::Preprocess(const char *url, KObjectRef scope) { KObjectRef evaluator = this->FindEvaluatorWithMethod("canPreprocess", url); if (!evaluator.isNull()) { KMethodRef preprocess = evaluator->GetMethod("preprocess"); if (!preprocess.isNull()) { ValueList args; args.push_back(Value::NewString(url)); args.push_back(Value::NewObject(scope)); KValueRef result = preprocess->Call(args); if (result->IsObject()) { KObjectRef object = result->ToObject(); AutoPtr<PreprocessData> data = new PreprocessData(); if (object->HasProperty("data")) { KValueRef objectData = object->Get("data"); if (objectData->IsObject()) { BlobRef blobData = objectData->ToObject().cast<Blob>(); if (!blobData.isNull()) { data->data = blobData; } } else if (objectData->IsString()) { data->data = new Blob(objectData->ToString(), strlen(objectData->ToString())); } } else { throw ValueException::FromString("Preprocessor didn't return any data"); } if (object->HasProperty("mimeType")) { data->mimeType = object->Get("mimeType")->ToString(); } else { throw ValueException::FromString("Preprocessor didn't return a mimeType"); } return data; } } else { throw ValueException::FromFormat( "Error preprocessing: No \"preprocess\" method found on evaluator for url: \"%s\"", url); } } else { throw ValueException::FromFormat("Error preprocessing: No evaluator found for url: \"%s\"", url); } return 0; }
bool HTTPClientBinding::BeginRequest(KValueRef sendData) { if (this->Get("connected")->ToBool()) { return false; } // Reset internal variables for new request if (this->dirty) { this->Reset(); } // Determine what data type we have to send if (sendData->IsObject()) { KObjectRef dataObject = sendData->ToObject(); KMethodRef nativePathMethod(dataObject->GetMethod("nativePath")); KMethodRef sizeMethod(dataObject->GetMethod("size")); if (nativePathMethod.isNull() || sizeMethod.isNull()) { std::string err("Unsupported File-like object: did not have" "nativePath and size methods"); logger->Error(err); throw ValueException::FromString(err); } const char* filename = nativePathMethod->Call()->ToString(); if (!filename) { std::string err("Unsupported File-like object: nativePath method" "did not return a String"); logger->Error(err); throw ValueException::FromString(err); } this->datastream = new std::ifstream(filename, std::ios::in | std::ios::binary); if (this->datastream->fail()) { std::string err("Failed to open file: "); err.append(filename); logger->Error(err); throw ValueException::FromString(err); } this->contentLength = sizeMethod->Call()->ToInt(); } else if (sendData->IsString()) { std::string dataString(sendData->ToString()); if (!dataString.empty()) { this->datastream = new std::istringstream(dataString, std::ios::in | std::ios::binary); this->contentLength = dataString.length(); } } else { // Sending no data this->datastream = 0; } this->dirty = true; this->Set("connected", Value::NewBool(true)); if (this->async) { this->thread = new Poco::Thread(); this->thread->start(*this); } else { this->ExecuteRequest(); } return true; }