Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@
*/
package com.codename1.impl.javase;

import com.codename1.io.Log;
import com.codename1.ui.BrowserWindow;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.util.EventDispatcher;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;


/**
Expand Down Expand Up @@ -131,4 +138,134 @@ protected void fireCloseEvent(ActionEvent evt) {
protected void fireLoadEvent(ActionEvent evt) {
loadListeners.fireActionEvent(evt);
}

/**
* Whether {@code url} is addressed to an application rather than to a page:
* its scheme is anything but {@code http}, {@code https}, {@code file},
* {@code about}, {@code data}, {@code blob}, {@code jar} or
* {@code javascript}. A desktop web view cannot load such an address --
* an OAuth redirect such as {@code com.example.app:/oauth2redirect?code=...}
* -- so a window that reports only successful loads never reports it, and a
* listener waiting for it (an OIDC sign-in) waits forever. A window reports
* these addresses to its load listeners when navigation reaches them.
*
* @param url an address, or null
* @return true when it has a scheme and the scheme is not a web one
*/
protected static boolean isAppSchemeUrl(String url) {
if (url == null) {
return false;
}
int colon = url.indexOf(':');
if (colon <= 0) {
return false;
}
for (int i = 0; i < colon; i++) {
char c = url.charAt(i);
boolean ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|| (i > 0 && ((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.'));
if (!ok) {
return false;
}
}
String scheme = url.substring(0, colon);
for (String web : WEB_SCHEMES) {
if (web.equalsIgnoreCase(scheme)) {
return false;
}
}
return true;
}

private static final String[] WEB_SCHEMES = {
"http", "https", "file", "about", "data", "blob", "jar", "javascript"
};

private static boolean appSchemeHandlerTried;

/**
* Lets a desktop web view navigate to an address addressed to an app, so a
* window sees it. Without a {@link URLStreamHandler} for a scheme, JavaFX's
* web view drops every navigation to it -- a 302 redirect, a meta refresh,
* a link -- without a load or a location change. This installs, once per
* JVM and only when a browser window is opened, a
* {@link URLStreamHandlerFactory} that answers for a scheme
* {@link #isAppSchemeUrl(String)} accepts and the JDK has no handler for;
* its connections fail, so the page never loads. Every other scheme gets
* the JDK's handler as before. When another factory is already installed
* nothing changes, and such redirects stay invisible.
*/
protected static synchronized void installAppSchemeHandler() {
if (appSchemeHandlerTried) {
return;
}
appSchemeHandlerTried = true;
try {
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
return isUnhandledAppScheme(protocol) ? APP_SCHEME_HANDLER : null;
}
});
} catch (Error alreadySet) {
Log.p("A URL stream handler factory is already installed, so a browser window can't see "
+ "a redirect to an app's own scheme: " + alreadySet);
}
}

/**
* Whether {@code protocol} is an app's scheme that neither the JDK nor
* {@code java.protocol.handler.pkgs} handles.
*/
static boolean isUnhandledAppScheme(String protocol) {
if (protocol == null || !isAppSchemeUrl(protocol + ":")) {
return false;
}
if (hasHandlerClass("sun.net.www.protocol", protocol)) {
return false;
}
String pkgs = System.getProperty("java.protocol.handler.pkgs");
if (pkgs != null) {
for (String pkg : pkgs.split("\\|")) {
if (pkg.trim().length() > 0 && hasHandlerClass(pkg.trim(), protocol)) {
return false;
}
}
}
return true;
}

private static boolean hasHandlerClass(String pkg, String protocol) {
String name = pkg + "." + protocol + ".Handler";
try {
Class.forName(name, false, null);
return true;
} catch (ClassNotFoundException | LinkageError notBoot) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
return false;
}
try {
Class.forName(name, false, cl);
return true;
} catch (ClassNotFoundException | LinkageError notFound) {
return false;
}
}
}

/** Parses an app's address; never loads it. */
private static final URLStreamHandler APP_SCHEME_HANDLER = new URLStreamHandler() {
@Override
protected URLConnection openConnection(final URL u) {
return new URLConnection(u) {
@Override
public void connect() throws IOException {
// FileNotFoundException: the web view reports the failed
// load without logging a stack trace.
throw new FileNotFoundException(u.getProtocol() + ": addresses are handed to the app, not loaded");
}
};
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ public class FXBrowserWindowSE extends AbstractBrowserWindowSE {
private Stage stage;
private WebView webview;
private Worker.State state;
private String lastHandOff;


public FXBrowserWindowSE(String startURL) {
installAppSchemeHandler();
try {
initUI(startURL);
} catch (IllegalStateException ex) {
Expand Down Expand Up @@ -90,10 +92,22 @@ public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t,

if (t1 == Worker.State.SUCCEEDED) {
fireLoadEvent(new ActionEvent(webview.getEngine().locationProperty().get()));
} else if (t1 == Worker.State.FAILED) {
handOff(webview.getEngine().locationProperty().get());
}
}

});
// A web view cannot load an address addressed to an app (an OAuth
// redirect to com.example.app:/oauth2redirect?code=...), so that load
// never succeeds. Report the address when navigation reaches it, as a
// phone hands it to the app, so SystemBrowser's OIDC sign-in completes.
webview.getEngine().locationProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> ov, String oldLocation, String newLocation) {
handOff(newLocation);
}
});

StackPane stackPane = new StackPane();
stackPane.getChildren().add(webview);
Expand All @@ -114,6 +128,18 @@ public void handle(WindowEvent t) {



/**
* Reports {@code url} to the load listeners when it is addressed to an app,
* once per address.
*/
private void handOff(String url) {
if (!isAppSchemeUrl(url) || url.equals(lastHandOff)) {
return;
}
lastHandOff = url;
fireLoadEvent(new ActionEvent(url));
}

@Override
public void show() {
if (!Platform.isFxApplicationThread()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.impl.javase;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* The addresses a simulator browser window hands to its load listeners although
* a web view cannot load them: OAuth redirects to an app's own scheme.
*/
public class AbstractBrowserWindowSETest {

@Test
public void anAppsRedirectIsAnAppAddress() {
assertTrue(AbstractBrowserWindowSE.isAppSchemeUrl("com.example.app:/oauth2redirect?code=abc&state=xyz"));
assertTrue(AbstractBrowserWindowSE.isAppSchemeUrl("ca.weblite.xataworks.mobile:/oauth2redirect?code=1"));
assertTrue(AbstractBrowserWindowSE.isAppSchemeUrl("myapp://callback"));
assertTrue(AbstractBrowserWindowSE.isAppSchemeUrl("my-app+x.y:/cb"));
}

@Test
public void webAddressesAreNot() {
assertFalse(AbstractBrowserWindowSE.isAppSchemeUrl("https://accounts.example.com/connect"));
assertFalse(AbstractBrowserWindowSE.isAppSchemeUrl("HTTP://example.com/"));
assertFalse(AbstractBrowserWindowSE.isAppSchemeUrl("file:/tmp/page.html"));
assertFalse(AbstractBrowserWindowSE.isAppSchemeUrl("about:blank"));
assertFalse(AbstractBrowserWindowSE.isAppSchemeUrl("data:text/html,hi"));
assertFalse(AbstractBrowserWindowSE.isAppSchemeUrl("blob:https://example.com/1"));
assertFalse(AbstractBrowserWindowSE.isAppSchemeUrl("jar:file:/a.jar!/x.html"));
assertFalse(AbstractBrowserWindowSE.isAppSchemeUrl("javascript:void(0)"));
}

@Test
public void onlySchemesTheJdkDoesNotHandleGetTheAppHandler() {
assertTrue(AbstractBrowserWindowSE.isUnhandledAppScheme("ca.weblite.xataworks.mobile"));
assertTrue(AbstractBrowserWindowSE.isUnhandledAppScheme("myapp"));
assertFalse(AbstractBrowserWindowSE.isUnhandledAppScheme("http"));
assertFalse(AbstractBrowserWindowSE.isUnhandledAppScheme("https"));
assertFalse(AbstractBrowserWindowSE.isUnhandledAppScheme("file"));
assertFalse(AbstractBrowserWindowSE.isUnhandledAppScheme("jar"));
assertFalse(AbstractBrowserWindowSE.isUnhandledAppScheme("ftp"));
assertFalse(AbstractBrowserWindowSE.isUnhandledAppScheme("mailto"));
assertFalse(AbstractBrowserWindowSE.isUnhandledAppScheme(null));
}

@Test
public void notAnAddressIsNot() {
assertFalse(AbstractBrowserWindowSE.isAppSchemeUrl(null));
assertFalse(AbstractBrowserWindowSE.isAppSchemeUrl(""));
assertFalse(AbstractBrowserWindowSE.isAppSchemeUrl("/relative/path"));
assertFalse(AbstractBrowserWindowSE.isAppSchemeUrl(":nothing"));
assertFalse(AbstractBrowserWindowSE.isAppSchemeUrl("1app:/cb"));
assertFalse(AbstractBrowserWindowSE.isAppSchemeUrl("a b:/cb"));
}
}
Loading