previous_session_info.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #ifndef COMPONENTS_PREVIOUS_SESSION_INFO_PREVIOUS_SESSION_INFO_H_
  5. #define COMPONENTS_PREVIOUS_SESSION_INFO_PREVIOUS_SESSION_INFO_H_
  6. #import <UIKit/UIKit.h>
  7. #include "base/callback_helpers.h"
  8. #include "url/gurl.h"
  9. namespace base {
  10. class TimeDelta;
  11. }
  12. namespace previous_session_info_constants {
  13. // - The (Integer) representing UIApplicationState.
  14. extern NSString* const kPreviousSessionInfoApplicationState;
  15. // Key in the UserDefaults for a boolean value keeping track of memory warnings.
  16. extern NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating;
  17. // Key in the UserDefaults for a double value which stores OS start time.
  18. extern NSString* const kOSStartTime;
  19. // Key in the UserDefaults for a boolean describing whether or not the session
  20. // restoration is in progress.
  21. extern NSString* const kPreviousSessionInfoRestoringSession;
  22. // Key in the UserDefaults for an array which contains the ids for the connected
  23. // scene sessions on the previous run.
  24. extern NSString* const kPreviousSessionInfoConnectedSceneSessionIDs;
  25. // Key in the UserDefaults for a dictionary with session info params.
  26. extern NSString* const kPreviousSessionInfoParams;
  27. // Key in the UserDefaults for the memory footprint of the browser process.
  28. extern NSString* const kPreviousSessionInfoMemoryFootprint;
  29. // Key in the UserDefaults for the number of open tabs.
  30. extern NSString* const kPreviousSessionInfoTabCount;
  31. // Key in the UserDefaults for the number of open "off the record" tabs.
  32. extern NSString* const kPreviousSessionInfoOTRTabCount;
  33. // The values of this enum are persisted (both to NSUserDefaults and logs) and
  34. // represent the state of the last session (which may have been running a
  35. // different version of the application).
  36. // Therefore, entries should not be renumbered and numeric values should never
  37. // be reused.
  38. enum class DeviceThermalState {
  39. kUnknown = 0,
  40. kNominal = 1,
  41. kFair = 2,
  42. kSerious = 3,
  43. kCritical = 4,
  44. kMaxValue = kCritical,
  45. };
  46. // The values of this enum are persisted (both to NSUserDefaults and logs) and
  47. // represent the state of the last session (which may have been running a
  48. // different version of the application).
  49. // Therefore, entries should not be renumbered and numeric values should never
  50. // be reused.
  51. enum class DeviceBatteryState {
  52. kUnknown = 0,
  53. kUnplugged = 1,
  54. kCharging = 2,
  55. // Battery is plugged into power and the battery is 100% charged.
  56. kFull = 3,
  57. kMaxValue = kFull,
  58. };
  59. } // namespace previous_session_info_constants
  60. // PreviousSessionInfo has two jobs:
  61. // - Holding information about the last session, persisted across restart.
  62. // These informations are accessible via the properties on the shared
  63. // instance.
  64. // - Persist information about the current session, for use in a next session.
  65. @interface PreviousSessionInfo : NSObject
  66. // UIApplicationState at the end of the previous session or nil if state is
  67. // unknown.
  68. @property(nonatomic, assign, readonly) UIApplicationState* applicationState;
  69. // The battery level of the device at the end of the previous session.
  70. @property(nonatomic, assign, readonly) float deviceBatteryLevel;
  71. // The battery state of the device at the end of the previous session.
  72. @property(nonatomic, assign, readonly)
  73. previous_session_info_constants::DeviceBatteryState deviceBatteryState;
  74. // The storage available, in kilobytes, at the end of the previous session or -1
  75. // if no previous session data is available.
  76. @property(nonatomic, assign, readonly) NSInteger availableDeviceStorage;
  77. // The thermal state of the device at the end of the previous session.
  78. @property(nonatomic, assign, readonly)
  79. previous_session_info_constants::DeviceThermalState deviceThermalState;
  80. // Whether the app received a memory warning seconds before being terminated.
  81. @property(nonatomic, assign, readonly)
  82. BOOL didSeeMemoryWarningShortlyBeforeTerminating;
  83. // Whether the app was updated between the previous and the current session.
  84. @property(nonatomic, assign, readonly) BOOL isFirstSessionAfterUpgrade;
  85. // Whether the language has been changed between the previous and the current
  86. // session.
  87. @property(nonatomic, assign, readonly) BOOL isFirstSessionAfterLanguageChange;
  88. // Whether or not the OS was restarted between the previous and the current
  89. // session.
  90. @property(nonatomic, assign, readonly) BOOL OSRestartedAfterPreviousSession;
  91. // The OS version during the previous session or nil if no previous session data
  92. // is available.
  93. @property(nonatomic, strong, readonly) NSString* OSVersion;
  94. // The date time at which recording for the previous sesion has started. Note
  95. // that recording usually starts soon after startup, but not exactly at the
  96. // startup.
  97. @property(nonatomic, strong, readonly) NSDate* sessionStartTime;
  98. // The time at which the previous sesion ended. Note that this is only an
  99. // estimate and is updated whenever another value of the receiver is updated.
  100. @property(nonatomic, strong, readonly) NSDate* sessionEndTime;
  101. // YES if the previous session was terminated during session restoration.
  102. // Reset to NO after resetSessionRestorationFlag call.
  103. @property(nonatomic, readonly) BOOL terminatedDuringSessionRestoration;
  104. // The list of the session IDs for all the connected scenes, used for crash
  105. // restoration.
  106. @property(nonatomic, readonly)
  107. NSMutableSet<NSString*>* connectedSceneSessionsIDs;
  108. // Crash report parameters as key-value pairs.
  109. @property(nonatomic, readonly)
  110. NSDictionary<NSString*, NSString*>* reportParameters;
  111. // Memory footprint in bytes of the browser process.
  112. @property(nonatomic, readonly) NSInteger memoryFootprint;
  113. // YES if ApplicationWillTerminate notification was posted for the previous
  114. // session.
  115. @property(nonatomic, readonly) BOOL applicationWillTerminateWasReceived;
  116. // Number of open tabs in the previous session.
  117. @property(nonatomic, readonly) NSInteger tabCount;
  118. // Number of open "off the record" tabs in the previous session.
  119. @property(nonatomic, readonly) NSInteger OTRTabCount;
  120. // Singleton PreviousSessionInfo. During the lifetime of the app, the returned
  121. // object is the same, and describes the previous session, even after a new
  122. // session has started (by calling beginRecordingCurrentSession).
  123. + (instancetype)sharedInstance;
  124. // Clears the persisted information about the previous session and starts
  125. // persisting information about the current session, for use in a next session.
  126. - (void)beginRecordingCurrentSession;
  127. // Starts memory usage data recording with given |interval|.
  128. - (void)startRecordingMemoryFootprintWithInterval:(base::TimeDelta)interval;
  129. // Stops memory usage data recording. No-op if
  130. // startRecordingMemoryFootprintWithInterval was no called.
  131. - (void)stopRecordingMemoryFootprint;
  132. // Updates the currently available device storage, in kilobytes.
  133. - (void)updateAvailableDeviceStorage:(NSInteger)availableStorage;
  134. // Updates the saved last known session time.
  135. - (void)updateSessionEndTime;
  136. // Updates the saved last known battery level of the device.
  137. - (void)updateStoredBatteryLevel;
  138. // Updates the saved last known battery state of the device.
  139. - (void)updateStoredBatteryState;
  140. // Updates the saved last known thermal state of the device.
  141. - (void)updateStoredThermalState;
  142. // When a session has begun, records that a memory warning was received.
  143. - (void)setMemoryWarningFlag;
  144. // When a session has begun, records that any memory warning flagged can be
  145. // ignored.
  146. - (void)resetMemoryWarningFlag;
  147. // Adds |sessionID| to the list of connected sessions.
  148. - (void)addSceneSessionID:(NSString*)sessionID;
  149. // Removes |sessionID| from the list of connected sessions.
  150. - (void)removeSceneSessionID:(NSString*)sessionID;
  151. // Empties the list of connected session.
  152. - (void)resetConnectedSceneSessionIDs;
  153. // Must be called when Chrome starts session restoration. The returned closure
  154. // runner will clear up the flag when destroyed. Can be used on different
  155. // threads.
  156. - (base::ScopedClosureRunner)startSessionRestoration;
  157. // Must be called after reporting UTE metrics when app is started after UTE.
  158. // Automatically called when ScopedClosureRunner returned from -startRestoration
  159. // gets destructed.
  160. - (void)resetSessionRestorationFlag;
  161. // Records number of regular (non off the record) tabs.
  162. - (void)updateCurrentSessionTabCount:(NSInteger)count;
  163. // Records number of off the record tabs.
  164. - (void)updateCurrentSessionOTRTabCount:(NSInteger)count;
  165. // Records information crash report parameters.
  166. - (void)setReportParameterValue:(NSString*)value forKey:(NSString*)key;
  167. - (void)setReportParameterURL:(const GURL&)URL forKey:(NSString*)key;
  168. - (void)removeReportParameterForKey:(NSString*)key;
  169. @end
  170. #endif // COMPONENTS_PREVIOUS_SESSION_INFO_PREVIOUS_SESSION_INFO_H_