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 int32_t MAX_NOTIFICATION_DISABLE_NUM = 1000; 28 } // namespace SetDisabled(bool disabled)29void NotificationDisable::SetDisabled(bool disabled) 30 { 31 disabled_ = disabled; 32 } 33 SetBundleList(const std::vector<std::string> & bundleList)34void NotificationDisable::SetBundleList(const std::vector<std::string> &bundleList) 35 { 36 bundleList_ = bundleList; 37 } 38 GetDisabled() const39bool NotificationDisable::GetDisabled() const 40 { 41 return disabled_; 42 } 43 GetBundleList() const44std::vector<std::string> NotificationDisable::GetBundleList() const 45 { 46 return bundleList_; 47 } 48 Marshalling(Parcel & parcel) const49bool NotificationDisable::Marshalling(Parcel &parcel) const 50 { 51 if (!parcel.WriteBool(disabled_)) { 52 ANS_LOGE("Failed to write disabled"); 53 return false; 54 } 55 auto size = bundleList_.size(); 56 if (size > MAX_NOTIFICATION_DISABLE_NUM) { 57 ANS_LOGE("Size exceeds the range"); 58 return false; 59 } 60 if (!parcel.WriteInt32(size)) { 61 ANS_LOGE("Failed to write bundle list size"); 62 return false; 63 } 64 for (uint32_t index = 0; index < size; ++index) { 65 if (!parcel.WriteString(bundleList_[index])) { 66 ANS_LOGE("Failed to write bundle list"); 67 return false; 68 } 69 } 70 return true; 71 } 72 ReadFromParcel(Parcel & parcel)73bool NotificationDisable::ReadFromParcel(Parcel &parcel) 74 { 75 disabled_ = parcel.ReadBool(); 76 auto size = parcel.ReadUint32(); 77 if (size > MAX_NOTIFICATION_DISABLE_NUM) { 78 ANS_LOGE("Size exceeds the range"); 79 return false; 80 } 81 bundleList_.resize(size); 82 for (uint32_t index = 0; index < size; ++index) { 83 if (!parcel.ReadString(bundleList_[index])) { 84 ANS_LOGE("Failed to read bundle list"); 85 return false; 86 } 87 } 88 return true; 89 } 90 Unmarshalling(Parcel & parcel)91NotificationDisable *NotificationDisable::Unmarshalling(Parcel &parcel) 92 { 93 auto objptr = new (std::nothrow) NotificationDisable(); 94 if ((objptr != nullptr) && !objptr->ReadFromParcel(parcel)) { 95 delete objptr; 96 objptr = nullptr; 97 } 98 return objptr; 99 } 100 ToJson()101std::string NotificationDisable::ToJson() 102 { 103 nlohmann::json jsonObject; 104 jsonObject[DISABLED] = disabled_; 105 jsonObject[BUNDLELIST] = nlohmann::json(bundleList_); 106 return jsonObject.dump(); 107 } 108 FromJson(const std::string & jsonObj)109void NotificationDisable::FromJson(const std::string &jsonObj) 110 { 111 if (jsonObj.empty() || !nlohmann::json::accept(jsonObj)) { 112 ANS_LOGE("Invalid json string"); 113 return; 114 } 115 nlohmann::json jsonObject = nlohmann::json::parse(jsonObj, nullptr, false); 116 if (jsonObject.is_null() or !jsonObject.is_object()) { 117 ANS_LOGE("Invalid JSON object"); 118 return; 119 } 120 if (jsonObject.is_discarded()) { 121 ANS_LOGE("Failed to parse json string"); 122 return; 123 } 124 const auto &jsonEnd = jsonObject.cend(); 125 if (jsonObject.find(DISABLED) != jsonEnd && jsonObject.at(DISABLED).is_boolean()) { 126 disabled_ = jsonObject.at(DISABLED).get<bool>(); 127 } 128 if (jsonObject.find(BUNDLELIST) != jsonEnd && jsonObject.at(BUNDLELIST).is_array()) { 129 bundleList_ = jsonObject.at(BUNDLELIST).get<std::vector<std::string>>(); 130 } 131 } 132 } 133 }