void C_AwesomiumBrowserManager::DispatchJavaScriptMethods(C_AwesomiumBrowserInstance* pBrowserInstance)
{
	//WebView* pWebView = m_webViews[pWebTab];

	std::string previousObjectName = "-1";

	JSValue response;
	JSObject responseObject;
	std::vector<JavaScriptMethodCall_t*>& methodCalls = pBrowserInstance->GetJavaScriptMethodCalls();
	for (auto pJavaScriptMethodCall : methodCalls)
	{
		if (previousObjectName != pJavaScriptMethodCall->objectName)
		{
			previousObjectName = pJavaScriptMethodCall->objectName;
			response = pBrowserInstance->GetWebView()->ExecuteJavascriptWithResult(WSLit(pJavaScriptMethodCall->objectName.c_str()), WSLit(""));
			if (!response.IsObject())
				continue;

			responseObject = response.ToObject();
		}

		JSArray arguments;
		for (auto argument : pJavaScriptMethodCall->methodArguments)
			arguments.Push(WSLit(argument.c_str()));

		responseObject.InvokeAsync(WSLit(pJavaScriptMethodCall->methodName.c_str()), arguments);
	}

	//m_pWebBrowser->DispatchJavaScriptMethods(pWebTab);
}
void C_AwesomiumBrowserManager::DispatchJavaScriptMethod(C_AwesomiumBrowserInstance* pBrowserInstance, std::string objectName, std::string objectMethod, std::vector<std::string> methodArguments)
{
	//WebView* pWebView = m_webViews[pWebTab];

	JSValue response = pBrowserInstance->GetWebView()->ExecuteJavascriptWithResult(WSLit(objectName.c_str()), WSLit(""));
	if (response.IsObject())
	{
		JSObject object = response.ToObject();
		JSArray arguments;

		for (auto argument : methodArguments)
			arguments.Push(WSLit(argument.c_str()));

		object.InvokeAsync(WSLit(objectMethod.c_str()), arguments);
	}

	//m_pWebBrowser->DispatchJavaScriptMethod(pWebTab, objectName, objectMethod, methodArguments);
	/*
	for (auto arg : args)
	{
	DevMsg("Argument: %s\n", arg->text.c_str());
	}
	*/
}
void MenuListener::OnShowPopupMenu(WebView *caller, const WebPopupMenuInfo &menu_info)
{
//	DevMsg("DISABLED FOR TESTING!\n");
//	return;

	//C_WebTab* pWebTab = g_pAnarchyManager->GetWebManager()->GetWebBrowser()->FindWebTab(caller);
	//C_WebTab* pHudWebTab = g_pAnarchyManager->GetWebManager()->GetHudWebTab();
	//WebView* pHudWebView = g_pAnarchyManager->GetWebManager()->GetWebBrowser()->FindWebView(pHudWebTab);

	C_AwesomiumBrowserInstance* pAwesomiumBrowserInstance = g_pAnarchyManager->GetAwesomiumBrowserManager()->FindAwesomiumBrowserInstance(caller);	// FIXME: This should be a general EmbeddedInstance of any type.
	C_AwesomiumBrowserInstance* pHudBrowserInstance = g_pAnarchyManager->GetAwesomiumBrowserManager()->FindAwesomiumBrowserInstance("hud");
	WebView* pHudWebView = pHudBrowserInstance->GetWebView();

	DevMsg("Pop menu detected!\n");

	std::vector<std::string> methodArguments;
	methodArguments.push_back(pAwesomiumBrowserInstance->GetId());
	methodArguments.push_back(VarArgs("%i", menu_info.bounds.x));
	methodArguments.push_back(VarArgs("%i", menu_info.bounds.y));
	methodArguments.push_back(VarArgs("%i", menu_info.bounds.width));
	methodArguments.push_back(VarArgs("%i", menu_info.bounds.height));
	methodArguments.push_back(VarArgs("%i", menu_info.item_height));
	methodArguments.push_back(VarArgs("%f", menu_info.item_font_size));
	methodArguments.push_back(VarArgs("%i", menu_info.selected_item));
	methodArguments.push_back(VarArgs("%i", menu_info.right_aligned));

	for (int i = 0; i < menu_info.items.size(); i++)
	{
		if (menu_info.items[i].type == kWebMenuItemType_Option)
			methodArguments.push_back("Option");
		else if (menu_info.items[i].type == kWebMenuItemType_CheckableOption)
			methodArguments.push_back("CheckableOption");
		else if (menu_info.items[i].type == kWebMenuItemType_Group)
			methodArguments.push_back("Group");
		else if (menu_info.items[i].type == kWebMenuItemType_Separator)
			methodArguments.push_back("Separator");

		methodArguments.push_back(WebStringToCharString(menu_info.items[i].label));
		methodArguments.push_back(WebStringToCharString(menu_info.items[i].tooltip));
		methodArguments.push_back(VarArgs("%i", menu_info.items[i].action));
		methodArguments.push_back(VarArgs("%i", menu_info.items[i].right_to_left));
		methodArguments.push_back(VarArgs("%i", menu_info.items[i].has_directional_override));
		methodArguments.push_back(VarArgs("%i", menu_info.items[i].enabled));
		methodArguments.push_back(VarArgs("%i", menu_info.items[i].checked));
	}
	
	std::string objectName = "window.arcadeHud";
	std::string objectMethod = "showPopupMenu";

	JSValue response = pHudWebView->ExecuteJavascriptWithResult(WSLit(objectName.c_str()), WSLit(""));
	if (response.IsObject())
	{
		JSObject object = response.ToObject();
		JSArray arguments;

		for (auto argument : methodArguments)
			arguments.Push(WSLit(argument.c_str()));

		object.InvokeAsync(WSLit(objectMethod.c_str()), arguments);
	}
}
Exemplo n.º 4
0
JSValue IrcExtension::Handle(
    const std::string &functionName,
    const JSArray &args)
{
    // connect(channelName)
    if (functionName == "connect") {
        // were in the middle of something
        if (irc_is_connected(session)) {
            return JSValue::Undefined();
        }

        if (hThread) {
            // thread finished?
            if (WaitForSingleObject(hThread, 0) == WAIT_OBJECT_0) {
                CloseHandle(hThread);	
                hThread = NULL;
            } else {
                // thread is still running
                return JSValue::Undefined();
            }
        }


        if (args.size() < 1 || !args[0].IsString()) {
            // TODO: log this and exit
            return JSValue::Undefined();
        }

        channelName = ToString(args[0].ToString());
        serverName = channelName + ".jtvirc.com";
        port = 6667;

        if (args.size() >= 2) {
            if (!(args[1].IsString())) {
                // log error
                return JSValue::Undefined();
            }
            serverName = ToString(args[1].ToString());
        }

        if (args.size() == 3) {
            if (!args[2].IsInteger()) {
                // log error
                return JSValue::Undefined();
            }
            port = (unsigned int)args[2].ToInteger();
        }

        isJoinedChannel = false;
       
        hThread = (HANDLE)_beginthreadex(NULL, 0, &IrcThread, this, 0, NULL);

        return JSValue::Undefined();    


    } else if (functionName == "disconnect") { // disconnect()

        if (args.size() != 0) {
            // TODO: log invalid method call
            return JSValue::Undefined();
        }

        irc_disconnect(session);

        if (hThread) {
            WaitForSingleObject( hThread, INFINITE );
            CloseHandle(hThread);
            hThread = NULL;
        }

        isJoinedChannel = false;
        moderators.clear();
        latestMessage.Clear();
        
        EnterCriticalSection(&messageLock);
        messages.clear();
        LeaveCriticalSection(&messageLock);

        return JSValue::Undefined();

    } else if (functionName == "getMessages") {  // [message, message, ..] getMessages()
        EnterCriticalSection(&messageLock);

        if (args.size() != 0) {
            // TODO: log invalid method call
            return JSValue::Undefined();
        }

        JSArray returnArgs;

        while(messages.size()) {
            IrcMessage &message = messages[0];
            JSObject newMessageObject;
            
            newMessageObject.SetProperty(WSLit("nickname"), ToWebString(message.username));
            newMessageObject.SetProperty(WSLit("message"), ToWebString(message.message));
            newMessageObject.SetProperty(WSLit("color"), ToWebString(message.color));
            
            JSArray groups;
            for(auto i = message.groups.begin(); i != message.groups.end(); i++) {
                groups.Push(ToWebString(*i));
            }
            newMessageObject.SetProperty(WSLit("groups"), groups);
            
            JSArray emoticons;
            for(auto i = message.emoticons.begin(); i != message.emoticons.end(); i++) {
                groups.Push(ToWebString(*i));
            }
            newMessageObject.SetProperty(WSLit("emoticons"), emoticons);

            returnArgs.Push(newMessageObject);
            messages.erase(messages.begin());
        }

        LeaveCriticalSection(&messageLock);

        return returnArgs;

    } else if (functionName == "isConnected") {
        return JSValue(irc_is_connected(session) && isJoinedChannel);
    }

    return JSValue::Undefined();
}