• 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_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* SHARED_BUNDLE_INFO_NAME = "name";
28 const char* SHARED_BUNDLE_INFO_COMPATIBLE_POLICY = "compatiblePolicy";
29 const char* SHARED_MODULE_INFOS = "sharedModuleInfos";
30 }
31 
ReadFromParcel(Parcel & parcel)32 bool SharedBundleInfo::ReadFromParcel(Parcel &parcel)
33 {
34     name = Str16ToStr8(parcel.ReadString16());
35     compatiblePolicy = static_cast<CompatiblePolicy>(parcel.ReadInt32());
36 
37     int32_t size;
38     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, size);
39     CONTAINER_SECURITY_VERIFY(parcel, size, &sharedModuleInfos);
40     for (auto i = 0; i < size; i++) {
41         std::unique_ptr<SharedModuleInfo> info(parcel.ReadParcelable<SharedModuleInfo>());
42         if (!info) {
43             APP_LOGE("ReadParcelable<SharedModuleInfo> failed");
44             return false;
45         }
46         sharedModuleInfos.emplace_back(*info);
47     }
48 
49     return true;
50 }
51 
Marshalling(Parcel & parcel) const52 bool SharedBundleInfo::Marshalling(Parcel &parcel) const
53 {
54     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(name));
55     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(compatiblePolicy));
56     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, sharedModuleInfos.size());
57     for (auto &info : sharedModuleInfos) {
58         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &info);
59     }
60 
61     return true;
62 }
63 
Unmarshalling(Parcel & parcel)64 SharedBundleInfo *SharedBundleInfo::Unmarshalling(Parcel &parcel)
65 {
66     SharedBundleInfo *info = new (std::nothrow) SharedBundleInfo();
67     if (info && !info->ReadFromParcel(parcel)) {
68         APP_LOGW("read from parcel failed");
69         delete info;
70         info = nullptr;
71     }
72     return info;
73 }
74 
to_json(nlohmann::json & jsonObject,const SharedBundleInfo & sharedBundleInfo)75 void to_json(nlohmann::json &jsonObject, const SharedBundleInfo &sharedBundleInfo)
76 {
77     jsonObject = nlohmann::json {
78         {SHARED_BUNDLE_INFO_NAME, sharedBundleInfo.name},
79         {SHARED_BUNDLE_INFO_COMPATIBLE_POLICY, sharedBundleInfo.compatiblePolicy},
80         {SHARED_MODULE_INFOS, sharedBundleInfo.sharedModuleInfos}
81     };
82 }
83 
from_json(const nlohmann::json & jsonObject,SharedBundleInfo & sharedBundleInfo)84 void from_json(const nlohmann::json &jsonObject, SharedBundleInfo &sharedBundleInfo)
85 {
86     const auto &jsonObjectEnd = jsonObject.end();
87     int32_t parseResult = ERR_OK;
88     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
89         jsonObjectEnd,
90         SHARED_BUNDLE_INFO_NAME,
91         sharedBundleInfo.name,
92         false,
93         parseResult);
94     GetValueIfFindKey<CompatiblePolicy>(jsonObject,
95         jsonObjectEnd,
96         SHARED_BUNDLE_INFO_COMPATIBLE_POLICY,
97         sharedBundleInfo.compatiblePolicy,
98         JsonType::NUMBER,
99         false,
100         parseResult,
101         ArrayType::NOT_ARRAY);
102     GetValueIfFindKey<std::vector<SharedModuleInfo>>(jsonObject,
103         jsonObjectEnd,
104         SHARED_MODULE_INFOS,
105         sharedBundleInfo.sharedModuleInfos,
106         JsonType::ARRAY,
107         false,
108         parseResult,
109         ArrayType::OBJECT);
110     if (parseResult != ERR_OK) {
111         APP_LOGE("read SharedBundleInfo error : %{public}d", parseResult);
112     }
113 }
114 } // AppExecFwk
115 } // OHOS