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
3 // found in the LICENSE file.
5 #include "ios/chrome/browser/crash_loop_detection_util.h"
7 #import <Foundation/Foundation.h>
10 NSString* const kAppStartupFailureCountKey = @"AppStartupFailureCount";
13 namespace crash_util {
15 int GetFailedStartupAttemptCount() {
16 static int startup_attempt_count = -1;
17 if (startup_attempt_count == -1) {
18 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
19 startup_attempt_count = [defaults integerForKey:kAppStartupFailureCountKey];
21 return startup_attempt_count;
24 void IncrementFailedStartupAttemptCount(bool flush_immediately) {
25 int startup_attempt_count = GetFailedStartupAttemptCount();
26 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
27 [defaults setInteger:(startup_attempt_count + 1)
28 forKey:kAppStartupFailureCountKey];
29 if (flush_immediately)
30 [defaults synchronize];
33 void ResetFailedStartupAttemptCount() {
34 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
35 if ([defaults integerForKey:kAppStartupFailureCountKey] != 0) {
36 [defaults setInteger:0 forKey:kAppStartupFailureCountKey];
37 [defaults synchronize];
41 } // namespace crash_util