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 #include "sam_log.h"
17 #include "string_ex.h"
18 #include "schedule/system_ability_event_handler.h"
19
20 namespace OHOS {
SystemAbilityEventHandler(const std::shared_ptr<SystemAbilityStateMachine> & stateMachine)21 SystemAbilityEventHandler::SystemAbilityEventHandler(const std::shared_ptr<SystemAbilityStateMachine>& stateMachine)
22 {
23 stateMachine_ = stateMachine;
24 InitEventHandlerMap();
25 }
26
InitEventHandlerMap()27 void SystemAbilityEventHandler::InitEventHandlerMap()
28 {
29 abilityEventHandlerMap_[AbilityStateEvent::ABILITY_LOAD_FAILED_EVENT] =
30 &SystemAbilityEventHandler::HandleAbilityLoadFailedEventLocked;
31 abilityEventHandlerMap_[AbilityStateEvent::ABILITY_LOAD_SUCCESS_EVENT] =
32 &SystemAbilityEventHandler::HandleAbilityLoadSuccessEventLocked;
33 abilityEventHandlerMap_[AbilityStateEvent::ABILITY_UNLOAD_SUCCESS_EVENT] =
34 &SystemAbilityEventHandler::HandleAbilityUnLoadSuccessEventLocked;
35 processEventHandlerMap_[ProcessStateEvent::PROCESS_STARTED_EVENT] =
36 &SystemAbilityEventHandler::HandleProcessStartedEventLocked;
37 processEventHandlerMap_[ProcessStateEvent::PROCESS_STOPPED_EVENT] =
38 &SystemAbilityEventHandler::HandleProcessStoppedEventLocked;
39 }
40
HandleAbilityEventLocked(const std::shared_ptr<SystemAbilityContext> & context,AbilityStateEvent event)41 int32_t SystemAbilityEventHandler::HandleAbilityEventLocked(const std::shared_ptr<SystemAbilityContext>& context,
42 AbilityStateEvent event)
43 {
44 if (context == nullptr) {
45 HILOGE("Scheduler:context is null");
46 return ERR_INVALID_VALUE;
47 }
48 HILOGD("Scheduler SA:%{public}d handle SA event %{public}d start",
49 context->systemAbilityId, event);
50 auto iter = abilityEventHandlerMap_.find(event);
51 if (iter != abilityEventHandlerMap_.end()) {
52 auto func = iter->second;
53 if (func != nullptr) {
54 return (this->*func)(context);
55 }
56 }
57 HILOGE("Scheduler SA:%{public}d invalid SA event %{public}d", context->systemAbilityId, event);
58 return ERR_INVALID_VALUE;
59 }
60
HandleProcessEventLocked(const std::shared_ptr<SystemProcessContext> & context,const ProcessInfo & processInfo,ProcessStateEvent event)61 int32_t SystemAbilityEventHandler::HandleProcessEventLocked(const std::shared_ptr<SystemProcessContext>& context,
62 const ProcessInfo& processInfo, ProcessStateEvent event)
63 {
64 if (context == nullptr) {
65 HILOGE("Scheduler:context is null");
66 return ERR_INVALID_VALUE;
67 }
68 HILOGD("Scheduler proc:%{public}s handle proc event %{public}d start",
69 Str16ToStr8(context->processName).c_str(), event);
70 auto iter = processEventHandlerMap_.find(event);
71 if (iter != processEventHandlerMap_.end()) {
72 auto func = iter->second;
73 if (func != nullptr) {
74 return (this->*func)(context, processInfo);
75 }
76 }
77 HILOGE("Scheduler proc:%{public}s invalid proc event %{public}d",
78 Str16ToStr8(context->processName).c_str(), event);
79 return ERR_INVALID_VALUE;
80 }
81
HandleAbilityLoadFailedEventLocked(const std::shared_ptr<SystemAbilityContext> & context)82 int32_t SystemAbilityEventHandler::HandleAbilityLoadFailedEventLocked(
83 const std::shared_ptr<SystemAbilityContext>& context)
84 {
85 HILOGI("Scheduler SA:%{public}d handle load fail event", context->systemAbilityId);
86 int32_t result = ERR_OK;
87 switch (context->state) {
88 case SystemAbilityState::LOADING:
89 if (context->pendingEvent == PendingEvent::UNLOAD_ABILITY_EVENT) {
90 HILOGI("Scheduler SA:%{public}d rm pending unload event", context->systemAbilityId);
91 context->pendingEvent = PendingEvent::NO_EVENT;
92 }
93 result = stateMachine_->AbilityStateTransitionLocked(context, SystemAbilityState::NOT_LOADED);
94 break;
95 default:
96 result = ERR_INVALID_VALUE;
97 HILOGE("Scheduler SA:%{public}d in state %{public}d,can't handle load fail event",
98 context->systemAbilityId, context->state);
99 break;
100 }
101 return result;
102 }
103
HandleAbilityLoadSuccessEventLocked(const std::shared_ptr<SystemAbilityContext> & context)104 int32_t SystemAbilityEventHandler::HandleAbilityLoadSuccessEventLocked(
105 const std::shared_ptr<SystemAbilityContext>& context)
106 {
107 HILOGI("Scheduler SA:%{public}d handle load suc event", context->systemAbilityId);
108 int32_t result = ERR_OK;
109 switch (context->state) {
110 case SystemAbilityState::NOT_LOADED:
111 case SystemAbilityState::LOADING:
112 result = stateMachine_->AbilityStateTransitionLocked(context, SystemAbilityState::LOADED);
113 break;
114 default:
115 result = ERR_INVALID_VALUE;
116 HILOGE("Scheduler SA:%{public}d in state %{public}d,can't handle load suc event",
117 context->systemAbilityId, context->state);
118 break;
119 }
120 return result;
121 }
122
HandleAbilityUnLoadSuccessEventLocked(const std::shared_ptr<SystemAbilityContext> & context)123 int32_t SystemAbilityEventHandler::HandleAbilityUnLoadSuccessEventLocked(
124 const std::shared_ptr<SystemAbilityContext>& context)
125 {
126 HILOGI("Scheduler SA:%{public}d handle unload suc event", context->systemAbilityId);
127 int32_t result = ERR_OK;
128 switch (context->state) {
129 case SystemAbilityState::LOADING:
130 if (context->pendingEvent == PendingEvent::UNLOAD_ABILITY_EVENT) {
131 HILOGI("Scheduler SA:%{public}d rm pending unload event", context->systemAbilityId);
132 context->pendingEvent = PendingEvent::NO_EVENT;
133 }
134 result = stateMachine_->AbilityStateTransitionLocked(context, SystemAbilityState::NOT_LOADED);
135 break;
136 case SystemAbilityState::LOADED:
137 case SystemAbilityState::UNLOADABLE:
138 case SystemAbilityState::UNLOADING:
139 result = stateMachine_->AbilityStateTransitionLocked(context, SystemAbilityState::NOT_LOADED);
140 break;
141 default:
142 result = ERR_INVALID_VALUE;
143 HILOGE("Scheduler SA:%{public}d in state %{public}d,not need handle unload suc event",
144 context->systemAbilityId, context->state);
145 break;
146 }
147 return result;
148 }
149
HandleProcessStartedEventLocked(const std::shared_ptr<SystemProcessContext> & context,const ProcessInfo & processInfo)150 int32_t SystemAbilityEventHandler::HandleProcessStartedEventLocked(
151 const std::shared_ptr<SystemProcessContext>& context, const ProcessInfo& processInfo)
152 {
153 HILOGI("Scheduler proc:%{public}s handle started event", Str16ToStr8(context->processName).c_str());
154 context->pid = processInfo.pid;
155 context->uid = processInfo.uid;
156 int32_t result = ERR_OK;
157 switch (context->state) {
158 case SystemProcessState::NOT_STARTED:
159 result = stateMachine_->ProcessStateTransitionLocked(context, SystemProcessState::STARTED);
160 break;
161 default:
162 result = ERR_INVALID_VALUE;
163 HILOGE("Scheduler proc:%{public}s in state %{public}d,not need handle started event",
164 Str16ToStr8(context->processName).c_str(), context->state);
165 break;
166 }
167 return result;
168 }
169
HandleProcessStoppedEventLocked(const std::shared_ptr<SystemProcessContext> & context,const ProcessInfo & processInfo)170 int32_t SystemAbilityEventHandler::HandleProcessStoppedEventLocked(
171 const std::shared_ptr<SystemProcessContext>& context, const ProcessInfo& processInfo)
172 {
173 HILOGI("Scheduler proc:%{public}s handle stopped event", Str16ToStr8(context->processName).c_str());
174 int32_t result = ERR_OK;
175 switch (context->state) {
176 case SystemProcessState::STARTED:
177 case SystemProcessState::STOPPING:
178 result = stateMachine_->ProcessStateTransitionLocked(context, SystemProcessState::NOT_STARTED);
179 break;
180 default:
181 result = ERR_INVALID_VALUE;
182 HILOGE("Scheduler proc:%{public}s in state %{public}d,not need handle stopped event",
183 Str16ToStr8(context->processName).c_str(), context->state);
184 break;
185 }
186 return result;
187 }
188 }