Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / ios / chrome / browser / metrics / previous_session_info.mm
blobf77c3f902ceec1f0876f75426db9e3daa8dc2932
1 // Copyright 2015 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 #import "ios/chrome/browser/metrics/previous_session_info.h"
7 #include "base/logging.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/sys_string_conversions.h"
10 #include "components/version_info/version_info.h"
12 namespace {
14 // Key in the UserDefaults for a boolean value keeping track of memory warnings.
15 NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating =
16     @"DidSeeMemoryWarning";
18 // Key in the NSUserDefaults for a string value that stores the version of the
19 // last session.
20 NSString* const kLastRanVersion = @"LastRanVersion";
22 }  // namespace
24 @interface PreviousSessionInfo ()
26 // Whether beginRecordingCurrentSession was called.
27 @property(nonatomic, assign) BOOL didBeginRecordingCurrentSession;
29 // Redefined to be read-write.
30 @property(nonatomic, assign) BOOL didSeeMemoryWarningShortlyBeforeTerminating;
31 @property(nonatomic, assign) BOOL isFirstSessionAfterUpgrade;
33 @end
35 @implementation PreviousSessionInfo
37 @synthesize didBeginRecordingCurrentSession = _didBeginRecordingCurrentSession;
38 @synthesize didSeeMemoryWarningShortlyBeforeTerminating =
39     _didSeeMemoryWarningShortlyBeforeTerminating;
40 @synthesize isFirstSessionAfterUpgrade = _isFirstSessionAfterUpgrade;
42 // Singleton PreviousSessionInfo.
43 static PreviousSessionInfo* gSharedInstance = nil;
45 + (instancetype)sharedInstance {
46   if (!gSharedInstance) {
47     gSharedInstance = [[PreviousSessionInfo alloc] init];
49     // Load the persisted information.
50     NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
51     gSharedInstance.didSeeMemoryWarningShortlyBeforeTerminating =
52         [defaults boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
53     NSString* lastRanVersion = [defaults stringForKey:kLastRanVersion];
54     NSString* currentVersion =
55         base::SysUTF8ToNSString(version_info::GetVersionNumber());
56     gSharedInstance.isFirstSessionAfterUpgrade =
57         ![lastRanVersion isEqualToString:currentVersion];
58   }
59   return gSharedInstance;
62 + (void)resetSharedInstanceForTesting {
63   [gSharedInstance release];
64   gSharedInstance = nil;
67 - (void)beginRecordingCurrentSession {
68   if (self.didBeginRecordingCurrentSession)
69     return;
70   self.didBeginRecordingCurrentSession = YES;
72   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
74   // Set the new version.
75   NSString* currentVersion =
76       base::SysUTF8ToNSString(version_info::GetVersionNumber());
77   [defaults setObject:currentVersion forKey:kLastRanVersion];
79   // Clear the memory warning flag.
80   [defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
82   // Save critical state information for crash detection.
83   [defaults synchronize];
86 - (void)setMemoryWarningFlag {
87   if (!self.didBeginRecordingCurrentSession)
88     return;
90   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
91   [defaults setBool:YES forKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
92   // Save critical state information for crash detection.
93   [defaults synchronize];
96 - (void)resetMemoryWarningFlag {
97   if (!self.didBeginRecordingCurrentSession)
98     return;
100   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
101   [defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
102   // Save critical state information for crash detection.
103   [defaults synchronize];
106 @end