• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "event_subscribe_client.h"
16 #include "iservice_registry.h"
17 #include "security_guard_log.h"
18 #include "data_collect_manager_idl_proxy.h"
19 #include "data_collect_manager_idl.h"
20 #include "security_event_filter.h"
21 #include "security_guard_define.h"
22 #include "acquire_data_manager_callback_service.h"
23 namespace OHOS::Security::SecurityGuard {
24 namespace {
25     std::set<std::shared_ptr<EventSubscribeClient>> g_clients{};
26     std::mutex g_clientMutex{};
27 
28 }
29 
Deleter(EventSubscribeClient * client)30 void EventSubscribeClient::Deleter(EventSubscribeClient *client)
31 {
32     SGLOGI("enter EventSubscribeClient Deleter");
33     auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
34     if (registry == nullptr) {
35         SGLOGE("GetSystemAbilityManager error");
36         return;
37     }
38     auto object = registry->GetSystemAbility(DATA_COLLECT_MANAGER_SA_ID);
39     auto proxy = iface_cast<DataCollectManagerIdl>(object);
40     if (proxy == nullptr) {
41         SGLOGE("proxy is null");
42         return;
43     }
44     int32_t ret = proxy->DestoryClient(client->eventGroup_, client->clientId_);
45     if (ret != SUCCESS) {
46         SGLOGI("DeleteClient result, ret=%{public}d", ret);
47         return;
48     }
49     if (client->deathRecipient_ != nullptr) {
50         object->RemoveDeathRecipient(client->deathRecipient_);
51     }
52     delete client;
53 }
54 
CreatClient(const std::string & eventGroup,EventCallback callback,std::shared_ptr<EventSubscribeClient> & client)55 int32_t EventSubscribeClient::CreatClient(const std::string &eventGroup, EventCallback callback,
56     std::shared_ptr<EventSubscribeClient> &client)
57 {
58     SGLOGI("enter EventSubscribeClient CreatClient");
59     std::lock_guard<std::mutex> lock(g_clientMutex);
60     if (callback == nullptr) {
61         SGLOGE("callback is nullptr");
62         return NULL_OBJECT;
63     }
64     auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
65     if (registry == nullptr) {
66         SGLOGE("GetSystemAbilityManager error");
67         return NULL_OBJECT;
68     }
69     auto object = registry->GetSystemAbility(DATA_COLLECT_MANAGER_SA_ID);
70     auto proxy = iface_cast<DataCollectManagerIdl>(object);
71     if (proxy == nullptr) {
72         SGLOGE("proxy is null");
73         return NULL_OBJECT;
74     }
75     auto serviceCallback = new (std::nothrow) AcquireDataManagerCallbackService();
76     if (serviceCallback == nullptr) {
77         SGLOGE("serviceCallback is null");
78         return NULL_OBJECT;
79     }
80     serviceCallback->RegistCallBack(callback);
81     std::string clientId = ConstructClientId(serviceCallback);
82     int32_t ret = proxy->CreatClient(eventGroup, clientId, serviceCallback);
83     if (ret != SUCCESS) {
84         SGLOGI("NewClient result, ret=%{public}d", ret);
85         return ret;
86     }
87     client = std::shared_ptr<EventSubscribeClient>(new EventSubscribeClient(), Deleter);
88     client->callback_ = serviceCallback;
89     client->eventGroup_ = eventGroup;
90     client->clientId_ = clientId;
91     ret = SetDeathRecipient(client, object);
92     if (ret != SUCCESS) {
93         SGLOGE("SetDeathRecipient fail ret=%{public}d", ret);
94         return ret;
95     }
96     return SUCCESS;
97 }
98 
ConstructClientId(const AcquireDataManagerCallbackService * serviceCallback)99 std::string EventSubscribeClient::ConstructClientId(const AcquireDataManagerCallbackService *serviceCallback)
100 {
101     std::string timeStr = std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
102     std::string ptrStr = std::to_string(reinterpret_cast<int64_t>(serviceCallback));
103     std::size_t hash = std::hash<std::string>{}(timeStr + ptrStr);
104     return std::to_string(hash);
105 }
106 
SetDeathRecipient(std::shared_ptr<EventSubscribeClient> client,const sptr<IRemoteObject> & remote)107 int32_t EventSubscribeClient::SetDeathRecipient(std::shared_ptr<EventSubscribeClient> client,
108     const sptr<IRemoteObject> &remote)
109 {
110     if (client->deathRecipient_ == nullptr) {
111         client->deathRecipient_ = new (std::nothrow) DeathRecipient();
112         if (client->deathRecipient_ == nullptr) {
113             SGLOGE("deathRecipient_ is nullptr.");
114             return NULL_OBJECT;
115         }
116         if (!remote->AddDeathRecipient(client->deathRecipient_)) {
117             SGLOGE("Failed to add death recipient");
118         }
119     }
120     return SUCCESS;
121 }
122 
Subscribe(int64_t eventId)123 int32_t EventSubscribeClient::Subscribe(int64_t eventId)
124 {
125     SGLOGI("enter EventSubscribeClient Subscribe");
126     auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
127     if (registry == nullptr) {
128         SGLOGE("GetSystemAbilityManager error");
129         return NULL_OBJECT;
130     }
131     auto object = registry->GetSystemAbility(DATA_COLLECT_MANAGER_SA_ID);
132     auto proxy = iface_cast<DataCollectManagerIdl>(object);
133     if (proxy == nullptr) {
134         SGLOGE("proxy is null");
135         return NULL_OBJECT;
136     }
137     int32_t ret = proxy->Subscribe(eventId, clientId_);
138     if (ret != SUCCESS) {
139         SGLOGI("Subscribe result, ret=%{public}d", ret);
140         return ret;
141     }
142     return SUCCESS;
143 }
144 
Unsubscribe(int64_t eventId)145 int32_t EventSubscribeClient::Unsubscribe(int64_t eventId)
146 {
147     SGLOGI("enter EventSubscribeClient UnSubscribe");
148     auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
149     if (registry == nullptr) {
150         SGLOGE("GetSystemAbilityManager error");
151         return NULL_OBJECT;
152     }
153     auto object = registry->GetSystemAbility(DATA_COLLECT_MANAGER_SA_ID);
154     auto proxy = iface_cast<DataCollectManagerIdl>(object);
155     if (proxy == nullptr) {
156         SGLOGE("proxy is null");
157         return NULL_OBJECT;
158     }
159     int32_t ret = proxy->Unsubscribe(eventId, clientId_);
160     if (ret != SUCCESS) {
161         SGLOGI("UnSubscribe result, ret=%{public}d", ret);
162         return ret;
163     }
164     return SUCCESS;
165 }
166 
AddFilter(const std::shared_ptr<EventMuteFilter> & filter)167 int32_t EventSubscribeClient::AddFilter(const std::shared_ptr<EventMuteFilter> &filter)
168 {
169     SGLOGI("enter EventSubscribeClient AddFilter");
170     auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
171     if (registry == nullptr) {
172         SGLOGE("GetSystemAbilityManager error");
173         return NULL_OBJECT;
174     }
175     auto object = registry->GetSystemAbility(DATA_COLLECT_MANAGER_SA_ID);
176     auto proxy = iface_cast<DataCollectManagerIdl>(object);
177     if (proxy == nullptr) {
178         SGLOGE("proxy is null");
179         return NULL_OBJECT;
180     }
181     if (filter == nullptr) {
182         SGLOGE("subscribeMute is null");
183         return NULL_OBJECT;
184     }
185     SecurityEventFilter innerFilter(*filter);
186     int32_t ret = proxy->AddFilter(innerFilter, clientId_);
187     if (ret != SUCCESS) {
188         SGLOGI("UnSubscribe result, ret=%{public}d", ret);
189         return ret;
190     }
191     return SUCCESS;
192 }
RemoveFilter(const std::shared_ptr<EventMuteFilter> & filter)193 int32_t EventSubscribeClient::RemoveFilter(const std::shared_ptr<EventMuteFilter> &filter)
194 {
195     SGLOGI("enter EventSubscribeClient RemoveFilter");
196     auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
197     if (registry == nullptr) {
198         SGLOGE("GetSystemAbilityManager error");
199         return NULL_OBJECT;
200     }
201     auto object = registry->GetSystemAbility(DATA_COLLECT_MANAGER_SA_ID);
202     auto proxy = iface_cast<DataCollectManagerIdl>(object);
203     if (proxy == nullptr) {
204         SGLOGE("proxy is null");
205         return NULL_OBJECT;
206     }
207     if (filter == nullptr) {
208         SGLOGE("subscribeMute is null");
209         return NULL_OBJECT;
210     }
211     SecurityEventFilter innerFilter(*filter);
212     int32_t ret = proxy->RemoveFilter(innerFilter, clientId_);
213     if (ret != SUCCESS) {
214         SGLOGI("RemoveFilter result, ret=%{public}d", ret);
215         return ret;
216     }
217     return SUCCESS;
218 }
219 }