| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #import "ios/chrome/browser/metrics/previous_session_info.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Key in the UserDefaults for a boolean value keeping track of memory warnings. |
| 15 NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating = |
| 16 @"DidSeeMemoryWarning"; |
| 17 |
| 18 // Key in the NSUserDefaults for a string value that stores the version of the |
| 19 // last session. |
| 20 NSString* const kLastRanVersion = @"LastRanVersion"; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 @interface PreviousSessionInfo () |
| 25 |
| 26 // Whether beginRecordingCurrentSession was called. |
| 27 @property(nonatomic, assign) BOOL didBeginRecordingCurrentSession; |
| 28 |
| 29 // Redefined to be read-write. |
| 30 @property(nonatomic, assign) BOOL didSeeMemoryWarningShortlyBeforeTerminating; |
| 31 @property(nonatomic, assign) BOOL isFirstSessionAfterUpgrade; |
| 32 |
| 33 @end |
| 34 |
| 35 @implementation PreviousSessionInfo |
| 36 |
| 37 @synthesize didBeginRecordingCurrentSession = _didBeginRecordingCurrentSession; |
| 38 @synthesize didSeeMemoryWarningShortlyBeforeTerminating = |
| 39 _didSeeMemoryWarningShortlyBeforeTerminating; |
| 40 @synthesize isFirstSessionAfterUpgrade = _isFirstSessionAfterUpgrade; |
| 41 |
| 42 // Singleton PreviousSessionInfo. |
| 43 static PreviousSessionInfo* gSharedInstance = nil; |
| 44 |
| 45 + (instancetype)sharedInstance { |
| 46 if (!gSharedInstance) { |
| 47 gSharedInstance = [[PreviousSessionInfo alloc] init]; |
| 48 |
| 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 = base::SysUTF8ToNSString( |
| 55 ios::GetChromeBrowserProvider()->GetVersionNumber()); |
| 56 gSharedInstance.isFirstSessionAfterUpgrade = |
| 57 ![lastRanVersion isEqualToString:currentVersion]; |
| 58 } |
| 59 return gSharedInstance; |
| 60 } |
| 61 |
| 62 + (void)resetSharedInstanceForTesting { |
| 63 [gSharedInstance release]; |
| 64 gSharedInstance = nil; |
| 65 } |
| 66 |
| 67 - (void)beginRecordingCurrentSession { |
| 68 if (self.didBeginRecordingCurrentSession) |
| 69 return; |
| 70 self.didBeginRecordingCurrentSession = YES; |
| 71 |
| 72 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 73 |
| 74 // Set the new version. |
| 75 NSString* currentVersion = base::SysUTF8ToNSString( |
| 76 ios::GetChromeBrowserProvider()->GetVersionNumber()); |
| 77 [defaults setObject:currentVersion forKey:kLastRanVersion]; |
| 78 |
| 79 // Clear the memory warning flag. |
| 80 [defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]; |
| 81 |
| 82 // Save critical state information for crash detection. |
| 83 [defaults synchronize]; |
| 84 } |
| 85 |
| 86 - (void)setMemoryWarningFlag { |
| 87 if (!self.didBeginRecordingCurrentSession) |
| 88 return; |
| 89 |
| 90 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 91 [defaults setBool:YES forKey:kDidSeeMemoryWarningShortlyBeforeTerminating]; |
| 92 // Save critical state information for crash detection. |
| 93 [defaults synchronize]; |
| 94 } |
| 95 |
| 96 - (void)resetMemoryWarningFlag { |
| 97 if (!self.didBeginRecordingCurrentSession) |
| 98 return; |
| 99 |
| 100 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 101 [defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]; |
| 102 // Save critical state information for crash detection. |
| 103 [defaults synchronize]; |
| 104 } |
| 105 |
| 106 @end |
| OLD | NEW |