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