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

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

Issue 2340293003: Add MemoryMonitorAndroid (Closed)
Patch Set: rebase 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 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 // static
24 bool MemoryMonitorAndroid::Register(JNIEnv* env) {
25 return RegisterNativesImpl(env);
26 }
27
28 MemoryMonitorAndroid::MemoryMonitorAndroid() {}
29
30 MemoryMonitorAndroid::~MemoryMonitorAndroid() {}
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);
40 JNIEnv* env = base::android::AttachCurrentThread();
41 Java_MemoryMonitorAndroid_getMemoryInfo(
42 env, base::android::GetApplicationContext(),
43 reinterpret_cast<intptr_t>(out));
44 }
45
46 // Called by JNI to populate ActivityManager.MemoryInfo.
47 static void GetMemoryInfoCallback(
48 JNIEnv* env,
49 const base::android::JavaParamRef<jclass>& clazz,
50 jlong avail_mem,
51 jboolean low_memory,
52 jlong threshold,
53 jlong total_mem,
54 jlong out_ptr) {
55 DCHECK(out_ptr);
56 MemoryMonitorAndroid::MemoryInfo* info =
57 reinterpret_cast<MemoryMonitorAndroid::MemoryInfo*>(out_ptr);
58 info->avail_mem = avail_mem;
59 info->low_memory = low_memory;
60 info->threshold = threshold;
61 info->total_mem = total_mem;
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
« no previous file with comments | « content/browser/memory/memory_monitor_android.h ('k') | content/browser/memory/memory_monitor_android_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698