-
Notifications
You must be signed in to change notification settings - Fork 418
/
Copy pathCEFBrowserComponent.java
522 lines (438 loc) · 16 KB
/
CEFBrowserComponent.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.codename1.impl.javase.cef;
import com.codename1.impl.javase.IBrowserComponent;
import com.codename1.impl.javase.JavaSEPort;
import com.codename1.impl.javase.JavaSEPort.Peer;
import com.codename1.ui.BrowserComponent;
import com.codename1.ui.CN;
import com.codename1.ui.Display;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.BrowserNavigationCallback;
import java.awt.EventQueue;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame;
import org.cef.CefApp;
import org.cef.CefSettings;
import org.cef.browser.CN1CefBrowserFactory;
import org.cef.browser.CefBrowserFactory;
/**
*
* @author shannah
*/
public class CEFBrowserComponent extends Peer implements IBrowserComponent {
private static final String OS = System.getProperty("os.name").toLowerCase();
private static final boolean isWindows = isWindows();
private static final boolean isMac = isMac();
private static final boolean isUnix = isUnix();
private static final boolean is64Bit = is64Bit();
private static final String ARCH = System.getProperty("os.arch");
private static final boolean is64Bit() {
String model = System.getProperty("sun.arch.data.model",
System.getProperty("com.ibm.vm.bitmode"));
if (model != null) {
return "64".equals(model);
}
if ("x86-64".equals(ARCH)
|| "ia64".equals(ARCH)
|| "ppc64".equals(ARCH) || "ppc64le".equals(ARCH)
|| "sparcv9".equals(ARCH)
|| "mips64".equals(ARCH) || "mips64el".equals(ARCH)
|| "amd64".equals(ARCH)
|| "aarch64".equals(ARCH)) {
return true;
}
return false;
}
static {
CefBrowserFactory.setInstance(new CN1CefBrowserFactory());
}
private static boolean isWindows() {
return (OS.indexOf("win") >= 0);
}
private static boolean isMac() {
return (OS.indexOf("mac") >= 0);
}
private static boolean isUnix() {
return (OS.indexOf("nux") >= 0);
}
private BrowserPanel panel;
private boolean ready;
private List<Runnable> readyCallbacks = new LinkedList<Runnable>();
private com.codename1.ui.events.FocusListener focusListener = new com.codename1.ui.events.FocusListener() {
@Override
public void focusGained(com.codename1.ui.Component cmp) {
EventQueue.invokeLater(new Runnable() {
public void run() {
panel.requestFocus();
}
});
}
@Override
public void focusLost(com.codename1.ui.Component cmp) {
}
};
public CEFBrowserComponent(JFrame frame, BrowserPanel browserPanel) {
super(frame, browserPanel);
this.panel = browserPanel;
setFocusable(true);
}
@Override
protected void finalize() throws Throwable {
cleanup();
super.finalize();
}
@Override
protected void initComponent() {
super.initComponent();
addFocusListener(focusListener);
}
@Override
protected void deinitialize() {
removeFocusListener(focusListener);
super.deinitialize();
}
private static String getLibPath() {
String out = System.getProperty("cef.libPath", null);
if (out != null) {
return out;
}
if (isMac) {
String cefRoot = System.getProperty("cef.dir",
System.getProperty("user.home")+File.separator+".codenameone"+File.separator+"cef")+File.separator;
return cefRoot + "macos64";
} else if (isWindows) {
String bitSuffix = is64Bit ? "64" : "32";
String cefRoot = System.getProperty("cef.dir",
System.getProperty("user.home")+File.separator+".codenameone"+File.separator+"cef")+File.separator+"lib"+File.separator;
return cefRoot + "win"+bitSuffix;
} else if (isUnix && is64Bit) {
String bitSuffix = is64Bit ? "64" : "32";
String cefRoot = System.getProperty("cef.dir",
System.getProperty("user.home")+File.separator+".codenameone"+File.separator+"cef")+File.separator+"lib"+File.separator;
return cefRoot + "linux"+bitSuffix;
}else {
throw new UnsupportedOperationException("CEF Not implemented on this platform yet");
}
}
private static String[] createArgs() {
List<String> args = new ArrayList<String>();
if (isMac) {
if (!"true".equals(System.getProperty("cn1.cef.bundled"))) {
// The cn1.cef.bundled flag is set in SEWrapper to indicate that CEF is bundled in the .app
// Otherwise it needs to get CEF from the central location specified
args.add(String.format("--framework-dir-path=%s/Chromium Embedded Framework.framework", getLibPath()));
args.add(String.format("--main-bundle-path=%s/jcef Helper.app", getLibPath()));
args.add(String.format("--browser-subprocess-path=%s/jcef Helper.app/Contents/MacOS/jcef Helper", getLibPath()));
}
args.add("--disable-gpu");
} else if (isWindows) {
// no extra stuff here
//args.add(String.format("--browser-subprocess-path=%s\\jcef_helper.exer", getLibPath()));
args.add("--disable-gpu");
args.add("--disable-software-rasterizer");
args.add("--disable-gpu-compositing");
} else if (isUnix) {
// no extra stuff here
//args.add(String.format("--browser-subprocess-path=%s\\jcef_helper.exer", getLibPath()));
args.add("--disable-gpu");
args.add("--disable-software-rasterizer");
args.add("--disable-gpu-compositing");
} else {
throw new UnsupportedOperationException("CEF Not implemented on this platform yet");
}
//args.add("--allow-file-access-from-files");
args.add("--touch-events=enabled");
args.add("--enable-media-stream");
//args.add("--device-scale-factor=4");
//args.add("--force-device-scale-factor=4");
args.add("--autoplay-policy=no-user-gesture-required");
args.add("--enable-usermedia-screen-capturing");
//System.out.println("CEF Args: "+args);
return args.toArray(new String[args.size()]);
}
public static CEFBrowserComponent create(BrowserComponent bc) {
return create(new CEFBrowserComponentAdapter(bc));
}
public static CEFBrowserComponent create(CEFBrowserComponentListener parent) {
return create(null, parent);
}
public static CEFBrowserComponent create(final String startingURL, final CEFBrowserComponentListener parent) {
CefSettings settings = new CefSettings();
String[] args = createArgs();
// Perform startup initialization on platforms that require it.
if (!"true".equals(System.getProperty("cef.started", "false"))) {
if (!CefApp.startup(args)) {
System.err.println("CEFStartup initialization failed");
throw new RuntimeException("CEF Startup initialization failed!");
}
System.setProperty("cef.started", "true");
}
// OSR mode is enabled by default on Linux.
// and disabled by default on Windows and Mac OS X.
boolean osrEnabledArg = true;
boolean transparentPaintingEnabledArg = true;
boolean createImmediately = true;
for (String arg : args) {
arg = arg.toLowerCase();
if (arg.equals("--off-screen-rendering-enabled")) {
osrEnabledArg = true;
} else if (arg.equals("--transparent-painting-enabled")) {
transparentPaintingEnabledArg = true;
} else if (arg.equals("--create-immediately")) {
createImmediately = true;
}
}
CEFPeerComponentBuffer buffer = new CEFPeerComponentBuffer();
final WeakReference<CEFBrowserComponentListener> parentRef = new WeakReference<CEFBrowserComponentListener>(parent);
BrowserNavigationCallback navigationCallback = new BrowserNavigationCallback() {
private CEFBrowserComponentListener l = parentRef.get();
@Override
public boolean shouldNavigate(String url) {
//System.out.println("in shouldNavigate "+url);
//CEFBrowserComponentListener l = parentRef.get();
if (l != null) {
return l.shouldNavigate(url);
}
return false;
}
};
final BrowserPanel panel = new BrowserPanel(
startingURL, buffer, navigationCallback, osrEnabledArg, transparentPaintingEnabledArg, createImmediately, args) {
private CEFBrowserComponentListener p = parentRef.get();
@Override
protected void onError(ActionEvent l) {
//CEFBrowserComponentListener p = parentRef.get();
if (p != null) {
p.onError(l);
}
}
@Override
protected void onStart(ActionEvent l) {
if (p != null) {
p.onStart(l);
}
}
@Override
protected void onLoad(ActionEvent l) {
if (p != null) {
p.onLoad(l);
}
}
};
java.awt.Container cnt = JavaSEPort.instance.getCanvas().getParent();
while (!(cnt instanceof JFrame)) {
cnt = cnt.getParent();
if (cnt == null) {
System.err.println("CEFBrowserComponent requires a JFrame as an ancestor. None found. Returning null");
return null;
}
}
final CEFBrowserComponent out = new CEFBrowserComponent((JFrame)cnt, panel);
out.setPeerComponentBuffer(buffer);
final WeakReference<CEFBrowserComponent> weakRef = new WeakReference<CEFBrowserComponent>(out);
panel.setReadyCallback(new Runnable() {
public void run() {
CEFBrowserComponent callback = weakRef.get();
if (callback != null) {
callback.fireReady();
}
}
});
return out;
}
@Override
public void back() {
if (!ready) {
readyCallbacks.add(new Runnable() {
public void run() {
back();
}
});
return;
}
panel.getBrowser().goBack();
}
@Override
public void forward() {
if (!ready) {
readyCallbacks.add(new Runnable() {
public void run() {
forward();
}
});
return;
}
panel.getBrowser().goForward();
}
@Override
public void setPage(String html, String baseUrl) {
//String url = "data:text/html,"+com.codename1.io.Util.encodeUrl(html);
if (Display.getInstance().getProperty("cef.setPage.useDataURI", "false").equals("true")) {
setURL("data:text/html,"+com.codename1.io.Util.encodeUrl(html));
return;
}
try {
byte[] bytes = html.getBytes("UTF-8");
StreamWrapper stream = new StreamWrapper(new ByteArrayInputStream(bytes), "text/html" , bytes.length);
String id = BrowserPanel.getStreamRegistry().registerStream(stream);
String url = "cn1stream://stream/"+id;
setURL(url);
} catch (Exception ex) {
setURL("data:text/html,"+com.codename1.io.Util.encodeUrl(html));
}
}
@Override
public String getTitle() {
if (!ready) {
return title_;
}
return panel.getTitle();
}
@Override
public String getURL() {
if (!ready) {
return url_;
}
return panel.getURL();
}
private String url_;
private String title_;
private final Object readyLock = new Object();
@Override
public void setURL(final String url) {
synchronized(readyLock) {
if (!ready) {
url_ = url;
readyCallbacks.add(new Runnable() {
public void run() {
setURL(url);
}
});
return;
}
}
url_ = url;
panel.getBrowser().loadURL(url);
}
@Override
public void stop() {
if (!ready) {
readyCallbacks.add(new Runnable() {
public void run() {
stop();
}
});
return;
}
panel.getBrowser().stopLoad();
}
@Override
public void reload() {
if (!ready) {
readyCallbacks.add(new Runnable() {
public void run() {
reload();
}
});
return;
}
panel.getBrowser().reload();
}
@Override
public boolean hasBack() {
if (!ready) {
return false;
}
return panel.getBrowser().canGoBack();
}
@Override
public boolean hasForward() {
if (!ready) {
return false;
}
return panel.getBrowser().canGoForward();
}
@Override
public void execute(final String js) {
if (!ready) {
readyCallbacks.add(new Runnable() {
public void run() {
execute(js);
}
});
return;
}
panel.getBrowser().executeJavaScript(js, js, 0);
}
@Override
public String executeAndReturnString(String js) {
throw new UnsupportedOperationException("Not supported .");
}
@Override
public void setProperty(String key, Object value) {
if(key.equalsIgnoreCase("User-Agent")) {
//panel.getBrowser().getClient().
}
}
@Override
public void runLater(Runnable r) {
EventQueue.invokeLater(r);
}
@Override
public void clearHistory() {
}
@Override
public void exposeInJavaScript(Object o, String name) {
}
public void fireReady() {
if (!CN.isEdt()) {
CN.callSerially(new Runnable() {
public void run() {
fireReady();
}
});
return;
}
List<Runnable> toRun = null;
synchronized(readyLock) {
if (!ready) {
ready = true;
if (!readyCallbacks.isEmpty()) {
toRun = new ArrayList<Runnable>(readyCallbacks);
readyCallbacks.clear();
}
}
}
if (toRun != null && !toRun.isEmpty()) {
while (!toRun.isEmpty()) {
toRun.remove(0).run();
}
}
}
@Override
public boolean supportsExecuteAndReturnString() {
return false;
}
public void cleanup() {
if (panel != null) {
final BrowserPanel fPanel = panel;
panel = null;
EventQueue.invokeLater(new Runnable() {
public void run() {
if (fPanel != null) {
fPanel.cleanup();
}
}
});
}
}
}