List icon Contents

From 7.12.2 to 7.13

In this migration guide we describe what API has been removed in 7.13 and what alternatives you should use instead.

Network

Intercepting requests

v7.12.2

To intercept URL requests with the standard (e.g. http, https) and custom schemes (e.g. my-app, jxb) the InterceptRequestCallback callback is used. It allows intercepting requests and override response data as if it was sent from a web server.

The following example demonstrates how to intercept requests with the jxb custom scheme:

Java
Kotlin
network.set(InterceptRequestCallback.class, params -> {
    UrlRequest urlRequest = params.urlRequest();
    if (urlRequest.url().startsWith("jxb://")) {
        UrlRequestJob job = network.newUrlRequestJob(
                UrlRequestJob.Options
                        .newBuilder(urlRequest.id(), HttpStatus.OK)
                        .addHttpHeader(HttpHeader.of("Content-Type", "text/plain"))
                        .build());
        job.write("Hello!".getBytes());
        job.complete();
        return InterceptRequestCallback.Response.intercept(job);
    }
    return InterceptRequestCallback.Response.proceed();
});

...

browser.navigation().loadUrl("jxb://app/hello");
network.set(InterceptRequestCallback::class.java, InterceptRequestCallback { params ->
    val urlRequest: UrlRequest = params.urlRequest()
    if (urlRequest.url().startsWith("jxb://")) {
        val job = network.newUrlRequestJob(
            UrlRequestJob.Options
                .newBuilder(urlRequest.id(), HttpStatus.OK)
                .addHttpHeader(HttpHeader.of("Content-Type", "text/plain"))
                .build()
        )
        job.write("Hello!".toByteArray())
        job.complete()
        return InterceptRequestCallback.Response.intercept(job)
    }
    InterceptRequestCallback.Response.proceed()
})

...

browser.navigation().loadUrl("jxb://app/hello")

v7.13

The previous approach with intercepting all requests has several security limitations and issues:

  • the intercepted custom schemes are treated as non-standard, so you cannot intercept jxb scheme and return HTML with JavaScript that accesses LocalStorage. It will lead to the access denied error;
  • not all schemes can be intercepted. For example, it is forbidden to intercept schemes such as chrome, data, or chrome-extensions. The API allows doing this which might lead to unexpected behavior or crash inside Chromium;
  • some schemes are treated as local schemes, e.g. file. They cannot be intercepted because it is not a network request.

In this version, we improved the API that allows intercepting URL requests and registering custom schemes. The new version does not have the security limitations and issues described above.

The following example demonstrates how to register the custom jxb scheme and intercept all corresponding URL requests:

Java
Kotlin
EngineOptions options = EngineOptions
        .newBuilder(renderingMode)
        .addScheme(Scheme.of("jxb"), params -> {
            UrlRequestJob job = params.newUrlRequestJob(
                    UrlRequestJob.Options
                            .newBuilder(HttpStatus.OK)
                            .addHttpHeader(HttpHeader.of("Content-Type", "text/plain"))
                            .build());
            job.write("Hello!".getBytes());
            job.complete();
            return Response.intercept(job);
        })
        .build();
Engine engine = Engine.newInstance(options);

...

browser.navigation().loadUrl("jxb://app/hello");
val options = EngineOptions
        .newBuilder(renderingMode)
        .addScheme(Scheme.of("jxb")) { params ->
            val job = params.newUrlRequestJob(
                UrlRequestJob.Options
                    .newBuilder(HttpStatus.OK)
                    .addHttpHeader(HttpHeader.of("Content-Type", "text/plain"))
                    .build()
            )
            job.write("Hello!".toByteArray())
            job.complete()
            Response.intercept(job)
        }
        .build()
val engine = Engine.newInstance(options)

...

browser.navigation().loadUrl("jxb://app/hello")

Removed API

There is no need to use the com.teamdev.jxbrowser.net.UrlRequest.id() method due to the alternative way to register custom schemes and intercept URL requests, so it was removed.