Commit 730adc18 authored by Mike Dougherty's avatar Mike Dougherty Committed by Commit Bot

Save and expose additional data about the previous application session.

Adds device thermal state, low power mode, and whether or not the OS was updated
between the previous and current session.

Bug: 965393
Change-Id: Ibb91efe391a10daa983aafe56e93a9d4e02fa114
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1621710
Commit-Queue: Mike Dougherty <michaeldo@chromium.org>
Reviewed-by: default avatarOlivier Robin <olivierrobin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#662714}
parent 640b83bb
...@@ -10,6 +10,18 @@ ...@@ -10,6 +10,18 @@
namespace previous_session_info_constants { namespace previous_session_info_constants {
// Key in the UserDefaults for a boolean value keeping track of memory warnings. // Key in the UserDefaults for a boolean value keeping track of memory warnings.
extern NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating; extern NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating;
// The values of this enum are persisted representing the state of the last
// session (which may have been running a different version of the application).
// Therefore, entries should not be renumbered and numeric values should never
// be reused.
enum class DeviceThermalState {
kUnknown = 0,
kNominal = 1,
kFair = 2,
kSerious = 3,
kCritical = 4,
};
} // namespace previous_session_info_constants } // namespace previous_session_info_constants
// PreviousSessionInfo has two jobs: // PreviousSessionInfo has two jobs:
...@@ -19,10 +31,21 @@ extern NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating; ...@@ -19,10 +31,21 @@ extern NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating;
// - Persist information about the current session, for use in a next session. // - Persist information about the current session, for use in a next session.
@interface PreviousSessionInfo : NSObject @interface PreviousSessionInfo : NSObject
// The thermal state of the device at the end of the previous session.
@property(nonatomic, assign, readonly)
previous_session_info_constants::DeviceThermalState deviceThermalState;
// Whether the device was in low power mode at the end of the previous session.
@property(nonatomic, assign, readonly) BOOL deviceWasInLowPowerMode;
// Whether the app received a memory warning seconds before being terminated. // Whether the app received a memory warning seconds before being terminated.
@property(nonatomic, assign, readonly) @property(nonatomic, assign, readonly)
BOOL didSeeMemoryWarningShortlyBeforeTerminating; BOOL didSeeMemoryWarningShortlyBeforeTerminating;
// Whether or not the system OS was updated between the previous and the
// current session.
@property(nonatomic, assign, readonly) BOOL isFirstSessionAfterOSUpgrade;
// Whether the app was updated between the previous and the current session. // Whether the app was updated between the previous and the current session.
@property(nonatomic, assign, readonly) BOOL isFirstSessionAfterUpgrade; @property(nonatomic, assign, readonly) BOOL isFirstSessionAfterUpgrade;
...@@ -39,6 +62,12 @@ extern NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating; ...@@ -39,6 +62,12 @@ extern NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating;
// persisting information about the current session, for use in a next session. // persisting information about the current session, for use in a next session.
- (void)beginRecordingCurrentSession; - (void)beginRecordingCurrentSession;
// Updates the saved last known low power mode setting of the device.
- (void)updateStoredLowPowerMode;
// Updates the saved last known thermal state of the device.
- (void)updateStoredThermalState;
// When a session has begun, records that a memory warning was received. // When a session has begun, records that a memory warning was received.
- (void)setMemoryWarningFlag; - (void)setMemoryWarningFlag;
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/system/sys_info.h"
#include "components/version_info/version_info.h" #include "components/version_info/version_info.h"
#import "ios/chrome/browser/metrics/previous_session_info_private.h" #import "ios/chrome/browser/metrics/previous_session_info_private.h"
...@@ -13,15 +14,41 @@ ...@@ -13,15 +14,41 @@
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
using previous_session_info_constants::DeviceThermalState;
namespace { namespace {
// Key in the NSUserDefaults for a string value that stores the version of the // Translates a NSProcessInfoThermalState value to DeviceThermalState value.
// last session. DeviceThermalState GetThermalStateFromNSProcessInfoThermalState(
NSString* const kLastRanVersion = @"LastRanVersion"; NSProcessInfoThermalState process_info_thermal_state) {
switch (process_info_thermal_state) {
case NSProcessInfoThermalStateNominal:
return DeviceThermalState::kNominal;
case NSProcessInfoThermalStateFair:
return DeviceThermalState::kFair;
case NSProcessInfoThermalStateSerious:
return DeviceThermalState::kSerious;
case NSProcessInfoThermalStateCritical:
return DeviceThermalState::kCritical;
}
// Key in the NSUserDefaults for a string value that stores the language of the return DeviceThermalState::kUnknown;
// last session. }
// NSUserDefaults keys.
// - The (string) application version.
NSString* const kLastRanVersion = @"LastRanVersion";
// - The (string) device language.
NSString* const kLastRanLanguage = @"LastRanLanguage"; NSString* const kLastRanLanguage = @"LastRanLanguage";
// - The (string) OS version.
NSString* const kPreviousSessionInfoOSVersion = @"PreviousSessionInfoOSVersion";
// - The (integer) underlying value of the DeviceThermalState enum representing
// the device thermal state.
NSString* const kPreviousSessionInfoThermalState =
@"PreviousSessionInfoThermalState";
// - A (boolean) describing whether or not low power mode is enabled.
NSString* const kPreviousSessionInfoLowPowerMode =
@"PreviousSessionInfoLowPowerMode";
} // namespace } // namespace
...@@ -36,7 +63,10 @@ NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating = ...@@ -36,7 +63,10 @@ NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating =
@property(nonatomic, assign) BOOL didBeginRecordingCurrentSession; @property(nonatomic, assign) BOOL didBeginRecordingCurrentSession;
// Redefined to be read-write. // Redefined to be read-write.
@property(nonatomic, assign) DeviceThermalState deviceThermalState;
@property(nonatomic, assign) BOOL deviceWasInLowPowerMode;
@property(nonatomic, assign) BOOL didSeeMemoryWarningShortlyBeforeTerminating; @property(nonatomic, assign) BOOL didSeeMemoryWarningShortlyBeforeTerminating;
@property(nonatomic, assign) BOOL isFirstSessionAfterOSUpgrade;
@property(nonatomic, assign) BOOL isFirstSessionAfterUpgrade; @property(nonatomic, assign) BOOL isFirstSessionAfterUpgrade;
@property(nonatomic, assign) BOOL isFirstSessionAfterLanguageChange; @property(nonatomic, assign) BOOL isFirstSessionAfterLanguageChange;
...@@ -44,9 +74,12 @@ NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating = ...@@ -44,9 +74,12 @@ NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating =
@implementation PreviousSessionInfo @implementation PreviousSessionInfo
@synthesize deviceThermalState = _deviceThermalState;
@synthesize deviceWasInLowPowerMode = _deviceWasInLowPowerMode;
@synthesize didBeginRecordingCurrentSession = _didBeginRecordingCurrentSession; @synthesize didBeginRecordingCurrentSession = _didBeginRecordingCurrentSession;
@synthesize didSeeMemoryWarningShortlyBeforeTerminating = @synthesize didSeeMemoryWarningShortlyBeforeTerminating =
_didSeeMemoryWarningShortlyBeforeTerminating; _didSeeMemoryWarningShortlyBeforeTerminating;
@synthesize isFirstSessionAfterOSUpgrade = _isFirstSessionAfterOSUpgrade;
@synthesize isFirstSessionAfterUpgrade = _isFirstSessionAfterUpgrade; @synthesize isFirstSessionAfterUpgrade = _isFirstSessionAfterUpgrade;
@synthesize isFirstSessionAfterLanguageChange = @synthesize isFirstSessionAfterLanguageChange =
_isFirstSessionAfterLanguageChange; _isFirstSessionAfterLanguageChange;
...@@ -63,6 +96,18 @@ static PreviousSessionInfo* gSharedInstance = nil; ...@@ -63,6 +96,18 @@ static PreviousSessionInfo* gSharedInstance = nil;
gSharedInstance.didSeeMemoryWarningShortlyBeforeTerminating = gSharedInstance.didSeeMemoryWarningShortlyBeforeTerminating =
[defaults boolForKey:previous_session_info_constants:: [defaults boolForKey:previous_session_info_constants::
kDidSeeMemoryWarningShortlyBeforeTerminating]; kDidSeeMemoryWarningShortlyBeforeTerminating];
gSharedInstance.deviceWasInLowPowerMode =
[defaults boolForKey:kPreviousSessionInfoLowPowerMode];
gSharedInstance.deviceThermalState = static_cast<DeviceThermalState>(
[defaults integerForKey:kPreviousSessionInfoThermalState]);
NSString* versionOfOSAtLastRun =
[defaults stringForKey:kPreviousSessionInfoOSVersion];
NSString* currentOSVersion =
base::SysUTF8ToNSString(base::SysInfo::OperatingSystemVersion());
gSharedInstance.isFirstSessionAfterOSUpgrade =
![versionOfOSAtLastRun isEqualToString:currentOSVersion];
NSString* lastRanVersion = [defaults stringForKey:kLastRanVersion]; NSString* lastRanVersion = [defaults stringForKey:kLastRanVersion];
NSString* currentVersion = NSString* currentVersion =
base::SysUTF8ToNSString(version_info::GetVersionNumber()); base::SysUTF8ToNSString(version_info::GetVersionNumber());
...@@ -88,12 +133,17 @@ static PreviousSessionInfo* gSharedInstance = nil; ...@@ -88,12 +133,17 @@ static PreviousSessionInfo* gSharedInstance = nil;
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
// Set the new version. // Set the current Chrome version.
NSString* currentVersion = NSString* currentVersion =
base::SysUTF8ToNSString(version_info::GetVersionNumber()); base::SysUTF8ToNSString(version_info::GetVersionNumber());
[defaults setObject:currentVersion forKey:kLastRanVersion]; [defaults setObject:currentVersion forKey:kLastRanVersion];
// Set the new language. // Set the current OS version.
NSString* currentOSVersion =
base::SysUTF8ToNSString(base::SysInfo::OperatingSystemVersion());
[defaults setObject:currentOSVersion forKey:kPreviousSessionInfoOSVersion];
// Set the current language.
NSString* currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0]; NSString* currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
[defaults setObject:currentLanguage forKey:kLastRanLanguage]; [defaults setObject:currentLanguage forKey:kLastRanLanguage];
...@@ -102,10 +152,46 @@ static PreviousSessionInfo* gSharedInstance = nil; ...@@ -102,10 +152,46 @@ static PreviousSessionInfo* gSharedInstance = nil;
removeObjectForKey:previous_session_info_constants:: removeObjectForKey:previous_session_info_constants::
kDidSeeMemoryWarningShortlyBeforeTerminating]; kDidSeeMemoryWarningShortlyBeforeTerminating];
[self updateStoredLowPowerMode];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(updateStoredLowPowerMode)
name:NSProcessInfoPowerStateDidChangeNotification
object:nil];
[self updateStoredThermalState];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(updateStoredThermalState)
name:NSProcessInfoThermalStateDidChangeNotification
object:nil];
// Save critical state information for crash detection. // Save critical state information for crash detection.
[defaults synchronize]; [defaults synchronize];
} }
- (void)updateStoredLowPowerMode {
BOOL isLowPoweredModeEnabled =
[[NSProcessInfo processInfo] isLowPowerModeEnabled];
[[NSUserDefaults standardUserDefaults]
setInteger:isLowPoweredModeEnabled
forKey:kPreviousSessionInfoLowPowerMode];
}
- (void)updateStoredThermalState {
NSProcessInfo* processInfo = [NSProcessInfo processInfo];
// Translate value to an app defined enum as the system could change the
// underlying values of NSProcessInfoThermalState between OS versions.
DeviceThermalState thermalState =
GetThermalStateFromNSProcessInfoThermalState([processInfo thermalState]);
NSInteger thermalStateValue =
static_cast<std::underlying_type<DeviceThermalState>::type>(thermalState);
[[NSUserDefaults standardUserDefaults]
setInteger:thermalStateValue
forKey:kPreviousSessionInfoThermalState];
}
- (void)setMemoryWarningFlag { - (void)setMemoryWarningFlag {
if (!self.didBeginRecordingCurrentSession) if (!self.didBeginRecordingCurrentSession)
return; return;
......
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