| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 // ====== New Architecture ===== | |
| 6 // = This code is only used in the new iOS Chrome architecture. = | |
| 7 // ============================================================================ | |
| 8 | |
| 9 #import "ios/chrome/app/application_state.h" | |
| 10 | |
| 11 #include <memory> | |
| 12 | |
| 13 #include "base/logging.h" | |
| 14 #include "base/memory/ptr_util.h" | |
| 15 #include "base/supports_user_data.h" | |
| 16 #import "ios/chrome/app/application_step.h" | |
| 17 | |
| 18 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 19 #error "This file requires ARC support." | |
| 20 #endif | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Specialization of base::SupportsUserData for use in this class. | |
| 25 class PersistentApplicationState : public base::SupportsUserData { | |
| 26 public: | |
| 27 ~PersistentApplicationState() override {} | |
| 28 }; | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 @interface ApplicationState () | |
| 33 @property(nonatomic, readwrite, copy) NSDictionary* launchOptions; | |
| 34 @end | |
| 35 | |
| 36 @implementation ApplicationState { | |
| 37 std::unique_ptr<PersistentApplicationState> _persistentState; | |
| 38 } | |
| 39 | |
| 40 @synthesize application = _application; | |
| 41 @synthesize browserState = _browserState; | |
| 42 @synthesize URLOpener = _URLOpener; | |
| 43 @synthesize phase = _phase; | |
| 44 @synthesize window = _window; | |
| 45 @synthesize launchSteps = _launchSteps; | |
| 46 @synthesize terminationSteps = _terminationSteps; | |
| 47 @synthesize backgroundSteps = _backgroundSteps; | |
| 48 @synthesize foregroundSteps = _foregroundSteps; | |
| 49 @synthesize launchOptions = _launchOptions; | |
| 50 | |
| 51 #pragma mark - Object lifecycle | |
| 52 | |
| 53 - (instancetype)init { | |
| 54 if ((self = [super init])) { | |
| 55 _phase = APPLICATION_COLD; | |
| 56 _launchSteps = [ApplicationStepArray array]; | |
| 57 _terminationSteps = [ApplicationStepArray array]; | |
| 58 _backgroundSteps = [ApplicationStepArray array]; | |
| 59 _foregroundSteps = [ApplicationStepArray array]; | |
| 60 _persistentState = base::MakeUnique<PersistentApplicationState>(); | |
| 61 } | |
| 62 return self; | |
| 63 } | |
| 64 | |
| 65 #pragma mark - Public API | |
| 66 | |
| 67 - (base::SupportsUserData*)persistentState { | |
| 68 return _persistentState.get(); | |
| 69 } | |
| 70 | |
| 71 - (void)launchWithOptions:(NSDictionary*)launchOptions { | |
| 72 self.launchOptions = launchOptions; | |
| 73 [self continueLaunch]; | |
| 74 } | |
| 75 | |
| 76 - (void)continueLaunch { | |
| 77 [self runSteps:self.launchSteps]; | |
| 78 } | |
| 79 | |
| 80 - (void)terminate { | |
| 81 CHECK(self.phase != APPLICATION_TERMINATING); | |
| 82 self.phase = APPLICATION_TERMINATING; | |
| 83 [self runSteps:self.terminationSteps]; | |
| 84 CHECK(self.terminationSteps.count == 0); | |
| 85 } | |
| 86 | |
| 87 - (void)background { | |
| 88 [self runSteps:self.backgroundSteps]; | |
| 89 } | |
| 90 | |
| 91 - (void)foreground { | |
| 92 [self runSteps:self.foregroundSteps]; | |
| 93 } | |
| 94 | |
| 95 #pragma mark - Running steps | |
| 96 | |
| 97 // While the first step in |steps| can run in |self|, pop it, run it, and | |
| 98 // release ownership of it. | |
| 99 - (void)runSteps:(ApplicationStepArray*)steps { | |
| 100 while ([steps.firstObject canRunInState:self]) { | |
| 101 id<ApplicationStep> nextStep = steps.firstObject; | |
| 102 [steps removeObject:nextStep]; | |
| 103 // |nextStep| should not be in |steps| when -runInState is called. | |
| 104 // (Some steps may re-insert themselves into |steps|, for example). | |
| 105 DCHECK(![steps containsObject:nextStep]); | |
| 106 [nextStep runInState:self]; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 @end | |
| OLD | NEW |