Skip to content

Commit 6edd30f

Browse files
committed
Remove global instances
1 parent 70f1dec commit 6edd30f

File tree

4 files changed

+91
-97
lines changed

4 files changed

+91
-97
lines changed

src/main/java/com/duckly/jbcef/JBCefApp.java

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void dispose() {
8080
//fixme use addCefCustomSchemeHandlerFactory method if possible
8181
private static final JBCefSourceSchemeHandlerFactory ourSourceSchemeHandlerFactory = new JBCefSourceSchemeHandlerFactory();
8282

83-
JBCefApp(@NotNull JCefAppConfig config) throws IllegalStateException {
83+
public JBCefApp(@NotNull JCefAppConfig config) throws IllegalStateException {
8484
boolean started = false;
8585
try {
8686
started = CefApp.startup(ArrayUtil.EMPTY_STRING_ARRAY);
@@ -187,44 +187,44 @@ Disposable getDisposable() {
187187
return myDisposable;
188188
}
189189

190-
/**
191-
* Returns {@code JBCefApp} instance. If the app has not yet been initialized
192-
* then starts up CEF and initializes the app.
193-
*
194-
* @throws IllegalStateException when JCEF initialization is not possible in current env
195-
*/
196-
@NotNull
197-
public static JBCefApp getInstance() {
198-
if (Holder.INSTANCE == null) {
199-
throw new IllegalStateException("JCEF is not supported in this env or failed to initialize");
200-
}
201-
return Holder.INSTANCE;
202-
}
203-
204-
private static final class Holder {
205-
@Nullable static final JBCefApp INSTANCE;
206-
207-
static {
208-
ourInitialized.set(true);
209-
JCefAppConfig config = null;
210-
if (isSupported(true)) {
211-
try {
212-
config = JCefAppConfig.getInstance();
213-
}
214-
catch (Exception e) {
215-
LOG.error(e);
216-
}
217-
}
218-
JBCefApp app = null;
219-
if (config != null) {
220-
try {
221-
app = new JBCefApp(config);
222-
} catch (IllegalStateException ignore) {
223-
}
224-
}
225-
INSTANCE = app;
226-
}
227-
}
190+
// /**
191+
// * Returns {@code JBCefApp} instance. If the app has not yet been initialized
192+
// * then starts up CEF and initializes the app.
193+
// *
194+
// * @throws IllegalStateException when JCEF initialization is not possible in current env
195+
// */
196+
// @NotNull
197+
// public static JBCefApp getInstance() {
198+
// if (Holder.INSTANCE == null) {
199+
// throw new IllegalStateException("JCEF is not supported in this env or failed to initialize");
200+
// }
201+
// return Holder.INSTANCE;
202+
// }
203+
//
204+
// private static final class Holder {
205+
// @Nullable static final JBCefApp INSTANCE;
206+
//
207+
// static {
208+
// ourInitialized.set(true);
209+
// JCefAppConfig config = null;
210+
// if (isSupported(true)) {
211+
// try {
212+
// config = JCefAppConfig.getInstance();
213+
// }
214+
// catch (Exception e) {
215+
// LOG.error(e);
216+
// }
217+
// }
218+
// JBCefApp app = null;
219+
// if (config != null) {
220+
// try {
221+
// app = new JBCefApp(config);
222+
// } catch (IllegalStateException ignore) {
223+
// }
224+
// }
225+
// INSTANCE = app;
226+
// }
227+
// }
228228

229229
/**
230230
* Returns whether JCEF is supported. For that:
@@ -305,7 +305,7 @@ private static boolean isSupported(boolean logging) {
305305

306306
@NotNull
307307
public JBCefClient createClient() {
308-
return new JBCefClient(myCefApp.createClient());
308+
return new JBCefClient(myCefApp.createClient(), myDisposable);
309309
}
310310

311311
/**

src/main/java/com/duckly/jbcef/JBCefBrowser.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public JBCefBrowser(@NotNull CefBrowser cefBrowser, @NotNull JBCefClient client)
186186
this(cefBrowser, client, false, null);
187187
}
188188

189-
private JBCefBrowser(@NotNull JBCefClient client, boolean isDefaultClient, @Nullable String url) {
189+
public JBCefBrowser(@NotNull JBCefClient client, boolean isDefaultClient, @Nullable String url) {
190190
this(null, client, isDefaultClient, url);
191191
}
192192

@@ -375,22 +375,22 @@ private static void loadString(CefBrowser cefBrowser, String html, String url) {
375375
cefBrowser.loadURL(url);
376376
}
377377

378-
/**
379-
* Creates a browser with default {@link JBCefClient}. The default client is disposed with this browser and may not be used with other browsers.
380-
*/
381-
@SuppressWarnings("unused")
382-
public JBCefBrowser() {
383-
this(JBCefApp.getInstance().createClient(), true, null);
384-
}
385-
386-
/**
387-
* @see #JBCefBrowser()
388-
* @param url initial url
389-
*/
390-
@SuppressWarnings("unused")
391-
public JBCefBrowser(@NotNull String url) {
392-
this(JBCefApp.getInstance().createClient(), true, url);
393-
}
378+
// /**
379+
// * Creates a browser with default {@link JBCefClient}. The default client is disposed with this browser and may not be used with other browsers.
380+
// */
381+
// @SuppressWarnings("unused")
382+
// public JBCefBrowser() {
383+
// this(JBCefApp.getInstance().createClient(), true, null);
384+
// }
385+
//
386+
// /**
387+
// * @see #JBCefBrowser()
388+
// * @param url initial url
389+
// */
390+
// @SuppressWarnings("unused")
391+
// public JBCefBrowser(@NotNull String url) {
392+
// this(JBCefApp.getInstance().createClient(), true, url);
393+
// }
394394

395395
@NotNull
396396
public JComponent getComponent() {

src/main/java/com/duckly/jbcef/JBCefClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package com.duckly.jbcef;
33

44
//import com.intellij.application.options.RegistryManager;
5+
import com.intellij.openapi.Disposable;
56
import com.intellij.openapi.diagnostic.Logger;
67
import com.intellij.openapi.util.Disposer;
78
import com.intellij.openapi.util.Ref;
@@ -77,9 +78,9 @@ public class JBCefClient implements JBCefDisposable {
7778
private final HandlerSupport<CefLoadHandler> myLoadHandler = new HandlerSupport<>();
7879
private final HandlerSupport<CefRequestHandler> myRequestHandler = new HandlerSupport<>();
7980

80-
JBCefClient(@NotNull CefClient client) {
81+
JBCefClient(@NotNull CefClient client, @NotNull Disposable parentDisposable) {
8182
myCefClient = client;
82-
Disposer.register(JBCefApp.getInstance().getDisposable(), this);
83+
Disposer.register(parentDisposable, this);
8384

8485
Runnable createPool = () -> {
8586
if (myJSQueryPool != null) {

src/main/java/com/duckly/jbcef/JCefAppConfig.java

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,31 @@
1616
* @author Anton Tarasov
1717
*/
1818
public abstract class JCefAppConfig {
19-
protected final CefSettings cefSettings;
19+
protected final CefSettings cefSettings = new CefSettings();
2020
protected final List<String> appArgs = new ArrayList<>();
2121

2222
private static final AtomicReference<Double> forceDeviceScaleFactor = new AtomicReference<>(Double.valueOf(0));
2323

24-
JCefAppConfig() {
25-
this.cefSettings = new CefSettings();
26-
}
27-
28-
JCefAppConfig(JCefAppConfig config) {
29-
this.cefSettings = config.cefSettings.clone();
30-
this.appArgs.addAll(config.appArgs);
31-
}
32-
33-
private static class Holder {
34-
static JCefAppConfig INSTANCE;
35-
36-
static {
37-
if (OS.isMacintosh()) {
38-
INSTANCE = new JCefAppConfigMac();
39-
}
40-
else if (OS.isLinux()) {
41-
INSTANCE = new JCefAppConfigLinux();
42-
}
43-
else if (OS.isWindows()) {
44-
INSTANCE = new JCefAppConfigWindows();
45-
}
46-
else {
47-
INSTANCE = null;
48-
assert false : "JCEF: unknown platform";
49-
}
24+
// private static class Holder {
25+
// static JCefAppConfig INSTANCE;
26+
//
27+
// static {
28+
// INSTANCE = newInstance();
29+
// assert INSTANCE != null : "JCEF: unknown platform";
30+
// }
31+
// }
32+
33+
public static JCefAppConfig newInstance() {
34+
if (OS.isMacintosh()) {
35+
return new JCefAppConfigMac();
36+
}
37+
if (OS.isLinux()) {
38+
return new JCefAppConfigLinux();
5039
}
40+
if (OS.isWindows()) {
41+
return new JCefAppConfigWindows();
42+
}
43+
return null;
5144
}
5245

5346
public String[] getAppArgs() {
@@ -62,17 +55,17 @@ public CefSettings getCefSettings() {
6255
return cefSettings;
6356
}
6457

65-
/**
66-
* @throws IllegalStateException in case of unsupported platform
67-
*/
68-
public static JCefAppConfig getInstance() {
69-
if (Holder.INSTANCE != null) {
70-
Holder.INSTANCE.init();
71-
} else {
72-
throw new IllegalStateException("JCEF is not supported on this platform");
73-
}
74-
return Holder.INSTANCE;
75-
}
58+
// /**
59+
// * @throws IllegalStateException in case of unsupported platform
60+
// */
61+
// public static JCefAppConfig getInstance() {
62+
// if (Holder.INSTANCE != null) {
63+
// Holder.INSTANCE.init();
64+
// } else {
65+
// throw new IllegalStateException("JCEF is not supported on this platform");
66+
// }
67+
// return Holder.INSTANCE;
68+
// }
7669

7770
/**
7871
* Tries to load full JCEF version string from version.info file

0 commit comments

Comments
 (0)