1 /*
2 * Copyright (c) 2022 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)25 PersistTime::PersistTime(const uint32_t resourceIndex, const bool isPersist, const int64_t endTime,
26 const std::string &reason) : resourceIndex_(resourceIndex), isPersist_(isPersist), endTime_(endTime),
27 reason_(reason) {}
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 resource.push_back(info);
82 }
83 root["resourceUnitList"] = resource;
84 }
85 }
86
ParseFromJson(const nlohmann::json & value)87 bool ResourceApplicationRecord::ParseFromJson(const nlohmann::json& value)
88 {
89 if (value.empty()) {
90 BGTASK_LOGE("value is empty");
91 return false;
92 }
93 if (!CommonUtils::CheckJsonValue(value, {"uid", "pid", "bundleName", "resourceNumber"}) ||
94 !value.at("uid").is_number_integer() || !value.at("pid").is_number_integer() ||
95 !value.at("bundleName").is_string() || !value.at("resourceNumber").is_number_integer()) {
96 BGTASK_LOGE("checkJsonValue of value is failed");
97 return false;
98 }
99 this->uid_ = value.at("uid").get<int32_t>();
100 this->pid_ = value.at("pid").get<int32_t>();
101 this->bundleName_ = value.at("bundleName").get<std::string>();
102 this->resourceNumber_ = value.at("resourceNumber").get<uint32_t>();
103 if (value.count("resourceUnitList") > 0 && value.at("resourceUnitList").is_array()) {
104 const nlohmann::json &resourceVal = value.at("resourceUnitList");
105 auto nums = static_cast<int32_t>(resourceVal.size());
106 for (int i = 0; i < nums; ++i) {
107 if (resourceVal.at(i).is_null() || !resourceVal.at(i).is_object()) {
108 BGTASK_LOGE("resourceVal.at(%{public}d) is null or is not object", i);
109 continue;
110 }
111 const nlohmann::json &persistTime = resourceVal.at(i);
112 if (!CommonUtils::CheckJsonValue(persistTime, {"resourceIndex", "isPersist", "endTime", "reason"}) ||
113 !persistTime.at("resourceIndex").is_number_integer() || !persistTime.at("isPersist").is_boolean() ||
114 !persistTime.at("endTime").is_number_integer() || !persistTime.at("reason").is_string()) {
115 BGTASK_LOGE("checkJsonValue of persistTime is failed");
116 continue;
117 }
118 uint32_t resourceIndex = persistTime.at("resourceIndex").get<uint32_t>();
119 bool isPersist_ = persistTime.at("isPersist").get<bool>();
120 int64_t endTime_ = persistTime.at("endTime").get<int64_t>();
121 std::string reason_ = persistTime.at("reason").get<std::string>();
122 this->resourceUnitList_.emplace_back(PersistTime {resourceIndex, isPersist_, endTime_,
123 reason_});
124 }
125 } else {
126 BGTASK_LOGE("resourceUnitList error");
127 }
128 return true;
129 }
130 } // namespace BackgroundTaskMgr
131 } // namespace OHOS