1 /* 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 3 * 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 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "webrtc/modules/utility/interface/helpers_android.h" 12 13 #include <assert.h> 14 #include <stddef.h> 15 16 namespace webrtc { 17 18 AttachThreadScoped::AttachThreadScoped(JavaVM* jvm) 19 : attached_(false), jvm_(jvm), env_(NULL) { 20 jint ret_val = jvm->GetEnv(reinterpret_cast<void**>(&env_), JNI_VERSION_1_4); 21 if (ret_val == JNI_EDETACHED) { 22 // Attach the thread to the Java VM. 23 ret_val = jvm_->AttachCurrentThread(&env_, NULL); 24 attached_ = ret_val == JNI_OK; 25 assert(attached_); 26 } 27 } 28 29 AttachThreadScoped::~AttachThreadScoped() { 30 if (attached_ && (jvm_->DetachCurrentThread() < 0)) { 31 assert(false); 32 } 33 } 34 35 JNIEnv* AttachThreadScoped::env() { return env_; } 36 37 } // namespace webrtc 38