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 {
GetInstance()22 FreezeUtil& FreezeUtil::GetInstance()
23 {
24 static FreezeUtil instance;
25 return instance;
26 }
27
AddLifecycleEvent(const LifecycleFlow & flow,const std::string & entry)28 void FreezeUtil::AddLifecycleEvent(const LifecycleFlow &flow, const std::string &entry)
29 {
30 auto newEntry = TimeUtil::DefaultCurrentTimeStr() + "; " + entry;
31 std::lock_guard lock(mutex_);
32 auto iter = lifecycleFlow_.find(flow);
33 if (iter != lifecycleFlow_.end()) {
34 iter->second += "\n" + newEntry;
35 } else {
36 lifecycleFlow_.emplace(flow, newEntry);
37 }
38 }
39
AppendLifecycleEvent(const LifecycleFlow & flow,const std::string & entry)40 bool FreezeUtil::AppendLifecycleEvent(const LifecycleFlow &flow, const std::string &entry)
41 {
42 std::lock_guard lock(mutex_);
43 auto iter = lifecycleFlow_.find(flow);
44 if (iter == lifecycleFlow_.end()) {
45 return false;
46 }
47 auto newEntry = TimeUtil::DefaultCurrentTimeStr() + "; " + entry;
48 iter->second += "\n" + newEntry;
49 return true;
50 }
51
GetLifecycleEvent(const LifecycleFlow & flow)52 std::string FreezeUtil::GetLifecycleEvent(const LifecycleFlow &flow)
53 {
54 std::lock_guard lock(mutex_);
55 auto search = lifecycleFlow_.find(flow);
56 if (search != lifecycleFlow_.end()) {
57 return search->second;
58 }
59 return "";
60 }
61
DeleteLifecycleEvent(const LifecycleFlow & flow)62 void FreezeUtil::DeleteLifecycleEvent(const LifecycleFlow &flow)
63 {
64 std::lock_guard lock(mutex_);
65 DeleteLifecycleEventInner(flow);
66 }
67
DeleteLifecycleEvent(sptr<IRemoteObject> token)68 void FreezeUtil::DeleteLifecycleEvent(sptr<IRemoteObject> token)
69 {
70 std::lock_guard lock(mutex_);
71 if (lifecycleFlow_.empty()) {
72 return;
73 }
74 LifecycleFlow foregroundFlow = { token, TimeoutState::FOREGROUND };
75 DeleteLifecycleEventInner(foregroundFlow);
76
77 LifecycleFlow backgroundFlow = { token, TimeoutState::BACKGROUND };
78 DeleteLifecycleEventInner(backgroundFlow);
79 }
80
DeleteLifecycleEventInner(const LifecycleFlow & flow)81 void FreezeUtil::DeleteLifecycleEventInner(const LifecycleFlow &flow)
82 {
83 if (lifecycleFlow_.count(flow)) {
84 lifecycleFlow_.erase(flow);
85 }
86 TAG_LOGD(AAFwkTag::DEFAULT, "lifecycleFlow size: %{public}zu", lifecycleFlow_.size());
87 }
88
AddAppLifecycleEvent(pid_t pid,const std::string & entry)89 void FreezeUtil::AddAppLifecycleEvent(pid_t pid, const std::string &entry)
90 {
91 std::lock_guard lock(mutex_);
92 auto newEntry = TimeUtil::DefaultCurrentTimeStr() + "; " + entry;
93 auto iter = appLifeCycleFlow_.find(pid);
94 if (iter != appLifeCycleFlow_.end()) {
95 iter->second += "\n" + newEntry;
96 } else {
97 appLifeCycleFlow_.emplace(pid, newEntry);
98 }
99 }
100
DeleteAppLifecycleEvent(pid_t pid)101 void FreezeUtil::DeleteAppLifecycleEvent(pid_t pid)
102 {
103 std::lock_guard lock(mutex_);
104 appLifeCycleFlow_.erase(pid);
105 }
106
GetAppLifecycleEvent(pid_t pid)107 std::string FreezeUtil::GetAppLifecycleEvent(pid_t pid)
108 {
109 std::lock_guard lock(mutex_);
110 auto search = appLifeCycleFlow_.find(pid);
111 if (search != appLifeCycleFlow_.end()) {
112 return search->second;
113 }
114 return "";
115 }
116 } // namespace OHOS::AbilityRuntime
117