Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Unified Diff: base/android/jni_android.cc

Issue 7518032: Android's paths and message loop implementation with JNI (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix dependence again. Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/android/jni_android.cc
diff --git a/base/android/jni_android.cc b/base/android/jni_android.cc
new file mode 100644
index 0000000000000000000000000000000000000000..05df3485f2b2d83af1fd24320fab3477d14b4255
--- /dev/null
+++ b/base/android/jni_android.cc
@@ -0,0 +1,69 @@
+// Copyright (c) 2011 The Chromium 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 "base/android/jni_android.h"
+
+#include "base/android/auto_jobject.h"
+#include "base/logging.h"
+
+namespace {
+JavaVM* g_jvm = 0;
+jobject g_application_context = NULL;
+}
+
+namespace base {
+namespace android {
+
+JNIEnv* AttachCurrentThread() {
+ if (!g_jvm)
M-A Ruel 2011/11/11 13:09:07 Having this function silently fail is a recipe for
+ return NULL;
+
+ JNIEnv* env = NULL;
+ jint ret = g_jvm->AttachCurrentThread(&env, NULL);
+ DCHECK_EQ(ret, JNI_OK);
+ return env;
+}
+
+void DetachFromVM() {
+ // Ignore the return value, if the thread is not attached, DetachCurrentThread
+ // will fail. But it is ok as the native thread may never be attached.
+ if (g_jvm)
+ g_jvm->DetachCurrentThread();
+}
+
+void InitVM(JavaVM* vm) {
+ DCHECK(!g_jvm);
+ g_jvm = vm;
+}
+
+void InitApplicationContext(jobject context) {
+ DCHECK(!g_application_context);
+ g_application_context = context;
+}
+
+jobject GetApplicationContext() {
+ DCHECK(g_application_context);
+ return g_application_context;
+}
+
+jmethodID GetMethodID(JNIEnv* env,
+ jclass clazz,
+ const char* const method,
+ const char* const jni_signature) {
+ jmethodID id = env->GetMethodID(clazz, method, jni_signature);
+ DCHECK(id) << method;
+ CheckException(env);
+ return id;
+}
+
+bool CheckException(JNIEnv* env) {
+ if (env->ExceptionCheck() == JNI_FALSE)
+ return false;
+ env->ExceptionDescribe();
+ env->ExceptionClear();
+ return true;
+}
+
+} // namespace android
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698