public void TestSingleFileProtocolHandler()
        {
            var filename = new Holder<string>();

            using (var protocolHandler = new FileProtocolHandler(Context, "asset", FilenameProtocolHandler, (fn) => "text/html"))
            using (var window = new Window(Context)) {
                window.ChromeSend += (w, msg, args) => {
                    if (msg == "filename")
                        filename.Value = args[0];
                };
                window.NavigateTo("asset://test/foo.html");

                WaitFor(filename, "test/foo.html", 5);
            }
        }
        public void TestTwoProtocolHandlers()
        {
            var filename = new Holder<string>();

            using (var protocolHandler1 = new FileProtocolHandler(Context, "pone", FilenameProtocolHandler, (fn) => "text/html"))
            using (var protocolHandler2 = new FileProtocolHandler(Context, "ptwo", FilenameProtocolHandler, (fn) => "text/html"))
            using (var window = new Window(Context)) {
                window.ChromeSend += (w, msg, args) => {
                    if (msg == "filename")
                        filename.Value = args[0];
                };

                window.NavigateTo("pone://test/one.html");

                WaitFor(filename, "test/one.html", 5);

                window.NavigateTo("ptwo://test/two.html");

                WaitFor(filename, "test/two.html", 5);
            }
        }
        protected override void LoadContent()
        {
            // We have to inject our teardown code here because if we let XNA's
            //  normal disposal handlers run, Chrome's message pump gets confused
            //  and hangs our process
            var form = (System.Windows.Forms.Control.FromHandle(Window.Handle) as System.Windows.Forms.Form);
            form.FormClosing += (s, e) => Teardown();

            spriteBatch = new SpriteBatch(GraphicsDevice);

            background = Content.Load<Texture2D>("background");

            var context = Context.Create();

            var assetRoot = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
            assetProtocol = new FileProtocolHandler(
                context, "asset", (filename) => {
                    try {
                        return File.OpenRead(Path.Combine(assetRoot, filename));
                    } catch {
                        return null;
                    }
                });

            browser = new TextureBackedWindow(context, GraphicsDevice);
            browser.Transparent = true;
            browser.LoadingStateChanged += WebKit_LoadingStateChanged;
            browser.Load += WebKit_Load;
            browser.AddressBarChanged += WebKit_AddressBarChanged;
            browser.Resize(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height - 27);

            int rtWidth = browser.Texture.Width;
            int rtHeight = browser.Texture.Height;

            try {
                oldPageRt = new RenderTarget2D(
                    GraphicsDevice, rtWidth, rtHeight, 2,
                    SurfaceFormat.Color, RenderTargetUsage.DiscardContents
                );
            } catch {
                rtWidth = NextPowerOfTwo(rtWidth);
                rtHeight = NextPowerOfTwo(rtHeight);
                oldPageRt = new RenderTarget2D(
                    GraphicsDevice, rtWidth, rtHeight, 2,
                    SurfaceFormat.Color, RenderTargetUsage.DiscardContents
                );
            }

            focusedFrame = navBar = new TextureBackedWindow(context, GraphicsDevice);
            navBar.Focus();
            navBar.Resize(GraphicsDevice.Viewport.Width, 27);

            navBar.ChromeSend.Register("back", browser.GoBack);
            navBar.ChromeSend.Register("forward", browser.GoForward);
            navBar.ChromeSend.Register("go", (args) =>
                browser.NavigateTo(args[0])
            );

            browser.NavigateTo("asset://./xnatest.html");
            navBar.NavigateTo("asset://./navbar.html");
        }
Example #4
0
        private void Form1_Load(object sender, EventArgs e)
        {
            AssetProtocol = new FileProtocolHandler(
                WebKit.GetWindow().Context,
                "asset",
                (filename) => {
                    try {
                        return File.OpenRead(Path.Combine(
                            Path.GetDirectoryName(Application.ExecutablePath), filename
                        ));
                    } catch {
                        return null;
                    }
                }
            );
            ViewSourceProtocol = new ViewSourceProtocolHandler(
                WebKit.GetWindow().Context
            );

            var filePath = "asset://./test.html";
            WebKit.GetWindow().NavigateTo(filePath);
        }