1 /*
2 * Copyright (c) 2021 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 "ams_mgr_scheduler.h"
17 #include <sys/types.h>
18
19 #include "datetime_ex.h"
20 #include "ipc_skeleton.h"
21 #include "system_ability_definition.h"
22
23 #include "app_death_recipient.h"
24 #include "app_log_wrapper.h"
25 #include "app_mgr_constants.h"
26 #include "perf_profile.h"
27
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 const std::string TASK_LOAD_ABILITY = "LoadAbilityTask";
32 const std::string TASK_TERMINATE_ABILITY = "TerminateAbilityTask";
33 const std::string TASK_UPDATE_ABILITY_STATE = "UpdateAbilityStateTask";
34 const std::string TASK_REGISTER_APP_STATE_CALLBACK = "RegisterAppStateCallbackTask";
35 const std::string TASK_STOP_ALL_PROCESS = "StopAllProcessTask";
36 const std::string TASK_ABILITY_BEHAVIOR_ANALYSIS = "AbilityBehaviorAnalysisTask";
37 const std::string TASK_KILL_PROCESS_BY_ABILITYTOKEN = "KillProcessByAbilityTokenTask";
38 const std::string TASK_KILL_APPLICATION = "KillApplicationTask";
39 }; // namespace
40
AmsMgrScheduler(const std::shared_ptr<AppMgrServiceInner> & mgrServiceInner_,const std::shared_ptr<AMSEventHandler> & handler_)41 AmsMgrScheduler::AmsMgrScheduler(
42 const std::shared_ptr<AppMgrServiceInner> &mgrServiceInner_, const std::shared_ptr<AMSEventHandler> &handler_)
43 : amsMgrServiceInner_(mgrServiceInner_), amsHandler_(handler_)
44 {}
45
~AmsMgrScheduler()46 AmsMgrScheduler::~AmsMgrScheduler()
47 {
48 APP_LOGI("AmsMgrScheduler instance destroyed");
49 }
50
LoadAbility(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & preToken,const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<ApplicationInfo> & appInfo)51 void AmsMgrScheduler::LoadAbility(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
52 const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<ApplicationInfo> &appInfo)
53 {
54 if (!abilityInfo || !appInfo) {
55 APP_LOGE("param error");
56 return;
57 }
58
59 if (!IsReady()) {
60 return;
61 }
62 PerfProfile::GetInstance().SetAbilityLoadStartTime(GetTickCount());
63 std::function <void()> loadAbilityFunc =
64 std::bind(&AppMgrServiceInner::LoadAbility, amsMgrServiceInner_, token, preToken, abilityInfo, appInfo);
65
66 amsHandler_->PostTask(loadAbilityFunc, TASK_LOAD_ABILITY);
67 }
68
UpdateAbilityState(const sptr<IRemoteObject> & token,const AbilityState state)69 void AmsMgrScheduler::UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state)
70 {
71 if (!IsReady()) {
72 return;
73 }
74 std::function <void()> updateAbilityStateFunc =
75 std::bind(&AppMgrServiceInner::UpdateAbilityState, amsMgrServiceInner_, token, state);
76 amsHandler_->PostTask(updateAbilityStateFunc, TASK_UPDATE_ABILITY_STATE);
77 }
78
TerminateAbility(const sptr<IRemoteObject> & token)79 void AmsMgrScheduler::TerminateAbility(const sptr<IRemoteObject> &token)
80 {
81 if (!IsReady()) {
82 return;
83 }
84 std::function <void()> terminateAbilityFunc =
85 std::bind(&AppMgrServiceInner::TerminateAbility, amsMgrServiceInner_, token);
86 amsHandler_->PostTask(terminateAbilityFunc, TASK_TERMINATE_ABILITY);
87 }
88
RegisterAppStateCallback(const sptr<IAppStateCallback> & callback)89 void AmsMgrScheduler::RegisterAppStateCallback(const sptr<IAppStateCallback> &callback)
90 {
91 if (!IsReady()) {
92 return;
93 }
94 std::function <void()> registerAppStateCallbackFunc =
95 std::bind(&AppMgrServiceInner::RegisterAppStateCallback, amsMgrServiceInner_, callback);
96 amsHandler_->PostTask(registerAppStateCallbackFunc, TASK_REGISTER_APP_STATE_CALLBACK);
97 }
98
Reset()99 void AmsMgrScheduler::Reset()
100 {
101 if (!IsReady()) {
102 return;
103 }
104 std::function <void()> resetFunc = std::bind(&AppMgrServiceInner::StopAllProcess, amsMgrServiceInner_);
105 amsHandler_->PostTask(resetFunc, TASK_STOP_ALL_PROCESS);
106 }
107
AbilityBehaviorAnalysis(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & preToken,const int32_t visibility,const int32_t perceptibility,const int32_t connectionState)108 void AmsMgrScheduler::AbilityBehaviorAnalysis(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
109 const int32_t visibility, const int32_t perceptibility, const int32_t connectionState)
110 {
111 if (!IsReady()) {
112 return;
113 }
114 std::function <void()> abilityBehaviorAnalysisFunc = std::bind(&AppMgrServiceInner::AbilityBehaviorAnalysis,
115 amsMgrServiceInner_,
116 token,
117 preToken,
118 visibility,
119 perceptibility,
120 connectionState);
121 amsHandler_->PostTask(abilityBehaviorAnalysisFunc, TASK_ABILITY_BEHAVIOR_ANALYSIS);
122 }
123
KillProcessByAbilityToken(const sptr<IRemoteObject> & token)124 void AmsMgrScheduler::KillProcessByAbilityToken(const sptr<IRemoteObject> &token)
125 {
126 if (!IsReady()) {
127 return;
128 }
129 std::function <void()> killProcessByAbilityTokenFunc =
130 std::bind(&AppMgrServiceInner::KillProcessByAbilityToken, amsMgrServiceInner_, token);
131 amsHandler_->PostTask(killProcessByAbilityTokenFunc, TASK_KILL_PROCESS_BY_ABILITYTOKEN);
132 }
133
AbilityAttachTimeOut(const sptr<IRemoteObject> & token)134 void AmsMgrScheduler::AbilityAttachTimeOut(const sptr<IRemoteObject> &token)
135 {
136 APP_LOGI("AmsMgrScheduler AttachTimeOut begin");
137 if (!IsReady()) {
138 return;
139 }
140 auto task = [=]() { amsMgrServiceInner_->HandleAbilityAttachTimeOut(token); };
141 amsHandler_->PostTask(task);
142 APP_LOGI("AmsMgrScheduler AttachTimeOut end");
143 }
144
KillApplication(const std::string & bundleName)145 int32_t AmsMgrScheduler::KillApplication(const std::string &bundleName)
146 {
147 if (!IsReady()) {
148 return ERR_INVALID_OPERATION;
149 }
150 return amsMgrServiceInner_->KillApplication(bundleName);
151 }
152
IsReady() const153 bool AmsMgrScheduler::IsReady() const
154 {
155 if (!amsMgrServiceInner_) {
156 APP_LOGE("amsMgrServiceInner_ is null");
157 return false;
158 }
159 if (!amsHandler_) {
160 APP_LOGE("amsHandler_ is null");
161 return false;
162 }
163 return true;
164 }
165
CompelVerifyPermission(const std::string & permission,int pid,int uid,std::string & message)166 int AmsMgrScheduler::CompelVerifyPermission(const std::string &permission, int pid, int uid, std::string &message)
167 {
168 if (!IsReady()) {
169 return ERR_INVALID_OPERATION;
170 }
171 return amsMgrServiceInner_->CompelVerifyPermission(permission, pid, uid, message);
172 }
173 } // namespace AppExecFwk
174 } // namespace OHOS