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_proxy.h"
17
18 #include "mem_mgr_constant.h"
19 #include "memmgr_log.h"
20 #include "parcel.h"
21
22 namespace OHOS {
23 namespace Memory {
24 namespace {
25 const std::string TAG = "MemMgrProxy";
26 }
27
GetBundlePriorityList(BundlePriorityList & bundlePrioList)28 int32_t MemMgrProxy::GetBundlePriorityList(BundlePriorityList &bundlePrioList)
29 {
30 HILOGE("called");
31 sptr<IRemoteObject> remote = Remote();
32 MessageParcel data;
33 if (!data.WriteInterfaceToken(IMemMgr::GetDescriptor())) {
34 HILOGE("write interface token failed");
35 return ERR_FLATTEN_OBJECT;
36 }
37 if (!data.WriteParcelable(&bundlePrioList)) {
38 HILOGE("write bundlePrioList failed");
39 return ERR_FLATTEN_OBJECT;
40 }
41 MessageParcel reply;
42 MessageOption option;
43 int32_t error = remote->SendRequest(
44 static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_GET_BUNDLE_PRIORITY_LIST), data, reply, option);
45 if (error != ERR_NONE) {
46 HILOGE("transact failed, error: %{public}d", error);
47 return error;
48 }
49 std::shared_ptr<BundlePriorityList> list
50 = std::shared_ptr<BundlePriorityList>(reply.ReadParcelable<BundlePriorityList>());
51 if (list == nullptr) {
52 return -1;
53 }
54 bundlePrioList = *list;
55 return ERR_OK;
56 }
57
NotifyDistDevStatus(int32_t pid,int32_t uid,const std::string & name,bool connected)58 int32_t MemMgrProxy::NotifyDistDevStatus(int32_t pid, int32_t uid, const std::string &name, bool connected)
59 {
60 HILOGI("called, pid=%{public}d, uid=%{public}d, name=%{public}s, connected=%{public}d", pid, uid, name.c_str(),
61 connected);
62 sptr<IRemoteObject> remote = Remote();
63 MessageParcel data;
64 if (!data.WriteInterfaceToken(IMemMgr::GetDescriptor())) {
65 HILOGE("write interface token failed");
66 return ERR_FLATTEN_OBJECT;
67 }
68 if (!data.WriteInt32(pid) || !data.WriteInt32(uid) || !data.WriteString(name) || !data.WriteBool(connected)) {
69 HILOGE("write params failed");
70 return ERR_INVALID_DATA;
71 }
72 MessageParcel reply;
73 MessageOption option;
74 int32_t error = remote->SendRequest(
75 static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_NOTIFY_DIST_DEV_STATUS), data, reply, option);
76 if (error != ERR_NONE) {
77 HILOGE("transact failed, error: %{public}d", error);
78 return error;
79 }
80 int32_t ret;
81 if (!reply.ReadInt32(ret)) {
82 HILOGE("read result failed");
83 return IPC_PROXY_ERR;
84 }
85 return ret;
86 }
87
GetKillLevelOfLmkd(int32_t & killLevel)88 int32_t MemMgrProxy::GetKillLevelOfLmkd(int32_t &killLevel)
89 {
90 HILOGI("called");
91 sptr<IRemoteObject> remote = Remote();
92 if (remote == nullptr) {
93 return ERR_NULL_OBJECT;
94 }
95 MessageParcel data;
96 if (!data.WriteInterfaceToken(IMemMgr::GetDescriptor())) {
97 HILOGE("write interface token failed");
98 return ERR_FLATTEN_OBJECT;
99 }
100
101 MessageParcel reply;
102 MessageOption option;
103 int32_t error = remote->SendRequest(
104 static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_GET_KILL_LEVEL_OF_LMKD), data, reply, option);
105 if (error != ERR_NONE) {
106 HILOGE("transact failed, error: %{public}d", error);
107 return error;
108 }
109
110 int32_t curKillLevel = 0;
111 if (!reply.ReadInt32(curKillLevel)) {
112 HILOGE("read result failed");
113 return IPC_PROXY_ERR;
114 }
115 killLevel = curKillLevel;
116 return ERR_OK;
117 }
118
119 #ifdef USE_PURGEABLE_MEMORY
RegisterActiveApps(int32_t pid,int32_t uid)120 int32_t MemMgrProxy::RegisterActiveApps(int32_t pid, int32_t uid)
121 {
122 HILOGI("called, pid=%{public}d, uid=%{public}d", pid, uid);
123 sptr<IRemoteObject> remote = Remote();
124 MessageParcel data;
125 if (!data.WriteInterfaceToken(IMemMgr::GetDescriptor())) {
126 HILOGE("write interface token failed");
127 return ERR_FLATTEN_OBJECT;
128 }
129 if (!data.WriteInt32(pid) || !data.WriteInt32(uid)) {
130 HILOGE("write params failed");
131 return ERR_INVALID_DATA;
132 }
133 MessageParcel reply;
134 MessageOption option;
135 int32_t error = remote->SendRequest(
136 static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_REGISTER_ACTIVE_APPS), data, reply, option);
137 if (error != ERR_NONE) {
138 HILOGE("transact failed, error: %{public}d", error);
139 return error;
140 }
141 int32_t ret;
142 if (!reply.ReadInt32(ret)) {
143 HILOGE("read result failed");
144 return IPC_PROXY_ERR;
145 }
146 return ret;
147 }
148
DeregisterActiveApps(int32_t pid,int32_t uid)149 int32_t MemMgrProxy::DeregisterActiveApps(int32_t pid, int32_t uid)
150 {
151 HILOGI("called, pid=%{public}d, uid=%{public}d", pid, uid);
152 sptr<IRemoteObject> remote = Remote();
153 MessageParcel data;
154 if (!data.WriteInterfaceToken(IMemMgr::GetDescriptor())) {
155 HILOGE("write interface token failed");
156 return ERR_FLATTEN_OBJECT;
157 }
158 if (!data.WriteInt32(pid) || !data.WriteInt32(uid)) {
159 HILOGE("write params failed");
160 return ERR_INVALID_DATA;
161 }
162 MessageParcel reply;
163 MessageOption option;
164 int32_t error = remote->SendRequest(
165 static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_DEREGISTER_ACTIVE_APPS), data, reply, option);
166 if (error != ERR_NONE) {
167 HILOGE("transact failed, error: %{public}d", error);
168 return error;
169 }
170 int32_t ret;
171 if (!reply.ReadInt32(ret)) {
172 HILOGE("read result failed");
173 return IPC_PROXY_ERR;
174 }
175 return ret;
176 }
177
SubscribeAppState(const sptr<IAppStateSubscriber> & subscriber)178 int32_t MemMgrProxy::SubscribeAppState(const sptr<IAppStateSubscriber> &subscriber)
179 {
180 HILOGI("called");
181 if (subscriber == nullptr) {
182 HILOGE("subscriber is null");
183 return ERR_NULL_OBJECT;
184 }
185 sptr<IRemoteObject> remote = Remote();
186 MessageParcel data;
187 if (!data.WriteInterfaceToken(IMemMgr::GetDescriptor())) {
188 HILOGE("write interface token failed");
189 return ERR_FLATTEN_OBJECT;
190 }
191 if (!data.WriteRemoteObject(subscriber->AsObject())) {
192 HILOGE("write subscriber failed");
193 return ERR_INVALID_DATA;
194 }
195 MessageParcel reply;
196 MessageOption option;
197 int32_t error = remote->SendRequest(
198 static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_SUBSCRIBE_APP_STATE), data, reply, option);
199 if (error != ERR_NONE) {
200 HILOGE("transact failed, error: %{public}d", error);
201 return error;
202 }
203 int32_t ret;
204 if (!reply.ReadInt32(ret)) {
205 HILOGE("read result failed");
206 return IPC_PROXY_ERR;
207 }
208 return ret;
209 }
210
UnsubscribeAppState(const sptr<IAppStateSubscriber> & subscriber)211 int32_t MemMgrProxy::UnsubscribeAppState(const sptr<IAppStateSubscriber> &subscriber)
212 {
213 HILOGI("called");
214 if (subscriber == nullptr) {
215 HILOGE("subscriber is null");
216 return ERR_NULL_OBJECT;
217 }
218 sptr<IRemoteObject> remote = Remote();
219 MessageParcel data;
220 if (!data.WriteInterfaceToken(IMemMgr::GetDescriptor())) {
221 HILOGE("write interface token failed");
222 return ERR_FLATTEN_OBJECT;
223 }
224 if (!data.WriteRemoteObject(subscriber->AsObject())) {
225 HILOGE("write subscriber failed");
226 return ERR_INVALID_DATA;
227 }
228 MessageParcel reply;
229 MessageOption option;
230 int32_t error = remote->SendRequest(
231 static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_UNSUBSCRIBE_APP_STATE), data, reply, option);
232 if (error != ERR_NONE) {
233 HILOGE("transact failed, error: %{public}d", error);
234 return error;
235 }
236 int32_t ret;
237 if (!reply.ReadInt32(ret)) {
238 HILOGE("read result failed");
239 return IPC_PROXY_ERR;
240 }
241 return ret;
242 }
243
GetAvailableMemory(int32_t & memSize)244 int32_t MemMgrProxy::GetAvailableMemory(int32_t &memSize)
245 {
246 HILOGI("called");
247 sptr<IRemoteObject> remote = Remote();
248 MessageParcel data;
249 if (!data.WriteInterfaceToken(IMemMgr::GetDescriptor())) {
250 HILOGE("write interface token failed");
251 return ERR_FLATTEN_OBJECT;
252 }
253 MessageParcel reply;
254 MessageOption option;
255 int32_t error = remote->SendRequest(
256 static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_GET_AVAILABLE_MEMORY), data, reply, option);
257 if (error != ERR_NONE) {
258 HILOGE("transact failed, error: %{public}d", error);
259 return error;
260 }
261 if (!reply.ReadInt32(memSize)) {
262 HILOGE("read result failed");
263 return IPC_PROXY_ERR;
264 }
265 return ERR_OK;
266 }
267
GetTotalMemory(int32_t & memSize)268 int32_t MemMgrProxy::GetTotalMemory(int32_t &memSize)
269 {
270 HILOGI("called");
271 sptr<IRemoteObject> remote = Remote();
272 MessageParcel data;
273 if (!data.WriteInterfaceToken(IMemMgr::GetDescriptor())) {
274 HILOGE("write interface token failed");
275 return ERR_FLATTEN_OBJECT;
276 }
277 MessageParcel reply;
278 MessageOption option;
279 int32_t error = remote->SendRequest(
280 static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_GET_TOTAL_MEMORY), data, reply, option);
281 if (error != ERR_NONE) {
282 HILOGE("transact failed, error: %{public}d", error);
283 return error;
284 }
285 if (!reply.ReadInt32(memSize)) {
286 HILOGE("read result failed");
287 return IPC_PROXY_ERR;
288 }
289 return ERR_OK;
290 }
291 #endif // USE_PURGEABLE_MEMORY
292
OnWindowVisibilityChanged(const std::vector<sptr<MemMgrWindowInfo>> & MemMgrWindowInfo)293 int32_t MemMgrProxy::OnWindowVisibilityChanged(const std::vector<sptr<MemMgrWindowInfo>> &MemMgrWindowInfo)
294 {
295 HILOGD("called");
296 sptr<IRemoteObject> remote = Remote();
297 MessageParcel data;
298 if (!data.WriteInterfaceToken(IMemMgr::GetDescriptor())) {
299 HILOGE("write interface token failed");
300 return ERR_FLATTEN_OBJECT;
301 }
302 if (!data.WriteUint32(static_cast<uint32_t>(MemMgrWindowInfo.size()))) {
303 HILOGE("write MemMgrWindowInfo size failed");
304 return ERR_INVALID_DATA;
305 }
306 for (auto &info : MemMgrWindowInfo) {
307 if (!data.WriteParcelable(info)) {
308 HILOGE("write MemMgrWindowInfo failed");
309 return ERR_INVALID_DATA;
310 }
311 }
312 MessageParcel reply;
313 MessageOption option;
314 int32_t error = remote->SendRequest(
315 static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_ON_WINDOW_VISIBILITY_CHANGED), data, reply, option);
316 if (error != ERR_NONE) {
317 HILOGE("transact failed, error: %{public}d", error);
318 return error;
319 }
320 int32_t ret;
321 if (!reply.ReadInt32(ret)) {
322 HILOGE("read result failed");
323 return IPC_PROXY_ERR;
324 }
325 return ret;
326 }
327
GetReclaimPriorityByPid(int32_t pid,int32_t & priority)328 int32_t MemMgrProxy::GetReclaimPriorityByPid(int32_t pid, int32_t &priority)
329 {
330 HILOGD("called");
331 sptr<IRemoteObject> remote = Remote();
332 MessageParcel data;
333 if (!data.WriteInterfaceToken(IMemMgr::GetDescriptor())) {
334 HILOGE("write interface token failed");
335 return ERR_FLATTEN_OBJECT;
336 }
337 if (!data.WriteInt32(pid)) {
338 HILOGE("write pid failed");
339 return ERR_INVALID_DATA;
340 }
341 MessageParcel reply;
342 MessageOption option;
343 int32_t error = remote->SendRequest(
344 static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_GET_PRIORITY_BY_PID), data, reply, option);
345 if (error != ERR_NONE) {
346 HILOGE("transact failed, error: %{public}d", error);
347 return error;
348 }
349
350 int32_t curPriority = RECLAIM_PRIORITY_UNKNOWN + 1;
351 if (!reply.ReadInt32(curPriority)) {
352 HILOGE("read result failed");
353 return IPC_PROXY_ERR;
354 }
355 priority = curPriority;
356 return ERR_OK;
357 }
358 } // namespace Memory
359 } // namespace OHOS
360