| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "ios/chrome/browser/crash_loop_detection_util.h" | 5 #include "ios/chrome/browser/crash_loop_detection_util.h" |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 | 8 |
| 9 #if !defined(__has_feature) || !__has_feature(objc_arc) | 9 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 10 #error "This file requires ARC support." | 10 #error "This file requires ARC support." |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 static int startup_attempt_count = -1; |
| 14 NSString* const kAppStartupFailureCountKey = @"AppStartupFailureCount"; | 15 NSString* const kAppStartupFailureCountKey = @"AppStartupFailureCount"; |
| 15 } | 16 } |
| 16 | 17 |
| 17 namespace crash_util { | 18 namespace crash_util { |
| 18 | 19 |
| 19 int GetFailedStartupAttemptCount() { | 20 int GetFailedStartupAttemptCount() { |
| 20 static int startup_attempt_count = -1; | |
| 21 if (startup_attempt_count == -1) { | 21 if (startup_attempt_count == -1) { |
| 22 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | 22 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 23 startup_attempt_count = [defaults integerForKey:kAppStartupFailureCountKey]; | 23 startup_attempt_count = [defaults integerForKey:kAppStartupFailureCountKey]; |
| 24 } | 24 } |
| 25 return startup_attempt_count; | 25 return startup_attempt_count; |
| 26 } | 26 } |
| 27 | 27 |
| 28 void IncrementFailedStartupAttemptCount(bool flush_immediately) { | 28 void IncrementFailedStartupAttemptCount(bool flush_immediately) { |
| 29 int startup_attempt_count = GetFailedStartupAttemptCount(); | 29 int startup_attempt_count = GetFailedStartupAttemptCount(); |
| 30 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | 30 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 31 [defaults setInteger:(startup_attempt_count + 1) | 31 [defaults setInteger:(startup_attempt_count + 1) |
| 32 forKey:kAppStartupFailureCountKey]; | 32 forKey:kAppStartupFailureCountKey]; |
| 33 if (flush_immediately) | 33 if (flush_immediately) |
| 34 [defaults synchronize]; | 34 [defaults synchronize]; |
| 35 } | 35 } |
| 36 | 36 |
| 37 void ResetFailedStartupAttemptCount() { | 37 void ResetFailedStartupAttemptCount() { |
| 38 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | 38 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 39 if ([defaults integerForKey:kAppStartupFailureCountKey] != 0) { | 39 if ([defaults integerForKey:kAppStartupFailureCountKey] != 0) { |
| 40 [defaults setInteger:0 forKey:kAppStartupFailureCountKey]; | 40 [defaults setInteger:0 forKey:kAppStartupFailureCountKey]; |
| 41 [defaults synchronize]; | 41 [defaults synchronize]; |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 | 44 |
| 45 void ResetFailedStartupAttemptCountForTests() { |
| 46 ResetFailedStartupAttemptCount(); |
| 47 startup_attempt_count = -1; |
| 48 } |
| 49 |
| 45 } // namespace crash_util | 50 } // namespace crash_util |
| OLD | NEW |