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 
16 #include "security_event_query_callback_stub.h"
17 
18 #include <cstdint>
19 #include <string>
20 #include <vector>
21 
22 #include "security_guard_define.h"
23 #include "security_guard_log.h"
24 
25 namespace OHOS::Security::SecurityGuard {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)26 int32_t SecurityEventQueryCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
27     MessageParcel& reply, MessageOption& option)
28 {
29     if (SecurityEventQueryCallbackStub::GetDescriptor() != data.ReadInterfaceToken()) {
30         SGLOGE("Descriptor error");
31         return NO_PERMISSION;
32     }
33     SGLOGD("OnRemoteRequest, code=%{public}u", code);
34 
35     switch (code) {
36         case CMD_ON_QUERY: {
37             uint32_t size = 0;
38             if (!data.ReadUint32(size)) {
39                 SGLOGE("failed to get the event size");
40                 return BAD_PARAM;
41             }
42 
43             if (size > MAX_QUERY_EVENT_SIZE) {
44                 SGLOGE("the event size error");
45                 return BAD_PARAM;
46             }
47             std::vector<SecurityCollector::SecurityEvent> events;
48             for (uint32_t index = 0; index < size; index++) {
49                 std::shared_ptr<SecurityCollector::SecurityEvent> event(
50                     data.ReadParcelable<SecurityCollector::SecurityEvent>());
51                 if (event == nullptr) {
52                     SGLOGE("failed read security event");
53                     return BAD_PARAM;
54                 }
55                 events.emplace_back(*event);
56             }
57             OnQuery(events);
58             return SUCCESS;
59         }
60         case CMD_ON_COMPLETE: {
61             OnComplete();
62             return SUCCESS;
63         }
64         case CMD_ON_ERROR: {
65             std::string message = data.ReadString();
66             OnError(message);
67             return SUCCESS;
68         }
69         default:
70             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
71     }
72 }
73 } // namespace OHOS::Security::SecurityGuard
74