Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 918dff42c789efa804749576c1ec03056937bd59 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.browser;


import java.io.*;
import org.eclipse.swt.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.cef3.*;
import org.eclipse.swt.internal.win32.*;
import org.eclipse.swt.widgets.*;

public class CEF extends WebBrowser {
	boolean ignoreDispose;
	CEFClient client;
	CEFBrowser cefBrowser;

	static boolean LibraryLoaded;
	static CEFApp App;
	static String InitError;
	
	public static cef_string_t STRING_EMPTY;
	
	static final String CEF3_PATH = "org.eclipse.swt.browser.CEF3Path"; //$NON-NLS-1$

	static {
		/*
		 * To use CEF3 the full path of its extracted package must be provided in the
		 * org.eclipse.swt.browser.CEF3Path java property.  If a value representing a
		 * valid directory is set for this property then add it to the library lookup
		 * path and then attempt to load the swt-cef3 library.
		 */
		
		// TODO is InitError really needed?  Some failure branches below don't set it
		String CEF3Path = System.getProperty(CEF3_PATH);
		if (CEF3Path.length() > 0) {
			File file = new File(CEF3Path);
			if (!(file.exists() && file.isDirectory())) {
				InitError = "org.eclipse.swt.browser.CEF3Path value is not valid: " + CEF3Path;  // $NON-NLS-1$
			} else {
				TCHAR buffer = new TCHAR(0, CEF3Path, true);
				boolean success = OS.SetDllDirectory(buffer);
				if (success) {
					try {
						Library.loadLibrary("swt-cef3"); // $NON-NLS-1$

						/* initialize STRING_EMPTY */
						long /*int*/ string = CreateCEFString("");
						STRING_EMPTY = new cef_string_t();
						CEF3.memmove(STRING_EMPTY, string, cef_string_t.sizeof);

						/* initialize CEF3 */
						cef_main_args_t args = new cef_main_args_t();
						args.instance = OS.GetModuleHandle(null);
						App = new CEFApp();
						App.add_ref();
						int exitCode = CEF3.cef_execute_process(args, App.getAddress());
						if (exitCode < 0) {

							/* extract the subprocess executable */
							String NAME_SUBPROC_EXE = "swt-cef3subproc-win32.exe"; //$NON-NLS-1$
							File directory = new File(System.getProperty("user.home"), Library.SWT_LIB_DIR()); //$NON-NLS-1$
							file = new File(directory, NAME_SUBPROC_EXE);
							if (!file.exists()) {
								java.io.InputStream is = Library.class.getResourceAsStream("/" + NAME_SUBPROC_EXE); //$NON-NLS-1$
								if (is != null) {
									if (!directory.exists()) {
										directory.mkdirs();
									}
									int read;
									byte[] bytes = new byte[4096];
									try {
										FileOutputStream os = new FileOutputStream(file);
										while ((read = is.read(bytes)) != -1) {
											os.write(bytes, 0, read);
										}
										os.close();
										is.close();
									} catch (Exception e) {
									}
								}
							}

							if (!file.exists()) {
								InitError = "Failed to extract SWT's cef3 subprocess executable: " + NAME_SUBPROC_EXE; //$NON-NLS-1$
							} else {
								cef_settings_t settings = new cef_settings_t();
								settings.size = cef_settings_t.sizeof;
								settings.multi_threaded_message_loop = 1;
								settings.release_dcheck_enabled = 1;
								long /*int*/ subprocessPath = CreateCEFString(file.getAbsolutePath());
								settings.browser_subprocess_path = new cef_string_t();
								CEF3.memmove(settings.browser_subprocess_path, subprocessPath, cef_string_t.sizeof);
								App.add_ref();
								int rc = CEF3.cef_initialize(args, settings, App.getAddress());
								if (rc != 0) {
									LibraryLoaded = true;
								} else {
									InitError = "cef3 initialization failed"; //$NON-NLS-1$
								}
							}
						}
					} catch (Throwable e) {
						InitError = "Failed to load the swt-cef3 library, org.eclipse.swt.browser.CEF3Path: " + CEF3Path; //$NON-NLS-1$
					}
				}
			}
		}

		if (LibraryLoaded) {
			Display.getCurrent().addListener(SWT.Dispose,  new Listener() {
				public void handleEvent(Event event) {
					App.release();
					App = null;
				}
			});

			NativeClearSessions = new Runnable() {
				public void run() {
					if (!LibraryLoaded) return;
					// TODO do it
				}
			};

			NativeGetCookie = new Runnable() {
				public void run() {
					if (!LibraryLoaded) return;
					// TODO use CookieUrl and CookieName, set value of CookieValue
				}
			};

			NativeSetCookie = new Runnable() {
				public void run() {
					if (!LibraryLoaded) return;
					// TODO set CookieValue on CookieUrl, set CookieResult to true if successful
				}
			};

			if (NativePendingCookies != null) {
				SetPendingCookies(NativePendingCookies);
				NativePendingCookies = null;
			}
		} else {
			if (App != null) {
				App.release();
				App = null;
			}
		}
	}


static long /*int*/ CreateCEFString(String string) {
	char[] chars = new char[string.length()];
	string.getChars(0, string.length(), chars, 0);
	long /*int*/ result = CEF3.cef_string_userfree_alloc();
	CEF3.cef_string_set(chars, chars.length, result, 1);
	return result;
}

static boolean IsInstalled() {
	if (!LibraryLoaded) return false;
	// TODO if CEF3 has API to get its version then verify that it is supported, for now assume that it is
	return true;
}

public void create(Composite parent, int style) {
	cef_window_info_t windowInfo = new cef_window_info_t();
	int extStyle = OS.WS_EX_NOINHERITLAYOUT;
	if ((style & SWT.BORDER) != 0) extStyle |= OS.WS_EX_CLIENTEDGE;
	if ((style & SWT.RIGHT_TO_LEFT) != 0) extStyle |= OS.WS_EX_LAYOUTRTL;
	windowInfo.ex_style = extStyle; 
	windowInfo.style = OS.WS_CHILD | OS.WS_VISIBLE | OS.WS_CLIPSIBLINGS;
	windowInfo.x = windowInfo.width = OS.CW_USEDEFAULT;
	windowInfo.ex_style = 0; 
	windowInfo.window_name = STRING_EMPTY;
	windowInfo.parent_window = browser.handle;

	cef_browser_settings_t browserSettings = new cef_browser_settings_t();
	browserSettings.size = cef_browser_settings_t.sizeof;

	long /*int*/ url = CreateCEFString("about:blank");
	client = new CEFClient(this);
	client.add_ref();
	int rc = CEF3.cef_browser_host_create_browser(windowInfo, client.getAddress(), url, browserSettings);
	if (rc == 0) {
		return;
	}

	Listener listener = new Listener() {
		public void handleEvent(Event event) {
			switch (event.type) {
				case SWT.Dispose: {
					/* make this handler run after other dispose listeners */
					if (ignoreDispose) {
						ignoreDispose = false;
						break;
					}
					ignoreDispose = true;
					browser.notifyListeners(event.type, event);
					event.type = SWT.NONE;
					onDispose(event);
					break;
				}
			}
		}
	};
	browser.addListener(SWT.Dispose, listener);
	browser.addListener(SWT.KeyDown, listener);
}

public boolean back() {
	// TODO
	return false;
}

public boolean close() {
	// TODO
	return false;
}

void browserCreated(long /*int*/ handle) {
	cefBrowser = new CEFBrowser(handle);
	cefBrowser.add_ref(); // TODO release() when disposed
}

public boolean execute(String script) {
	// TODO
	return false;
}

public boolean forward() {
	// TODO
	return false;
}

public String getBrowserType() {
	// TODO should this return "CEF3"?
	return "webkit"; //$NON-NLS-1$
}

public String getText() {
	// TODO
	return null;
}

public String getUrl() {
	// TODO
	return null;
}

public boolean isBackEnabled() {
	// TODO
	return false;
}

public boolean isForwardEnabled() {
	// TODO
	return false;
}

void onDispose(Event e) {
	if (client != null) {
		client.release();
	}
	client = null;
	if (cefBrowser != null) {
		cefBrowser.release();
	}
	cefBrowser = null;
	// TODO more clean up
}

public void refresh() {
	// TODO
}

public boolean setText(String html, boolean trusted) {
	// TODO
	return false;
}

public boolean setUrl(String url, String postData, String[] headers) {
	// TODO postData, headers...
	long /*int*/ result = cefBrowser.get_main_frame();
	if (result == 0) {
		return false;
	}

	CEFFrame frame = new CEFFrame(result);
	long /*int*/ urlPtr = CreateCEFString(url);
	frame.load_url(urlPtr);
	return true;
}

public void stop() {
	// TODO
}

}

Back to the top