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