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 "overlay_bundle_info.h"
17
18 #include "app_log_wrapper.h"
19 #include "bundle_constants.h"
20 #include "json_util.h"
21 #include "nlohmann/json.hpp"
22 #include "parcel_macro.h"
23 #include "string_ex.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 const char* BUNDLE_OVERLAY_BUNDLE_NAME = "bundleName";
29 const char* BUNDLE_OVERLAY_BUNDLE_DIR = "bundleDir";
30 const char* BUNDLE_OVERLAY_BUNDLE_STATE = "state";
31 const char* BUNDLE_OVERLAY_BUNDLE_PRIORITY = "priority";
32 } // namespace
ReadFromParcel(Parcel & parcel)33 bool OverlayBundleInfo::ReadFromParcel(Parcel &parcel)
34 {
35 bundleName = Str16ToStr8(parcel.ReadString16());
36 bundleDir = Str16ToStr8(parcel.ReadString16());
37 state = parcel.ReadInt32();
38 priority = parcel.ReadInt32();
39 return true;
40 }
41
Marshalling(Parcel & parcel) const42 bool OverlayBundleInfo::Marshalling(Parcel &parcel) const
43 {
44 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
45 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleDir));
46 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, state);
47 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, priority);
48 return true;
49 }
50
Unmarshalling(Parcel & parcel)51 OverlayBundleInfo *OverlayBundleInfo::Unmarshalling(Parcel &parcel)
52 {
53 OverlayBundleInfo *info = new (std::nothrow) OverlayBundleInfo();
54 if (info && !info->ReadFromParcel(parcel)) {
55 APP_LOGW("read from parcel failed");
56 delete info;
57 info = nullptr;
58 }
59 return info;
60 }
61
to_json(nlohmann::json & jsonObject,const OverlayBundleInfo & overlayBundleInfo)62 void to_json(nlohmann::json &jsonObject, const OverlayBundleInfo &overlayBundleInfo)
63 {
64 jsonObject = nlohmann::json {
65 {BUNDLE_OVERLAY_BUNDLE_NAME, overlayBundleInfo.bundleName},
66 {BUNDLE_OVERLAY_BUNDLE_DIR, overlayBundleInfo.bundleDir},
67 {BUNDLE_OVERLAY_BUNDLE_STATE, overlayBundleInfo.state},
68 {BUNDLE_OVERLAY_BUNDLE_PRIORITY, overlayBundleInfo.priority}
69 };
70 }
71
from_json(const nlohmann::json & jsonObject,OverlayBundleInfo & overlayBundleInfo)72 void from_json(const nlohmann::json &jsonObject, OverlayBundleInfo &overlayBundleInfo)
73 {
74 const auto &jsonObjectEnd = jsonObject.end();
75 int32_t parseResult = ERR_OK;
76 GetValueIfFindKey<std::string>(jsonObject,
77 jsonObjectEnd,
78 BUNDLE_OVERLAY_BUNDLE_NAME,
79 overlayBundleInfo.bundleName,
80 JsonType::STRING,
81 true,
82 parseResult,
83 ArrayType::NOT_ARRAY);
84 GetValueIfFindKey<std::string>(jsonObject,
85 jsonObjectEnd,
86 BUNDLE_OVERLAY_BUNDLE_DIR,
87 overlayBundleInfo.bundleDir,
88 JsonType::STRING,
89 true,
90 parseResult,
91 ArrayType::NOT_ARRAY);
92 GetValueIfFindKey<int32_t>(jsonObject,
93 jsonObjectEnd,
94 BUNDLE_OVERLAY_BUNDLE_STATE,
95 overlayBundleInfo.state,
96 JsonType::NUMBER,
97 true,
98 parseResult,
99 ArrayType::NOT_ARRAY);
100 GetValueIfFindKey<int32_t>(jsonObject,
101 jsonObjectEnd,
102 BUNDLE_OVERLAY_BUNDLE_PRIORITY,
103 overlayBundleInfo.priority,
104 JsonType::NUMBER,
105 true,
106 parseResult,
107 ArrayType::NOT_ARRAY);
108 if (parseResult != ERR_OK) {
109 APP_LOGE("overlayBundleInfo from_json error : %{public}d", parseResult);
110 }
111 }
112 } // AppExecFwk
113 } // OHOS
114