1 /*
2 * Copyright (c) 2025 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 "plugin_bundle_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* PLUGIN_BUNDLE_INFO_PLUGIN_BUNDLE_NAME = "pluginBundleName";
28 const char* PLUGIN_BUNDLE_INFO_LABEL = "label";
29 const char* PLUGIN_BUNDLE_INFO_ICON = "icon";
30 const char* PLUGIN_BUNDLE_INFO_VERSION_NAME = "versionName";
31 const char* PLUGIN_BUNDLE_INFO_ICON_ID = "iconId";
32 const char* PLUGIN_BUNDLE_INFO_LABEL_ID = "labelId";
33 const char* PLUGIN_BUNDLE_INFO_VERSION_CODE = "versionCode";
34 const char* PLUGIN_BUNDLE_INFO_APP_IDENTIFIER = "appIdentifier";
35 const char* PLUGIN_BUNDLE_INFO_APP_ID = "appId";
36 const char* PLUGIN_BUNDLE_INFO_MODULE_INFOS = "pluginModuleInfos";
37 }
38
ReadFromParcel(Parcel & parcel)39 bool PluginBundleInfo::ReadFromParcel(Parcel &parcel)
40 {
41 iconId = parcel.ReadUint32();
42 labelId = parcel.ReadUint32();
43 versionCode = parcel.ReadUint32();
44
45 pluginBundleName = parcel.ReadString();
46 label = parcel.ReadString();
47 icon = parcel.ReadString();
48 versionName = parcel.ReadString();
49 appIdentifier = parcel.ReadString();
50 appId = parcel.ReadString();
51
52 int32_t size;
53 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, size);
54 CONTAINER_SECURITY_VERIFY(parcel, size, &pluginModuleInfos);
55 for (auto i = 0; i < size; i++) {
56 std::unique_ptr<PluginModuleInfo> info(parcel.ReadParcelable<PluginModuleInfo>());
57 if (!info) {
58 APP_LOGE("ReadParcelable<PluginModuleInfo> failed");
59 return false;
60 }
61 pluginModuleInfos.emplace_back(*info);
62 }
63
64 return true;
65 }
66
Marshalling(Parcel & parcel) const67 bool PluginBundleInfo::Marshalling(Parcel &parcel) const
68 {
69 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, iconId);
70 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, labelId);
71 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, versionCode);
72
73 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, pluginBundleName);
74 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, label);
75 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, icon);
76 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, versionName);
77 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, appIdentifier);
78 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, appId);
79
80 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, pluginModuleInfos.size());
81 for (auto &info : pluginModuleInfos) {
82 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &info);
83 }
84
85 return true;
86 }
87
Unmarshalling(Parcel & parcel)88 PluginBundleInfo *PluginBundleInfo::Unmarshalling(Parcel &parcel)
89 {
90 PluginBundleInfo *info = new (std::nothrow) PluginBundleInfo();
91 if (info && !info->ReadFromParcel(parcel)) {
92 APP_LOGW("read from parcel failed");
93 delete info;
94 info = nullptr;
95 }
96 return info;
97 }
98
to_json(nlohmann::json & jsonObject,const PluginBundleInfo & pluginBundleInfo)99 void to_json(nlohmann::json &jsonObject, const PluginBundleInfo &pluginBundleInfo)
100 {
101 jsonObject = nlohmann::json {
102 {PLUGIN_BUNDLE_INFO_PLUGIN_BUNDLE_NAME, pluginBundleInfo.pluginBundleName},
103 {PLUGIN_BUNDLE_INFO_LABEL, pluginBundleInfo.label},
104 {PLUGIN_BUNDLE_INFO_ICON, pluginBundleInfo.icon},
105 {PLUGIN_BUNDLE_INFO_VERSION_NAME, pluginBundleInfo.versionName},
106 {PLUGIN_BUNDLE_INFO_APP_IDENTIFIER, pluginBundleInfo.appIdentifier},
107 {PLUGIN_BUNDLE_INFO_APP_ID, pluginBundleInfo.appId},
108 {PLUGIN_BUNDLE_INFO_ICON_ID, pluginBundleInfo.iconId},
109 {PLUGIN_BUNDLE_INFO_LABEL_ID, pluginBundleInfo.labelId},
110 {PLUGIN_BUNDLE_INFO_VERSION_CODE, pluginBundleInfo.versionCode},
111 {PLUGIN_BUNDLE_INFO_MODULE_INFOS, pluginBundleInfo.pluginModuleInfos}
112 };
113 }
114
from_json(const nlohmann::json & jsonObject,PluginBundleInfo & pluginBundleInfo)115 void from_json(const nlohmann::json &jsonObject, PluginBundleInfo &pluginBundleInfo)
116 {
117 const auto &jsonObjectEnd = jsonObject.end();
118 int32_t parseResult = ERR_OK;
119 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
120 jsonObjectEnd,
121 PLUGIN_BUNDLE_INFO_PLUGIN_BUNDLE_NAME,
122 pluginBundleInfo.pluginBundleName,
123 false,
124 parseResult);
125 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
126 jsonObjectEnd,
127 PLUGIN_BUNDLE_INFO_LABEL,
128 pluginBundleInfo.label,
129 false,
130 parseResult);
131 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
132 jsonObjectEnd,
133 PLUGIN_BUNDLE_INFO_ICON,
134 pluginBundleInfo.icon,
135 false,
136 parseResult);
137 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
138 jsonObjectEnd,
139 PLUGIN_BUNDLE_INFO_VERSION_NAME,
140 pluginBundleInfo.versionName,
141 false,
142 parseResult);
143 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
144 jsonObjectEnd,
145 PLUGIN_BUNDLE_INFO_APP_IDENTIFIER,
146 pluginBundleInfo.appIdentifier,
147 false,
148 parseResult);
149 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
150 jsonObjectEnd,
151 PLUGIN_BUNDLE_INFO_APP_ID,
152 pluginBundleInfo.appId,
153 false,
154 parseResult);
155 GetValueIfFindKey<uint32_t>(jsonObject,
156 jsonObjectEnd,
157 PLUGIN_BUNDLE_INFO_ICON_ID,
158 pluginBundleInfo.iconId,
159 JsonType::NUMBER,
160 false,
161 parseResult,
162 ArrayType::NOT_ARRAY);
163 GetValueIfFindKey<uint32_t>(jsonObject,
164 jsonObjectEnd,
165 PLUGIN_BUNDLE_INFO_LABEL_ID,
166 pluginBundleInfo.labelId,
167 JsonType::NUMBER,
168 false,
169 parseResult,
170 ArrayType::NOT_ARRAY);
171 GetValueIfFindKey<uint32_t>(jsonObject,
172 jsonObjectEnd,
173 PLUGIN_BUNDLE_INFO_VERSION_CODE,
174 pluginBundleInfo.versionCode,
175 JsonType::NUMBER,
176 false,
177 parseResult,
178 ArrayType::NOT_ARRAY);
179 GetValueIfFindKey<std::vector<PluginModuleInfo>>(jsonObject,
180 jsonObjectEnd,
181 PLUGIN_BUNDLE_INFO_MODULE_INFOS,
182 pluginBundleInfo.pluginModuleInfos,
183 JsonType::ARRAY,
184 false,
185 parseResult,
186 ArrayType::OBJECT);
187 if (parseResult != ERR_OK) {
188 APP_LOGE("read pluginBundleInfo error : %{public}d", parseResult);
189 }
190 }
191 } // AppExecFwk
192 } // OHOS
193