• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "security_collector_manager_proxy.h"
17 
18 #include "security_collector_define.h"
19 #include "security_collector_log.h"
20 
21 namespace OHOS::Security::SecurityCollector {
SecurityCollectorManagerProxy(const sptr<IRemoteObject> & impl)22 SecurityCollectorManagerProxy::SecurityCollectorManagerProxy(const sptr<IRemoteObject> &impl)
23     : IRemoteProxy<ISecurityCollectorManager>(impl)
24 {
25 }
26 
Subscribe(const SecurityCollectorSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & callback)27 int32_t SecurityCollectorManagerProxy::Subscribe(const SecurityCollectorSubscribeInfo &subscribeInfo,
28     const sptr<IRemoteObject> &callback)
29 {
30     LOGI("enter SecurityCollectorManagerProxy Subscribe");
31     MessageParcel data;
32     MessageParcel reply;
33 
34     if (!data.WriteInterfaceToken(GetDescriptor())) {
35         LOGE("WriteInterfaceToken error");
36         return WRITE_ERR;
37     }
38 
39     if (!data.WriteParcelable(&subscribeInfo)) {
40         LOGE("failed to write parcelable for subscribeInfo");
41         return WRITE_ERR;
42     }
43 
44     data.WriteRemoteObject(callback);
45 
46     MessageOption option = { MessageOption::TF_SYNC };
47     sptr<IRemoteObject> remote = Remote();
48     if (remote == nullptr) {
49         LOGE("Remote error");
50         return NULL_OBJECT;
51     }
52     int ret = remote->SendRequest(CMD_COLLECTOR_SUBCRIBE, data, reply, option);
53     if (ret != ERR_NONE) {
54         LOGE("ret=%{public}d", ret);
55         return ret;
56     }
57     ret = reply.ReadInt32();
58     LOGD("reply=%{public}d", ret);
59     return ret;
60 }
61 
Unsubscribe(const sptr<IRemoteObject> & callback)62 int32_t SecurityCollectorManagerProxy::Unsubscribe(const sptr<IRemoteObject> &callback)
63 {
64     LOGI("enter SecurityCollectorManagerProxy Unsubscribe");
65     MessageParcel data;
66     MessageParcel reply;
67 
68     if (!data.WriteInterfaceToken(GetDescriptor())) {
69         LOGE("WriteInterfaceToken error");
70         return WRITE_ERR;
71     }
72 
73     data.WriteRemoteObject(callback);
74 
75     MessageOption option = { MessageOption::TF_SYNC };
76     sptr<IRemoteObject> remote = Remote();
77     if (remote == nullptr) {
78         LOGE("Remote error");
79         return NULL_OBJECT;
80     }
81     int ret = remote->SendRequest(CMD_COLLECTOR_UNSUBCRIBE, data, reply, option);
82     if (ret != ERR_NONE) {
83         LOGE("ret=%{public}d", ret);
84         return ret;
85     }
86     ret = reply.ReadInt32();
87     LOGD("reply=%{public}d", ret);
88     return ret;
89 }
90 
CollectorStart(const SecurityCollectorSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & callback)91 int32_t SecurityCollectorManagerProxy::CollectorStart(const SecurityCollectorSubscribeInfo &subscribeInfo,
92     const sptr<IRemoteObject> &callback)
93 {
94     MessageParcel data;
95     MessageParcel reply;
96     LOGI("enter SecurityCollectorManagerProxy CollectorStart");
97     if (!data.WriteInterfaceToken(GetDescriptor())) {
98         LOGE("WriteInterfaceToken error");
99         return WRITE_ERR;
100     }
101 
102     if (!data.WriteParcelable(&subscribeInfo)) {
103         LOGE("failed to write parcelable for subscribeInfo");
104         return WRITE_ERR;
105     }
106     data.WriteRemoteObject(callback);
107     MessageOption option = { MessageOption::TF_SYNC };
108     sptr<IRemoteObject> remote = Remote();
109     if (remote == nullptr) {
110         LOGE("Remote error");
111         return NULL_OBJECT;
112     }
113     int ret = remote->SendRequest(CMD_COLLECTOR_START, data, reply, option);
114     if (ret != ERR_NONE) {
115         LOGE("ret=%{public}d", ret);
116         return ret;
117     }
118     ret = reply.ReadInt32();
119     LOGD("reply=%{public}d", ret);
120     return ret;
121 }
122 
CollectorStop(const SecurityCollectorSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & callback)123 int32_t SecurityCollectorManagerProxy::CollectorStop(const SecurityCollectorSubscribeInfo &subscribeInfo,
124     const sptr<IRemoteObject> &callback)
125 {
126     MessageParcel data;
127     MessageParcel reply;
128     LOGI("enter SecurityCollectorManagerProxy CollectorStop");
129     if (!data.WriteInterfaceToken(GetDescriptor())) {
130         LOGE("WriteInterfaceToken error");
131         return WRITE_ERR;
132     }
133 
134     if (!data.WriteParcelable(&subscribeInfo)) {
135         LOGE("failed to write parcelable for subscribeInfo");
136         return WRITE_ERR;
137     }
138     data.WriteRemoteObject(callback);
139     MessageOption option = { MessageOption::TF_SYNC };
140     sptr<IRemoteObject> remote = Remote();
141     if (remote == nullptr) {
142         LOGE("Remote error");
143         return NULL_OBJECT;
144     }
145     int ret = remote->SendRequest(CMD_COLLECTOR_STOP, data, reply, option);
146     if (ret != ERR_NONE) {
147         LOGE("ret=%{public}d", ret);
148         return ret;
149     }
150     ret = reply.ReadInt32();
151     LOGD("reply=%{public}d", ret);
152     return ret;
153 }
154 
QuerySecurityEvent(const std::vector<SecurityEventRuler> rulers,std::vector<SecurityEvent> & events)155 int32_t SecurityCollectorManagerProxy::QuerySecurityEvent(const std::vector<SecurityEventRuler> rulers,
156     std::vector<SecurityEvent> &events)
157 {
158     LOGI("enter SecurityCollectorManagerProxy QuerySecurityEvent");
159     MessageParcel data;
160     MessageParcel reply;
161 
162     if (!data.WriteInterfaceToken(GetDescriptor())) {
163         LOGE("WriteInterfaceToken error");
164         return WRITE_ERR;
165     }
166 
167     if (!data.WriteUint32(rulers.size())) {
168         LOGE("failed to WriteInt32 for parcelable vector size");
169         return WRITE_ERR;
170     }
171 
172     for (const auto &ruler : rulers) {
173         if (!data.WriteParcelable(&ruler)) {
174             LOGE("failed to WriteParcelable for parcelable");
175             return WRITE_ERR;
176         }
177     }
178 
179     MessageOption option = { MessageOption::TF_SYNC };
180     sptr<IRemoteObject> remote = Remote();
181     if (remote == nullptr) {
182         LOGE("Remote error");
183         return NULL_OBJECT;
184     }
185     int ret = remote->SendRequest(CMD_SECURITY_EVENT_QUERY, data, reply, option);
186     if (ret != ERR_NONE) {
187         LOGE("ret=%{public}d", ret);
188         return ret;
189     }
190 
191     uint32_t size = 0;
192     if (!reply.ReadUint32(size)) {
193         LOGE("failed to get the event size");
194         return BAD_PARAM;
195     }
196 
197     if (size > MAX_QUERY_EVENT_SIZE) {
198         LOGE("the event size error");
199         return BAD_PARAM;
200     }
201     for (uint32_t index = 0; index < size; index++) {
202         std::shared_ptr<SecurityEvent> event(reply.ReadParcelable<SecurityEvent>());
203         if (event == nullptr) {
204             LOGE("failed read security event");
205             return BAD_PARAM;
206         }
207         events.emplace_back(*event);
208     }
209     return SUCCESS;
210 }
211 
AddFilter(const SecurityCollectorEventFilter & subscribeMute)212 int32_t SecurityCollectorManagerProxy::AddFilter(const SecurityCollectorEventFilter &subscribeMute)
213 {
214     LOGI("enter SecurityCollectorManagerProxy AddFilter");
215     MessageParcel data;
216     MessageParcel reply;
217 
218     if (!data.WriteInterfaceToken(GetDescriptor())) {
219         LOGE("WriteInterfaceToken error");
220         return WRITE_ERR;
221     }
222 
223     if (!data.WriteParcelable(&subscribeMute)) {
224         LOGE("failed to write parcelable for subscribeMute");
225         return WRITE_ERR;
226     }
227 
228     MessageOption option = { MessageOption::TF_SYNC };
229     sptr<IRemoteObject> remote = Remote();
230     if (remote == nullptr) {
231         LOGE("Remote error");
232         return NULL_OBJECT;
233     }
234     int ret = remote->SendRequest(CMD_SECURITY_EVENT_MUTE, data, reply, option);
235     if (ret != ERR_NONE) {
236         LOGE("ret=%{public}d", ret);
237         return ret;
238     }
239     ret = reply.ReadInt32();
240     LOGD("reply=%{public}d", ret);
241     return ret;
242 }
243 
RemoveFilter(const SecurityCollectorEventFilter & subscribeMute)244 int32_t SecurityCollectorManagerProxy::RemoveFilter(const SecurityCollectorEventFilter &subscribeMute)
245 {
246     LOGI("enter SecurityCollectorManagerProxy RemoveFilter");
247     MessageParcel data;
248     MessageParcel reply;
249 
250     if (!data.WriteInterfaceToken(GetDescriptor())) {
251         LOGE("WriteInterfaceToken error");
252         return WRITE_ERR;
253     }
254 
255     if (!data.WriteParcelable(&subscribeMute)) {
256         LOGE("failed to write parcelable for subscribeMute");
257         return WRITE_ERR;
258     }
259 
260     MessageOption option = { MessageOption::TF_SYNC };
261     sptr<IRemoteObject> remote = Remote();
262     if (remote == nullptr) {
263         LOGE("Remote error");
264         return NULL_OBJECT;
265     }
266     int ret = remote->SendRequest(CMD_SECURITY_EVENT_UNMUTE, data, reply, option);
267     if (ret != ERR_NONE) {
268         LOGE("ret=%{public}d", ret);
269         return ret;
270     }
271     ret = reply.ReadInt32();
272     LOGD("reply=%{public}d", ret);
273     return ret;
274 }
275 }