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