OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/android/context_utils.h" |
| 6 |
| 7 #include <jni.h> |
| 8 |
| 9 #include "base/lazy_instance.h" |
| 10 #include "jni/ContextUtils_jni.h" |
| 11 |
| 12 namespace base { |
| 13 namespace android { |
| 14 |
| 15 namespace { |
| 16 |
| 17 // Leak the global app context, as it is used from a non-joinable worker thread |
| 18 // that may still be running at shutdown. There is no harm in doing this. |
| 19 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >::Leaky |
| 20 g_application_context = LAZY_INSTANCE_INITIALIZER; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 const jobject GetApplicationContext() { |
| 25 DCHECK(!g_application_context.Get().is_null()); |
| 26 return g_application_context.Get().obj(); |
| 27 } |
| 28 |
| 29 static void InitApplicationContext(JNIEnv* env, |
| 30 const JavaParamRef<jclass>& clazz, |
| 31 const JavaParamRef<jobject>& context) { |
| 32 if (env->IsSameObject(g_application_context.Get().obj(), context.obj())) { |
| 33 // It's safe to set the context more than once if it's the same context. |
| 34 return; |
| 35 } |
| 36 DCHECK(g_application_context.Get().is_null()); |
| 37 g_application_context.Get().Reset(context); |
| 38 } |
| 39 |
| 40 bool RegisterContextUtils(JNIEnv* env) { |
| 41 return RegisterNativesImpl(env); |
| 42 } |
| 43 |
| 44 } // namespace android |
| 45 } // namespace base |
OLD | NEW |