Index: content/browser/memory/memory_monitor_android.cc |
diff --git a/content/browser/memory/memory_monitor_android.cc b/content/browser/memory/memory_monitor_android.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..661aad51941639c33494d9a36476ef607340d962 |
--- /dev/null |
+++ b/content/browser/memory/memory_monitor_android.cc |
@@ -0,0 +1,69 @@ |
+// Copyright (c) 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 "content/browser/memory/memory_monitor_android.h" |
+ |
+#include "base/android/context_utils.h" |
+#include "base/android/jni_android.h" |
+#include "base/memory/ptr_util.h" |
+#include "jni/MemoryMonitorAndroid_jni.h" |
+ |
+namespace content { |
+ |
+namespace { |
+const size_t kMBShift = 20; |
+} |
+ |
+// static |
+std::unique_ptr<MemoryMonitorAndroid> MemoryMonitorAndroid::Create() { |
+ return base::WrapUnique(new MemoryMonitorAndroid); |
+} |
+ |
+MemoryMonitorAndroid::MemoryMonitorAndroid() { |
+ InitializeFieldIDs(); |
+} |
+ |
+MemoryMonitorAndroid::~MemoryMonitorAndroid() {} |
+ |
+int MemoryMonitorAndroid::GetFreeMemoryUntilCriticalMB() { |
+ DCHECK(CanGetMemoryInfo()); |
+ MemoryInfo info; |
+ GetMemoryInfo(&info); |
+ return (info.avail_mem - info.threshold) >> kMBShift; |
+} |
+ |
+bool MemoryMonitorAndroid::CanGetMemoryInfo() { |
+ return (avail_mem_id_ != 0 && low_memory_id_ != 0 && threshold_id_ != 0 && |
+ total_mem_id_ != 0); |
+} |
+ |
+void MemoryMonitorAndroid::GetMemoryInfo(MemoryInfo* out) { |
+ DCHECK(out); |
+ DCHECK(CanGetMemoryInfo()); |
+ JNIEnv* env = base::android::AttachCurrentThread(); |
+ base::android::ScopedJavaLocalRef<jobject> info = |
+ Java_MemoryMonitorAndroid_getMemoryInfo( |
+ env, base::android::GetApplicationContext()); |
+ out->avail_mem = env->GetLongField(info.obj(), avail_mem_id_); |
+ out->low_memory = env->GetBooleanField(info.obj(), low_memory_id_); |
+ out->threshold = env->GetLongField(info.obj(), threshold_id_); |
+ out->total_mem = env->GetLongField(info.obj(), total_mem_id_); |
+} |
+ |
+void MemoryMonitorAndroid::InitializeFieldIDs() { |
+ JNIEnv* env = base::android::AttachCurrentThread(); |
+ base::android::ScopedJavaLocalRef<jclass> clazz = |
+ base::android::GetClass(env, "android/app/ActivityManager$MemoryInfo"); |
+ avail_mem_id_ = env->GetFieldID(clazz.obj(), "availMem", "J"); |
boliu
2016/09/27 00:19:11
we go to great lengths to not hand write jni code.
agrieve
2016/09/27 00:35:05
You can generate wrappers for sdk classes, but I d
boliu
2016/09/27 03:00:23
Yeah, ok. There really is nothing great here :/
C
bashi
2016/09/27 03:51:36
Thank you for suggestions. PS#4 uses native callba
|
+ low_memory_id_ = env->GetFieldID(clazz.obj(), "lowMemory", "Z"); |
+ threshold_id_ = env->GetFieldID(clazz.obj(), "threshold", "J"); |
+ total_mem_id_ = env->GetFieldID(clazz.obj(), "totalMem", "J"); |
+} |
+ |
+// Implementation of a factory function defined in memory_monitor.h. |
+std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { |
+ return MemoryMonitorAndroid::Create(); |
+} |
+ |
+} // namespace content |