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 return false;
91 }
92 this->uid_ = value.at("uid").get<int32_t>();
93 this->pid_ = value.at("pid").get<int32_t>();
94 this->bundleName_ = value.at("bundleName").get<std::string>();
95 this->resourceNumber_ = value.at("resourceNumber").get<uint32_t>();
96 if (value.count("resourceUnitList") > 0) {
97 const nlohmann::json &resourceVal = value.at("resourceUnitList");
98 auto nums = static_cast<int32_t>(resourceVal.size());
99 for (int i = 0; i < nums; ++i) {
100 const nlohmann::json &persistTime = resourceVal.at(i);
101 uint32_t resourceIndex = persistTime.at("resourceIndex").get<uint32_t>();
102 bool isPersist_ = persistTime.at("isPersist").get<bool>();
103 int64_t endTime_ = persistTime.at("endTime").get<int64_t>();
104 std::string reason_ = persistTime.at("reason").get<std::string>();
105 this->resourceUnitList_.emplace_back(PersistTime {resourceIndex, isPersist_, endTime_,
106 reason_});
107 }
108 }
109 return true;
110 }
111 } // namespace BackgroundTaskMgr
112 } // namespace OHOS