OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 CHECK_EXCEPTION(jni_); | 210 CHECK_EXCEPTION(jni_); |
211 const int size = jni_->GetStringUTFLength(j_string); | 211 const int size = jni_->GetStringUTFLength(j_string); |
212 CHECK_EXCEPTION(jni_); | 212 CHECK_EXCEPTION(jni_); |
213 std::string ret(jchars, size); | 213 std::string ret(jchars, size); |
214 jni_->ReleaseStringUTFChars(j_string, jchars); | 214 jni_->ReleaseStringUTFChars(j_string, jchars); |
215 CHECK_EXCEPTION(jni_); | 215 CHECK_EXCEPTION(jni_); |
216 return ret; | 216 return ret; |
217 } | 217 } |
218 | 218 |
219 // static | 219 // static |
220 void JVM::Initialize(JavaVM* jvm, jobject context) { | 220 void JVM::Initialize(JavaVM* jvm) { |
221 ALOGD("JVM::Initialize%s", GetThreadInfo().c_str()); | 221 ALOGD("JVM::Initialize%s", GetThreadInfo().c_str()); |
222 RTC_CHECK(!g_jvm); | 222 RTC_CHECK(!g_jvm); |
223 g_jvm = new JVM(jvm, context); | 223 g_jvm = new JVM(jvm); |
224 } | 224 } |
225 | 225 |
226 // static | 226 // static |
227 void JVM::Uninitialize() { | 227 void JVM::Uninitialize() { |
228 ALOGD("JVM::Uninitialize%s", GetThreadInfo().c_str()); | 228 ALOGD("JVM::Uninitialize%s", GetThreadInfo().c_str()); |
229 RTC_DCHECK(g_jvm); | 229 RTC_DCHECK(g_jvm); |
230 delete g_jvm; | 230 delete g_jvm; |
231 g_jvm = nullptr; | 231 g_jvm = nullptr; |
232 } | 232 } |
233 | 233 |
234 // static | 234 // static |
235 JVM* JVM::GetInstance() { | 235 JVM* JVM::GetInstance() { |
236 RTC_DCHECK(g_jvm); | 236 RTC_DCHECK(g_jvm); |
237 return g_jvm; | 237 return g_jvm; |
238 } | 238 } |
239 | 239 |
240 JVM::JVM(JavaVM* jvm, jobject context) | 240 JVM::JVM(JavaVM* jvm) : jvm_(jvm) { |
241 : jvm_(jvm) { | |
242 ALOGD("JVM::JVM%s", GetThreadInfo().c_str()); | 241 ALOGD("JVM::JVM%s", GetThreadInfo().c_str()); |
243 RTC_CHECK(jni()) << "AttachCurrentThread() must be called on this thread."; | 242 RTC_CHECK(jni()) << "AttachCurrentThread() must be called on this thread."; |
244 context_ = NewGlobalRef(jni(), context); | |
245 LoadClasses(jni()); | 243 LoadClasses(jni()); |
246 } | 244 } |
247 | 245 |
248 JVM::~JVM() { | 246 JVM::~JVM() { |
249 ALOGD("JVM::~JVM%s", GetThreadInfo().c_str()); | 247 ALOGD("JVM::~JVM%s", GetThreadInfo().c_str()); |
250 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 248 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
251 FreeClassReferences(jni()); | 249 FreeClassReferences(jni()); |
252 DeleteGlobalRef(jni(), context_); | |
253 } | 250 } |
254 | 251 |
255 std::unique_ptr<JNIEnvironment> JVM::environment() { | 252 std::unique_ptr<JNIEnvironment> JVM::environment() { |
256 ALOGD("JVM::environment%s", GetThreadInfo().c_str()); | 253 ALOGD("JVM::environment%s", GetThreadInfo().c_str()); |
257 // The JNIEnv is used for thread-local storage. For this reason, we cannot | 254 // The JNIEnv is used for thread-local storage. For this reason, we cannot |
258 // share a JNIEnv between threads. If a piece of code has no other way to get | 255 // share a JNIEnv between threads. If a piece of code has no other way to get |
259 // its JNIEnv, we should share the JavaVM, and use GetEnv to discover the | 256 // its JNIEnv, we should share the JavaVM, and use GetEnv to discover the |
260 // thread's JNIEnv. (Assuming it has one, if not, use AttachCurrentThread). | 257 // thread's JNIEnv. (Assuming it has one, if not, use AttachCurrentThread). |
261 // See // http://developer.android.com/training/articles/perf-jni.html. | 258 // See // http://developer.android.com/training/articles/perf-jni.html. |
262 JNIEnv* jni = GetEnv(jvm_); | 259 JNIEnv* jni = GetEnv(jvm_); |
263 if (!jni) { | 260 if (!jni) { |
264 ALOGE("AttachCurrentThread() has not been called on this thread."); | 261 ALOGE("AttachCurrentThread() has not been called on this thread."); |
265 return std::unique_ptr<JNIEnvironment>(); | 262 return std::unique_ptr<JNIEnvironment>(); |
266 } | 263 } |
267 return std::unique_ptr<JNIEnvironment>(new JNIEnvironment(jni)); | 264 return std::unique_ptr<JNIEnvironment>(new JNIEnvironment(jni)); |
268 } | 265 } |
269 | 266 |
270 JavaClass JVM::GetClass(const char* name) { | 267 JavaClass JVM::GetClass(const char* name) { |
271 ALOGD("JVM::GetClass(%s)%s", name, GetThreadInfo().c_str()); | 268 ALOGD("JVM::GetClass(%s)%s", name, GetThreadInfo().c_str()); |
272 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 269 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
273 return JavaClass(jni(), LookUpClass(name)); | 270 return JavaClass(jni(), LookUpClass(name)); |
274 } | 271 } |
275 | 272 |
276 } // namespace webrtc | 273 } // namespace webrtc |
OLD | NEW |