1 /*
2  * Copyright (c) 2023 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 "acquire_data_manager.h"
16 
17 #include "iservice_registry.h"
18 #include "data_collect_manager_proxy.h"
19 #include "acquire_data_manager_callback_service.h"
20 #include "security_guard_define.h"
21 #include "security_guard_log.h"
22 
23 namespace OHOS::Security::SecurityGuard {
OnRemoteDied(const wptr<IRemoteObject> & remote)24 void AcquireDataManager::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
25 {
26     if (remote == nullptr) {
27         SGLOGE("remote object is nullptr");
28         return;
29     }
30     sptr<IRemoteObject> object = remote.promote();
31     if (object == nullptr) {
32         SGLOGE("object is nullptr");
33         return;
34     }
35     object->RemoveDeathRecipient(this);
36     AcquireDataManager::GetInstance().HandleDecipient();
37 }
38 
HandleDecipient()39 void AcquireDataManager::HandleDecipient()
40 {
41     std::lock_guard<std::mutex> lock(mutex_);
42     callback_ = nullptr;
43 }
44 
Subscribe(const std::shared_ptr<SecurityCollector::ICollectorSubscriber> & subscriber)45 int32_t AcquireDataManager::Subscribe(const std::shared_ptr<SecurityCollector::ICollectorSubscriber> &subscriber)
46 {
47     std::lock_guard<std::mutex> lock(mutex_);
48     if (subscriber == nullptr) {
49         SGLOGE("subscriber is nullptr");
50         return NULL_OBJECT;
51     }
52     auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
53     if (registry == nullptr) {
54         SGLOGE("GetSystemAbilityManager error");
55         return NULL_OBJECT;
56     }
57 
58     auto object = registry->GetSystemAbility(DATA_COLLECT_MANAGER_SA_ID);
59     auto proxy = iface_cast<IDataCollectManager>(object);
60     if (proxy == nullptr) {
61         SGLOGE("proxy is null");
62         return NULL_OBJECT;
63     }
64 
65     if (deathRecipient_ == nullptr) {
66         deathRecipient_ = new (std::nothrow) DeathRecipient();
67         if (deathRecipient_ == nullptr) {
68             SGLOGE("deathRecipient_ is nullptr.");
69             return NULL_OBJECT;
70         }
71     }
72 
73     if (!object->AddDeathRecipient(deathRecipient_)) {
74         SGLOGE("Failed to add death recipient");
75     }
76 
77     sptr<AcquireDataManagerCallbackService> callback = new (std::nothrow) AcquireDataManagerCallbackService(subscriber);
78     if (callback == nullptr) {
79         SGLOGE("callback is null");
80         return NULL_OBJECT;
81     }
82 
83     int32_t ret = proxy->Subscribe(subscriber->GetSubscribeInfo(), callback);
84     SGLOGI("Subscribe result, ret=%{public}d", ret);
85     callback_ = callback;
86     return ret;
87 }
88 
Unsubscribe(const std::shared_ptr<SecurityCollector::ICollectorSubscriber> & subscriber)89 int32_t AcquireDataManager::Unsubscribe(const std::shared_ptr<SecurityCollector::ICollectorSubscriber> &subscriber)
90 {
91     std::lock_guard<std::mutex> lock(mutex_);
92     auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
93     if (registry == nullptr) {
94         SGLOGE("GetSystemAbilityManager error");
95         return NULL_OBJECT;
96     }
97 
98     auto object = registry->GetSystemAbility(DATA_COLLECT_MANAGER_SA_ID);
99     auto proxy = iface_cast<IDataCollectManager>(object);
100     if (proxy == nullptr) {
101         SGLOGE("proxy is null");
102         return NULL_OBJECT;
103     }
104 
105     int32_t ret = proxy->Unsubscribe(callback_);
106     SGLOGI("Unsubscribe result, ret=%{public}d", ret);
107     callback_ = nullptr;
108     return ret;
109 }
110 }