• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "accesstoken_kit.h"
24 #include "app_death_recipient.h"
25 #include "app_mgr_constants.h"
26 #include "hilog_tag_wrapper.h"
27 #include "perf_profile.h"
28 #include "permission_constants.h"
29 #include "permission_verification.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 namespace {
34 constexpr const char* TASK_TERMINATE_ABILITY = "TerminateAbilityTask";
35 constexpr const char* TASK_UPDATE_ABILITY_STATE = "UpdateAbilityStateTask";
36 constexpr const char* TASK_UPDATE_EXTENSION_STATE = "UpdateExtensionStateTask";
37 constexpr const char* TASK_REGISTER_APP_STATE_CALLBACK = "RegisterAppStateCallbackTask";
38 constexpr const char* TASK_STOP_ALL_PROCESS = "StopAllProcessTask";
39 constexpr const char* TASK_ABILITY_BEHAVIOR_ANALYSIS = "AbilityBehaviorAnalysisTask";
40 constexpr const char* TASK_KILL_PROCESS_BY_ABILITY_TOKEN = "KillProcessByAbilityTokenTask";
41 constexpr const char* TASK_KILL_PROCESSES_BY_USERID = "KillProcessesByUserIdTask";
42 constexpr const char* TASK_KILL_PROCESSES_BY_PIDS = "KillProcessesByPids";
43 constexpr const char* TASK_ATTACH_PID_TO_PARENT = "AttachPidToParent";
44 constexpr const char* TASK_KILL_APPLICATION = "KillApplicationTask";
45 constexpr const char* TASK_CLEAR_PROCESS_BY_ABILITY_TOKEN = "ClearProcessByAbilityTokenTask";
46 constexpr const char* FOUNDATION_NAME = "foundation";
47 constexpr const char* SCENE_BOARD_BUNDLE_NAME = "com.ohos.sceneboard";
48 constexpr const char* SCENEBOARD_ABILITY_NAME = "com.ohos.sceneboard.MainAbility";
49 constexpr const char* TASK_SCENE_BOARD_ATTACH_TIMEOUT = "sceneBoardAttachTimeoutTask";
50 constexpr const char* TASK_ATTACHED_TO_STATUS_BAR = "AttachedToStatusBar";
51 constexpr const char* TASK_BLOCK_PROCESS_CACHE_BY_PIDS = "BlockProcessCacheByPids";
52 constexpr const char* POWER_OFF_ABILITY = "PoweroffAbility";
53 constexpr const char* TASK_SEND_APP_SPAWN_UNINSTALL_DEBUG_HAP_MSG = "SendAppSpawnUninstallDebugHapMsgTask";
54 constexpr int32_t SCENE_BOARD_ATTACH_TIMEOUT_TASK_TIME = 1000;
55 constexpr int32_t LOAD_TASK_TIMEOUT = 30000000; // us
56 };  // namespace
57 
AmsMgrScheduler(const std::shared_ptr<AppMgrServiceInner> & mgrServiceInner_,const std::shared_ptr<AAFwk::TaskHandlerWrap> & handler_)58 AmsMgrScheduler::AmsMgrScheduler(
59     const std::shared_ptr<AppMgrServiceInner> &mgrServiceInner_,
60     const std::shared_ptr<AAFwk::TaskHandlerWrap> &handler_)
61     : amsMgrServiceInner_(mgrServiceInner_), amsHandler_(handler_)
62 {}
63 
~AmsMgrScheduler()64 AmsMgrScheduler::~AmsMgrScheduler()
65 {
66     TAG_LOGI(AAFwkTag::APPMGR, "AmsMgrScheduler instance destroyed");
67 }
68 
LoadAbility(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<ApplicationInfo> & appInfo,const std::shared_ptr<AAFwk::Want> & want,std::shared_ptr<AbilityRuntime::LoadParam> loadParam)69 void AmsMgrScheduler::LoadAbility(const std::shared_ptr<AbilityInfo> &abilityInfo,
70     const std::shared_ptr<ApplicationInfo> &appInfo,
71     const std::shared_ptr<AAFwk::Want> &want, std::shared_ptr<AbilityRuntime::LoadParam> loadParam)
72 {
73     if (!abilityInfo || !appInfo) {
74         TAG_LOGE(AAFwkTag::APPMGR, "param error");
75         return;
76     }
77 
78     if (!IsReady()) {
79         return;
80     }
81 
82     if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
83         TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed");
84         return;
85     }
86     PerfProfile::GetInstance().SetAbilityLoadStartTime(GetTickCount());
87     TAG_LOGI(AAFwkTag::APPMGR, "SubmitLoadTask: %{public}s-%{public}s", abilityInfo->bundleName.c_str(),
88         abilityInfo->name.c_str());
89     std::function<void()> loadAbilityFunc = [amsMgrServiceInner = amsMgrServiceInner_,
90         abilityInfo, appInfo, want, loadParam]() {
91         amsMgrServiceInner->LoadAbility(abilityInfo, appInfo, want, loadParam);
92     };
93 
94     // cache other application load ability task before scene board attach
95     if (!amsMgrServiceInner_->GetSceneBoardAttachFlag() && abilityInfo->bundleName != SCENE_BOARD_BUNDLE_NAME) {
96         amsMgrServiceInner_->CacheLoadAbilityTask(loadAbilityFunc);
97         return;
98     }
99     if (abilityInfo->bundleName == SCENE_BOARD_BUNDLE_NAME && abilityInfo->name == SCENEBOARD_ABILITY_NAME) {
100         amsMgrServiceInner_->SetSceneBoardAttachFlag(false);
101         // set scene board attach timeout task
102         std::weak_ptr<AppMgrServiceInner> amsMgrServiceInner = amsMgrServiceInner_;
103         auto timeoutTask = [amsMgrServiceInner]() {
104             auto inner = amsMgrServiceInner.lock();
105             if (inner != nullptr) {
106                 inner->SetSceneBoardAttachFlag(true);
107             }
108         };
109         amsHandler_->SubmitTask(timeoutTask, TASK_SCENE_BOARD_ATTACH_TIMEOUT, SCENE_BOARD_ATTACH_TIMEOUT_TASK_TIME);
110     }
111 
112     AAFwk::TaskAttribute taskAttr{
113         .taskName_ = "LoadAbilityTask",
114         .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE,
115         .timeout_ = LOAD_TASK_TIMEOUT
116     };
117 
118     if (abilityInfo->bundleName == SCENE_BOARD_BUNDLE_NAME && abilityInfo->name == POWER_OFF_ABILITY) {
119         TAG_LOGI(AAFwkTag::APPMGR, "poweroff insert");
120         taskAttr.insertHead_ = true;
121     }
122 
123     amsHandler_->SubmitTask(loadAbilityFunc, taskAttr);
124 }
125 
UpdateAbilityState(const sptr<IRemoteObject> & token,const AbilityState state)126 void AmsMgrScheduler::UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state)
127 {
128     if (!IsReady()) {
129         return;
130     }
131 
132     if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
133         TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed.");
134         return;
135     }
136     std::function<void()> updateAbilityStateFunc = [amsMgrServiceInner = amsMgrServiceInner_, token, state] () {
137         amsMgrServiceInner->UpdateAbilityState(token, state);
138     };
139     amsHandler_->SubmitTask(updateAbilityStateFunc, AAFwk::TaskAttribute{
140         .taskName_ = TASK_UPDATE_ABILITY_STATE,
141         .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
142     });
143 }
144 
UpdateExtensionState(const sptr<IRemoteObject> & token,const ExtensionState state)145 void AmsMgrScheduler::UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state)
146 {
147     if (!IsReady()) {
148         return;
149     }
150 
151     if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
152         TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed.");
153         return;
154     }
155     std::function<void()> updateExtensionStateFunc = [amsMgrServiceInner = amsMgrServiceInner_, token, state]() {
156         amsMgrServiceInner->UpdateExtensionState(token, state);
157     };
158     amsHandler_->SubmitTask(updateExtensionStateFunc, AAFwk::TaskAttribute{
159         .taskName_ = TASK_UPDATE_EXTENSION_STATE,
160         .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
161     });
162 }
163 
TerminateAbility(const sptr<IRemoteObject> & token,bool clearMissionFlag)164 void AmsMgrScheduler::TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag)
165 {
166     if (!IsReady()) {
167         return;
168     }
169 
170     if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
171         TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed.");
172         return;
173     }
174     std::function<void()> terminateAbilityFunc = [amsMgrServiceInner = amsMgrServiceInner_, token, clearMissionFlag]() {
175         amsMgrServiceInner->TerminateAbility(token, clearMissionFlag);
176     };
177     amsHandler_->SubmitTask(terminateAbilityFunc, AAFwk::TaskAttribute{
178         .taskName_ = TASK_TERMINATE_ABILITY,
179         .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
180     });
181 }
182 
RegisterAppStateCallback(const sptr<IAppStateCallback> & callback)183 void AmsMgrScheduler::RegisterAppStateCallback(const sptr<IAppStateCallback> &callback)
184 {
185     if (!AAFwk::PermissionVerification::GetInstance()->IsSACall()) {
186         TAG_LOGE(AAFwkTag::APPMGR, "caller is not SA");
187         return;
188     }
189     if (!IsReady()) {
190         return;
191     }
192     std::function<void()> registerAppStateCallbackFunc = [amsMgrServiceInner = amsMgrServiceInner_, callback]() {
193         amsMgrServiceInner->RegisterAppStateCallback(callback);
194     };
195     amsHandler_->SubmitTask(registerAppStateCallbackFunc, TASK_REGISTER_APP_STATE_CALLBACK);
196 }
197 
AbilityBehaviorAnalysis(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & preToken,const int32_t visibility,const int32_t perceptibility,const int32_t connectionState)198 void AmsMgrScheduler::AbilityBehaviorAnalysis(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
199     const int32_t visibility, const int32_t perceptibility, const int32_t connectionState)
200 {
201     if (!IsReady()) {
202         return;
203     }
204 
205     if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
206         TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed.");
207         return;
208     }
209     std::function<void()> abilityBehaviorAnalysisFunc = [amsMgrServiceInner = amsMgrServiceInner_, token, preToken,
210         visibility, perceptibility, connectionState]() {
211         amsMgrServiceInner->AbilityBehaviorAnalysis(token, preToken,
212             visibility, perceptibility, connectionState);
213     };
214     amsHandler_->SubmitTask(abilityBehaviorAnalysisFunc, TASK_ABILITY_BEHAVIOR_ANALYSIS);
215 }
216 
KillProcessByAbilityToken(const sptr<IRemoteObject> & token)217 void AmsMgrScheduler::KillProcessByAbilityToken(const sptr<IRemoteObject> &token)
218 {
219     if (!IsReady()) {
220         return;
221     }
222 
223     if (amsMgrServiceInner_->VerifyKillProcessPermission(token) != ERR_OK) {
224         TAG_LOGE(AAFwkTag::APPMGR, "Permission verify failed");
225         return;
226     }
227 
228     std::function<void()> killProcessByAbilityTokenFunc = [amsMgrServiceInner = amsMgrServiceInner_, token]() {
229         amsMgrServiceInner->KillProcessByAbilityToken(token);
230     };
231     amsHandler_->SubmitTask(killProcessByAbilityTokenFunc, TASK_KILL_PROCESS_BY_ABILITY_TOKEN);
232 }
233 
KillProcessesByUserId(int32_t userId)234 void AmsMgrScheduler::KillProcessesByUserId(int32_t userId)
235 {
236     if (!IsReady()) {
237         return;
238     }
239 
240     if (!AAFwk::PermissionVerification::GetInstance()->JudgeCallerIsAllowedToUseSystemAPI()) {
241         TAG_LOGE(AAFwkTag::APPMGR, "The caller is not system-app, can not use system-api");
242         return;
243     }
244 
245     bool isCallingFromFoundation =
246         AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(FOUNDATION_NAME);
247     auto permission = AAFwk::PermissionConstants::PERMISSION_CLEAN_BACKGROUND_PROCESSES;
248     if (!isCallingFromFoundation &&
249         amsMgrServiceInner_->VerifyAccountPermission(permission, userId) == ERR_PERMISSION_DENIED) {
250         TAG_LOGE(AAFwkTag::APPMGR, "Permission verify failed");
251         return;
252     }
253 
254     std::function<void()> killProcessesByUserIdFunc = [amsMgrServiceInner = amsMgrServiceInner_, userId]() {
255         amsMgrServiceInner->KillProcessesByUserId(userId);
256     };
257     amsHandler_->SubmitTask(killProcessesByUserIdFunc, TASK_KILL_PROCESSES_BY_USERID);
258 }
259 
KillProcessesByPids(std::vector<int32_t> & pids)260 void AmsMgrScheduler::KillProcessesByPids(std::vector<int32_t> &pids)
261 {
262     if (!IsReady()) {
263         return;
264     }
265 
266     pid_t callingPid = IPCSkeleton::GetCallingPid();
267     pid_t pid = getprocpid();
268     if (callingPid != pid) {
269         TAG_LOGE(AAFwkTag::APPMGR, "Not allow other process to call.");
270         return;
271     }
272 
273     std::function<void()> killProcessesByPidsFunc = [amsMgrServiceInner = amsMgrServiceInner_, pids]() mutable {
274         amsMgrServiceInner->KillProcessesByPids(pids);
275     };
276     amsHandler_->SubmitTask(killProcessesByPidsFunc, TASK_KILL_PROCESSES_BY_PIDS);
277 }
278 
AttachPidToParent(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & callerToken)279 void AmsMgrScheduler::AttachPidToParent(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callerToken)
280 {
281     if (!IsReady()) {
282         return;
283     }
284 
285     pid_t callingPid = IPCSkeleton::GetCallingPid();
286     pid_t pid = getprocpid();
287     if (callingPid != pid) {
288         TAG_LOGE(AAFwkTag::APPMGR, "Not allow other process to call.");
289         return;
290     }
291 
292     std::function<void()> attachPidToParentFunc = [amsMgrServiceInner = amsMgrServiceInner_, token, callerToken]() {
293         amsMgrServiceInner->AttachPidToParent(token, callerToken);
294     };
295     amsHandler_->SubmitTask(attachPidToParentFunc, TASK_ATTACH_PID_TO_PARENT);
296 }
297 
KillProcessWithAccount(const std::string & bundleName,const int accountId,const bool clearPageStack)298 int32_t AmsMgrScheduler::KillProcessWithAccount(
299     const std::string &bundleName, const int accountId, const bool clearPageStack)
300 {
301     TAG_LOGI(AAFwkTag::APPMGR, "bundleName = %{public}s, accountId = %{public}d, clearPageStack = %{public}d",
302         bundleName.c_str(), accountId, clearPageStack);
303     if (!IsReady()) {
304         return ERR_INVALID_OPERATION;
305     }
306     return amsMgrServiceInner_->KillApplicationByUserId(bundleName, 0, accountId, clearPageStack,
307         "KillProcessWithAccount");
308 }
309 
KillProcessesInBatch(const std::vector<int32_t> & pids)310 int32_t AmsMgrScheduler::KillProcessesInBatch(const std::vector<int32_t> &pids)
311 {
312     TAG_LOGI(AAFwkTag::APPMGR, "pids.size=%{public}zu", pids.size());
313     if (!IsReady()) {
314         return ERR_INVALID_OPERATION;
315     }
316     return amsMgrServiceInner_->KillProcessesInBatch(pids);
317 }
318 
AbilityAttachTimeOut(const sptr<IRemoteObject> & token)319 void AmsMgrScheduler::AbilityAttachTimeOut(const sptr<IRemoteObject> &token)
320 {
321     TAG_LOGI(AAFwkTag::APPMGR, "AmsMgrScheduler AttachTimeOut begin");
322     if (!IsReady()) {
323         return;
324     }
325 
326     if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
327         TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed.");
328         return;
329     }
330     auto task = [amsMgrServiceInner = amsMgrServiceInner_, token]() {
331         amsMgrServiceInner->HandleAbilityAttachTimeOut(token);
332     };
333     amsHandler_->SubmitTask(task, "AbilityAttachTimeOut");
334 }
335 
PrepareTerminate(const sptr<IRemoteObject> & token,bool clearMissionFlag)336 void AmsMgrScheduler::PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag)
337 {
338     TAG_LOGD(AAFwkTag::APPMGR, "Notify AppMgrService to prepare to terminate the ability.");
339     if (!IsReady()) {
340         return;
341     }
342 
343     if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
344         TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed.");
345         return;
346     }
347     auto task = [=]() { amsMgrServiceInner_->PrepareTerminate(token, clearMissionFlag); };
348     amsHandler_->SubmitTask(task, {
349         .taskName_ = "PrepareTerminate",
350         .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
351     });
352 }
353 
UpdateApplicationInfoInstalled(const std::string & bundleName,const int uid)354 int32_t AmsMgrScheduler::UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid)
355 {
356     if (!IsReady()) {
357         return ERR_INVALID_OPERATION;
358     }
359 
360     return amsMgrServiceInner_->UpdateApplicationInfoInstalled(bundleName, uid);
361 }
362 
KillApplication(const std::string & bundleName,const bool clearPageStack)363 int32_t AmsMgrScheduler::KillApplication(const std::string &bundleName, const bool clearPageStack)
364 {
365     TAG_LOGI(AAFwkTag::APPMGR, "bundleName = %{public}s, clearPageStack = %{public}d",
366         bundleName.c_str(), clearPageStack);
367     if (!IsReady()) {
368         return ERR_INVALID_OPERATION;
369     }
370 
371     return amsMgrServiceInner_->KillApplication(bundleName, clearPageStack);
372 }
373 
ForceKillApplication(const std::string & bundleName,const int userId,const int appIndex)374 int32_t AmsMgrScheduler::ForceKillApplication(const std::string &bundleName,
375     const int userId, const int appIndex)
376 {
377     TAG_LOGI(AAFwkTag::APPMGR, "bundleName=%{public}s,userId=%{public}d,apIndex=%{public}d",
378         bundleName.c_str(), userId, appIndex);
379     if (!IsReady()) {
380         return ERR_INVALID_OPERATION;
381     }
382 
383     return amsMgrServiceInner_->ForceKillApplication(bundleName, userId, appIndex);
384 }
385 
KillProcessesByAccessTokenId(const uint32_t accessTokenId)386 int32_t AmsMgrScheduler::KillProcessesByAccessTokenId(const uint32_t accessTokenId)
387 {
388     TAG_LOGI(AAFwkTag::APPMGR, "accessTokenId=%{public}d", accessTokenId);
389     if (!IsReady()) {
390         return ERR_INVALID_OPERATION;
391     }
392 
393     return amsMgrServiceInner_->KillProcessesByAccessTokenId(accessTokenId);
394 }
395 
KillApplicationByUid(const std::string & bundleName,const int uid,const std::string & reason)396 int32_t AmsMgrScheduler::KillApplicationByUid(const std::string &bundleName, const int uid,
397     const std::string& reason)
398 {
399     TAG_LOGI(AAFwkTag::APPMGR, "bundleName = %{public}s, uid = %{public}d", bundleName.c_str(), uid);
400     if (!IsReady()) {
401         return ERR_INVALID_OPERATION;
402     }
403     return amsMgrServiceInner_->KillApplicationByUid(bundleName, uid, reason);
404 }
405 
KillApplicationSelf(const bool clearPageStack,const std::string & reason)406 int32_t AmsMgrScheduler::KillApplicationSelf(const bool clearPageStack, const std::string& reason)
407 {
408     if (!IsReady()) {
409         return ERR_INVALID_OPERATION;
410     }
411     return amsMgrServiceInner_->KillApplicationSelf(clearPageStack, reason);
412 }
413 
IsReady() const414 bool AmsMgrScheduler::IsReady() const
415 {
416     if (!amsMgrServiceInner_) {
417         TAG_LOGE(AAFwkTag::APPMGR, "amsMgrServiceInner_ is null");
418         return false;
419     }
420     if (!amsHandler_) {
421         TAG_LOGE(AAFwkTag::APPMGR, "amsHandler_ is null");
422         return false;
423     }
424     return true;
425 }
426 
GetRunningProcessInfoByToken(const sptr<IRemoteObject> & token,AppExecFwk::RunningProcessInfo & info)427 void AmsMgrScheduler::GetRunningProcessInfoByToken(
428     const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info)
429 {
430     if (!IsReady()) {
431         return;
432     }
433 
434     amsMgrServiceInner_->GetRunningProcessInfoByToken(token, info);
435 }
436 
SetAbilityForegroundingFlagToAppRecord(const pid_t pid)437 void AmsMgrScheduler::SetAbilityForegroundingFlagToAppRecord(const pid_t pid)
438 {
439     if (!IsReady()) {
440         return;
441     }
442     amsMgrServiceInner_->SetAbilityForegroundingFlagToAppRecord(pid);
443 }
444 
StartSpecifiedAbility(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,int32_t requestId)445 void AmsMgrScheduler::StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
446     int32_t requestId)
447 {
448     if (!IsReady()) {
449         return;
450     }
451 
452     if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
453         TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed.");
454         return;
455     }
456     auto task = [=]() { amsMgrServiceInner_->StartSpecifiedAbility(want, abilityInfo, requestId); };
457     amsHandler_->SubmitTask(task, {
458         .taskName_ = "StartSpecifiedAbility",
459         .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
460     });
461 }
462 
StartSpecifiedProcess(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,int32_t requestId)463 void AmsMgrScheduler::StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
464     int32_t requestId)
465 {
466     if (!IsReady()) {
467         TAG_LOGW(AAFwkTag::APPMGR, "not ready.");
468         return;
469     }
470 
471     if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
472         TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed.");
473         return;
474     }
475     auto task = [=]() { amsMgrServiceInner_->StartSpecifiedProcess(want, abilityInfo, requestId); };
476     amsHandler_->SubmitTask(task, {
477         .taskName_ = "StartSpecifiedProcess",
478         .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
479     });
480 }
481 
RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> & response)482 void AmsMgrScheduler::RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response)
483 {
484     if (!AAFwk::PermissionVerification::GetInstance()->IsSACall()) {
485         TAG_LOGE(AAFwkTag::APPMGR, "caller is not SA");
486         return;
487     }
488     if (!IsReady()) {
489         return;
490     }
491     auto task = [=]() { amsMgrServiceInner_->RegisterStartSpecifiedAbilityResponse(response); };
492     amsHandler_->SubmitTask(task, "RegisterStartSpecifiedAbilityResponse");
493 }
494 
GetApplicationInfoByProcessID(const int pid,AppExecFwk::ApplicationInfo & application,bool & debug)495 int AmsMgrScheduler::GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug)
496 {
497     if (!IsReady()) {
498         return ERR_INVALID_OPERATION;
499     }
500     return amsMgrServiceInner_->GetApplicationInfoByProcessID(pid, application, debug);
501 }
502 
NotifyAppMgrRecordExitReason(int32_t pid,int32_t reason,const std::string & exitMsg)503 int32_t AmsMgrScheduler::NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg)
504 {
505     if (!IsReady()) {
506         return ERR_INVALID_OPERATION;
507     }
508     return amsMgrServiceInner_->NotifyAppMgrRecordExitReason(pid, reason, exitMsg);
509 }
510 
SetCurrentUserId(const int32_t userId)511 void AmsMgrScheduler::SetCurrentUserId(const int32_t userId)
512 {
513     if (!IsReady()) {
514         return;
515     }
516     amsMgrServiceInner_->SetCurrentUserId(userId);
517 }
518 
SetEnableStartProcessFlagByUserId(int32_t userId,bool enableStartProcess)519 void AmsMgrScheduler::SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess)
520 {
521     if (!IsReady()) {
522         return;
523     }
524     if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
525         TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
526         return;
527     }
528     amsMgrServiceInner_->SetEnableStartProcessFlagByUserId(userId, enableStartProcess);
529 }
530 
GetBundleNameByPid(const int pid,std::string & bundleName,int32_t & uid)531 int32_t AmsMgrScheduler::GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid)
532 {
533     if (!IsReady()) {
534         return ERR_INVALID_OPERATION;
535     }
536     return amsMgrServiceInner_->GetBundleNameByPid(pid, bundleName, uid);
537 }
538 
RegisterAppDebugListener(const sptr<IAppDebugListener> & listener)539 int32_t AmsMgrScheduler::RegisterAppDebugListener(const sptr<IAppDebugListener> &listener)
540 {
541     if (!IsReady()) {
542         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
543         return ERR_INVALID_OPERATION;
544     }
545     return amsMgrServiceInner_->RegisterAppDebugListener(listener);
546 }
547 
UnregisterAppDebugListener(const sptr<IAppDebugListener> & listener)548 int32_t AmsMgrScheduler::UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener)
549 {
550     if (!IsReady()) {
551         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
552         return ERR_INVALID_OPERATION;
553     }
554     return amsMgrServiceInner_->UnregisterAppDebugListener(listener);
555 }
556 
AttachAppDebug(const std::string & bundleName)557 int32_t AmsMgrScheduler::AttachAppDebug(const std::string &bundleName)
558 {
559     if (!IsReady()) {
560         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
561         return ERR_INVALID_OPERATION;
562     }
563     return amsMgrServiceInner_->AttachAppDebug(bundleName);
564 }
565 
DetachAppDebug(const std::string & bundleName)566 int32_t AmsMgrScheduler::DetachAppDebug(const std::string &bundleName)
567 {
568     if (!IsReady()) {
569         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
570         return ERR_INVALID_OPERATION;
571     }
572     return amsMgrServiceInner_->DetachAppDebug(bundleName);
573 }
574 
SetAppWaitingDebug(const std::string & bundleName,bool isPersist)575 int32_t AmsMgrScheduler::SetAppWaitingDebug(const std::string &bundleName, bool isPersist)
576 {
577     if (!IsReady()) {
578         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
579         return ERR_INVALID_OPERATION;
580     }
581     return amsMgrServiceInner_->SetAppWaitingDebug(bundleName, isPersist);
582 }
583 
CancelAppWaitingDebug()584 int32_t AmsMgrScheduler::CancelAppWaitingDebug()
585 {
586     if (!IsReady()) {
587         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
588         return ERR_INVALID_OPERATION;
589     }
590     return amsMgrServiceInner_->CancelAppWaitingDebug();
591 }
592 
GetWaitingDebugApp(std::vector<std::string> & debugInfoList)593 int32_t AmsMgrScheduler::GetWaitingDebugApp(std::vector<std::string> &debugInfoList)
594 {
595     if (!IsReady()) {
596         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
597         return ERR_INVALID_OPERATION;
598     }
599     return amsMgrServiceInner_->GetWaitingDebugApp(debugInfoList);
600 }
601 
IsWaitingDebugApp(const std::string & bundleName)602 bool AmsMgrScheduler::IsWaitingDebugApp(const std::string &bundleName)
603 {
604     if (!IsReady()) {
605         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
606         return false;
607     }
608     return amsMgrServiceInner_->IsWaitingDebugApp(bundleName);
609 }
610 
ClearNonPersistWaitingDebugFlag()611 void AmsMgrScheduler::ClearNonPersistWaitingDebugFlag()
612 {
613     if (!IsReady()) {
614         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
615         return;
616     }
617     amsMgrServiceInner_->ClearNonPersistWaitingDebugFlag();
618 }
619 
RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> & response)620 int32_t AmsMgrScheduler::RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response)
621 {
622     if (!IsReady()) {
623         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
624         return ERR_INVALID_OPERATION;
625     }
626     return amsMgrServiceInner_->RegisterAbilityDebugResponse(response);
627 }
628 
IsAttachDebug(const std::string & bundleName)629 bool AmsMgrScheduler::IsAttachDebug(const std::string &bundleName)
630 {
631     if (!IsReady()) {
632         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
633         return false;
634     }
635     return amsMgrServiceInner_->IsAttachDebug(bundleName);
636 }
637 
SetKeepAliveEnableState(const std::string & bundleName,bool enable,int32_t uid)638 void AmsMgrScheduler::SetKeepAliveEnableState(const std::string &bundleName, bool enable, int32_t uid)
639 {
640     if (!IsReady()) {
641         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
642         return;
643     }
644     amsMgrServiceInner_->SetKeepAliveEnableState(bundleName, enable, uid);
645 }
646 
ClearProcessByToken(sptr<IRemoteObject> token)647 void AmsMgrScheduler::ClearProcessByToken(sptr<IRemoteObject> token)
648 {
649     if (!IsReady()) {
650         return;
651     }
652 
653     auto callerTokenId = IPCSkeleton::GetCallingTokenID();
654     Security::AccessToken::NativeTokenInfo nativeInfo;
655     Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(callerTokenId, nativeInfo);
656     if (nativeInfo.processName != "foundation") {
657         TAG_LOGE(AAFwkTag::APPMGR, "caller is not foundation.");
658         return;
659     }
660 
661     std::function<void()> clearProcessByTokenFunc = [amsMgrServiceInner = amsMgrServiceInner_, token]() {
662         amsMgrServiceInner->ClearProcessByToken(token);
663     };
664     amsHandler_->SubmitTask(clearProcessByTokenFunc, TASK_CLEAR_PROCESS_BY_ABILITY_TOKEN);
665 }
666 
IsMemorySizeSufficent()667 bool AmsMgrScheduler::IsMemorySizeSufficent()
668 {
669     if (!IsReady()) {
670         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
671         return true;
672     }
673     if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
674         TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed.");
675         return true;
676     }
677     return amsMgrServiceInner_->IsMemorySizeSufficent();
678 }
679 
AttachedToStatusBar(const sptr<IRemoteObject> & token)680 void AmsMgrScheduler::AttachedToStatusBar(const sptr<IRemoteObject> &token)
681 {
682     if (!IsReady()) {
683         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
684         return;
685     }
686     auto callerTokenId = IPCSkeleton::GetCallingTokenID();
687     Security::AccessToken::NativeTokenInfo nativeInfo;
688     Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(callerTokenId, nativeInfo);
689     if (nativeInfo.processName != "foundation") {
690         TAG_LOGE(AAFwkTag::APPMGR, "caller is not foundation.");
691         return;
692     }
693     std::function<void()> attachedToStatusBarFunc =
694         std::bind(&AppMgrServiceInner::AttachedToStatusBar, amsMgrServiceInner_, token);
695     amsHandler_->SubmitTask(attachedToStatusBarFunc, TASK_ATTACHED_TO_STATUS_BAR);
696 }
697 
BlockProcessCacheByPids(const std::vector<int32_t> & pids)698 void AmsMgrScheduler::BlockProcessCacheByPids(const std::vector<int32_t> &pids)
699 {
700     if (!IsReady()) {
701         return;
702     }
703 
704     pid_t callingPid = IPCSkeleton::GetCallingPid();
705     pid_t pid = getprocpid();
706     if (callingPid != pid) {
707         TAG_LOGE(AAFwkTag::APPMGR, "Not allow other process to call.");
708         return;
709     }
710 
711     std::function<void()> blockProcCacheFunc = [amsMgrServiceInner = amsMgrServiceInner_, pids]() mutable {
712         amsMgrServiceInner->BlockProcessCacheByPids(pids);
713     };
714     amsHandler_->SubmitTask(blockProcCacheFunc, TASK_BLOCK_PROCESS_CACHE_BY_PIDS);
715 }
716 
IsKilledForUpgradeWeb(const std::string & bundleName)717 bool AmsMgrScheduler::IsKilledForUpgradeWeb(const std::string &bundleName)
718 {
719     if (!IsReady()) {
720         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
721         return false;
722     }
723     return amsMgrServiceInner_->IsKilledForUpgradeWeb(bundleName);
724 }
725 
CleanAbilityByUserRequest(const sptr<IRemoteObject> & token)726 bool AmsMgrScheduler::CleanAbilityByUserRequest(const sptr<IRemoteObject> &token)
727 {
728     if (!IsReady()) {
729         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
730         return false;
731     }
732 
733     if (IPCSkeleton::GetCallingPid() != getprocpid()) {
734         TAG_LOGE(AAFwkTag::APPMGR, "Not allow other process to call.");
735         return false;
736     }
737     return amsMgrServiceInner_->CleanAbilityByUserRequest(token);
738 }
IsProcessContainsOnlyUIAbility(const pid_t pid)739 bool AmsMgrScheduler::IsProcessContainsOnlyUIAbility(const pid_t pid)
740 {
741     if (!IsReady()) {
742         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
743         return false;
744     }
745     pid_t callingPid = IPCSkeleton::GetCallingPid();
746     pid_t procPid = getprocpid();
747     if (callingPid != procPid) {
748         TAG_LOGE(AAFwkTag::APPMGR, "Not allow other process to call.");
749         return false;
750     }
751     return amsMgrServiceInner_->IsProcessContainsOnlyUIAbility(pid);
752 }
753 
IsProcessAttached(sptr<IRemoteObject> token)754 bool AmsMgrScheduler::IsProcessAttached(sptr<IRemoteObject> token)
755 {
756     if (!IsReady()) {
757         TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
758         return false;
759     }
760     return amsMgrServiceInner_->IsProcessAttached(token);
761 }
762 
SendAppSpawnUninstallDebugHapMsg(int32_t userId)763 void AmsMgrScheduler::SendAppSpawnUninstallDebugHapMsg(int32_t userId)
764 {
765     if (!IsReady()) {
766         TAG_LOGE(AAFwkTag::APPMGR, "ready failed");
767         return;
768     }
769     if (!AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(FOUNDATION_NAME)) {
770         TAG_LOGE(AAFwkTag::APPMGR, "caller is not foundation");
771         return;
772     }
773     auto task = [amsMgrServiceInner = amsMgrServiceInner_, userId]() {
774         amsMgrServiceInner->SendAppSpawnUninstallDebugHapMsg(userId);
775     };
776     amsHandler_->SubmitTask(task, TASK_SEND_APP_SPAWN_UNINSTALL_DEBUG_HAP_MSG);
777 }
778 } // namespace AppExecFwk
779 }  // namespace OHOS
780