• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "app_mgr_stub.h"
17 
18 #include "ipc_skeleton.h"
19 #include "ipc_types.h"
20 #include "iremote_object.h"
21 
22 #include "ability_info.h"
23 #include "app_mgr_proxy.h"
24 #include "app_scheduler_interface.h"
25 #include "appexecfwk_errors.h"
26 #include "bytrace.h"
27 #include "hilog_wrapper.h"
28 #include "iapp_state_callback.h"
29 #include "want.h"
30 #include "bundle_info.h"
31 
32 namespace OHOS {
33 namespace AppExecFwk {
AppMgrStub()34 AppMgrStub::AppMgrStub()
35 {
36     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::APP_ATTACH_APPLICATION)] =
37         &AppMgrStub::HandleAttachApplication;
38     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::APP_APPLICATION_FOREGROUNDED)] =
39         &AppMgrStub::HandleApplicationForegrounded;
40     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::APP_APPLICATION_BACKGROUNDED)] =
41         &AppMgrStub::HandleApplicationBackgrounded;
42     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::APP_APPLICATION_TERMINATED)] =
43         &AppMgrStub::HandleApplicationTerminated;
44     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::APP_CHECK_PERMISSION)] =
45         &AppMgrStub::HandleCheckPermission;
46     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::APP_ABILITY_CLEANED)] =
47         &AppMgrStub::HandleAbilityCleaned;
48     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::APP_GET_MGR_INSTANCE)] = &AppMgrStub::HandleGetAmsMgr;
49     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::APP_CLEAR_UP_APPLICATION_DATA)] =
50         &AppMgrStub::HandleClearUpApplicationData;
51     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::APP_GET_ALL_RUNNING_PROCESSES)] =
52         &AppMgrStub::HandleGetAllRunningProcesses;
53     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::APP_GET_RUNNING_PROCESSES_BY_USER_ID)] =
54         &AppMgrStub::HandleGetProcessRunningInfosByUserId;
55     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::APP_GET_SYSTEM_MEMORY_ATTR)] =
56         &AppMgrStub::HandleGetSystemMemoryAttr;
57     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::APP_ADD_ABILITY_STAGE_INFO_DONE)] =
58         &AppMgrStub::HandleAddAbilityStageDone;
59     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::STARTUP_RESIDENT_PROCESS)] =
60         &AppMgrStub::HandleStartupResidentProcess;
61     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::REGISTER_APPLICATION_STATE_OBSERVER)] =
62         &AppMgrStub::HandleRegisterApplicationStateObserver;
63     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::UNREGISTER_APPLICATION_STATE_OBSERVER)] =
64         &AppMgrStub::HandleUnregisterApplicationStateObserver;
65     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::GET_FOREGROUND_APPLICATIONS)] =
66         &AppMgrStub::HandleGetForegroundApplications;
67     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::START_USER_TEST_PROCESS)] =
68         &AppMgrStub::HandleStartUserTestProcess;
69     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::FINISH_USER_TEST)] =
70         &AppMgrStub::HandleFinishUserTest;
71     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::SCHEDULE_ACCEPT_WANT_DONE)] =
72         &AppMgrStub::HandleScheduleAcceptWantDone;
73     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::APP_GET_ABILITY_RECORDS_BY_PROCESS_ID)] =
74         &AppMgrStub::HandleGetAbilityRecordsByProcessID;
75     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::START_RENDER_PROCESS)] =
76         &AppMgrStub::HandleStartRenderProcess;
77     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::ATTACH_RENDER_PROCESS)] =
78         &AppMgrStub::HandleAttachRenderProcess;
79     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::GET_RENDER_PROCESS_TERMINATION_STATUS)] =
80         &AppMgrStub::HandleGetRenderProcessTerminationStatus;
81     memberFuncMap_[static_cast<uint32_t>(IAppMgr::Message::POST_ANR_TASK_BY_PID)] =
82         &AppMgrStub::HandlePostANRTaskByProcessID;
83 }
84 
~AppMgrStub()85 AppMgrStub::~AppMgrStub()
86 {
87     memberFuncMap_.clear();
88 }
89 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)90 int AppMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
91 {
92     HILOG_INFO("AppMgrStub::OnReceived, code = %{public}u, flags= %{public}d.", code, option.GetFlags());
93     std::u16string descriptor = AppMgrStub::GetDescriptor();
94     std::u16string remoteDescriptor = data.ReadInterfaceToken();
95     if (descriptor != remoteDescriptor) {
96         HILOG_ERROR("local descriptor is not equal to remote");
97         return ERR_INVALID_STATE;
98     }
99 
100     auto itFunc = memberFuncMap_.find(code);
101     if (itFunc != memberFuncMap_.end()) {
102         auto memberFunc = itFunc->second;
103         if (memberFunc != nullptr) {
104             return (this->*memberFunc)(data, reply);
105         }
106     }
107     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
108 }
109 
HandleAttachApplication(MessageParcel & data,MessageParcel & reply)110 int32_t AppMgrStub::HandleAttachApplication(MessageParcel &data, MessageParcel &reply)
111 {
112     BYTRACE(BYTRACE_TAG_APP);
113     sptr<IRemoteObject> client = data.ReadRemoteObject();
114     AttachApplication(client);
115     return NO_ERROR;
116 }
117 
HandleApplicationForegrounded(MessageParcel & data,MessageParcel & reply)118 int32_t AppMgrStub::HandleApplicationForegrounded(MessageParcel &data, MessageParcel &reply)
119 {
120     BYTRACE(BYTRACE_TAG_APP);
121     ApplicationForegrounded(data.ReadInt32());
122     return NO_ERROR;
123 }
124 
HandleApplicationBackgrounded(MessageParcel & data,MessageParcel & reply)125 int32_t AppMgrStub::HandleApplicationBackgrounded(MessageParcel &data, MessageParcel &reply)
126 {
127     BYTRACE(BYTRACE_TAG_APP);
128     ApplicationBackgrounded(data.ReadInt32());
129     return NO_ERROR;
130 }
131 
HandleApplicationTerminated(MessageParcel & data,MessageParcel & reply)132 int32_t AppMgrStub::HandleApplicationTerminated(MessageParcel &data, MessageParcel &reply)
133 {
134     BYTRACE(BYTRACE_TAG_APP);
135     ApplicationTerminated(data.ReadInt32());
136     return NO_ERROR;
137 }
138 
HandleCheckPermission(MessageParcel & data,MessageParcel & reply)139 int32_t AppMgrStub::HandleCheckPermission(MessageParcel &data, MessageParcel &reply)
140 {
141     BYTRACE(BYTRACE_TAG_APP);
142     int32_t recordId = data.ReadInt32();
143     std::string permission = data.ReadString();
144     int32_t result = CheckPermission(recordId, permission);
145     reply.WriteInt32(result);
146     return NO_ERROR;
147 }
148 
HandleAbilityCleaned(MessageParcel & data,MessageParcel & reply)149 int32_t AppMgrStub::HandleAbilityCleaned(MessageParcel &data, MessageParcel &reply)
150 {
151     BYTRACE(BYTRACE_TAG_APP);
152     sptr<IRemoteObject> token = data.ReadRemoteObject();
153     AbilityCleaned(token);
154     return NO_ERROR;
155 }
156 
HandleGetAmsMgr(MessageParcel & data,MessageParcel & reply)157 int32_t AppMgrStub::HandleGetAmsMgr(MessageParcel &data, MessageParcel &reply)
158 {
159     BYTRACE(BYTRACE_TAG_APP);
160     int32_t result = NO_ERROR;
161     sptr<IAmsMgr> amsMgr = GetAmsMgr();
162     if (!amsMgr) {
163         HILOG_ERROR("abilitymgr instance is nullptr");
164         result = ERR_NO_INIT;
165     } else {
166         if (!reply.WriteRemoteObject(amsMgr->AsObject())) {
167             HILOG_ERROR("failed to reply abilitymgr instance to client, for write parcel error");
168             result = ERR_APPEXECFWK_PARCEL_ERROR;
169         }
170     }
171     reply.WriteInt32(result);
172     return NO_ERROR;
173 }
174 
HandleClearUpApplicationData(MessageParcel & data,MessageParcel & reply)175 int32_t AppMgrStub::HandleClearUpApplicationData(MessageParcel &data, MessageParcel &reply)
176 {
177     BYTRACE(BYTRACE_TAG_APP);
178     std::string bundleName = data.ReadString();
179     int32_t result = ClearUpApplicationData(bundleName);
180     reply.WriteInt32(result);
181     return NO_ERROR;
182 }
183 
HandleGetAllRunningProcesses(MessageParcel & data,MessageParcel & reply)184 int32_t AppMgrStub::HandleGetAllRunningProcesses(MessageParcel &data, MessageParcel &reply)
185 {
186     BYTRACE(BYTRACE_TAG_APP);
187     std::vector<RunningProcessInfo> info;
188     auto result = GetAllRunningProcesses(info);
189     reply.WriteInt32(info.size());
190     for (auto &it : info) {
191         if (!reply.WriteParcelable(&it)) {
192             return ERR_INVALID_VALUE;
193         }
194     }
195     if (!reply.WriteInt32(result)) {
196         return ERR_INVALID_VALUE;
197     }
198     return NO_ERROR;
199 }
200 
HandleGetProcessRunningInfosByUserId(MessageParcel & data,MessageParcel & reply)201 int32_t AppMgrStub::HandleGetProcessRunningInfosByUserId(MessageParcel &data, MessageParcel &reply)
202 {
203     BYTRACE(BYTRACE_TAG_APP);
204     int32_t userId = data.ReadInt32();
205     std::vector<RunningProcessInfo> info;
206     auto result = GetProcessRunningInfosByUserId(info, userId);
207     reply.WriteInt32(info.size());
208     for (auto &it : info) {
209         if (!reply.WriteParcelable(&it)) {
210             return ERR_INVALID_VALUE;
211         }
212     }
213     if (!reply.WriteInt32(result)) {
214         return ERR_INVALID_VALUE;
215     }
216     return NO_ERROR;
217 }
218 
HandleGetSystemMemoryAttr(MessageParcel & data,MessageParcel & reply)219 int32_t AppMgrStub::HandleGetSystemMemoryAttr(MessageParcel &data, MessageParcel &reply)
220 {
221     BYTRACE(BYTRACE_TAG_APP);
222     SystemMemoryAttr memoryInfo;
223     std::string strConfig;
224     data.ReadString(strConfig);
225     GetSystemMemoryAttr(memoryInfo, strConfig);
226     if (reply.WriteParcelable(&memoryInfo)) {
227         HILOG_ERROR("want write failed.");
228         return ERR_INVALID_VALUE;
229     }
230     return NO_ERROR;
231 }
232 
HandleAddAbilityStageDone(MessageParcel & data,MessageParcel & reply)233 int32_t AppMgrStub::HandleAddAbilityStageDone(MessageParcel &data, MessageParcel &reply)
234 {
235     int32_t recordId = data.ReadInt32();
236     AddAbilityStageDone(recordId);
237     return NO_ERROR;
238 }
239 
HandleStartupResidentProcess(MessageParcel & data,MessageParcel & reply)240 int32_t AppMgrStub::HandleStartupResidentProcess(MessageParcel &data, MessageParcel &reply)
241 {
242     BYTRACE(BYTRACE_TAG_APP);
243     std::vector<AppExecFwk::BundleInfo> bundleInfos;
244     int32_t infoSize = data.ReadInt32();
245     for (int32_t i = 0; i < infoSize; i++) {
246         std::unique_ptr<AppExecFwk::BundleInfo> bundleInfo(data.ReadParcelable<AppExecFwk::BundleInfo>());
247         if (!bundleInfo) {
248             HILOG_ERROR("Read Parcelable infos failed.");
249             return ERR_INVALID_VALUE;
250         }
251         bundleInfos.emplace_back(*bundleInfo);
252     }
253     StartupResidentProcess(bundleInfos);
254     return NO_ERROR;
255 }
256 
HandleRegisterApplicationStateObserver(MessageParcel & data,MessageParcel & reply)257 int32_t AppMgrStub::HandleRegisterApplicationStateObserver(MessageParcel &data, MessageParcel &reply)
258 {
259     auto callback = iface_cast<AppExecFwk::IApplicationStateObserver>(data.ReadRemoteObject());
260     int32_t result = RegisterApplicationStateObserver(callback);
261     reply.WriteInt32(result);
262     return NO_ERROR;
263 }
264 
HandleUnregisterApplicationStateObserver(MessageParcel & data,MessageParcel & reply)265 int32_t AppMgrStub::HandleUnregisterApplicationStateObserver(MessageParcel &data, MessageParcel &reply)
266 {
267     auto callback = iface_cast<AppExecFwk::IApplicationStateObserver>(data.ReadRemoteObject());
268     int32_t result = UnregisterApplicationStateObserver(callback);
269     reply.WriteInt32(result);
270     return NO_ERROR;
271 }
272 
273 
HandleGetForegroundApplications(MessageParcel & data,MessageParcel & reply)274 int32_t AppMgrStub::HandleGetForegroundApplications(MessageParcel &data, MessageParcel &reply)
275 {
276     std::vector<AppStateData> appStateDatas;
277     int32_t result = GetForegroundApplications(appStateDatas);
278     reply.WriteInt32(appStateDatas.size());
279     for (auto &it : appStateDatas) {
280         if (!reply.WriteParcelable(&it)) {
281             return ERR_INVALID_VALUE;
282         }
283     }
284     if (!reply.WriteInt32(result)) {
285         return ERR_INVALID_VALUE;
286     }
287     return result;
288 }
289 
HandleStartUserTestProcess(MessageParcel & data,MessageParcel & reply)290 int32_t AppMgrStub::HandleStartUserTestProcess(MessageParcel &data, MessageParcel &reply)
291 {
292     AAFwk::Want *want = data.ReadParcelable<AAFwk::Want>();
293     if (want == nullptr) {
294         HILOG_ERROR("want is nullptr");
295         return ERR_INVALID_VALUE;
296     }
297     BundleInfo *bundleInfo = data.ReadParcelable<BundleInfo>();
298     if (bundleInfo == nullptr) {
299         HILOG_ERROR("want is nullptr");
300         delete want;
301         return ERR_INVALID_VALUE;
302     }
303     auto observer = data.ReadRemoteObject();
304     int32_t userId = data.ReadInt32();
305     int32_t result = StartUserTestProcess(*want, observer, *bundleInfo, userId);
306     reply.WriteInt32(result);
307     delete want;
308     delete bundleInfo;
309     return result;
310 }
311 
HandleFinishUserTest(MessageParcel & data,MessageParcel & reply)312 int32_t AppMgrStub::HandleFinishUserTest(MessageParcel &data, MessageParcel &reply)
313 {
314     std::string msg = data.ReadString();
315     int resultCode = data.ReadInt32();
316     std::string bundleName = data.ReadString();
317     int32_t result = FinishUserTest(msg, resultCode, bundleName);
318     reply.WriteInt32(result);
319     return result;
320 }
321 
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)322 int32_t AppMgrStub::RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer)
323 {
324     return NO_ERROR;
325 }
326 
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)327 int32_t AppMgrStub::UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer)
328 {
329     return NO_ERROR;
330 }
331 
GetForegroundApplications(std::vector<AppStateData> & list)332 int32_t AppMgrStub::GetForegroundApplications(std::vector<AppStateData> &list)
333 {
334     return NO_ERROR;
335 }
336 
HandleScheduleAcceptWantDone(MessageParcel & data,MessageParcel & reply)337 int32_t AppMgrStub::HandleScheduleAcceptWantDone(MessageParcel &data, MessageParcel &reply)
338 {
339     auto recordId = data.ReadInt32();
340     AAFwk::Want *want = data.ReadParcelable<AAFwk::Want>();
341     if (want == nullptr) {
342         HILOG_ERROR("want is nullptr");
343         return ERR_INVALID_VALUE;
344     }
345     auto flag = data.ReadString();
346 
347     ScheduleAcceptWantDone(recordId, *want, flag);
348     delete want;
349     return NO_ERROR;
350 }
351 
HandleGetAbilityRecordsByProcessID(MessageParcel & data,MessageParcel & reply)352 int32_t AppMgrStub::HandleGetAbilityRecordsByProcessID(MessageParcel &data, MessageParcel &reply)
353 {
354     BYTRACE(BYTRACE_TAG_APP);
355     int32_t pid = data.ReadInt32();
356     std::vector<sptr<IRemoteObject>> tokens;
357     auto result = GetAbilityRecordsByProcessID(pid, tokens);
358     reply.WriteInt32(tokens.size());
359     for (auto &it : tokens) {
360         if (!reply.WriteRemoteObject(it)) {
361             HILOG_ERROR("failed to write query result.");
362             return ERR_FLATTEN_OBJECT;
363         }
364     }
365     if (!reply.WriteInt32(result)) {
366         return ERR_INVALID_VALUE;
367     }
368     return NO_ERROR;
369 }
370 
HandleStartRenderProcess(MessageParcel & data,MessageParcel & reply)371 int32_t AppMgrStub::HandleStartRenderProcess(MessageParcel &data, MessageParcel &reply)
372 {
373     std::string renderParam = data.ReadString();
374     int32_t ipcFd = data.ReadFileDescriptor();
375     int32_t sharedFd = data.ReadFileDescriptor();
376     int32_t renderPid = 0;
377     int32_t result = StartRenderProcess(renderParam, ipcFd, sharedFd, renderPid);
378     if (!reply.WriteInt32(result)) {
379         HILOG_ERROR("write result error.");
380         return ERR_INVALID_VALUE;
381     }
382     if (!reply.WriteInt32(renderPid)) {
383         HILOG_ERROR("write renderPid error.");
384         return ERR_INVALID_VALUE;
385     }
386     return result;
387 }
388 
HandleAttachRenderProcess(MessageParcel & data,MessageParcel & reply)389 int32_t AppMgrStub::HandleAttachRenderProcess(MessageParcel &data, MessageParcel &reply)
390 {
391     sptr<IRemoteObject> scheduler = data.ReadRemoteObject();
392     AttachRenderProcess(scheduler);
393     return NO_ERROR;
394 }
395 
HandleGetRenderProcessTerminationStatus(MessageParcel & data,MessageParcel & reply)396 int32_t AppMgrStub::HandleGetRenderProcessTerminationStatus(MessageParcel &data, MessageParcel &reply)
397 {
398     int32_t renderPid = data.ReadInt32();
399     int status = 0;
400     int32_t result = GetRenderProcessTerminationStatus(renderPid, status);
401     if (!reply.WriteInt32(result)) {
402         HILOG_ERROR("write result error.");
403         return ERR_INVALID_VALUE;
404     }
405     if (!reply.WriteInt32(status)) {
406         HILOG_ERROR("write status error.");
407         return ERR_INVALID_VALUE;
408     }
409     return result;
410 }
411 
HandlePostANRTaskByProcessID(MessageParcel & data,MessageParcel & reply)412 int32_t AppMgrStub::HandlePostANRTaskByProcessID(MessageParcel &data, MessageParcel &reply)
413 {
414     auto pid = data.ReadInt32();
415     PostANRTaskByProcessID(pid);
416     return NO_ERROR;
417 }
418 }  // namespace AppExecFwk
419 }  // namespace OHOS
420