OLD | NEW |
---|---|
(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 if (!CanGetMemoryInfo()) { | |
31 LOG(ERROR) << "Could not get memory info"; | |
32 return 0; | |
33 } | |
34 MemoryInfo info; | |
35 GetMemoryInfo(&info); | |
36 return (info.avail_mem - info.threshold) >> kMBShift; | |
37 } | |
38 | |
39 bool MemoryMonitorAndroid::CanGetMemoryInfo() { | |
40 return (avail_mem_id_ != 0 && low_memory_id_ != 0 && threshold_id_ != 0 && | |
41 total_mem_id_ != 0); | |
42 } | |
43 | |
44 void MemoryMonitorAndroid::GetMemoryInfo(MemoryInfo* out) { | |
45 DCHECK(out); | |
46 DCHECK(CanGetMemoryInfo()); | |
47 JNIEnv* env = base::android::AttachCurrentThread(); | |
48 base::android::ScopedJavaLocalRef<jobject> info = | |
49 Java_MemoryMonitorAndroid_getMemoryInfo( | |
50 env, base::android::GetApplicationContext()); | |
51 out->avail_mem = env->GetLongField(info.obj(), avail_mem_id_); | |
52 out->low_memory = env->GetBooleanField(info.obj(), low_memory_id_); | |
53 out->threshold = env->GetLongField(info.obj(), threshold_id_); | |
54 out->total_mem = env->GetLongField(info.obj(), total_mem_id_); | |
55 } | |
56 | |
57 void MemoryMonitorAndroid::InitializeFieldIDs() { | |
58 JNIEnv* env = base::android::AttachCurrentThread(); | |
59 base::android::ScopedJavaLocalRef<jclass> clazz = | |
60 base::android::GetClass(env, "android/app/ActivityManager$MemoryInfo"); | |
61 avail_mem_id_ = env->GetFieldID(clazz.obj(), "availMem", "J"); | |
62 low_memory_id_ = env->GetFieldID(clazz.obj(), "lowMemory", "Z"); | |
63 threshold_id_ = env->GetFieldID(clazz.obj(), "threshold", "J"); | |
64 total_mem_id_ = env->GetFieldID(clazz.obj(), "totalMem", "J"); | |
65 } | |
66 | |
67 // 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.
| |
68 std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { | |
69 return MemoryMonitorAndroid::Create(); | |
70 } | |
71 | |
72 } // namespace content | |
OLD | NEW |