-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCefApp.cpp
More file actions
217 lines (181 loc) · 6.61 KB
/
Copy pathCefApp.cpp
File metadata and controls
217 lines (181 loc) · 6.61 KB
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
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "CefApp.h"
#include <string>
#include "include/cef_app.h"
#include "include/cef_browser.h"
#include "include/cef_path_util.h"
#include "browser_process_handler.h"
#include "client_handler.h"
#include "render_handler.h"
#include "scheme_handler_factory.h"
#include "jni_util.h"
#include "util.h"
#if defined(OS_LINUX)
#include <gdk/gdkx.h>
#endif
#if defined(OS_MACOSX)
#include "util_mac.h"
#endif
#if defined(OS_POSIX)
#include "signal_restore_posix.h"
#endif
namespace {
class ClientApp : public CefApp {
public:
explicit ClientApp(const std::string& module_dir,
const jobject app_handler)
: module_dir_(module_dir), app_handler_(NULL) {
JNIEnv *env = GetJNIEnv();
if (env)
app_handler_ = env->NewGlobalRef(app_handler);
process_handler_ = new BrowserProcessHandler(app_handler_);
}
virtual ~ClientApp() {
if (!app_handler_)
return;
BEGIN_ENV(env)
env->DeleteGlobalRef(app_handler_);
END_ENV(env)
}
virtual void OnBeforeCommandLineProcessing(
const CefString& process_type,
CefRefPtr<CefCommandLine> command_line) OVERRIDE {
// If the java code has registered an AppHandler, we'll forward
// the commandline processing to it before we append the essential
// switches "locale_pak" and "use-core-animation".
if (app_handler_ != NULL && process_type.empty()) {
BEGIN_ENV(env)
jstring jprocess_type = NewJNIString(env, process_type);
jobject jcommand_line = NewJNIObject(env, "org/cef/callback/CefCommandLine_N");
if (jcommand_line != NULL) {
SetCefForJNIObject(env, jcommand_line, command_line.get(), "CefCommandLine");
JNI_CALL_VOID_METHOD(env,
app_handler_,
"onBeforeCommandLineProcessing",
"(Ljava/lang/String;Lorg/cef/callback/CefCommandLine;)V",
jprocess_type,
jcommand_line);
SetCefForJNIObject<CefCommandLine>(env, jcommand_line, NULL, "CefCommandLine");
}
END_ENV(env)
}
if (process_type.empty()) {
#if defined(OS_MACOSX)
// Specify a path for the locale.pak file because CEF will fail to locate
// it based on the app bundle structure.
const std::string& locale_path = util_mac::GetAbsPath(
module_dir_ + "/../Frameworks/Chromium Embedded Framework.framework/"
"Resources/en.lproj/locale.pak");
command_line->AppendSwitchWithValue("locale_pak", locale_path);
// If windowed rendering is used, we need the browser window as CALayer
// due Java7 is CALayer based instead of NSLayer based.
command_line->AppendSwitch("use-core-animation");
#endif // defined(OS_MACOSX)
}
}
virtual void OnRegisterCustomSchemes(CefRefPtr<CefSchemeRegistrar> registrar) OVERRIDE {
if (!app_handler_)
return;
BEGIN_ENV(env)
jobject jregistrar = NewJNIObject(env, "org/cef/callback/CefSchemeRegistrar_N");
if (jregistrar != NULL) {
SetCefForJNIObject(env, jregistrar, registrar.get(), "CefSchemeRegistrar");
JNI_CALL_VOID_METHOD(env,
app_handler_,
"onRegisterCustomSchemes",
"(Lorg/cef/callback/CefSchemeRegistrar;)V",
jregistrar);
SetCefForJNIObject<CefSchemeRegistrar>(env, jregistrar, NULL, "CefSchemeRegistrar");
}
END_ENV(env)
}
virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() OVERRIDE {
return process_handler_.get();
}
private:
std::string module_dir_;
jobject app_handler_;
CefRefPtr<BrowserProcessHandler> process_handler_;
IMPLEMENT_REFCOUNTING(ClientApp);
};
std::string GetHelperPath(const std::string& module_dir) {
#if defined(OS_MACOSX)
return util_mac::GetAbsPath(
module_dir + "/../Frameworks/jcef Helper.app/Contents/MacOS/jcef Helper");
#elif defined(OS_WIN)
return module_dir + "\\jcef_helper.exe";
#elif defined(OS_LINUX)
return module_dir + "/jcef_helper";
#endif
}
} // namespace
JNIEXPORT jboolean JNICALL Java_org_cef_CefApp_N_1Initialize
(JNIEnv *env, jobject c, jstring argPathToJavaDLL, jobject appHandler) {
JavaVM* jvm;
jint rs = env->GetJavaVM(&jvm);
if (rs != JNI_OK) {
ASSERT(false); // Not reached.
return false;
}
SetJVM(jvm);
#if defined(OS_WIN)
CefMainArgs main_args(::GetModuleHandle(NULL));
#else
CefMainArgs main_args(0, NULL);
#endif
const std::string& module_dir = GetJNIString(env, argPathToJavaDLL);
CefSettings settings;
CefString(&settings.browser_subprocess_path) = GetHelperPath(module_dir);
settings.no_sandbox = true;
#if defined(OS_LINUX)
CefString(&settings.resources_dir_path) = module_dir;
CefString(&settings.locales_dir_path) = module_dir + "/locales";
#endif
CefRefPtr<ClientApp> client_app(new ClientApp(module_dir, appHandler));
bool res = false;
#if defined(OS_POSIX)
// CefInitialize will reset signal handlers. Backup/restore the original
// signal handlers to avoid crashes in the JVM (see issue #41).
BackupSignalHandlers();
#endif
#if defined(OS_MACOSX)
res = util_mac::CefInitializeOnMainThread(main_args, settings,
client_app.get());
#else
res = CefInitialize(main_args, settings, client_app.get(), NULL);
#endif
#if defined(OS_POSIX)
RestoreSignalHandlers();
#endif
return res ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT void JNICALL Java_org_cef_CefApp_N_1Shutdown
(JNIEnv *env, jobject) {
#if defined(OS_MACOSX)
util_mac::CefQuitMessageLoopOnMainThread();
#else
CefShutdown();
#endif
}
JNIEXPORT void JNICALL Java_org_cef_CefApp_N_1DoMessageLoopWork
(JNIEnv *env, jobject) {
CefDoMessageLoopWork();
}
JNIEXPORT jboolean JNICALL Java_org_cef_CefApp_N_1RegisterSchemeHandlerFactory
(JNIEnv *env, jobject, jstring jSchemeName, jstring jDomainName, jobject jFactory) {
if (!jFactory)
return JNI_FALSE;
CefRefPtr<SchemeHandlerFactory> factory = new SchemeHandlerFactory(env, jFactory);
if (!factory)
return JNI_FALSE;
bool result = CefRegisterSchemeHandlerFactory(GetJNIString(env, jSchemeName),
GetJNIString(env, jDomainName),
factory.get());
return result ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT jboolean JNICALL Java_org_cef_CefApp_N_1ClearSchemeHandlerFactories
(JNIEnv *, jobject) {
return CefClearSchemeHandlerFactories() ? JNI_TRUE : JNI_FALSE;
}