safe_mode_app_state_agent.mm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2021 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. #import "ios/chrome/app/safe_mode_app_state_agent.h"
  5. #include "base/ios/ios_util.h"
  6. #include "base/version.h"
  7. #import "ios/chrome/app/application_delegate/app_state.h"
  8. #import "ios/chrome/app/application_delegate/app_state_observer.h"
  9. #import "ios/chrome/browser/ui/main/scene_state.h"
  10. #import "ios/chrome/browser/ui/main/scene_state_observer.h"
  11. #import "ios/chrome/browser/ui/safe_mode/safe_mode_coordinator.h"
  12. #import "ios/chrome/browser/ui/scoped_ui_blocker/scoped_ui_blocker.h"
  13. #if !defined(__has_feature) || !__has_feature(objc_arc)
  14. #error "This file requires ARC support."
  15. #endif
  16. @interface SafeModeAppAgent () <SafeModeCoordinatorDelegate,
  17. AppStateObserver,
  18. SceneStateObserver> {
  19. // Multiwindow UI blocker used when safe mode is active to only show the safe
  20. // mode UI on one window.
  21. std::unique_ptr<ScopedUIBlocker> _safeModeBlocker;
  22. }
  23. // This flag is set when the first scene has activated since the startup, and
  24. // never reset.
  25. @property(nonatomic, assign) BOOL firstSceneHasActivated;
  26. // The app state the agent is connected to.
  27. @property(nonatomic, weak, readonly) AppState* appState;
  28. // Safe mode coordinator. If this is non-nil, the app is displaying the safe
  29. // mode UI.
  30. @property(nonatomic, strong) SafeModeCoordinator* safeModeCoordinator;
  31. @end
  32. @implementation SafeModeAppAgent
  33. #pragma mark - AppStateAgent
  34. - (void)setAppState:(AppState*)appState {
  35. // This should only be called once!
  36. DCHECK(!_appState);
  37. _appState = appState;
  38. [appState addObserver:self];
  39. }
  40. #pragma mark - SafeModeCoordinatorDelegate Implementation
  41. - (void)coordinatorDidExitSafeMode:(SafeModeCoordinator*)coordinator {
  42. DCHECK(coordinator);
  43. [self stopSafeMode];
  44. // Transition out of Safe Mode init stage to the next stage.
  45. [self.appState queueTransitionToNextInitStage];
  46. }
  47. #pragma mark - SceneStateObserver
  48. - (void)sceneState:(SceneState*)sceneState
  49. transitionedToActivationLevel:(SceneActivationLevel)level {
  50. // Don't try to trigger Safe Mode when the scene is not yet active on the
  51. // foreground.
  52. if (level < SceneActivationLevelForegroundActive) {
  53. return;
  54. }
  55. // Don't try to trigger Safe Mode when the app has already passed the safe
  56. // mode stage when the scene transitions to foreground. If the init stage is
  57. // still Safe Mode at this moment it means that safe mode has to be triggered.
  58. if (self.appState.initStage != InitStageSafeMode) {
  59. return;
  60. }
  61. // Don't try to show the safe mode UI on multiple scenes; one scene is
  62. // sufficient.
  63. if (self.firstSceneHasActivated) {
  64. return;
  65. }
  66. self.firstSceneHasActivated = YES;
  67. [self startSafeMode:sceneState];
  68. }
  69. #pragma mark - AppStateObserver
  70. - (void)appState:(AppState*)appState
  71. didTransitionFromInitStage:(InitStage)previousInitStage {
  72. if (self.appState.initStage != InitStageSafeMode) {
  73. return;
  74. }
  75. // Iterate further in the init stages when safe mode isn't needed; stop
  76. // and switch the app to safe mode otherwise.
  77. if ([SafeModeCoordinator shouldStart]) {
  78. return;
  79. }
  80. [self.appState queueTransitionToNextInitStage];
  81. }
  82. - (void)appState:(AppState*)appState sceneConnected:(SceneState*)sceneState {
  83. [sceneState addObserver:self];
  84. }
  85. #pragma mark - Internals
  86. - (void)startSafeMode:(SceneState*)sceneState {
  87. DCHECK(sceneState);
  88. DCHECK(!_safeModeBlocker);
  89. self.safeModeCoordinator =
  90. [[SafeModeCoordinator alloc] initWithWindow:sceneState.window];
  91. self.safeModeCoordinator.delegate = self;
  92. // Activate the main window, which will prompt the views to load.
  93. [sceneState.window makeKeyAndVisible];
  94. [self.safeModeCoordinator start];
  95. if (base::ios::IsMultipleScenesSupported()) {
  96. _safeModeBlocker = std::make_unique<ScopedUIBlocker>(sceneState);
  97. }
  98. }
  99. - (void)stopSafeMode {
  100. if (_safeModeBlocker) {
  101. _safeModeBlocker.reset();
  102. }
  103. self.safeModeCoordinator = nil;
  104. }
  105. @end