1 /*
2 * Copyright (c) 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 "el5_filekey_manager_service_ability.h"
17
18 #include "el5_filekey_manager_error.h"
19 #include "el5_memory_manager.h"
20 #include "system_ability_definition.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25 namespace {
26 REGISTER_SYSTEM_ABILITY_BY_ID(El5FilekeyManagerServiceAbility, EL5_FILEKEY_MANAGER_SERVICE_ID, false);
27 constexpr int32_t SA_READY_TO_UNLOAD = 0;
28 constexpr int32_t SA_REFUSE_TO_UNLOAD = -1;
29 const std::string LOW_MEMORY_PARAM = "resourceschedule.memmgr.low.memory.prepare";
30 }
31
El5FilekeyManagerServiceAbility(int32_t systemAbilityId,bool runOnCreate)32 El5FilekeyManagerServiceAbility::El5FilekeyManagerServiceAbility(int32_t systemAbilityId, bool runOnCreate)
33 : SystemAbility(systemAbilityId, runOnCreate), service_(nullptr)
34 {
35 LOG_INFO("El5FilekeyManagerServiceAbility start.");
36 }
37
~El5FilekeyManagerServiceAbility()38 El5FilekeyManagerServiceAbility::~El5FilekeyManagerServiceAbility()
39 {
40 LOG_INFO("El5FilekeyManagerServiceAbility stop.");
41 }
42
OnStart(const SystemAbilityOnDemandReason & startReason)43 void El5FilekeyManagerServiceAbility::OnStart(const SystemAbilityOnDemandReason &startReason)
44 {
45 LOG_INFO("OnStart called.");
46 std::string reasonName = startReason.GetName();
47 LOG_INFO("El5FilekeyManager onStart reason name:%{public}s", reasonName.c_str());
48
49 if (service_ != nullptr) {
50 LOG_ERROR("The El5FilekeyManagerService has existed.");
51 return;
52 }
53
54 service_ = DelayedSingleton<El5FilekeyManagerService>::GetInstance();
55 int32_t ret = service_->Init();
56 if (ret != EFM_SUCCESS) {
57 LOG_ERROR("Failed to init the El5FilekeyManagerService instance.");
58 return;
59 }
60
61 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
62 AddSystemAbilityListener(SCREENLOCK_SERVICE_ID);
63 AddSystemAbilityListener(TELEPHONY_CALL_MANAGER_SYS_ABILITY_ID);
64 AddSystemAbilityListener(TIME_SERVICE_ID);
65 AddSystemAbilityListener(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
66 AddSystemAbilityListener(STORAGE_MANAGER_DAEMON_ID);
67
68 if (reasonName == "usual.event.SCREEN_LOCKED") {
69 service_->SetPolicyScreenLocked();
70 } else if (reasonName == "usual.event.USER_REMOVED" || reasonName == "usual.event.USER_STOPPED") {
71 std::string strUserId = startReason.GetValue();
72 int32_t userId = 0;
73 if (StrToInt(strUserId, userId)) {
74 LOG_INFO("El5 manager start, common event:%{public}s userId:%{public}d", reasonName.c_str(), userId);
75 service_->HandleUserCommonEvent(reasonName, userId);
76 } else {
77 LOG_ERROR("El5 manager start, invalid userId:%{public}s", strUserId.c_str());
78 }
79 }
80
81 if (!Publish(service_.get())) {
82 LOG_ERROR("Failed to publish El5FilekeyManagerService to SystemAbilityMgr");
83 return;
84 }
85 }
86
OnIdle(const SystemAbilityOnDemandReason & idleReason)87 int32_t El5FilekeyManagerServiceAbility::OnIdle(const SystemAbilityOnDemandReason &idleReason)
88 {
89 std::string reasonName = idleReason.GetName();
90 LOG_INFO("IldeReason name=%{public}s, value=%{public}s.", reasonName.c_str(), idleReason.GetValue().c_str());
91 if (reasonName != LOW_MEMORY_PARAM) {
92 return SA_READY_TO_UNLOAD;
93 }
94
95 if (El5MemoryManager::GetInstance().IsFunctionFinished() &&
96 El5MemoryManager::GetInstance().IsAllowUnloadService()) {
97 return SA_READY_TO_UNLOAD;
98 }
99
100 return SA_REFUSE_TO_UNLOAD;
101 }
102
OnStop()103 void El5FilekeyManagerServiceAbility::OnStop()
104 {
105 LOG_INFO("OnStop called.");
106 if (service_) {
107 service_->UnInit();
108 service_ = nullptr;
109 }
110 }
111
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)112 void El5FilekeyManagerServiceAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
113 {
114 if (service_) {
115 service_->OnAddSystemAbility(systemAbilityId, deviceId);
116 }
117 }
118 } // namespace AccessToken
119 } // namespace Security
120 } // namespace OHOS
121