1 /*
2 * Copyright (c) 2023-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
16 #include "freeze_util.h"
17
18 #include "hilog_tag_wrapper.h"
19 #include "time_util.h"
20
21 namespace OHOS::AbilityRuntime {
22 namespace {
23 constexpr int32_t MAX_ENTRY_COUNT = 20;
ConcatStr(const std::list<std::string> & strList,const std::string & split)24 std::string ConcatStr(const std::list<std::string> &strList, const std::string &split)
25 {
26 if (strList.empty()) {
27 return "";
28 }
29 if (strList.size() == 1) {
30 return strList.front();
31 }
32
33 int32_t reserveSize = 0;
34 for (const auto &item : strList) {
35 reserveSize += static_cast<int32_t>(split.size() + item.size());
36 }
37 reserveSize -= static_cast<int32_t>(split.size());
38 std::string result;
39 if (reserveSize > 0) {
40 result.reserve(reserveSize);
41 }
42 result.append(strList.front());
43 auto iter = strList.begin();
44 for (++iter; iter != strList.end(); ++iter) {
45 result.append(split).append(*iter);
46 }
47 return result;
48 }
49 }
50
GetInstance()51 FreezeUtil& FreezeUtil::GetInstance()
52 {
53 static FreezeUtil instance;
54 return instance;
55 }
56
AddLifecycleEvent(sptr<IRemoteObject> token,const std::string & entry)57 void FreezeUtil::AddLifecycleEvent(sptr<IRemoteObject> token, const std::string &entry)
58 {
59 auto newEntry = TimeUtil::DefaultCurrentTimeStr() + "; " + entry;
60 std::lock_guard lock(mutex_);
61 auto &entryList = lifecycleFlow_[token];
62 entryList.emplace_back(TimeUtil::DefaultCurrentTimeStr() + "; " + entry);
63 if (entryList.size() > MAX_ENTRY_COUNT) {
64 entryList.pop_front();
65 }
66 }
67
AppendLifecycleEvent(sptr<IRemoteObject> token,const std::string & entry)68 bool FreezeUtil::AppendLifecycleEvent(sptr<IRemoteObject> token, const std::string &entry)
69 {
70 std::lock_guard lock(mutex_);
71 auto iter = lifecycleFlow_.find(token);
72 if (iter == lifecycleFlow_.end()) {
73 return false;
74 }
75 auto &entryList = iter->second;
76 entryList.emplace_back(TimeUtil::DefaultCurrentTimeStr() + "; " + entry);
77 if (entryList.size() > MAX_ENTRY_COUNT) {
78 entryList.pop_front();
79 }
80 return true;
81 }
82
GetLifecycleEvent(sptr<IRemoteObject> token)83 std::string FreezeUtil::GetLifecycleEvent(sptr<IRemoteObject> token)
84 {
85 std::lock_guard lock(mutex_);
86 auto search = lifecycleFlow_.find(token);
87 if (search != lifecycleFlow_.end()) {
88 return ConcatStr(search->second, "\n");
89 }
90 return "";
91 }
92
DeleteLifecycleEvent(sptr<IRemoteObject> token)93 void FreezeUtil::DeleteLifecycleEvent(sptr<IRemoteObject> token)
94 {
95 std::lock_guard lock(mutex_);
96 lifecycleFlow_.erase(token);
97 }
98
AddAppLifecycleEvent(pid_t pid,const std::string & entry)99 void FreezeUtil::AddAppLifecycleEvent(pid_t pid, const std::string &entry)
100 {
101 std::lock_guard lock(mutex_);
102 auto &entryList = appLifeCycleFlow_[pid];
103 entryList.emplace_back(TimeUtil::DefaultCurrentTimeStr() + "; " + entry);
104 if (entryList.size() > MAX_ENTRY_COUNT) {
105 entryList.pop_front();
106 }
107 }
108
DeleteAppLifecycleEvent(pid_t pid)109 void FreezeUtil::DeleteAppLifecycleEvent(pid_t pid)
110 {
111 std::lock_guard lock(mutex_);
112 appLifeCycleFlow_.erase(pid);
113 }
114
GetAppLifecycleEvent(pid_t pid)115 std::string FreezeUtil::GetAppLifecycleEvent(pid_t pid)
116 {
117 std::lock_guard lock(mutex_);
118 auto search = appLifeCycleFlow_.find(pid);
119 if (search != appLifeCycleFlow_.end()) {
120 return ConcatStr(search->second, "\n");
121 }
122 return "";
123 }
124 } // namespace OHOS::AbilityRuntime
125