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
166 lines (131 loc) · 4 KB
/
Copy pathcontext.cpp
File metadata and controls
166 lines (131 loc) · 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
// 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)
// 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.
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::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();
}
CefShutdown();
#endif
}
void Context::DoMessageLoopWork() {
DCHECK(thread_checker_.CalledOnValidThread());
#if defined(OS_MACOSX)
util_mac::CefDoMessageLoopWorkOnMainThread();
#else
CefDoMessageLoopWork();
#endif
}
Context::Context() : external_message_pump_(true) {
DCHECK(!g_context);
g_context = this;
}
Context::~Context() {
DCHECK(thread_checker_.CalledOnValidThread());
g_context = NULL;
}