• 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 "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     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
77         jsonObjectEnd,
78         BUNDLE_OVERLAY_BUNDLE_NAME,
79         overlayBundleInfo.bundleName,
80         true,
81         parseResult);
82     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
83         jsonObjectEnd,
84         BUNDLE_OVERLAY_BUNDLE_DIR,
85         overlayBundleInfo.bundleDir,
86         true,
87         parseResult);
88     GetValueIfFindKey<int32_t>(jsonObject,
89         jsonObjectEnd,
90         BUNDLE_OVERLAY_BUNDLE_STATE,
91         overlayBundleInfo.state,
92         JsonType::NUMBER,
93         true,
94         parseResult,
95         ArrayType::NOT_ARRAY);
96     GetValueIfFindKey<int32_t>(jsonObject,
97         jsonObjectEnd,
98         BUNDLE_OVERLAY_BUNDLE_PRIORITY,
99         overlayBundleInfo.priority,
100         JsonType::NUMBER,
101         true,
102         parseResult,
103         ArrayType::NOT_ARRAY);
104     if (parseResult != ERR_OK) {
105         APP_LOGE("overlayBundleInfo from_json error : %{public}d", parseResult);
106     }
107 }
108 } // AppExecFwk
109 } // OHOS
110