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 #include "json_util.h"
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 namespace {
33 const char* JSON_KEY_NAME = "name";
34 const char* JSON_KEY_UID = "uid";
35 const char* JSON_KEY_PERMISSION = "permission";
36 const char* JSON_KEY_DATA = "data";
37 const char* JSON_KEY_TYPE = "type";
38 const char* JSON_KEY_EVENTS = "events";
39 }  // namespace
40 
ReadFromParcel(Parcel & parcel)41 bool CommonEventInfo::ReadFromParcel(Parcel &parcel)
42 {
43     name = Str16ToStr8(parcel.ReadString16());
44     bundleName = Str16ToStr8(parcel.ReadString16());
45     uid = parcel.ReadInt32();
46     permission = Str16ToStr8(parcel.ReadString16());
47 
48     int32_t typeSize;
49     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, typeSize);
50     CONTAINER_SECURITY_VERIFY(parcel, typeSize, &data);
51     for (int32_t i = 0; i < typeSize; i++) {
52         data.emplace_back(Str16ToStr8(parcel.ReadString16()));
53     }
54     int32_t dataSize;
55     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, dataSize);
56     CONTAINER_SECURITY_VERIFY(parcel, dataSize, &type);
57     for (int32_t i = 0; i < dataSize; i++) {
58         type.emplace_back(Str16ToStr8(parcel.ReadString16()));
59     }
60     int32_t eventsSize;
61     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, eventsSize);
62     CONTAINER_SECURITY_VERIFY(parcel, eventsSize, &events);
63     for (int32_t i = 0; i < eventsSize; i++) {
64         events.emplace_back(Str16ToStr8(parcel.ReadString16()));
65     }
66     return true;
67 }
68 
Unmarshalling(Parcel & parcel)69 CommonEventInfo *CommonEventInfo::Unmarshalling(Parcel &parcel)
70 {
71     std::unique_ptr<CommonEventInfo> info = std::make_unique<CommonEventInfo>();
72     if (!info->ReadFromParcel(parcel)) {
73         APP_LOGW("read from parcel failed");
74         info = nullptr;
75     }
76     return info.release();
77 }
78 
Marshalling(Parcel & parcel) const79 bool CommonEventInfo::Marshalling(Parcel &parcel) const
80 {
81     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(name));
82     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
83     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, uid);
84     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(permission));
85 
86     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, data.size());
87     for (auto &dataSingle : data) {
88         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(dataSingle));
89     }
90     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, type.size());
91     for (auto &typeSingle : type) {
92         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(typeSingle));
93     }
94     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, events.size());
95     for (auto &event : events) {
96         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(event));
97     }
98     return true;
99 }
100 
to_json(nlohmann::json & jsonObject,const CommonEventInfo & commonEvent)101 void to_json(nlohmann::json &jsonObject, const CommonEventInfo &commonEvent)
102 {
103     jsonObject = nlohmann::json {
104         {JSON_KEY_NAME, commonEvent.name},
105         {Constants::BUNDLE_NAME, commonEvent.bundleName},
106         {JSON_KEY_UID, commonEvent.uid},
107         {JSON_KEY_PERMISSION, commonEvent.permission},
108         {JSON_KEY_DATA, commonEvent.data},
109         {JSON_KEY_TYPE, commonEvent.type},
110         {JSON_KEY_EVENTS, commonEvent.events}
111         };
112 }
113 
from_json(const nlohmann::json & jsonObject,CommonEventInfo & commonEvent)114 void from_json(const nlohmann::json &jsonObject, CommonEventInfo &commonEvent)
115 {
116     const auto &jsonObjectEnd = jsonObject.end();
117     int32_t parseResult = ERR_OK;
118     GetValueIfFindKey<std::string>(jsonObject,
119         jsonObjectEnd,
120         JSON_KEY_NAME,
121         commonEvent.name,
122         JsonType::STRING,
123         false,
124         parseResult,
125         ArrayType::NOT_ARRAY);
126     GetValueIfFindKey<std::string>(jsonObject,
127         jsonObjectEnd,
128         Constants::BUNDLE_NAME,
129         commonEvent.bundleName,
130         JsonType::STRING,
131         false,
132         parseResult,
133         ArrayType::NOT_ARRAY);
134     GetValueIfFindKey<int>(jsonObject,
135         jsonObjectEnd,
136         JSON_KEY_UID,
137         commonEvent.uid,
138         JsonType::NUMBER,
139         false,
140         parseResult,
141         ArrayType::NOT_ARRAY);
142     GetValueIfFindKey<std::string>(jsonObject,
143         jsonObjectEnd,
144         JSON_KEY_PERMISSION,
145         commonEvent.permission,
146         JsonType::STRING,
147         false,
148         parseResult,
149         ArrayType::NOT_ARRAY);
150     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
151         jsonObjectEnd,
152         JSON_KEY_DATA,
153         commonEvent.data,
154         JsonType::ARRAY,
155         false,
156         parseResult,
157         ArrayType::STRING);
158     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
159         jsonObjectEnd,
160         JSON_KEY_TYPE,
161         commonEvent.type,
162         JsonType::ARRAY,
163         false,
164         parseResult,
165         ArrayType::STRING);
166     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
167         jsonObjectEnd,
168         JSON_KEY_EVENTS,
169         commonEvent.events,
170         JsonType::ARRAY,
171         false,
172         parseResult,
173         ArrayType::STRING);
174     if (parseResult != ERR_OK) {
175         APP_LOGE("module commonEvent jsonObject error : %{public}d", parseResult);
176     }
177 }
178 }  // namespace AppExecFwk
179 }  // namespace OHOS
180