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 #include "app_state_subscriber.h"
17
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20 #include "memmgr_log.h"
21
22 namespace OHOS {
23 namespace Memory {
24 namespace {
25 const std::string TAG = "AppStateSubscriber";
26 }
AppStateSubscriber()27 AppStateSubscriber::AppStateSubscriber()
28 {
29 #ifdef USE_PURGEABLE_MEMORY
30 impl_ = new (std::nothrow) AppStateSubscriberImpl(*this);
31 #endif
32 }
33
~AppStateSubscriber()34 AppStateSubscriber::~AppStateSubscriber()
35 {
36 #ifdef USE_PURGEABLE_MEMORY
37 impl_->OnListenerDied();
38 delete impl_;
39 #endif
40 }
41
OnConnected()42 void AppStateSubscriber::OnConnected() {}
43
OnDisconnected()44 void AppStateSubscriber::OnDisconnected() {}
45
OnAppStateChanged(int32_t pid,int32_t uid,int32_t state)46 void AppStateSubscriber::OnAppStateChanged(int32_t pid, int32_t uid, int32_t state) {}
47
OnTrim(SystemMemoryLevel level)48 void AppStateSubscriber::OnTrim(SystemMemoryLevel level) {}
49
ForceReclaim(int32_t pid,int32_t uid)50 void AppStateSubscriber::ForceReclaim(int32_t pid, int32_t uid) {}
51
OnRemoteDied(const wptr<IRemoteObject> & object)52 void AppStateSubscriber::OnRemoteDied(const wptr<IRemoteObject> &object) {}
53
54 #ifdef USE_PURGEABLE_MEMORY
GetImpl() const55 const sptr<AppStateSubscriber::AppStateSubscriberImpl> AppStateSubscriber::GetImpl() const
56 {
57 return impl_;
58 }
59
AppStateSubscriberImpl(AppStateSubscriber & subscriber)60 AppStateSubscriber::AppStateSubscriberImpl::AppStateSubscriberImpl(AppStateSubscriber &subscriber)
61 : subscriber_(subscriber)
62 {
63 recipient_ = new (std::nothrow) DeathRecipient(*this);
64 }
65
~AppStateSubscriberImpl()66 AppStateSubscriber::AppStateSubscriberImpl::~AppStateSubscriberImpl()
67 {
68 delete recipient_;
69 }
70
OnConnected()71 void AppStateSubscriber::AppStateSubscriberImpl::OnConnected()
72 {
73 if (GetMemMgrProxy() && recipient_ != nullptr) {
74 proxy_->AsObject()->AddDeathRecipient(recipient_);
75 }
76 HILOGI("CALLED");
77 std::lock_guard<std::mutex> lock(mutex_alive);
78 if (!isListenerAlive_) {
79 HILOGE("Listener already died");
80 return;
81 }
82 subscriber_.OnConnected();
83 }
84
OnDisconnected()85 void AppStateSubscriber::AppStateSubscriberImpl::OnDisconnected()
86 {
87 if (GetMemMgrProxy() && recipient_ != nullptr) {
88 proxy_->AsObject()->RemoveDeathRecipient(recipient_);
89 }
90 HILOGI("CALLED");
91 std::lock_guard<std::mutex> lock(mutex_alive);
92 if (!isListenerAlive_) {
93 HILOGE("Listener already died");
94 return;
95 }
96 subscriber_.OnDisconnected();
97 }
98
OnAppStateChanged(int32_t pid,int32_t uid,int32_t state)99 void AppStateSubscriber::AppStateSubscriberImpl::OnAppStateChanged(int32_t pid, int32_t uid, int32_t state)
100 {
101 HILOGI("CALLED");
102 std::lock_guard<std::mutex> lock(mutex_alive);
103 if (!isListenerAlive_) {
104 HILOGE("Listener already died");
105 return;
106 }
107 subscriber_.OnAppStateChanged(pid, uid, state);
108 }
109
ForceReclaim(int32_t pid,int32_t uid)110 void AppStateSubscriber::AppStateSubscriberImpl::ForceReclaim(int32_t pid, int32_t uid)
111 {
112 HILOGI("CALLED");
113 std::lock_guard<std::mutex> lock(mutex_alive);
114 if (!isListenerAlive_) {
115 HILOGE("Listener already died");
116 return;
117 }
118 subscriber_.ForceReclaim(pid, uid);
119 }
120
OnTrim(SystemMemoryLevel level)121 void AppStateSubscriber::AppStateSubscriberImpl::OnTrim(SystemMemoryLevel level)
122 {
123 HILOGI("CALLED");
124 std::lock_guard<std::mutex> lock(mutex_alive);
125 if (!isListenerAlive_) {
126 HILOGE("Listener already died");
127 return;
128 }
129 subscriber_.OnTrim(level);
130 }
131
OnListenerDied()132 void AppStateSubscriber::AppStateSubscriberImpl::OnListenerDied()
133 {
134 HILOGI("CALLED");
135 std::lock_guard<std::mutex> lock(mutex_alive);
136 isListenerAlive_ = false;
137 }
138
GetMemMgrProxy()139 bool AppStateSubscriber::AppStateSubscriberImpl::GetMemMgrProxy()
140 {
141 std::lock_guard<std::mutex> lock(mutex_proxy);
142 if (proxy_ && proxy_->AsObject() != nullptr) {
143 return true;
144 }
145 sptr<ISystemAbilityManager> systemAbilityManager =
146 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
147 if (!systemAbilityManager) {
148 return false;
149 }
150
151 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(MEMORY_MANAGER_SA_ID);
152 if (!remoteObject) {
153 return false;
154 }
155
156 proxy_ = iface_cast<IMemMgr>(remoteObject);
157 if ((!proxy_) || (proxy_->AsObject() == nullptr)) {
158 return false;
159 }
160 return true;
161 }
162
DeathRecipient(AppStateSubscriberImpl & subscriberImpl)163 AppStateSubscriber::AppStateSubscriberImpl::DeathRecipient::DeathRecipient(AppStateSubscriberImpl &subscriberImpl)
164 : subscriberImpl_(subscriberImpl) {}
165
~DeathRecipient()166 AppStateSubscriber::AppStateSubscriberImpl::DeathRecipient::~DeathRecipient() {}
167
OnRemoteDied(const wptr<IRemoteObject> & object)168 void AppStateSubscriber::AppStateSubscriberImpl::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
169 {
170 subscriberImpl_.proxy_ = nullptr;
171 subscriberImpl_.subscriber_.OnRemoteDied(object);
172 }
173 #endif // USE_PURGEABLE_MEMORY
174 }
175 }
176