Exemplo n.º 1
0
	AppBinding::AppBinding(Host *host,SharedBoundObject global) : host(host),global(global)
	{
		this->SetMethod("getID", &AppBinding::GetID);
		this->SetMethod("getName", &AppBinding::GetName);
		this->SetMethod("getVersion", &AppBinding::GetVersion);
		this->SetMethod("getUpdateURL", &AppBinding::GetUpdateURL);
		this->SetMethod("getGUID", &AppBinding::GetGUID);
		this->SetMethod("appURLToPath", &AppBinding::AppURLToPath);

		// FIXME: for now this version is hardcoded
		SharedValue version = Value::NewDouble(PRODUCT_VERSION);
		global->Set("version", version);

		// platform
		SharedValue platform = Value::NewString(host->GetPlatform());
		global->Set("platform",platform);

		SharedBoundList argList = new StaticBoundList();
		for (int i = 0; i < Host::GetInstance()->GetCommandLineArgCount(); i++) {
			argList->Append(Value::NewString(Host::GetInstance()->GetCommandLineArg(i)));
		}
		SharedValue arguments = Value::NewList(argList);
		Set("arguments", arguments);

		this->SetMethod("exit",&AppBinding::Exit);
		this->SetMethod("loadProperties", &AppBinding::LoadProperties);
	}
Exemplo n.º 2
0
void NetworkBinding::RemoveBinding(void* binding)
{
    std::vector<SharedBoundObject>::iterator i = bindings.begin();
    while(i!=bindings.end())
    {
        SharedBoundObject b = (*i);
        if (binding == b.get())
        {
            bindings.erase(i);
            break;
        }
        i++;
    }
}
Exemplo n.º 3
0
void NetworkBinding::GetHostByAddress(const ValueList& args, SharedValue result)
{
    if (args.at(0)->IsObject())
    {
        SharedBoundObject obj = args.at(0)->ToObject();
        SharedPtr<IPAddressBinding> b = obj.cast<IPAddressBinding>();
        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
            IPAddress addr(b->GetAddress()->toString());
            SharedPtr<HostBinding> binding = new HostBinding(addr);
            if (binding->IsInvalid())
            {
                throw ValueException::FromString("Could not resolve address");
            }
            result->SetObject(binding);
            return;
        }
        else
        {
            SharedValue bo = obj->Get("toString");
            if (bo->IsMethod())
            {
                SharedBoundMethod m = bo->ToMethod();
                ValueList args;
                SharedValue tostr = m->Call(args);
                this->_GetByHost(tostr->ToString(),result);
                return;
            }
            throw ValueException::FromString("Unknown object passed");
        }
    }
    else if (args.at(0)->IsString())
    {
        // in this case, they just passed in a string so resolve as
        // normal
        this->_GetByHost(args.at(0)->ToString(),result);
    }
}
Exemplo n.º 4
0
Win32UserWindow::Win32UserWindow(SharedUIBinding binding, WindowConfig* config, SharedUserWindow& parent) :
	UserWindow(binding, config, parent),
	script_evaluator(binding->GetHost()),
	menuBarHandle(NULL),
	menuInUse(NULL),
	menu(NULL),
	contextMenuHandle(NULL),
	initial_icon(NULL),
	web_inspector(NULL)
{
	static bool initialized = false;
	win32_host = static_cast<kroll::Win32Host*>(binding->GetHost());
	if (!initialized) {
		INITCOMMONCONTROLSEX InitCtrlEx;

		InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
		InitCtrlEx.dwICC = 0x00004000; //ICC_STANDARD_CLASSES;
		InitCommonControlsEx(&InitCtrlEx);

		curl_register_local_handler(&Titanium_app_url_handler);
		curl_register_local_handler(&Titanium_ti_url_handler);
		addScriptEvaluator(&script_evaluator);
	}

	Win32UserWindow::RegisterWindowClass(win32_host->GetInstanceHandle());
	window_handle
			= CreateWindowEx(WS_EX_LAYERED, windowClassName,
					config->GetTitle().c_str(), WS_CLIPCHILDREN, CW_USEDEFAULT,
					0, CW_USEDEFAULT, 0, NULL, NULL,
					win32_host->GetInstanceHandle(), NULL);

	if (window_handle == NULL) {
		std::cout << "Error Creating Window: " << GetLastError() << std::endl;
	}
	std::cout << "window_handle = " << (int) window_handle << std::endl;

	// make our HWND available to 3rd party devs without needing our headers
	SharedValue windowHandle = Value::NewVoidPtr((void*) window_handle);
	this->Set("windowHandle", windowHandle);
	this->SetMethod("addMessageHandler", &Win32UserWindow::AddMessageHandler);

	SetWindowUserData(window_handle, this);

	this->ReloadTiWindowConfig();
	this->SetupDecorations(false);

	Bounds b;
	b.x = config->GetX();
	b.y = config->GetY();
	b.width = config->GetWidth();
	b.height = config->GetHeight();
	SetBounds(b);

	//web_view = WebView::createInstance();
	HRESULT hr = CoCreateInstance(CLSID_WebView, 0, CLSCTX_ALL, IID_IWebView,
			(void**) &web_view);
	if (FAILED(hr)) {
		std::cerr << "Error Creating WebView: ";
		if (hr == REGDB_E_CLASSNOTREG)
			std::cerr << "REGDB_E_CLASSNOTREG" << std::endl;
		else if (hr == CLASS_E_NOAGGREGATION)
			std::cerr << "CLASS_E_NOAGGREGATION" << std::endl;
		else if (hr == E_NOINTERFACE)
			std::cerr << "E_NOINTERFACE" << std::endl;
		else if (hr == E_UNEXPECTED)
			std::cerr << "E_UNEXPECTED" << std::endl;
		else if (hr == E_OUTOFMEMORY)
			std::cerr << "E_OUTOFMEMORY" << std::endl;
		else if (hr == E_INVALIDARG)
			std::cerr << "E_INVALIDARG" << std::endl;
		else
			fprintf(stderr, "Unknown Error? %x\n", hr);
	}

	// set the custom user agent for Titanium
	double version = host->GetGlobalObject()->Get("version")->ToDouble();
	char userAgent[128];
	sprintf(userAgent, "%s/%0.2f", PRODUCT_NAME, version);
	_bstr_t ua(userAgent);
	web_view->setApplicationNameForUserAgent(ua.copy());

	// place our user agent string in the global so we can later use it
	SharedBoundObject global = host->GetGlobalObject();
	_bstr_t uaurl("http://titaniumapp.com");
	BSTR uaresp;
	web_view->userAgentForURL(uaurl.copy(), &uaresp);
	std::string ua_str = _bstr_t(uaresp);
	global->Set("userAgent", Value::NewString(ua_str.c_str()));

	std::cout << "create frame load delegate " << std::endl;
	frameLoadDelegate = new Win32WebKitFrameLoadDelegate(this);
	uiDelegate = new Win32WebKitUIDelegate(this);
	policyDelegate = new Win32WebKitPolicyDelegate(this);

	std::cout << "set delegates, set host window, webview=" << (int) web_view
			<< std::endl;
	hr = web_view->setFrameLoadDelegate(frameLoadDelegate);
	hr = web_view->setUIDelegate(uiDelegate);
	hr = web_view->setPolicyDelegate(policyDelegate);
	hr = web_view->setHostWindow((OLE_HANDLE) window_handle);

	std::cout << "init with frame" << std::endl;
	RECT client_rect;
	GetClientRect(window_handle, &client_rect);
	hr = web_view->initWithFrame(client_rect, 0, 0);

	AppConfig *appConfig = AppConfig::Instance();
	std::string appid = appConfig->GetAppID();

	IWebPreferences *prefs = NULL;
	hr = CoCreateInstance(CLSID_WebPreferences, 0, CLSCTX_ALL,
			IID_IWebPreferences, (void**) &prefs);
	if (FAILED(hr) || prefs == NULL) {
		std::cerr << "Couldn't create the preferences object" << std::endl;
	} else {
		_bstr_t pi(appid.c_str());
		prefs->initWithIdentifier(pi.copy(), &prefs);

		prefs->setCacheModel(WebCacheModelDocumentBrowser);
		prefs->setPlugInsEnabled(true);
		prefs->setJavaEnabled(true);
		prefs->setJavaScriptEnabled(true);
		prefs->setDOMPasteAllowed(true);

		IWebPreferencesPrivate* privatePrefs = NULL;
		hr = prefs->QueryInterface(IID_IWebPreferencesPrivate,
				(void**) &privatePrefs);
		if (FAILED(hr)) {
			std::cerr << "Failed to get private preferences" << std::endl;
		} else {
			privatePrefs->setDeveloperExtrasEnabled(host->IsDebugMode());
			//privatePrefs->setDeveloperExtrasEnabled(host->IsDebugMode());
			privatePrefs->setDatabasesEnabled(true);
			privatePrefs->setLocalStorageEnabled(true);
			privatePrefs->setOfflineWebApplicationCacheEnabled(true);

			_bstr_t db_path(
					FileUtils::GetApplicationDataDirectory(appid).c_str());
			privatePrefs->setLocalStorageDatabasePath(db_path.copy());
			privatePrefs->Release();
		}

		web_view->setPreferences(prefs);
		prefs->Release();
	}

	// allow app:// and ti:// to run with local permissions (cross-domain ajax,etc)
	_bstr_t app_proto("app");
	web_view->registerURLSchemeAsLocal(app_proto.copy());

	_bstr_t ti_proto("ti");
	web_view->registerURLSchemeAsLocal(ti_proto.copy());

	IWebViewPrivate *web_view_private;
	hr = web_view->QueryInterface(IID_IWebViewPrivate,
			(void**) &web_view_private);
	hr = web_view_private->viewWindow((OLE_HANDLE*) &view_window_handle);

	hr = web_view_private->inspector(&web_inspector);
	if (FAILED(hr) || web_inspector == NULL) {
		std::cerr << "Couldn't retrieve the web inspector object" << std::endl;
	}

	web_view_private->Release();

	_bstr_t inspector_url("ti://com.titaniumapp/runtime/inspector/inspector.html");
	_bstr_t localized_strings_url("ti://com.titaniumapp/runtime/inspector/localizedStrings.js");
	web_inspector->setInspectorURL(inspector_url.copy());
	web_inspector->setLocalizedStringsURL(localized_strings_url.copy());

	hr = web_view->mainFrame(&web_frame);
	//web_view->setShouldCloseWithWindow(TRUE);

	std::cout << "resize subviews" << std::endl;
	ResizeSubViews();

	// ensure we have valid restore values
	restore_bounds = GetBounds();
	restore_styles = GetWindowLong(window_handle, GWL_STYLE);

	if (this->config->IsFullScreen()) {
		this->SetFullScreen(true);
	}

	if (this->config->IsTopMost() && this->config->IsVisible()) {
		this->SetTopMost(true);
	}

	// set this flag to indicate that when the frame is loaded
	// we want to show the window - we do this to prevent white screen
	// while the URL is being fetched
	this->requires_display = true;

	// set initial window icon to icon associated with exe file
	char exePath[MAX_PATH];
	GetModuleFileNameA(GetModuleHandle(NULL), exePath, MAX_PATH);
	initial_icon = ExtractIcon(win32_host->GetInstanceHandle(), exePath, 0);
	if (initial_icon) {
		SendMessageA(window_handle, (UINT) WM_SETICON, ICON_BIG,
				(LPARAM) initial_icon);
	}
}