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..23159fb82acc945626104754ab02f1a5fe4fda49 |
--- /dev/null |
+++ b/content/browser/memory/memory_monitor_android.cc |
@@ -0,0 +1,72 @@ |
+// 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() { |
+ if (!CanGetMemoryInfo()) { |
+ LOG(ERROR) << "Could not get memory info"; |
+ return 0; |
+ } |
+ 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"); |
+ 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 factory function defined in memory_monitor.h. |
haraken
2016/09/16 06:49:26
a factory function
bashi
2016/09/20 02:57:01
Done.
|
+std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { |
+ return MemoryMonitorAndroid::Create(); |
+} |
+ |
+} // namespace content |