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     if (service_ != nullptr) {
45         LOG_ERROR("The El5FilekeyManagerService has existed.");
46         return;
47     }
48 
49     service_ = DelayedSingleton<El5FilekeyManagerService>::GetInstance();
50     int32_t ret = service_->Init();
51     if (ret != EFM_SUCCESS) {
52         LOG_ERROR("Failed to init the El5FilekeyManagerService instance.");
53         return;
54     }
55 
56     if (reasonName == "usual.event.SCREEN_LOCKED") {
57         service_->SetPolicyScreenLocked();
58     } else if (reasonName == "usual.event.USER_REMOVED" || reasonName == "usual.event.USER_STOPPED") {
59         std::string strUserId = startReason.GetValue();
60         int32_t userId = 0;
61         if (StrToInt(strUserId, userId)) {
62             LOG_INFO("el5 manager start, common event:%{public}s userId:%{public}d", reasonName.c_str(), userId);
63             service_->HandleUserCommonEvent(reasonName, userId);
64         } else {
65             LOG_ERROR("el5 manager start, invalid userId:%{public}s", strUserId.c_str());
66         }
67     }
68 
69     if (!Publish(service_.get())) {
70         LOG_ERROR("Failed to publish El5FilekeyManagerService to SystemAbilityMgr");
71         return;
72     }
73 }
74 
OnStop()75 void El5FilekeyManagerServiceAbility::OnStop()
76 {
77     LOG_INFO("onStop called.");
78     service_ = nullptr;
79 }
80 }  // namespace AccessToken
81 }  // namespace Security
82 }  // namespace OHOS
83