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