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

Side by Side Diff: content/browser/memory/memory_monitor_android.cc

Issue 2340293003: Add MemoryMonitorAndroid (Closed)
Patch Set: DCHECK Created 4 years, 2 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/memory/memory_monitor_android.h"
6
7 #include "base/android/context_utils.h"
8 #include "base/android/jni_android.h"
9 #include "base/memory/ptr_util.h"
10 #include "jni/MemoryMonitorAndroid_jni.h"
11
12 namespace content {
13
14 namespace {
15 const size_t kMBShift = 20;
16 }
17
18 // static
19 std::unique_ptr<MemoryMonitorAndroid> MemoryMonitorAndroid::Create() {
20 return base::WrapUnique(new MemoryMonitorAndroid);
21 }
22
23 MemoryMonitorAndroid::MemoryMonitorAndroid() {
24 InitializeFieldIDs();
25 }
26
27 MemoryMonitorAndroid::~MemoryMonitorAndroid() {}
28
29 int MemoryMonitorAndroid::GetFreeMemoryUntilCriticalMB() {
30 DCHECK(CanGetMemoryInfo());
31 MemoryInfo info;
32 GetMemoryInfo(&info);
33 return (info.avail_mem - info.threshold) >> kMBShift;
34 }
35
36 bool MemoryMonitorAndroid::CanGetMemoryInfo() {
37 return (avail_mem_id_ != 0 && low_memory_id_ != 0 && threshold_id_ != 0 &&
38 total_mem_id_ != 0);
39 }
40
41 void MemoryMonitorAndroid::GetMemoryInfo(MemoryInfo* out) {
42 DCHECK(out);
43 DCHECK(CanGetMemoryInfo());
44 JNIEnv* env = base::android::AttachCurrentThread();
45 base::android::ScopedJavaLocalRef<jobject> info =
46 Java_MemoryMonitorAndroid_getMemoryInfo(
47 env, base::android::GetApplicationContext());
48 out->avail_mem = env->GetLongField(info.obj(), avail_mem_id_);
49 out->low_memory = env->GetBooleanField(info.obj(), low_memory_id_);
50 out->threshold = env->GetLongField(info.obj(), threshold_id_);
51 out->total_mem = env->GetLongField(info.obj(), total_mem_id_);
52 }
53
54 void MemoryMonitorAndroid::InitializeFieldIDs() {
55 JNIEnv* env = base::android::AttachCurrentThread();
56 base::android::ScopedJavaLocalRef<jclass> clazz =
57 base::android::GetClass(env, "android/app/ActivityManager$MemoryInfo");
58 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
59 low_memory_id_ = env->GetFieldID(clazz.obj(), "lowMemory", "Z");
60 threshold_id_ = env->GetFieldID(clazz.obj(), "threshold", "J");
61 total_mem_id_ = env->GetFieldID(clazz.obj(), "totalMem", "J");
62 }
63
64 // Implementation of a factory function defined in memory_monitor.h.
65 std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() {
66 return MemoryMonitorAndroid::Create();
67 }
68
69 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698