• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 "app_scheduler.h"
17 
18 #include "ability_manager_service.h"
19 #include "ability_util.h"
20 #include "app_utils.h"
21 #include "hitrace_meter.h"
22 #include "param.h"
23 #include "utils/state_utils.h"
24 
25 namespace OHOS {
26 namespace AAFwk {
AppScheduler()27 AppScheduler::AppScheduler() : appMgrClient_(std::make_unique<AppExecFwk::AppMgrClient>())
28 {}
29 
~AppScheduler()30 AppScheduler::~AppScheduler()
31 {}
32 
Init(const std::weak_ptr<AppStateCallback> & callback)33 bool AppScheduler::Init(const std::weak_ptr<AppStateCallback> &callback)
34 {
35     CHECK_POINTER_RETURN_BOOL(callback.lock());
36     CHECK_POINTER_RETURN_BOOL(appMgrClient_);
37 
38     std::lock_guard<std::mutex> guard(lock_);
39     if (isInit_) {
40         return true;
41     }
42 
43     callback_ = callback;
44     /* because the errcode type of AppMgr Client API will be changed to int,
45      * so must to covert the return result  */
46     int result = static_cast<int>(appMgrClient_->ConnectAppMgrService());
47     if (result != ERR_OK) {
48         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to ConnectAppMgrService");
49         return false;
50     }
51     this->IncStrongRef(this);
52     result = static_cast<int>(appMgrClient_->RegisterAppStateCallback(sptr<AppScheduler>(this)));
53     if (result != ERR_OK) {
54         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to RegisterAppStateCallback");
55         return false;
56     }
57 
58     startSpecifiedAbilityResponse_ = new (std::nothrow) StartSpecifiedAbilityResponse();
59     if (startSpecifiedAbilityResponse_ == nullptr) {
60         TAG_LOGE(AAFwkTag::ABILITYMGR, "null startSpecifiedAbilityResponse_");
61         return false;
62     }
63     appMgrClient_->RegisterStartSpecifiedAbilityResponse(startSpecifiedAbilityResponse_);
64 
65     TAG_LOGI(AAFwkTag::ABILITYMGR, "success to ConnectAppMgrService");
66     isInit_ = true;
67     return true;
68 }
69 
LoadAbility(const AbilityRuntime::LoadParam & loadParam,const AppExecFwk::AbilityInfo & abilityInfo,const AppExecFwk::ApplicationInfo & applicationInfo,const Want & want)70 int AppScheduler::LoadAbility(const AbilityRuntime::LoadParam &loadParam, const AppExecFwk::AbilityInfo &abilityInfo,
71     const AppExecFwk::ApplicationInfo &applicationInfo, const Want &want)
72 {
73     if (AppUtils::GetInstance().IsForbidStart()) {
74         TAG_LOGW(AAFwkTag::ABILITYMGR, "forbid start: %{public}s", abilityInfo.bundleName.c_str());
75         return INNER_ERR;
76     }
77     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
78     TAG_LOGD(AAFwkTag::SERVICE_EXT, "called");
79     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
80     /* because the errcode type of AppMgr Client API will be changed to int,
81      * so must to covert the return result  */
82     int ret = static_cast<int>(IN_PROCESS_CALL(
83         appMgrClient_->LoadAbility(abilityInfo, applicationInfo, want, loadParam)));
84     if (ret != ERR_OK) {
85         TAG_LOGE(AAFwkTag::SERVICE_EXT, "AppScheduler fail to LoadAbility. ret %{public}d", ret);
86         return INNER_ERR;
87     }
88     return ERR_OK;
89 }
90 
TerminateAbility(const sptr<IRemoteObject> & token,bool clearMissionFlag)91 int AppScheduler::TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag)
92 {
93     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
94     TAG_LOGD(AAFwkTag::ABILITYMGR, "Terminate ability.");
95     CHECK_POINTER_AND_RETURN(appMgrClient_, ERR_NULL_APP_MGR_CLIENT);
96     /* because the errcode type of AppMgr Client API will be changed to int,
97      * so must to covert the return result  */
98     int ret = static_cast<int>(IN_PROCESS_CALL(appMgrClient_->TerminateAbility(token, clearMissionFlag)));
99     if (ret != ERR_OK) {
100         TAG_LOGE(AAFwkTag::ABILITYMGR, "AppScheduler fail to TerminateAbility. ret %{public}d", ret);
101         return ERR_APP_MGR_TERMINATTE_ABILITY_FAILED;
102     }
103     return ERR_OK;
104 }
105 
UpdateApplicationInfoInstalled(const std::string & bundleName,const int32_t uid,const std::string & moduleName,bool isPlugin)106 int AppScheduler::UpdateApplicationInfoInstalled(const std::string &bundleName, const int32_t uid,
107     const std::string &moduleName, bool isPlugin)
108 {
109     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
110     TAG_LOGD(AAFwkTag::ABILITYMGR, "Start to update the application info after new module installed.");
111     int ret = (int)appMgrClient_->UpdateApplicationInfoInstalled(bundleName, uid, moduleName, isPlugin);
112     if (ret != ERR_OK) {
113         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to UpdateApplicationInfoInstalled");
114         return INNER_ERR;
115     }
116 
117     return ERR_OK;
118 }
119 
MoveToForeground(const sptr<IRemoteObject> & token)120 void AppScheduler::MoveToForeground(const sptr<IRemoteObject> &token)
121 {
122     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
123     TAG_LOGD(AAFwkTag::ABILITYMGR, "Start to move the ability to foreground.");
124     CHECK_POINTER(appMgrClient_);
125     IN_PROCESS_CALL_WITHOUT_RET(
126         appMgrClient_->UpdateAbilityState(token, AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND));
127 }
128 
MoveToBackground(const sptr<IRemoteObject> & token)129 void AppScheduler::MoveToBackground(const sptr<IRemoteObject> &token)
130 {
131     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
132     TAG_LOGD(AAFwkTag::ABILITYMGR, "Move the app to background.");
133     CHECK_POINTER(appMgrClient_);
134     IN_PROCESS_CALL_WITHOUT_RET(
135         appMgrClient_->UpdateAbilityState(token, AppExecFwk::AbilityState::ABILITY_STATE_BACKGROUND));
136 }
137 
UpdateAbilityState(const sptr<IRemoteObject> & token,const AppExecFwk::AbilityState state)138 void AppScheduler::UpdateAbilityState(const sptr<IRemoteObject> &token, const AppExecFwk::AbilityState state)
139 {
140     TAG_LOGD(AAFwkTag::ABILITYMGR, "UpdateAbilityState.");
141     CHECK_POINTER(appMgrClient_);
142     IN_PROCESS_CALL_WITHOUT_RET(appMgrClient_->UpdateAbilityState(token, state));
143 }
144 
UpdateExtensionState(const sptr<IRemoteObject> & token,const AppExecFwk::ExtensionState state)145 void AppScheduler::UpdateExtensionState(const sptr<IRemoteObject> &token, const AppExecFwk::ExtensionState state)
146 {
147     if (AppUtils::GetInstance().IsForbidStart()) {
148         TAG_LOGW(AAFwkTag::ABILITYMGR, "forbid start UpdateExtensionState");
149         return;
150     }
151     TAG_LOGD(AAFwkTag::ABILITYMGR, "UpdateExtensionState.");
152     CHECK_POINTER(appMgrClient_);
153     IN_PROCESS_CALL_WITHOUT_RET(appMgrClient_->UpdateExtensionState(token, state));
154 }
155 
KillProcessByAbilityToken(const sptr<IRemoteObject> & token)156 void AppScheduler::KillProcessByAbilityToken(const sptr<IRemoteObject> &token)
157 {
158     TAG_LOGI(AAFwkTag::ABILITYMGR, "kill process");
159     CHECK_POINTER(appMgrClient_);
160     appMgrClient_->KillProcessByAbilityToken(token);
161 }
162 
KillProcessesByUserId(int32_t userId,bool isNeedSendAppSpawnMsg,sptr<AAFwk::IUserCallback> callback)163 void AppScheduler::KillProcessesByUserId(int32_t userId, bool isNeedSendAppSpawnMsg,
164     sptr<AAFwk::IUserCallback> callback)
165 {
166     TAG_LOGI(
167         AAFwkTag::ABILITYMGR, "user id: %{public}d isNeedSendAppSpawnMsg: %{public}d", userId, isNeedSendAppSpawnMsg);
168     CHECK_POINTER(appMgrClient_);
169     appMgrClient_->KillProcessesByUserId(userId, isNeedSendAppSpawnMsg, callback);
170 }
171 
KillProcessesByPids(const std::vector<int32_t> & pids,const std::string & reason,bool subProcess,bool isKillPrecedeStart)172 int32_t AppScheduler::KillProcessesByPids(const std::vector<int32_t> &pids, const std::string &reason, bool subProcess,
173     bool isKillPrecedeStart)
174 {
175     TAG_LOGI(AAFwkTag::ABILITYMGR, "call");
176     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
177     int32_t ret = appMgrClient_->KillProcessesByPids(pids, reason, subProcess, isKillPrecedeStart);
178     if (ret != ERR_OK) {
179         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to KillProcessesByPids");
180         return ret;
181     }
182     return ERR_OK;
183 }
184 
AttachPidToParent(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & callerToken)185 void AppScheduler::AttachPidToParent(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callerToken)
186 {
187     TAG_LOGI(AAFwkTag::ABILITYMGR, "call");
188     CHECK_POINTER(appMgrClient_);
189     appMgrClient_->AttachPidToParent(token, callerToken);
190 }
191 
ConvertToAppAbilityState(const int32_t state)192 AppAbilityState AppScheduler::ConvertToAppAbilityState(const int32_t state)
193 {
194     AppExecFwk::AbilityState abilityState = static_cast<AppExecFwk::AbilityState>(state);
195     switch (abilityState) {
196         case AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND: {
197             return AppAbilityState::ABILITY_STATE_FOREGROUND;
198         }
199         case AppExecFwk::AbilityState::ABILITY_STATE_BACKGROUND: {
200             return AppAbilityState::ABILITY_STATE_BACKGROUND;
201         }
202         default:
203             return AppAbilityState::ABILITY_STATE_UNDEFINED;
204     }
205 }
206 
GetAbilityState() const207 AppAbilityState AppScheduler::GetAbilityState() const
208 {
209     return appAbilityState_;
210 }
211 
OnAbilityRequestDone(const sptr<IRemoteObject> & token,const AppExecFwk::AbilityState state)212 void AppScheduler::OnAbilityRequestDone(const sptr<IRemoteObject> &token, const AppExecFwk::AbilityState state)
213 {
214     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
215     TAG_LOGD(AAFwkTag::ABILITYMGR, "state:%{public}d", static_cast<int32_t>(state));
216     auto callback = callback_.lock();
217     CHECK_POINTER(callback);
218     appAbilityState_ = ConvertToAppAbilityState(static_cast<int32_t>(state));
219     callback->OnAbilityRequestDone(token, static_cast<int32_t>(state));
220 }
221 
NotifyConfigurationChange(const AppExecFwk::Configuration & config,int32_t userId)222 void AppScheduler::NotifyConfigurationChange(const AppExecFwk::Configuration &config, int32_t userId)
223 {
224     auto callback = callback_.lock();
225     CHECK_POINTER(callback);
226     callback->NotifyConfigurationChange(config, userId);
227 }
228 
NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> & bundleInfos)229 void AppScheduler::NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos)
230 {
231     auto callback = callback_.lock();
232     CHECK_POINTER(callback);
233     callback->NotifyStartResidentProcess(bundleInfos);
234 }
235 
NotifyStartKeepAliveProcess(std::vector<AppExecFwk::BundleInfo> & bundleInfos)236 void AppScheduler::NotifyStartKeepAliveProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos)
237 {
238     auto callback = callback_.lock();
239     CHECK_POINTER(callback);
240     callback->NotifyStartKeepAliveProcess(bundleInfos);
241 }
242 
OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> & abilityTokens)243 void AppScheduler::OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens)
244 {
245     auto callback = callback_.lock();
246     CHECK_POINTER(callback);
247     callback->OnAppRemoteDied(abilityTokens);
248 }
249 
OnStartProcessFailed(const std::vector<sptr<IRemoteObject>> & abilityTokens)250 void AppScheduler::OnStartProcessFailed(const std::vector<sptr<IRemoteObject>> &abilityTokens)
251 {
252     auto callback = callback_.lock();
253     CHECK_POINTER(callback);
254     callback->OnStartProcessFailed(abilityTokens);
255 }
256 
OnCacheExitInfo(uint32_t accessTokenId,const AppExecFwk::RunningProcessInfo & exitInfo,const std::string & bundleName,const std::vector<std::string> & abilityNames,const std::vector<std::string> & uiExtensionNames)257 void AppScheduler::OnCacheExitInfo(uint32_t accessTokenId, const AppExecFwk::RunningProcessInfo &exitInfo,
258     const std::string &bundleName, const std::vector<std::string> &abilityNames,
259     const std::vector<std::string> &uiExtensionNames)
260 {
261     auto callback = callback_.lock();
262     CHECK_POINTER(callback);
263     callback->OnCacheExitInfo(accessTokenId, exitInfo, bundleName, abilityNames, uiExtensionNames);
264 }
265 
NotifyAppPreCache(int32_t pid,int32_t userId)266 void AppScheduler::NotifyAppPreCache(int32_t pid, int32_t userId)
267 {
268     auto callback = callback_.lock();
269     CHECK_POINTER(callback);
270     callback->NotifyAppPreCache(pid, userId);
271 }
272 
KillApplication(const std::string & bundleName,bool clearPageStack,int32_t appIndex)273 int AppScheduler::KillApplication(const std::string &bundleName, bool clearPageStack, int32_t appIndex)
274 {
275     TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
276     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
277     int ret = (int)appMgrClient_->KillApplication(bundleName, clearPageStack, appIndex);
278     if (ret != ERR_OK) {
279         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed kill app");
280         return INNER_ERR;
281     }
282 
283     return ERR_OK;
284 }
285 
ForceKillApplication(const std::string & bundleName,const int userId,const int appIndex)286 int AppScheduler::ForceKillApplication(const std::string &bundleName,
287     const int userId, const int appIndex)
288 {
289     TAG_LOGI(AAFwkTag::ABILITYMGR, "call");
290     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
291     int ret = (int)appMgrClient_->ForceKillApplication(bundleName, userId, appIndex);
292     if (ret != ERR_OK) {
293         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed force kill app");
294         return INNER_ERR;
295     }
296 
297     return ERR_OK;
298 }
299 
KillProcessesByAccessTokenId(const uint32_t accessTokenId)300 int AppScheduler::KillProcessesByAccessTokenId(const uint32_t accessTokenId)
301 {
302     TAG_LOGI(AAFwkTag::ABILITYMGR, "call");
303     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
304     int ret = (int)appMgrClient_->KillProcessesByAccessTokenId(accessTokenId);
305     if (ret != ERR_OK) {
306         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed force kill app");
307         return INNER_ERR;
308     }
309 
310     return ERR_OK;
311 }
312 
KillApplicationByUid(const std::string & bundleName,int32_t uid,const std::string & reason)313 int AppScheduler::KillApplicationByUid(const std::string &bundleName, int32_t uid,
314     const std::string& reason)
315 {
316     TAG_LOGI(AAFwkTag::ABILITYMGR, "[%{public}s(%{public}s)] enter", __FILE__, __FUNCTION__);
317     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
318     int ret = (int)appMgrClient_->KillApplicationByUid(bundleName, uid, reason);
319     if (ret != ERR_OK) {
320         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail kill app");
321         return INNER_ERR;
322     }
323 
324     return ERR_OK;
325 }
326 
NotifyUninstallOrUpgradeApp(const std::string & bundleName,int32_t uid,bool isUpgrade)327 int AppScheduler::NotifyUninstallOrUpgradeApp(const std::string &bundleName, int32_t uid,
328     bool isUpgrade)
329 {
330     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
331     int ret = (int)appMgrClient_->NotifyUninstallOrUpgradeApp(bundleName, uid, isUpgrade);
332     if (ret != ERR_OK) {
333         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail kill app");
334         return INNER_ERR;
335     }
336 
337     return ERR_OK;
338 }
339 
AttachTimeOut(const sptr<IRemoteObject> & token)340 void AppScheduler::AttachTimeOut(const sptr<IRemoteObject> &token)
341 {
342     CHECK_POINTER(appMgrClient_);
343     IN_PROCESS_CALL_WITHOUT_RET(appMgrClient_->AbilityAttachTimeOut(token));
344 }
345 
PrepareTerminate(const sptr<IRemoteObject> & token,bool clearMissionFlag)346 void AppScheduler::PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag)
347 {
348     CHECK_POINTER(appMgrClient_);
349     IN_PROCESS_CALL_WITHOUT_RET(appMgrClient_->PrepareTerminate(token, clearMissionFlag));
350 }
351 
OnAppStateChanged(const AppExecFwk::AppProcessData & appData)352 void AppScheduler::OnAppStateChanged(const AppExecFwk::AppProcessData &appData)
353 {
354     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
355     auto callback = callback_.lock();
356     CHECK_POINTER(callback);
357     AppInfo info;
358     for (const auto &list : appData.appDatas) {
359         AppData data;
360         data.appName = list.appName;
361         data.uid = list.uid;
362         info.appData.push_back(data);
363     }
364     info.processName = appData.processName;
365     info.state = static_cast<AppState>(appData.appState);
366     info.pid = appData.pid;
367     info.appIndex = appData.appIndex;
368     info.instanceKey = appData.instanceKey;
369     info.bundleName = appData.bundleName;
370     callback->OnAppStateChanged(info);
371 }
372 
GetRunningProcessInfoByToken(const sptr<IRemoteObject> & token,AppExecFwk::RunningProcessInfo & info)373 void AppScheduler::GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info)
374 {
375     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
376     CHECK_POINTER(appMgrClient_);
377     IN_PROCESS_CALL_WITHOUT_RET(appMgrClient_->GetRunningProcessInfoByToken(token, info));
378 }
379 
GetRunningProcessInfoByPid(const pid_t pid,OHOS::AppExecFwk::RunningProcessInfo & info) const380 void AppScheduler::GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info) const
381 {
382     CHECK_POINTER(appMgrClient_);
383     IN_PROCESS_CALL_WITHOUT_RET(appMgrClient_->GetRunningProcessInfoByPid(pid, info));
384 }
385 
GetRunningProcessInfoByChildProcessPid(const pid_t childPid,OHOS::AppExecFwk::RunningProcessInfo & info) const386 void AppScheduler::GetRunningProcessInfoByChildProcessPid(const pid_t childPid,
387     OHOS::AppExecFwk::RunningProcessInfo &info) const
388 {
389     CHECK_POINTER(appMgrClient_);
390     IN_PROCESS_CALL_WITHOUT_RET(appMgrClient_->GetRunningProcessInfoByChildProcessPid(childPid, info));
391 }
392 
SetAbilityForegroundingFlagToAppRecord(const pid_t pid) const393 void AppScheduler::SetAbilityForegroundingFlagToAppRecord(const pid_t pid) const
394 {
395     CHECK_POINTER(appMgrClient_);
396     IN_PROCESS_CALL_WITHOUT_RET(appMgrClient_->SetAbilityForegroundingFlagToAppRecord(pid));
397 }
398 
StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> & bundleInfos)399 void AppScheduler::StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos)
400 {
401     CHECK_POINTER(appMgrClient_);
402     appMgrClient_->StartupResidentProcess(bundleInfos);
403 }
404 
StartSpecifiedAbility(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,int32_t requestId)405 void AppScheduler::StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
406     int32_t requestId)
407 {
408     CHECK_POINTER(appMgrClient_);
409     IN_PROCESS_CALL_WITHOUT_RET(appMgrClient_->StartSpecifiedAbility(want, abilityInfo, requestId));
410 }
411 
OnAcceptWantResponse(const AAFwk::Want & want,const std::string & flag,int32_t requestId)412 void StartSpecifiedAbilityResponse::OnAcceptWantResponse(
413     const AAFwk::Want &want, const std::string &flag, int32_t requestId)
414 {
415     DelayedSingleton<AbilityManagerService>::GetInstance()->OnAcceptWantResponse(want, flag, requestId);
416 }
417 
PrepareTerminateApp(const pid_t pid,const std::string & moduleName)418 void AppScheduler::PrepareTerminateApp(const pid_t pid, const std::string &moduleName)
419 {
420     CHECK_POINTER(appMgrClient_);
421     IN_PROCESS_CALL_WITHOUT_RET(appMgrClient_->PrepareTerminateApp(pid, moduleName));
422 }
423 
OnTimeoutResponse(int32_t requestId)424 void StartSpecifiedAbilityResponse::OnTimeoutResponse(int32_t requestId)
425 {
426     DelayedSingleton<AbilityManagerService>::GetInstance()->OnStartSpecifiedAbilityTimeoutResponse(requestId);
427 }
428 
StartSpecifiedProcess(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,int32_t requestId,const std::string & customProcess)429 void AppScheduler::StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
430     int32_t requestId, const std::string &customProcess)
431 {
432     CHECK_POINTER(appMgrClient_);
433     IN_PROCESS_CALL_WITHOUT_RET(appMgrClient_->StartSpecifiedProcess(want, abilityInfo, requestId, customProcess));
434 }
435 
OnNewProcessRequestResponse(const std::string & flag,int32_t requestId,const std::string & callerProcessName)436 void StartSpecifiedAbilityResponse::OnNewProcessRequestResponse(const std::string &flag, int32_t requestId,
437     const std::string &callerProcessName)
438 {
439     DelayedSingleton<AbilityManagerService>::GetInstance()->OnStartSpecifiedProcessResponse(flag, requestId,
440         callerProcessName);
441 }
442 
OnNewProcessRequestTimeoutResponse(int32_t requestId)443 void StartSpecifiedAbilityResponse::OnNewProcessRequestTimeoutResponse(int32_t requestId)
444 {
445     DelayedSingleton<AbilityManagerService>::GetInstance()->OnStartSpecifiedProcessTimeoutResponse(requestId);
446 }
447 
OnStartSpecifiedFailed(int32_t requestId)448 void StartSpecifiedAbilityResponse::OnStartSpecifiedFailed(int32_t requestId)
449 {
450     DelayedSingleton<AbilityManagerService>::GetInstance()->OnStartSpecifiedFailed(requestId);
451 }
452 
GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> & info)453 int AppScheduler::GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> &info)
454 {
455     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
456     return static_cast<int>(appMgrClient_->GetAllRunningProcesses(info));
457 }
458 
GetProcessRunningInfosByUserId(std::vector<AppExecFwk::RunningProcessInfo> & info,int32_t userId)459 int AppScheduler::GetProcessRunningInfosByUserId(std::vector<AppExecFwk::RunningProcessInfo> &info, int32_t userId)
460 {
461     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
462     return static_cast<int>(appMgrClient_->GetProcessRunningInfosByUserId(info, userId));
463 }
464 
ConvertAppState(const AppState & state)465 std::string AppScheduler::ConvertAppState(const AppState &state)
466 {
467     return StateUtils::AppStateToStrMap(state);
468 }
469 
StartUserTest(const Want & want,const sptr<IRemoteObject> & observer,const AppExecFwk::BundleInfo & bundleInfo,int32_t userId)470 int AppScheduler::StartUserTest(
471     const Want &want, const sptr<IRemoteObject> &observer, const AppExecFwk::BundleInfo &bundleInfo, int32_t userId)
472 {
473     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
474     int ret = appMgrClient_->StartUserTestProcess(want, observer, bundleInfo, userId);
475     if (ret != ERR_OK) {
476         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed start user test");
477         return INNER_ERR;
478     }
479     return ERR_OK;
480 }
481 
FinishUserTest(const std::string & msg,const int64_t & resultCode,const std::string & bundleName)482 int AppScheduler::FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName)
483 {
484     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
485     int ret = appMgrClient_->FinishUserTest(msg, resultCode, bundleName);
486     if (ret != ERR_OK) {
487         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed start user test");
488         return INNER_ERR;
489     }
490     return ERR_OK;
491 }
492 
UpdateConfiguration(const AppExecFwk::Configuration & config)493 int AppScheduler::UpdateConfiguration(const AppExecFwk::Configuration &config)
494 {
495     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
496     auto ret = static_cast<int>(appMgrClient_->UpdateConfiguration(config));
497     if (ret != ERR_OK) {
498         TAG_LOGE(AAFwkTag::ABILITYMGR, "updateConfiguration failed");
499         return INNER_ERR;
500     }
501 
502     return ERR_OK;
503 }
504 
GetConfiguration(AppExecFwk::Configuration & config)505 int AppScheduler::GetConfiguration(AppExecFwk::Configuration &config)
506 {
507     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
508     auto ret = static_cast<int>(appMgrClient_->GetConfiguration(config));
509     if (ret != ERR_OK) {
510         TAG_LOGE(AAFwkTag::ABILITYMGR, "getConfiguration failed");
511         return INNER_ERR;
512     }
513 
514     return ERR_OK;
515 }
516 
GetAbilityRecordsByProcessID(const int pid,std::vector<sptr<IRemoteObject>> & tokens)517 int AppScheduler::GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens)
518 {
519     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
520     auto ret = static_cast<int>(appMgrClient_->GetAbilityRecordsByProcessID(pid, tokens));
521     if (ret != ERR_OK) {
522         TAG_LOGE(AAFwkTag::ABILITYMGR, "getAbilityRecordsByProcessID failed");
523         return INNER_ERR;
524     }
525 
526     return ERR_OK;
527 }
528 
GetApplicationInfoByProcessID(const int pid,AppExecFwk::ApplicationInfo & application,bool & debug)529 int AppScheduler::GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug)
530 {
531     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
532     auto ret = static_cast<int>(appMgrClient_->GetApplicationInfoByProcessID(pid, application, debug));
533     if (ret != ERR_OK) {
534         TAG_LOGE(AAFwkTag::ABILITYMGR, "getApplicationInfoByProcessID failed");
535         return ret;
536     }
537 
538     return ERR_OK;
539 }
540 
NotifyAppMgrRecordExitReason(int32_t pid,int32_t reason,const std::string & exitMsg)541 int32_t AppScheduler::NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg)
542 {
543     if (pid < 0) {
544         TAG_LOGW(AAFwkTag::ABILITYMGR, "pid<0");
545         return ERR_INVALID_VALUE;
546     }
547     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
548     auto ret = static_cast<int32_t>(IN_PROCESS_CALL(appMgrClient_->NotifyAppMgrRecordExitReason(pid, reason, exitMsg)));
549     if (ret != ERR_OK) {
550         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed");
551         return ret;
552     }
553     return ERR_OK;
554 }
555 
GetBundleNameByPid(const int pid,std::string & bundleName,int32_t & uid)556 int32_t AppScheduler::GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid)
557 {
558     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
559     int32_t ret = static_cast<int32_t>(IN_PROCESS_CALL(appMgrClient_->GetBundleNameByPid(pid, bundleName, uid)));
560     if (ret != ERR_OK) {
561         TAG_LOGE(AAFwkTag::ABILITYMGR, "get bundle name failed");
562         return INNER_ERR;
563     }
564     return ERR_OK;
565 }
566 
SetCurrentUserId(const int32_t userId)567 void AppScheduler::SetCurrentUserId(const int32_t userId)
568 {
569     CHECK_POINTER(appMgrClient_);
570     IN_PROCESS_CALL_WITHOUT_RET(appMgrClient_->SetCurrentUserId(userId));
571 }
572 
SetEnableStartProcessFlagByUserId(int32_t userId,bool enableStartProcess)573 void AppScheduler::SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess)
574 {
575     CHECK_POINTER(appMgrClient_);
576     IN_PROCESS_CALL_WITHOUT_RET(appMgrClient_->SetEnableStartProcessFlagByUserId(userId, enableStartProcess));
577 }
578 
NotifyFault(const AppExecFwk::FaultData & faultData)579 int32_t AppScheduler::NotifyFault(const AppExecFwk::FaultData &faultData)
580 {
581     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
582     auto ret = static_cast<int>(appMgrClient_->NotifyAppFault(faultData));
583     if (ret != ERR_OK) {
584         TAG_LOGE(AAFwkTag::ABILITYMGR, "notifyAppFault failed");
585         return INNER_ERR;
586     }
587 
588     return ERR_OK;
589 }
590 
RegisterAppDebugListener(const sptr<AppExecFwk::IAppDebugListener> & listener)591 int32_t AppScheduler::RegisterAppDebugListener(const sptr<AppExecFwk::IAppDebugListener> &listener)
592 {
593     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
594     auto ret = static_cast<int32_t>(appMgrClient_->RegisterAppDebugListener(listener));
595     if (ret != ERR_OK) {
596         TAG_LOGE(AAFwkTag::ABILITYMGR, "register app debug listener failed");
597         return INNER_ERR;
598     }
599     return ERR_OK;
600 }
601 
UnregisterAppDebugListener(const sptr<AppExecFwk::IAppDebugListener> & listener)602 int32_t AppScheduler::UnregisterAppDebugListener(const sptr<AppExecFwk::IAppDebugListener> &listener)
603 {
604     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
605     auto ret = static_cast<int32_t>(appMgrClient_->UnregisterAppDebugListener(listener));
606     if (ret != ERR_OK) {
607         TAG_LOGE(AAFwkTag::ABILITYMGR, "unregister app debug listener failed");
608         return INNER_ERR;
609     }
610     return ERR_OK;
611 }
612 
AttachAppDebug(const std::string & bundleName,bool isDebugFromLocal)613 int32_t AppScheduler::AttachAppDebug(const std::string &bundleName, bool isDebugFromLocal)
614 {
615     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
616     auto ret = static_cast<int32_t>(appMgrClient_->AttachAppDebug(bundleName, isDebugFromLocal));
617     if (ret != ERR_OK) {
618         TAG_LOGE(AAFwkTag::ABILITYMGR, "attach app debug failed");
619         return INNER_ERR;
620     }
621     return ERR_OK;
622 }
623 
DetachAppDebug(const std::string & bundleName)624 int32_t AppScheduler::DetachAppDebug(const std::string &bundleName)
625 {
626     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
627     auto ret = static_cast<int32_t>(appMgrClient_->DetachAppDebug(bundleName));
628     if (ret != ERR_OK) {
629         TAG_LOGE(AAFwkTag::ABILITYMGR, "detach app debug failed");
630         return INNER_ERR;
631     }
632     return ERR_OK;
633 }
634 
RegisterAbilityDebugResponse(const sptr<AppExecFwk::IAbilityDebugResponse> & response)635 int32_t AppScheduler::RegisterAbilityDebugResponse(const sptr<AppExecFwk::IAbilityDebugResponse> &response)
636 {
637     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
638     auto ret = static_cast<int32_t>(appMgrClient_->RegisterAbilityDebugResponse(response));
639     if (ret != ERR_OK) {
640         TAG_LOGE(AAFwkTag::ABILITYMGR, "register ability debug response failed");
641         return INNER_ERR;
642     }
643     return ERR_OK;
644 }
645 
IsAttachDebug(const std::string & bundleName)646 bool AppScheduler::IsAttachDebug(const std::string &bundleName)
647 {
648     CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR);
649     auto ret = static_cast<int32_t>(appMgrClient_->IsAttachDebug(bundleName));
650     if (ret != ERR_OK) {
651         TAG_LOGE(AAFwkTag::ABILITYMGR, "call attach debug failed");
652         return INNER_ERR;
653     }
654     return ERR_OK;
655 }
656 
ClearProcessByToken(sptr<IRemoteObject> token) const657 void AppScheduler::ClearProcessByToken(sptr<IRemoteObject> token) const
658 {
659     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
660     CHECK_POINTER(appMgrClient_);
661     appMgrClient_->ClearProcessByToken(token);
662 }
663 
IsMemorySizeSufficent() const664 bool AppScheduler::IsMemorySizeSufficent() const
665 {
666     if (!appMgrClient_) {
667         TAG_LOGE(AAFwkTag::ABILITYMGR, "null appMgrClient");
668         return true;
669     }
670     return appMgrClient_->IsMemorySizeSufficent();
671 }
672 
IsNoRequireBigMemory() const673 bool AppScheduler::IsNoRequireBigMemory() const
674 {
675     if (!appMgrClient_) {
676         TAG_LOGE(AAFwkTag::ABILITYMGR, "null appMgrClient");
677         return true;
678     }
679     return appMgrClient_->IsNoRequireBigMemory();
680 }
681 
AttachedToStatusBar(const sptr<IRemoteObject> & token)682 void AppScheduler::AttachedToStatusBar(const sptr<IRemoteObject> &token)
683 {
684     TAG_LOGI(AAFwkTag::ABILITYMGR, "call");
685     CHECK_POINTER(appMgrClient_);
686     appMgrClient_->AttachedToStatusBar(token);
687 }
688 
BlockProcessCacheByPids(const std::vector<int32_t> & pids)689 void AppScheduler::BlockProcessCacheByPids(const std::vector<int32_t> &pids)
690 {
691     TAG_LOGI(AAFwkTag::ABILITYMGR, "call");
692     CHECK_POINTER(appMgrClient_);
693     appMgrClient_->BlockProcessCacheByPids(pids);
694 }
695 
IsKilledForUpgradeWeb(const std::string & bundleName)696 bool AppScheduler::IsKilledForUpgradeWeb(const std::string &bundleName)
697 {
698     if (!appMgrClient_) {
699         TAG_LOGE(AAFwkTag::ABILITYMGR, "null appMgrClient");
700         return false;
701     }
702     return appMgrClient_->IsKilledForUpgradeWeb(bundleName);
703 }
704 
CleanAbilityByUserRequest(const sptr<IRemoteObject> & token)705 bool AppScheduler::CleanAbilityByUserRequest(const sptr<IRemoteObject> &token)
706 {
707     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
708     TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
709     if (!appMgrClient_) {
710         TAG_LOGE(AAFwkTag::ABILITYMGR, "null appMgrClient");
711         return false;
712     }
713     return IN_PROCESS_CALL(appMgrClient_->CleanAbilityByUserRequest(token));
714 }
IsProcessContainsOnlyUIAbility(const pid_t pid)715 bool AppScheduler::IsProcessContainsOnlyUIAbility(const pid_t pid)
716 {
717     if (!appMgrClient_) {
718         TAG_LOGE(AAFwkTag::ABILITYMGR, "null appMgrClient");
719         return false;
720     }
721     return appMgrClient_->IsProcessContainsOnlyUIAbility(pid);
722 }
723 
IsProcessAttached(sptr<IRemoteObject> token) const724 bool AppScheduler::IsProcessAttached(sptr<IRemoteObject> token) const
725 {
726     if (!appMgrClient_) {
727         TAG_LOGE(AAFwkTag::ABILITYMGR, "null appMgrClient");
728         return false;
729     }
730     return appMgrClient_->IsProcessAttached(token);
731 }
732 
IsCallerKilling(const std::string & callerKey) const733 bool AppScheduler::IsCallerKilling(const std::string& callerKey) const
734 {
735     if (!appMgrClient_) {
736         TAG_LOGE(AAFwkTag::ABILITYMGR, "appMgrClient is nullptr");
737         return false;
738     }
739     return appMgrClient_->IsCallerKilling(callerKey);
740 }
741 
SetProcessCacheStatus(int32_t pid,bool isSupport)742 void AppScheduler::SetProcessCacheStatus(int32_t pid, bool isSupport)
743 {
744     if (!appMgrClient_) {
745         TAG_LOGE(AAFwkTag::ABILITYMGR, "appMgrClient is nullptr");
746         return;
747     }
748     appMgrClient_->SetSupportedProcessCache(pid, isSupport);
749 }
750 
PreloadApplicationByPhase(const std::string & bundleName,int32_t userId,int32_t appIndex,AppExecFwk::PreloadPhase preloadPhase)751 int32_t AppScheduler::PreloadApplicationByPhase(const std::string &bundleName, int32_t userId, int32_t appIndex,
752     AppExecFwk::PreloadPhase preloadPhase)
753 {
754     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
755     if (appMgrClient_ == nullptr) {
756         TAG_LOGE(AAFwkTag::ABILITYMGR, "null appMgrClient");
757         return INNER_ERR;
758     }
759     return IN_PROCESS_CALL(appMgrClient_->PreloadApplicationByPhase(bundleName, userId, appIndex, preloadPhase));
760 }
761 
NotifyPreloadAbilityStateChanged(sptr<IRemoteObject> token,bool isPreForeground)762 int32_t AppScheduler::NotifyPreloadAbilityStateChanged(sptr<IRemoteObject> token, bool isPreForeground)
763 {
764     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
765     if (appMgrClient_ == nullptr) {
766         TAG_LOGE(AAFwkTag::ABILITYMGR, "null appMgrClient");
767         return INNER_ERR;
768     }
769     return IN_PROCESS_CALL(appMgrClient_->NotifyPreloadAbilityStateChanged(token, isPreForeground));
770 }
771 
CheckPreloadAppRecordExist(const std::string & bundleName,int32_t userId,int32_t appIndex,bool & isExist)772 int32_t AppScheduler::CheckPreloadAppRecordExist(const std::string &bundleName, int32_t userId, int32_t appIndex,
773     bool &isExist)
774 {
775     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
776     if (appMgrClient_ == nullptr) {
777         TAG_LOGE(AAFwkTag::ABILITYMGR, "null appMgrClient");
778         return INNER_ERR;
779     }
780     return IN_PROCESS_CALL(appMgrClient_->CheckPreloadAppRecordExist(bundleName, userId, appIndex, isExist));
781 }
782 
VerifyKillProcessPermission(const std::string & bundleName) const783 int32_t AppScheduler::VerifyKillProcessPermission(const std::string &bundleName) const
784 {
785     if (!appMgrClient_) {
786         TAG_LOGE(AAFwkTag::ABILITYMGR, "null appMgrClient");
787         return INNER_ERR;
788     }
789     return appMgrClient_->VerifyKillProcessPermission(bundleName);
790 }
791 } // namespace AAFwk
792 }  // namespace OHOS
793