Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 #import "ios/chrome/app/application_delegate/memory_warning_helper.h" | |
| 6 | |
| 7 #include "base/memory/memory_pressure_listener.h" | |
| 8 #include "base/metrics/histogram.h" | |
| 9 #include "ios/chrome/browser/crash_report/breakpad_helper.h" | |
| 10 #import "ios/chrome/browser/metrics/previous_session_info.h" | |
| 11 | |
| 12 namespace { | |
| 13 // The number of seconds to wait after a memory warning to clear the flag used | |
| 14 // to detect Out Of Memory crashes. | |
| 15 // NOTE: From local tests on various devices, this interval ranges between 1 and | |
| 16 // 3 seconds. It is set to 5 to ensure all out of memory crashes are identified, | |
| 17 // even if this may lead to overcounting them. | |
| 18 const CFTimeInterval kOutOfMemoryResetTimeInterval = 5; | |
| 19 } | |
| 20 | |
| 21 @interface MemoryWarningHelper () { | |
| 22 // The time at which to reset the OOM crash flag in the user defaults. This | |
| 23 // is used to handle receiving multiple memory warnings in short | |
| 24 // succession. | |
| 25 CFAbsoluteTime _outOfMemoryResetTime; | |
| 26 } | |
| 27 // Resets the OOM crash flag from the user defaults. | |
| 28 - (void)resetOutOfMemoryFlagIfNecessary; | |
| 29 @end | |
| 30 | |
| 31 @implementation MemoryWarningHelper | |
| 32 | |
| 33 @synthesize foregroundMemoryWarningCount = _foregroundMemoryWarningCount; | |
| 34 | |
| 35 - (void)handleMemoryPressure { | |
| 36 // Notify the system that the memory is critical and something should be done. | |
| 37 base::MemoryPressureListener::NotifyMemoryPressure( | |
| 38 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); | |
| 39 | |
| 40 ++_foregroundMemoryWarningCount; | |
| 41 // Register that we might die because of memory. If we are still alive in | |
| 42 // |kOutOfMemoryResetTimeInterval| seconds, reset the flag. | |
| 43 [[PreviousSessionInfo sharedInstance] setMemoryWarningFlag]; | |
| 44 _outOfMemoryResetTime = | |
| 45 CFAbsoluteTimeGetCurrent() + kOutOfMemoryResetTimeInterval; | |
| 46 | |
| 47 dispatch_after(kOutOfMemoryResetTimeInterval, dispatch_get_main_queue(), ^{ | |
| 48 [self resetOutOfMemoryFlagIfNecessary]; | |
| 49 }); | |
| 50 | |
| 51 // Add information to breakpad. | |
|
sdefresne
2016/05/19 10:09:33
nit: I would move this before the "dispatch_after"
| |
| 52 breakpad_helper::SetMemoryWarningCount(_foregroundMemoryWarningCount); | |
| 53 breakpad_helper::SetMemoryWarningInProgress(true); | |
| 54 } | |
| 55 | |
| 56 - (void)resetOutOfMemoryFlagIfNecessary { | |
| 57 if (CFAbsoluteTimeGetCurrent() < _outOfMemoryResetTime) | |
| 58 return; | |
| 59 _outOfMemoryResetTime = 0; | |
| 60 [[PreviousSessionInfo sharedInstance] resetMemoryWarningFlag]; | |
| 61 breakpad_helper::SetMemoryWarningInProgress(false); | |
| 62 } | |
| 63 | |
| 64 - (void)resetForegroundMemoryWarningCount { | |
| 65 _foregroundMemoryWarningCount = 0; | |
| 66 } | |
| 67 | |
| 68 @end | |
| OLD | NEW |