Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Unified Diff: content/browser/memory/memory_monitor_android.cc

Issue 2340293003: Add MemoryMonitorAndroid (Closed)
Patch Set: Use native callback instead of returning MemoryInfo Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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
index 661aad51941639c33494d9a36476ef607340d962..bbe3352176616d1c22ee170175a6b4a6484083f4 100644
--- a/content/browser/memory/memory_monitor_android.cc
+++ b/content/browser/memory/memory_monitor_android.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2016 The Chromium Authors. All rights reserved.
+// 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.
@@ -20,45 +20,45 @@ std::unique_ptr<MemoryMonitorAndroid> MemoryMonitorAndroid::Create() {
return base::WrapUnique(new MemoryMonitorAndroid);
}
-MemoryMonitorAndroid::MemoryMonitorAndroid() {
- InitializeFieldIDs();
+// static
+bool MemoryMonitorAndroid::Register(JNIEnv* env) {
+ return RegisterNativesImpl(env);
}
+MemoryMonitorAndroid::MemoryMonitorAndroid() {}
+
MemoryMonitorAndroid::~MemoryMonitorAndroid() {}
int MemoryMonitorAndroid::GetFreeMemoryUntilCriticalMB() {
- DCHECK(CanGetMemoryInfo());
MemoryInfo info;
GetMemoryInfo(&info);
return (info.avail_mem - info.threshold) >> kMBShift;
bashi 2016/09/27 03:51:36 agrieve@ gave me great advice not to call GetMemor
}
-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_);
+ Java_MemoryMonitorAndroid_getMemoryInfo(
+ env, base::android::GetApplicationContext(),
+ reinterpret_cast<intptr_t>(out));
}
-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");
+// Called by JNI to populate ActivityManager.MemoryInfo.
+static void GetMemoryInfoCallback(
+ JNIEnv* env,
+ const base::android::JavaParamRef<jclass>& clazz,
+ jlong avail_mem,
+ jboolean low_memory,
+ jlong threshold,
+ jlong total_mem,
+ jlong out_ptr) {
+ DCHECK(out_ptr);
+ MemoryMonitorAndroid::MemoryInfo* info =
+ reinterpret_cast<MemoryMonitorAndroid::MemoryInfo*>(out_ptr);
+ info->avail_mem = avail_mem;
+ info->low_memory = low_memory;
+ info->threshold = threshold;
+ info->total_mem = total_mem;
}
// Implementation of a factory function defined in memory_monitor.h.

Powered by Google App Engine
This is Rietveld 408576698