1 /*
2 * Copyright (c) 2021 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 "bundle_info.h"
17
18 #include "string_ex.h"
19
20 #include "app_log_wrapper.h"
21 #include "json_serializer.h"
22 #include "parcel_macro.h"
23 #include "message_parcel.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27
ReadFromParcel(Parcel & parcel)28 bool BundleInfo::ReadFromParcel(Parcel &parcel)
29 {
30 MessageParcel *messageParcel = reinterpret_cast<MessageParcel *>(&parcel);
31 if (!messageParcel) {
32 APP_LOGE("Type conversion failed");
33 return false;
34 }
35 uint32_t length = messageParcel->ReadUint32();
36 if (length == 0) {
37 APP_LOGE("Invalid data length");
38 return false;
39 }
40 const char *data = reinterpret_cast<const char *>(messageParcel->ReadRawData(length));
41 if (!data) {
42 APP_LOGE("Fail to read raw data, length = %{public}d", length);
43 return false;
44 }
45 nlohmann::json jsonObject = nlohmann::json::parse(data, nullptr, false);
46 if (jsonObject.is_discarded()) {
47 APP_LOGE("failed to parse BundleInfo");
48 return false;
49 }
50 *this = jsonObject.get<BundleInfo>();
51 return true;
52 }
53
Marshalling(Parcel & parcel) const54 bool BundleInfo::Marshalling(Parcel &parcel) const
55 {
56 MessageParcel *messageParcel = reinterpret_cast<MessageParcel *>(&parcel);
57 if (!messageParcel) {
58 APP_LOGE("Type conversion failed");
59 return false;
60 }
61 nlohmann::json json = *this;
62 std::string str = json.dump();
63 if (!messageParcel->WriteUint32(str.size() + 1)) {
64 APP_LOGE("Failed to write data size");
65 return false;
66 }
67 if (!messageParcel->WriteRawData(str.c_str(), str.size() + 1)) {
68 APP_LOGE("Failed to write data");
69 return false;
70 }
71 return true;
72 }
73
Unmarshalling(Parcel & parcel)74 BundleInfo *BundleInfo::Unmarshalling(Parcel &parcel)
75 {
76 BundleInfo *info = new (std::nothrow) BundleInfo();
77 if (info && !info->ReadFromParcel(parcel)) {
78 APP_LOGW("read from parcel failed");
79 delete info;
80 info = nullptr;
81 }
82 return info;
83 }
84
to_json(nlohmann::json & jsonObject,const BundleInfo & bundleInfo)85 void to_json(nlohmann::json &jsonObject, const BundleInfo &bundleInfo)
86 {
87 jsonObject = nlohmann::json{
88 {"name", bundleInfo.name},
89 {"label", bundleInfo.label},
90 {"description", bundleInfo.description},
91 {"vendor", bundleInfo.vendor},
92 {"isKeepAlive", bundleInfo.isKeepAlive},
93 {"isNativeApp", bundleInfo.isNativeApp},
94 {"isDifferentName", bundleInfo.isDifferentName},
95 {"applicationInfo", bundleInfo.applicationInfo},
96 {"abilityInfos", bundleInfo.abilityInfos},
97 {"jointUserId", bundleInfo.jointUserId},
98 {"versionCode", bundleInfo.versionCode},
99 {"versionName", bundleInfo.versionName},
100 {"minSdkVersion", bundleInfo.minSdkVersion},
101 {"maxSdkVersion", bundleInfo.maxSdkVersion},
102 {"mainEntry", bundleInfo.mainEntry},
103 {"cpuAbi", bundleInfo.cpuAbi},
104 {"appId", bundleInfo.appId},
105 {"compatibleVersion", bundleInfo.compatibleVersion},
106 {"targetVersion", bundleInfo.targetVersion},
107 {"releaseType", bundleInfo.releaseType},
108 {"uid", bundleInfo.uid},
109 {"gid", bundleInfo.gid},
110 {"seInfo", bundleInfo.seInfo},
111 {"installTime", bundleInfo.installTime},
112 {"updateTime", bundleInfo.updateTime},
113 {"entryModuleName", bundleInfo.entryModuleName},
114 {"reqPermissions", bundleInfo.reqPermissions},
115 {"defPermissions", bundleInfo.defPermissions},
116 {"hapModuleNames", bundleInfo.hapModuleNames},
117 {"moduleNames", bundleInfo.moduleNames},
118 {"modulePublicDirs", bundleInfo.modulePublicDirs},
119 {"moduleDirs", bundleInfo.moduleDirs},
120 {"moduleResPaths", bundleInfo.moduleResPaths}
121 };
122 }
123
from_json(const nlohmann::json & jsonObject,BundleInfo & bundleInfo)124 void from_json(const nlohmann::json &jsonObject, BundleInfo &bundleInfo)
125 {
126 bundleInfo.name = jsonObject.at("name").get<std::string>();
127 bundleInfo.label = jsonObject.at("label").get<std::string>();
128 bundleInfo.description = jsonObject.at("description").get<std::string>();
129 bundleInfo.vendor = jsonObject.at("vendor").get<std::string>();
130 bundleInfo.isKeepAlive = jsonObject.at("isKeepAlive").get<bool>();
131 bundleInfo.isNativeApp = jsonObject.at("isNativeApp").get<bool>();
132 bundleInfo.isDifferentName = jsonObject.at("isDifferentName").get<bool>();
133 bundleInfo.applicationInfo = jsonObject.at("applicationInfo").get<ApplicationInfo>();
134 bundleInfo.abilityInfos = jsonObject.at("abilityInfos").get<std::vector<AbilityInfo>>();
135 bundleInfo.versionCode = jsonObject.at("versionCode").get<uint32_t>();
136 bundleInfo.versionName = jsonObject.at("versionName").get<std::string>();
137 bundleInfo.jointUserId = jsonObject.at("jointUserId").get<std::string>();
138 bundleInfo.minSdkVersion = jsonObject.at("minSdkVersion").get<int32_t>();
139 bundleInfo.maxSdkVersion = jsonObject.at("maxSdkVersion").get<int32_t>();
140 bundleInfo.mainEntry = jsonObject.at("mainEntry").get<std::string>();
141 bundleInfo.cpuAbi = jsonObject.at("cpuAbi").get<std::string>();
142 bundleInfo.appId = jsonObject.at("appId").get<std::string>();
143 bundleInfo.compatibleVersion = jsonObject.at("compatibleVersion").get<int>();
144 bundleInfo.targetVersion = jsonObject.at("targetVersion").get<int>();
145 bundleInfo.releaseType = jsonObject.at("releaseType").get<std::string>();
146 bundleInfo.uid = jsonObject.at("uid").get<int>();
147 bundleInfo.gid = jsonObject.at("gid").get<int>();
148 bundleInfo.seInfo = jsonObject.at("seInfo").get<std::string>();
149 bundleInfo.installTime = jsonObject.at("installTime").get<int64_t>();
150 bundleInfo.updateTime = jsonObject.at("updateTime").get<int64_t>();
151 bundleInfo.entryModuleName = jsonObject.at("entryModuleName").get<std::string>();
152 bundleInfo.reqPermissions = jsonObject.at("reqPermissions").get<std::vector<std::string>>();
153 bundleInfo.defPermissions = jsonObject.at("defPermissions").get<std::vector<std::string>>();
154 bundleInfo.hapModuleNames = jsonObject.at("hapModuleNames").get<std::vector<std::string>>();
155 bundleInfo.moduleNames = jsonObject.at("moduleNames").get<std::vector<std::string>>();
156 bundleInfo.modulePublicDirs = jsonObject.at("modulePublicDirs").get<std::vector<std::string>>();
157 bundleInfo.moduleDirs = jsonObject.at("moduleDirs").get<std::vector<std::string>>();
158 bundleInfo.moduleResPaths = jsonObject.at("moduleResPaths").get<std::vector<std::string>>();
159 }
160
161 } // namespace AppExecFwk
162 } // namespace OHOS