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