Commit 1a289251 authored by sdefresne's avatar sdefresne Committed by Commit bot

[iOS] Upstream crash loop detection utilities

BUG=429756

Review URL: https://codereview.chromium.org/1147703002

Cr-Commit-Position: refs/heads/master@{#330361}
parent 0b3379c2
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_CRASH_LOOP_DETECTION_UTIL_H_
#define IOS_CHROME_BROWSER_CRASH_LOOP_DETECTION_UTIL_H_
namespace crash_util {
// Returns the number of consecutive failed startups ('instant' crashes) prior
// to this one. This method always returns the number of prior failures even
// after calls to increment or reset the value.
int GetFailedStartupAttemptCount();
// Increases the failed startup count. This should be called immediately after
// startup, so that if there is a crash, it is recorded.
// If |flush_immediately| is true, the value will be persisted immediately. If
// |flush_immediately| is false, this should be followed by a call to
// [[NSUserDefaults standardUserDefaults] synchronize]. This is optional to
// allow coallescing of the potentially expensive call during startup.
void IncrementFailedStartupAttemptCount(bool flush_immediately);
// Resets the failed startup count. This should be called once there is some
// indication the user isn't in a crash loop (e.g., some amount of time has
// elapsed, or some deliberate user action has been taken).
void ResetFailedStartupAttemptCount();
} // namespace crash_util
#endif // IOS_CHROME_BROWSER_CRASH_LOOP_DETECTION_UTIL_H_
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/chrome/browser/crash_loop_detection_util.h"
#import <Foundation/Foundation.h>
namespace {
NSString* const kAppStartupFailureCountKey = @"AppStartupFailureCount";
}
namespace crash_util {
int GetFailedStartupAttemptCount() {
static int startup_attempt_count = -1;
if (startup_attempt_count == -1) {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
startup_attempt_count = [defaults integerForKey:kAppStartupFailureCountKey];
}
return startup_attempt_count;
}
void IncrementFailedStartupAttemptCount(bool flush_immediately) {
int startup_attempt_count = GetFailedStartupAttemptCount();
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:(startup_attempt_count + 1)
forKey:kAppStartupFailureCountKey];
if (flush_immediately)
[defaults synchronize];
}
void ResetFailedStartupAttemptCount() {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if ([defaults integerForKey:kAppStartupFailureCountKey] != 0) {
[defaults setInteger:0 forKey:kAppStartupFailureCountKey];
[defaults synchronize];
}
}
} // namespace crash_util
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <Foundation/Foundation.h>
#include "ios/chrome/browser/crash_loop_detection_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
namespace {
// The key used to store the count in the implementation.
NSString* const kAppStartupAttemptCountKey = @"AppStartupFailureCount";
typedef PlatformTest CrashLoopDetectionUtilTest;
TEST_F(CrashLoopDetectionUtilTest, FullCycle) {
// Simulate one prior crash.
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:1 forKey:kAppStartupAttemptCountKey];
EXPECT_EQ(1, crash_util::GetFailedStartupAttemptCount());
crash_util::IncrementFailedStartupAttemptCount(false);
// It should still report 1, since it's reporting failures prior to this
// launch.
EXPECT_EQ(1, crash_util::GetFailedStartupAttemptCount());
// ... but under the hood the value should now be 2.
EXPECT_EQ(2, [defaults integerForKey:kAppStartupAttemptCountKey]);
// If it's mistakenly incerement again, nothing should change.
crash_util::IncrementFailedStartupAttemptCount(false);
EXPECT_EQ(2, [defaults integerForKey:kAppStartupAttemptCountKey]);
// After a reset it should be 0 internally, but the same via the API.
crash_util::ResetFailedStartupAttemptCount();
EXPECT_EQ(1, crash_util::GetFailedStartupAttemptCount());
EXPECT_EQ(0, [defaults integerForKey:kAppStartupAttemptCountKey]);
}
} // namespace
......@@ -122,6 +122,8 @@
'browser/chrome_url_constants.h',
'browser/chrome_url_util.h',
'browser/chrome_url_util.mm',
'browser/crash_loop_detection_util.h',
'browser/crash_loop_detection_util.mm',
'browser/crash_report/crash_report_background_uploader.h',
'browser/crash_report/crash_report_background_uploader.mm',
'browser/dom_distiller/distiller_viewer.cc',
......@@ -225,8 +227,6 @@
'browser/ui/animation_util.mm',
'browser/ui/background_generator.h',
'browser/ui/background_generator.mm',
'browser/ui/commands/UIKit+ChromeExecuteCommand.h',
'browser/ui/commands/UIKit+ChromeExecuteCommand.mm',
'browser/ui/commands/clear_browsing_data_command.h',
'browser/ui/commands/clear_browsing_data_command.mm',
'browser/ui/commands/generic_chrome_command.h',
......@@ -240,6 +240,8 @@
'browser/ui/commands/show_mail_composer_command.mm',
'browser/ui/commands/show_signin_command.h',
'browser/ui/commands/show_signin_command.mm',
'browser/ui/commands/UIKit+ChromeExecuteCommand.h',
'browser/ui/commands/UIKit+ChromeExecuteCommand.mm',
'browser/ui/file_locations.h',
'browser/ui/file_locations.mm',
'browser/ui/image_util.h',
......
......@@ -27,6 +27,7 @@
'sources': [
'app/safe_mode_util_unittest.cc',
'browser/chrome_url_util_unittest.mm',
'browser/crash_loop_detection_util_unittest.mm',
'browser/experimental_flags_unittest.mm',
'browser/geolocation/CLLocation+XGeoHeaderTest.mm',
'browser/geolocation/location_manager_unittest.mm',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment