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