• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "distributed_bundle_option.h"
17 #include <string>
18 #include "ans_log_wrapper.h"
19 #include "parcel.h"
20 
21 namespace OHOS {
22 namespace Notification {
DistributedBundleOption(std::shared_ptr<NotificationBundleOption> & bundle,const bool enable)23 DistributedBundleOption::DistributedBundleOption(std::shared_ptr<NotificationBundleOption> &bundle, const bool enable)
24     : bundle_(bundle), enable_(enable)
25 {}
26 
~DistributedBundleOption()27 DistributedBundleOption::~DistributedBundleOption()
28 {}
29 
GetBundle() const30 std::shared_ptr<NotificationBundleOption> DistributedBundleOption::GetBundle() const
31 {
32     return bundle_;
33 }
34 
SetBundle(std::shared_ptr<NotificationBundleOption> bundle)35 void DistributedBundleOption::SetBundle(std::shared_ptr<NotificationBundleOption> bundle)
36 {
37     bundle_ = bundle;
38 }
39 
isEnable() const40 bool DistributedBundleOption::isEnable() const
41 {
42     return enable_;
43 }
44 
SetEnable(const bool & enable)45 void DistributedBundleOption::SetEnable(const bool& enable)
46 {
47     enable_ = enable;
48 }
49 
Dump()50 std::string DistributedBundleOption::Dump()
51 {
52     return "DistributedBundleOption{ "
53             "bundleName = " +(bundle_ ? bundle_->GetBundleName() : "null")  +
54             ", uid = " + (bundle_ ? std::to_string(bundle_->GetUid()) : "null") +
55             ", enable = " + std::to_string(enable_) +
56             " }";
57 }
58 
Marshalling(Parcel & parcel) const59 bool DistributedBundleOption::Marshalling(Parcel &parcel) const
60 {
61     bool valid {false};
62     valid = bundle_ ? true : false;
63     if (!parcel.WriteBool(valid)) {
64         ANS_LOGE("Failed to write the flag which indicate whether bundle is null");
65         return false;
66     }
67 
68     if (valid) {
69         if (!parcel.WriteParcelable(bundle_.get())) {
70             ANS_LOGE("Failed to write bundle");
71             return false;
72         }
73     }
74 
75     if (!parcel.WriteBool(enable_)) {
76         ANS_LOGE("Failed to write enable");
77         return false;
78     }
79 
80     return true;
81 }
82 
Unmarshalling(Parcel & parcel)83 DistributedBundleOption *DistributedBundleOption::Unmarshalling(Parcel &parcel)
84 {
85     auto pDistributedBundleOption = new (std::nothrow) DistributedBundleOption();
86     if (pDistributedBundleOption && !pDistributedBundleOption->ReadFromParcel(parcel)) {
87         delete pDistributedBundleOption;
88         pDistributedBundleOption = nullptr;
89     }
90 
91     return pDistributedBundleOption;
92 }
93 
ReadFromParcel(Parcel & parcel)94 bool DistributedBundleOption::ReadFromParcel(Parcel &parcel)
95 {
96     bool valid {false};
97 
98     valid = parcel.ReadBool();
99     if (valid) {
100         bundle_ = std::shared_ptr<NotificationBundleOption>(parcel.ReadParcelable<NotificationBundleOption>());
101         if (!bundle_) {
102             ANS_LOGE("Failed to read bundle");
103             return false;
104         }
105     }
106 
107     enable_ = parcel.ReadBool();
108 
109     return true;
110 }
111 
ToJson(nlohmann::json & jsonObject) const112 bool DistributedBundleOption::ToJson(nlohmann::json &jsonObject) const
113 {
114     if (bundle_ != nullptr) {
115         nlohmann::json bundleOptionObj;
116         if (!NotificationJsonConverter::ConvertToJson(bundle_.get(), bundleOptionObj)) {
117             ANS_LOGE("Cannot convert notificationBundleOption to JSON.");
118             return false;
119         }
120         jsonObject["bundle"] = bundleOptionObj;
121     }
122 
123     jsonObject["enable"] = enable_;
124     return true;
125 }
126 
FromJson(const nlohmann::json & jsonObject)127 DistributedBundleOption *DistributedBundleOption::FromJson(const nlohmann::json &jsonObject)
128 {
129     if (jsonObject.is_null() or !jsonObject.is_object()) {
130         ANS_LOGE("Invalid JSON object");
131         return nullptr;
132     }
133 
134     auto *pDistributedBundleOption = new (std::nothrow) DistributedBundleOption();
135     if (pDistributedBundleOption == nullptr) {
136         ANS_LOGE("null pDistributedBundleOption");
137         return nullptr;
138     }
139 
140     const auto &jsonEnd = jsonObject.cend();
141 
142     if (jsonObject.find("bundle") != jsonEnd) {
143         auto bundleOptionObj = jsonObject.at("bundle");
144         if (!bundleOptionObj.is_null()) {
145             auto *pBundleOption = NotificationJsonConverter::ConvertFromJson<NotificationBundleOption>(bundleOptionObj);
146             if (pBundleOption == nullptr) {
147                 ANS_LOGE("null pBundleOption");
148                 return nullptr;
149             }
150 
151             pDistributedBundleOption->bundle_ = std::shared_ptr<NotificationBundleOption>(pBundleOption);
152         }
153     }
154 
155     if (jsonObject.find("enable") != jsonEnd && jsonObject.at("enable").is_boolean()) {
156         pDistributedBundleOption->enable_ = jsonObject.at("enable").get<bool>();
157     }
158     return pDistributedBundleOption;
159 }
160 
161 }  // namespace Notification
162 }  // namespace OHOS