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_EVENT_HANDLER_H
17 #define OHOS_SYSTEM_ABILITY_MANAGER_SYSTEM_ABILITY_EVENT_HANDLER_H
18 
19 #include "schedule/system_ability_state_machine.h"
20 
21 namespace OHOS {
22 struct ProcessInfo {
23     std::u16string processName;
24     int32_t pid = -1;
25     int32_t uid = -1;
26 };
27 
28 enum class AbilityStateEvent {
29     ABILITY_LOAD_SUCCESS_EVENT = 0,
30     ABILITY_LOAD_FAILED_EVENT,
31     ABILITY_UNLOAD_SUCCESS_EVENT,
32 };
33 
34 enum class ProcessStateEvent {
35     PROCESS_STARTED_EVENT = 0,
36     PROCESS_STOPPED_EVENT,
37 };
38 
39 class SystemAbilityEventHandler {
40 public:
41     explicit SystemAbilityEventHandler(const std::shared_ptr<SystemAbilityStateMachine>& stateMachine);
42     int32_t HandleAbilityEventLocked(const std::shared_ptr<SystemAbilityContext>& context, AbilityStateEvent event);
43     int32_t HandleProcessEventLocked(const std::shared_ptr<SystemProcessContext>& context,
44         const ProcessInfo& processInfo, ProcessStateEvent event);
45 private:
46     void InitEventHandlerMap();
47     int32_t HandleAbilityLoadFailedEventLocked(const std::shared_ptr<SystemAbilityContext>& context);
48     int32_t HandleAbilityLoadSuccessEventLocked(const std::shared_ptr<SystemAbilityContext>& context);
49     int32_t HandleAbilityUnLoadSuccessEventLocked(const std::shared_ptr<SystemAbilityContext>& context);
50     int32_t HandleProcessStartedEventLocked(const std::shared_ptr<SystemProcessContext>& context,
51         const ProcessInfo& processInfo);
52     int32_t HandleProcessStoppedEventLocked(const std::shared_ptr<SystemProcessContext>& context,
53         const ProcessInfo& processInfo);
54     using AbilityEventHandlerFunc =
55         int32_t(SystemAbilityEventHandler::*)(const std::shared_ptr<SystemAbilityContext>& context);
56     using ProcessEventHandlerFunc =
57         int32_t(SystemAbilityEventHandler::*)(const std::shared_ptr<SystemProcessContext>& context,
58         const ProcessInfo& processInfo);
59     std::shared_ptr<SystemAbilityStateMachine> stateMachine_;
60     std::map<AbilityStateEvent, AbilityEventHandlerFunc> abilityEventHandlerMap_;
61     std::map<ProcessStateEvent, ProcessEventHandlerFunc> processEventHandlerMap_;
62 };
63 } // namespace OHOS
64 
65 #endif // !defined(OHOS_SYSTEM_ABILITY_MANAGER_SYSTEM_ABILITY_EVENT_HANDLER_H)