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 "notification_bundle_option.h"
17
18 #include <string> // for operator+, basic_string, to_...
19
20 #include "ans_log_wrapper.h"
21 #include "parcel.h" // for Parcel
22
23 namespace OHOS {
24 namespace Notification {
NotificationBundleOption(const std::string & bundleName,const int32_t uid)25 NotificationBundleOption::NotificationBundleOption(const std::string &bundleName, const int32_t uid)
26 : bundleName_(bundleName), uid_(uid)
27 {}
28
~NotificationBundleOption()29 NotificationBundleOption::~NotificationBundleOption()
30 {}
31
SetBundleName(const std::string & bundleName)32 void NotificationBundleOption::SetBundleName(const std::string &bundleName)
33 {
34 bundleName_ = bundleName;
35 }
36
GetBundleName() const37 std::string NotificationBundleOption::GetBundleName() const
38 {
39 return bundleName_;
40 }
41
SetUid(const int32_t uid)42 void NotificationBundleOption::SetUid(const int32_t uid)
43 {
44 uid_ = uid;
45 }
46
GetUid() const47 int32_t NotificationBundleOption::GetUid() const
48 {
49 return uid_;
50 }
51
Dump()52 std::string NotificationBundleOption::Dump()
53 {
54 return "NotificationBundleOption{ "
55 "bundleName = " + bundleName_ +
56 ", uid = " + std::to_string(uid_) +
57 " }";
58 }
59
Marshalling(Parcel & parcel) const60 bool NotificationBundleOption::Marshalling(Parcel &parcel) const
61 {
62 if (!parcel.WriteString(bundleName_)) {
63 ANS_LOGE("Failed to write bundle name");
64 return false;
65 }
66
67 if (!parcel.WriteInt32(uid_)) {
68 ANS_LOGE("Failed to write uid");
69 return false;
70 }
71
72 return true;
73 }
74
Unmarshalling(Parcel & parcel)75 NotificationBundleOption *NotificationBundleOption::Unmarshalling(Parcel &parcel)
76 {
77 auto pbundleOption = new (std::nothrow) NotificationBundleOption();
78 if (pbundleOption && !pbundleOption->ReadFromParcel(parcel)) {
79 delete pbundleOption;
80 pbundleOption = nullptr;
81 }
82
83 return pbundleOption;
84 }
85
ReadFromParcel(Parcel & parcel)86 bool NotificationBundleOption::ReadFromParcel(Parcel &parcel)
87 {
88 if (!parcel.ReadString(bundleName_)) {
89 ANS_LOGE("Failed to read bundle name");
90 return false;
91 }
92
93 uid_ = parcel.ReadInt32();
94
95 return true;
96 }
97
ToJson(nlohmann::json & jsonObject) const98 bool NotificationBundleOption::ToJson(nlohmann::json &jsonObject) const
99 {
100 jsonObject["uid"] = uid_;
101 jsonObject["bundleName"] = bundleName_;
102 return true;
103 }
104
FromJson(const nlohmann::json & jsonObject)105 NotificationBundleOption *NotificationBundleOption::FromJson(const nlohmann::json &jsonObject)
106 {
107 if (jsonObject.is_null() or !jsonObject.is_object()) {
108 ANS_LOGE("Invalid JSON object");
109 return nullptr;
110 }
111
112 auto *pBundle = new (std::nothrow) NotificationBundleOption();
113 if (pBundle == nullptr) {
114 ANS_LOGE("Failed to create bundle option instance");
115 return nullptr;
116 }
117
118 const auto &jsonEnd = jsonObject.cend();
119 if (jsonObject.find("uid") != jsonEnd && jsonObject.at("uid").is_number_integer()) {
120 pBundle->uid_ = jsonObject.at("uid").get<int32_t>();
121 }
122
123 if (jsonObject.find("bundleName") != jsonEnd && jsonObject.at("bundleName").is_string()) {
124 pBundle->bundleName_ = jsonObject.at("bundleName").get<std::string>();
125 }
126
127 return pBundle;
128 }
129
130 } // namespace Notification
131 } // namespace OHOS
132