forked from chromiumembedded/java-cef
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.cpp
More file actions
195 lines (157 loc) · 5.4 KB
/
Copy pathcontext.cpp
File metadata and controls
195 lines (157 loc) · 5.4 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
// Copyright (c) 2016 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 "context.h"
#include "include/cef_app.h"
#include "client_app.h"
#include "jni_util.h"
#if defined(OS_MACOSX)
#include "util_mac.h"
#endif
#if defined(OS_POSIX)
#include "signal_restore_posix.h"
#endif
namespace {
Context* g_context = NULL;
} // namespace
// static
void Context::Create() {
new Context();
}
// static
void Context::Destroy() {
DCHECK(g_context);
if (g_context)
delete g_context;
}
// static
Context* Context::GetInstance() {
return g_context;
}
bool Context::PreInitialize(JNIEnv* env, jobject c) {
DCHECK(thread_checker_.CalledOnValidThread());
JavaVM* jvm;
jint rs = env->GetJavaVM(&jvm);
DCHECK_EQ(rs, JNI_OK);
if (rs != JNI_OK)
return JNI_FALSE;
SetJVM(jvm);
jobject javaClass = env->GetObjectClass(c);
jobject javaClassLoader = NULL;
JNI_CALL_METHOD(env, javaClass, "getClassLoader", "()Ljava/lang/ClassLoader;",
Object, javaClassLoader);
env->DeleteLocalRef(javaClass);
ASSERT(javaClassLoader);
if (!javaClassLoader)
return false;
SetJavaClassLoader(env, javaClassLoader);
return true;
}
bool Context::Initialize(JNIEnv* env,
jobject c,
jstring argPathToJavaDLL,
jobject appHandler,
jobject jsettings) {
DCHECK(thread_checker_.CalledOnValidThread());
#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 = GetJNISettings(env, jsettings);
// Sandbox is not supported because:
// - Use of a separate sub-process executable on Windows.
// - Use of a temporary file to communicate custom schemes to the
// renderer process.
settings.no_sandbox = true;
#if defined(OS_WIN) || defined(OS_LINUX)
// Use external message pump with OSR.
external_message_pump_ = !!settings.windowless_rendering_enabled;
if (!external_message_pump_) {
// Windowed rendering on Windows requires multi-threaded message loop,
// otherwise something eats the messages required by Java and the Java
// window becomes unresponsive.
//
// Actually the same appears to be true for Linux, which is why we also
// need multithreaded message loops there. Note that however, on Linux
// it is more difficult to get this to work: it is necessary for the first
// call to Xlib to be a call to XInitThreads! Since Java itself calls
// Xlib when it initializes the first window, an application must make
// sure to invoke this method before any other Xlib functions are called
// - including by the Java runtime itself, which makes this feat a little
// tricky. The CefApp class exposes a static method for this purpose,
// initXlibForMultithreading(), but the host application must load the
// jcef native lib by itself in order to use it, and it must invoke it
// VERY early, ideally at the beginning of the main method.
// Another neat trick to get this done is to create a special native lib
// just for this purpose like described in this StackOverflow thread:
// https://stackoverflow.com/questions/24559368
settings.multi_threaded_message_loop = true;
}
#endif
// Use CefAppHandler.onScheduleMessagePumpWork to schedule calls to
// DoMessageLoopWork.
settings.external_message_pump = external_message_pump_;
CefRefPtr<ClientApp> client_app(
new ClientApp(module_dir, CefString(&settings.cache_path), 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;
}
void Context::OnContextInitialized() {
REQUIRE_UI_THREAD();
temp_window_.reset(new TempWindow());
}
void Context::DoMessageLoopWork() {
DCHECK(thread_checker_.CalledOnValidThread());
#if defined(OS_MACOSX)
util_mac::CefDoMessageLoopWorkOnMainThread();
#else
CefDoMessageLoopWork();
#endif
}
void Context::Shutdown() {
DCHECK(thread_checker_.CalledOnValidThread());
// Clear scheme handler factories on shutdown to avoid refcount DCHECK.
CefClearSchemeHandlerFactories();
ClientApp::eraseTempFiles();
#if defined(OS_MACOSX)
util_mac::CefShutdownOnMainThread();
#else
// Pump CefDoMessageLoopWork a few times before shutting down.
if (external_message_pump_) {
for (int i = 0; i < 10; ++i)
CefDoMessageLoopWork();
}
temp_window_.reset(nullptr);
CefShutdown();
#endif
}
Context::Context() : external_message_pump_(true) {
DCHECK(!g_context);
g_context = this;
#if defined(OS_MACOSX)
// On macOS we create this object very early to allow LibraryLoader
// assignment. However, we still want the PreInitialize() call to determine
// thread ownership.
thread_checker_.DetachFromThread();
#endif
}
Context::~Context() {
DCHECK(thread_checker_.CalledOnValidThread());
g_context = NULL;
}