• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "mem_mgr_client.h"
17 #include "memmgr_log.h"
18 
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 
23 namespace OHOS {
24 namespace Memory {
25 namespace {
26 const std::string TAG = "MemMgrClient";
27 }
28 
29 IMPLEMENT_SINGLE_INSTANCE(MemMgrClient);
30 
GetBundlePriorityList(BundlePriorityList & bundlePrioList)31 int32_t MemMgrClient::GetBundlePriorityList(BundlePriorityList &bundlePrioList)
32 {
33     HILOGE("called");
34     auto dps = GetMemMgrService();
35     if (dps == nullptr) {
36         HILOGE("MemMgrService is null");
37         return -1;
38     }
39     return dps->GetBundlePriorityList(bundlePrioList);
40 }
41 
NotifyDistDevStatus(int32_t pid,int32_t uid,const std::string & name,bool connected)42 int32_t MemMgrClient::NotifyDistDevStatus(int32_t pid, int32_t uid, const std::string &name, bool connected)
43 {
44     HILOGI("called, pid=%{public}d, uid=%{public}d, name=%{public}s, connected=%{public}d", pid, uid, name.c_str(),
45         connected);
46     auto dps = GetMemMgrService();
47     if (dps == nullptr) {
48         HILOGE("MemMgrService is null");
49         return -1;
50     }
51     return dps->NotifyDistDevStatus(pid, uid, name, connected);
52 }
53 
GetKillLevelOfLmkd(int32_t & killLevel)54 int32_t MemMgrClient::GetKillLevelOfLmkd(int32_t &killLevel)
55 {
56     HILOGE("called");
57     auto dps = GetMemMgrService();
58     if (dps == nullptr) {
59         HILOGE("MemMgrService is null");
60         return -1;
61     }
62     return dps->GetKillLevelOfLmkd(killLevel);
63 }
64 
65 #ifdef USE_PURGEABLE_MEMORY
RegisterActiveApps(int32_t pid,int32_t uid)66 int32_t MemMgrClient::RegisterActiveApps(int32_t pid, int32_t uid)
67 {
68     HILOGI("called, pid=%{public}d, uid=%{public}d", pid, uid);
69     auto dps = GetMemMgrService();
70     if (dps == nullptr) {
71         HILOGE("MemMgrService is null");
72         return -1;
73     }
74     return dps->RegisterActiveApps(pid, uid);
75 }
76 
DeregisterActiveApps(int32_t pid,int32_t uid)77 int32_t MemMgrClient::DeregisterActiveApps(int32_t pid, int32_t uid)
78 {
79     HILOGI("called, pid=%{public}d, uid=%{public}d", pid, uid);
80     auto dps = GetMemMgrService();
81     if (dps == nullptr) {
82         HILOGE("MemMgrService is null");
83         return -1;
84     }
85     return dps->DeregisterActiveApps(pid, uid);
86 }
87 
SubscribeAppState(const AppStateSubscriber & subscriber)88 int32_t MemMgrClient::SubscribeAppState(const AppStateSubscriber &subscriber)
89 {
90     HILOGI("called");
91     auto dps = GetMemMgrService();
92     if (dps == nullptr) {
93         HILOGE("MemMgrService is null");
94         return -1;
95     }
96     sptr<AppStateSubscriber::AppStateSubscriberImpl> subscriberSptr = subscriber.GetImpl();
97     if (subscriberSptr == nullptr) {
98         HILOGE("subscriberSptr is null");
99         return -1;
100     }
101     return dps->SubscribeAppState(subscriberSptr);
102 }
103 
UnsubscribeAppState(const AppStateSubscriber & subscriber)104 int32_t MemMgrClient::UnsubscribeAppState(const AppStateSubscriber &subscriber)
105 {
106     HILOGI("called");
107     auto dps = GetMemMgrService();
108     if (dps == nullptr) {
109         HILOGE("MemMgrService is null");
110         return -1;
111     }
112     sptr<AppStateSubscriber::AppStateSubscriberImpl> subscriberSptr = subscriber.GetImpl();
113     if (subscriberSptr == nullptr) {
114         HILOGE("subscriberSptr is null");
115         return -1;
116     }
117     return dps->UnsubscribeAppState(subscriberSptr);
118 }
119 
GetAvailableMemory(int32_t & memSize)120 int32_t MemMgrClient::GetAvailableMemory(int32_t &memSize)
121 {
122     HILOGI("called");
123     auto dps = GetMemMgrService();
124     if (dps == nullptr) {
125         HILOGE("MemMgrService is null");
126         return -1;
127     }
128     return dps->GetAvailableMemory(memSize);
129 }
130 
GetTotalMemory(int32_t & memSize)131 int32_t MemMgrClient::GetTotalMemory(int32_t &memSize)
132 {
133     HILOGI("called");
134     auto dps = GetMemMgrService();
135     if (dps == nullptr) {
136         HILOGE("MemMgrService is null");
137         return -1;
138     }
139     return dps->GetTotalMemory(memSize);
140 }
141 
GetAvailableMemory()142 int32_t MemMgrClient::GetAvailableMemory()
143 {
144     int32_t memSize = 0;
145     if (GetAvailableMemory(memSize) != 0) {
146         return -1;
147     }
148     return memSize;
149 }
150 
GetTotalMemory()151 int32_t MemMgrClient::GetTotalMemory()
152 {
153     int32_t memSize = 0;
154     if (GetTotalMemory(memSize) != 0) {
155         return -1;
156     }
157     return memSize;
158 }
159 
160 #else
RegisterActiveApps(int32_t pid,int32_t uid)161 int32_t MemMgrClient::RegisterActiveApps(int32_t pid, int32_t uid)
162 {
163     return -1;
164 }
165 
DeregisterActiveApps(int32_t pid,int32_t uid)166 int32_t MemMgrClient::DeregisterActiveApps(int32_t pid, int32_t uid)
167 {
168     return -1;
169 }
170 
SubscribeAppState(const AppStateSubscriber & subscriber)171 int32_t MemMgrClient::SubscribeAppState(const AppStateSubscriber &subscriber)
172 {
173     return -1;
174 }
175 
UnsubscribeAppState(const AppStateSubscriber & subscriber)176 int32_t MemMgrClient::UnsubscribeAppState(const AppStateSubscriber &subscriber)
177 {
178     return -1;
179 }
180 
GetAvailableMemory(int32_t & memSize)181 int32_t MemMgrClient::GetAvailableMemory(int32_t &memSize)
182 {
183     return -1;
184 }
185 
GetTotalMemory(int32_t & memSize)186 int32_t MemMgrClient::GetTotalMemory(int32_t &memSize)
187 {
188     return -1;
189 }
190 
GetAvailableMemory()191 int32_t MemMgrClient::GetAvailableMemory()
192 {
193     return -1;
194 }
195 
GetTotalMemory()196 int32_t MemMgrClient::GetTotalMemory()
197 {
198     return -1;
199 }
200 #endif // USE_PURGEABLE_MEMORY
201 
OnWindowVisibilityChanged(const std::vector<sptr<MemMgrWindowInfo>> & MemMgrWindowInfo)202 int32_t MemMgrClient::OnWindowVisibilityChanged(const std::vector<sptr<MemMgrWindowInfo>> &MemMgrWindowInfo)
203 {
204     HILOGD("called");
205     auto dps = GetMemMgrService();
206     if (dps == nullptr) {
207         HILOGE("MemMgrService is null");
208         return -1;
209     }
210     return dps->OnWindowVisibilityChanged(MemMgrWindowInfo);
211 }
212 
GetReclaimPriorityByPid(int32_t pid,int32_t & priority)213 int32_t MemMgrClient::GetReclaimPriorityByPid(int32_t pid, int32_t &priority)
214 {
215     HILOGD("called");
216     auto dps = GetMemMgrService();
217     if (dps == nullptr) {
218         HILOGE("MemMgrService is null");
219         return -1;
220     }
221     return dps->GetReclaimPriorityByPid(pid, priority);
222 }
223 
GetMemMgrService()224 sptr<IMemMgr> MemMgrClient::GetMemMgrService()
225 {
226     HILOGI("called");
227     std::lock_guard<std::mutex> lock(mutex_);
228 
229     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
230     if (samgrProxy == nullptr) {
231         HILOGE("get samgr failed");
232         return nullptr;
233     }
234     auto object = samgrProxy->GetSystemAbility(MEMORY_MANAGER_SA_ID);
235     if (object == nullptr) {
236         HILOGE("get service failed");
237         return nullptr;
238     }
239     HILOGI("get service succeed");
240     dpProxy_ = iface_cast<IMemMgr>(object);
241     return dpProxy_;
242 }
243 } // namespace Memory
244 } // namespace OHOS
245