[blink-in-js] Migrate resources required for blink-in-js to grd - part 2
[chromium-blink-merge.git] / athena / resource_manager / memory_pressure_notifier.cc
blob0dc8dbd73c89fa1eb458934ac99ba79e1b8f69e0
1 // Copyright (c) 2014 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.
5 #include "athena/resource_manager/memory_pressure_notifier.h"
7 #include "athena/resource_manager/public/resource_manager_delegate.h"
8 #include "base/time/time.h"
11 namespace athena {
13 MemoryPressureNotifier::MemoryPressureNotifier(
14 MemoryPressureObserver* listener)
15 : listener_(listener),
16 current_pressure_(MemoryPressureObserver::MEMORY_PRESSURE_UNKNOWN) {
17 StartObserving();
20 MemoryPressureNotifier::~MemoryPressureNotifier() {
21 StopObserving();
24 void MemoryPressureNotifier::StartObserving() {
25 int time_in_ms = listener_->GetDelegate()->MemoryPressureIntervalInMS();
26 timer_.Start(FROM_HERE,
27 base::TimeDelta::FromMilliseconds(time_in_ms),
28 base::Bind(&MemoryPressureNotifier::CheckMemoryPressure,
29 base::Unretained(this)));
32 void MemoryPressureNotifier::StopObserving() {
33 // If StartObserving failed, StopObserving will still get called.
34 timer_.Stop();
37 void MemoryPressureNotifier::CheckMemoryPressure() {
38 MemoryPressureObserver::MemoryPressure pressure =
39 GetMemoryPressureLevelFromFillLevel(
40 listener_->GetDelegate()->GetUsedMemoryInPercent());
41 if (current_pressure_ != pressure ||
42 (pressure != MemoryPressureObserver::MEMORY_PRESSURE_LOW &&
43 pressure != MemoryPressureObserver::MEMORY_PRESSURE_UNKNOWN)) {
44 // If we are anything worse than |MEMORY_PRESSURE_LOW|, we notify the
45 // listener.
46 current_pressure_ = pressure;
47 listener_->OnMemoryPressure(current_pressure_);
51 MemoryPressureObserver::MemoryPressure
52 MemoryPressureNotifier::GetMemoryPressureLevelFromFillLevel(
53 int memory_fill_level) {
54 if (memory_fill_level == 0)
55 return MemoryPressureObserver::MEMORY_PRESSURE_UNKNOWN;
56 if (memory_fill_level < 50)
57 return MemoryPressureObserver::MEMORY_PRESSURE_LOW;
58 if (memory_fill_level < 75)
59 return MemoryPressureObserver::MEMORY_PRESSURE_MODERATE;
60 if (memory_fill_level < 90)
61 return MemoryPressureObserver::MEMORY_PRESSURE_HIGH;
62 return MemoryPressureObserver::MEMORY_PRESSURE_CRITICAL;
65 } // namespace athena