• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_wrapper.h"
19 
20 namespace OHOS::AbilityRuntime {
GetInstance()21 FreezeUtil& FreezeUtil::GetInstance()
22 {
23     static FreezeUtil instance;
24     return instance;
25 }
26 
AddLifecycleEvent(const LifecycleFlow & flow,const std::string & entry)27 void FreezeUtil::AddLifecycleEvent(const LifecycleFlow &flow, const std::string &entry)
28 {
29     std::lock_guard lock(mutex_);
30     if (lifecycleFlow_.count(flow)) {
31         lifecycleFlow_[flow] = lifecycleFlow_[flow] + "\n" + entry;
32     } else {
33         lifecycleFlow_[flow] = entry;
34     }
35 }
36 
GetLifecycleEvent(const LifecycleFlow & flow)37 std::string FreezeUtil::GetLifecycleEvent(const LifecycleFlow &flow)
38 {
39     std::lock_guard lock(mutex_);
40     auto search = lifecycleFlow_.find(flow);
41     if (search != lifecycleFlow_.end()) {
42         return search->second;
43     }
44     return "";
45 }
46 
DeleteLifecycleEvent(const LifecycleFlow & flow)47 void FreezeUtil::DeleteLifecycleEvent(const LifecycleFlow &flow)
48 {
49     std::lock_guard lock(mutex_);
50     DeleteLifecycleEventInner(flow);
51 }
52 
DeleteLifecycleEvent(sptr<IRemoteObject> token)53 void FreezeUtil::DeleteLifecycleEvent(sptr<IRemoteObject> token)
54 {
55     std::lock_guard lock(mutex_);
56     if (lifecycleFlow_.empty()) {
57         return;
58     }
59     LifecycleFlow foregroundFlow = { token, TimeoutState::FOREGROUND };
60     DeleteLifecycleEventInner(foregroundFlow);
61 
62     LifecycleFlow backgroundFlow = { token, TimeoutState::BACKGROUND };
63     DeleteLifecycleEventInner(backgroundFlow);
64 }
65 
DeleteLifecycleEventInner(const LifecycleFlow & flow)66 void FreezeUtil::DeleteLifecycleEventInner(const LifecycleFlow &flow)
67 {
68     if (lifecycleFlow_.count(flow)) {
69         lifecycleFlow_.erase(flow);
70     }
71     HILOG_DEBUG("lifecycleFlow_ size: %{public}zu", lifecycleFlow_.size());
72 }
73 }  // namespace OHOS::AbilityRuntime
74