1 /*
2 * Copyright (c) 2021 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 "common_event_info.h"
17
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <cerrno>
21 #include <cstring>
22 #include "json_serializer.h"
23 #include "nlohmann/json.hpp"
24 #include "string_ex.h"
25 #include "parcel_macro.h"
26 #include "app_log_wrapper.h"
27 #include "bundle_constants.h"
28
29 namespace OHOS {
30 namespace AppExecFwk {
31 namespace {
32 const std::string JSON_KEY_NAME = "name";
33 const std::string JSON_KEY_UID = "uid";
34 const std::string JSON_KEY_PERMISSION = "permission";
35 const std::string JSON_KEY_DATA = "data";
36 const std::string JSON_KEY_TYPE = "type";
37 const std::string JSON_KEY_EVENTS = "events";
38 } // namespace
39
ReadFromParcel(Parcel & parcel)40 bool CommonEventInfo::ReadFromParcel(Parcel &parcel)
41 {
42 name = Str16ToStr8(parcel.ReadString16());
43 bundleName = Str16ToStr8(parcel.ReadString16());
44 uid = parcel.ReadInt32();
45 permission = Str16ToStr8(parcel.ReadString16());
46
47 int32_t typeSize;
48 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, typeSize);
49 CONTAINER_SECURITY_VERIFY(parcel, typeSize, &data);
50 for (int32_t i = 0; i < typeSize; i++) {
51 data.emplace_back(Str16ToStr8(parcel.ReadString16()));
52 }
53 int32_t dataSize;
54 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, dataSize);
55 CONTAINER_SECURITY_VERIFY(parcel, dataSize, &type);
56 for (int32_t i = 0; i < dataSize; i++) {
57 type.emplace_back(Str16ToStr8(parcel.ReadString16()));
58 }
59 int32_t eventsSize;
60 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, eventsSize);
61 CONTAINER_SECURITY_VERIFY(parcel, eventsSize, &events);
62 for (int32_t i = 0; i < eventsSize; i++) {
63 events.emplace_back(Str16ToStr8(parcel.ReadString16()));
64 }
65 return true;
66 }
67
Unmarshalling(Parcel & parcel)68 CommonEventInfo *CommonEventInfo::Unmarshalling(Parcel &parcel)
69 {
70 std::unique_ptr<CommonEventInfo> info = std::make_unique<CommonEventInfo>();
71 if (!info->ReadFromParcel(parcel)) {
72 APP_LOGW("read from parcel failed");
73 info = nullptr;
74 }
75 return info.release();
76 }
77
Marshalling(Parcel & parcel) const78 bool CommonEventInfo::Marshalling(Parcel &parcel) const
79 {
80 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(name));
81 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
82 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, uid);
83 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(permission));
84
85 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, data.size());
86 for (auto &dataSingle : data) {
87 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(dataSingle));
88 }
89 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, type.size());
90 for (auto &typeSingle : type) {
91 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(typeSingle));
92 }
93 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, events.size());
94 for (auto &event : events) {
95 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(event));
96 }
97 return true;
98 }
99
to_json(nlohmann::json & jsonObject,const CommonEventInfo & commonEvent)100 void to_json(nlohmann::json &jsonObject, const CommonEventInfo &commonEvent)
101 {
102 jsonObject = nlohmann::json {
103 {JSON_KEY_NAME, commonEvent.name},
104 {Constants::BUNDLE_NAME, commonEvent.bundleName},
105 {JSON_KEY_UID, commonEvent.uid},
106 {JSON_KEY_PERMISSION, commonEvent.permission},
107 {JSON_KEY_DATA, commonEvent.data},
108 {JSON_KEY_TYPE, commonEvent.type},
109 {JSON_KEY_EVENTS, commonEvent.events}
110 };
111 }
112
from_json(const nlohmann::json & jsonObject,CommonEventInfo & commonEvent)113 void from_json(const nlohmann::json &jsonObject, CommonEventInfo &commonEvent)
114 {
115 commonEvent.name = jsonObject.at(JSON_KEY_NAME).get<std::string>();
116 commonEvent.bundleName = jsonObject.at(Constants::BUNDLE_NAME).get<std::string>();
117 commonEvent.uid = jsonObject.at(JSON_KEY_UID).get<int>();
118 commonEvent.permission = jsonObject.at(JSON_KEY_PERMISSION).get<std::string>();
119 commonEvent.data = jsonObject.at(JSON_KEY_DATA).get<std::vector<std::string>>();
120 commonEvent.type = jsonObject.at(JSON_KEY_TYPE).get<std::vector<std::string>>();
121 commonEvent.events = jsonObject.at(JSON_KEY_EVENTS).get<std::vector<std::string>>();
122 }
123 } // namespace AppExecFwk
124 } // namespace OHOS
125