1 /* 2 * Copyright (c) 2024 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 #include "notification_disable.h" 16 17 #include "ans_const_define.h" 18 #include "ans_log_wrapper.h" 19 #include "nlohmann/json.hpp" 20 #include "parcel.h" 21 22 namespace OHOS { 23 namespace Notification { 24 namespace { 25 constexpr const char *DISABLED = "disabled"; 26 constexpr const char *BUNDLELIST = "bundleList"; 27 constexpr const char *USERID = "userId"; 28 constexpr int32_t MAX_NOTIFICATION_DISABLE_NUM = 1000; 29 } // namespace SetDisabled(bool disabled)30void NotificationDisable::SetDisabled(bool disabled) 31 { 32 disabled_ = disabled; 33 } 34 SetBundleList(const std::vector<std::string> & bundleList)35void NotificationDisable::SetBundleList(const std::vector<std::string> &bundleList) 36 { 37 bundleList_ = bundleList; 38 } 39 SetUserId(int32_t userId)40void NotificationDisable::SetUserId(int32_t userId) 41 { 42 userId_ = userId; 43 } 44 GetDisabled() const45bool NotificationDisable::GetDisabled() const 46 { 47 return disabled_; 48 } 49 GetBundleList() const50std::vector<std::string> NotificationDisable::GetBundleList() const 51 { 52 return bundleList_; 53 } 54 GetUserId() const55int32_t NotificationDisable::GetUserId() const 56 { 57 return userId_; 58 } 59 Marshalling(Parcel & parcel) const60bool NotificationDisable::Marshalling(Parcel &parcel) const 61 { 62 if (!parcel.WriteBool(disabled_)) { 63 ANS_LOGE("Failed to write disabled"); 64 return false; 65 } 66 auto size = bundleList_.size(); 67 if (size > MAX_NOTIFICATION_DISABLE_NUM) { 68 ANS_LOGE("Size exceeds the range"); 69 return false; 70 } 71 if (!parcel.WriteInt32(size)) { 72 ANS_LOGE("Failed to write bundle list size"); 73 return false; 74 } 75 for (uint32_t index = 0; index < size; ++index) { 76 if (!parcel.WriteString(bundleList_[index])) { 77 ANS_LOGE("Failed to write bundle list"); 78 return false; 79 } 80 } 81 if (!parcel.WriteInt32(userId_)) { 82 ANS_LOGE("Failed to write userId"); 83 return false; 84 } 85 return true; 86 } 87 ReadFromParcel(Parcel & parcel)88bool NotificationDisable::ReadFromParcel(Parcel &parcel) 89 { 90 disabled_ = parcel.ReadBool(); 91 auto size = parcel.ReadUint32(); 92 if (size > MAX_NOTIFICATION_DISABLE_NUM) { 93 ANS_LOGE("Size exceeds the range"); 94 return false; 95 } 96 bundleList_.resize(size); 97 for (uint32_t index = 0; index < size; ++index) { 98 if (!parcel.ReadString(bundleList_[index])) { 99 ANS_LOGE("Failed to read bundle list"); 100 return false; 101 } 102 } 103 userId_ = parcel.ReadInt32(); 104 return true; 105 } 106 Unmarshalling(Parcel & parcel)107NotificationDisable *NotificationDisable::Unmarshalling(Parcel &parcel) 108 { 109 auto objptr = new (std::nothrow) NotificationDisable(); 110 if ((objptr != nullptr) && !objptr->ReadFromParcel(parcel)) { 111 delete objptr; 112 objptr = nullptr; 113 } 114 return objptr; 115 } 116 ToJson()117std::string NotificationDisable::ToJson() 118 { 119 nlohmann::json jsonObject; 120 jsonObject[DISABLED] = disabled_; 121 jsonObject[BUNDLELIST] = nlohmann::json(bundleList_); 122 jsonObject[USERID] = userId_; 123 return jsonObject.dump(); 124 } 125 FromJson(const std::string & jsonObj)126void NotificationDisable::FromJson(const std::string &jsonObj) 127 { 128 if (jsonObj.empty() || !nlohmann::json::accept(jsonObj)) { 129 ANS_LOGE("Invalid json string"); 130 return; 131 } 132 nlohmann::json jsonObject = nlohmann::json::parse(jsonObj, nullptr, false); 133 if (jsonObject.is_null() or !jsonObject.is_object()) { 134 ANS_LOGE("Invalid JSON object"); 135 return; 136 } 137 if (jsonObject.is_discarded()) { 138 ANS_LOGE("Failed to parse json string"); 139 return; 140 } 141 const auto &jsonEnd = jsonObject.cend(); 142 if (jsonObject.find(DISABLED) != jsonEnd && jsonObject.at(DISABLED).is_boolean()) { 143 disabled_ = jsonObject.at(DISABLED).get<bool>(); 144 } 145 if (jsonObject.find(BUNDLELIST) != jsonEnd && jsonObject.at(BUNDLELIST).is_array()) { 146 bundleList_ = jsonObject.at(BUNDLELIST).get<std::vector<std::string>>(); 147 } 148 if (jsonObject.find(USERID) != jsonEnd && jsonObject.at(USERID).is_number_integer()) { 149 userId_ = jsonObject.at(USERID).get<int32_t>(); 150 } 151 } 152 } 153 }