• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
119         jsonObjectEnd,
120         JSON_KEY_NAME,
121         commonEvent.name,
122         false,
123         parseResult);
124     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
125         jsonObjectEnd,
126         Constants::BUNDLE_NAME,
127         commonEvent.bundleName,
128         false,
129         parseResult);
130     GetValueIfFindKey<int>(jsonObject,
131         jsonObjectEnd,
132         JSON_KEY_UID,
133         commonEvent.uid,
134         JsonType::NUMBER,
135         false,
136         parseResult,
137         ArrayType::NOT_ARRAY);
138     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
139         jsonObjectEnd,
140         JSON_KEY_PERMISSION,
141         commonEvent.permission,
142         false,
143         parseResult);
144     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
145         jsonObjectEnd,
146         JSON_KEY_DATA,
147         commonEvent.data,
148         JsonType::ARRAY,
149         false,
150         parseResult,
151         ArrayType::STRING);
152     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
153         jsonObjectEnd,
154         JSON_KEY_TYPE,
155         commonEvent.type,
156         JsonType::ARRAY,
157         false,
158         parseResult,
159         ArrayType::STRING);
160     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
161         jsonObjectEnd,
162         JSON_KEY_EVENTS,
163         commonEvent.events,
164         JsonType::ARRAY,
165         false,
166         parseResult,
167         ArrayType::STRING);
168     if (parseResult != ERR_OK) {
169         APP_LOGE("module commonEvent jsonObject error : %{public}d", parseResult);
170     }
171 }
172 }  // namespace AppExecFwk
173 }  // namespace OHOS
174