/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "bundle_info.h" #include "string_ex.h" #include "app_log_wrapper.h" #include "json_serializer.h" #include "parcel_macro.h" #include "message_parcel.h" namespace OHOS { namespace AppExecFwk { bool BundleInfo::ReadFromParcel(Parcel &parcel) { MessageParcel *messageParcel = reinterpret_cast(&parcel); if (!messageParcel) { APP_LOGE("Type conversion failed"); return false; } uint32_t length = messageParcel->ReadUint32(); if (length == 0) { APP_LOGE("Invalid data length"); return false; } const char *data = reinterpret_cast(messageParcel->ReadRawData(length)); if (!data) { APP_LOGE("Fail to read raw data, length = %{public}d", length); return false; } nlohmann::json jsonObject = nlohmann::json::parse(data, nullptr, false); if (jsonObject.is_discarded()) { APP_LOGE("failed to parse BundleInfo"); return false; } *this = jsonObject.get(); return true; } bool BundleInfo::Marshalling(Parcel &parcel) const { MessageParcel *messageParcel = reinterpret_cast(&parcel); if (!messageParcel) { APP_LOGE("Type conversion failed"); return false; } nlohmann::json json = *this; std::string str = json.dump(); if (!messageParcel->WriteUint32(str.size() + 1)) { APP_LOGE("Failed to write data size"); return false; } if (!messageParcel->WriteRawData(str.c_str(), str.size() + 1)) { APP_LOGE("Failed to write data"); return false; } return true; } BundleInfo *BundleInfo::Unmarshalling(Parcel &parcel) { BundleInfo *info = new (std::nothrow) BundleInfo(); if (info && !info->ReadFromParcel(parcel)) { APP_LOGW("read from parcel failed"); delete info; info = nullptr; } return info; } void to_json(nlohmann::json &jsonObject, const BundleInfo &bundleInfo) { jsonObject = nlohmann::json{ {"name", bundleInfo.name}, {"label", bundleInfo.label}, {"description", bundleInfo.description}, {"vendor", bundleInfo.vendor}, {"isKeepAlive", bundleInfo.isKeepAlive}, {"isNativeApp", bundleInfo.isNativeApp}, {"isDifferentName", bundleInfo.isDifferentName}, {"applicationInfo", bundleInfo.applicationInfo}, {"abilityInfos", bundleInfo.abilityInfos}, {"jointUserId", bundleInfo.jointUserId}, {"versionCode", bundleInfo.versionCode}, {"versionName", bundleInfo.versionName}, {"minSdkVersion", bundleInfo.minSdkVersion}, {"maxSdkVersion", bundleInfo.maxSdkVersion}, {"mainEntry", bundleInfo.mainEntry}, {"cpuAbi", bundleInfo.cpuAbi}, {"appId", bundleInfo.appId}, {"compatibleVersion", bundleInfo.compatibleVersion}, {"targetVersion", bundleInfo.targetVersion}, {"releaseType", bundleInfo.releaseType}, {"uid", bundleInfo.uid}, {"gid", bundleInfo.gid}, {"seInfo", bundleInfo.seInfo}, {"installTime", bundleInfo.installTime}, {"updateTime", bundleInfo.updateTime}, {"entryModuleName", bundleInfo.entryModuleName}, {"reqPermissions", bundleInfo.reqPermissions}, {"defPermissions", bundleInfo.defPermissions}, {"hapModuleNames", bundleInfo.hapModuleNames}, {"moduleNames", bundleInfo.moduleNames}, {"modulePublicDirs", bundleInfo.modulePublicDirs}, {"moduleDirs", bundleInfo.moduleDirs}, {"moduleResPaths", bundleInfo.moduleResPaths} }; } void from_json(const nlohmann::json &jsonObject, BundleInfo &bundleInfo) { bundleInfo.name = jsonObject.at("name").get(); bundleInfo.label = jsonObject.at("label").get(); bundleInfo.description = jsonObject.at("description").get(); bundleInfo.vendor = jsonObject.at("vendor").get(); bundleInfo.isKeepAlive = jsonObject.at("isKeepAlive").get(); bundleInfo.isNativeApp = jsonObject.at("isNativeApp").get(); bundleInfo.isDifferentName = jsonObject.at("isDifferentName").get(); bundleInfo.applicationInfo = jsonObject.at("applicationInfo").get(); bundleInfo.abilityInfos = jsonObject.at("abilityInfos").get>(); bundleInfo.versionCode = jsonObject.at("versionCode").get(); bundleInfo.versionName = jsonObject.at("versionName").get(); bundleInfo.jointUserId = jsonObject.at("jointUserId").get(); bundleInfo.minSdkVersion = jsonObject.at("minSdkVersion").get(); bundleInfo.maxSdkVersion = jsonObject.at("maxSdkVersion").get(); bundleInfo.mainEntry = jsonObject.at("mainEntry").get(); bundleInfo.cpuAbi = jsonObject.at("cpuAbi").get(); bundleInfo.appId = jsonObject.at("appId").get(); bundleInfo.compatibleVersion = jsonObject.at("compatibleVersion").get(); bundleInfo.targetVersion = jsonObject.at("targetVersion").get(); bundleInfo.releaseType = jsonObject.at("releaseType").get(); bundleInfo.uid = jsonObject.at("uid").get(); bundleInfo.gid = jsonObject.at("gid").get(); bundleInfo.seInfo = jsonObject.at("seInfo").get(); bundleInfo.installTime = jsonObject.at("installTime").get(); bundleInfo.updateTime = jsonObject.at("updateTime").get(); bundleInfo.entryModuleName = jsonObject.at("entryModuleName").get(); bundleInfo.reqPermissions = jsonObject.at("reqPermissions").get>(); bundleInfo.defPermissions = jsonObject.at("defPermissions").get>(); bundleInfo.hapModuleNames = jsonObject.at("hapModuleNames").get>(); bundleInfo.moduleNames = jsonObject.at("moduleNames").get>(); bundleInfo.modulePublicDirs = jsonObject.at("modulePublicDirs").get>(); bundleInfo.moduleDirs = jsonObject.at("moduleDirs").get>(); bundleInfo.moduleResPaths = jsonObject.at("moduleResPaths").get>(); } } // namespace AppExecFwk } // namespace OHOS