Clean up base::android::Init/GetApplicationContext()

- Modify InitApplicationContext() to take a ScopedJavaRef
- Clarify documentation


Review URL: http://codereview.chromium.org/8894002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@114010 0039d316-1c4b-4281-b951-d872f2087c98
parent a5437283
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include <map> #include <map>
#include "base/android/scoped_java_ref.h"
#include "base/atomicops.h" #include "base/atomicops.h"
#include "base/lazy_instance.h" #include "base/lazy_instance.h"
#include "base/logging.h" #include "base/logging.h"
...@@ -55,7 +54,6 @@ namespace android { ...@@ -55,7 +54,6 @@ namespace android {
JNIEnv* AttachCurrentThread() { JNIEnv* AttachCurrentThread() {
if (!g_jvm) if (!g_jvm)
return NULL; return NULL;
JNIEnv* env = NULL; JNIEnv* env = NULL;
jint ret = g_jvm->AttachCurrentThread(&env, NULL); jint ret = g_jvm->AttachCurrentThread(&env, NULL);
DCHECK_EQ(ret, JNI_OK); DCHECK_EQ(ret, JNI_OK);
...@@ -74,9 +72,9 @@ void InitVM(JavaVM* vm) { ...@@ -74,9 +72,9 @@ void InitVM(JavaVM* vm) {
g_jvm = vm; g_jvm = vm;
} }
void InitApplicationContext(jobject context) { void InitApplicationContext(const JavaRef<jobject>& context) {
DCHECK(!g_application_context); DCHECK(!g_application_context);
g_application_context = context; g_application_context = context.env()->NewGlobalRef(context.obj());
} }
jobject GetApplicationContext() { jobject GetApplicationContext() {
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include <jni.h> #include <jni.h>
#include <sys/types.h> #include <sys/types.h>
#include "base/android/scoped_java_ref.h"
namespace base { namespace base {
namespace android { namespace android {
...@@ -21,13 +23,15 @@ void DetachFromVM(); ...@@ -21,13 +23,15 @@ void DetachFromVM();
// InitApplicationContext(). // InitApplicationContext().
void InitVM(JavaVM* vm); void InitVM(JavaVM* vm);
// Initializes the global application context object. The |context| should be // Initializes the global application context object. The |context| can be any
// the global reference of application context object. It is not necessarily // valid reference to the application context. Internally holds a global ref to
// called after InitVM(). // the context. InitVM and InitApplicationContext maybe called in either order.
// TODO: We might combine InitVM() and InitApplicationContext() into one method. // TODO: We might combine InitVM() and InitApplicationContext() into one method.
void InitApplicationContext(jobject context); void InitApplicationContext(const JavaRef<jobject>& context);
// Returns the application context assigned by InitApplicationContext(). // Gets a global ref to the application context set with
// InitApplicationContext(). Ownership is retained by the function - the caller
// must NOT release it.
jobject GetApplicationContext(); jobject GetApplicationContext();
// Gets the method ID from the class name. Clears the pending Java exception // Gets the method ID from the class name. Clears the pending Java exception
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment