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 #ifndef OHOS_ABILITY_RUNTIME_FREEZE_UTIL_H 17 #define OHOS_ABILITY_RUNTIME_FREEZE_UTIL_H 18 19 #include <mutex> 20 #include <unordered_map> 21 22 #include "iremote_object.h" 23 24 namespace OHOS::AbilityRuntime { 25 class FreezeUtil { 26 public: 27 enum class TimeoutState { 28 UNKNOWN = 0, 29 LOAD, 30 FOREGROUND, 31 BACKGROUND 32 }; 33 34 struct LifecycleFlow { 35 sptr<IRemoteObject> token; 36 TimeoutState state = TimeoutState::UNKNOWN; 37 38 bool operator==(const LifecycleFlow &other) const 39 { 40 return token == other.token && state == other.state; 41 } 42 }; 43 44 FreezeUtil& operator=(const FreezeUtil&) = delete; 45 FreezeUtil(const FreezeUtil&) = delete; 46 virtual ~FreezeUtil() = default; 47 static FreezeUtil& GetInstance(); 48 49 void AddLifecycleEvent(const LifecycleFlow &flow, const std::string &entry); 50 bool AppendLifecycleEvent(const LifecycleFlow &flow, const std::string &entry); 51 std::string GetLifecycleEvent(const LifecycleFlow &flow); 52 void DeleteLifecycleEvent(const LifecycleFlow &flow); 53 void DeleteLifecycleEvent(sptr<IRemoteObject> token); 54 55 void AddAppLifecycleEvent(pid_t pid, const std::string &entry); 56 void DeleteAppLifecycleEvent(pid_t pid); 57 std::string GetAppLifecycleEvent(pid_t pid); 58 private: 59 FreezeUtil() = default; 60 void DeleteLifecycleEventInner(const LifecycleFlow &flow); 61 62 class LifecycleFlowObjHash { 63 public: operator()64 size_t operator() (const LifecycleFlow &flow) const 65 { 66 return reinterpret_cast<size_t>(flow.token.GetRefPtr()) + std::hash<int>()(static_cast<int>(flow.state)); 67 } 68 }; 69 70 std::mutex mutex_; 71 std::unordered_map<LifecycleFlow, std::string, LifecycleFlowObjHash> lifecycleFlow_; 72 std::unordered_map<pid_t, std::string> appLifeCycleFlow_; 73 }; 74 } // namespace OHOS::AbilityRuntime 75 #endif // OHOS_ABILITY_RUNTIME_FREEZE_UTIL_H