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 28 namespace OHOS { 29 namespace Memory { 30 class AppStateSubscriber { 31 public: 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 82 private: 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 ~AppStateSubscriberImpl(); 105 106 /* * 107 * @brief Called back when the subscriber is connected to Memory Manager Service. 108 */ 109 void OnConnected() override; 110 111 /* * 112 * @brief Called back when the subscriber is disconnected to Memory Manager Service. 113 */ 114 void OnDisconnected() override; 115 116 /* * 117 * @brief Called back when app state change. 118 * 119 * @param pid pid of the process whose state is changed. 120 * @param uid uid of the process whose state is changed. 121 * @param state new state of the app. 122 */ 123 void OnAppStateChanged(int32_t pid, int32_t uid, int32_t state) override; 124 125 /* * 126 * @brief Called back when need to reclaim memory. 127 * 128 * @param pid pid of the process which need to reclaim. 129 * @param uid uid of the process which need to reclaim. 130 */ 131 void ForceReclaim(int32_t pid, int32_t uid) override; 132 133 /* * 134 * @brief Called back when get systemMemoryLevel message. 135 * 136 * @param level current memory level. 137 */ 138 void OnTrim(SystemMemoryLevel level) override; 139 140 /* * 141 * @brief Get managed proxy of memory manager. 142 * 143 * @return True if success, else false. 144 */ 145 bool GetMemMgrProxy(); 146 147 void OnListenerDied(); 148 149 public: 150 AppStateSubscriber &subscriber_; 151 sptr<DeathRecipient> recipient_ { nullptr }; 152 sptr<IMemMgr> proxy_ { nullptr }; 153 std::mutex mutex_proxy {}; 154 std::mutex mutex_alive {}; 155 156 private: 157 bool isListenerAlive_ = true; 158 }; 159 private: 160 const sptr<AppStateSubscriberImpl> GetImpl() const; 161 162 private: 163 sptr<AppStateSubscriberImpl> impl_ { nullptr }; 164 165 friend class MemMgrClient; 166 #endif // USE_PURGEABLE_MEMORY 167 }; 168 } // namespace Memory 169 } // namespace OHOS 170 #endif // OHOS_MEMORY_MEMMGR_APP_STATE_SUBSCRIBER_H 171