When I create CefMessageRouter for a 2nd (and subsequent) browser - it does not work. Please find the patch below for reproducing.
In the patched test: When a browser is created a JS query callback is installed with the name - "cef_query_N" - where N counts a browser instance.
When the browser is shown select in the menu: Tests > Show Info. It will execute the JS callback and print its name:
cef_query_1
Then show another browser: Tests > New window, then again: Tests > Show Info. It prints:
"Uncaught TypeError: window.cef_query_2 is not a function"
Then in the second browser select: Tests > Show DevTools. In the Console tab execute: dir(window), then search for "cef" in the Window object:
cef_query_1: ƒ cef_query_1()
cef_query_cancel_1: ƒ cef_query_cancel_1()
The previous "cef_query_1" is only present.
Then if you show another (3rd) browser and look for "cef" in its DevTools you'll find:
cef_query_1: ƒ cef_query_1()
cef_query_2: ƒ cef_query_2()
cef_query_cancel_1: ƒ cef_query_cancel_1()
cef_query_cancel_2: ƒ cef_query_cancel_2()
And so on. The JS context is always one step back.
I did some investigation and discovered the problem has appeared since JCEF 78.2.7+g9cd8bac+chromium-78.0.3904.70. With 77.1.18+g8e8d602+chromium-77.0.3865.120 (I did the update manually) it worked fine. Looks like https://bitbucket.org/chromiumembedded/ ... c4a8724849 could cause it.
I tried it on Windows, but on macOS it's confirmed to be also broken (and on Linux is likely as well).
I doubt there's a workaround, but if it is, could you please advice? Until it is fixed...
Thank you,
Anton.
- Code: Select all
diff --git a/java/tests/detailed/MainFrame.java b/java/tests/detailed/MainFrame.java
index a00e2cb..0987bce 100644
--- a/java/tests/detailed/MainFrame.java
+++ b/java/tests/detailed/MainFrame.java
@@ -79,6 +79,7 @@ public class MainFrame extends BrowserFrame {
frame.setVisible(true);
}
+ public static int queryCounter;
private final CefClient client_;
private String errorMsg_ = "";
private ControlPanel control_pane_;
@@ -137,7 +138,10 @@ public class MainFrame extends BrowserFrame {
// calls (JavaScript binding). We're using the default configuration, so
// that the JavaScript binding methods "cefQuery" and "cefQueryCancel"
// are used.
- CefMessageRouter msgRouter = CefMessageRouter.create();
+ CefMessageRouter.CefMessageRouterConfig config = new CefMessageRouter.CefMessageRouterConfig();
+ config.jsQueryFunction = "cef_query_" + (++queryCounter);
+ config.jsCancelFunction = "cef_query_cancel_" + queryCounter;
+ CefMessageRouter msgRouter = CefMessageRouter.create(config);
msgRouter.addHandler(new MessageRouterHandler(), true);
msgRouter.addHandler(new MessageRouterHandlerEx(client_), false);
client_.addMessageRouter(msgRouter);
diff --git a/java/tests/detailed/handler/MessageRouterHandler.java b/java/tests/detailed/handler/MessageRouterHandler.java
index 1d37c85..9a4b6f7 100644
--- a/java/tests/detailed/handler/MessageRouterHandler.java
+++ b/java/tests/detailed/handler/MessageRouterHandler.java
@@ -13,6 +13,7 @@ public class MessageRouterHandler extends CefMessageRouterHandlerAdapter {
@Override
public boolean onQuery(CefBrowser browser, CefFrame frame, long query_id, String request,
boolean persistent, CefQueryCallback callback) {
+ System.out.println(request);
if (request.indexOf("BindingTest:") == 0) {
// Reverse the message and return it to the JavaScript caller.
String msg = request.substring(12);
diff --git a/java/tests/detailed/ui/MenuBar.java b/java/tests/detailed/ui/MenuBar.java
index 3d7a4d1..b9d39d9 100644
--- a/java/tests/detailed/ui/MenuBar.java
+++ b/java/tests/detailed/ui/MenuBar.java
@@ -366,6 +366,10 @@ public class MenuBar extends JMenuBar {
String js = "var x=window.open(); x.document.open(); x.document.write('" + info
+ "'); x.document.close();";
browser_.executeJavaScript(js, "", 0);
+
+ String jsFunc = "cef_query_" + MainFrame.queryCounter;
+ String jsQuery = "window." + jsFunc + "({request: '" + jsFunc + "'});";
+ browser_.executeJavaScript(jsQuery, "", 0);
}
});
testMenu.add(showInfo);