void URLToFileURLCallback(const char* url, char* buffer, int bufferLength) { strncpy(buffer, url, bufferLength); buffer[bufferLength - 1] = '\0'; try { string newURL = url; string path = URLUtils::URLToPath(newURL); if (path != newURL) newURL = URLUtils::PathToFileURL(path); strncpy(buffer, newURL.c_str(), bufferLength); buffer[bufferLength - 1] = '\0'; } catch (ValueException& e) { SharedString ss = e.DisplayString(); Logger* log = Logger::Get("UI.URL"); log->Error("Could not convert %s to a path: %s", url, ss->c_str()); } catch (...) { Logger* log = Logger::Get("UI.URL"); log->Error("Could not convert %s to a path", url); } }
VALUE RubyUtils::GenericKMethodCall(SharedKMethod method, VALUE args) { ValueList kargs; for (int i = 0; i < RARRAY(args)->len; i++) { VALUE rarg = rb_ary_entry(args, i); SharedValue arg = RubyUtils::ToKrollValue(rarg); Value::Unwrap(arg); kargs.push_back(arg); } try { SharedValue result = method->Call(kargs); return RubyUtils::ToRubyValue(result); } catch (ValueException& e) { // TODO: Eventually wrap these up in a special exception // class so that we can unwrap them into ValueExceptions again SharedString ss = e.DisplayString(); rb_raise(rb_eStandardError, ss->c_str()); return Qnil; } }
SharedPtr<Module> Host::LoadModule(std::string& path, ModuleProvider *provider) { ScopedLock lock(&moduleMutex); //TI-180: Don't load the same module twice SharedPtr<Module> module = this->GetModuleByPath(path); if (!module.isNull()) { logger->Warn("Module cannot be loaded twice: %s", path.c_str()); return NULL; } try { logger->Debug("Loading module: %s", path.c_str()); this->CopyModuleAppResources(path); this->ReadModuleManifest(path); module = provider->CreateModule(path); module->SetProvider(provider); // set the provider module->Initialize(); // loadedModules keeps track of the Module which is loaded from the // module shared-object, while application->modules holds the KComponent // metadata description of each module. this->loadedModules.push_back(module); this->application->UsingModule(module->GetName(), module->GetVersion(), path); logger->Info("Loaded module = %s", module->GetName().c_str()); } catch (kroll::ValueException& e) { SharedString s = e.GetValue()->DisplayString(); logger->Error("Could not load module (%s): %s", path.c_str(), s->c_str()); #ifdef OS_OSX KrollDumpStackTrace(); #endif } catch(std::exception &e) { string msg = e.what(); logger->Error("Could not load module (%s): %s", path.c_str(), msg.c_str()); #ifdef OS_OSX KrollDumpStackTrace(); #endif } catch(...) { logger->Error("Could not load module (%s)", path.c_str()); #ifdef OS_OSX KrollDumpStackTrace(); #endif } return module; }
void Win32TrayItem::HandleDoubleLeftClick() { try { this->FireEvent(Event::DOUBLE_CLICKED); } catch (ValueException& e) { Logger* logger = Logger::Get("UI.Win32TrayItem"); SharedString ss = e.DisplayString(); logger->Error("Tray icon double left click callback failed: %s", ss->c_str()); } }
void RubyEvaluator::Evaluate(const ValueList& args, KValueRef result) { args.VerifyException("evaluate", "s s s o"); //const char *mimeType = args.GetString(0).c_str(); std::string name = args.GetString(1); std::string code = args.GetString(2); global_object = args.GetObject(3); VALUE ctx = this->GetContext(global_object); VALUE rargs = rb_ary_new(); rb_ary_push(rargs, ctx); rb_ary_push(rargs, rb_str_new2(code.c_str())); int error; VALUE returnValue = rb_protect(reval_do_call, rargs, &error); RubyEvaluator::ContextToGlobal(ctx, global_object); if (error != 0) { std::string error("An error occured while parsing Ruby ("); error += name; error += "): "; // Display a stringified version of the exception. VALUE exception = rb_gv_get("$!"); KValueRef v = RubyUtils::ToKrollValue(exception); SharedString ss = v->DisplayString(); error.append(ss->c_str()); // Try to make a nice backtrace for the user. VALUE backtrace = rb_funcall(exception, rb_intern("backtrace"), 0); VALUE rBacktraceString = rb_funcall(backtrace, rb_intern("join"), 1, rb_str_new2("\n")); if (TYPE(rBacktraceString) == T_STRING) { error.append("\n"); error.append(StringValuePtr(rBacktraceString)); } Logger *logger = Logger::Get("Ruby"); logger->Error(error); result->SetUndefined(); return; } result->SetValue(RubyUtils::ToKrollValue(returnValue)); }
void MonkeyBinding::Callback(const ValueList &args, SharedValue result) { SharedKObject event = args.at(1)->ToObject(); std::string url_value = event->Get("url")->ToString(); std::vector< std::pair< std::pair< VectorOfPatterns,VectorOfPatterns >,std::string> >::iterator iter = scripts.begin(); while(iter!=scripts.end()) { std::pair< std::pair< VectorOfPatterns,VectorOfPatterns >, std::string> e = (*iter++); VectorOfPatterns include = e.first.first; VectorOfPatterns exclude = e.first.second; if (Matches(exclude,url_value)) { continue; } if (Matches(include,url_value)) { // I got a castle in brooklyn, that's where i dwell try { SharedKMethod eval = event->Get("scope")->ToObject()->Get("window")->ToObject()->Get("eval")->ToMethod(); #ifdef DEBUG std::cout << ">>> loading user script for " << url_value << std::endl; #endif eval->Call(Value::NewString(e.second)); } catch (ValueException &ex) { Logger* logger = Logger::Get("Monkey"); SharedString ss = ex.DisplayString(); int line = -1; if (ex.GetValue()->IsObject() && ex.GetValue()->ToObject()->Get("line")->IsNumber()) { line = ex.GetValue()->ToObject()->Get("line")->ToInt(); } logger->Error( "Exception generated evaluating user script for %s " "(line %i): %s", url_value.c_str(), line, ss->c_str()); } catch (std::exception &ex) { Logger* logger = Logger::Get("Monkey"); logger->Error("Exception generated evaluating user script for %s, Exception: %s",url_value.c_str(),ex.what()); } } } }
static void FireEventCallback(KMethodRef callback, AutoPtr<Event> event, bool synchronous, KObjectRef thisObject) { try { RunOnMainThread(callback, thisObject, ValueList(Value::NewObject(event)), synchronous); } catch (ValueException& e) { Logger* logger = Logger::Get("KEventObject"); SharedString ss = e.DisplayString(); logger->Error("Exception caught during event callback (target=[%s]): %s", event->target->GetType().c_str(), ss->c_str()); } }
void Win32TrayItem::HandleLeftClick() { if (callback.isNull()) return; try { ValueList args; callback->Call(args); } catch (ValueException& e) { Logger* logger = Logger::Get("UI.Win32TrayItem"); SharedString ss = e.DisplayString(); logger->Error("Tray icon callback failed: %s", ss->c_str()); } }
void TrayClickedCallback(GtkStatusIcon *status_icon, gpointer data) { TrayItemGtk* item = static_cast<TrayItemGtk*>(data); KMethodRef cb = item->GetCallback(); if (cb.isNull()) return; try { ValueList args; cb->Call(args); } catch (ValueException& e) { Logger* logger = Logger::Get("UI.TrayItemGtk"); SharedString ss = e.DisplayString(); logger->Error("Tray icon callback failed: %s", ss->c_str()); } }
void MonkeyBinding::EvaluateUserScript( SharedKObject event, std::string& url, SharedKObject windowObject, std::string& scriptSource) { static Logger *logger = Logger::Get("Monkey"); // I got a castle in brooklyn, that's where i dwell // Word, brother. if (!windowObject->Get("eval")->IsMethod()) { logger->Error("Found a window object without an " "eval function (%s) -- aborting", url.c_str()); return; } SharedKObject target = event->GetObject("target"); if (!windowObject->Get(GLOBAL_NS_VARNAME)->IsObject() && !target.isNull() && target->Get("insertAPI")->IsMethod()) { logger->Info("Forcing Titanium API into: %s\n", url.c_str()); target->CallNS("insertAPI", Value::NewObject(windowObject)); } SharedKMethod evalFunction = windowObject->GetMethod("eval"); logger->Info("Loading userscript for %s\n", url.c_str()); try { evalFunction->Call(Value::NewString(scriptSource)); } catch (ValueException &ex) { SharedString ss = ex.DisplayString(); int line = -1; if (ex.GetValue()->IsObject() && ex.GetValue()->ToObject()->Get("line")->IsNumber()) { line = ex.GetValue()->ToObject()->Get("line")->ToInt(); } logger->Error( "Exception generated evaluating user script for %s " "(line %i): %s", url.c_str(), line, ss->c_str()); } }
std::string TiURLToPath(const std::string& tiURL) { try { Poco::URI inURI = Poco::URI(tiURL); if (inURI.getScheme() != "ti") { return tiURL; } std::string host(inURI.getHost()); SharedApplication app = Host::GetInstance()->GetApplication(); std::string path(app->GetComponentPath(host)); if (path.empty()) { throw ValueException::FromString("Could not find component "+host); } std::vector<std::string> segments; inURI.getPathSegments(segments); for (size_t i = 0; i < segments.size(); i++) { path = FileUtils::Join(path.c_str(), segments[i].c_str(), NULL); } return path; } catch (ValueException& e) { SharedString ss = e.DisplayString(); Logger* log = Logger::Get("URLUtils"); log->Error("Could not convert %s to a path: %s", tiURL.c_str(), ss->c_str()); } catch (...) { Logger* log = Logger::Get("URLUtils"); log->Error("Could not convert %s to a path", tiURL.c_str()); } return tiURL; }
std::string URLToPath(const std::string& url) { Poco::URI inURI = Poco::URI(url); try { if (url == BlankPageURL()) { return BlankURLToFilePath(); } if (inURI.getScheme() == "ti") { return TiURLToPath(url); } else if (inURI.getScheme() == "app") { return AppURLToPath(url); } else if (inURI.getScheme().empty()) { // There is no scheme for this URL, so we have to/ guess at this point if // it's a path or a relative app:// URL. If a file can be found, assume thi // is a file path. if (FileUtils::IsFile(url)) return url; // Otherwise treat this like an app:// URL relative to the root. std::string newURL("app://"); newURL.append(url); return AppURLToPath(newURL); } } catch (ValueException& e) { SharedString ss = e.DisplayString(); Logger* log = Logger::Get("URLUtils"); log->Error("Could not convert %s to a path: %s", url.c_str(), ss->c_str()); } return url; }
std::string URLToPath(std::string& url) { Poco::URI inURI = Poco::URI(url); try { if (inURI.getScheme() == "ti") { return TiURLToPath(url); } else if (inURI.getScheme() == "app") { return AppURLToPath(url); } } catch (ValueException& e) { SharedString ss = e.DisplayString(); Logger* log = Logger::Get("URLUtils"); log->Error("Could not convert %s to a path: %s", url.c_str(), ss->c_str()); } return url; }
std::string AppURLToPath(const std::string& inURL) { try { Poco::URI inURI = Poco::URI(inURL); if (inURI.getScheme() != "app") { return inURL; } std::string appURL(NormalizeAppURL(inURL)); inURI = Poco::URI(appURL); SharedApplication app = Host::GetInstance()->GetApplication(); std::string path(app->GetResourcesPath()); std::vector<std::string> segments; inURI.getPathSegments(segments); for (size_t i = 0; i < segments.size(); i++) { path = FileUtils::Join(path.c_str(), segments[i].c_str(), NULL); } return path; } catch (ValueException& e) { SharedString ss = e.DisplayString(); Logger* log = Logger::Get("URLUtils"); log->Error("Could not convert %s to a path: %s", inURL.c_str(), ss->c_str()); } catch (...) { Logger* log = Logger::Get("URLUtils"); log->Error("Could not convert %s to a path", inURL.c_str()); } return inURL; }
void Network::NetworkStatusChange(bool online) { static Logger* log = Logger::Get("NetworkStatus"); log->Debug("ti.Network: Online status changed ==> %i", online); this->Set("online", Value::NewBool(online)); ValueList args = ValueList(); args.push_back(Value::NewBool(online)); std::vector<Listener>::iterator it = this->listeners.begin(); while (it != this->listeners.end()) { KMethodRef callback = (*it++).callback; try { RunOnMainThread(callback, args, false); } catch(ValueException& e) { SharedString ss = e.GetValue()->DisplayString(); log->Error("Network.NetworkStatus callback failed: %s", ss->c_str()); } } }
KValueRef KObject::Get(SharedString name) { return this->Get(name->c_str()); }
void KObject::Set(SharedString name, KValueRef value) { this->Set(name->c_str(), value); }
int Host::Run() { if (this->waitForDebugger) { #ifdef OS_WIN32 DebugBreak(); #else printf("Waiting for debugger (Press Any Key to Continue pid=%i)...\n", getpid()); getchar(); #endif } try { ScopedLock lock(&moduleMutex); this->AddModuleProvider(this); this->LoadModules(); } catch (ValueException e) { SharedString ss = e.GetValue()->DisplayString(); logger->Error(*ss); return 1; } // Depending on the implementation of platform-specific host, // it may block in Start() or implement a UI loop which will // be continually called until this->running becomes false. try { this->running = this->Start(); if (this->runUILoop) { while (this->running) { if (!this->RunLoop()) { break; } } } } catch (kroll::ValueException& e) { SharedString s = e.GetValue()->DisplayString(); logger->Error("Caught exception in main loop: %s", s->c_str()); } ScopedLock lock(&moduleMutex); this->Stop(); this->UnloadModuleProviders(); this->UnloadModules(); this->globalObject = NULL; // Stop the profiler, if it was enabled StopProfiling(); logger->Notice("Exiting with exit code: %i", exitCode); Logger::Shutdown(); return this->exitCode; }