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 for (int32_t i = 0; i < typeSize; i++) {
50 data.emplace_back(Str16ToStr8(parcel.ReadString16()));
51 }
52 int32_t dataSize;
53 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, dataSize);
54 for (int32_t i = 0; i < dataSize; i++) {
55 type.emplace_back(Str16ToStr8(parcel.ReadString16()));
56 }
57 int32_t eventsSize;
58 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, eventsSize);
59 for (int32_t i = 0; i < eventsSize; i++) {
60 events.emplace_back(Str16ToStr8(parcel.ReadString16()));
61 }
62 return true;
63 }
64
Unmarshalling(Parcel & parcel)65 CommonEventInfo *CommonEventInfo::Unmarshalling(Parcel &parcel)
66 {
67 std::unique_ptr<CommonEventInfo> info = std::make_unique<CommonEventInfo>();
68 if (!info->ReadFromParcel(parcel)) {
69 APP_LOGW("read from parcel failed");
70 info = nullptr;
71 }
72 return info.release();
73 }
74
Marshalling(Parcel & parcel) const75 bool CommonEventInfo::Marshalling(Parcel &parcel) const
76 {
77 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(name));
78 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
79 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, uid);
80 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(permission));
81
82 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, data.size());
83 for (auto &dataSingle : data) {
84 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(dataSingle));
85 }
86 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, type.size());
87 for (auto &typeSingle : type) {
88 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(typeSingle));
89 }
90 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, events.size());
91 for (auto &event : events) {
92 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(event));
93 }
94 return true;
95 }
96
to_json(nlohmann::json & jsonObject,const CommonEventInfo & commonEvent)97 void to_json(nlohmann::json &jsonObject, const CommonEventInfo &commonEvent)
98 {
99 jsonObject = nlohmann::json {
100 {JSON_KEY_NAME, commonEvent.name},
101 {Constants::BUNDLE_NAME, commonEvent.bundleName},
102 {JSON_KEY_UID, commonEvent.uid},
103 {JSON_KEY_PERMISSION, commonEvent.permission},
104 {JSON_KEY_DATA, commonEvent.data},
105 {JSON_KEY_TYPE, commonEvent.type},
106 {JSON_KEY_EVENTS, commonEvent.events}
107 };
108 }
109
from_json(const nlohmann::json & jsonObject,CommonEventInfo & commonEvent)110 void from_json(const nlohmann::json &jsonObject, CommonEventInfo &commonEvent)
111 {
112 commonEvent.name = jsonObject.at(JSON_KEY_NAME).get<std::string>();
113 commonEvent.bundleName = jsonObject.at(Constants::BUNDLE_NAME).get<std::string>();
114 commonEvent.uid = jsonObject.at(JSON_KEY_UID).get<int>();
115 commonEvent.permission = jsonObject.at(JSON_KEY_PERMISSION).get<std::string>();
116 commonEvent.data = jsonObject.at(JSON_KEY_DATA).get<std::vector<std::string>>();
117 commonEvent.type = jsonObject.at(JSON_KEY_TYPE).get<std::vector<std::string>>();
118 commonEvent.events = jsonObject.at(JSON_KEY_EVENTS).get<std::vector<std::string>>();
119 }
120 } // namespace AppExecFwk
121 } // namespace OHOS
122