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 "memmgr_log.h"
17 #include "app_state_subscriber_stub.h"
18
19 namespace OHOS {
20 namespace Memory {
21 namespace {
22 const std::string TAG = "AppStateSubscriberStub";
23 }
24
AppStateSubscriberStub()25 AppStateSubscriberStub::AppStateSubscriberStub() {}
~AppStateSubscriberStub()26 AppStateSubscriberStub::~AppStateSubscriberStub() {}
27
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)28 int32_t AppStateSubscriberStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
29 MessageOption &option)
30 {
31 std::u16string descriptor = AppStateSubscriberStub::GetDescriptor();
32 std::u16string remoteDescriptor = data.ReadInterfaceToken();
33 if (descriptor != remoteDescriptor) {
34 HILOGE("local descriptor not match remote");
35 return -1;
36 }
37 HILOGI("get remote request code: %{public}d", code);
38 return OnRemoteRequestInner(code, data, reply, option);
39 }
40
OnRemoteRequestInner(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int32_t AppStateSubscriberStub::OnRemoteRequestInner(uint32_t code, MessageParcel &data, MessageParcel &reply,
42 MessageOption &option)
43 {
44 switch (code) {
45 case static_cast<uint32_t>(AppStateSubscriberInterfaceCode::ON_CONNECTED): {
46 return HandleOnConnected();
47 }
48 case static_cast<uint32_t>(AppStateSubscriberInterfaceCode::ON_DISCONNECTED): {
49 return HandleOnDisconnected();
50 }
51 case static_cast<uint32_t>(AppStateSubscriberInterfaceCode::ON_APP_STATE_CHANGED): {
52 return HandleOnAppStateChanged(data);
53 }
54 case static_cast<uint32_t>(AppStateSubscriberInterfaceCode::ON_TRIM): {
55 return HandleOnTrim(data);
56 }
57 case static_cast<uint32_t>(AppStateSubscriberInterfaceCode::FORCE_RECLAIM): {
58 return HandleForceReclaim(data);
59 }
60 default: {
61 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
62 }
63 }
64 }
65
HandleOnConnected()66 int32_t AppStateSubscriberStub::HandleOnConnected()
67 {
68 OnConnected();
69 return 0;
70 }
71
HandleOnDisconnected()72 int32_t AppStateSubscriberStub::HandleOnDisconnected()
73 {
74 OnDisconnected();
75 return 0;
76 }
77
HandleOnAppStateChanged(MessageParcel & data)78 int32_t AppStateSubscriberStub::HandleOnAppStateChanged(MessageParcel &data)
79 {
80 int32_t pid;
81 int32_t uid;
82 int32_t state;
83 if (!data.ReadInt32(pid) || !data.ReadInt32(uid) || !data.ReadInt32(state)) {
84 HILOGE("read pid, uid or new app state failed");
85 return -1;
86 }
87 OnAppStateChanged(pid, uid, state);
88 return 0;
89 }
90
HandleOnTrim(MessageParcel & data)91 int32_t AppStateSubscriberStub::HandleOnTrim(MessageParcel &data)
92 {
93 int32_t levelTemp;
94 if (!data.ReadInt32(levelTemp)) {
95 HILOGE("read memory level of system failed");
96 return -1;
97 }
98 SystemMemoryLevel level = static_cast<SystemMemoryLevel>(levelTemp);
99 OnTrim(level);
100 return 0;
101 }
102
HandleForceReclaim(MessageParcel & data)103 int32_t AppStateSubscriberStub::HandleForceReclaim(MessageParcel &data)
104 {
105 int32_t pid;
106 int32_t uid;
107 if (!data.ReadInt32(pid) || !data.ReadInt32(uid)) {
108 HILOGE("read pid or uid failed");
109 return -1;
110 }
111 ForceReclaim(pid, uid);
112 return 0;
113 }
114 } // namespace Memory
115 } // namespace OHOS