1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_SYSTEM_ABILITY_MANAGER_SYSTEM_ABILITY_STATE_MACHINE_H 17 #define OHOS_SYSTEM_ABILITY_MANAGER_SYSTEM_ABILITY_STATE_MACHINE_H 18 19 #include <map> 20 #include <memory> 21 #include <string> 22 23 #include "schedule/system_ability_state_context.h" 24 #include "schedule/system_ability_state_listener.h" 25 26 namespace OHOS { 27 class SystemAbilityStateHandler { 28 public: SystemAbilityStateHandler(const std::shared_ptr<SystemAbilityStateListener> & listener)29 explicit SystemAbilityStateHandler(const std::shared_ptr<SystemAbilityStateListener>& listener) 30 : listener_(listener) {}; ~SystemAbilityStateHandler()31 virtual ~SystemAbilityStateHandler() {}; 32 virtual bool CanEnter(SystemAbilityState fromState) = 0; 33 virtual void OnEnter(const std::shared_ptr<SystemAbilityContext>& context) = 0; 34 protected: 35 std::weak_ptr<SystemAbilityStateListener> listener_; 36 }; 37 38 class NotLoadedStateHandler : public SystemAbilityStateHandler { 39 public: NotLoadedStateHandler(const std::shared_ptr<SystemAbilityStateListener> & listener)40 explicit NotLoadedStateHandler(const std::shared_ptr<SystemAbilityStateListener>& listener) 41 : SystemAbilityStateHandler(listener) {}; 42 bool CanEnter(SystemAbilityState fromState) override; 43 void OnEnter(const std::shared_ptr<SystemAbilityContext>& context) override; 44 }; 45 46 class LoadingStateHandler : public SystemAbilityStateHandler { 47 public: LoadingStateHandler(const std::shared_ptr<SystemAbilityStateListener> & listener)48 explicit LoadingStateHandler(const std::shared_ptr<SystemAbilityStateListener>& listener) 49 : SystemAbilityStateHandler(listener) {}; 50 bool CanEnter(SystemAbilityState fromState) override; 51 void OnEnter(const std::shared_ptr<SystemAbilityContext>& context) override; 52 }; 53 54 class LoadedStateHandler : public SystemAbilityStateHandler { 55 public: LoadedStateHandler(const std::shared_ptr<SystemAbilityStateListener> & listener)56 explicit LoadedStateHandler(const std::shared_ptr<SystemAbilityStateListener>& listener) 57 : SystemAbilityStateHandler(listener) {}; 58 bool CanEnter(SystemAbilityState fromState) override; 59 void OnEnter(const std::shared_ptr<SystemAbilityContext>& context) override; 60 }; 61 62 class UnloadableStateHandler : public SystemAbilityStateHandler { 63 public: UnloadableStateHandler(const std::shared_ptr<SystemAbilityStateListener> & listener)64 explicit UnloadableStateHandler(const std::shared_ptr<SystemAbilityStateListener>& listener) 65 : SystemAbilityStateHandler(listener) {}; 66 bool CanEnter(SystemAbilityState fromState) override; 67 void OnEnter(const std::shared_ptr<SystemAbilityContext>& context) override; 68 }; 69 70 class UnloadingStateHandler : public SystemAbilityStateHandler { 71 public: UnloadingStateHandler(const std::shared_ptr<SystemAbilityStateListener> & listener)72 explicit UnloadingStateHandler(const std::shared_ptr<SystemAbilityStateListener>& listener) 73 : SystemAbilityStateHandler(listener) {}; 74 bool CanEnter(SystemAbilityState fromState) override; 75 void OnEnter(const std::shared_ptr<SystemAbilityContext>& context) override; 76 }; 77 78 class SystemProcessStateHandler { 79 public: SystemProcessStateHandler(const std::shared_ptr<SystemAbilityStateListener> & listener)80 explicit SystemProcessStateHandler(const std::shared_ptr<SystemAbilityStateListener>& listener) 81 : listener_(listener) {}; ~SystemProcessStateHandler()82 virtual ~SystemProcessStateHandler() {}; 83 virtual bool CanEnter(SystemProcessState fromState) = 0; 84 virtual void OnEnter(const std::shared_ptr<SystemProcessContext>& context) = 0; 85 protected: 86 std::weak_ptr<SystemAbilityStateListener> listener_; 87 }; 88 89 class NotStartedStateHandler : public SystemProcessStateHandler { 90 public: NotStartedStateHandler(const std::shared_ptr<SystemAbilityStateListener> & listener)91 explicit NotStartedStateHandler(const std::shared_ptr<SystemAbilityStateListener>& listener) 92 : SystemProcessStateHandler(listener) {}; 93 bool CanEnter(SystemProcessState fromState) override; 94 void OnEnter(const std::shared_ptr<SystemProcessContext>& context) override; 95 }; 96 97 class StartedStateHandler : public SystemProcessStateHandler { 98 public: StartedStateHandler(const std::shared_ptr<SystemAbilityStateListener> & listener)99 explicit StartedStateHandler(const std::shared_ptr<SystemAbilityStateListener>& listener) 100 : SystemProcessStateHandler(listener) {}; 101 bool CanEnter(SystemProcessState fromState) override; 102 void OnEnter(const std::shared_ptr<SystemProcessContext>& context) override; 103 }; 104 105 class StoppingStateHandler : public SystemProcessStateHandler { 106 public: StoppingStateHandler(const std::shared_ptr<SystemAbilityStateListener> & listener)107 explicit StoppingStateHandler(const std::shared_ptr<SystemAbilityStateListener>& listener) 108 : SystemProcessStateHandler(listener) {}; 109 bool CanEnter(SystemProcessState fromState) override; 110 void OnEnter(const std::shared_ptr<SystemProcessContext>& context) override; 111 }; 112 113 class SystemAbilityStateMachine { 114 public: 115 explicit SystemAbilityStateMachine(const std::shared_ptr<SystemAbilityStateListener>& listener); 116 int32_t AbilityStateTransitionLocked(const std::shared_ptr<SystemAbilityContext>& context, 117 SystemAbilityState state); 118 int32_t ProcessStateTransitionLocked(const std::shared_ptr<SystemProcessContext>& context, 119 SystemProcessState state); 120 private: 121 void InitStateHandlerMap(const std::shared_ptr<SystemAbilityStateListener>& listener); 122 bool UpdateStateCount(const std::shared_ptr<SystemProcessContext>& context, 123 SystemAbilityState fromState, SystemAbilityState toState); 124 std::map<SystemAbilityState, std::shared_ptr<SystemAbilityStateHandler>> abilityStateHandlerMap_; 125 std::map<SystemProcessState, std::shared_ptr<SystemProcessStateHandler>> processStateHandlerMap_; 126 }; 127 } // namespace OHOS 128 129 #endif // !defined(OHOS_SYSTEM_ABILITY_MANAGER_SYSTEM_ABILITY_STATE_MACHINE_H)