1 /*
2  * Copyright (c) 2022-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 "uri_permission_manager_service.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "if_system_ability_manager.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 
24 namespace OHOS {
25 namespace AAFwk {
26 const bool REGISTER_RESULT =
27     SystemAbility::MakeAndRegisterAbility(DelayedSingleton<UriPermissionManagerService>::GetInstance().get());
28 
UriPermissionManagerService()29 UriPermissionManagerService::UriPermissionManagerService() : SystemAbility(URI_PERMISSION_MGR_SERVICE_ID, true) {}
30 
~UriPermissionManagerService()31 UriPermissionManagerService::~UriPermissionManagerService()
32 {
33     if (impl_ != nullptr) {
34         impl_ = nullptr;
35     }
36 }
37 
OnStart()38 void UriPermissionManagerService::OnStart()
39 {
40     TAG_LOGI(AAFwkTag::URIPERMMGR, "UriPermissionManagerService start is triggered.");
41     if (!Init()) {
42         TAG_LOGE(AAFwkTag::URIPERMMGR, "init failed.");
43         return;
44     }
45 
46     if (!registerToService_) {
47         auto systemAabilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
48         if (!systemAabilityMgr || systemAabilityMgr->AddSystemAbility(URI_PERMISSION_MGR_SERVICE_ID, impl_) != 0) {
49             TAG_LOGE(AAFwkTag::URIPERMMGR, "fail to register to system ability manager");
50             return;
51         }
52         TAG_LOGI(AAFwkTag::URIPERMMGR, "register to system ability manager success");
53         registerToService_ = true;
54     }
55 }
56 
OnStop()57 void UriPermissionManagerService::OnStop()
58 {
59     TAG_LOGI(AAFwkTag::URIPERMMGR, "called");
60     SelfClean();
61 }
62 
IsServiceReady() const63 bool UriPermissionManagerService::IsServiceReady() const
64 {
65     return ready_;
66 }
67 
Init()68 bool UriPermissionManagerService::Init()
69 {
70     if (ready_) {
71         TAG_LOGW(AAFwkTag::URIPERMMGR, "init more than one time.");
72         return true;
73     }
74 
75     if (impl_ == nullptr) {
76         impl_ = new UriPermissionManagerStubImpl();
77     }
78     ready_ = true;
79     return true;
80 }
81 
SelfClean()82 void UriPermissionManagerService::SelfClean()
83 {
84     if (ready_) {
85         ready_ = false;
86         if (registerToService_) {
87             registerToService_ = false;
88         }
89     }
90 }
91 }  // namespace AAFwk
92 }  // namespace OHOS
93