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 "allow_record.h" 17 18 namespace OHOS { 19 namespace DevStandbyMgr { ParseToJson()20nlohmann::json AllowRecord::ParseToJson() 21 { 22 nlohmann::json value; 23 value["uid"] = uid_; 24 value["pid"] = pid_; 25 value["name"] = name_; 26 value["allowType"] = allowType_; 27 value["reasonCode"] = reasonCode_; 28 if (!allowTimeList_.empty()) { 29 nlohmann::json allowList; 30 for (const auto &iter : allowTimeList_) { 31 nlohmann::json info; 32 info["allowTypeIndex"] = iter.allowTypeIndex_; 33 info["endTime"] = iter.endTime_; 34 info["reason"] = iter.reason_; 35 allowList.push_back(info); 36 } 37 value["allowTimeList"] = allowList; 38 } 39 return value; 40 } 41 setAllowTime(const nlohmann::json & persistTime)42bool AllowRecord::setAllowTime(const nlohmann::json& persistTime) 43 { 44 bool checkAllowTypeIndex = !persistTime.contains("allowTypeIndex") 45 || !persistTime["allowTypeIndex"].is_number_integer(); 46 bool checkEndTime = !persistTime.contains("endTime") || !persistTime["endTime"].is_number_integer(); 47 bool checkReason = !persistTime.contains("reason") || !persistTime["reason"].is_string(); 48 if (checkAllowTypeIndex || checkEndTime || checkReason) { 49 return false; 50 } 51 uint32_t allowTypeIndex = persistTime.at("allowTypeIndex").get<uint32_t>(); 52 int64_t endTime_ = persistTime.at("endTime").get<int64_t>(); 53 std::string reason_ = persistTime.at("reason").get<std::string>(); 54 this->allowTimeList_.emplace_back(AllowTime {allowTypeIndex, endTime_, 55 reason_}); 56 return true; 57 } 58 setAllowRecordField(const nlohmann::json & value)59bool AllowRecord::setAllowRecordField(const nlohmann::json& value) 60 { 61 bool checkUid = !value.contains("uid") || !value["uid"].is_number_integer(); 62 bool checkPid = !value.contains("pid") || !value["pid"].is_number_integer(); 63 bool checkName = !value.contains("name") || !value["name"].is_string(); 64 bool checkAllowType = !value.contains("allowType") || !value["allowType"].is_number_integer(); 65 bool checkReasonCode = !value.contains("reasonCode") || !value["reasonCode"].is_number_integer(); 66 bool checkParam = checkUid || checkPid || checkName || checkAllowType || checkReasonCode; 67 if (checkParam) { 68 return false; 69 } 70 this->uid_ = value.at("uid").get<int32_t>(); 71 this->pid_ = value.at("pid").get<int32_t>(); 72 this->name_ = value.at("name").get<std::string>(); 73 this->allowType_ = value.at("allowType").get<uint32_t>(); 74 this->reasonCode_ = value.at("reasonCode").get<uint32_t>(); 75 return true; 76 } 77 ParseFromJson(const nlohmann::json & value)78bool AllowRecord::ParseFromJson(const nlohmann::json& value) 79 { 80 if (value.empty() || !setAllowRecordField(value)) { 81 return false; 82 } 83 if (value.contains("allowTimeList") && value["allowTimeList"].is_array()) { 84 const nlohmann::json &allowTimeVal = value.at("allowTimeList"); 85 auto nums = static_cast<int32_t>(allowTimeVal.size()); 86 for (int i = 0; i < nums; ++i) { 87 const nlohmann::json &persistTime = allowTimeVal.at(i); 88 if (!setAllowTime(persistTime)) { 89 return false; 90 } 91 } 92 } 93 return true; 94 } 95 } // namespace DevStandbyMgr 96 } // namespace OHOS 97