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_module_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* MODULE_OVERLAY_BUNDLE_NAME = "bundleName";
29 const char* MODULE_OVERLAY_MODULE_NAME = "moduleName";
30 const char* MODULE_OVERLAY_HAP_PATH = "hapPath";
31 const char* MODULE_OVERLAY_PRIORITY = "priority";
32 const char* MODULE_OVERLAY_STATE = "state";
33 const char* MODULE_TARGET_MODULE_NAME = "targetModuleName";
34 } // namespace
35
ReadFromParcel(Parcel & parcel)36 bool OverlayModuleInfo::ReadFromParcel(Parcel &parcel)
37 {
38 bundleName = Str16ToStr8(parcel.ReadString16());
39 moduleName = Str16ToStr8(parcel.ReadString16());
40 targetModuleName = Str16ToStr8(parcel.ReadString16());
41 hapPath = Str16ToStr8(parcel.ReadString16());
42 priority = parcel.ReadInt32();
43 state = parcel.ReadInt32();
44 return true;
45 }
46
Marshalling(Parcel & parcel) const47 bool OverlayModuleInfo::Marshalling(Parcel &parcel) const
48 {
49 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
50 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
51 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(targetModuleName));
52 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hapPath));
53 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, priority);
54 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, state);
55 return true;
56 }
57
Unmarshalling(Parcel & parcel)58 OverlayModuleInfo *OverlayModuleInfo::Unmarshalling(Parcel &parcel)
59 {
60 OverlayModuleInfo *info = new (std::nothrow) OverlayModuleInfo();
61 if (info && !info->ReadFromParcel(parcel)) {
62 APP_LOGW("read from parcel failed");
63 delete info;
64 info = nullptr;
65 }
66 return info;
67 }
68
to_json(nlohmann::json & jsonObject,const OverlayModuleInfo & overlayModuleInfo)69 void to_json(nlohmann::json &jsonObject, const OverlayModuleInfo &overlayModuleInfo)
70 {
71 jsonObject = nlohmann::json {
72 {MODULE_OVERLAY_BUNDLE_NAME, overlayModuleInfo.bundleName},
73 {MODULE_OVERLAY_MODULE_NAME, overlayModuleInfo.moduleName},
74 {MODULE_TARGET_MODULE_NAME, overlayModuleInfo.targetModuleName},
75 {MODULE_OVERLAY_HAP_PATH, overlayModuleInfo.hapPath},
76 {MODULE_OVERLAY_PRIORITY, overlayModuleInfo.priority},
77 {MODULE_OVERLAY_STATE, overlayModuleInfo.state}
78 };
79 }
80
from_json(const nlohmann::json & jsonObject,OverlayModuleInfo & overlayModuleInfo)81 void from_json(const nlohmann::json &jsonObject, OverlayModuleInfo &overlayModuleInfo)
82 {
83 const auto &jsonObjectEnd = jsonObject.end();
84 int32_t parseResult = ERR_OK;
85 GetValueIfFindKey<std::string>(jsonObject,
86 jsonObjectEnd,
87 MODULE_OVERLAY_BUNDLE_NAME,
88 overlayModuleInfo.bundleName,
89 JsonType::STRING,
90 true,
91 parseResult,
92 ArrayType::NOT_ARRAY);
93 GetValueIfFindKey<std::string>(jsonObject,
94 jsonObjectEnd,
95 MODULE_OVERLAY_MODULE_NAME,
96 overlayModuleInfo.moduleName,
97 JsonType::STRING,
98 true,
99 parseResult,
100 ArrayType::NOT_ARRAY);
101 GetValueIfFindKey<std::string>(jsonObject,
102 jsonObjectEnd,
103 MODULE_TARGET_MODULE_NAME,
104 overlayModuleInfo.targetModuleName,
105 JsonType::STRING,
106 true,
107 parseResult,
108 ArrayType::NOT_ARRAY);
109 GetValueIfFindKey<std::string>(jsonObject,
110 jsonObjectEnd,
111 MODULE_OVERLAY_HAP_PATH,
112 overlayModuleInfo.hapPath,
113 JsonType::STRING,
114 true,
115 parseResult,
116 ArrayType::NOT_ARRAY);
117 GetValueIfFindKey<int32_t>(jsonObject,
118 jsonObjectEnd,
119 MODULE_OVERLAY_PRIORITY,
120 overlayModuleInfo.priority,
121 JsonType::NUMBER,
122 true,
123 parseResult,
124 ArrayType::NOT_ARRAY);
125 GetValueIfFindKey<int32_t>(jsonObject,
126 jsonObjectEnd,
127 MODULE_OVERLAY_STATE,
128 overlayModuleInfo.state,
129 JsonType::NUMBER,
130 true,
131 parseResult,
132 ArrayType::NOT_ARRAY);
133 if (parseResult != ERR_OK) {
134 APP_LOGE("overlayModuleInfo from_json error : %{public}d", parseResult);
135 }
136 }
137 } // AppExecFwk
138 } // OHOS
139