1/*
2 * Copyright (c) 2022 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_MEMORY_MEMMGR_APP_STATE_SUBSCRIBER_H
17#define OHOS_MEMORY_MEMMGR_APP_STATE_SUBSCRIBER_H
18
19#include <iremote_broker.h>
20
21#include "i_mem_mgr.h"
22#ifdef USE_PURGEABLE_MEMORY
23#include "app_state_subscriber_stub.h"
24#else
25#include "memory_level_constants.h"
26#endif
27
28namespace OHOS {
29namespace Memory {
30class AppStateSubscriber {
31public:
32    /* *
33     * Default constructor used to create a instance.
34     */
35    AppStateSubscriber();
36
37    /* *
38     * Default destructor.
39     */
40    virtual ~AppStateSubscriber();
41
42    /* *
43     * Called back when the subscriber is connected to Memory Manager Service.
44     */
45    virtual void OnConnected();
46
47    /* *
48     * Called back when the subscriber is disconnected to Memory Manager Service.
49     */
50    virtual void OnDisconnected();
51
52    /* *
53     * @brief Called back when app state change.
54     *
55     * @param pid pid of the process whose state is changed.
56     * @param uid uid of the process whose state is changed.
57     * @param state new state of the app.
58     */
59    virtual void OnAppStateChanged(int32_t pid, int32_t uid, int32_t state);
60
61    /* *
62     * @brief Called back when need to reclaim memory.
63     *
64     * @param pid pid of the process which need to reclaim.
65     * @param uid uid of the process which need to reclaim.
66     */
67    virtual void ForceReclaim(int32_t pid, int32_t uid);
68
69    /* *
70     * @brief Called back when get systemMemoryLevel message.
71     *
72     * @param level current memory level.
73     */
74    virtual void OnTrim(SystemMemoryLevel level);
75
76    /* *
77     * @brief Called back when the Memory Manager Service has died.
78     */
79    virtual void OnRemoteDied(const wptr<IRemoteObject> &object);
80
81#ifdef USE_PURGEABLE_MEMORY
82private:
83    class AppStateSubscriberImpl final : public AppStateSubscriberStub {
84    public:
85        class DeathRecipient final : public IRemoteObject::DeathRecipient {
86        public:
87            DeathRecipient(AppStateSubscriberImpl &subscriberImpl);
88
89            ~DeathRecipient();
90
91            /* *
92             * @brief Called back when remote object has died.
93             *
94             * @param object Object which has died.
95             */
96            void OnRemoteDied(const wptr<IRemoteObject> &object) override;
97
98        private:
99            AppStateSubscriberImpl &subscriberImpl_;
100        };
101
102    public:
103        AppStateSubscriberImpl(AppStateSubscriber &subscriber);
104
105        /* *
106         * @brief Called back when the subscriber is connected to Memory Manager Service.
107         */
108        void OnConnected() override;
109
110        /* *
111         * @brief Called back when the subscriber is disconnected to Memory Manager Service.
112         */
113        void OnDisconnected() override;
114
115        /* *
116         * @brief Called back when app state change.
117         *
118         * @param pid pid of the process whose state is changed.
119         * @param uid uid of the process whose state is changed.
120         * @param state new state of the app.
121         */
122        void OnAppStateChanged(int32_t pid, int32_t uid, int32_t state) override;
123
124        /* *
125         * @brief Called back when need to reclaim memory.
126         *
127         * @param pid pid of the process which need to reclaim.
128         * @param uid uid of the process which need to reclaim.
129         */
130        void ForceReclaim(int32_t pid, int32_t uid) override;
131
132        /* *
133         * @brief Called back when get systemMemoryLevel message.
134         *
135         * @param level current memory level.
136         */
137        void OnTrim(SystemMemoryLevel level) override;
138
139        /* *
140         * @brief Get managed proxy of memory manager.
141         *
142         * @return True if success, else false.
143         */
144        bool GetMemMgrProxy();
145
146        void OnListenerDied();
147
148    public:
149        AppStateSubscriber &subscriber_;
150        sptr<DeathRecipient> recipient_ { nullptr };
151        sptr<IMemMgr> proxy_ { nullptr };
152        std::mutex mutex_proxy {};
153        std::mutex mutex_alive {};
154
155    private:
156        bool isListenerAlive_ = true;
157    };
158private:
159    const sptr<AppStateSubscriberImpl> GetImpl() const;
160
161private:
162    sptr<AppStateSubscriberImpl> impl_ { nullptr };
163
164    friend class MemMgrClient;
165#endif // USE_PURGEABLE_MEMORY
166};
167} // namespace Memory
168} // namespace OHOS
169#endif // OHOS_MEMORY_MEMMGR_APP_STATE_SUBSCRIBER_H
170