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 "app_mgr_service.h"
17
18 #include <chrono>
19 #include <cstdlib>
20 #include <nlohmann/json.hpp>
21 #include <sys/types.h>
22 #include <thread>
23
24 #include "ability_manager_errors.h"
25 #include "app_death_recipient.h"
26 #include "app_mgr_constants.h"
27 #include "datetime_ex.h"
28 #include "fd_guard.h"
29 #include "freeze_util.h"
30 #include "global_constant.h"
31 #include "hilog_tag_wrapper.h"
32 #include "hitrace_meter.h"
33 #include "in_process_call_wrapper.h"
34 #include "ipc_skeleton.h"
35 #include "perf_profile.h"
36 #include "permission_constants.h"
37 #include "permission_verification.h"
38 #include "system_ability_definition.h"
39 #include "base/security/access_token/interfaces/innerkits/accesstoken/include/accesstoken_kit.h"
40 #include "app_mgr_service_const.h"
41 #include "app_mgr_service_dump_error_code.h"
42 #include "cache_process_manager.h"
43
44 namespace OHOS {
45 namespace AppExecFwk {
46 using AAFwk::FdGuard;
47 constexpr const char* OPTION_KEY_HELP = "-h";
48 constexpr const char* OPTION_KEY_DUMP_IPC = "--ipc";
49 constexpr const char* OPTION_KEY_DUMP_FFRT = "--ffrt";
50 const int32_t HIDUMPER_SERVICE_UID = 1212;
51 constexpr const int INDEX_PID = 1;
52 constexpr const int INDEX_CMD = 2;
53 constexpr const size_t VALID_DUMP_IPC_ARG_SIZE = 3;
54 constexpr const size_t VALID_DUMP_FFRT_ARG_SIZE = 2;
55 constexpr const int MAX_DUMP_FFRT_PID_NUMBER = 3;
56 constexpr const int BASE_TEN = 10;
57 constexpr const char SIGN_TERMINAL = '\0';
58 namespace {
59 using namespace std::chrono_literals;
60 constexpr const char* TASK_INIT_APPMGRSERVICEINNER = "InitAppMgrServiceInnerTask";
61 constexpr const char* TASK_ATTACH_APPLICATION = "AttachApplicationTask";
62 constexpr const char* TASK_APPLICATION_FOREGROUNDED = "ApplicationForegroundedTask";
63 constexpr const char* TASK_APPLICATION_BACKGROUNDED = "ApplicationBackgroundedTask";
64 constexpr const char* TASK_APPLICATION_TERMINATED = "ApplicationTerminatedTask";
65 constexpr const char* TASK_ABILITY_CLEANED = "AbilityCleanedTask";
66 constexpr const char* TASK_ADD_APP_DEATH_RECIPIENT = "AddAppRecipientTask";
67 constexpr const char* TASK_CLEAR_UP_APPLICATION_DATA = "ClearUpApplicationDataTask";
68 constexpr const char* TASK_STARTUP_RESIDENT_PROCESS = "StartupResidentProcess";
69 constexpr const char* TASK_ADD_ABILITY_STAGE_DONE = "AddAbilityStageDone";
70 constexpr const char* TASK_START_USER_TEST_PROCESS = "StartUserTestProcess";
71 constexpr const char* TASK_FINISH_USER_TEST = "FinishUserTest";
72 constexpr const char* TASK_ATTACH_RENDER_PROCESS = "AttachRenderTask";
73 constexpr const char* TASK_ATTACH_CHILD_PROCESS = "AttachChildProcessTask";
74 constexpr const char* TASK_EXIT_CHILD_PROCESS_SAFELY = "ExitChildProcessSafelyTask";
75 constexpr const char* FOUNDATION_PROCESS = "foundation";
76 constexpr int32_t USER_UID = 2000;
77 } // namespace
78
79 REGISTER_SYSTEM_ABILITY_BY_ID(AppMgrService, APP_MGR_SERVICE_ID, true);
80
AppMgrService()81 AppMgrService::AppMgrService()
82 {
83 appMgrServiceInner_ = std::make_shared<AppMgrServiceInner>();
84 TAG_LOGI(AAFwkTag::APPMGR, "instance created with no para");
85 PerfProfile::GetInstance().SetAmsLoadStartTime(GetTickCount());
86 }
87
AppMgrService(const int32_t serviceId,bool runOnCreate)88 AppMgrService::AppMgrService(const int32_t serviceId, bool runOnCreate) : SystemAbility(serviceId, runOnCreate)
89 {
90 appMgrServiceInner_ = std::make_shared<AppMgrServiceInner>();
91 TAG_LOGI(AAFwkTag::APPMGR, "instance created");
92 PerfProfile::GetInstance().SetAmsLoadStartTime(GetTickCount());
93 }
94
~AppMgrService()95 AppMgrService::~AppMgrService()
96 {
97 TAG_LOGI(AAFwkTag::APPMGR, "instance destroyed");
98 }
99
OnStart()100 void AppMgrService::OnStart()
101 {
102 TAG_LOGI(AAFwkTag::APPMGR, "ready to start service");
103 if (appMgrServiceState_.serviceRunningState == ServiceRunningState::STATE_RUNNING) {
104 TAG_LOGW(AAFwkTag::APPMGR, "failed to start service since it's already running");
105 return;
106 }
107
108 ErrCode errCode = Init();
109 if (FAILED(errCode)) {
110 TAG_LOGE(AAFwkTag::APPMGR, "init failed, errCode: %{public}08x", errCode);
111 return;
112 }
113 appMgrServiceState_.serviceRunningState = ServiceRunningState::STATE_RUNNING;
114 AddSystemAbilityListener(WINDOW_MANAGER_SERVICE_ID);
115 TAG_LOGI(AAFwkTag::APPMGR, "start service success");
116 PerfProfile::GetInstance().SetAmsLoadEndTime(GetTickCount());
117 PerfProfile::GetInstance().Dump();
118 }
119
OnStop()120 void AppMgrService::OnStop()
121 {
122 TAG_LOGI(AAFwkTag::APPMGR, "ready to stop service");
123 appMgrServiceState_.serviceRunningState = ServiceRunningState::STATE_NOT_START;
124 eventHandler_.reset();
125 taskHandler_.reset();
126 if (appMgrServiceInner_) {
127 appMgrServiceInner_->OnStop();
128 }
129 TAG_LOGI(AAFwkTag::APPMGR, "stop service success");
130 }
131
SetInnerService(const std::shared_ptr<AppMgrServiceInner> & innerService)132 void AppMgrService::SetInnerService(const std::shared_ptr<AppMgrServiceInner> &innerService)
133 {
134 appMgrServiceInner_ = innerService;
135 }
136
QueryServiceState()137 AppMgrServiceState AppMgrService::QueryServiceState()
138 {
139 if (appMgrServiceInner_) {
140 appMgrServiceState_.connectionState = appMgrServiceInner_->QueryAppSpawnConnectionState();
141 }
142 return appMgrServiceState_;
143 }
144
Init()145 ErrCode AppMgrService::Init()
146 {
147 TAG_LOGI(AAFwkTag::APPMGR, "ready to init");
148 if (!appMgrServiceInner_) {
149 TAG_LOGE(AAFwkTag::APPMGR, "init failed without inner service");
150 return ERR_INVALID_OPERATION;
151 }
152
153 taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler("app_mgr_task_queue");
154 taskHandler_->SetPrintTaskLog(true);
155 eventHandler_ = std::make_shared<AMSEventHandler>(taskHandler_, appMgrServiceInner_);
156 appMgrServiceInner_->SetTaskHandler(taskHandler_);
157 appMgrServiceInner_->SetEventHandler(eventHandler_);
158 DelayedSingleton<CacheProcessManager>::GetInstance()->SetAppMgr(appMgrServiceInner_);
159 std::function<void()> initAppMgrServiceInnerTask = [appMgrServiceInner = appMgrServiceInner_]() {
160 appMgrServiceInner->Init();
161 };
162 taskHandler_->SubmitTask(initAppMgrServiceInnerTask, TASK_INIT_APPMGRSERVICEINNER);
163
164 ErrCode openErr = appMgrServiceInner_->OpenAppSpawnConnection();
165 if (FAILED(openErr)) {
166 TAG_LOGW(AAFwkTag::APPMGR, "failed to connect to AppSpawnDaemon! errCode: %{public}08x", openErr);
167 }
168 if (!Publish(this)) {
169 TAG_LOGE(AAFwkTag::APPMGR, "failed to publish app mgr service to systemAbilityMgr");
170 return ERR_APPEXECFWK_SERVICE_NOT_CONNECTED;
171 }
172 amsMgrScheduler_ = new (std::nothrow) AmsMgrScheduler(appMgrServiceInner_, taskHandler_);
173 if (!amsMgrScheduler_) {
174 TAG_LOGE(AAFwkTag::APPMGR, "init failed without ability manager service scheduler");
175 return ERR_INVALID_OPERATION;
176 }
177 TAG_LOGI(AAFwkTag::APPMGR, "init success");
178 return ERR_OK;
179 }
180
AttachApplication(const sptr<IRemoteObject> & app)181 void AppMgrService::AttachApplication(const sptr<IRemoteObject> &app)
182 {
183 TAG_LOGD(AAFwkTag::APPMGR, "called");
184 if (!IsReady()) {
185 TAG_LOGE(AAFwkTag::APPMGR, "AttachApplication failed, not ready.");
186 return;
187 }
188
189 AbilityRuntime::FreezeUtil::GetInstance().AddAppLifecycleEvent(IPCSkeleton::GetCallingPid(),
190 "AppMgrService::AttachApplication");
191 pid_t pid = IPCSkeleton::GetCallingPid();
192 auto appScheduler = iface_cast<IAppScheduler>(app);
193 if (appScheduler == nullptr) {
194 TAG_LOGE(AAFwkTag::APPMGR, "appScheduler null");
195 }
196 std::function<void()> attachApplicationFunc = [appMgrServiceInner = appMgrServiceInner_, pid, appScheduler]() {
197 appMgrServiceInner->AttachApplication(pid, appScheduler);
198 };
199 taskHandler_->SubmitTask(attachApplicationFunc, AAFwk::TaskAttribute{
200 .taskName_ = TASK_ATTACH_APPLICATION,
201 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
202 });
203 }
204
PreloadApplication(const std::string & bundleName,int32_t userId,AppExecFwk::PreloadMode preloadMode,int32_t appIndex)205 int32_t AppMgrService::PreloadApplication(const std::string &bundleName, int32_t userId,
206 AppExecFwk::PreloadMode preloadMode, int32_t appIndex)
207 {
208 TAG_LOGD(AAFwkTag::APPMGR, "PreloadApplication called");
209 if (!IsReady()) {
210 TAG_LOGE(AAFwkTag::APPMGR, "PreloadApplication failed, appMgr not ready.");
211 return ERR_INVALID_OPERATION;
212 }
213 return appMgrServiceInner_->PreloadApplication(bundleName, userId, preloadMode, appIndex);
214 }
215
ApplicationForegrounded(const int32_t recordId)216 void AppMgrService::ApplicationForegrounded(const int32_t recordId)
217 {
218 if (!IsReady()) {
219 return;
220 }
221 if (!JudgeAppSelfCalled(recordId)) {
222 return;
223 }
224 AbilityRuntime::FreezeUtil::GetInstance().AddAppLifecycleEvent(IPCSkeleton::GetCallingPid(),
225 "AppMgrService::AppForegrounded");
226 std::function<void()> applicationForegroundedFunc = [appMgrServiceInner = appMgrServiceInner_, recordId]() {
227 appMgrServiceInner->ApplicationForegrounded(recordId);
228 };
229 taskHandler_->SubmitTask(applicationForegroundedFunc, AAFwk::TaskAttribute{
230 .taskName_ = TASK_APPLICATION_FOREGROUNDED,
231 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
232 });
233 }
234
ApplicationBackgrounded(const int32_t recordId)235 void AppMgrService::ApplicationBackgrounded(const int32_t recordId)
236 {
237 if (!IsReady()) {
238 return;
239 }
240 if (!JudgeAppSelfCalled(recordId)) {
241 return;
242 }
243 AbilityRuntime::FreezeUtil::GetInstance().AddAppLifecycleEvent(IPCSkeleton::GetCallingPid(),
244 "AppMgrService::AppBackgrounded");
245 taskHandler_->CancelTask("appbackground_" + std::to_string(recordId));
246 std::function<void()> applicationBackgroundedFunc = [appMgrServiceInner = appMgrServiceInner_, recordId]() {
247 appMgrServiceInner->ApplicationBackgrounded(recordId);
248 };
249 taskHandler_->SubmitTask(applicationBackgroundedFunc, AAFwk::TaskAttribute{
250 .taskName_ = TASK_APPLICATION_BACKGROUNDED,
251 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
252 });
253 }
254
ApplicationTerminated(const int32_t recordId)255 void AppMgrService::ApplicationTerminated(const int32_t recordId)
256 {
257 if (!IsReady()) {
258 return;
259 }
260 if (!JudgeAppSelfCalled(recordId)) {
261 return;
262 }
263 std::function<void()> applicationTerminatedFunc = [appMgrServiceInner = appMgrServiceInner_, recordId]() {
264 appMgrServiceInner->ApplicationTerminated(recordId);
265 };
266 taskHandler_->SubmitTask(applicationTerminatedFunc, AAFwk::TaskAttribute{
267 .taskName_ = TASK_APPLICATION_TERMINATED,
268 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
269 });
270 }
271
AbilityCleaned(const sptr<IRemoteObject> & token)272 void AppMgrService::AbilityCleaned(const sptr<IRemoteObject> &token)
273 {
274 if (!IsReady()) {
275 return;
276 }
277
278 auto callerUid = IPCSkeleton::GetCallingUid();
279 auto appRecord = appMgrServiceInner_->GetTerminatingAppRunningRecord(token);
280 if (!appRecord || appRecord->GetUid() != callerUid) {
281 TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed.");
282 return;
283 }
284
285 std::function<void()> abilityCleanedFunc = [appMgrServiceInner = appMgrServiceInner_, token]() {
286 appMgrServiceInner->AbilityTerminated(token);
287 };
288 taskHandler_->SubmitTask(abilityCleanedFunc, AAFwk::TaskAttribute{
289 .taskName_ = TASK_ABILITY_CLEANED,
290 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
291 });
292 }
293
IsReady() const294 bool AppMgrService::IsReady() const
295 {
296 if (appMgrServiceInner_ && taskHandler_ && eventHandler_) {
297 return true;
298 }
299
300 TAG_LOGW(AAFwkTag::APPMGR, "Not ready");
301 return false;
302 }
303
StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> & bundleInfos)304 void AppMgrService::StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos)
305 {
306 if (!IsReady()) {
307 return;
308 }
309 pid_t callingPid = IPCSkeleton::GetCallingPid();
310 pid_t pid = getprocpid();
311 if (callingPid != pid) {
312 TAG_LOGE(AAFwkTag::APPMGR, "Not this process call.");
313 return;
314 }
315 TAG_LOGI(AAFwkTag::APPMGR, "Notify start resident process");
316 std::function <void()> startupResidentProcess = [appMgrServiceInner = appMgrServiceInner_, bundleInfos]() {
317 appMgrServiceInner->LoadResidentProcess(bundleInfos);
318 };
319 taskHandler_->SubmitTask(startupResidentProcess, AAFwk::TaskAttribute{
320 .taskName_ = TASK_STARTUP_RESIDENT_PROCESS,
321 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
322 });
323 }
324
GetAmsMgr()325 sptr<IAmsMgr> AppMgrService::GetAmsMgr()
326 {
327 return amsMgrScheduler_;
328 }
329
ClearUpApplicationData(const std::string & bundleName,int32_t appCloneIndex,int32_t userId)330 int32_t AppMgrService::ClearUpApplicationData(const std::string &bundleName, int32_t appCloneIndex, int32_t userId)
331 {
332 if (!AAFwk::PermissionVerification::GetInstance()->JudgeCallerIsAllowedToUseSystemAPI()) {
333 TAG_LOGE(AAFwkTag::APPMGR, "The caller is not system-app, can not use system-api");
334 return AAFwk::ERR_NOT_SYSTEM_APP;
335 }
336 if (!IsReady()) {
337 return ERR_INVALID_OPERATION;
338 }
339 std::shared_ptr<RemoteClientManager> remoteClientManager = std::make_shared<RemoteClientManager>();
340 if (remoteClientManager == nullptr) {
341 TAG_LOGE(AAFwkTag::APPMGR, "The remoteClientManager is nullptr.");
342 return ERR_INVALID_OPERATION;
343 }
344 auto bundleMgrHelper = remoteClientManager->GetBundleManagerHelper();
345 if (bundleMgrHelper == nullptr) {
346 TAG_LOGE(AAFwkTag::APPMGR, "The bundleMgrHelper is nullptr.");
347 return ERR_INVALID_OPERATION;
348 }
349 int32_t callingUid = IPCSkeleton::GetCallingUid();
350 if ((callingUid != 0 && callingUid != USER_UID) || userId < 0) {
351 std::string callerBundleName;
352 auto result = IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callingUid, callerBundleName));
353 if (result != ERR_OK) {
354 TAG_LOGE(AAFwkTag::APPMGR, "GetBundleName failed: %{public}d.", result);
355 return ERR_INVALID_OPERATION;
356 }
357 auto isCallingPerm = AAFwk::PermissionVerification::GetInstance()->VerifyCallingPermission(
358 AAFwk::PermissionConstants::PERMISSION_CLEAN_APPLICATION_DATA);
359 if (!isCallingPerm) {
360 TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed");
361 return AAFwk::CHECK_PERMISSION_FAILED;
362 }
363 }
364 if (appCloneIndex < 0 || appCloneIndex > AbilityRuntime::GlobalConstant::MAX_APP_CLONE_INDEX) {
365 TAG_LOGE(AAFwkTag::APPMGR, "appCloneIndex is invalid.");
366 return AAFwk::ERR_APP_CLONE_INDEX_INVALID;
367 }
368 pid_t pid = IPCSkeleton::GetCallingPid();
369 return appMgrServiceInner_->ClearUpApplicationData(bundleName, callingUid, pid, appCloneIndex, userId);
370 }
371
ClearUpApplicationDataBySelf(int32_t userId)372 int32_t AppMgrService::ClearUpApplicationDataBySelf(int32_t userId)
373 {
374 if (!IsReady()) {
375 return ERR_INVALID_OPERATION;
376 }
377 int32_t uid = IPCSkeleton::GetCallingUid();
378 pid_t pid = IPCSkeleton::GetCallingPid();
379 return appMgrServiceInner_->ClearUpApplicationDataBySelf(uid, pid, userId);
380 }
381
GetAllRunningProcesses(std::vector<RunningProcessInfo> & info)382 int32_t AppMgrService::GetAllRunningProcesses(std::vector<RunningProcessInfo> &info)
383 {
384 if (!IsReady()) {
385 return ERR_INVALID_OPERATION;
386 }
387 return appMgrServiceInner_->GetAllRunningProcesses(info);
388 }
389
GetRunningMultiAppInfoByBundleName(const std::string & bundleName,RunningMultiAppInfo & info)390 int32_t AppMgrService::GetRunningMultiAppInfoByBundleName(const std::string &bundleName,
391 RunningMultiAppInfo &info)
392 {
393 if (!IsReady()) {
394 return ERR_INVALID_OPERATION;
395 }
396
397 if (!AAFwk::PermissionVerification::GetInstance()->JudgeCallerIsAllowedToUseSystemAPI()) {
398 TAG_LOGE(AAFwkTag::ABILITYMGR, "The caller is not system-app, can not use system-api");
399 return ERR_INVALID_OPERATION;
400 }
401
402 bool isCallingPermission = AAFwk::PermissionVerification::GetInstance()->VerifyRunningInfoPerm();
403 if (!isCallingPermission) {
404 TAG_LOGE(AAFwkTag::APPMGR, "GetRunningMultiAppInfoByBundleName, Permission verification failed.");
405 return ERR_PERMISSION_DENIED;
406 }
407 return appMgrServiceInner_->GetRunningMultiAppInfoByBundleName(bundleName, info);
408 }
409
GetRunningProcessesByBundleType(BundleType bundleType,std::vector<RunningProcessInfo> & info)410 int32_t AppMgrService::GetRunningProcessesByBundleType(BundleType bundleType,
411 std::vector<RunningProcessInfo> &info)
412 {
413 if (!IsReady()) {
414 return ERR_INVALID_OPERATION;
415 }
416 return appMgrServiceInner_->GetRunningProcessesByBundleType(bundleType, info);
417 }
418
GetAllRenderProcesses(std::vector<RenderProcessInfo> & info)419 int32_t AppMgrService::GetAllRenderProcesses(std::vector<RenderProcessInfo> &info)
420 {
421 if (!IsReady()) {
422 return ERR_INVALID_OPERATION;
423 }
424 return appMgrServiceInner_->GetAllRenderProcesses(info);
425 }
426
GetAllChildrenProcesses(std::vector<ChildProcessInfo> & info)427 int AppMgrService::GetAllChildrenProcesses(std::vector<ChildProcessInfo> &info)
428 {
429 if (!IsReady()) {
430 return ERR_INVALID_OPERATION;
431 }
432 return appMgrServiceInner_->GetAllChildrenProcesses(info);
433 }
434
JudgeSandboxByPid(pid_t pid,bool & isSandbox)435 int32_t AppMgrService::JudgeSandboxByPid(pid_t pid, bool &isSandbox)
436 {
437 if (!IsReady()) {
438 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
439 return ERR_INVALID_OPERATION;
440 }
441 bool isCallingPermission =
442 AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(FOUNDATION_PROCESS);
443 if (!isCallingPermission) {
444 TAG_LOGE(AAFwkTag::APPMGR, "VerificationAllToken failed.");
445 return ERR_PERMISSION_DENIED;
446 }
447 auto appRunningRecord = appMgrServiceInner_->GetAppRunningRecordByPid(pid);
448 if (appRunningRecord && appRunningRecord->GetAppIndex() > AbilityRuntime::GlobalConstant::MAX_APP_CLONE_INDEX) {
449 isSandbox = true;
450 TAG_LOGD(AAFwkTag::APPMGR, "current app is a sandbox.");
451 return ERR_OK;
452 }
453 TAG_LOGD(AAFwkTag::APPMGR, "current app is not a sandbox.");
454 return ERR_OK;
455 }
456
GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> & info,int32_t userId)457 int32_t AppMgrService::GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId)
458 {
459 if (!IsReady()) {
460 return ERR_INVALID_OPERATION;
461 }
462 return appMgrServiceInner_->GetProcessRunningInfosByUserId(info, userId);
463 }
464
GetProcessRunningInformation(RunningProcessInfo & info)465 int32_t AppMgrService::GetProcessRunningInformation(RunningProcessInfo &info)
466 {
467 if (!IsReady()) {
468 return ERR_INVALID_OPERATION;
469 }
470 return appMgrServiceInner_->GetProcessRunningInformation(info);
471 }
472
NotifyMemoryLevel(int32_t level)473 int32_t AppMgrService::NotifyMemoryLevel(int32_t level)
474 {
475 if (!IsReady()) {
476 return ERR_INVALID_OPERATION;
477 }
478 return appMgrServiceInner_->NotifyMemoryLevel(level);
479 }
480
NotifyProcMemoryLevel(const std::map<pid_t,MemoryLevel> & procLevelMap)481 int32_t AppMgrService::NotifyProcMemoryLevel(const std::map<pid_t, MemoryLevel> &procLevelMap)
482 {
483 if (!IsReady()) {
484 return ERR_INVALID_OPERATION;
485 }
486 return appMgrServiceInner_->NotifyProcMemoryLevel(procLevelMap);
487 }
488
DumpHeapMemory(const int32_t pid,OHOS::AppExecFwk::MallocInfo & mallocInfo)489 int32_t AppMgrService::DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo)
490 {
491 if (!IsReady()) {
492 return ERR_INVALID_OPERATION;
493 }
494 return appMgrServiceInner_->DumpHeapMemory(pid, mallocInfo);
495 }
496
497 // Authenticate dump permissions
HasDumpPermission() const498 bool AppMgrService::HasDumpPermission() const
499 {
500 uint32_t callingTokenID = IPCSkeleton::GetCallingTokenID();
501 int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callingTokenID, "ohos.permission.DUMP");
502 if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
503 TAG_LOGE(AAFwkTag::APPMGR, "No dump permission, please check!");
504 return false;
505 }
506 return true;
507 }
508
DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo & info)509 int32_t AppMgrService::DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo &info)
510 {
511 if (!IsReady() || !HasDumpPermission()) {
512 return ERR_INVALID_OPERATION;
513 }
514 return appMgrServiceInner_->DumpJsHeapMemory(info);
515 }
516
AddAbilityStageDone(const int32_t recordId)517 void AppMgrService::AddAbilityStageDone(const int32_t recordId)
518 {
519 if (!IsReady()) {
520 return;
521 }
522 if (!JudgeAppSelfCalled(recordId)) {
523 return;
524 }
525 std::function <void()> addAbilityStageDone = [appMgrServiceInner = appMgrServiceInner_, recordId]() {
526 appMgrServiceInner->AddAbilityStageDone(recordId);
527 };
528 taskHandler_->SubmitTask(addAbilityStageDone, AAFwk::TaskAttribute{
529 .taskName_ = TASK_ADD_ABILITY_STAGE_DONE,
530 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
531 });
532 }
533
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer,const std::vector<std::string> & bundleNameList)534 int32_t AppMgrService::RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer,
535 const std::vector<std::string> &bundleNameList)
536 {
537 TAG_LOGD(AAFwkTag::APPMGR, "begin");
538 if (!IsReady()) {
539 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
540 return ERR_INVALID_OPERATION;
541 }
542 return appMgrServiceInner_->RegisterApplicationStateObserver(observer, bundleNameList);
543 }
544
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)545 int32_t AppMgrService::UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer)
546 {
547 TAG_LOGD(AAFwkTag::APPMGR, "begin");
548 if (!IsReady()) {
549 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
550 return ERR_INVALID_OPERATION;
551 }
552 return appMgrServiceInner_->UnregisterApplicationStateObserver(observer);
553 }
554
RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> & observer)555 int32_t AppMgrService::RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer)
556 {
557 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
558 TAG_LOGD(AAFwkTag::APPMGR, "called");
559 if (!IsReady()) {
560 TAG_LOGE(AAFwkTag::APPMGR, "Not ready.");
561 return ERR_INVALID_OPERATION;
562 }
563 return appMgrServiceInner_->RegisterAbilityForegroundStateObserver(observer);
564 }
565
UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> & observer)566 int32_t AppMgrService::UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer)
567 {
568 TAG_LOGD(AAFwkTag::APPMGR, "called");
569 if (!IsReady()) {
570 TAG_LOGE(AAFwkTag::APPMGR, "Not ready.");
571 return ERR_INVALID_OPERATION;
572 }
573 return appMgrServiceInner_->UnregisterAbilityForegroundStateObserver(observer);
574 }
575
GetForegroundApplications(std::vector<AppStateData> & list)576 int32_t AppMgrService::GetForegroundApplications(std::vector<AppStateData> &list)
577 {
578 TAG_LOGD(AAFwkTag::APPMGR, "begin");
579 if (!IsReady()) {
580 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
581 return ERR_INVALID_OPERATION;
582 }
583 return appMgrServiceInner_->GetForegroundApplications(list);
584 }
585
StartUserTestProcess(const AAFwk::Want & want,const sptr<IRemoteObject> & observer,const AppExecFwk::BundleInfo & bundleInfo,int32_t userId)586 int AppMgrService::StartUserTestProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer,
587 const AppExecFwk::BundleInfo &bundleInfo, int32_t userId)
588 {
589 if (!IsReady()) {
590 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
591 return ERR_INVALID_OPERATION;
592 }
593 if (!AAFwk::PermissionVerification::GetInstance()->IsShellCall()) {
594 TAG_LOGE(AAFwkTag::APPMGR, "StartUserTestProcess is not shell call.");
595 return ERR_INVALID_OPERATION;
596 }
597 std::function<void()> startUserTestProcessFunc = [appMgrServiceInner = appMgrServiceInner_,
598 want, observer, bundleInfo, userId]() {
599 appMgrServiceInner->StartUserTestProcess(want, observer, bundleInfo, userId);
600 };
601 taskHandler_->SubmitTask(startUserTestProcessFunc, TASK_START_USER_TEST_PROCESS);
602 return ERR_OK;
603 }
604
FinishUserTest(const std::string & msg,const int64_t & resultCode,const std::string & bundleName)605 int AppMgrService::FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName)
606 {
607 if (!IsReady()) {
608 TAG_LOGE(AAFwkTag::APPMGR, "Not ready");
609 return ERR_INVALID_OPERATION;
610 }
611 std::shared_ptr<RemoteClientManager> remoteClientManager = std::make_shared<RemoteClientManager>();
612 if (remoteClientManager == nullptr) {
613 TAG_LOGE(AAFwkTag::APPMGR, "The remoteClientManager is nullptr.");
614 return ERR_INVALID_OPERATION;
615 }
616 auto bundleMgrHelper = remoteClientManager->GetBundleManagerHelper();
617 if (bundleMgrHelper == nullptr) {
618 TAG_LOGE(AAFwkTag::APPMGR, "The bundleMgrHelper is nullptr.");
619 return ERR_INVALID_OPERATION;
620 }
621 int32_t callingUid = IPCSkeleton::GetCallingUid();
622 std::string callerBundleName;
623 auto result = IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callingUid, callerBundleName));
624 if (result == ERR_OK) {
625 TAG_LOGI(AAFwkTag::APPMGR, "The callingPid_ is %{public}s.", callerBundleName.c_str());
626 if (bundleName != callerBundleName) {
627 TAG_LOGE(AAFwkTag::APPMGR, "Not this process call.");
628 return ERR_INVALID_OPERATION;
629 }
630 } else {
631 TAG_LOGE(AAFwkTag::APPMGR, "GetBundleName failed: %{public}d.", result);
632 return ERR_INVALID_OPERATION;
633 }
634 pid_t callingPid = IPCSkeleton::GetCallingPid();
635 std::function<void()> finishUserTestProcessFunc = [appMgrServiceInner = appMgrServiceInner_, msg,
636 resultCode, bundleName, callingPid]() {
637 appMgrServiceInner->FinishUserTest(msg, resultCode, bundleName, callingPid);
638 };
639 taskHandler_->SubmitTask(finishUserTestProcessFunc, TASK_FINISH_USER_TEST);
640 return ERR_OK;
641 }
642
Dump(int fd,const std::vector<std::u16string> & args)643 int AppMgrService::Dump(int fd, const std::vector<std::u16string>& args)
644 {
645 TAG_LOGD(AAFwkTag::APPMGR, "called");
646 if (!IsReady()) {
647 TAG_LOGE(AAFwkTag::APPMGR, "not ready.");
648 return ERR_APPEXECFWK_HIDUMP_ERROR;
649 }
650
651 std::string result;
652 auto errCode = Dump(args, result);
653 int ret = dprintf(fd, "%s\n", result.c_str());
654 if (ret < 0) {
655 TAG_LOGE(AAFwkTag::APPMGR, "dprintf error.");
656 return ERR_APPEXECFWK_HIDUMP_ERROR;
657 }
658 return errCode;
659 }
660
Dump(const std::vector<std::u16string> & args,std::string & result)661 int AppMgrService::Dump(const std::vector<std::u16string>& args, std::string& result)
662 {
663 TAG_LOGD(AAFwkTag::APPMGR, "called");
664 auto size = args.size();
665 if (size == 0) {
666 return ShowHelp(args, result);
667 }
668
669 std::string optionKey = Str16ToStr8(args[0]);
670 if (optionKey == OPTION_KEY_HELP) {
671 return ShowHelp(args, result);
672 }
673 if (optionKey == OPTION_KEY_DUMP_IPC) {
674 return DumpIpc(args, result);
675 }
676 if (optionKey == OPTION_KEY_DUMP_FFRT) {
677 return DumpFfrt(args, result);
678 }
679 result.append("error: unkown option.\n");
680 TAG_LOGE(AAFwkTag::APPMGR, "option key %{public}s does not exist", optionKey.c_str());
681 return DumpErrorCode::ERR_UNKNOWN_OPTION_ERROR;
682 }
683
ShowHelp(const std::vector<std::u16string> & args,std::string & result)684 int AppMgrService::ShowHelp(const std::vector<std::u16string>& args, std::string& result)
685 {
686 result.append("Usage:\n")
687 .append("-h ")
688 .append("help text for the tool\n")
689 .append("--ffrt pid1[,pid2,pid3] ")
690 .append("dump ffrt info\n");
691
692 return ERR_OK;
693 }
694
DumpIpcAllInner(const AppMgrService::DumpIpcKey key,std::string & result)695 int AppMgrService::DumpIpcAllInner(const AppMgrService::DumpIpcKey key, std::string& result)
696 {
697 TAG_LOGI(AAFwkTag::APPMGR, "called");
698 switch (key) {
699 case KEY_DUMP_IPC_START:
700 return DumpIpcAllStart(result);
701 case KEY_DUMP_IPC_STOP:
702 return DumpIpcAllStop(result);
703 case KEY_DUMP_IPC_STAT:
704 return DumpIpcAllStat(result);
705 default: {
706 result.append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
707 .append(MSG_DUMP_FAIL_REASON_INTERNAL, strlen(MSG_DUMP_FAIL_REASON_INTERNAL));
708 TAG_LOGE(AAFwkTag::APPMGR, "dump ipc all function does not exist");
709 return DumpErrorCode::ERR_INTERNAL_ERROR;
710 }
711 }
712 }
713
DumpIpcWithPidInner(const AppMgrService::DumpIpcKey key,const std::string & optionPid,std::string & result)714 int AppMgrService::DumpIpcWithPidInner(const AppMgrService::DumpIpcKey key,
715 const std::string& optionPid, std::string& result)
716 {
717 TAG_LOGI(AAFwkTag::APPMGR, "called");
718 int32_t pid = -1;
719 char* end = nullptr;
720 pid = static_cast<int32_t>(std::strtol(optionPid.c_str(), &end, BASE_TEN));
721 if (end && *end != SIGN_TERMINAL) {
722 result.append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
723 .append(MSG_DUMP_FAIL_REASON_INVALILD_PID, strlen(MSG_DUMP_FAIL_REASON_INVALILD_PID));
724 TAG_LOGE(AAFwkTag::APPMGR, "invalid pid: %{public}s", optionPid.c_str());
725 return DumpErrorCode::ERR_INVALID_PID_ERROR;
726 }
727 if (pid < 0) {
728 result.append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
729 .append(MSG_DUMP_FAIL_REASON_INVALILD_PID, strlen(MSG_DUMP_FAIL_REASON_INVALILD_PID));
730 TAG_LOGE(AAFwkTag::APPMGR, "invalid pid: %{public}s", optionPid.c_str());
731 return DumpErrorCode::ERR_INVALID_PID_ERROR;
732 }
733
734 switch (key) {
735 case KEY_DUMP_IPC_START:
736 return DumpIpcStart(pid, result);
737 case KEY_DUMP_IPC_STOP:
738 return DumpIpcStop(pid, result);
739 case KEY_DUMP_IPC_STAT:
740 return DumpIpcStat(pid, result);
741 default: {
742 TAG_LOGE(AAFwkTag::APPMGR, "option key %{public}d does not exist", key);
743 result.append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
744 .append(MSG_DUMP_FAIL_REASON_INTERNAL, strlen(MSG_DUMP_FAIL_REASON_INTERNAL));
745 TAG_LOGE(AAFwkTag::APPMGR, "dump ipc function does not exist");
746 return DumpErrorCode::ERR_INTERNAL_ERROR;
747 }
748 }
749 }
750
DumpFfrtInner(const std::string & pidsRaw,std::string & result)751 int AppMgrService::DumpFfrtInner(const std::string& pidsRaw, std::string& result)
752 {
753 TAG_LOGI(AAFwkTag::APPMGR, "Called");
754 std::vector<std::string> pidsStr;
755 SplitStr(pidsRaw, ",", pidsStr);
756 if (pidsStr.empty()) {
757 TAG_LOGE(AAFwkTag::APPMGR, "no valid pids are found");
758 return DumpErrorCode::ERR_INVALID_PID_ERROR;
759 }
760 if (pidsStr.size() > MAX_DUMP_FFRT_PID_NUMBER) {
761 pidsStr.resize(MAX_DUMP_FFRT_PID_NUMBER);
762 }
763 std::vector<int32_t> pids;
764 for (const auto& pidStr : pidsStr) {
765 int pid = -1;
766 char* end = nullptr;
767 pid = static_cast<int32_t>(std::strtol(pidStr.c_str(), &end, BASE_TEN));
768 if (end && *end != SIGN_TERMINAL) {
769 TAG_LOGE(AAFwkTag::APPMGR, "invalid pid:%{public}s", pidStr.c_str());
770 continue;
771 }
772 if (pid < 0) {
773 TAG_LOGE(AAFwkTag::APPMGR, "invalid pid: %{public}s", pidStr.c_str());
774 continue;
775 }
776 TAG_LOGD(AAFwkTag::APPMGR, "valid pid:%{public}d", pid);
777 pids.push_back(pid);
778 }
779 TAG_LOGD(AAFwkTag::APPMGR, "number of valid pids:%{public}d", static_cast<int>(pids.size()));
780 if (pids.empty()) {
781 TAG_LOGE(AAFwkTag::APPMGR, "no valid pids are found");
782 return DumpErrorCode::ERR_INVALID_PID_ERROR;
783 }
784 return appMgrServiceInner_->DumpFfrt(pids, result);
785 }
786
GetDumpIpcKeyByOption(const std::string & option,DumpIpcKey & key)787 bool AppMgrService::GetDumpIpcKeyByOption(const std::string &option, DumpIpcKey &key)
788 {
789 if (option == "--start-stat") {
790 key = KEY_DUMP_IPC_START;
791 return true;
792 }
793 if (option == "--stop-stat") {
794 key = KEY_DUMP_IPC_STOP;
795 return true;
796 }
797 if (option == "--stat") {
798 key = KEY_DUMP_IPC_STAT;
799 return true;
800 }
801 return false;
802 }
803
DumpIpc(const std::vector<std::u16string> & args,std::string & result)804 int AppMgrService::DumpIpc(const std::vector<std::u16string>& args, std::string& result)
805 {
806 TAG_LOGD(AAFwkTag::APPMGR, "Called. AppMgrService::DumpIpc start");
807 if (args.size() != VALID_DUMP_IPC_ARG_SIZE) {
808 result.append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
809 .append(MSG_DUMP_FAIL_REASON_INVALILD_NUM_ARGS, strlen(MSG_DUMP_FAIL_REASON_INVALILD_NUM_ARGS));
810 TAG_LOGE(AAFwkTag::APPMGR, "invalid number of arguments");
811 return DumpErrorCode::ERR_INVALID_NUM_ARGS_ERROR;
812 }
813 auto isHidumperServiceCall = (IPCSkeleton::GetCallingUid() == HIDUMPER_SERVICE_UID);
814 if (!isHidumperServiceCall) {
815 result.append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
816 .append(MSG_DUMP_FAIL_REASON_PERMISSION_DENY, strlen(MSG_DUMP_FAIL_REASON_PERMISSION_DENY));
817 TAG_LOGE(AAFwkTag::APPMGR, "Permission deny.");
818 return DumpErrorCode::ERR_PERMISSION_DENY_ERROR;
819 }
820
821 std::string optionCmd = Str16ToStr8(args[INDEX_CMD]);
822 std::string optionPid = Str16ToStr8(args[INDEX_PID]);
823 TAG_LOGD(AAFwkTag::APPMGR, "option pid:%{public}s, option cmd:%{public}s",
824 optionPid.c_str(), optionCmd.c_str());
825
826 DumpIpcKey key;
827 if (!GetDumpIpcKeyByOption(optionCmd, key)) {
828 result.append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
829 .append(MSG_DUMP_FAIL_REASON_INVALILD_CMD, strlen(MSG_DUMP_FAIL_REASON_INVALILD_CMD));
830 TAG_LOGE(AAFwkTag::APPMGR, "option command %{public}s does not exist", optionCmd.c_str());
831 return DumpErrorCode::ERR_INVALID_CMD_ERROR;
832 }
833
834 if (optionPid == "-a" || optionPid == "all" || optionPid == "--all") {
835 return DumpIpcAllInner(key, result);
836 }
837 return DumpIpcWithPidInner(key, optionPid, result);
838 }
839
DumpFfrt(const std::vector<std::u16string> & args,std::string & result)840 int AppMgrService::DumpFfrt(const std::vector<std::u16string>& args, std::string& result)
841 {
842 TAG_LOGD(AAFwkTag::APPMGR, "Called. AppMgrService::DumpFfrt start");
843 if (args.size() != VALID_DUMP_FFRT_ARG_SIZE) {
844 result.append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
845 .append(MSG_DUMP_FAIL_REASON_INVALILD_NUM_ARGS, strlen(MSG_DUMP_FAIL_REASON_INVALILD_NUM_ARGS));
846 TAG_LOGE(AAFwkTag::APPMGR, "invalid number of arguments");
847 return DumpErrorCode::ERR_INVALID_NUM_ARGS_ERROR;
848 }
849 auto isHidumperServiceCall = (IPCSkeleton::GetCallingUid() == HIDUMPER_SERVICE_UID);
850 if (!isHidumperServiceCall) {
851 result.append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
852 .append(MSG_DUMP_FAIL_REASON_PERMISSION_DENY, strlen(MSG_DUMP_FAIL_REASON_PERMISSION_DENY));
853 TAG_LOGE(AAFwkTag::APPMGR, "Permission deny.");
854 return DumpErrorCode::ERR_PERMISSION_DENY_ERROR;
855 }
856
857 std::string pidsRaw = Str16ToStr8(args[INDEX_PID]);
858 TAG_LOGD(AAFwkTag::APPMGR, "pids:%{public}s", pidsRaw.c_str());
859
860 return DumpFfrtInner(pidsRaw, result);
861 }
862
DumpIpcAllStart(std::string & result)863 int AppMgrService::DumpIpcAllStart(std::string& result)
864 {
865 TAG_LOGD(AAFwkTag::APPMGR, "called");
866 return appMgrServiceInner_->DumpIpcAllStart(result);
867 }
868
DumpIpcAllStop(std::string & result)869 int AppMgrService::DumpIpcAllStop(std::string& result)
870 {
871 TAG_LOGD(AAFwkTag::APPMGR, "called");
872 return appMgrServiceInner_->DumpIpcAllStop(result);
873 }
874
DumpIpcAllStat(std::string & result)875 int AppMgrService::DumpIpcAllStat(std::string& result)
876 {
877 TAG_LOGD(AAFwkTag::APPMGR, "called");
878 return appMgrServiceInner_->DumpIpcAllStat(result);
879 }
880
DumpIpcStart(const int32_t pid,std::string & result)881 int AppMgrService::DumpIpcStart(const int32_t pid, std::string& result)
882 {
883 TAG_LOGD(AAFwkTag::APPMGR, "called");
884 return appMgrServiceInner_->DumpIpcStart(pid, result);
885 }
886
DumpIpcStop(const int32_t pid,std::string & result)887 int AppMgrService::DumpIpcStop(const int32_t pid, std::string& result)
888 {
889 TAG_LOGD(AAFwkTag::APPMGR, "called");
890 return appMgrServiceInner_->DumpIpcStop(pid, result);
891 }
892
DumpIpcStat(const int32_t pid,std::string & result)893 int AppMgrService::DumpIpcStat(const int32_t pid, std::string& result)
894 {
895 TAG_LOGD(AAFwkTag::APPMGR, "called");
896 return appMgrServiceInner_->DumpIpcStat(pid, result);
897 }
898
ScheduleAcceptWantDone(const int32_t recordId,const AAFwk::Want & want,const std::string & flag)899 void AppMgrService::ScheduleAcceptWantDone(const int32_t recordId, const AAFwk::Want &want, const std::string &flag)
900 {
901 if (!IsReady()) {
902 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
903 return;
904 }
905 if (!JudgeAppSelfCalled(recordId)) {
906 return;
907 }
908 auto task = [appMgrServiceInner = appMgrServiceInner_, recordId, want, flag]() {
909 appMgrServiceInner->ScheduleAcceptWantDone(recordId, want, flag);
910 };
911 taskHandler_->SubmitTask(task, "ScheduleAcceptWantDone");
912 }
913
ScheduleNewProcessRequestDone(const int32_t recordId,const AAFwk::Want & want,const std::string & flag)914 void AppMgrService::ScheduleNewProcessRequestDone(const int32_t recordId, const AAFwk::Want &want,
915 const std::string &flag)
916 {
917 if (!IsReady()) {
918 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
919 return;
920 }
921 if (!JudgeAppSelfCalled(recordId)) {
922 return;
923 }
924 auto task = [appMgrServiceInner = appMgrServiceInner_, recordId, want, flag]() {
925 appMgrServiceInner->ScheduleNewProcessRequestDone(recordId, want, flag);
926 };
927 taskHandler_->SubmitTask(task, AAFwk::TaskAttribute{
928 .taskName_ = "ScheduleNewProcessRequestDone",
929 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
930 });
931 }
932
GetAbilityRecordsByProcessID(const int pid,std::vector<sptr<IRemoteObject>> & tokens)933 int AppMgrService::GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens)
934 {
935 if (!IsReady()) {
936 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
937 return ERR_INVALID_OPERATION;
938 }
939 auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall();
940 if (!isSaCall) {
941 TAG_LOGE(AAFwkTag::APPMGR, "Not SA call.");
942 return ERR_INVALID_OPERATION;
943 }
944 return appMgrServiceInner_->GetAbilityRecordsByProcessID(pid, tokens);
945 }
946
PreStartNWebSpawnProcess()947 int32_t AppMgrService::PreStartNWebSpawnProcess()
948 {
949 TAG_LOGI(AAFwkTag::APPMGR, "PreStartNWebSpawnProcess");
950 if (!IsReady()) {
951 TAG_LOGE(AAFwkTag::APPMGR, "PreStartNWebSpawnProcess failed, AppMgrService not ready.");
952 return ERR_INVALID_OPERATION;
953 }
954
955 return appMgrServiceInner_->PreStartNWebSpawnProcess(IPCSkeleton::GetCallingPid());
956 }
957
StartRenderProcess(const std::string & renderParam,int32_t ipcFd,int32_t sharedFd,int32_t crashFd,pid_t & renderPid,bool isGPU)958 int32_t AppMgrService::StartRenderProcess(const std::string &renderParam, int32_t ipcFd,
959 int32_t sharedFd, int32_t crashFd, pid_t &renderPid, bool isGPU)
960 {
961 FdGuard ipcFdGuard(ipcFd);
962 FdGuard sharedFdGuard(sharedFd);
963 FdGuard crashFdGuard(crashFd);
964 if (!IsReady()) {
965 TAG_LOGE(AAFwkTag::APPMGR, "StartRenderProcess failed, AppMgrService not ready.");
966 return ERR_INVALID_OPERATION;
967 }
968
969 auto result = appMgrServiceInner_->StartRenderProcess(IPCSkeleton::GetCallingPid(),
970 renderParam, ipcFd, sharedFd, crashFd, renderPid, isGPU);
971 if (result == ERR_OK) {
972 ipcFdGuard.Release();
973 sharedFdGuard.Release();
974 crashFdGuard.Release();
975 }
976 return result;
977 }
978
AttachRenderProcess(const sptr<IRemoteObject> & scheduler)979 void AppMgrService::AttachRenderProcess(const sptr<IRemoteObject> &scheduler)
980 {
981 TAG_LOGD(AAFwkTag::APPMGR, "called");
982 if (!IsReady()) {
983 TAG_LOGE(AAFwkTag::APPMGR, "AttachRenderProcess failed, not ready.");
984 return;
985 }
986
987 auto pid = IPCSkeleton::GetCallingPid();
988 auto fun = [appMgrServiceInner = appMgrServiceInner_, pid, scheduler]() {
989 appMgrServiceInner->AttachRenderProcess(pid, iface_cast<IRenderScheduler>(scheduler));
990 };
991 taskHandler_->SubmitTask(fun, AAFwk::TaskAttribute{
992 .taskName_ = TASK_ATTACH_RENDER_PROCESS,
993 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
994 });
995 }
996
SaveBrowserChannel(sptr<IRemoteObject> browser)997 void AppMgrService::SaveBrowserChannel(sptr<IRemoteObject> browser)
998 {
999 if (!IsReady()) {
1000 TAG_LOGE(AAFwkTag::APPMGR, "SaveBrowserChannel not ready");
1001 return;
1002 }
1003
1004 appMgrServiceInner_->SaveBrowserChannel(IPCSkeleton::GetCallingPid(), browser);
1005 }
1006
GetRenderProcessTerminationStatus(pid_t renderPid,int & status)1007 int32_t AppMgrService::GetRenderProcessTerminationStatus(pid_t renderPid, int &status)
1008 {
1009 if (!IsReady()) {
1010 TAG_LOGE(AAFwkTag::APPMGR, "GetRenderProcessTerminationStatus failed, AppMgrService not ready.");
1011 return ERR_INVALID_OPERATION;
1012 }
1013
1014 return appMgrServiceInner_->GetRenderProcessTerminationStatus(renderPid, status);
1015 }
1016
GetConfiguration(Configuration & config)1017 int32_t AppMgrService::GetConfiguration(Configuration& config)
1018 {
1019 if (!IsReady()) {
1020 TAG_LOGE(AAFwkTag::APPMGR, "GetConfiguration failed, AppMgrService not ready.");
1021 return ERR_INVALID_OPERATION;
1022 }
1023 config = *(appMgrServiceInner_->GetConfiguration());
1024 return ERR_OK;
1025 }
1026
UpdateConfiguration(const Configuration & config,const int32_t userId)1027 int32_t AppMgrService::UpdateConfiguration(const Configuration& config, const int32_t userId)
1028 {
1029 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1030 if (!IsReady()) {
1031 TAG_LOGE(AAFwkTag::APPMGR, "UpdateConfiguration failed, AppMgrService not ready.");
1032 return ERR_INVALID_OPERATION;
1033 }
1034 return appMgrServiceInner_->UpdateConfiguration(config, userId);
1035 }
1036
UpdateConfigurationByBundleName(const Configuration & config,const std::string & name)1037 int32_t AppMgrService::UpdateConfigurationByBundleName(const Configuration& config, const std::string &name)
1038 {
1039 if (!IsReady()) {
1040 TAG_LOGE(AAFwkTag::APPMGR, "UpdateConfigurationByBundleName failed, AppMgrService not ready.");
1041 return ERR_INVALID_OPERATION;
1042 }
1043 return appMgrServiceInner_->UpdateConfigurationByBundleName(config, name);
1044 }
1045
RegisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)1046 int32_t AppMgrService::RegisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
1047 {
1048 if (!IsReady()) {
1049 TAG_LOGE(AAFwkTag::APPMGR, "RegisterConfigurationObserver failed, AppMgrService not ready.");
1050 return ERR_INVALID_OPERATION;
1051 }
1052 return appMgrServiceInner_->RegisterConfigurationObserver(observer);
1053 }
1054
UnregisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)1055 int32_t AppMgrService::UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
1056 {
1057 if (!IsReady()) {
1058 TAG_LOGE(AAFwkTag::APPMGR, "UnregisterConfigurationObserver failed, AppMgrService not ready.");
1059 return ERR_INVALID_OPERATION;
1060 }
1061 return appMgrServiceInner_->UnregisterConfigurationObserver(observer);
1062 }
1063
GetAppRunningStateByBundleName(const std::string & bundleName)1064 bool AppMgrService::GetAppRunningStateByBundleName(const std::string &bundleName)
1065 {
1066 if (!IsReady()) {
1067 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1068 return false;
1069 }
1070
1071 return appMgrServiceInner_->GetAppRunningStateByBundleName(bundleName);
1072 }
1073
NotifyLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)1074 int32_t AppMgrService::NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
1075 {
1076 if (!IsReady()) {
1077 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1078 return ERR_INVALID_OPERATION;
1079 }
1080 return appMgrServiceInner_->NotifyLoadRepairPatch(bundleName, callback);
1081 }
1082
NotifyHotReloadPage(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)1083 int32_t AppMgrService::NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
1084 {
1085 if (!IsReady()) {
1086 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1087 return ERR_INVALID_OPERATION;
1088 }
1089 return appMgrServiceInner_->NotifyHotReloadPage(bundleName, callback);
1090 }
1091
1092 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
SetContinuousTaskProcess(int32_t pid,bool isContinuousTask)1093 int32_t AppMgrService::SetContinuousTaskProcess(int32_t pid, bool isContinuousTask)
1094 {
1095 if (!AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(FOUNDATION_PROCESS)) {
1096 TAG_LOGE(AAFwkTag::ABILITYMGR, "caller is not foundation.");
1097 return ERR_INVALID_OPERATION;
1098 }
1099 if (!IsReady()) {
1100 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1101 return ERR_INVALID_OPERATION;
1102 }
1103
1104 return appMgrServiceInner_->SetContinuousTaskProcess(pid, isContinuousTask);
1105 }
1106 #endif
1107
NotifyUnLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)1108 int32_t AppMgrService::NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
1109 {
1110 if (!IsReady()) {
1111 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1112 return ERR_INVALID_OPERATION;
1113 }
1114 return appMgrServiceInner_->NotifyUnLoadRepairPatch(bundleName, callback);
1115 }
1116
JudgeAppSelfCalled(int32_t recordId)1117 bool AppMgrService::JudgeAppSelfCalled(int32_t recordId)
1118 {
1119 if (appMgrServiceInner_ == nullptr) {
1120 return false;
1121 }
1122
1123 auto callingTokenId = IPCSkeleton::GetCallingTokenID();
1124 std::shared_ptr<AppRunningRecord> appRecord = appMgrServiceInner_->GetAppRunningRecordByAppRecordId(recordId);
1125 if (appRecord == nullptr || ((appRecord->GetApplicationInfo())->accessTokenId) != callingTokenId) {
1126 TAG_LOGE(AAFwkTag::APPMGR, "Is not self, not enabled");
1127 return false;
1128 }
1129
1130 return true;
1131 }
1132
IsSharedBundleRunning(const std::string & bundleName,uint32_t versionCode)1133 bool AppMgrService::IsSharedBundleRunning(const std::string &bundleName, uint32_t versionCode)
1134 {
1135 if (!IsReady()) {
1136 return ERR_INVALID_OPERATION;
1137 }
1138 return appMgrServiceInner_->IsSharedBundleRunning(bundleName, versionCode);
1139 }
1140
StartNativeProcessForDebugger(const AAFwk::Want & want)1141 int32_t AppMgrService::StartNativeProcessForDebugger(const AAFwk::Want &want)
1142 {
1143 if (!IsReady()) {
1144 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1145 return ERR_INVALID_OPERATION;
1146 }
1147 auto isShellCall = AAFwk::PermissionVerification::GetInstance()->IsShellCall();
1148 if (!isShellCall) {
1149 TAG_LOGE(AAFwkTag::APPMGR, "permission denied, only called by shell.");
1150 return ERR_INVALID_OPERATION;
1151 }
1152 auto ret = appMgrServiceInner_->StartNativeProcessForDebugger(want);
1153 if (ret != ERR_OK) {
1154 TAG_LOGE(AAFwkTag::APPMGR, "debuggablePipe fail to start native process.");
1155 }
1156 return ret;
1157 }
1158
GetBundleNameByPid(const int32_t pid,std::string & bundleName,int32_t & uid)1159 int32_t AppMgrService::GetBundleNameByPid(const int32_t pid, std::string &bundleName, int32_t &uid)
1160 {
1161 if (!IsReady()) {
1162 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1163 return ERR_INVALID_OPERATION;
1164 }
1165 return appMgrServiceInner_->GetBundleNameByPid(pid, bundleName, uid);
1166 }
1167
GetRunningProcessInfoByPid(const pid_t pid,OHOS::AppExecFwk::RunningProcessInfo & info)1168 int32_t AppMgrService::GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info)
1169 {
1170 if (!IsReady()) {
1171 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1172 return ERR_INVALID_OPERATION;
1173 }
1174 return appMgrServiceInner_->GetRunningProcessInfoByPid(pid, info);
1175 }
1176
NotifyAppFault(const FaultData & faultData)1177 int32_t AppMgrService::NotifyAppFault(const FaultData &faultData)
1178 {
1179 if (!IsReady()) {
1180 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1181 return ERR_INVALID_OPERATION;
1182 }
1183
1184 auto ret = appMgrServiceInner_->NotifyAppFault(faultData);
1185 if (ret != ERR_OK) {
1186 TAG_LOGE(AAFwkTag::APPMGR, "Notify fault data fail.");
1187 }
1188 return ret;
1189 }
1190
NotifyAppFaultBySA(const AppFaultDataBySA & faultData)1191 int32_t AppMgrService::NotifyAppFaultBySA(const AppFaultDataBySA &faultData)
1192 {
1193 if (!IsReady()) {
1194 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1195 return ERR_INVALID_OPERATION;
1196 }
1197
1198 auto ret = appMgrServiceInner_->NotifyAppFaultBySA(faultData);
1199 if (ret != ERR_OK) {
1200 TAG_LOGE(AAFwkTag::APPMGR, "Notify fault data fail.");
1201 }
1202 return ret;
1203 }
1204
SetAppFreezeFilter(int32_t pid)1205 bool AppMgrService::SetAppFreezeFilter(int32_t pid)
1206 {
1207 if (!IsReady()) {
1208 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1209 return ERR_INVALID_OPERATION;
1210 }
1211
1212 auto ret = appMgrServiceInner_->SetAppFreezeFilter(pid);
1213 if (!ret) {
1214 TAG_LOGE(AAFwkTag::APPMGR, "SetAppFreezeFilter fail.");
1215 }
1216 return ret;
1217 }
1218
GetProcessMemoryByPid(const int32_t pid,int32_t & memorySize)1219 int32_t AppMgrService::GetProcessMemoryByPid(const int32_t pid, int32_t &memorySize)
1220 {
1221 if (!IsReady()) {
1222 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1223 return ERR_INVALID_OPERATION;
1224 }
1225
1226 return appMgrServiceInner_->GetProcessMemoryByPid(pid, memorySize);
1227 }
1228
GetRunningProcessInformation(const std::string & bundleName,int32_t userId,std::vector<RunningProcessInfo> & info)1229 int32_t AppMgrService::GetRunningProcessInformation(const std::string &bundleName, int32_t userId,
1230 std::vector<RunningProcessInfo> &info)
1231 {
1232 if (!IsReady()) {
1233 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1234 return ERR_INVALID_OPERATION;
1235 }
1236
1237 return appMgrServiceInner_->GetRunningProcessInformation(bundleName, userId, info);
1238 }
1239
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)1240 void AppMgrService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
1241 {
1242 TAG_LOGI(AAFwkTag::APPMGR, "systemAbilityId: %{public}d add", systemAbilityId);
1243 if (!IsReady()) {
1244 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1245 return;
1246 }
1247
1248 if (systemAbilityId != WINDOW_MANAGER_SERVICE_ID) {
1249 return;
1250 }
1251
1252 appMgrServiceInner_->InitFocusListener();
1253 appMgrServiceInner_->InitWindowVisibilityChangedListener();
1254 }
1255
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)1256 void AppMgrService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
1257 {
1258 TAG_LOGI(AAFwkTag::APPMGR, "systemAbilityId: %{public}d remove", systemAbilityId);
1259 if (!IsReady()) {
1260 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1261 return;
1262 }
1263
1264 if (systemAbilityId != WINDOW_MANAGER_SERVICE_ID) {
1265 return;
1266 }
1267
1268 appMgrServiceInner_->FreeFocusListener();
1269 appMgrServiceInner_->FreeWindowVisibilityChangedListener();
1270 }
1271
ChangeAppGcState(pid_t pid,int32_t state)1272 int32_t AppMgrService::ChangeAppGcState(pid_t pid, int32_t state)
1273 {
1274 TAG_LOGD(AAFwkTag::APPMGR, "called");
1275 if (!appMgrServiceInner_) {
1276 return ERR_INVALID_VALUE;
1277 }
1278 return appMgrServiceInner_->ChangeAppGcState(pid, state);
1279 }
1280
NotifyPageShow(const sptr<IRemoteObject> & token,const PageStateData & pageStateData)1281 int32_t AppMgrService::NotifyPageShow(const sptr<IRemoteObject> &token, const PageStateData &pageStateData)
1282 {
1283 TAG_LOGD(AAFwkTag::APPMGR,
1284 "bundleName: %{public}s, moduelName: %{public}s, abilityName: %{public}s, pageName: %{public}s",
1285 pageStateData.bundleName.c_str(), pageStateData.moduleName.c_str(), pageStateData.abilityName.c_str(),
1286 pageStateData.pageName.c_str());
1287 if (!IsReady()) {
1288 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1289 return ERR_INVALID_OPERATION;
1290 }
1291 return appMgrServiceInner_->NotifyPageShow(token, pageStateData);
1292 }
1293
NotifyPageHide(const sptr<IRemoteObject> & token,const PageStateData & pageStateData)1294 int32_t AppMgrService::NotifyPageHide(const sptr<IRemoteObject> &token, const PageStateData &pageStateData)
1295 {
1296 TAG_LOGD(AAFwkTag::APPMGR,
1297 "bundleName: %{public}s, moduelName: %{public}s, abilityName: %{public}s, pageName: %{public}s",
1298 pageStateData.bundleName.c_str(), pageStateData.moduleName.c_str(), pageStateData.abilityName.c_str(),
1299 pageStateData.pageName.c_str());
1300 if (!IsReady()) {
1301 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1302 return ERR_INVALID_OPERATION;
1303 }
1304 return appMgrServiceInner_->NotifyPageHide(token, pageStateData);
1305 }
1306
RegisterAppRunningStatusListener(const sptr<IRemoteObject> & listener)1307 int32_t AppMgrService::RegisterAppRunningStatusListener(const sptr<IRemoteObject> &listener)
1308 {
1309 TAG_LOGD(AAFwkTag::APPMGR, "called");
1310 if (!IsReady()) {
1311 TAG_LOGE(AAFwkTag::APPMGR, "Not ready");
1312 return ERR_INVALID_OPERATION;
1313 }
1314 return appMgrServiceInner_->RegisterAppRunningStatusListener(listener);
1315 }
1316
UnregisterAppRunningStatusListener(const sptr<IRemoteObject> & listener)1317 int32_t AppMgrService::UnregisterAppRunningStatusListener(const sptr<IRemoteObject> &listener)
1318 {
1319 TAG_LOGD(AAFwkTag::APPMGR, "called");
1320 if (!IsReady()) {
1321 TAG_LOGE(AAFwkTag::APPMGR, "Not ready.");
1322 return ERR_INVALID_OPERATION;
1323 }
1324 return appMgrServiceInner_->UnregisterAppRunningStatusListener(listener);
1325 }
1326
RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> & observer)1327 int32_t AppMgrService::RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer)
1328 {
1329 TAG_LOGD(AAFwkTag::APPMGR, "called");
1330 if (!IsReady()) {
1331 TAG_LOGE(AAFwkTag::APPMGR, "Not ready.");
1332 return ERR_INVALID_OPERATION;
1333 }
1334 return appMgrServiceInner_->RegisterAppForegroundStateObserver(observer);
1335 }
1336
UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> & observer)1337 int32_t AppMgrService::UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer)
1338 {
1339 TAG_LOGD(AAFwkTag::APPMGR, "called");
1340 if (!IsReady()) {
1341 TAG_LOGE(AAFwkTag::APPMGR, "Not ready.");
1342 return ERR_INVALID_OPERATION;
1343 }
1344 return appMgrServiceInner_->UnregisterAppForegroundStateObserver(observer);
1345 }
1346
IsApplicationRunning(const std::string & bundleName,bool & isRunning)1347 int32_t AppMgrService::IsApplicationRunning(const std::string &bundleName, bool &isRunning)
1348 {
1349 if (!IsReady()) {
1350 return ERR_INVALID_OPERATION;
1351 }
1352 return appMgrServiceInner_->IsApplicationRunning(bundleName, isRunning);
1353 }
1354
IsAppRunning(const std::string & bundleName,int32_t appCloneIndex,bool & isRunning)1355 int32_t AppMgrService::IsAppRunning(const std::string &bundleName, int32_t appCloneIndex, bool &isRunning)
1356 {
1357 if (!IsReady()) {
1358 return ERR_INVALID_OPERATION;
1359 }
1360 return appMgrServiceInner_->IsAppRunning(bundleName, appCloneIndex, isRunning);
1361 }
1362
StartChildProcess(pid_t & childPid,const ChildProcessRequest & request)1363 int32_t AppMgrService::StartChildProcess(pid_t &childPid, const ChildProcessRequest &request)
1364 {
1365 TAG_LOGD(AAFwkTag::APPMGR, "called");
1366 std::vector<FdGuard> fds;
1367 for (const auto &[name, fd] : request.args.fds) {
1368 fds.emplace_back(fd);
1369 }
1370 if (!IsReady()) {
1371 TAG_LOGE(AAFwkTag::APPMGR, "StartChildProcess failed, AppMgrService not ready.");
1372 return ERR_INVALID_OPERATION;
1373 }
1374
1375 auto result = appMgrServiceInner_->StartChildProcess(IPCSkeleton::GetCallingPid(), childPid, request);
1376 if (result == ERR_OK) {
1377 for (auto &fd : fds) {
1378 fd.Release();
1379 }
1380 }
1381 return result;
1382 }
1383
GetChildProcessInfoForSelf(ChildProcessInfo & info)1384 int32_t AppMgrService::GetChildProcessInfoForSelf(ChildProcessInfo &info)
1385 {
1386 if (!IsReady()) {
1387 TAG_LOGE(AAFwkTag::APPMGR, "StartChildProcess failed, AppMgrService not ready.");
1388 return ERR_INVALID_OPERATION;
1389 }
1390 return appMgrServiceInner_->GetChildProcessInfoForSelf(info);
1391 }
1392
AttachChildProcess(const sptr<IRemoteObject> & childScheduler)1393 void AppMgrService::AttachChildProcess(const sptr<IRemoteObject> &childScheduler)
1394 {
1395 TAG_LOGD(AAFwkTag::APPMGR, "AttachChildProcess.");
1396 if (!IsReady()) {
1397 TAG_LOGE(AAFwkTag::APPMGR, "AttachChildProcess failed, not ready.");
1398 return;
1399 }
1400 if (!taskHandler_) {
1401 TAG_LOGE(AAFwkTag::APPMGR, "taskHandler_ is null.");
1402 return;
1403 }
1404 pid_t pid = IPCSkeleton::GetCallingPid();
1405 std::function<void()> task = [appMgrServiceInner = appMgrServiceInner_, pid, childScheduler]() {
1406 appMgrServiceInner->AttachChildProcess(pid, iface_cast<IChildScheduler>(childScheduler));
1407 };
1408 taskHandler_->SubmitTask(task, AAFwk::TaskAttribute{
1409 .taskName_ = TASK_ATTACH_CHILD_PROCESS,
1410 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
1411 });
1412 }
1413
ExitChildProcessSafely()1414 void AppMgrService::ExitChildProcessSafely()
1415 {
1416 if (!IsReady()) {
1417 TAG_LOGE(AAFwkTag::APPMGR, "ExitChildProcessSafely failed, AppMgrService not ready.");
1418 return;
1419 }
1420 if (!taskHandler_) {
1421 TAG_LOGE(AAFwkTag::APPMGR, "taskHandler_ is null.");
1422 return;
1423 }
1424 pid_t pid = IPCSkeleton::GetCallingPid();
1425 std::function<void()> task = [appMgrServiceInner = appMgrServiceInner_, pid]() {
1426 appMgrServiceInner->ExitChildProcessSafelyByChildPid(pid);
1427 };
1428 taskHandler_->SubmitTask(task, AAFwk::TaskAttribute{
1429 .taskName_ = TASK_EXIT_CHILD_PROCESS_SAFELY,
1430 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
1431 });
1432 }
1433
IsFinalAppProcess()1434 bool AppMgrService::IsFinalAppProcess()
1435 {
1436 if (!IsReady()) {
1437 TAG_LOGE(AAFwkTag::APPMGR, "Not ready.");
1438 return false;
1439 }
1440 return appMgrServiceInner_->IsFinalAppProcessByBundleName("");
1441 }
1442
RegisterRenderStateObserver(const sptr<IRenderStateObserver> & observer)1443 int32_t AppMgrService::RegisterRenderStateObserver(const sptr<IRenderStateObserver> &observer)
1444 {
1445 if (!IsReady()) {
1446 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService not ready.");
1447 return ERR_INVALID_OPERATION;
1448 }
1449
1450 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
1451 TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed");
1452 return ERR_PERMISSION_DENIED;
1453 }
1454 return appMgrServiceInner_->RegisterRenderStateObserver(observer);
1455 }
1456
UnregisterRenderStateObserver(const sptr<IRenderStateObserver> & observer)1457 int32_t AppMgrService::UnregisterRenderStateObserver(const sptr<IRenderStateObserver> &observer)
1458 {
1459 if (!IsReady()) {
1460 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService not ready.");
1461 return ERR_INVALID_OPERATION;
1462 }
1463
1464 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
1465 TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed");
1466 return ERR_PERMISSION_DENIED;
1467 }
1468 return appMgrServiceInner_->UnregisterRenderStateObserver(observer);
1469 }
1470
UpdateRenderState(pid_t renderPid,int32_t state)1471 int32_t AppMgrService::UpdateRenderState(pid_t renderPid, int32_t state)
1472 {
1473 if (!IsReady()) {
1474 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService not ready.");
1475 return ERR_INVALID_OPERATION;
1476 }
1477 return appMgrServiceInner_->UpdateRenderState(renderPid, state);
1478 }
1479
SignRestartAppFlag(int32_t uid)1480 int32_t AppMgrService::SignRestartAppFlag(int32_t uid)
1481 {
1482 if (!IsReady()) {
1483 TAG_LOGE(AAFwkTag::APPMGR, "Not ready.");
1484 return ERR_INVALID_OPERATION;
1485 }
1486 bool isCallingPermission =
1487 AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(FOUNDATION_PROCESS);
1488 if (!isCallingPermission) {
1489 TAG_LOGE(AAFwkTag::APPMGR, "VerificationAllToken failed.");
1490 return ERR_PERMISSION_DENIED;
1491 }
1492 return appMgrServiceInner_->SignRestartAppFlag(uid);
1493 }
1494
GetAppRunningUniqueIdByPid(pid_t pid,std::string & appRunningUniqueId)1495 int32_t AppMgrService::GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId)
1496 {
1497 if (!IsReady()) {
1498 TAG_LOGE(AAFwkTag::APPMGR, "GetAppRunningUniqueIdByPid, Not ready.");
1499 return ERR_INVALID_OPERATION;
1500 }
1501 bool isCallingPermission = AAFwk::PermissionVerification::GetInstance()->IsSACall() &&
1502 AAFwk::PermissionVerification::GetInstance()->VerifyRunningInfoPerm();
1503 if (!isCallingPermission) {
1504 TAG_LOGE(AAFwkTag::APPMGR, "GetAppRunningUniqueIdByPid, Not SA call or Permission verification failed.");
1505 return ERR_PERMISSION_DENIED;
1506 }
1507 return appMgrServiceInner_->GetAppRunningUniqueIdByPid(pid, appRunningUniqueId);
1508 }
1509
GetAllUIExtensionRootHostPid(pid_t pid,std::vector<pid_t> & hostPids)1510 int32_t AppMgrService::GetAllUIExtensionRootHostPid(pid_t pid, std::vector<pid_t> &hostPids)
1511 {
1512 if (!IsReady()) {
1513 TAG_LOGE(AAFwkTag::APPMGR, "Not ready.");
1514 return ERR_INVALID_OPERATION;
1515 }
1516
1517 return appMgrServiceInner_->GetAllUIExtensionRootHostPid(pid, hostPids);
1518 }
1519
GetAllUIExtensionProviderPid(pid_t hostPid,std::vector<pid_t> & providerPids)1520 int32_t AppMgrService::GetAllUIExtensionProviderPid(pid_t hostPid, std::vector<pid_t> &providerPids)
1521 {
1522 if (!IsReady()) {
1523 TAG_LOGE(AAFwkTag::APPMGR, "Not ready.");
1524 return ERR_INVALID_OPERATION;
1525 }
1526
1527 return appMgrServiceInner_->GetAllUIExtensionProviderPid(hostPid, providerPids);
1528 }
1529
NotifyMemorySizeStateChanged(bool isMemorySizeSufficent)1530 int32_t AppMgrService::NotifyMemorySizeStateChanged(bool isMemorySizeSufficent)
1531 {
1532 if (!IsReady()) {
1533 TAG_LOGE(AAFwkTag::APPMGR, "AppMgrService is not ready.");
1534 return ERR_INVALID_OPERATION;
1535 }
1536
1537 return appMgrServiceInner_->NotifyMemorySizeStateChanged(isMemorySizeSufficent);
1538 }
1539
StartNativeChildProcess(const std::string & libName,int32_t childProcessCount,const sptr<IRemoteObject> & callback)1540 int32_t AppMgrService::StartNativeChildProcess(const std::string &libName, int32_t childProcessCount,
1541 const sptr<IRemoteObject> &callback)
1542 {
1543 TAG_LOGI(AAFwkTag::APPMGR, "Called");
1544 if (!IsReady()) {
1545 TAG_LOGE(AAFwkTag::APPMGR, "Not ready.");
1546 return ERR_INVALID_OPERATION;
1547 }
1548
1549 return appMgrServiceInner_->StartNativeChildProcess(
1550 IPCSkeleton::GetCallingPid(), libName, childProcessCount, callback);
1551 }
1552
SetSupportedProcessCache(int32_t pid,bool isSupport)1553 int32_t AppMgrService::SetSupportedProcessCache(int32_t pid, bool isSupport)
1554 {
1555 TAG_LOGI(AAFwkTag::APPMGR, "Called");
1556 if (!IsReady()) {
1557 TAG_LOGE(AAFwkTag::APPMGR, "Not ready.");
1558 return ERR_INVALID_OPERATION;
1559 }
1560 if (!AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(FOUNDATION_PROCESS)) {
1561 TAG_LOGE(AAFwkTag::ABILITYMGR, "caller not foundation");
1562 return ERR_INVALID_OPERATION;
1563 }
1564 return appMgrServiceInner_->SetSupportedProcessCache(pid, isSupport);
1565 }
1566
SetAppAssertionPauseState(bool flag)1567 void AppMgrService::SetAppAssertionPauseState(bool flag)
1568 {
1569 TAG_LOGI(AAFwkTag::APPMGR, "Called");
1570 if (!IsReady()) {
1571 TAG_LOGE(AAFwkTag::APPMGR, "Not ready.");
1572 return;
1573 }
1574 return appMgrServiceInner_->SetAppAssertionPauseState(flag);
1575 }
1576
SetSupportedProcessCacheSelf(bool isSupport)1577 int32_t AppMgrService::SetSupportedProcessCacheSelf(bool isSupport)
1578 {
1579 TAG_LOGI(AAFwkTag::APPMGR, "Called");
1580 if (!IsReady()) {
1581 TAG_LOGE(AAFwkTag::APPMGR, "Not ready.");
1582 return ERR_INVALID_OPERATION;
1583 }
1584 return appMgrServiceInner_->SetSupportedProcessCacheSelf(isSupport);
1585 }
1586
CheckCallingIsUserTestMode(const pid_t pid,bool & isUserTest)1587 int32_t AppMgrService::CheckCallingIsUserTestMode(const pid_t pid, bool &isUserTest)
1588 {
1589 TAG_LOGD(AAFwkTag::APPMGR, "called");
1590 if (!appMgrServiceInner_) {
1591 return ERR_INVALID_VALUE;
1592 }
1593 return appMgrServiceInner_->CheckCallingIsUserTestModeInner(pid, isUserTest);
1594 }
1595
NotifyProcessDependedOnWeb()1596 int32_t AppMgrService::NotifyProcessDependedOnWeb()
1597 {
1598 TAG_LOGD(AAFwkTag::APPMGR, "called.");
1599 if (!appMgrServiceInner_) {
1600 TAG_LOGE(AAFwkTag::APPMGR, "Not ready.");
1601 return ERR_INVALID_VALUE;
1602 }
1603 return appMgrServiceInner_->NotifyProcessDependedOnWeb();
1604 }
1605
KillProcessDependedOnWeb()1606 void AppMgrService::KillProcessDependedOnWeb()
1607 {
1608 TAG_LOGD(AAFwkTag::APPMGR, "called.");
1609 if (!AAFwk::PermissionVerification::GetInstance()->VerifyKillProcessDependedOnWebPermission()) {
1610 TAG_LOGE(AAFwkTag::ABILITYMGR, "caller have not permission.");
1611 return;
1612 }
1613 if (!appMgrServiceInner_) {
1614 return;
1615 }
1616 appMgrServiceInner_->KillProcessDependedOnWeb();
1617 }
1618
RestartResidentProcessDependedOnWeb()1619 void AppMgrService::RestartResidentProcessDependedOnWeb()
1620 {
1621 TAG_LOGD(AAFwkTag::APPMGR, "called.");
1622 if (!AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(FOUNDATION_PROCESS)) {
1623 TAG_LOGE(AAFwkTag::ABILITYMGR, "caller is not foundation.");
1624 return;
1625 }
1626 if (!appMgrServiceInner_) {
1627 return;
1628 }
1629 appMgrServiceInner_->RestartResidentProcessDependedOnWeb();
1630 }
1631
GetAppIndexByPid(pid_t pid,int32_t & appIndex)1632 int32_t AppMgrService::GetAppIndexByPid(pid_t pid, int32_t &appIndex)
1633 {
1634 bool isCallingPermission =
1635 AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(FOUNDATION_PROCESS);
1636 if (!isCallingPermission) {
1637 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
1638 return ERR_PERMISSION_DENIED;
1639 }
1640 if (!appMgrServiceInner_) {
1641 TAG_LOGE(AAFwkTag::APPMGR, "appMgrServiceInner_ is nullptr");
1642 return ERR_INVALID_VALUE;
1643 }
1644 return appMgrServiceInner_->GetAppIndexByPid(pid, appIndex);
1645 }
1646 } // namespace AppExecFwk
1647 } // namespace OHOS
1648