-
Notifications
You must be signed in to change notification settings - Fork 26
/
JourneyBrowserView.java
176 lines (152 loc) · 6.75 KB
/
JourneyBrowserView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.codebrig.journey;
import com.codebrig.journey.proxy.CefAppProxy;
import com.codebrig.journey.proxy.CefBrowserProxy;
import com.codebrig.journey.proxy.CefClientProxy;
import com.codebrig.journey.proxy.browser.CefFrameProxy;
import com.codebrig.journey.proxy.handler.CefLifeSpanHandlerProxy;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import static org.joor.Reflect.on;
/**
* Wraps CefApp/CefClient/CefBrowser and extends JComponent for ease of implementation.
*
* @author <a href="mailto:brandon.fergerson@codebrig.com">Brandon Fergerson</a>
* @version 0.4.0
* @since 0.1.1
*/
@SuppressWarnings({"WeakerAccess", "unused"})
public class JourneyBrowserView extends JComponent {
public static final String ABOUT_BLANK = "about:blank";
public static final JourneySettings DEFAULT_SETTINGS;
static {
JourneyLoader.setup();
DEFAULT_SETTINGS = new JourneySettings();
DEFAULT_SETTINGS.setWindowlessRenderingEnabled(false);
DEFAULT_SETTINGS.setLogFile(new File(JourneyLoader.NATIVE_DIRECTORY, "debug.log").getAbsolutePath());
}
private static JourneySettings journeySettings;
private static CefAppProxy cefApp;
private CefClientProxy cefClient;
private CefBrowserProxy cefBrowser;
public JourneyBrowserView(CefBrowserProxy browser) {
this.cefBrowser = Objects.requireNonNull(browser);
this.cefClient = browser.getClient();
setLayout(new BorderLayout());
if (SwingUtilities.isEventDispatchThread()) {
add(cefBrowser.getUIComponent(), "Center");
} else {
try {
SwingUtilities.invokeAndWait(() -> {
add(cefBrowser.getUIComponent(), "Center");
});
} catch (InterruptedException | InvocationTargetException ex) {
throw new RuntimeException(ex);
}
}
}
public JourneyBrowserView() {
this(getDefaultCEFArguments(), DEFAULT_SETTINGS, ABOUT_BLANK);
}
public JourneyBrowserView(JourneySettings journeySettings, String initialUrl) {
this(getDefaultCEFArguments(), journeySettings, initialUrl);
}
public JourneyBrowserView(String initialUrl) {
this(getDefaultCEFArguments(), DEFAULT_SETTINGS, initialUrl);
}
public JourneyBrowserView(String[] args, JourneySettings journeySettings, String initialUrl) {
JourneyBrowserView.journeySettings = journeySettings;
setLayout(new BorderLayout());
if (SwingUtilities.isEventDispatchThread()) {
if (cefApp == null) {
Object realCefApp = on(JourneyLoader.getJourneyClassLoader().loadClass("org.cef.CefApp"))
.call("getInstance", args, journeySettings.asCefSettings()).get();
cefApp = on(realCefApp).as(CefAppProxy.class, JourneyLoader.getJourneyClassLoader());
}
cefClient = cefApp.createClient();
cefBrowser = cefClient.createBrowser(initialUrl, false, false);
add(cefBrowser.getUIComponent(), "Center");
} else {
try {
SwingUtilities.invokeAndWait(() -> {
if (cefApp == null) {
Object realCefApp = on(JourneyLoader.getJourneyClassLoader().loadClass("org.cef.CefApp"))
.call("getInstance", args, journeySettings.asCefSettings()).get();
cefApp = on(realCefApp).as(CefAppProxy.class, JourneyLoader.getJourneyClassLoader());
}
cefClient = cefApp.createClient();
cefBrowser = cefClient.createBrowser(initialUrl, false, false);
add(cefBrowser.getUIComponent(), "Center");
});
} catch (InterruptedException | InvocationTargetException ex) {
throw new RuntimeException(ex);
}
}
//https://github.com/CodeBrig/Journey/issues/13
if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
cefClient.addLifeSpanHandler(CefLifeSpanHandlerProxy.createHandler(new CefLifeSpanHandlerProxy() {
@Override
public boolean onBeforePopup(CefBrowserProxy browser, CefFrameProxy frame, String targetUrl, String targetFrameName) {
return false;
}
@Override
public void onAfterCreated(CefBrowserProxy browser) {
new Timer().schedule(
new TimerTask() {
@Override
public void run() {
if (browser.getZoomLevel() != -1.5) {
browser.setZoomLevel(-1.5);
}
}
}, 0, 50
);
}
@Override
public void onAfterParentChanged(CefBrowserProxy browser) {
}
@Override
public boolean doClose(CefBrowserProxy browser) {
return false;
}
@Override
public void onBeforeClose(CefBrowserProxy browser) {
}
}));
}
}
public JourneySettings getJourneySettings() {
return journeySettings;
}
public CefAppProxy getCefApp() {
return cefApp;
}
public CefClientProxy getCefClient() {
return cefClient;
}
public CefBrowserProxy getCefBrowser() {
return cefBrowser;
}
private static String[] getDefaultCEFArguments() {
if (System.getProperty("os.name").toLowerCase().startsWith("mac")) {
File frameworkDirPath = new File(JourneyLoader.NATIVE_DIRECTORY,
"jcef_app.app/Contents/Frameworks/Chromium Embedded Framework.framework");
File browserSubprocessPath = new File(JourneyLoader.NATIVE_DIRECTORY,
"jcef_app.app/Contents/Frameworks/jcef Helper.app/Contents/MacOS/jcef Helper");
File resourcesDirPath = new File(JourneyLoader.NATIVE_DIRECTORY,
"jcef_app.app/Contents/Frameworks/Chromium Embedded Framework.framework/Resources");
return new String[]{
"--framework-dir-path=" + frameworkDirPath.getAbsolutePath(),
"--browser-subprocess-path=" + browserSubprocessPath.getAbsolutePath(),
"--resources-dir-path=" + resourcesDirPath.getAbsolutePath(),
"--disable-gpu"
};
} else {
return new String[0];
}
}
}