• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "shared_module_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 char* SHARED_MODULE_INFO_NAME = "name";
28 const char* SHARED_MODULE_INFO_VERSION_CODE = "versionCode";
29 const char* SHARED_MODULE_INFO_VERSION_NAME = "versionName";
30 const char* SHARED_MODULE_INFO_DESCRIPTION = "description";
31 const char* SHARED_MODULE_INFO_DESCRIPTION_ID = "descriptionId";
32 const char* SHARED_MODULE_INFO_COMPRESS_NATIVE_LIBS = "compressNativeLibs";
33 const char* SHARED_MODULE_INFO_HAP_PATH = "hapPath";
34 const char* SHARED_MODULE_INFO_CPU_ABI = "cpuAbi";
35 const char* SHARED_MODULE_INFO_NATIVE_LIBRARY_PATH = "nativeLibraryPath";
36 const char* SHARED_MODULE_INFO_NATIVE_LIBRARY_FILE_NAMES = "nativeLibraryFileNames";
37 }
38 
ReadFromParcel(Parcel & parcel)39 bool SharedModuleInfo::ReadFromParcel(Parcel &parcel)
40 {
41     name = Str16ToStr8(parcel.ReadString16());
42     versionCode = parcel.ReadUint32();
43     versionName = Str16ToStr8(parcel.ReadString16());
44     description = Str16ToStr8(parcel.ReadString16());
45     descriptionId = parcel.ReadUint32();
46     compressNativeLibs = parcel.ReadBool();
47     hapPath = Str16ToStr8(parcel.ReadString16());
48     cpuAbi = Str16ToStr8(parcel.ReadString16());
49     nativeLibraryPath = Str16ToStr8(parcel.ReadString16());
50     int32_t nativeLibraryFileNamesSize;
51     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, nativeLibraryFileNamesSize);
52     CONTAINER_SECURITY_VERIFY(parcel, nativeLibraryFileNamesSize, &nativeLibraryFileNames);
53     for (int32_t i = 0; i < nativeLibraryFileNamesSize; ++i) {
54         nativeLibraryFileNames.emplace_back(Str16ToStr8(parcel.ReadString16()));
55     }
56     return true;
57 }
58 
Marshalling(Parcel & parcel) const59 bool SharedModuleInfo::Marshalling(Parcel &parcel) const
60 {
61     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(name));
62     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, versionCode);
63     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(versionName));
64     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(description));
65     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, descriptionId);
66     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, compressNativeLibs);
67     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hapPath));
68     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(cpuAbi));
69     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(nativeLibraryPath));
70     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, nativeLibraryFileNames.size());
71     for (auto &fileName : nativeLibraryFileNames) {
72         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(fileName));
73     }
74     return true;
75 }
76 
Unmarshalling(Parcel & parcel)77 SharedModuleInfo *SharedModuleInfo::Unmarshalling(Parcel &parcel)
78 {
79     SharedModuleInfo *info = new (std::nothrow) SharedModuleInfo();
80     if (info && !info->ReadFromParcel(parcel)) {
81         APP_LOGW("read from parcel failed");
82         delete info;
83         info = nullptr;
84     }
85     return info;
86 }
87 
to_json(nlohmann::json & jsonObject,const SharedModuleInfo & sharedModuleInfo)88 void to_json(nlohmann::json &jsonObject, const SharedModuleInfo &sharedModuleInfo)
89 {
90     jsonObject = nlohmann::json {
91         {SHARED_MODULE_INFO_NAME, sharedModuleInfo.name},
92         {SHARED_MODULE_INFO_VERSION_CODE, sharedModuleInfo.versionCode},
93         {SHARED_MODULE_INFO_VERSION_NAME, sharedModuleInfo.versionName},
94         {SHARED_MODULE_INFO_DESCRIPTION, sharedModuleInfo.description},
95         {SHARED_MODULE_INFO_DESCRIPTION_ID, sharedModuleInfo.descriptionId},
96         {SHARED_MODULE_INFO_COMPRESS_NATIVE_LIBS, sharedModuleInfo.compressNativeLibs},
97         {SHARED_MODULE_INFO_HAP_PATH, sharedModuleInfo.hapPath},
98         {SHARED_MODULE_INFO_CPU_ABI, sharedModuleInfo.cpuAbi},
99         {SHARED_MODULE_INFO_NATIVE_LIBRARY_PATH, sharedModuleInfo.nativeLibraryPath},
100         {SHARED_MODULE_INFO_NATIVE_LIBRARY_FILE_NAMES, sharedModuleInfo.nativeLibraryFileNames}
101     };
102 }
103 
from_json(const nlohmann::json & jsonObject,SharedModuleInfo & sharedModuleInfo)104 void from_json(const nlohmann::json &jsonObject, SharedModuleInfo &sharedModuleInfo)
105 {
106     const auto &jsonObjectEnd = jsonObject.end();
107     int32_t parseResult = ERR_OK;
108     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd,
109         SHARED_MODULE_INFO_NAME,
110         sharedModuleInfo.name, false, parseResult);
111     GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd,
112         SHARED_MODULE_INFO_VERSION_CODE,
113         sharedModuleInfo.versionCode, JsonType::NUMBER, false, parseResult,
114         ArrayType::NOT_ARRAY);
115     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd,
116         SHARED_MODULE_INFO_VERSION_NAME,
117         sharedModuleInfo.versionName, false, parseResult);
118     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd,
119         SHARED_MODULE_INFO_DESCRIPTION,
120         sharedModuleInfo.description, false, parseResult);
121     GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd,
122         SHARED_MODULE_INFO_DESCRIPTION_ID,
123         sharedModuleInfo.descriptionId, JsonType::NUMBER, false, parseResult,
124         ArrayType::NOT_ARRAY);
125     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, jsonObjectEnd,
126         SHARED_MODULE_INFO_COMPRESS_NATIVE_LIBS,
127         sharedModuleInfo.compressNativeLibs, false, parseResult);
128     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd,
129         SHARED_MODULE_INFO_HAP_PATH,
130         sharedModuleInfo.hapPath, false, parseResult);
131     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd,
132         SHARED_MODULE_INFO_CPU_ABI,
133         sharedModuleInfo.cpuAbi, false, parseResult);
134     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd,
135         SHARED_MODULE_INFO_NATIVE_LIBRARY_PATH,
136         sharedModuleInfo.nativeLibraryPath, false, parseResult);
137     GetValueIfFindKey<std::vector<std::string>>(jsonObject, jsonObjectEnd,
138         SHARED_MODULE_INFO_NATIVE_LIBRARY_FILE_NAMES,
139         sharedModuleInfo.nativeLibraryFileNames, JsonType::ARRAY, false, parseResult,
140         ArrayType::STRING);
141     if (parseResult != ERR_OK) {
142         APP_LOGE("read SharedModuleInfo error : %{public}d", parseResult);
143     }
144 }
145 } // AppExecFwk
146 } // OHOS