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 #include "background_task_manager_access_client.h"
16
17 #include "accesstoken_log.h"
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 namespace {
25 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
26 LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "BackgourndTaskManagerAccessClient"
27 };
28 static constexpr int32_t ERROR = -1;
29 std::recursive_mutex g_instanceMutex;
30 } // namespace
31
GetInstance()32 BackgourndTaskManagerAccessClient& BackgourndTaskManagerAccessClient::GetInstance()
33 {
34 static BackgourndTaskManagerAccessClient* instance = nullptr;
35 if (instance == nullptr) {
36 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
37 if (instance == nullptr) {
38 instance = new BackgourndTaskManagerAccessClient();
39 }
40 }
41 return *instance;
42 }
43
BackgourndTaskManagerAccessClient()44 BackgourndTaskManagerAccessClient::BackgourndTaskManagerAccessClient()
45 {}
46
~BackgourndTaskManagerAccessClient()47 BackgourndTaskManagerAccessClient::~BackgourndTaskManagerAccessClient()
48 {
49 std::lock_guard<std::mutex> lock(proxyMutex_);
50 ReleaseProxy();
51 }
52
SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber> & subscriber)53 int32_t BackgourndTaskManagerAccessClient::SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber)
54 {
55 if (subscriber == nullptr) {
56 ACCESSTOKEN_LOG_ERROR(LABEL, "Callback is nullptr.");
57 return ERROR;
58 }
59 auto proxy = GetProxy();
60 if (proxy == nullptr) {
61 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
62 return ERROR;
63 }
64 return proxy->SubscribeBackgroundTask(subscriber);
65 }
66
UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber> & subscriber)67 int32_t BackgourndTaskManagerAccessClient::UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber)
68 {
69 if (subscriber == nullptr) {
70 ACCESSTOKEN_LOG_ERROR(LABEL, "Callback is nullptr.");
71 return ERROR;
72 }
73 auto proxy = GetProxy();
74 if (proxy == nullptr) {
75 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
76 return ERROR;
77 }
78 return proxy->UnsubscribeBackgroundTask(subscriber);
79 }
80
GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> & list)81 int32_t BackgourndTaskManagerAccessClient::GetContinuousTaskApps(
82 std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list)
83 {
84 auto proxy = GetProxy();
85 if (proxy == nullptr) {
86 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
87 return ERROR;
88 }
89 return proxy->GetContinuousTaskApps(list);
90 }
91
InitProxy()92 void BackgourndTaskManagerAccessClient::InitProxy()
93 {
94 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
95 if (sam == nullptr) {
96 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null");
97 return;
98 }
99 auto backgroundTaskManagerSa = sam->GetSystemAbility(BACKGROUND_TASK_MANAGER_SERVICE_ID);
100 if (backgroundTaskManagerSa == nullptr) {
101 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null",
102 BACKGROUND_TASK_MANAGER_SERVICE_ID);
103 return;
104 }
105
106 serviceDeathObserver_ = new (std::nothrow) BackgroundTaskMgrDeathRecipient();
107 if (serviceDeathObserver_ != nullptr) {
108 backgroundTaskManagerSa->AddDeathRecipient(serviceDeathObserver_);
109 }
110
111 proxy_ = new BackgroundTaskManagerAccessProxy(backgroundTaskManagerSa);
112 if (proxy_ == nullptr) {
113 ACCESSTOKEN_LOG_ERROR(LABEL, "Iface_cast get null");
114 }
115 }
116
OnRemoteDiedHandle()117 void BackgourndTaskManagerAccessClient::OnRemoteDiedHandle()
118 {
119 std::lock_guard<std::mutex> lock(proxyMutex_);
120 ReleaseProxy();
121 }
122
GetProxy()123 sptr<IBackgroundTaskMgr> BackgourndTaskManagerAccessClient::GetProxy()
124 {
125 std::lock_guard<std::mutex> lock(proxyMutex_);
126 if (proxy_ == nullptr) {
127 InitProxy();
128 }
129 return proxy_;
130 }
131
ReleaseProxy()132 void BackgourndTaskManagerAccessClient::ReleaseProxy()
133 {
134 if (proxy_ != nullptr && serviceDeathObserver_ != nullptr) {
135 proxy_->AsObject()->RemoveDeathRecipient(serviceDeathObserver_);
136 }
137 proxy_ = nullptr;
138 serviceDeathObserver_ = nullptr;
139 }
140 } // namespace AccessToken
141 } // namespace Security
142 } // namespace OHOS
143