• 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_stub.h"
17 #include "memmgr_log.h"
18 #include "memmgr_config_manager.h"
19 #include "kernel_interface.h"
20 #include "low_memory_killer.h"
21 #include "parcel.h"
22 
23 namespace OHOS {
24 namespace Memory {
25 namespace {
26     const std::string TAG = "MemMgrStub";
27     constexpr int MAX_PARCEL_SIZE = 100000;
28 }
29 
MemMgrStub()30 MemMgrStub::MemMgrStub()
31 {
32     memberFuncMap_[static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_GET_BUNDLE_PRIORITY_LIST)] =
33         &MemMgrStub::HandleGetBunldePriorityList;
34     memberFuncMap_[static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_NOTIFY_DIST_DEV_STATUS)] =
35         &MemMgrStub::HandleNotifyDistDevStatus;
36     memberFuncMap_[static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_GET_KILL_LEVEL_OF_LMKD)] =
37         &MemMgrStub::HandleGetKillLevelOfLmkd;
38 #ifdef USE_PURGEABLE_MEMORY
39     memberFuncMap_[static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_REGISTER_ACTIVE_APPS)] =
40         &MemMgrStub::HandleRegisterActiveApps;
41     memberFuncMap_[static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_DEREGISTER_ACTIVE_APPS)] =
42         &MemMgrStub::HandleDeregisterActiveApps;
43     memberFuncMap_[static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_SUBSCRIBE_APP_STATE)] =
44         &MemMgrStub::HandleSubscribeAppState;
45     memberFuncMap_[static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_UNSUBSCRIBE_APP_STATE)] =
46         &MemMgrStub::HandleUnsubscribeAppState;
47     memberFuncMap_[static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_GET_AVAILABLE_MEMORY)] =
48         &MemMgrStub::HandleGetAvailableMemory;
49     memberFuncMap_[static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_GET_TOTAL_MEMORY)] =
50         &MemMgrStub::HandleGetTotalMemory;
51 #endif
52     memberFuncMap_[static_cast<uint32_t>(MemMgrInterfaceCode::MEM_MGR_ON_WINDOW_VISIBILITY_CHANGED)] =
53         &MemMgrStub::HandleOnWindowVisibilityChanged;
54 }
55 
~MemMgrStub()56 MemMgrStub::~MemMgrStub()
57 {
58     memberFuncMap_.clear();
59 }
60 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)61 int MemMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
62 {
63     HILOGI("MemMgrStub::OnReceived, code = %{public}d, flags= %{public}d.", code, option.GetFlags());
64     std::u16string descriptor = MemMgrStub::GetDescriptor();
65     std::u16string remoteDescriptor = data.ReadInterfaceToken();
66     if (descriptor != remoteDescriptor) {
67         HILOGE("local descriptor is not equal to remote");
68         return ERR_INVALID_STATE;
69     }
70 
71     auto itFunc = memberFuncMap_.find(code);
72     if (itFunc != memberFuncMap_.end()) {
73         auto memberFunc = itFunc->second;
74         if (memberFunc != nullptr) {
75             return (this->*memberFunc)(data, reply);
76         }
77     }
78     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
79 }
80 
HandleGetBunldePriorityList(MessageParcel & data,MessageParcel & reply)81 int32_t MemMgrStub::HandleGetBunldePriorityList(MessageParcel &data, MessageParcel &reply)
82 {
83     HILOGI("called");
84     std::shared_ptr<BundlePriorityList> list
85         = std::shared_ptr<BundlePriorityList>(data.ReadParcelable<BundlePriorityList>());
86 
87     if (!list) {
88         HILOGE("BundlePriorityList ReadParcelable failed");
89         return -1;
90     }
91     int32_t ret = GetBundlePriorityList(*list);
92     reply.WriteParcelable(list.get());
93     return ret;
94 }
95 
HandleNotifyDistDevStatus(MessageParcel & data,MessageParcel & reply)96 int32_t MemMgrStub::HandleNotifyDistDevStatus(MessageParcel &data, MessageParcel &reply)
97 {
98     HILOGI("called");
99     int32_t pid = 0;
100     int32_t uid = 0;
101     std::string name;
102     bool connected;
103     if (!data.ReadInt32(pid) || !data.ReadInt32(uid) || !data.ReadString(name) || !data.ReadBool(connected)) {
104         HILOGE("read params failed");
105         return IPC_STUB_ERR;
106     }
107     HILOGI("called, pid=%{public}d, uid=%{public}d, name=%{public}s, connected=%{public}d", pid, uid, name.c_str(),
108         connected);
109 
110     int32_t ret = NotifyDistDevStatus(pid, uid, name, connected);
111     if (!reply.WriteInt32(ret)) {
112         return IPC_STUB_ERR;
113     }
114     return ret;
115 }
116 
HandleGetKillLevelOfLmkd(MessageParcel & data,MessageParcel & reply)117 int32_t MemMgrStub::HandleGetKillLevelOfLmkd(MessageParcel &data, MessageParcel &reply)
118 {
119     HILOGI("called");
120     int32_t killLevel = LowMemoryKiller::GetInstance().GetKillLevel();
121     if (!reply.WriteInt32(killLevel)) {
122         return IPC_STUB_ERR;
123     }
124     return 0;
125 }
126 
127 #ifdef USE_PURGEABLE_MEMORY
HandleRegisterActiveApps(MessageParcel & data,MessageParcel & reply)128 int32_t MemMgrStub::HandleRegisterActiveApps(MessageParcel &data, MessageParcel &reply)
129 {
130     HILOGI("called");
131     int32_t pid = 0;
132     int32_t uid = 0;
133     if (!data.ReadInt32(pid) || !data.ReadInt32(uid)) {
134         HILOGE("read params failed");
135         return IPC_STUB_ERR;
136     }
137     HILOGI("called, pid=%{public}d, uid=%{public}d", pid, uid);
138 
139     int32_t ret = RegisterActiveApps(pid, uid);
140     if (!reply.WriteInt32(ret)) {
141         return IPC_STUB_ERR;
142     }
143     return ret;
144 }
145 
HandleDeregisterActiveApps(MessageParcel & data,MessageParcel & reply)146 int32_t MemMgrStub::HandleDeregisterActiveApps(MessageParcel &data, MessageParcel &reply)
147 {
148     HILOGI("called");
149     int32_t pid = 0;
150     int32_t uid = 0;
151     if (!data.ReadInt32(pid) || !data.ReadInt32(uid)) {
152         HILOGE("read params failed");
153         return IPC_STUB_ERR;
154     }
155     HILOGI("called, pid=%{public}d, uid=%{public}d", pid, uid);
156 
157     int32_t ret = DeregisterActiveApps(pid, uid);
158     if (!reply.WriteInt32(ret)) {
159         return IPC_STUB_ERR;
160     }
161     return ret;
162 }
163 
HandleSubscribeAppState(MessageParcel & data,MessageParcel & reply)164 int32_t MemMgrStub::HandleSubscribeAppState(MessageParcel &data, MessageParcel &reply)
165 {
166     HILOGI("called");
167     sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
168     if (subscriber == nullptr) {
169         HILOGE("read params failed");
170         return IPC_STUB_ERR;
171     }
172     int32_t ret = SubscribeAppState(iface_cast<IAppStateSubscriber>(subscriber));
173     if (!reply.WriteInt32(ret)) {
174         return IPC_STUB_ERR;
175     }
176     return ret;
177 }
178 
HandleUnsubscribeAppState(MessageParcel & data,MessageParcel & reply)179 int32_t MemMgrStub::HandleUnsubscribeAppState(MessageParcel &data, MessageParcel &reply)
180 {
181     HILOGI("called");
182     sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
183     if (subscriber == nullptr) {
184         HILOGE("read params failed");
185         return IPC_STUB_ERR;
186     }
187 
188     int32_t ret = UnsubscribeAppState(iface_cast<IAppStateSubscriber>(subscriber));
189     if (!reply.WriteInt32(ret)) {
190         return IPC_STUB_ERR;
191     }
192     return ret;
193 }
194 
HandleGetAvailableMemory(MessageParcel & data,MessageParcel & reply)195 int32_t MemMgrStub::HandleGetAvailableMemory(MessageParcel &data, MessageParcel &reply)
196 {
197     HILOGI("called");
198     int32_t memSize = 0;
199     int32_t ret = GetAvailableMemory(memSize);
200     if (!reply.WriteInt32(memSize)) {
201         return IPC_STUB_ERR;
202     }
203     return ret;
204 }
205 
HandleGetTotalMemory(MessageParcel & data,MessageParcel & reply)206 int32_t MemMgrStub::HandleGetTotalMemory(MessageParcel &data, MessageParcel &reply)
207 {
208     HILOGI("called");
209     int32_t memSize = 0;
210     int32_t ret = GetTotalMemory(memSize);
211     if (!reply.WriteInt32(memSize)) {
212         return IPC_STUB_ERR;
213     }
214     return ret;
215 }
216 #endif // USE_PURGEABLE_MEMORY
217 
HandleOnWindowVisibilityChanged(MessageParcel & data,MessageParcel & reply)218 int32_t MemMgrStub::HandleOnWindowVisibilityChanged(MessageParcel &data, MessageParcel &reply)
219 {
220     HILOGD("called");
221     std::vector<sptr<MemMgrWindowInfo>> infos;
222     uint32_t len = data.ReadUint32();
223     if (len < 0 || len > MAX_PARCEL_SIZE) {
224         return IPC_STUB_ERR;
225     }
226 
227     size_t readAbleSize = data.GetReadableBytes();
228     size_t size = static_cast<size_t>(len);
229     if ((size > readAbleSize) || (size > infos.max_size())) {
230         return IPC_STUB_ERR;
231     }
232     infos.resize(size);
233     if (infos.size() < size) {
234         return IPC_STUB_ERR;
235     }
236     size_t minDesireCapacity = sizeof(int32_t);
237     for (size_t i = 0; i < size; i++) {
238         readAbleSize = data.GetReadableBytes();
239         if (minDesireCapacity > readAbleSize) {
240             return IPC_STUB_ERR;
241         }
242         infos[i] = data.ReadParcelable<MemMgrWindowInfo>();
243     }
244 
245     int32_t ret = OnWindowVisibilityChanged(infos);
246     if (!reply.WriteInt32(ret)) {
247         return IPC_STUB_ERR;
248     }
249     return ret;
250 }
251 } // namespace Memory
252 } // namespace OHOS
253