diff options
author | torne <torne@chromium.org> | 2015-11-24 02:30:58 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-11-24 10:31:56 +0000 |
commit | 961a488f6a5d8c82b576444054f5d26b3f8a6877 (patch) | |
tree | 7aa3fdf6656de41b86f2a17bf3f58a276c2f2c54 /base/android/context_utils.cc | |
parent | 4bc70b49a4d47fa390b64c93a1dc1c5d99f14c0a (diff) | |
download | chromium_src-961a488f6a5d8c82b576444054f5d26b3f8a6877.zip chromium_src-961a488f6a5d8c82b576444054f5d26b3f8a6877.tar.gz chromium_src-961a488f6a5d8c82b576444054f5d26b3f8a6877.tar.bz2 |
Define a Java-side global application context.
Instead of each user of base setting the native-side global app context
separately, introduce a Java-side global app context, which is always
in sync with the native-side one. Switch most callers to setting
it on the Java side, except where this is problematic.
Callers of ApplicationStatus.getApplicationContext will be updated
incrementally in followup CLs once it's been verified that they only
require a Context and not a BaseChromiumApplication.
BUG=552419
Review URL: https://codereview.chromium.org/1407233017
Cr-Commit-Position: refs/heads/master@{#361306}
Diffstat (limited to 'base/android/context_utils.cc')
-rw-r--r-- | base/android/context_utils.cc | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/base/android/context_utils.cc b/base/android/context_utils.cc new file mode 100644 index 0000000..5e10f21 --- /dev/null +++ b/base/android/context_utils.cc @@ -0,0 +1,58 @@ +// Copyright 2015 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/context_utils.h" + +#include <jni.h> + +#include "base/android/scoped_java_ref.h" +#include "base/lazy_instance.h" +#include "jni/ContextUtils_jni.h" + +using base::android::JavaRef; + +namespace base { +namespace android { + +namespace { + +// Leak the global app context, as it is used from a non-joinable worker thread +// that may still be running at shutdown. There is no harm in doing this. +base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject>>::Leaky + g_application_context = LAZY_INSTANCE_INITIALIZER; + +void SetNativeApplicationContext(JNIEnv* env, const JavaRef<jobject>& context) { + if (env->IsSameObject(g_application_context.Get().obj(), context.obj())) { + // It's safe to set the context more than once if it's the same context. + return; + } + DCHECK(g_application_context.Get().is_null()); + g_application_context.Get().Reset(context); +} + +} // namespace + +const jobject GetApplicationContext() { + DCHECK(!g_application_context.Get().is_null()); + return g_application_context.Get().obj(); +} + +void InitApplicationContext(JNIEnv* env, const JavaRef<jobject>& context) { + SetNativeApplicationContext(env, context); + Java_ContextUtils_initJavaSideApplicationContext(env, context.obj()); +} + +static void InitNativeSideApplicationContext( + JNIEnv* env, + const JavaParamRef<jclass>& clazz, + const JavaParamRef<jobject>& context) { + SetNativeApplicationContext(env, context); +} + +bool RegisterContextUtils(JNIEnv* env) { + return RegisterNativesImpl(env); +} + +} // namespace android +} // namespace base |