1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#ifndef OHOS_ABILITY_RUNTIME_FREEZE_UTIL_H
17eace7efcSopenharmony_ci#define OHOS_ABILITY_RUNTIME_FREEZE_UTIL_H
18eace7efcSopenharmony_ci
19eace7efcSopenharmony_ci#include <mutex>
20eace7efcSopenharmony_ci#include <unordered_map>
21eace7efcSopenharmony_ci
22eace7efcSopenharmony_ci#include "iremote_object.h"
23eace7efcSopenharmony_ci
24eace7efcSopenharmony_cinamespace OHOS::AbilityRuntime {
25eace7efcSopenharmony_ciclass FreezeUtil {
26eace7efcSopenharmony_cipublic:
27eace7efcSopenharmony_ci    enum class TimeoutState {
28eace7efcSopenharmony_ci        UNKNOWN = 0,
29eace7efcSopenharmony_ci        LOAD,
30eace7efcSopenharmony_ci        FOREGROUND,
31eace7efcSopenharmony_ci        BACKGROUND
32eace7efcSopenharmony_ci    };
33eace7efcSopenharmony_ci
34eace7efcSopenharmony_ci    struct LifecycleFlow {
35eace7efcSopenharmony_ci        sptr<IRemoteObject> token;
36eace7efcSopenharmony_ci        TimeoutState state = TimeoutState::UNKNOWN;
37eace7efcSopenharmony_ci
38eace7efcSopenharmony_ci        bool operator==(const LifecycleFlow &other) const
39eace7efcSopenharmony_ci        {
40eace7efcSopenharmony_ci            return token == other.token && state == other.state;
41eace7efcSopenharmony_ci        }
42eace7efcSopenharmony_ci    };
43eace7efcSopenharmony_ci
44eace7efcSopenharmony_ci    FreezeUtil& operator=(const FreezeUtil&) = delete;
45eace7efcSopenharmony_ci    FreezeUtil(const FreezeUtil&) = delete;
46eace7efcSopenharmony_ci    virtual ~FreezeUtil() = default;
47eace7efcSopenharmony_ci    static FreezeUtil& GetInstance();
48eace7efcSopenharmony_ci
49eace7efcSopenharmony_ci    void AddLifecycleEvent(const LifecycleFlow &flow, const std::string &entry);
50eace7efcSopenharmony_ci    bool AppendLifecycleEvent(const LifecycleFlow &flow, const std::string &entry);
51eace7efcSopenharmony_ci    std::string GetLifecycleEvent(const LifecycleFlow &flow);
52eace7efcSopenharmony_ci    void DeleteLifecycleEvent(const LifecycleFlow &flow);
53eace7efcSopenharmony_ci    void DeleteLifecycleEvent(sptr<IRemoteObject> token);
54eace7efcSopenharmony_ci
55eace7efcSopenharmony_ci    void AddAppLifecycleEvent(pid_t pid, const std::string &entry);
56eace7efcSopenharmony_ci    void DeleteAppLifecycleEvent(pid_t pid);
57eace7efcSopenharmony_ci    std::string GetAppLifecycleEvent(pid_t pid);
58eace7efcSopenharmony_ciprivate:
59eace7efcSopenharmony_ci    FreezeUtil() = default;
60eace7efcSopenharmony_ci    void DeleteLifecycleEventInner(const LifecycleFlow &flow);
61eace7efcSopenharmony_ci
62eace7efcSopenharmony_ci    class LifecycleFlowObjHash {
63eace7efcSopenharmony_ci    public:
64eace7efcSopenharmony_ci        size_t operator() (const LifecycleFlow &flow) const
65eace7efcSopenharmony_ci        {
66eace7efcSopenharmony_ci            return reinterpret_cast<size_t>(flow.token.GetRefPtr()) + std::hash<int>()(static_cast<int>(flow.state));
67eace7efcSopenharmony_ci        }
68eace7efcSopenharmony_ci    };
69eace7efcSopenharmony_ci
70eace7efcSopenharmony_ci    std::mutex mutex_;
71eace7efcSopenharmony_ci    std::unordered_map<LifecycleFlow, std::string, LifecycleFlowObjHash> lifecycleFlow_;
72eace7efcSopenharmony_ci    std::unordered_map<pid_t, std::string> appLifeCycleFlow_;
73eace7efcSopenharmony_ci};
74eace7efcSopenharmony_ci}  // namespace OHOS::AbilityRuntime
75eace7efcSopenharmony_ci#endif  // OHOS_ABILITY_RUNTIME_FREEZE_UTIL_H