1ba5c3796Sopenharmony_ci/*
2ba5c3796Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3ba5c3796Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4ba5c3796Sopenharmony_ci * you may not use this file except in compliance with the License.
5ba5c3796Sopenharmony_ci * You may obtain a copy of the License at
6ba5c3796Sopenharmony_ci *
7ba5c3796Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8ba5c3796Sopenharmony_ci *
9ba5c3796Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10ba5c3796Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11ba5c3796Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12ba5c3796Sopenharmony_ci * See the License for the specific language governing permissions and
13ba5c3796Sopenharmony_ci * limitations under the License.
14ba5c3796Sopenharmony_ci */
15ba5c3796Sopenharmony_ci
16ba5c3796Sopenharmony_ci#include "mem_mgr_stub.h"
17ba5c3796Sopenharmony_ci
18ba5c3796Sopenharmony_ci#include "accesstoken_kit.h"
19ba5c3796Sopenharmony_ci#include "ipc_skeleton.h"
20ba5c3796Sopenharmony_ci#include "kernel_interface.h"
21ba5c3796Sopenharmony_ci#include "low_memory_killer.h"
22ba5c3796Sopenharmony_ci#include "memmgr_log.h"
23ba5c3796Sopenharmony_ci#include "memmgr_config_manager.h"
24ba5c3796Sopenharmony_ci#include "parcel.h"
25ba5c3796Sopenharmony_ci
26ba5c3796Sopenharmony_cinamespace OHOS {
27ba5c3796Sopenharmony_cinamespace Memory {
28ba5c3796Sopenharmony_cinamespace {
29ba5c3796Sopenharmony_ci    const std::string TAG = "MemMgrStub";
30ba5c3796Sopenharmony_ci    constexpr int MAX_PARCEL_SIZE = 100000;
31ba5c3796Sopenharmony_ci    constexpr int CAMERA_SERVICE_UID = 1047;
32ba5c3796Sopenharmony_ci    constexpr int FOUNDATION_UID = 5523;
33ba5c3796Sopenharmony_ci}
34ba5c3796Sopenharmony_ci
35ba5c3796Sopenharmony_ciMemMgrStub::MemMgrStub()
36ba5c3796Sopenharmony_ci{
37ba5c3796Sopenharmony_ci}
38ba5c3796Sopenharmony_ci
39ba5c3796Sopenharmony_ciMemMgrStub::~MemMgrStub()
40ba5c3796Sopenharmony_ci{
41ba5c3796Sopenharmony_ci}
42ba5c3796Sopenharmony_ci
43ba5c3796Sopenharmony_ciint MemMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
44ba5c3796Sopenharmony_ci{
45ba5c3796Sopenharmony_ci    HILOGI("MemMgrStub::OnReceived, code = %{public}d, flags= %{public}d.", code, option.GetFlags());
46ba5c3796Sopenharmony_ci    std::u16string descriptor = MemMgrStub::GetDescriptor();
47ba5c3796Sopenharmony_ci    std::u16string remoteDescriptor = data.ReadInterfaceToken();
48ba5c3796Sopenharmony_ci    if (descriptor != remoteDescriptor) {
49ba5c3796Sopenharmony_ci        HILOGE("local descriptor is not equal to remote");
50ba5c3796Sopenharmony_ci        return ERR_INVALID_STATE;
51ba5c3796Sopenharmony_ci    }
52ba5c3796Sopenharmony_ci
53ba5c3796Sopenharmony_ci    return OnRemoteRequestInner(code, data, reply, option);
54ba5c3796Sopenharmony_ci}
55ba5c3796Sopenharmony_ci
56ba5c3796Sopenharmony_ciint32_t MemMgrStub::OnRemoteRequestInner(uint32_t code,
57ba5c3796Sopenharmony_ci    MessageParcel &data, MessageParcel &reply, MessageOption &option)
58ba5c3796Sopenharmony_ci{
59ba5c3796Sopenharmony_ci    switch (code) {
60ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_GET_BUNDLE_PRIORITY_LIST):
61ba5c3796Sopenharmony_ci            return HandleGetBunldePriorityList(data, reply);
62ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_NOTIFY_DIST_DEV_STATUS):
63ba5c3796Sopenharmony_ci            return HandleNotifyDistDevStatus(data, reply);
64ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_GET_KILL_LEVEL_OF_LMKD):
65ba5c3796Sopenharmony_ci            return HandleGetKillLevelOfLmkd(data, reply);
66ba5c3796Sopenharmony_ci#ifdef USE_PURGEABLE_MEMORY
67ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_REGISTER_ACTIVE_APPS):
68ba5c3796Sopenharmony_ci            return HandleRegisterActiveApps(data, reply);
69ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_DEREGISTER_ACTIVE_APPS):
70ba5c3796Sopenharmony_ci            return HandleDeregisterActiveApps(data, reply);
71ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_SUBSCRIBE_APP_STATE):
72ba5c3796Sopenharmony_ci            return HandleSubscribeAppState(data, reply);
73ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_UNSUBSCRIBE_APP_STATE):
74ba5c3796Sopenharmony_ci            return HandleUnsubscribeAppState(data, reply);
75ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_GET_AVAILABLE_MEMORY):
76ba5c3796Sopenharmony_ci            return HandleGetAvailableMemory(data, reply);
77ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_GET_TOTAL_MEMORY):
78ba5c3796Sopenharmony_ci            return HandleGetTotalMemory(data, reply);
79ba5c3796Sopenharmony_ci#endif
80ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_ON_WINDOW_VISIBILITY_CHANGED):
81ba5c3796Sopenharmony_ci            return HandleOnWindowVisibilityChanged(data, reply);
82ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_GET_PRIORITY_BY_PID):
83ba5c3796Sopenharmony_ci            return HandleGetReclaimPriorityByPid(data, reply);
84ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_NOTIFY_PROCESS_STATE_CHANGED_SYNC):
85ba5c3796Sopenharmony_ci            return HandleNotifyProcessStateChangedSync(data, reply);
86ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_NOTIFY_PROCESS_STATE_CHANGED_ASYNC):
87ba5c3796Sopenharmony_ci            return HandleNotifyProcessStateChangedAsync(data, reply);
88ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_NOTIFY_PROCESS_STATUS):
89ba5c3796Sopenharmony_ci            return HandleNotifyProcessStatus(data, reply);
90ba5c3796Sopenharmony_ci        case static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_SET_CRITICAL):
91ba5c3796Sopenharmony_ci            return HandleSetCritical(data, reply);
92ba5c3796Sopenharmony_ci        default:
93ba5c3796Sopenharmony_ci            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
94ba5c3796Sopenharmony_ci    }
95ba5c3796Sopenharmony_ci}
96ba5c3796Sopenharmony_ci
97ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleGetBunldePriorityList(MessageParcel &data, MessageParcel &reply)
98ba5c3796Sopenharmony_ci{
99ba5c3796Sopenharmony_ci    HILOGI("called");
100ba5c3796Sopenharmony_ci    std::shared_ptr<BundlePriorityList> list
101ba5c3796Sopenharmony_ci        = std::shared_ptr<BundlePriorityList>(data.ReadParcelable<BundlePriorityList>());
102ba5c3796Sopenharmony_ci
103ba5c3796Sopenharmony_ci    if (!list) {
104ba5c3796Sopenharmony_ci        HILOGE("BundlePriorityList ReadParcelable failed");
105ba5c3796Sopenharmony_ci        return -1;
106ba5c3796Sopenharmony_ci    }
107ba5c3796Sopenharmony_ci    int32_t ret = GetBundlePriorityList(*list);
108ba5c3796Sopenharmony_ci    reply.WriteParcelable(list.get());
109ba5c3796Sopenharmony_ci    return ret;
110ba5c3796Sopenharmony_ci}
111ba5c3796Sopenharmony_ci
112ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleNotifyDistDevStatus(MessageParcel &data, MessageParcel &reply)
113ba5c3796Sopenharmony_ci{
114ba5c3796Sopenharmony_ci    HILOGI("called");
115ba5c3796Sopenharmony_ci    int32_t pid = 0;
116ba5c3796Sopenharmony_ci    int32_t uid = 0;
117ba5c3796Sopenharmony_ci    std::string name;
118ba5c3796Sopenharmony_ci    bool connected;
119ba5c3796Sopenharmony_ci    if (!data.ReadInt32(pid) || !data.ReadInt32(uid) || !data.ReadString(name) || !data.ReadBool(connected)) {
120ba5c3796Sopenharmony_ci        HILOGE("read params failed");
121ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
122ba5c3796Sopenharmony_ci    }
123ba5c3796Sopenharmony_ci    HILOGI("called, pid=%{public}d, uid=%{public}d, name=%{public}s, connected=%{public}d", pid, uid, name.c_str(),
124ba5c3796Sopenharmony_ci        connected);
125ba5c3796Sopenharmony_ci
126ba5c3796Sopenharmony_ci    int32_t ret = NotifyDistDevStatus(pid, uid, name, connected);
127ba5c3796Sopenharmony_ci    if (!reply.WriteInt32(ret)) {
128ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
129ba5c3796Sopenharmony_ci    }
130ba5c3796Sopenharmony_ci    return ret;
131ba5c3796Sopenharmony_ci}
132ba5c3796Sopenharmony_ci
133ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleGetKillLevelOfLmkd(MessageParcel &data, MessageParcel &reply)
134ba5c3796Sopenharmony_ci{
135ba5c3796Sopenharmony_ci    HILOGI("called");
136ba5c3796Sopenharmony_ci    int32_t killLevel = LowMemoryKiller::GetInstance().GetKillLevel();
137ba5c3796Sopenharmony_ci    if (!reply.WriteInt32(killLevel)) {
138ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
139ba5c3796Sopenharmony_ci    }
140ba5c3796Sopenharmony_ci    return 0;
141ba5c3796Sopenharmony_ci}
142ba5c3796Sopenharmony_ci
143ba5c3796Sopenharmony_ci#ifdef USE_PURGEABLE_MEMORY
144ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleRegisterActiveApps(MessageParcel &data, MessageParcel &reply)
145ba5c3796Sopenharmony_ci{
146ba5c3796Sopenharmony_ci    HILOGI("called");
147ba5c3796Sopenharmony_ci    int32_t pid = 0;
148ba5c3796Sopenharmony_ci    int32_t uid = 0;
149ba5c3796Sopenharmony_ci    if (!data.ReadInt32(pid) || !data.ReadInt32(uid)) {
150ba5c3796Sopenharmony_ci        HILOGE("read params failed");
151ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
152ba5c3796Sopenharmony_ci    }
153ba5c3796Sopenharmony_ci    HILOGI("called, pid=%{public}d, uid=%{public}d", pid, uid);
154ba5c3796Sopenharmony_ci
155ba5c3796Sopenharmony_ci    int32_t ret = RegisterActiveApps(pid, uid);
156ba5c3796Sopenharmony_ci    if (!reply.WriteInt32(ret)) {
157ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
158ba5c3796Sopenharmony_ci    }
159ba5c3796Sopenharmony_ci    return ret;
160ba5c3796Sopenharmony_ci}
161ba5c3796Sopenharmony_ci
162ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleDeregisterActiveApps(MessageParcel &data, MessageParcel &reply)
163ba5c3796Sopenharmony_ci{
164ba5c3796Sopenharmony_ci    HILOGI("called");
165ba5c3796Sopenharmony_ci    int32_t pid = 0;
166ba5c3796Sopenharmony_ci    int32_t uid = 0;
167ba5c3796Sopenharmony_ci    if (!data.ReadInt32(pid) || !data.ReadInt32(uid)) {
168ba5c3796Sopenharmony_ci        HILOGE("read params failed");
169ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
170ba5c3796Sopenharmony_ci    }
171ba5c3796Sopenharmony_ci    HILOGI("called, pid=%{public}d, uid=%{public}d", pid, uid);
172ba5c3796Sopenharmony_ci
173ba5c3796Sopenharmony_ci    int32_t ret = DeregisterActiveApps(pid, uid);
174ba5c3796Sopenharmony_ci    if (!reply.WriteInt32(ret)) {
175ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
176ba5c3796Sopenharmony_ci    }
177ba5c3796Sopenharmony_ci    return ret;
178ba5c3796Sopenharmony_ci}
179ba5c3796Sopenharmony_ci
180ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleSubscribeAppState(MessageParcel &data, MessageParcel &reply)
181ba5c3796Sopenharmony_ci{
182ba5c3796Sopenharmony_ci    HILOGI("called");
183ba5c3796Sopenharmony_ci    sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
184ba5c3796Sopenharmony_ci    if (subscriber == nullptr) {
185ba5c3796Sopenharmony_ci        HILOGE("read params failed");
186ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
187ba5c3796Sopenharmony_ci    }
188ba5c3796Sopenharmony_ci    int32_t ret = SubscribeAppState(iface_cast<IAppStateSubscriber>(subscriber));
189ba5c3796Sopenharmony_ci    if (!reply.WriteInt32(ret)) {
190ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
191ba5c3796Sopenharmony_ci    }
192ba5c3796Sopenharmony_ci    return ret;
193ba5c3796Sopenharmony_ci}
194ba5c3796Sopenharmony_ci
195ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleUnsubscribeAppState(MessageParcel &data, MessageParcel &reply)
196ba5c3796Sopenharmony_ci{
197ba5c3796Sopenharmony_ci    HILOGI("called");
198ba5c3796Sopenharmony_ci    sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
199ba5c3796Sopenharmony_ci    if (subscriber == nullptr) {
200ba5c3796Sopenharmony_ci        HILOGE("read params failed");
201ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
202ba5c3796Sopenharmony_ci    }
203ba5c3796Sopenharmony_ci
204ba5c3796Sopenharmony_ci    int32_t ret = UnsubscribeAppState(iface_cast<IAppStateSubscriber>(subscriber));
205ba5c3796Sopenharmony_ci    if (!reply.WriteInt32(ret)) {
206ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
207ba5c3796Sopenharmony_ci    }
208ba5c3796Sopenharmony_ci    return ret;
209ba5c3796Sopenharmony_ci}
210ba5c3796Sopenharmony_ci
211ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleGetAvailableMemory(MessageParcel &data, MessageParcel &reply)
212ba5c3796Sopenharmony_ci{
213ba5c3796Sopenharmony_ci    HILOGI("called");
214ba5c3796Sopenharmony_ci    int32_t memSize = 0;
215ba5c3796Sopenharmony_ci    int32_t ret = GetAvailableMemory(memSize);
216ba5c3796Sopenharmony_ci    if (!reply.WriteInt32(memSize)) {
217ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
218ba5c3796Sopenharmony_ci    }
219ba5c3796Sopenharmony_ci    return ret;
220ba5c3796Sopenharmony_ci}
221ba5c3796Sopenharmony_ci
222ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleGetTotalMemory(MessageParcel &data, MessageParcel &reply)
223ba5c3796Sopenharmony_ci{
224ba5c3796Sopenharmony_ci    HILOGI("called");
225ba5c3796Sopenharmony_ci    int32_t memSize = 0;
226ba5c3796Sopenharmony_ci    int32_t ret = GetTotalMemory(memSize);
227ba5c3796Sopenharmony_ci    if (!reply.WriteInt32(memSize)) {
228ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
229ba5c3796Sopenharmony_ci    }
230ba5c3796Sopenharmony_ci    return ret;
231ba5c3796Sopenharmony_ci}
232ba5c3796Sopenharmony_ci#endif // USE_PURGEABLE_MEMORY
233ba5c3796Sopenharmony_ci
234ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleOnWindowVisibilityChanged(MessageParcel &data, MessageParcel &reply)
235ba5c3796Sopenharmony_ci{
236ba5c3796Sopenharmony_ci    HILOGD("called");
237ba5c3796Sopenharmony_ci    std::vector<sptr<MemMgrWindowInfo>> infos;
238ba5c3796Sopenharmony_ci    uint32_t len = data.ReadUint32();
239ba5c3796Sopenharmony_ci    if (len < 0 || len > MAX_PARCEL_SIZE) {
240ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
241ba5c3796Sopenharmony_ci    }
242ba5c3796Sopenharmony_ci
243ba5c3796Sopenharmony_ci    size_t readAbleSize = data.GetReadableBytes();
244ba5c3796Sopenharmony_ci    size_t size = static_cast<size_t>(len);
245ba5c3796Sopenharmony_ci    if ((size > readAbleSize) || (size > infos.max_size())) {
246ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
247ba5c3796Sopenharmony_ci    }
248ba5c3796Sopenharmony_ci    infos.resize(size);
249ba5c3796Sopenharmony_ci    if (infos.size() < size) {
250ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
251ba5c3796Sopenharmony_ci    }
252ba5c3796Sopenharmony_ci    size_t minDesireCapacity = sizeof(int32_t);
253ba5c3796Sopenharmony_ci    for (size_t i = 0; i < size; i++) {
254ba5c3796Sopenharmony_ci        readAbleSize = data.GetReadableBytes();
255ba5c3796Sopenharmony_ci        if (minDesireCapacity > readAbleSize) {
256ba5c3796Sopenharmony_ci            return IPC_STUB_ERR;
257ba5c3796Sopenharmony_ci        }
258ba5c3796Sopenharmony_ci        infos[i] = data.ReadParcelable<MemMgrWindowInfo>();
259ba5c3796Sopenharmony_ci    }
260ba5c3796Sopenharmony_ci
261ba5c3796Sopenharmony_ci    int32_t ret = OnWindowVisibilityChanged(infos);
262ba5c3796Sopenharmony_ci    if (!reply.WriteInt32(ret)) {
263ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
264ba5c3796Sopenharmony_ci    }
265ba5c3796Sopenharmony_ci    return ret;
266ba5c3796Sopenharmony_ci}
267ba5c3796Sopenharmony_ci
268ba5c3796Sopenharmony_cibool MemMgrStub::IsCameraServiceCalling()
269ba5c3796Sopenharmony_ci{
270ba5c3796Sopenharmony_ci    int32_t callingUid = IPCSkeleton::GetCallingUid();
271ba5c3796Sopenharmony_ci    return callingUid == CAMERA_SERVICE_UID;
272ba5c3796Sopenharmony_ci}
273ba5c3796Sopenharmony_ci
274ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleGetReclaimPriorityByPid(MessageParcel &data, MessageParcel &reply)
275ba5c3796Sopenharmony_ci{
276ba5c3796Sopenharmony_ci    HILOGD("called");
277ba5c3796Sopenharmony_ci
278ba5c3796Sopenharmony_ci    if (!IsCameraServiceCalling()) {
279ba5c3796Sopenharmony_ci        HILOGE("calling process has no permission, call failled");
280ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
281ba5c3796Sopenharmony_ci    }
282ba5c3796Sopenharmony_ci    int32_t pid = data.ReadUint32();
283ba5c3796Sopenharmony_ci    int32_t priority = RECLAIM_PRIORITY_UNKNOWN + 1;
284ba5c3796Sopenharmony_ci    int32_t ret = GetReclaimPriorityByPid(pid, priority);
285ba5c3796Sopenharmony_ci
286ba5c3796Sopenharmony_ci    if (!reply.WriteInt32(priority)) {
287ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
288ba5c3796Sopenharmony_ci    }
289ba5c3796Sopenharmony_ci    return ret;
290ba5c3796Sopenharmony_ci}
291ba5c3796Sopenharmony_ci
292ba5c3796Sopenharmony_cibool MemMgrStub::CheckCallingToken()
293ba5c3796Sopenharmony_ci{
294ba5c3796Sopenharmony_ci    Security::AccessToken::AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID();
295ba5c3796Sopenharmony_ci    auto tokenFlag = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
296ba5c3796Sopenharmony_ci    if (tokenFlag == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
297ba5c3796Sopenharmony_ci        tokenFlag == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL) {
298ba5c3796Sopenharmony_ci            return true;
299ba5c3796Sopenharmony_ci    }
300ba5c3796Sopenharmony_ci    return false;
301ba5c3796Sopenharmony_ci}
302ba5c3796Sopenharmony_ci
303ba5c3796Sopenharmony_cibool IsFoundationCalling()
304ba5c3796Sopenharmony_ci{
305ba5c3796Sopenharmony_ci    return IPCSkeleton::GetCallingUid() == FOUNDATION_UID;
306ba5c3796Sopenharmony_ci}
307ba5c3796Sopenharmony_ci
308ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleNotifyProcessStateChangedSync(MessageParcel &data, MessageParcel &reply)
309ba5c3796Sopenharmony_ci{
310ba5c3796Sopenharmony_ci    HILOGD("called");
311ba5c3796Sopenharmony_ci
312ba5c3796Sopenharmony_ci    if (!IsFoundationCalling()) {
313ba5c3796Sopenharmony_ci        HILOGE("calling process has no permission, call failed");
314ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
315ba5c3796Sopenharmony_ci    }
316ba5c3796Sopenharmony_ci    std::unique_ptr<MemMgrProcessStateInfo> processStateInfo(data.ReadParcelable<MemMgrProcessStateInfo>());
317ba5c3796Sopenharmony_ci    if (processStateInfo == nullptr) {
318ba5c3796Sopenharmony_ci        HILOGE("ReadParcelable<MemMgrProcessStateInfo> failed");
319ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
320ba5c3796Sopenharmony_ci    }
321ba5c3796Sopenharmony_ci
322ba5c3796Sopenharmony_ci    int32_t ret = NotifyProcessStateChangedSync(*processStateInfo);
323ba5c3796Sopenharmony_ci    if (!reply.WriteInt32(ret)) {
324ba5c3796Sopenharmony_ci        HILOGE("reply write failed");
325ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
326ba5c3796Sopenharmony_ci    }
327ba5c3796Sopenharmony_ci    return ret;
328ba5c3796Sopenharmony_ci}
329ba5c3796Sopenharmony_ci
330ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleNotifyProcessStateChangedAsync(MessageParcel &data, MessageParcel &reply)
331ba5c3796Sopenharmony_ci{
332ba5c3796Sopenharmony_ci    HILOGD("called");
333ba5c3796Sopenharmony_ci
334ba5c3796Sopenharmony_ci    if (!IsFoundationCalling()) {
335ba5c3796Sopenharmony_ci        HILOGE("calling process has no permission, call failed");
336ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
337ba5c3796Sopenharmony_ci    }
338ba5c3796Sopenharmony_ci    std::unique_ptr<MemMgrProcessStateInfo> processStateInfo(data.ReadParcelable<MemMgrProcessStateInfo>());
339ba5c3796Sopenharmony_ci    if (processStateInfo == nullptr) {
340ba5c3796Sopenharmony_ci        HILOGE("ReadParcelable<MemMgrProcessStateInfo> failed");
341ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
342ba5c3796Sopenharmony_ci    }
343ba5c3796Sopenharmony_ci
344ba5c3796Sopenharmony_ci    int32_t ret = NotifyProcessStateChangedAsync(*processStateInfo);
345ba5c3796Sopenharmony_ci    if (!reply.WriteInt32(ret)) {
346ba5c3796Sopenharmony_ci        HILOGE("reply write failed");
347ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
348ba5c3796Sopenharmony_ci    }
349ba5c3796Sopenharmony_ci    return ret;
350ba5c3796Sopenharmony_ci}
351ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleNotifyProcessStatus(MessageParcel &data, MessageParcel &reply)
352ba5c3796Sopenharmony_ci{
353ba5c3796Sopenharmony_ci    HILOGD("called");
354ba5c3796Sopenharmony_ci
355ba5c3796Sopenharmony_ci    if (!CheckCallingToken()) {
356ba5c3796Sopenharmony_ci        HILOGE("calling process has no permission, call failed");
357ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
358ba5c3796Sopenharmony_ci    }
359ba5c3796Sopenharmony_ci    int32_t pid = 0;
360ba5c3796Sopenharmony_ci    int32_t type = 0;
361ba5c3796Sopenharmony_ci    int32_t status = 0;
362ba5c3796Sopenharmony_ci    int32_t saId = -1;
363ba5c3796Sopenharmony_ci    if (!data.ReadInt32(pid) || !data.ReadInt32(type) || !data.ReadInt32(status) || !data.ReadInt32(saId)) {
364ba5c3796Sopenharmony_ci        HILOGE("read params failed");
365ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
366ba5c3796Sopenharmony_ci    }
367ba5c3796Sopenharmony_ci
368ba5c3796Sopenharmony_ci    NotifyProcessStatus(pid, type, status, saId);
369ba5c3796Sopenharmony_ci
370ba5c3796Sopenharmony_ci    return 0;
371ba5c3796Sopenharmony_ci}
372ba5c3796Sopenharmony_ci
373ba5c3796Sopenharmony_ciint32_t MemMgrStub::HandleSetCritical(MessageParcel &data, MessageParcel &reply)
374ba5c3796Sopenharmony_ci{
375ba5c3796Sopenharmony_ci    HILOGD("called");
376ba5c3796Sopenharmony_ci
377ba5c3796Sopenharmony_ci    if (!CheckCallingToken()) {
378ba5c3796Sopenharmony_ci        HILOGE("calling process has no permission, call failed");
379ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
380ba5c3796Sopenharmony_ci    }
381ba5c3796Sopenharmony_ci    int32_t pid = 0;
382ba5c3796Sopenharmony_ci    bool critical = false;
383ba5c3796Sopenharmony_ci    int32_t saId = -1;
384ba5c3796Sopenharmony_ci    if (!data.ReadInt32(pid) || !data.ReadBool(critical) || !data.ReadInt32(saId)) {
385ba5c3796Sopenharmony_ci        HILOGE("read params failed");
386ba5c3796Sopenharmony_ci        return IPC_STUB_ERR;
387ba5c3796Sopenharmony_ci    }
388ba5c3796Sopenharmony_ci    if (IPCSkeleton::GetCallingPid() != pid) {
389ba5c3796Sopenharmony_ci        return 0;
390ba5c3796Sopenharmony_ci    }
391ba5c3796Sopenharmony_ci
392ba5c3796Sopenharmony_ci    SetCritical(pid, critical, saId);
393ba5c3796Sopenharmony_ci
394ba5c3796Sopenharmony_ci    return 0;
395ba5c3796Sopenharmony_ci}
396ba5c3796Sopenharmony_ci} // namespace Memory
397ba5c3796Sopenharmony_ci} // namespace OHOS
398