예제 #1
0
    void APIBinding::_CreateDependency(const ValueList& args, KValueRef result)
    {
        args.VerifyException("createDepenendency", "i,s,s,?i");
        int type = args.GetInt(0, UNKNOWN);
        string name = args.GetString(1);
        string version = args.GetString(2);
        int requirement = (int) args.GetNumber(3, Dependency::EQ);

        if (type != MODULE && type != RUNTIME
            && type != SDK && type != MOBILESDK
            && type != APP_UPDATE)
        {
            throw ValueException::FromString(
                "Tried to create a dependency with an unknown dependency type");
        }
        else if (requirement != Dependency::EQ
            && requirement != Dependency::GT
            && requirement != Dependency::LT
            && requirement != Dependency::GTE
            && requirement != Dependency::LTE)
        {
            throw ValueException::FromString(
                "Tried to create a dependency with an unknown requirement type");
        }
        else
        {
            SharedDependency d = Dependency::NewDependencyFromValues(
                static_cast<KComponentType>(type), name, version);
            KObjectRef dBinding = new DependencyBinding(d);
            result->SetObject(dBinding);
        }
    }
예제 #2
0
/*static*/
KValueRef Codec::ExtractZipAsync(const ValueList& args)
{
    std::string zipFile = args.GetString(0);
    std::string directory = args.GetString(1);
    AutoPtr<AsyncJob> job = args.GetObject(2).cast<AsyncJob>();
    KMethodRef callback = 0;
    if (args.size() > 3)
    {
        callback = args.GetMethod(3);
    }

    std::ifstream stream(UTF8ToSystem(zipFile).c_str(), std::ios::binary);
    Poco::Zip::Decompress decompressor(stream, directory);
    try
    {
        decompressor.decompressAllFiles();
    }
    catch (std::exception& e)
    {
        Logger::Get("Codec")->Error("exception decompressing: %s", e.what());
        throw ValueException::FromFormat("Exception during extraction: %s", e.what());
    }

    stream.close();

    if (!callback.isNull())
    {
        ValueList args;
        args.push_back(Value::NewString(directory));
        RunOnMainThread(callback, args, true);
    }

    return Value::Undefined;
}
	void HTTPClientBinding::SetRequestHeader(const ValueList& args, KValueRef result)
	{
		args.VerifyException("setRequestHeader", "ss");
		std::string key = args.GetString(0);
		std::string value = args.GetString(1);
		this->headers[key] = value;
	}
예제 #4
0
    void Clipboard::_SetData(const ValueList& args, KValueRef result)
    {
        args.VerifyException("setData", "s s|o|l|0");

        std::string mimeType(args.GetString(0));
        DataType type = MimeTypeToDataType(mimeType);

        if (args.at(1)->IsNull() ||
            (args.at(1)->IsString() && !strcmp(args.at(1)->ToString(), "")))
        {
            this->ClearData(type);
        }
        else if (type == URI_LIST)
        {
            std::vector<std::string> uriList(ValueToURIList(args.at(1)));
            this->SetURIList(uriList);
        }
        else if (type == IMAGE)
        {
            BytesRef imageBytes(ValueToBytes(args.at(1)));
            this->SetImage(mimeType, imageBytes);
        }
        else
        {
            std::string newText(args.GetString(1, ""));
            this->SetText(newText);
        }
    }
예제 #5
0
	void PHPEvaluator::Evaluate(const ValueList& args, KValueRef result)
	{
		static Poco::Mutex evaluatorMutex;
		Poco::Mutex::ScopedLock evaluatorLock(evaluatorMutex);

		args.VerifyException("evaluate", "s s s o");

		TSRMLS_FETCH();
		string mimeType(args.GetString(0));
		string name(args.GetString(1));
		string code(args.GetString(2));
		KObjectRef windowGlobal(args.GetObject(3));
		KValueRef kv(Value::Undefined);

		// Contexts must be the same for runs with the same global object.
		string contextId(GetContextId(windowGlobal));
		PHPUtils::GenerateCaseMap(code TSRMLS_CC);

		std::ostringstream codeString;
		codeString << "namespace " << contextId << " {\n";
		codeString << code << "\n";
		codeString << "  $__fns = get_defined_functions();\n";
		codeString << "  if (array_key_exists(\"user\", $__fns)) {\n";
		codeString << "   foreach($__fns[\"user\"] as $fname) {\n";
		codeString << "    if (!$window->$fname) {";
		codeString << "      krollAddFunction($window, $fname);\n";
		codeString << "    }\n";
		codeString << "   }\n";
		codeString << "  }\n";
		codeString << "}\n";
		printf("%s\n", codeString.str().c_str());

		// This seems to be needed to make PHP actually give us errors
		// at parse/compile time -- see: main/main.c line 969
		PG(during_request_startup) = 0;

		KObjectRef previousGlobal(PHPUtils::GetCurrentGlobalObject());
		PHPUtils::SwapGlobalObject(windowGlobal, &EG(symbol_table) TSRMLS_CC);

		zend_first_try
		{
			zend_eval_string((char *) codeString.str().c_str(), NULL, 
				(char *) name.c_str() TSRMLS_CC);
		}
		zend_catch
		{
			Logger::Get("PHP")->Error("Evaluation of script failed");
		}
		zend_end_try();

		PHPUtils::SwapGlobalObject(previousGlobal, &EG(symbol_table) TSRMLS_CC);
		result->SetValue(kv);
	}
예제 #6
0
    void Clipboard::_GetData(const ValueList& args, KValueRef result)
    {
        args.VerifyException("getData", "s");

        std::string mimeType(args.GetString(0));
        DataType type = MimeTypeToDataType(mimeType);
        if (type == URI_LIST)
        {
            std::vector<std::string>& list = this->GetURIList();
            if (mimeType == "url")
            {
                std::string url;
                if (list.size() > 0)
                    url = list.at(0);

                result->SetString(url.c_str());
            }
            else
            {
                result->SetList(StaticBoundList::FromStringVector(list));
            }
        }
        else if (type == IMAGE)
        {
            result->SetObject(this->GetImage(mimeType));
        }
        else 
        {
            result->SetString(this->GetText());
        }
    }
예제 #7
0
void Network::_CreateTCPSocket(const ValueList& args, KValueRef result)
{
    args.VerifyException("createTCPSocket", "sn");
    std::string host(args.GetString(0));
    int port = args.GetInt(1);
    result->SetObject(new TCPSocket(host, port));
}
예제 #8
0
	void Bytes::_Split(const ValueList& args, KValueRef result)
	{
		// This method now follows the spec located at:
		// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/split
		// Except support for regular expressions
		args.VerifyException("Bytes.split", "?s,i");

		KListRef list = new StaticBoundList();
		result->SetList(list);

		std::string target = "";
		if (this->size > 0)
		{
			target = this->buffer;
		}
		else
		{
			list->Append(Value::NewString(target));
			return;
		}

		if (args.size() <= 0)
		{
			list->Append(Value::NewString(target));
			return;
		}

		std::string separator = args.GetString(0);

		int limit = args.GetInt(1, INT_MAX);

		// We could use Poco's tokenizer here, but it doesn't split strings
		// like "abc,def,," -> ['abc', 'def', '', ''] correctly. It produces
		// ['abc', 'def', ''] which is a different behavior than the JS split.
		// So we roll our own for now -- it's not very efficient right now, but
		// it should be correct.
		size_t next = target.find(separator);
		while (target.size() > 0 && next != std::string::npos)
		{
			std::string token;
			if (separator.size() == 0)
			{
				token = target.substr(0, 1);
			}
			else
			{
				token = target.substr(0, next);
			}
			target = target.substr(next + 1);
			next = target.find(separator);

			if ((int) list->Size() >= limit)
				return;

			list->Append(Value::NewString(token));
		}

		if ((int) list->Size() < limit && separator.size() != 0)
			list->Append(Value::NewString(target));
	}
예제 #9
0
void KEventObject::_RemoveEventListener(const ValueList& args, KValueRef result)
{
    args.VerifyException("removeEventListener", "s m");

    std::string event(args.GetString(0));
    this->RemoveEventListener(event, args.GetMethod(1));
}
예제 #10
0
	void Bytes::_Concat(const ValueList& args, KValueRef result)
	{
		std::vector<BytesRef> bytes;
		bytes.push_back(BytesRef(this, true));

		for (size_t i = 0; i < args.size(); i++)
		{
			if (args.at(i)->IsObject())
			{
				BytesRef bytesObject(args.GetObject(i).cast<Bytes>());
				if (!bytesObject.isNull());
				{
					bytes.push_back(bytesObject);
				}
			}
			else if (args.at(i)->IsString())
			{
				std::string str(args.GetString(i));
				bytes.push_back(new Bytes(str));
			}
		}

		BytesRef newBytes = Bytes::Concat(bytes);
		result->SetObject(newBytes);
	}
예제 #11
0
void AnalyticsBinding::_SendEvent(const ValueList &args, KValueRef result)
{
	std::string eventString(args.GetString(0));
	{
		Poco::Mutex::ScopedLock lock(eventsLock);
		events.push(eventString);
	}
}
예제 #12
0
	AutoMenuItem UIBinding::__CreateMenuItem(const ValueList& args)
	{
		args.VerifyException("createMenuItem", "?s m|0 s|0");
		std::string label = args.GetString(0, "");
		KMethodRef eventListener = args.GetMethod(1, NULL);
		std::string iconURL = args.GetString(2, "");

		AutoMenuItem item = this->CreateMenuItem();
		if (!label.empty())
			item->SetLabel(label);
		if (!iconURL.empty())
			item->SetIcon(iconURL);
		if (!eventListener.isNull())
			item->AddEventListener(Event::CLICKED, eventListener);

		return item;
	}
예제 #13
0
void Properties::RemoveProperty(const ValueList& args, KValueRef result)
{
    args.VerifyException("removeProperty", "s");
    result->SetBool(config->removeProperty(args.GetString(0)));
    if (result) {
        this->SaveConfig();
    }
}
예제 #14
0
void MenuItem::_SetIcon(const ValueList& args, KValueRef result)
{
    args.VerifyException("setIcon", "s|0");
    std::string newIcon = "";
    if (args.size() > 0) {
        newIcon = args.GetString(0);
    }
    this->SetIcon(newIcon);
}
예제 #15
0
	void UIBinding::_SetIcon(const ValueList& args, KValueRef result)
	{
		args.VerifyException("setIcon", "s|0");

		std::string iconURL;
		if (args.size() > 0)
			iconURL = args.GetString(0);
		this->_SetIcon(iconURL);
	}
예제 #16
0
	void UIBinding::_SetBadge(const ValueList& args, KValueRef result)
	{
		std::string badgeText;
		if (args.size() > 0) {
			badgeText = args.GetString(0);
		}

		this->SetBadge(badgeText);
	}
예제 #17
0
	void WorkerContext::_ImportScripts(const ValueList &args, KValueRef result)
	{
		for (size_t c = 0; c < args.size(); c++)
		{
			std::string path(URLUtils::URLToPath(args.GetString(c)));
			GetLogger()->Debug("Attempting to import worker script = %s", path.c_str());
			KJSUtil::EvaluateFile(this->jsContext, path.c_str());
		}
	}
예제 #18
0
	void UIBinding::_SetDockIcon(const ValueList& args, KValueRef result)
	{
		std::string iconPath;
		if (args.size() > 0) {
			std::string in = args.GetString(0);
			iconPath = URLUtils::URLToPath(in);
		}
		this->SetDockIcon(iconPath);
	}
예제 #19
0
	void AppBinding::StdIn(const ValueList& args, KValueRef result)
	{
		args.VerifyException("stdin", "?ss");
		std::string input;
		char delimiter = '\n';

		if (args.size() >= 1)
		{
			std::cout << args.GetString(0);
		}

		if (args.size() >= 2)
		{
			delimiter = args.GetString(1).at(0);
		}

		getline(std::cin, input, delimiter);
		result->SetString(input);
	}
예제 #20
0
	void UIBinding::_AddTray(const ValueList& args, KValueRef result)
	{
		args.VerifyException("createTrayIcon", "s,?m");
		std::string iconURL = args.GetString(0);

		KMethodRef cbSingleClick = args.GetMethod(1, NULL);
		AutoTrayItem item = this->AddTray(iconURL, cbSingleClick);
		this->trayItems.push_back(item);
		result->SetObject(item);
	}
예제 #21
0
	void UIBinding::_SetBadgeImage(const ValueList& args, SharedValue result)
	{
		std::string iconPath;
		if (args.size() > 0) {
			std::string in = args.GetString(0);
			iconPath = URLUtils::URLToPath(in);
		}

		this->SetBadgeImage(iconPath);
	}
예제 #22
0
	void HTTPClientBinding::Open(const ValueList& args, KValueRef result)
	{
		args.VerifyException("open", "ss?bss");

		this->method = args.GetString(0);
		this->url = args.GetString(1);
		this->SetString("url", this->url);

		// Validate the scheme
		const std::string scheme = Poco::URI(url).getScheme();
		if (scheme != "http" && scheme != "https")
		{
			logger->Error("%s scheme is not supported", scheme.c_str());
			result->SetBool(false);
			return;
		}

		if (this->method.empty())
		{
			this->method = Poco::Net::HTTPRequest::HTTP_GET;
		}

		if (args.size() >= 3)
		{
			this->async = args.GetBool(2);
		}

		if (args.size() >= 4)
		{
			this->basicCredentials.setUsername(args.GetString(3));
			this->basicCredentials.setPassword(args.GetString(4));
		}

		// Get on*** handler functions
		this->ondatastream = this->GetMethod("ondatastream");
		this->onreadystate = this->GetMethod("onreadystatechange");
		this->onsendstream = this->GetMethod("onsendstream");
		this->onload = this->GetMethod("onload");

		this->ChangeState(1); // opened
		result->SetBool(true);
	}
예제 #23
0
	void TrayItem::_SetHint(const ValueList& args, KValueRef result)
	{
		args.VerifyException("setHint", "s|0");
		this->hint = "";
		if (args.size() > 0) {
			hint = args.GetString(0);
		}

		if (!removed)
			this->SetHint(hint);
	}
예제 #24
0
void Codec::DigestToHex(const ValueList& args, KValueRef result)
{
    args.VerifyException("digestToHex", "i s|o");
    
    int type = args.at(0)->ToInt();
    
    Poco::DigestEngine *engine = NULL;
    
    switch(type)
    {
        case CODEC_MD2:
        {
            engine = new Poco::MD2Engine(); 
            break;
        }
        case CODEC_MD4:
        {
            engine = new Poco::MD4Engine(); 
            break;
        }
        case CODEC_MD5:
        {
            engine = new Poco::MD5Engine(); 
            break;
        }
        case CODEC_SHA1:
        {
            engine = new Poco::SHA1Engine(); 
            break;
        }
        default:
        {
            std::ostringstream msg("Unsupported encoding type: ");
            msg << type;
            throw ValueException::FromString(msg.str());
        }
    }
    
    if (args.at(1)->IsString())
    {
        engine->update(args.GetString(1));
    }
    else
    {
        AutoPtr<Bytes> bytes(args.GetObject(1).cast<Bytes>());
        if (!bytes.isNull())
        {
            engine->update(bytes->Pointer(), bytes->Length());
        }
    }
    std::string data = Poco::DigestEngine::digestToHex(engine->digest()); 
    result->SetString(data);
    delete engine;
}
예제 #25
0
	void AppBinding::AppURLToPath(const ValueList& args, SharedValue result)
	{
		args.VerifyException("appURLToPath", "s");
		std::string url = args.GetString(0);
		if (url.find("app://") != 0 && url.find("://") == std::string::npos)
		{
			url = std::string("app://") + url;
		}
		std::string path = URLUtils::URLToPath(url);
		result->SetString(path);
	}
예제 #26
0
	void PHPEvaluator::CanEvaluate(const ValueList& args, KValueRef result)
	{
		args.VerifyException("canEvaluate", "s");
		
		result->SetBool(false);
		string mimeType(args.GetString(0));
		if (mimeType == "text/php")
		{
			result->SetBool(true);
		}
	}
예제 #27
0
/*static*/
KValueRef Codec::CreateZipAsync(const ValueList& args)
{
    std::string directory = args.GetString(0);
    std::string zipFile = args.GetString(1);
    AutoPtr<AsyncJob> job = args.GetObject(2).cast<AsyncJob>();
    KMethodRef callback = 0;
    if (args.size() > 3)
    {
        callback = args.GetMethod(3);
    }
    
    Poco::Path path(directory);
    path.makeDirectory();
    
    std::ofstream stream(UTF8ToSystem(zipFile).c_str(),
        std::ios::binary | std::ios::trunc);
    Poco::Zip::Compress compressor(stream, true);
    try
    {
        compressor.addRecursive(path);
    }
    catch (std::exception& e)
    {
        Logger::Get("Codec")->Error("exception compressing: %s", e.what());
        throw ValueException::FromFormat("Exception during zip: %s", e.what());
    }
    
    compressor.close();
    stream.close();
    
    if (!callback.isNull())
    {
        ValueList args;
        args.push_back(Value::NewString(zipFile));
        RunOnMainThread(callback, args, true);
    }
    
    return Value::Undefined;
}
예제 #28
0
    void Clipboard::_ClearData(const ValueList& args, KValueRef result)
    {
        args.VerifyException("setData", "?s");

        DataType type = UNKNOWN;
        if (args.size() > 0)
        {
            std::string mimeType(args.GetString(0));
            type = MimeTypeToDataType(mimeType);
        }

        this->ClearData(type);
    }
예제 #29
0
	void PHPEvaluator::CanPreprocess(const ValueList& args, KValueRef result)
	{
		args.VerifyException("canPreprocess", "s");

		string url(args.GetString(0));
		Poco::URI uri(url);
		
		result->SetBool(false);
		if (Script::HasExtension(uri.getPath().c_str(), "php"))
		{
			result->SetBool(true);
		}
	}
예제 #30
0
    void Clipboard::_HasData(const ValueList& args, KValueRef result)
    {
        args.VerifyException("hasData", "?s");

        DataType type = UNKNOWN;
        if (args.size() > 0)
        {
            std::string mimeType(args.GetString(0));
            type = MimeTypeToDataType(mimeType);
        }

        result->SetBool(this->HasData(type));
    }