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 } |
| 225 |
| 226 void JVM::Initialize(JavaVM* jvm, jobject context) { |
| 227 Initialize(jvm); |
| 228 |
| 229 // Pass in the context to the new ContextUtils class. |
| 230 JNIEnv* jni = g_jvm->jni(); |
| 231 jclass context_utils = FindClass(jni, "org/webrtc/ContextUtils"); |
| 232 jmethodID initialize_method = jni->GetStaticMethodID( |
| 233 context_utils, "initialize", "(Landroid/content/Context;)V"); |
| 234 jni->CallStaticVoidMethod(context_utils, initialize_method, context); |
224 } | 235 } |
225 | 236 |
226 // static | 237 // static |
227 void JVM::Uninitialize() { | 238 void JVM::Uninitialize() { |
228 ALOGD("JVM::Uninitialize%s", GetThreadInfo().c_str()); | 239 ALOGD("JVM::Uninitialize%s", GetThreadInfo().c_str()); |
229 RTC_DCHECK(g_jvm); | 240 RTC_DCHECK(g_jvm); |
230 delete g_jvm; | 241 delete g_jvm; |
231 g_jvm = nullptr; | 242 g_jvm = nullptr; |
232 } | 243 } |
233 | 244 |
234 // static | 245 // static |
235 JVM* JVM::GetInstance() { | 246 JVM* JVM::GetInstance() { |
236 RTC_DCHECK(g_jvm); | 247 RTC_DCHECK(g_jvm); |
237 return g_jvm; | 248 return g_jvm; |
238 } | 249 } |
239 | 250 |
240 JVM::JVM(JavaVM* jvm, jobject context) | 251 JVM::JVM(JavaVM* jvm) : jvm_(jvm) { |
241 : jvm_(jvm) { | |
242 ALOGD("JVM::JVM%s", GetThreadInfo().c_str()); | 252 ALOGD("JVM::JVM%s", GetThreadInfo().c_str()); |
243 RTC_CHECK(jni()) << "AttachCurrentThread() must be called on this thread."; | 253 RTC_CHECK(jni()) << "AttachCurrentThread() must be called on this thread."; |
244 context_ = NewGlobalRef(jni(), context); | |
245 LoadClasses(jni()); | 254 LoadClasses(jni()); |
246 } | 255 } |
247 | 256 |
248 JVM::~JVM() { | 257 JVM::~JVM() { |
249 ALOGD("JVM::~JVM%s", GetThreadInfo().c_str()); | 258 ALOGD("JVM::~JVM%s", GetThreadInfo().c_str()); |
250 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 259 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
251 FreeClassReferences(jni()); | 260 FreeClassReferences(jni()); |
252 DeleteGlobalRef(jni(), context_); | |
253 } | 261 } |
254 | 262 |
255 std::unique_ptr<JNIEnvironment> JVM::environment() { | 263 std::unique_ptr<JNIEnvironment> JVM::environment() { |
256 ALOGD("JVM::environment%s", GetThreadInfo().c_str()); | 264 ALOGD("JVM::environment%s", GetThreadInfo().c_str()); |
257 // The JNIEnv is used for thread-local storage. For this reason, we cannot | 265 // 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 | 266 // 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 | 267 // 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). | 268 // thread's JNIEnv. (Assuming it has one, if not, use AttachCurrentThread). |
261 // See // http://developer.android.com/training/articles/perf-jni.html. | 269 // See // http://developer.android.com/training/articles/perf-jni.html. |
262 JNIEnv* jni = GetEnv(jvm_); | 270 JNIEnv* jni = GetEnv(jvm_); |
263 if (!jni) { | 271 if (!jni) { |
264 ALOGE("AttachCurrentThread() has not been called on this thread."); | 272 ALOGE("AttachCurrentThread() has not been called on this thread."); |
265 return std::unique_ptr<JNIEnvironment>(); | 273 return std::unique_ptr<JNIEnvironment>(); |
266 } | 274 } |
267 return std::unique_ptr<JNIEnvironment>(new JNIEnvironment(jni)); | 275 return std::unique_ptr<JNIEnvironment>(new JNIEnvironment(jni)); |
268 } | 276 } |
269 | 277 |
270 JavaClass JVM::GetClass(const char* name) { | 278 JavaClass JVM::GetClass(const char* name) { |
271 ALOGD("JVM::GetClass(%s)%s", name, GetThreadInfo().c_str()); | 279 ALOGD("JVM::GetClass(%s)%s", name, GetThreadInfo().c_str()); |
272 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 280 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
273 return JavaClass(jni(), LookUpClass(name)); | 281 return JavaClass(jni(), LookUpClass(name)); |
274 } | 282 } |
275 | 283 |
276 } // namespace webrtc | 284 } // namespace webrtc |
OLD | NEW |