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 "hqf_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 HQF_INFO_HAP_SHA256 = "hapSha256";
28 const std::string HQF_INFO_HQF_FILE_PATH = "hqfFilePath";
29 const std::string HQF_INFO_TYPE = "type";
30 const std::string HQF_INFO_CPU_ABI = "cpuAbi";
31 const std::string HQF_INFO_NATIVE_LIBRARY_PATH = "nativeLibraryPath";
32 }
33
to_json(nlohmann::json & jsonObject,const HqfInfo & hqfInfo)34 void to_json(nlohmann::json &jsonObject, const HqfInfo &hqfInfo)
35 {
36 jsonObject = nlohmann::json {
37 {Constants::MODULE_NAME, hqfInfo.moduleName},
38 {HQF_INFO_HAP_SHA256, hqfInfo.hapSha256},
39 {HQF_INFO_HQF_FILE_PATH, hqfInfo.hqfFilePath},
40 {HQF_INFO_TYPE, hqfInfo.type},
41 {HQF_INFO_CPU_ABI, hqfInfo.cpuAbi},
42 {HQF_INFO_NATIVE_LIBRARY_PATH, hqfInfo.nativeLibraryPath}
43 };
44 }
45
from_json(const nlohmann::json & jsonObject,HqfInfo & hqfInfo)46 void from_json(const nlohmann::json &jsonObject, HqfInfo &hqfInfo)
47 {
48 const auto &jsonObjectEnd = jsonObject.end();
49 int32_t parseResult = ERR_OK;
50 GetValueIfFindKey<std::string>(jsonObject,
51 jsonObjectEnd,
52 Constants::MODULE_NAME,
53 hqfInfo.moduleName,
54 JsonType::STRING,
55 false,
56 parseResult,
57 ArrayType::NOT_ARRAY);
58 GetValueIfFindKey<std::string>(jsonObject,
59 jsonObjectEnd,
60 HQF_INFO_HAP_SHA256,
61 hqfInfo.hapSha256,
62 JsonType::STRING,
63 false,
64 parseResult,
65 ArrayType::NOT_ARRAY);
66 GetValueIfFindKey<std::string>(jsonObject,
67 jsonObjectEnd,
68 HQF_INFO_HQF_FILE_PATH,
69 hqfInfo.hqfFilePath,
70 JsonType::STRING,
71 false,
72 parseResult,
73 ArrayType::NOT_ARRAY);
74 GetValueIfFindKey<QuickFixType>(jsonObject,
75 jsonObjectEnd,
76 HQF_INFO_TYPE,
77 hqfInfo.type,
78 JsonType::NUMBER,
79 false,
80 parseResult,
81 ArrayType::NOT_ARRAY);
82 GetValueIfFindKey<std::string>(jsonObject,
83 jsonObjectEnd,
84 HQF_INFO_CPU_ABI,
85 hqfInfo.cpuAbi,
86 JsonType::STRING,
87 false,
88 parseResult,
89 ArrayType::NOT_ARRAY);
90 GetValueIfFindKey<std::string>(jsonObject,
91 jsonObjectEnd,
92 HQF_INFO_NATIVE_LIBRARY_PATH,
93 hqfInfo.nativeLibraryPath,
94 JsonType::STRING,
95 false,
96 parseResult,
97 ArrayType::NOT_ARRAY);
98 }
99
ReadFromParcel(Parcel & parcel)100 bool HqfInfo::ReadFromParcel(Parcel &parcel)
101 {
102 moduleName = Str16ToStr8(parcel.ReadString16());
103 hapSha256 = Str16ToStr8(parcel.ReadString16());
104 hqfFilePath = Str16ToStr8(parcel.ReadString16());
105 type = static_cast<QuickFixType>(parcel.ReadInt32());
106 cpuAbi = Str16ToStr8(parcel.ReadString16());
107 nativeLibraryPath = Str16ToStr8(parcel.ReadString16());
108 return true;
109 }
110
Marshalling(Parcel & parcel) const111 bool HqfInfo::Marshalling(Parcel &parcel) const
112 {
113 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
114 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hapSha256));
115 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hqfFilePath));
116 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(type));
117 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(cpuAbi));
118 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(nativeLibraryPath));
119 return true;
120 }
121
Unmarshalling(Parcel & parcel)122 HqfInfo *HqfInfo::Unmarshalling(Parcel &parcel)
123 {
124 HqfInfo *info = new (std::nothrow) HqfInfo();
125 if (info && !info->ReadFromParcel(parcel)) {
126 APP_LOGE("read from parcel failed");
127 delete info;
128 info = nullptr;
129 }
130 return info;
131 }
132 } // AppExecFwk
133 } // OHOS