• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "system_ability_definition.h"
20 
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 namespace {
25 REGISTER_SYSTEM_ABILITY_BY_ID(El5FilekeyManagerServiceAbility, EL5_FILEKEY_MANAGER_SERVICE_ID, false);
26 }
27 
El5FilekeyManagerServiceAbility(int32_t systemAbilityId,bool runOnCreate)28 El5FilekeyManagerServiceAbility::El5FilekeyManagerServiceAbility(int32_t systemAbilityId, bool runOnCreate)
29     : SystemAbility(systemAbilityId, runOnCreate), service_(nullptr)
30 {
31     LOG_INFO("El5FilekeyManagerServiceAbility start.");
32 }
33 
~El5FilekeyManagerServiceAbility()34 El5FilekeyManagerServiceAbility::~El5FilekeyManagerServiceAbility()
35 {
36     LOG_INFO("El5FilekeyManagerServiceAbility stop.");
37 }
38 
OnStart(const SystemAbilityOnDemandReason & startReason)39 void El5FilekeyManagerServiceAbility::OnStart(const SystemAbilityOnDemandReason &startReason)
40 {
41     LOG_INFO("OnStart called.");
42     std::string reasonName = startReason.GetName();
43     LOG_INFO("El5FilekeyManager onStart reason name:%{public}s", reasonName.c_str());
44 
45     if (service_ != nullptr) {
46         LOG_ERROR("The El5FilekeyManagerService has existed.");
47         return;
48     }
49 
50     service_ = DelayedSingleton<El5FilekeyManagerService>::GetInstance();
51     int32_t ret = service_->Init();
52     if (ret != EFM_SUCCESS) {
53         LOG_ERROR("Failed to init the El5FilekeyManagerService instance.");
54         return;
55     }
56 
57     AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
58     AddSystemAbilityListener(SCREENLOCK_SERVICE_ID);
59     AddSystemAbilityListener(TELEPHONY_CALL_MANAGER_SYS_ABILITY_ID);
60     AddSystemAbilityListener(TIME_SERVICE_ID);
61     AddSystemAbilityListener(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
62 
63     if (reasonName == "usual.event.SCREEN_LOCKED") {
64         service_->SetPolicyScreenLocked();
65     } else if (reasonName == "usual.event.USER_REMOVED" || reasonName == "usual.event.USER_STOPPED") {
66         std::string strUserId = startReason.GetValue();
67         int32_t userId = 0;
68         if (StrToInt(strUserId, userId)) {
69             LOG_INFO("El5 manager start, common event:%{public}s userId:%{public}d", reasonName.c_str(), userId);
70             service_->HandleUserCommonEvent(reasonName, userId);
71         } else {
72             LOG_ERROR("El5 manager start, invalid userId:%{public}s", strUserId.c_str());
73         }
74     }
75 
76     if (!Publish(service_.get())) {
77         LOG_ERROR("Failed to publish El5FilekeyManagerService to SystemAbilityMgr");
78         return;
79     }
80 }
81 
OnStop()82 void El5FilekeyManagerServiceAbility::OnStop()
83 {
84     LOG_INFO("OnStop called.");
85     if (service_) {
86         service_->UnInit();
87         service_ = nullptr;
88     }
89 }
90 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)91 void El5FilekeyManagerServiceAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
92 {
93     if (service_) {
94         service_->OnAddSystemAbility(systemAbilityId, deviceId);
95     }
96 }
97 }  // namespace AccessToken
98 }  // namespace Security
99 }  // namespace OHOS
100