Chromium Code Reviews

Unified Diff: components/memory_coordinator/android/memory_state_listener_android.cc

Issue 2259743002: Add MemoryStateListenerAndroid (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix dependency and update test Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
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..22aa59bb34b26b374c43f3995b6646d19a5c9214
--- /dev/null
+++ b/components/memory_coordinator/android/memory_state_listener_android.cc
@@ -0,0 +1,66 @@
+// 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) {
+ Java_MemoryStateListener_simulateOnTrimMemory(
+ base::android::AttachCurrentThread(),
+ base::android::GetApplicationContext(),
+ level);
+}
+
+} // namespace memory_coordinator

Powered by Google App Engine