| Index: components/memory_coordinator/android/memory_state_listener_android.cc
|
| diff --git a/components/memory_coordinator/android/memory_state_listener_android.cc b/components/memory_coordinator/android/memory_state_listener_android.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..0ea15b06aaacfdd69dfe360fc6c7bf32c052cdd3
|
| --- /dev/null
|
| +++ b/components/memory_coordinator/android/memory_state_listener_android.cc
|
| @@ -0,0 +1,71 @@
|
| +// Copyright 2016 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 "components/memory_coordinator/android/memory_state_listener_android.h"
|
| +
|
| +#include "base/android/context_utils.h"
|
| +#include "base/android/jni_android.h"
|
| +#include "base/lazy_instance.h"
|
| +#include "base/synchronization/lock.h"
|
| +#include "jni/MemoryStateListener_jni.h"
|
| +
|
| +namespace memory_coordinator {
|
| +
|
| +namespace {
|
| +
|
| +base::LazyInstance<base::Lock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER;
|
| +MemoryStateListenerAndroidDelegate* g_delegate = nullptr;
|
| +
|
| +} // namespace
|
| +
|
| +// Declared by jni_generator and called by JNI.
|
| +static void OnTrimMemory(JNIEnv* env,
|
| + const base::android::JavaParamRef<jclass>& jcaller,
|
| + jint level) {
|
| + base::AutoLock lock(*g_lock.Pointer());
|
| + if (g_delegate) {
|
| + g_delegate->OnTrimMemory(level);
|
| + }
|
| +}
|
| +
|
| +// static
|
| +bool MemoryStateListenerAndroid::Register(JNIEnv* env) {
|
| + return RegisterNativesImpl(env);
|
| +}
|
| +
|
| +// static
|
| +void MemoryStateListenerAndroid::Start(
|
| + MemoryStateListenerAndroidDelegate* delegate) {
|
| + base::AutoLock lock(*g_lock.Pointer());
|
| + DCHECK(!g_delegate);
|
| + g_delegate = delegate;
|
| + Java_MemoryStateListener_registerCallback(
|
| + base::android::AttachCurrentThread(),
|
| + base::android::GetApplicationContext());
|
| +}
|
| +
|
| +// static
|
| +void MemoryStateListenerAndroid::Stop() {
|
| + base::AutoLock lock(*g_lock.Pointer());
|
| + DCHECK(g_delegate);
|
| + Java_MemoryStateListener_unregisterCallback(
|
| + base::android::AttachCurrentThread(),
|
| + base::android::GetApplicationContext());
|
| + g_delegate = nullptr;
|
| +}
|
| +
|
| +// static
|
| +void MemoryStateListenerAndroid::SimulateOnTrimMemory(int level) {
|
| + JNIEnv* env = base::android::AttachCurrentThread();
|
| + base::android::ScopedJavaLocalRef<jclass> application_clazz =
|
| + base::android::GetClass(env, "android/app/Application");
|
| + jmethodID method =
|
| + base::android::MethodID::Get<base::android::MethodID::TYPE_INSTANCE>(
|
| + env, application_clazz.obj(), "onTrimMemory", "(I)V");
|
| + const base::android::JavaRef<jobject>& context =
|
| + base::android::GetApplicationContext();
|
| + env->CallVoidMethod(context.obj(), method, level);
|
| +}
|
| +
|
| +} // namespace memory_coordinator
|
|
|