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     GetValueIfFindKey<std::string>(jsonObject,
52         jsonObjectEnd,
53         Constants::MODULE_NAME,
54         hqfInfo.moduleName,
55         JsonType::STRING,
56         false,
57         parseResult,
58         ArrayType::NOT_ARRAY);
59     GetValueIfFindKey<std::string>(jsonObject,
60         jsonObjectEnd,
61         HQF_INFO_HAP_SHA256,
62         hqfInfo.hapSha256,
63         JsonType::STRING,
64         false,
65         parseResult,
66         ArrayType::NOT_ARRAY);
67     GetValueIfFindKey<std::string>(jsonObject,
68         jsonObjectEnd,
69         HQF_INFO_HQF_FILE_PATH,
70         hqfInfo.hqfFilePath,
71         JsonType::STRING,
72         false,
73         parseResult,
74         ArrayType::NOT_ARRAY);
75     GetValueIfFindKey<QuickFixType>(jsonObject,
76         jsonObjectEnd,
77         HQF_INFO_TYPE,
78         hqfInfo.type,
79         JsonType::NUMBER,
80         false,
81         parseResult,
82         ArrayType::NOT_ARRAY);
83     GetValueIfFindKey<std::string>(jsonObject,
84         jsonObjectEnd,
85         HQF_INFO_CPU_ABI,
86         hqfInfo.cpuAbi,
87         JsonType::STRING,
88         false,
89         parseResult,
90         ArrayType::NOT_ARRAY);
91     GetValueIfFindKey<std::string>(jsonObject,
92         jsonObjectEnd,
93         HQF_INFO_NATIVE_LIBRARY_PATH,
94         hqfInfo.nativeLibraryPath,
95         JsonType::STRING,
96         false,
97         parseResult,
98         ArrayType::NOT_ARRAY);
99     if (parseResult != ERR_OK) {
100         LOG_E(BMS_TAG_DEFAULT, "read module hqfInfo from jsonObject error, error code : %{public}d", parseResult);
101     }
102 }
103 
ReadFromParcel(Parcel & parcel)104 bool HqfInfo::ReadFromParcel(Parcel &parcel)
105 {
106     moduleName = Str16ToStr8(parcel.ReadString16());
107     hapSha256 = Str16ToStr8(parcel.ReadString16());
108     hqfFilePath = Str16ToStr8(parcel.ReadString16());
109     type = static_cast<QuickFixType>(parcel.ReadInt32());
110     cpuAbi = Str16ToStr8(parcel.ReadString16());
111     nativeLibraryPath = Str16ToStr8(parcel.ReadString16());
112     return true;
113 }
114 
Marshalling(Parcel & parcel) const115 bool HqfInfo::Marshalling(Parcel &parcel) const
116 {
117     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
118     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hapSha256));
119     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hqfFilePath));
120     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(type));
121     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(cpuAbi));
122     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(nativeLibraryPath));
123     return true;
124 }
125 
Unmarshalling(Parcel & parcel)126 HqfInfo *HqfInfo::Unmarshalling(Parcel &parcel)
127 {
128     HqfInfo *info = new (std::nothrow) HqfInfo();
129     if (info && !info->ReadFromParcel(parcel)) {
130         LOG_E(BMS_TAG_DEFAULT, "read from parcel failed");
131         delete info;
132         info = nullptr;
133     }
134     return info;
135 }
136 } // AppExecFwk
137 } // OHOS