1ba5c3796Sopenharmony_ci/*
2ba5c3796Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3ba5c3796Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4ba5c3796Sopenharmony_ci * you may not use this file except in compliance with the License.
5ba5c3796Sopenharmony_ci * You may obtain a copy of the License at
6ba5c3796Sopenharmony_ci *
7ba5c3796Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8ba5c3796Sopenharmony_ci *
9ba5c3796Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10ba5c3796Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11ba5c3796Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12ba5c3796Sopenharmony_ci * See the License for the specific language governing permissions and
13ba5c3796Sopenharmony_ci * limitations under the License.
14ba5c3796Sopenharmony_ci */
15ba5c3796Sopenharmony_ci
16ba5c3796Sopenharmony_ci#ifndef OHOS_MEMORY_MEMMGR_APP_STATE_SUBSCRIBER_H
17ba5c3796Sopenharmony_ci#define OHOS_MEMORY_MEMMGR_APP_STATE_SUBSCRIBER_H
18ba5c3796Sopenharmony_ci
19ba5c3796Sopenharmony_ci#include <iremote_broker.h>
20ba5c3796Sopenharmony_ci
21ba5c3796Sopenharmony_ci#include "i_mem_mgr.h"
22ba5c3796Sopenharmony_ci#ifdef USE_PURGEABLE_MEMORY
23ba5c3796Sopenharmony_ci#include "app_state_subscriber_stub.h"
24ba5c3796Sopenharmony_ci#else
25ba5c3796Sopenharmony_ci#include "memory_level_constants.h"
26ba5c3796Sopenharmony_ci#endif
27ba5c3796Sopenharmony_ci
28ba5c3796Sopenharmony_cinamespace OHOS {
29ba5c3796Sopenharmony_cinamespace Memory {
30ba5c3796Sopenharmony_ciclass AppStateSubscriber {
31ba5c3796Sopenharmony_cipublic:
32ba5c3796Sopenharmony_ci    /* *
33ba5c3796Sopenharmony_ci     * Default constructor used to create a instance.
34ba5c3796Sopenharmony_ci     */
35ba5c3796Sopenharmony_ci    AppStateSubscriber();
36ba5c3796Sopenharmony_ci
37ba5c3796Sopenharmony_ci    /* *
38ba5c3796Sopenharmony_ci     * Default destructor.
39ba5c3796Sopenharmony_ci     */
40ba5c3796Sopenharmony_ci    virtual ~AppStateSubscriber();
41ba5c3796Sopenharmony_ci
42ba5c3796Sopenharmony_ci    /* *
43ba5c3796Sopenharmony_ci     * Called back when the subscriber is connected to Memory Manager Service.
44ba5c3796Sopenharmony_ci     */
45ba5c3796Sopenharmony_ci    virtual void OnConnected();
46ba5c3796Sopenharmony_ci
47ba5c3796Sopenharmony_ci    /* *
48ba5c3796Sopenharmony_ci     * Called back when the subscriber is disconnected to Memory Manager Service.
49ba5c3796Sopenharmony_ci     */
50ba5c3796Sopenharmony_ci    virtual void OnDisconnected();
51ba5c3796Sopenharmony_ci
52ba5c3796Sopenharmony_ci    /* *
53ba5c3796Sopenharmony_ci     * @brief Called back when app state change.
54ba5c3796Sopenharmony_ci     *
55ba5c3796Sopenharmony_ci     * @param pid pid of the process whose state is changed.
56ba5c3796Sopenharmony_ci     * @param uid uid of the process whose state is changed.
57ba5c3796Sopenharmony_ci     * @param state new state of the app.
58ba5c3796Sopenharmony_ci     */
59ba5c3796Sopenharmony_ci    virtual void OnAppStateChanged(int32_t pid, int32_t uid, int32_t state);
60ba5c3796Sopenharmony_ci
61ba5c3796Sopenharmony_ci    /* *
62ba5c3796Sopenharmony_ci     * @brief Called back when need to reclaim memory.
63ba5c3796Sopenharmony_ci     *
64ba5c3796Sopenharmony_ci     * @param pid pid of the process which need to reclaim.
65ba5c3796Sopenharmony_ci     * @param uid uid of the process which need to reclaim.
66ba5c3796Sopenharmony_ci     */
67ba5c3796Sopenharmony_ci    virtual void ForceReclaim(int32_t pid, int32_t uid);
68ba5c3796Sopenharmony_ci
69ba5c3796Sopenharmony_ci    /* *
70ba5c3796Sopenharmony_ci     * @brief Called back when get systemMemoryLevel message.
71ba5c3796Sopenharmony_ci     *
72ba5c3796Sopenharmony_ci     * @param level current memory level.
73ba5c3796Sopenharmony_ci     */
74ba5c3796Sopenharmony_ci    virtual void OnTrim(SystemMemoryLevel level);
75ba5c3796Sopenharmony_ci
76ba5c3796Sopenharmony_ci    /* *
77ba5c3796Sopenharmony_ci     * @brief Called back when the Memory Manager Service has died.
78ba5c3796Sopenharmony_ci     */
79ba5c3796Sopenharmony_ci    virtual void OnRemoteDied(const wptr<IRemoteObject> &object);
80ba5c3796Sopenharmony_ci
81ba5c3796Sopenharmony_ci#ifdef USE_PURGEABLE_MEMORY
82ba5c3796Sopenharmony_ciprivate:
83ba5c3796Sopenharmony_ci    class AppStateSubscriberImpl final : public AppStateSubscriberStub {
84ba5c3796Sopenharmony_ci    public:
85ba5c3796Sopenharmony_ci        class DeathRecipient final : public IRemoteObject::DeathRecipient {
86ba5c3796Sopenharmony_ci        public:
87ba5c3796Sopenharmony_ci            DeathRecipient(AppStateSubscriberImpl &subscriberImpl);
88ba5c3796Sopenharmony_ci
89ba5c3796Sopenharmony_ci            ~DeathRecipient();
90ba5c3796Sopenharmony_ci
91ba5c3796Sopenharmony_ci            /* *
92ba5c3796Sopenharmony_ci             * @brief Called back when remote object has died.
93ba5c3796Sopenharmony_ci             *
94ba5c3796Sopenharmony_ci             * @param object Object which has died.
95ba5c3796Sopenharmony_ci             */
96ba5c3796Sopenharmony_ci            void OnRemoteDied(const wptr<IRemoteObject> &object) override;
97ba5c3796Sopenharmony_ci
98ba5c3796Sopenharmony_ci        private:
99ba5c3796Sopenharmony_ci            AppStateSubscriberImpl &subscriberImpl_;
100ba5c3796Sopenharmony_ci        };
101ba5c3796Sopenharmony_ci
102ba5c3796Sopenharmony_ci    public:
103ba5c3796Sopenharmony_ci        AppStateSubscriberImpl(AppStateSubscriber &subscriber);
104ba5c3796Sopenharmony_ci
105ba5c3796Sopenharmony_ci        /* *
106ba5c3796Sopenharmony_ci         * @brief Called back when the subscriber is connected to Memory Manager Service.
107ba5c3796Sopenharmony_ci         */
108ba5c3796Sopenharmony_ci        void OnConnected() override;
109ba5c3796Sopenharmony_ci
110ba5c3796Sopenharmony_ci        /* *
111ba5c3796Sopenharmony_ci         * @brief Called back when the subscriber is disconnected to Memory Manager Service.
112ba5c3796Sopenharmony_ci         */
113ba5c3796Sopenharmony_ci        void OnDisconnected() override;
114ba5c3796Sopenharmony_ci
115ba5c3796Sopenharmony_ci        /* *
116ba5c3796Sopenharmony_ci         * @brief Called back when app state change.
117ba5c3796Sopenharmony_ci         *
118ba5c3796Sopenharmony_ci         * @param pid pid of the process whose state is changed.
119ba5c3796Sopenharmony_ci         * @param uid uid of the process whose state is changed.
120ba5c3796Sopenharmony_ci         * @param state new state of the app.
121ba5c3796Sopenharmony_ci         */
122ba5c3796Sopenharmony_ci        void OnAppStateChanged(int32_t pid, int32_t uid, int32_t state) override;
123ba5c3796Sopenharmony_ci
124ba5c3796Sopenharmony_ci        /* *
125ba5c3796Sopenharmony_ci         * @brief Called back when need to reclaim memory.
126ba5c3796Sopenharmony_ci         *
127ba5c3796Sopenharmony_ci         * @param pid pid of the process which need to reclaim.
128ba5c3796Sopenharmony_ci         * @param uid uid of the process which need to reclaim.
129ba5c3796Sopenharmony_ci         */
130ba5c3796Sopenharmony_ci        void ForceReclaim(int32_t pid, int32_t uid) override;
131ba5c3796Sopenharmony_ci
132ba5c3796Sopenharmony_ci        /* *
133ba5c3796Sopenharmony_ci         * @brief Called back when get systemMemoryLevel message.
134ba5c3796Sopenharmony_ci         *
135ba5c3796Sopenharmony_ci         * @param level current memory level.
136ba5c3796Sopenharmony_ci         */
137ba5c3796Sopenharmony_ci        void OnTrim(SystemMemoryLevel level) override;
138ba5c3796Sopenharmony_ci
139ba5c3796Sopenharmony_ci        /* *
140ba5c3796Sopenharmony_ci         * @brief Get managed proxy of memory manager.
141ba5c3796Sopenharmony_ci         *
142ba5c3796Sopenharmony_ci         * @return True if success, else false.
143ba5c3796Sopenharmony_ci         */
144ba5c3796Sopenharmony_ci        bool GetMemMgrProxy();
145ba5c3796Sopenharmony_ci
146ba5c3796Sopenharmony_ci        void OnListenerDied();
147ba5c3796Sopenharmony_ci
148ba5c3796Sopenharmony_ci    public:
149ba5c3796Sopenharmony_ci        AppStateSubscriber &subscriber_;
150ba5c3796Sopenharmony_ci        sptr<DeathRecipient> recipient_ { nullptr };
151ba5c3796Sopenharmony_ci        sptr<IMemMgr> proxy_ { nullptr };
152ba5c3796Sopenharmony_ci        std::mutex mutex_proxy {};
153ba5c3796Sopenharmony_ci        std::mutex mutex_alive {};
154ba5c3796Sopenharmony_ci
155ba5c3796Sopenharmony_ci    private:
156ba5c3796Sopenharmony_ci        bool isListenerAlive_ = true;
157ba5c3796Sopenharmony_ci    };
158ba5c3796Sopenharmony_ciprivate:
159ba5c3796Sopenharmony_ci    const sptr<AppStateSubscriberImpl> GetImpl() const;
160ba5c3796Sopenharmony_ci
161ba5c3796Sopenharmony_ciprivate:
162ba5c3796Sopenharmony_ci    sptr<AppStateSubscriberImpl> impl_ { nullptr };
163ba5c3796Sopenharmony_ci
164ba5c3796Sopenharmony_ci    friend class MemMgrClient;
165ba5c3796Sopenharmony_ci#endif // USE_PURGEABLE_MEMORY
166ba5c3796Sopenharmony_ci};
167ba5c3796Sopenharmony_ci} // namespace Memory
168ba5c3796Sopenharmony_ci} // namespace OHOS
169ba5c3796Sopenharmony_ci#endif // OHOS_MEMORY_MEMMGR_APP_STATE_SUBSCRIBER_H
170