1 /*
2 * Copyright (c) 2022 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 "appqf_info.h"
17
18 #include "app_log_wrapper.h"
19 #include "json_util.h"
20 #include "nlohmann/json.hpp"
21 #include "parcel_macro.h"
22 #include "string_ex.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const std::string APP_QF_INFO_VERSION_CODE = "versionCode";
28 const std::string APP_QF_INFO_VERSION_NAME = "versionName";
29 const std::string APP_QF_INFO_CPU_ABI = "cpuAbi";
30 const std::string APP_QF_INFO_NATIVE_LIBRARY_PATH = "nativeLibraryPath";
31 const std::string APP_QF_INFO_HQF_INFOS = "hqfInfos";
32 const std::string APP_QF_INFO_TYPE = "type";
33 }
34
to_json(nlohmann::json & jsonObject,const AppqfInfo & appqfInfo)35 void to_json(nlohmann::json &jsonObject, const AppqfInfo &appqfInfo)
36 {
37 jsonObject = nlohmann::json {
38 {APP_QF_INFO_VERSION_CODE, appqfInfo.versionCode},
39 {APP_QF_INFO_VERSION_NAME, appqfInfo.versionName},
40 {APP_QF_INFO_CPU_ABI, appqfInfo.cpuAbi},
41 {APP_QF_INFO_NATIVE_LIBRARY_PATH, appqfInfo.nativeLibraryPath},
42 {APP_QF_INFO_TYPE, appqfInfo.type},
43 {APP_QF_INFO_HQF_INFOS, appqfInfo.hqfInfos}
44 };
45 }
46
from_json(const nlohmann::json & jsonObject,AppqfInfo & appqfInfo)47 void from_json(const nlohmann::json &jsonObject, AppqfInfo &appqfInfo)
48 {
49 const auto &jsonObjectEnd = jsonObject.end();
50 int32_t parseResult = ERR_OK;
51 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd,
52 APP_QF_INFO_VERSION_CODE, appqfInfo.versionCode,
53 JsonType::NUMBER, false, parseResult,
54 ArrayType::NOT_ARRAY);
55
56 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd,
57 APP_QF_INFO_VERSION_NAME, appqfInfo.versionName,
58 JsonType::STRING, false, parseResult,
59 ArrayType::NOT_ARRAY);
60
61 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd,
62 APP_QF_INFO_CPU_ABI, appqfInfo.cpuAbi,
63 JsonType::STRING, false, parseResult,
64 ArrayType::NOT_ARRAY);
65
66 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd,
67 APP_QF_INFO_NATIVE_LIBRARY_PATH, appqfInfo.nativeLibraryPath,
68 JsonType::STRING, false, parseResult,
69 ArrayType::NOT_ARRAY);
70
71 GetValueIfFindKey<QuickFixType>(jsonObject, jsonObjectEnd,
72 APP_QF_INFO_TYPE, appqfInfo.type,
73 JsonType::NUMBER, false, parseResult,
74 ArrayType::NOT_ARRAY);
75
76 GetValueIfFindKey<std::vector<HqfInfo>>(jsonObject, jsonObjectEnd,
77 APP_QF_INFO_HQF_INFOS, appqfInfo.hqfInfos,
78 JsonType::ARRAY, false, parseResult,
79 ArrayType::OBJECT);
80 }
81
ReadFromParcel(Parcel & parcel)82 bool AppqfInfo::ReadFromParcel(Parcel &parcel)
83 {
84 versionCode = parcel.ReadUint32();
85 versionName = Str16ToStr8(parcel.ReadString16());
86 cpuAbi = Str16ToStr8(parcel.ReadString16());
87 nativeLibraryPath = Str16ToStr8(parcel.ReadString16());
88 type = static_cast<QuickFixType>(parcel.ReadInt32());
89 int32_t hqfSize;
90 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, hqfSize);
91 for (auto i = 0; i < hqfSize; i++) {
92 std::unique_ptr<HqfInfo> hqfInfoPtr(parcel.ReadParcelable<HqfInfo>());
93 if (!hqfInfoPtr) {
94 APP_LOGE("ReadParcelable<HqfInfo> failed");
95 return false;
96 }
97 hqfInfos.emplace_back(*hqfInfoPtr);
98 }
99 return true;
100 }
101
Marshalling(Parcel & parcel) const102 bool AppqfInfo::Marshalling(Parcel &parcel) const
103 {
104 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, versionCode);
105 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(versionName));
106 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(cpuAbi));
107 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(nativeLibraryPath));
108 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(type));
109 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, hqfInfos.size());
110 for (auto &hqfInfo : hqfInfos) {
111 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &hqfInfo);
112 }
113 return true;
114 }
115
Unmarshalling(Parcel & parcel)116 AppqfInfo *AppqfInfo::Unmarshalling(Parcel &parcel)
117 {
118 AppqfInfo *info = new (std::nothrow) AppqfInfo();
119 if (info && !info->ReadFromParcel(parcel)) {
120 APP_LOGE("read from parcel failed");
121 delete info;
122 info = nullptr;
123 }
124 return info;
125 }
126 } // AppExecFwk
127 } // OHOS