• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 <thread>
20 
21 #include <nlohmann/json.hpp>
22 #include <sys/types.h>
23 
24 #include "datetime_ex.h"
25 #include "ipc_skeleton.h"
26 #include "system_ability_definition.h"
27 
28 #include "app_death_recipient.h"
29 #include "app_mgr_constants.h"
30 #include "hilog_wrapper.h"
31 #include "perf_profile.h"
32 #include "xcollie/watchdog.h"
33 
34 #include "permission_constants.h"
35 #include "permission_verification.h"
36 
37 namespace OHOS {
38 namespace AppExecFwk {
39 namespace {
40 using namespace std::chrono_literals;
41 #ifdef ABILITY_COMMAND_FOR_TEST
42 static const int APP_MS_BLOCK = 65;
43 #endif
44 const std::string TASK_ATTACH_APPLICATION = "AttachApplicationTask";
45 const std::string TASK_APPLICATION_FOREGROUNDED = "ApplicationForegroundedTask";
46 const std::string TASK_APPLICATION_BACKGROUNDED = "ApplicationBackgroundedTask";
47 const std::string TASK_APPLICATION_TERMINATED = "ApplicationTerminatedTask";
48 const std::string TASK_ABILITY_CLEANED = "AbilityCleanedTask";
49 const std::string TASK_ADD_APP_DEATH_RECIPIENT = "AddAppRecipientTask";
50 const std::string TASK_CLEAR_UP_APPLICATION_DATA = "ClearUpApplicationDataTask";
51 const std::string TASK_STARTUP_RESIDENT_PROCESS = "StartupResidentProcess";
52 const std::string TASK_ADD_ABILITY_STAGE_DONE = "AddAbilityStageDone";
53 const std::string TASK_START_USER_TEST_PROCESS = "StartUserTestProcess";
54 const std::string TASK_FINISH_USER_TEST = "FinishUserTest";
55 const std::string TASK_ATTACH_RENDER_PROCESS = "AttachRenderTask";
56 }  // namespace
57 
58 REGISTER_SYSTEM_ABILITY_BY_ID(AppMgrService, APP_MGR_SERVICE_ID, true);
59 
AppMgrService()60 AppMgrService::AppMgrService()
61 {
62     appMgrServiceInner_ = std::make_shared<AppMgrServiceInner>();
63     HILOG_INFO("instance created with no para");
64     PerfProfile::GetInstance().SetAmsLoadStartTime(GetTickCount());
65 }
66 
AppMgrService(const int32_t serviceId,bool runOnCreate)67 AppMgrService::AppMgrService(const int32_t serviceId, bool runOnCreate) : SystemAbility(serviceId, runOnCreate)
68 {
69     appMgrServiceInner_ = std::make_shared<AppMgrServiceInner>();
70     HILOG_INFO("instance created");
71     PerfProfile::GetInstance().SetAmsLoadStartTime(GetTickCount());
72 }
73 
~AppMgrService()74 AppMgrService::~AppMgrService()
75 {
76     HILOG_INFO("instance destroyed");
77 }
78 
OnStart()79 void AppMgrService::OnStart()
80 {
81     HILOG_INFO("ready to start service");
82     if (appMgrServiceState_.serviceRunningState == ServiceRunningState::STATE_RUNNING) {
83         HILOG_WARN("failed to start service since it's already running");
84         return;
85     }
86 
87     ErrCode errCode = Init();
88     if (FAILED(errCode)) {
89         HILOG_ERROR("init failed, errCode: %{public}08x", errCode);
90         return;
91     }
92     appMgrServiceState_.serviceRunningState = ServiceRunningState::STATE_RUNNING;
93     HILOG_INFO("start service success");
94     PerfProfile::GetInstance().SetAmsLoadEndTime(GetTickCount());
95     PerfProfile::GetInstance().Dump();
96 }
97 
OnStop()98 void AppMgrService::OnStop()
99 {
100     HILOG_INFO("ready to stop service");
101     appMgrServiceState_.serviceRunningState = ServiceRunningState::STATE_NOT_START;
102     handler_.reset();
103     runner_.reset();
104     if (appMgrServiceInner_) {
105         appMgrServiceInner_->OnStop();
106     }
107     HILOG_INFO("stop service success");
108 }
109 
SetInnerService(const std::shared_ptr<AppMgrServiceInner> & innerService)110 void AppMgrService::SetInnerService(const std::shared_ptr<AppMgrServiceInner> &innerService)
111 {
112     appMgrServiceInner_ = innerService;
113 }
114 
QueryServiceState()115 AppMgrServiceState AppMgrService::QueryServiceState()
116 {
117     if (appMgrServiceInner_) {
118         appMgrServiceState_.connectionState = appMgrServiceInner_->QueryAppSpawnConnectionState();
119     }
120     return appMgrServiceState_;
121 }
122 
Init()123 ErrCode AppMgrService::Init()
124 {
125     HILOG_INFO("ready to init");
126     // start main thread message loop.
127     runner_ = EventRunner::Create(Constants::APP_MGR_SERVICE_NAME);
128     if (!runner_) {
129         HILOG_ERROR("init failed due to create runner error");
130         return ERR_INVALID_OPERATION;
131     }
132     if (!appMgrServiceInner_) {
133         HILOG_ERROR("init failed without inner service");
134         return ERR_INVALID_OPERATION;
135     }
136 
137     handler_ = std::make_shared<AMSEventHandler>(runner_, appMgrServiceInner_);
138     appMgrServiceInner_->SetEventHandler(handler_);
139     appMgrServiceInner_->Init();
140 
141     ErrCode openErr = appMgrServiceInner_->OpenAppSpawnConnection();
142     if (FAILED(openErr)) {
143         HILOG_WARN("failed to connect to AppSpawnDaemon! errCode: %{public}08x", openErr);
144     }
145     if (!Publish(this)) {
146         HILOG_ERROR("failed to publish app mgr service to systemAbilityMgr");
147         return ERR_APPEXECFWK_SERVICE_NOT_CONNECTED;
148     }
149     amsMgrScheduler_ = new (std::nothrow) AmsMgrScheduler(appMgrServiceInner_, handler_);
150     if (!amsMgrScheduler_) {
151         HILOG_ERROR("init failed without ams scheduler");
152         return ERR_INVALID_OPERATION;
153     }
154     std::string threadName = Constants::APP_MGR_SERVICE_NAME + "(" + std::to_string(runner_->GetThreadId()) + ")";
155     if (HiviewDFX::Watchdog::GetInstance().AddThread(threadName, handler_) != 0) {
156         HILOG_ERROR("HiviewDFX::Watchdog::GetInstance AddThread Fail");
157     }
158     HILOG_INFO("init success");
159     return ERR_OK;
160 }
161 
CheckPermission(const int32_t recordId,const std::string & permission)162 int32_t AppMgrService::CheckPermission(
163     [[maybe_unused]] const int32_t recordId, [[maybe_unused]] const std::string &permission)
164 {
165     HILOG_INFO("check application's permission");
166 
167     return ERR_OK;
168 }
169 
AttachApplication(const sptr<IRemoteObject> & app)170 void AppMgrService::AttachApplication(const sptr<IRemoteObject> &app)
171 {
172     if (!IsReady()) {
173         HILOG_ERROR("AttachApplication failed, not ready.");
174         return;
175     }
176 
177     pid_t pid = IPCSkeleton::GetCallingPid();
178     AddAppDeathRecipient(pid);
179     std::function<void()> attachApplicationFunc =
180         std::bind(&AppMgrServiceInner::AttachApplication, appMgrServiceInner_, pid, iface_cast<IAppScheduler>(app));
181     handler_->PostTask(attachApplicationFunc, TASK_ATTACH_APPLICATION);
182 }
183 
ApplicationForegrounded(const int32_t recordId)184 void AppMgrService::ApplicationForegrounded(const int32_t recordId)
185 {
186     if (!IsReady()) {
187         return;
188     }
189     if (!JudgeSelfCalledByRecordId(recordId)) {
190         return;
191     }
192     std::function<void()> applicationForegroundedFunc =
193         std::bind(&AppMgrServiceInner::ApplicationForegrounded, appMgrServiceInner_, recordId);
194     handler_->PostTask(applicationForegroundedFunc, TASK_APPLICATION_FOREGROUNDED);
195 }
196 
ApplicationBackgrounded(const int32_t recordId)197 void AppMgrService::ApplicationBackgrounded(const int32_t recordId)
198 {
199     if (!IsReady()) {
200         return;
201     }
202     if (!JudgeSelfCalledByRecordId(recordId)) {
203         return;
204     }
205     std::function<void()> applicationBackgroundedFunc =
206         std::bind(&AppMgrServiceInner::ApplicationBackgrounded, appMgrServiceInner_, recordId);
207     handler_->PostTask(applicationBackgroundedFunc, TASK_APPLICATION_BACKGROUNDED);
208 }
209 
ApplicationTerminated(const int32_t recordId)210 void AppMgrService::ApplicationTerminated(const int32_t recordId)
211 {
212     if (!IsReady()) {
213         return;
214     }
215     if (!JudgeSelfCalledByRecordId(recordId)) {
216         return;
217     }
218     std::function<void()> applicationTerminatedFunc =
219         std::bind(&AppMgrServiceInner::ApplicationTerminated, appMgrServiceInner_, recordId);
220     handler_->PostTask(applicationTerminatedFunc, TASK_APPLICATION_TERMINATED);
221 }
222 
AbilityCleaned(const sptr<IRemoteObject> & token)223 void AppMgrService::AbilityCleaned(const sptr<IRemoteObject> &token)
224 {
225     if (!IsReady()) {
226         return;
227     }
228     std::function<void()> abilityCleanedFunc =
229         std::bind(&AppMgrServiceInner::AbilityTerminated, appMgrServiceInner_, token);
230     handler_->PostTask(abilityCleanedFunc, TASK_ABILITY_CLEANED);
231 }
232 
IsReady() const233 bool AppMgrService::IsReady() const
234 {
235     if (!appMgrServiceInner_) {
236         HILOG_ERROR("appMgrServiceInner is null");
237         return false;
238     }
239     if (!handler_) {
240         HILOG_ERROR("handler is null");
241         return false;
242     }
243     return true;
244 }
245 
AddAppDeathRecipient(const pid_t pid) const246 void AppMgrService::AddAppDeathRecipient(const pid_t pid) const
247 {
248     if (!IsReady()) {
249         return;
250     }
251     sptr<AppDeathRecipient> appDeathRecipient = new AppDeathRecipient();
252     appDeathRecipient->SetEventHandler(handler_);
253     appDeathRecipient->SetAppMgrServiceInner(appMgrServiceInner_);
254     std::function<void()> addAppRecipientFunc =
255         std::bind(&AppMgrServiceInner::AddAppDeathRecipient, appMgrServiceInner_, pid, appDeathRecipient);
256     handler_->PostTask(addAppRecipientFunc, TASK_ADD_APP_DEATH_RECIPIENT);
257 }
258 
StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> & bundleInfos)259 void AppMgrService::StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos)
260 {
261     if (!IsReady()) {
262         return;
263     }
264     pid_t callingPid = IPCSkeleton::GetCallingPid();
265     pid_t pid = getpid();
266     if (callingPid != pid) {
267         HILOG_ERROR("Not this process call.");
268         return;
269     }
270     HILOG_INFO("Notify start resident process");
271     std::function <void()> startupResidentProcess =
272         std::bind(&AppMgrServiceInner::LoadResidentProcess, appMgrServiceInner_, bundleInfos);
273     handler_->PostTask(startupResidentProcess, TASK_STARTUP_RESIDENT_PROCESS);
274 }
275 
GetAmsMgr()276 sptr<IAmsMgr> AppMgrService::GetAmsMgr()
277 {
278     return amsMgrScheduler_;
279 }
280 
ClearUpApplicationData(const std::string & bundleName)281 int32_t AppMgrService::ClearUpApplicationData(const std::string &bundleName)
282 {
283     auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall();
284     if (!isSaCall) {
285         auto isCallingPerm = AAFwk::PermissionVerification::GetInstance()->VerifyCallingPermission(
286             AAFwk::PermissionConstants::PERMISSION_CLEAN_APPLICATION_DATA);
287         if (!isCallingPerm) {
288             HILOG_ERROR("%{public}s: Permission verification failed", __func__);
289             return ERR_PERMISSION_DENIED;
290         }
291     }
292 
293     if (!IsReady()) {
294         return ERR_INVALID_OPERATION;
295     }
296     int32_t uid = IPCSkeleton::GetCallingUid();
297     pid_t pid = IPCSkeleton::GetCallingPid();
298     std::function<void()> clearUpApplicationDataFunc =
299         std::bind(&AppMgrServiceInner::ClearUpApplicationData, appMgrServiceInner_, bundleName, uid, pid);
300     handler_->PostTask(clearUpApplicationDataFunc, TASK_CLEAR_UP_APPLICATION_DATA);
301     return ERR_OK;
302 }
303 
GetAllRunningProcesses(std::vector<RunningProcessInfo> & info)304 int32_t AppMgrService::GetAllRunningProcesses(std::vector<RunningProcessInfo> &info)
305 {
306     if (!IsReady()) {
307         return ERR_INVALID_OPERATION;
308     }
309     return appMgrServiceInner_->GetAllRunningProcesses(info);
310 }
311 
GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> & info,int32_t userId)312 int32_t AppMgrService::GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId)
313 {
314     if (!IsReady()) {
315         return ERR_INVALID_OPERATION;
316     }
317     return appMgrServiceInner_->GetProcessRunningInfosByUserId(info, userId);
318 }
319 
GetProcessRunningInformation(RunningProcessInfo & info)320 int32_t AppMgrService::GetProcessRunningInformation(RunningProcessInfo &info)
321 {
322     if (!IsReady()) {
323         return ERR_INVALID_OPERATION;
324     }
325     return appMgrServiceInner_->GetProcessRunningInformation(info);
326 }
327 
NotifyMemoryLevel(int32_t level)328 int32_t AppMgrService::NotifyMemoryLevel(int32_t level)
329 {
330     if (!IsReady()) {
331         return ERR_INVALID_OPERATION;
332     }
333     return appMgrServiceInner_->NotifyMemoryLevel(level);
334 }
335 
AddAbilityStageDone(const int32_t recordId)336 void AppMgrService::AddAbilityStageDone(const int32_t recordId)
337 {
338     if (!IsReady()) {
339         return;
340     }
341     if (!JudgeSelfCalledByRecordId(recordId)) {
342         return;
343     }
344     std::function <void()> addAbilityStageDone =
345         std::bind(&AppMgrServiceInner::AddAbilityStageDone, appMgrServiceInner_, recordId);
346     handler_->PostTask(addAbilityStageDone, TASK_ADD_ABILITY_STAGE_DONE);
347 }
348 
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer,const std::vector<std::string> & bundleNameList)349 int32_t AppMgrService::RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer,
350     const std::vector<std::string> &bundleNameList)
351 {
352     HILOG_INFO("%{public}s begin", __func__);
353     if (!IsReady()) {
354         HILOG_ERROR("%{public}s begin, not ready", __func__);
355         return ERR_INVALID_OPERATION;
356     }
357     return appMgrServiceInner_->RegisterApplicationStateObserver(observer, bundleNameList);
358 }
359 
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)360 int32_t AppMgrService::UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer)
361 {
362     HILOG_INFO("%{public}s begin", __func__);
363     if (!IsReady()) {
364         HILOG_ERROR("%{public}s begin, not ready", __func__);
365         return ERR_INVALID_OPERATION;
366     }
367     return appMgrServiceInner_->UnregisterApplicationStateObserver(observer);
368 }
369 
GetForegroundApplications(std::vector<AppStateData> & list)370 int32_t AppMgrService::GetForegroundApplications(std::vector<AppStateData> &list)
371 {
372     HILOG_INFO("%{public}s begin", __func__);
373     if (!IsReady()) {
374         HILOG_ERROR("%{public}s begin, not ready", __func__);
375         return ERR_INVALID_OPERATION;
376     }
377     return appMgrServiceInner_->GetForegroundApplications(list);
378 }
379 
StartUserTestProcess(const AAFwk::Want & want,const sptr<IRemoteObject> & observer,const AppExecFwk::BundleInfo & bundleInfo,int32_t userId)380 int AppMgrService::StartUserTestProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer,
381     const AppExecFwk::BundleInfo &bundleInfo, int32_t userId)
382 {
383     if (!IsReady()) {
384         HILOG_ERROR("%{public}s begin, not ready", __func__);
385         return ERR_INVALID_OPERATION;
386     }
387     std::function<void()> startUserTestProcessFunc =
388         std::bind(&AppMgrServiceInner::StartUserTestProcess, appMgrServiceInner_, want, observer, bundleInfo, userId);
389     handler_->PostTask(startUserTestProcessFunc, TASK_START_USER_TEST_PROCESS);
390     return ERR_OK;
391 }
392 
FinishUserTest(const std::string & msg,const int64_t & resultCode,const std::string & bundleName)393 int AppMgrService::FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName)
394 {
395     if (!IsReady()) {
396         HILOG_ERROR("%{public}s begin, not ready", __func__);
397         return ERR_INVALID_OPERATION;
398     }
399     pid_t callingPid = IPCSkeleton::GetCallingPid();
400     std::function<void()> finishUserTestProcessFunc =
401         std::bind(&AppMgrServiceInner::FinishUserTest, appMgrServiceInner_, msg, resultCode, bundleName, callingPid);
402     handler_->PostTask(finishUserTestProcessFunc, TASK_FINISH_USER_TEST);
403     return ERR_OK;
404 }
405 
Dump(int fd,const std::vector<std::u16string> & args)406 int AppMgrService::Dump(int fd, const std::vector<std::u16string>& args)
407 {
408     if (!IsReady()) {
409         HILOG_ERROR("%{public}s, not ready.", __func__);
410         return ERR_APPEXECFWK_HIDUMP_ERROR;
411     }
412 
413     std::string result;
414     Dump(args, result);
415     int ret = dprintf(fd, "%s\n", result.c_str());
416     if (ret < 0) {
417         HILOG_ERROR("%{public}s, dprintf error.", __func__);
418         return ERR_APPEXECFWK_HIDUMP_ERROR;
419     }
420     return ERR_OK;
421 }
422 
Dump(const std::vector<std::u16string> & args,std::string & result) const423 void AppMgrService::Dump(const std::vector<std::u16string>& args, std::string& result) const
424 {
425     auto size = args.size();
426     if (size == 0) {
427         ShowHelp(result);
428         return;
429     }
430 
431     std::string optionKey = Str16ToStr8(args[0]);
432     if (optionKey != "-h") {
433         result.append("error: unkown option.\n");
434     }
435     ShowHelp(result);
436 }
437 
ShowHelp(std::string & result) const438 void AppMgrService::ShowHelp(std::string& result) const
439 {
440     result.append("Usage:\n")
441         .append("-h                          ")
442         .append("help text for the tool\n");
443 }
444 
ScheduleAcceptWantDone(const int32_t recordId,const AAFwk::Want & want,const std::string & flag)445 void AppMgrService::ScheduleAcceptWantDone(const int32_t recordId, const AAFwk::Want &want, const std::string &flag)
446 {
447     if (!IsReady()) {
448         HILOG_ERROR("%{public}s begin, not ready", __func__);
449         return;
450     }
451     if (!JudgeSelfCalledByRecordId(recordId)) {
452         return;
453     }
454     auto task = [=]() { appMgrServiceInner_->ScheduleAcceptWantDone(recordId, want, flag); };
455     handler_->PostTask(task);
456 }
457 
GetAbilityRecordsByProcessID(const int pid,std::vector<sptr<IRemoteObject>> & tokens)458 int AppMgrService::GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens)
459 {
460     if (!IsReady()) {
461         HILOG_ERROR("%{public}s begin, not ready", __func__);
462         return ERR_INVALID_OPERATION;
463     }
464     auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall();
465     if (!isSaCall) {
466         HILOG_ERROR("Not SA call.");
467         return ERR_INVALID_OPERATION;
468     }
469     return appMgrServiceInner_->GetAbilityRecordsByProcessID(pid, tokens);
470 }
471 
PreStartNWebSpawnProcess()472 int32_t AppMgrService::PreStartNWebSpawnProcess()
473 {
474     HILOG_INFO("PreStartNWebSpawnProcess");
475     if (!IsReady()) {
476         HILOG_ERROR("PreStartNWebSpawnProcess failed, AppMgrService not ready.");
477         return ERR_INVALID_OPERATION;
478     }
479 
480     return appMgrServiceInner_->PreStartNWebSpawnProcess(IPCSkeleton::GetCallingPid());
481 }
482 
StartRenderProcess(const std::string & renderParam,int32_t ipcFd,int32_t sharedFd,pid_t & renderPid)483 int32_t AppMgrService::StartRenderProcess(const std::string &renderParam, int32_t ipcFd,
484     int32_t sharedFd, pid_t &renderPid)
485 {
486     if (!IsReady()) {
487         HILOG_ERROR("StartRenderProcess failed, AppMgrService not ready.");
488         return ERR_INVALID_OPERATION;
489     }
490 
491     return appMgrServiceInner_->StartRenderProcess(IPCSkeleton::GetCallingPid(),
492         renderParam, ipcFd, sharedFd, renderPid);
493 }
494 
AttachRenderProcess(const sptr<IRemoteObject> & scheduler)495 void AppMgrService::AttachRenderProcess(const sptr<IRemoteObject> &scheduler)
496 {
497     HILOG_DEBUG("AttachRenderProcess called.");
498     if (!IsReady()) {
499         HILOG_ERROR("AttachRenderProcess failed, not ready.");
500         return;
501     }
502 
503     auto pid = IPCSkeleton::GetCallingPid();
504     auto fun = std::bind(&AppMgrServiceInner::AttachRenderProcess,
505         appMgrServiceInner_, pid, iface_cast<IRenderScheduler>(scheduler));
506     handler_->PostTask(fun, TASK_ATTACH_RENDER_PROCESS);
507 }
508 
GetRenderProcessTerminationStatus(pid_t renderPid,int & status)509 int32_t AppMgrService::GetRenderProcessTerminationStatus(pid_t renderPid, int &status)
510 {
511     if (!IsReady()) {
512         HILOG_ERROR("GetRenderProcessTerminationStatus failed, AppMgrService not ready.");
513         return ERR_INVALID_OPERATION;
514     }
515 
516     return appMgrServiceInner_->GetRenderProcessTerminationStatus(renderPid, status);
517 }
518 
GetConfiguration(Configuration & config)519 int32_t AppMgrService::GetConfiguration(Configuration& config)
520 {
521     if (!IsReady()) {
522         HILOG_ERROR("GetConfiguration failed, AppMgrService not ready.");
523         return ERR_INVALID_OPERATION;
524     }
525     config = *(appMgrServiceInner_->GetConfiguration());
526     return ERR_OK;
527 }
528 
UpdateConfiguration(const Configuration & config)529 int32_t AppMgrService::UpdateConfiguration(const Configuration& config)
530 {
531     if (!IsReady()) {
532         HILOG_ERROR("UpdateConfiguration failed, AppMgrService not ready.");
533         return ERR_INVALID_OPERATION;
534     }
535     return appMgrServiceInner_->UpdateConfiguration(config);
536 }
537 
RegisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)538 int32_t AppMgrService::RegisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
539 {
540     if (!IsReady()) {
541         HILOG_ERROR("RegisterConfigurationObserver failed, AppMgrService not ready.");
542         return ERR_INVALID_OPERATION;
543     }
544     return appMgrServiceInner_->RegisterConfigurationObserver(observer);
545 }
546 
UnregisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)547 int32_t AppMgrService::UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
548 {
549     if (!IsReady()) {
550         HILOG_ERROR("UnregisterConfigurationObserver failed, AppMgrService not ready.");
551         return ERR_INVALID_OPERATION;
552     }
553     return appMgrServiceInner_->UnregisterConfigurationObserver(observer);
554 }
555 
556 #ifdef ABILITY_COMMAND_FOR_TEST
BlockAppService()557 int AppMgrService::BlockAppService()
558 {
559     HILOG_DEBUG("%{public}s begin", __func__);
560     if (!IsReady()) {
561         return ERR_INVALID_OPERATION;
562     }
563     auto task = [=]() {
564         while (1) {
565             HILOG_DEBUG("%{public}s begin block app service", __func__);
566             std::this_thread::sleep_for(APP_MS_BLOCK*1s);
567         }
568     };
569     handler_->PostTask(task);
570     return ERR_OK;
571 }
572 #endif
573 
GetAppRunningStateByBundleName(const std::string & bundleName)574 bool AppMgrService::GetAppRunningStateByBundleName(const std::string &bundleName)
575 {
576     if (!IsReady()) {
577         HILOG_ERROR("AppMgrService is not ready.");
578         return false;
579     }
580 
581     return appMgrServiceInner_->GetAppRunningStateByBundleName(bundleName);
582 }
583 
NotifyLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)584 int32_t AppMgrService::NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
585 {
586     if (!IsReady()) {
587         HILOG_ERROR("AppMgrService is not ready.");
588         return ERR_INVALID_OPERATION;
589     }
590     auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall();
591     if (!isSaCall) {
592         return ERR_INVALID_OPERATION;
593     }
594     return appMgrServiceInner_->NotifyLoadRepairPatch(bundleName, callback);
595 }
596 
NotifyHotReloadPage(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)597 int32_t AppMgrService::NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
598 {
599     if (!IsReady()) {
600         HILOG_ERROR("AppMgrService is not ready.");
601         return ERR_INVALID_OPERATION;
602     }
603     auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall();
604     if (!isSaCall) {
605         return ERR_INVALID_OPERATION;
606     }
607     return appMgrServiceInner_->NotifyHotReloadPage(bundleName, callback);
608 }
609 
610 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
SetContinuousTaskProcess(int32_t pid,bool isContinuousTask)611 int32_t AppMgrService::SetContinuousTaskProcess(int32_t pid, bool isContinuousTask)
612 {
613     if (!IsReady()) {
614         HILOG_ERROR("AppMgrService is not ready.");
615         return ERR_INVALID_OPERATION;
616     }
617 
618     return appMgrServiceInner_->SetContinuousTaskProcess(pid, isContinuousTask);
619 }
620 #endif
621 
NotifyUnLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)622 int32_t AppMgrService::NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
623 {
624     if (!IsReady()) {
625         HILOG_ERROR("AppMgrService is not ready.");
626         return ERR_INVALID_OPERATION;
627     }
628     auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall();
629     if (!isSaCall) {
630         return ERR_INVALID_OPERATION;
631     }
632     return appMgrServiceInner_->NotifyUnLoadRepairPatch(bundleName, callback);
633 }
634 
JudgeSelfCalledByRecordId(int32_t recordId)635 bool AppMgrService::JudgeSelfCalledByRecordId(int32_t recordId)
636 {
637     auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall();
638     if (isSaCall) {
639         return true;
640     }
641 
642     if (appMgrServiceInner_ == nullptr) {
643         return false;
644     }
645 
646     auto callingTokenId = IPCSkeleton::GetCallingTokenID();
647     std::shared_ptr<AppRunningRecord> appRecord = appMgrServiceInner_->GetAppRunningRecordByAppRecordId(recordId);
648     if (appRecord == nullptr || ((appRecord->GetApplicationInfo())->accessTokenId) != callingTokenId) {
649         HILOG_ERROR("Is not self, not enabled");
650         return false;
651     }
652 
653     return true;
654 }
655 }  // namespace AppExecFwk
656 }  // namespace OHOS
657