1 /*
2 * Copyright (c) 2022-2025 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 "resource_application_record.h"
17
18 #include "common_utils.h"
19 #include "iremote_object.h"
20
21 #include "efficiency_resource_log.h"
22 #include "bg_efficiency_resources_mgr.h"
23 namespace OHOS {
24 namespace BackgroundTaskMgr {
PersistTime(const uint32_t resourceIndex,const bool isPersist,const int64_t endTime,const std::string & reason,const int64_t timeOut)25 PersistTime::PersistTime(const uint32_t resourceIndex, const bool isPersist, const int64_t endTime,
26 const std::string &reason, const int64_t timeOut)
27 : resourceIndex_(resourceIndex), isPersist_(isPersist), endTime_(endTime), reason_(reason), timeOut_(timeOut) {}
28
operator <(const PersistTime & rhs) const29 bool PersistTime::operator < (const PersistTime& rhs) const
30 {
31 return resourceIndex_ < rhs.resourceIndex_;
32 }
33
GetBundleName() const34 std::string ResourceApplicationRecord::GetBundleName() const
35 {
36 return bundleName_;
37 }
38
GetUid() const39 int32_t ResourceApplicationRecord::GetUid() const
40 {
41 return uid_;
42 }
43
GetPid() const44 int32_t ResourceApplicationRecord::GetPid() const
45 {
46 return pid_;
47 }
48
GetResourceNumber() const49 uint32_t ResourceApplicationRecord::GetResourceNumber() const
50 {
51 return resourceNumber_;
52 }
53
GetResourceUnitList()54 std::list<PersistTime>& ResourceApplicationRecord::GetResourceUnitList()
55 {
56 return resourceUnitList_;
57 }
58
ParseToJsonStr()59 std::string ResourceApplicationRecord::ParseToJsonStr()
60 {
61 nlohmann::json root;
62 ParseToJson(root);
63 return root.dump(CommonUtils::jsonFormat_);
64 }
65
ParseToJson(nlohmann::json & root)66 void ResourceApplicationRecord::ParseToJson(nlohmann::json &root)
67 {
68 root["bundleName"] = bundleName_;
69 root["uid"] = uid_;
70 root["pid"] = pid_;
71 root["resourceNumber"] = resourceNumber_;
72
73 if (!resourceUnitList_.empty()) {
74 nlohmann::json resource;
75 for (const auto &iter : resourceUnitList_) {
76 nlohmann::json info;
77 info["resourceIndex"] = iter.resourceIndex_;
78 info["isPersist"] = iter.isPersist_;
79 info["endTime"] = iter.endTime_;
80 info["reason"] = iter.reason_;
81 info["timeOut"] = iter.timeOut_;
82 resource.push_back(info);
83 }
84 root["resourceUnitList"] = resource;
85 }
86 }
87
ParseFromJson(const nlohmann::json & value)88 bool ResourceApplicationRecord::ParseFromJson(const nlohmann::json& value)
89 {
90 if (value.empty()) {
91 BGTASK_LOGE("value is empty");
92 return false;
93 }
94 if (!CommonUtils::CheckJsonValue(value, {"uid", "pid", "bundleName", "resourceNumber"}) ||
95 !value.at("uid").is_number_integer() || !value.at("pid").is_number_integer() ||
96 !value.at("bundleName").is_string() || !value.at("resourceNumber").is_number_integer()) {
97 BGTASK_LOGE("checkJsonValue of value is failed");
98 return false;
99 }
100 this->uid_ = value.at("uid").get<int32_t>();
101 this->pid_ = value.at("pid").get<int32_t>();
102 this->bundleName_ = value.at("bundleName").get<std::string>();
103 this->resourceNumber_ = value.at("resourceNumber").get<uint32_t>();
104 if (value.count("resourceUnitList") > 0 && value.at("resourceUnitList").is_array()) {
105 const nlohmann::json &resourceVal = value.at("resourceUnitList");
106 auto nums = static_cast<int32_t>(resourceVal.size());
107 for (int i = 0; i < nums; ++i) {
108 if (resourceVal.at(i).is_null() || !resourceVal.at(i).is_object()) {
109 BGTASK_LOGE("resourceVal.at(%{public}d) is null or is not object", i);
110 continue;
111 }
112 const nlohmann::json &persistTime = resourceVal.at(i);
113 if (!CommonUtils::CheckJsonValue(persistTime,
114 {"resourceIndex", "isPersist", "endTime", "reason", "timeOut"}) ||
115 !persistTime.at("resourceIndex").is_number_integer() || !persistTime.at("isPersist").is_boolean() ||
116 !persistTime.at("endTime").is_number_integer() || !persistTime.at("reason").is_string() ||
117 !persistTime.at("timeOut").is_number_integer()) {
118 BGTASK_LOGE("checkJsonValue of persistTime is failed");
119 continue;
120 }
121 uint32_t resourceIndex = persistTime.at("resourceIndex").get<uint32_t>();
122 bool isPersist_ = persistTime.at("isPersist").get<bool>();
123 int64_t endTime_ = persistTime.at("endTime").get<int64_t>();
124 std::string reason_ = persistTime.at("reason").get<std::string>();
125 int64_t timeOut_ = persistTime.at("timeOut").get<int64_t>();
126 this->resourceUnitList_.emplace_back(PersistTime {resourceIndex, isPersist_, endTime_,
127 reason_, timeOut_});
128 }
129 } else {
130 BGTASK_LOGE("resourceUnitList error");
131 }
132 return true;
133 }
134 } // namespace BackgroundTaskMgr
135 } // namespace OHOS