OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/memory/memory_monitor_android.h" | 5 #include "content/browser/memory/memory_monitor_android.h" |
6 | 6 |
7 #include "base/android/context_utils.h" | 7 #include "base/android/context_utils.h" |
8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "jni/MemoryMonitorAndroid_jni.h" | 10 #include "jni/MemoryMonitorAndroid_jni.h" |
11 | 11 |
12 namespace content { | 12 namespace content { |
13 | 13 |
14 namespace { | 14 namespace { |
15 const size_t kMBShift = 20; | 15 const size_t kMBShift = 20; |
16 } | 16 } |
17 | 17 |
18 // static | 18 // An implementation of MemoryMonitorAndroid::Delegate using the Android APIs. |
19 std::unique_ptr<MemoryMonitorAndroid> MemoryMonitorAndroid::Create() { | 19 class MemoryMonitorAndroidDelegateImpl : public MemoryMonitorAndroid::Delegate { |
20 return base::WrapUnique(new MemoryMonitorAndroid); | 20 public: |
21 } | 21 MemoryMonitorAndroidDelegateImpl() {} |
| 22 ~MemoryMonitorAndroidDelegateImpl() override {} |
22 | 23 |
23 // static | 24 using MemoryInfo = MemoryMonitorAndroid::MemoryInfo; |
24 bool MemoryMonitorAndroid::Register(JNIEnv* env) { | 25 void GetMemoryInfo(MemoryInfo* out) override; |
25 return RegisterNativesImpl(env); | |
26 } | |
27 | 26 |
28 MemoryMonitorAndroid::MemoryMonitorAndroid() {} | 27 private: |
| 28 DISALLOW_COPY_AND_ASSIGN(MemoryMonitorAndroidDelegateImpl); |
| 29 }; |
29 | 30 |
30 MemoryMonitorAndroid::~MemoryMonitorAndroid() {} | 31 void MemoryMonitorAndroidDelegateImpl::GetMemoryInfo(MemoryInfo* out) { |
31 | |
32 int MemoryMonitorAndroid::GetFreeMemoryUntilCriticalMB() { | |
33 MemoryInfo info; | |
34 GetMemoryInfo(&info); | |
35 return (info.avail_mem - info.threshold) >> kMBShift; | |
36 } | |
37 | |
38 void MemoryMonitorAndroid::GetMemoryInfo(MemoryInfo* out) { | |
39 DCHECK(out); | 32 DCHECK(out); |
40 JNIEnv* env = base::android::AttachCurrentThread(); | 33 JNIEnv* env = base::android::AttachCurrentThread(); |
41 Java_MemoryMonitorAndroid_getMemoryInfo( | 34 Java_MemoryMonitorAndroid_getMemoryInfo( |
42 env, base::android::GetApplicationContext(), | 35 env, base::android::GetApplicationContext(), |
43 reinterpret_cast<intptr_t>(out)); | 36 reinterpret_cast<intptr_t>(out)); |
44 } | 37 } |
45 | 38 |
46 // Called by JNI to populate ActivityManager.MemoryInfo. | 39 // Called by JNI to populate ActivityManager.MemoryInfo. |
47 static void GetMemoryInfoCallback( | 40 static void GetMemoryInfoCallback( |
48 JNIEnv* env, | 41 JNIEnv* env, |
49 const base::android::JavaParamRef<jclass>& clazz, | 42 const base::android::JavaParamRef<jclass>& clazz, |
50 jlong avail_mem, | 43 jlong avail_mem, |
51 jboolean low_memory, | 44 jboolean low_memory, |
52 jlong threshold, | 45 jlong threshold, |
53 jlong total_mem, | 46 jlong total_mem, |
54 jlong out_ptr) { | 47 jlong out_ptr) { |
55 DCHECK(out_ptr); | 48 DCHECK(out_ptr); |
56 MemoryMonitorAndroid::MemoryInfo* info = | 49 MemoryMonitorAndroid::MemoryInfo* info = |
57 reinterpret_cast<MemoryMonitorAndroid::MemoryInfo*>(out_ptr); | 50 reinterpret_cast<MemoryMonitorAndroid::MemoryInfo*>(out_ptr); |
58 info->avail_mem = avail_mem; | 51 info->avail_mem = avail_mem; |
59 info->low_memory = low_memory; | 52 info->low_memory = low_memory; |
60 info->threshold = threshold; | 53 info->threshold = threshold; |
61 info->total_mem = total_mem; | 54 info->total_mem = total_mem; |
62 } | 55 } |
63 | 56 |
| 57 // static |
| 58 std::unique_ptr<MemoryMonitorAndroid> MemoryMonitorAndroid::Create() { |
| 59 auto delegate = base::WrapUnique(new MemoryMonitorAndroidDelegateImpl); |
| 60 return base::WrapUnique(new MemoryMonitorAndroid(std::move(delegate))); |
| 61 } |
| 62 |
| 63 // static |
| 64 bool MemoryMonitorAndroid::Register(JNIEnv* env) { |
| 65 return RegisterNativesImpl(env); |
| 66 } |
| 67 |
| 68 MemoryMonitorAndroid::MemoryMonitorAndroid(std::unique_ptr<Delegate> delegate) |
| 69 : delegate_(std::move(delegate)) { |
| 70 DCHECK(delegate_.get()); |
| 71 } |
| 72 |
| 73 MemoryMonitorAndroid::~MemoryMonitorAndroid() {} |
| 74 |
| 75 int MemoryMonitorAndroid::GetFreeMemoryUntilCriticalMB() { |
| 76 MemoryInfo info; |
| 77 GetMemoryInfo(&info); |
| 78 return (info.avail_mem - info.threshold) >> kMBShift; |
| 79 } |
| 80 |
| 81 void MemoryMonitorAndroid::GetMemoryInfo(MemoryInfo* out) { |
| 82 delegate_->GetMemoryInfo(out); |
| 83 } |
| 84 |
64 // Implementation of a factory function defined in memory_monitor.h. | 85 // Implementation of a factory function defined in memory_monitor.h. |
65 std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { | 86 std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { |
66 return MemoryMonitorAndroid::Create(); | 87 return MemoryMonitorAndroid::Create(); |
67 } | 88 } |
68 | 89 |
69 } // namespace content | 90 } // namespace content |
OLD | NEW |