Skip to content

Commit

Permalink
fix an issue caused by chromium not setting the context class loader …
Browse files Browse the repository at this point in the history
…for the thread
  • Loading branch information
GiantLuigi4 committed Jun 14, 2023
1 parent ca1b1cc commit c77db46
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ minecraft_version=1.19.2
yarn_mappings=1.19+build.1

# Mod Properties
mod_version = 1.2.4
mod_version = 1.2.5
maven_group = io.github.javaarchive.forgecef
archives_base_name = mcef

Expand Down
36 changes: 34 additions & 2 deletions src/main/java/net/montoyo/mcef/client/AppHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
import org.cef.CefApp;
import org.cef.browser.CefBrowser;
import org.cef.browser.CefFrame;
import org.cef.callback.CefCallback;
import org.cef.callback.CefSchemeHandlerFactory;
import org.cef.callback.CefSchemeRegistrar;
import org.cef.handler.CefAppHandlerAdapter;
import org.cef.handler.CefResourceHandler;
import org.cef.misc.BoolRef;
import org.cef.misc.IntRef;
import org.cef.misc.StringRef;
import org.cef.network.CefRequest;
import org.cef.network.CefResponse;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -90,15 +95,42 @@ public void onContextInitialized() {
}

private static class SchemeHandlerFactory implements CefSchemeHandlerFactory {

private Class<? extends IScheme> cls;

private static final ClassLoader clr;

static {
// this may look odd, but it makes CEF load a *lot* faster, and also makes it not spam errors to console
ClassLoader c = Thread.currentThread().getContextClassLoader();
if (c == null) c = SchemeResourceHandler.class.getClassLoader();
clr = c;

try {
//noinspection unused
Class<?>[] LOADER = new Class[] {
IntRef.class,
BoolRef.class,
CefRequest.class,
StringRef.class,
SchemeResponseData.class,
SchemeResponseHeaders.class,
Class.forName("org.cef.callback.CefCallback_N"),
Class.forName("org.cef.network.CefResponse_N")
};
} catch (Throwable err) {
err.printStackTrace();
}
}

private final Class<? extends IScheme> cls;

private SchemeHandlerFactory(Class<? extends IScheme> cls) {
this.cls = cls;
}

@Override
public CefResourceHandler create(CefBrowser browser, CefFrame frame, String schemeName, CefRequest request) {
Thread.currentThread().setContextClassLoader(clr);

try {
return new SchemeResourceHandler(cls.newInstance());
} catch(Throwable t) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/montoyo/mcef/client/SchemeResourceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@

public class SchemeResourceHandler extends CefResourceHandlerAdapter {

// forge causes some problems without this
private static final ClassLoader clr;

static {
ClassLoader c = Thread.currentThread().getContextClassLoader();
if (c == null) c = SchemeResourceHandler.class.getClassLoader();
clr = c;
}

private final IScheme scheme;

public SchemeResourceHandler(IScheme scm) {
Expand All @@ -19,6 +28,8 @@ public SchemeResourceHandler(IScheme scm) {

@Override
public boolean processRequest(CefRequest request, CefCallback callback) {
Thread.currentThread().setContextClassLoader(clr);

SchemePreResponse resp = scheme.processRequest(request.getURL());

switch(resp) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ issueTrackerURL="" #optional

modId="forgecef" #mandatory

version="1.2.4" #mandatory
version="1.2.5" #mandatory

displayName="MCEF" #mandatory

Expand Down

0 comments on commit c77db46

Please sign in to comment.