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