1 /*
2  * Copyright (c) 2021-2024 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 "ams_mgr_stub.h"
17 #include "ability_manager_errors.h"
18 #include "ability_info.h"
19 #include "app_debug_listener_interface.h"
20 #include "app_mgr_proxy.h"
21 #include "app_scheduler_interface.h"
22 #include "appexecfwk_errors.h"
23 #include "hilog_tag_wrapper.h"
24 #include "hitrace_meter.h"
25 #include "iapp_state_callback.h"
26 #include "ipc_skeleton.h"
27 #include "ipc_types.h"
28 #include "iremote_object.h"
29 #include "param.h"
30 #include "string_ex.h"
31 
32 namespace OHOS {
33 namespace AppExecFwk {
34 namespace {
35 constexpr int32_t MAX_APP_DEBUG_COUNT = 100;
36 constexpr int32_t MAX_KILL_PROCESS_PID_COUNT = 100;
37 }
38 
AmsMgrStub()39 AmsMgrStub::AmsMgrStub()
40 {
41     CreateMemberFuncMap();
42 }
43 
~AmsMgrStub()44 AmsMgrStub::~AmsMgrStub() {}
45 
CreateMemberFuncMap()46 void AmsMgrStub::CreateMemberFuncMap() {}
47 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)48 int AmsMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
49 {
50     if (code != static_cast<uint32_t>(IAmsMgr::Message::Get_BUNDLE_NAME_BY_PID)) {
51         TAG_LOGI(AAFwkTag::APPMGR, "OnReceived, code: %{public}u, flags: %{public}d", code,
52             option.GetFlags());
53     }
54     std::u16string descriptor = AmsMgrStub::GetDescriptor();
55     std::u16string remoteDescriptor = data.ReadInterfaceToken();
56     if (descriptor != remoteDescriptor) {
57         TAG_LOGE(AAFwkTag::APPMGR, "invalid descriptor");
58         return ERR_INVALID_STATE;
59     }
60     return OnRemoteRequestInner(code, data, reply, option);
61 }
62 
OnRemoteRequestInner(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)63 int32_t AmsMgrStub::OnRemoteRequestInner(uint32_t code, MessageParcel &data,
64     MessageParcel &reply, MessageOption &option)
65 {
66     int retCode = ERR_OK;
67     retCode = OnRemoteRequestInnerFirst(code, data, reply, option);
68     if (retCode != AAFwk::ERR_CODE_NOT_EXIST) {
69         return retCode;
70     }
71     retCode = OnRemoteRequestInnerSecond(code, data, reply, option);
72     if (retCode != AAFwk::ERR_CODE_NOT_EXIST) {
73         return retCode;
74     }
75     retCode = OnRemoteRequestInnerThird(code, data, reply, option);
76     if (retCode != AAFwk::ERR_CODE_NOT_EXIST) {
77         return retCode;
78     }
79     retCode = OnRemoteRequestInnerFourth(code, data, reply, option);
80     if (retCode != AAFwk::ERR_CODE_NOT_EXIST) {
81         return retCode;
82     }
83     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
84 }
85 
OnRemoteRequestInnerFirst(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)86 int32_t AmsMgrStub::OnRemoteRequestInnerFirst(uint32_t code, MessageParcel &data,
87     MessageParcel &reply, MessageOption &option)
88 {
89     switch (static_cast<uint32_t>(code)) {
90         case static_cast<uint32_t>(IAmsMgr::Message::LOAD_ABILITY):
91             return HandleLoadAbility(data, reply);
92         case static_cast<uint32_t>(IAmsMgr::Message::TERMINATE_ABILITY):
93             return HandleTerminateAbility(data, reply);
94         case static_cast<uint32_t>(IAmsMgr::Message::UPDATE_ABILITY_STATE):
95             return HandleUpdateAbilityState(data, reply);
96         case static_cast<uint32_t>(IAmsMgr::Message::UPDATE_EXTENSION_STATE):
97             return HandleUpdateExtensionState(data, reply);
98         case static_cast<uint32_t>(IAmsMgr::Message::REGISTER_APP_STATE_CALLBACK):
99             return HandleRegisterAppStateCallback(data, reply);
100         case static_cast<uint32_t>(IAmsMgr::Message::ABILITY_BEHAVIOR_ANALYSIS):
101             return HandleAbilityBehaviorAnalysis(data, reply);
102         case static_cast<uint32_t>(IAmsMgr::Message::KILL_PEOCESS_BY_ABILITY_TOKEN):
103             return HandleKillProcessByAbilityToken(data, reply);
104         case static_cast<uint32_t>(IAmsMgr::Message::KILL_PROCESSES_BY_USERID):
105             return HandleKillProcessesByUserId(data, reply);
106         case static_cast<uint32_t>(IAmsMgr::Message::KILL_PROCESS_WITH_ACCOUNT):
107             return HandleKillProcessWithAccount(data, reply);
108         case static_cast<uint32_t>(IAmsMgr::Message::KILL_APPLICATION):
109             return HandleKillApplication(data, reply);
110         case static_cast<uint32_t>(IAmsMgr::Message::ABILITY_ATTACH_TIMEOUT):
111             return HandleAbilityAttachTimeOut(data, reply);
112         case static_cast<uint32_t>(IAmsMgr::Message::PREPARE_TERMINATE_ABILITY):
113             return HandlePrepareTerminate(data, reply);
114         case static_cast<uint32_t>(IAmsMgr::Message::KILL_APPLICATION_BYUID):
115             return HandleKillApplicationByUid(data, reply);
116         case static_cast<uint32_t>(IAmsMgr::Message::KILL_APPLICATION_SELF):
117             return HandleKillApplicationSelf(data, reply);
118         case static_cast<uint32_t>(IAmsMgr::Message::GET_RUNNING_PROCESS_INFO_BY_TOKEN):
119             return HandleGetRunningProcessInfoByToken(data, reply);
120     }
121     return AAFwk::ERR_CODE_NOT_EXIST;
122 }
123 
OnRemoteRequestInnerSecond(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)124 int32_t AmsMgrStub::OnRemoteRequestInnerSecond(uint32_t code, MessageParcel &data,
125     MessageParcel &reply, MessageOption &option)
126 {
127     switch (static_cast<uint32_t>(code)) {
128         case static_cast<uint32_t>(IAmsMgr::Message::SET_ABILITY_FOREGROUNDING_FLAG):
129             return HandleSetAbilityForegroundingFlagToAppRecord(data, reply);
130         case static_cast<uint32_t>(IAmsMgr::Message::START_SPECIFIED_ABILITY):
131             return HandleStartSpecifiedAbility(data, reply);
132         case static_cast<uint32_t>(IAmsMgr::Message::REGISTER_START_SPECIFIED_ABILITY_RESPONSE):
133             return HandleRegisterStartSpecifiedAbilityResponse(data, reply);
134         case static_cast<uint32_t>(IAmsMgr::Message::GET_APPLICATION_INFO_BY_PROCESS_ID):
135             return HandleGetApplicationInfoByProcessID(data, reply);
136         case static_cast<uint32_t>(IAmsMgr::Message::NOTIFY_APP_MGR_RECORD_EXIT_REASON):
137             return HandleNotifyAppMgrRecordExitReason(data, reply);
138         case static_cast<uint32_t>(IAmsMgr::Message::UPDATE_APPLICATION_INFO_INSTALLED):
139             return HandleUpdateApplicationInfoInstalled(data, reply);
140         case static_cast<uint32_t>(IAmsMgr::Message::SET_CURRENT_USER_ID):
141             return HandleSetCurrentUserId(data, reply);
142         case static_cast<uint32_t>(IAmsMgr::Message::ENABLE_START_PROCESS_FLAG_BY_USER_ID):
143             return HandleSetEnableStartProcessFlagByUserId(data, reply);
144         case static_cast<uint32_t>(IAmsMgr::Message::Get_BUNDLE_NAME_BY_PID):
145             return HandleGetBundleNameByPid(data, reply);
146         case static_cast<uint32_t>(IAmsMgr::Message::REGISTER_APP_DEBUG_LISTENER):
147             return HandleRegisterAppDebugListener(data, reply);
148         case static_cast<uint32_t>(IAmsMgr::Message::UNREGISTER_APP_DEBUG_LISTENER):
149             return HandleUnregisterAppDebugListener(data, reply);
150         case static_cast<uint32_t>(IAmsMgr::Message::ATTACH_APP_DEBUG):
151             return HandleAttachAppDebug(data, reply);
152         case static_cast<uint32_t>(IAmsMgr::Message::DETACH_APP_DEBUG):
153             return HandleDetachAppDebug(data, reply);
154         case static_cast<uint32_t>(IAmsMgr::Message::SET_APP_WAITING_DEBUG):
155             return HandleSetAppWaitingDebug(data, reply);
156         case static_cast<uint32_t>(IAmsMgr::Message::CANCEL_APP_WAITING_DEBUG):
157             return HandleCancelAppWaitingDebug(data, reply);
158         case static_cast<uint32_t>(IAmsMgr::Message::GET_WAITING_DEBUG_APP):
159             return HandleGetWaitingDebugApp(data, reply);
160         case static_cast<uint32_t>(IAmsMgr::Message::IS_WAITING_DEBUG_APP):
161             return HandleIsWaitingDebugApp(data, reply);
162     }
163     return AAFwk::ERR_CODE_NOT_EXIST;
164 }
165 
OnRemoteRequestInnerThird(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)166 int32_t AmsMgrStub::OnRemoteRequestInnerThird(uint32_t code, MessageParcel &data,
167     MessageParcel &reply, MessageOption &option)
168 {
169     switch (static_cast<uint32_t>(code)) {
170         case static_cast<uint32_t>(IAmsMgr::Message::CLEAR_NON_PERSIST_WAITING_DEBUG_FLAG):
171             return HandleClearNonPersistWaitingDebugFlag(data, reply);
172         case static_cast<uint32_t>(IAmsMgr::Message::REGISTER_ABILITY_DEBUG_RESPONSE):
173             return HandleRegisterAbilityDebugResponse(data, reply);
174         case static_cast<uint32_t>(IAmsMgr::Message::IS_ATTACH_DEBUG):
175             return HandleIsAttachDebug(data, reply);
176         case static_cast<uint32_t>(IAmsMgr::Message::CLEAR_PROCESS_BY_TOKEN):
177             return HandleClearProcessByToken(data, reply);
178         case static_cast<uint32_t>(IAmsMgr::Message::KILL_PROCESSES_BY_PIDS):
179             return HandleKillProcessesByPids(data, reply);
180         case static_cast<uint32_t>(IAmsMgr::Message::ATTACH_PID_TO_PARENT):
181             return HandleAttachPidToParent(data, reply);
182         case static_cast<uint32_t>(IAmsMgr::Message::IS_MEMORY_SIZE_SUFFICIENT):
183             return HandleIsMemorySizeSufficent(data, reply);
184         case static_cast<uint32_t>(IAmsMgr::Message::SET_KEEP_ALIVE_ENABLE_STATE):
185             return HandleSetKeepAliveEnableState(data, reply);
186         case static_cast<uint32_t>(IAmsMgr::Message::ATTACHED_TO_STATUS_BAR):
187             return HandleAttachedToStatusBar(data, reply);
188         case static_cast<uint32_t>(IAmsMgr::Message::UPDATE_CONFIGURATION):
189             return 0;
190         case static_cast<uint32_t>(IAmsMgr::Message::GET_CONFIGURATION):
191             return 0;
192         case static_cast<uint32_t>(IAmsMgr::Message::START_SPECIFIED_PROCESS):
193             return 0;
194         case static_cast<uint32_t>(IAmsMgr::Message::REGISTER_ABILITY_MS_DELEGATE):
195             return 0;
196         case static_cast<uint32_t>(IAmsMgr::Message::BLOCK_PROCESS_CACHE_BY_PIDS):
197             return HandleBlockProcessCacheByPids(data, reply);
198         case static_cast<uint32_t>(IAmsMgr::Message::IS_KILLED_FOR_UPGRADE_WEB):
199             return HandleIsKilledForUpgradeWeb(data, reply);
200     }
201     return AAFwk::ERR_CODE_NOT_EXIST;
202 }
203 
OnRemoteRequestInnerFourth(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)204 int32_t AmsMgrStub::OnRemoteRequestInnerFourth(uint32_t code, MessageParcel &data,
205     MessageParcel &reply, MessageOption &option)
206 {
207     switch (static_cast<uint32_t>(code)) {
208         case static_cast<uint32_t>(IAmsMgr::Message::IS_PROCESS_CONTAINS_ONLY_UI_EXTENSION):
209             return HandleIsProcessContainsOnlyUIAbility(data, reply);
210         case static_cast<uint32_t>(IAmsMgr::Message::FORCE_KILL_APPLICATION):
211             return HandleForceKillApplication(data, reply);
212         case static_cast<uint32_t>(IAmsMgr::Message::CLEAN_UIABILITY_BY_USER_REQUEST):
213             return HandleCleanAbilityByUserRequest(data, reply);
214         case static_cast<uint32_t>(IAmsMgr::Message::FORCE_KILL_APPLICATION_BY_ACCESS_TOKEN_ID):
215             return HandleKillProcessesByAccessTokenId(data, reply);
216         case static_cast<uint32_t>(IAmsMgr::Message::IS_PROCESS_ATTACHED):
217             return HandleIsProcessAttached(data, reply);
218         case static_cast<uint32_t>(IAmsMgr::Message::KILL_PROCESSES_IN_BATCH):
219             return HandleKillProcessesInBatch(data, reply);
220     }
221     return AAFwk::ERR_CODE_NOT_EXIST;
222 }
223 
HandleLoadAbility(MessageParcel & data,MessageParcel & reply)224 ErrCode AmsMgrStub::HandleLoadAbility(MessageParcel &data, MessageParcel &reply)
225 {
226     HITRACE_METER(HITRACE_TAG_APP);
227     std::shared_ptr<AbilityInfo> abilityInfo(data.ReadParcelable<AbilityInfo>());
228     if (!abilityInfo) {
229         TAG_LOGE(AAFwkTag::APPMGR, "ReadParcelable<AbilityInfo> failed");
230         return ERR_APPEXECFWK_PARCEL_ERROR;
231     }
232 
233     std::shared_ptr<ApplicationInfo> appInfo(data.ReadParcelable<ApplicationInfo>());
234     if (!appInfo) {
235         TAG_LOGE(AAFwkTag::APPMGR, "ReadParcelable<ApplicationInfo> failed");
236         return ERR_APPEXECFWK_PARCEL_ERROR;
237     }
238 
239     std::shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
240     if (!want) {
241         TAG_LOGE(AAFwkTag::APPMGR, "ReadParcelable want failed");
242         return ERR_APPEXECFWK_PARCEL_ERROR;
243     }
244     std::shared_ptr<AbilityRuntime::LoadParam> loadParam(data.ReadParcelable<AbilityRuntime::LoadParam>());
245     if (!loadParam) {
246         TAG_LOGE(AAFwkTag::APPMGR, "ReadParcelable loadParam failed");
247         return ERR_APPEXECFWK_PARCEL_ERROR;
248     }
249 
250     LoadAbility(abilityInfo, appInfo, want, loadParam);
251     return NO_ERROR;
252 }
253 
HandleTerminateAbility(MessageParcel & data,MessageParcel & reply)254 ErrCode AmsMgrStub::HandleTerminateAbility(MessageParcel &data, MessageParcel &reply)
255 {
256     HITRACE_METER(HITRACE_TAG_APP);
257     sptr<IRemoteObject> token = data.ReadRemoteObject();
258     bool clearMissionFlag = data.ReadBool();
259     TerminateAbility(token, clearMissionFlag);
260     return NO_ERROR;
261 }
262 
HandleUpdateAbilityState(MessageParcel & data,MessageParcel & reply)263 ErrCode AmsMgrStub::HandleUpdateAbilityState(MessageParcel &data, MessageParcel &reply)
264 {
265     HITRACE_METER(HITRACE_TAG_APP);
266     sptr<IRemoteObject> token = data.ReadRemoteObject();
267     int32_t state = data.ReadInt32();
268     UpdateAbilityState(token, static_cast<AbilityState>(state));
269     return NO_ERROR;
270 }
271 
HandleUpdateExtensionState(MessageParcel & data,MessageParcel & reply)272 ErrCode AmsMgrStub::HandleUpdateExtensionState(MessageParcel &data, MessageParcel &reply)
273 {
274     sptr<IRemoteObject> token = data.ReadRemoteObject();
275     int32_t state = data.ReadInt32();
276     UpdateExtensionState(token, static_cast<ExtensionState>(state));
277     return NO_ERROR;
278 }
279 
HandleRegisterAppStateCallback(MessageParcel & data,MessageParcel & reply)280 ErrCode AmsMgrStub::HandleRegisterAppStateCallback(MessageParcel &data, MessageParcel &reply)
281 {
282     HITRACE_METER(HITRACE_TAG_APP);
283     sptr<IAppStateCallback> callback = nullptr;
284     if (data.ReadBool()) {
285         sptr<IRemoteObject> obj = data.ReadRemoteObject();
286         callback = iface_cast<IAppStateCallback>(obj);
287     }
288     RegisterAppStateCallback(callback);
289     return NO_ERROR;
290 }
291 
HandleAbilityBehaviorAnalysis(MessageParcel & data,MessageParcel & reply)292 ErrCode AmsMgrStub::HandleAbilityBehaviorAnalysis(MessageParcel &data, MessageParcel &reply)
293 {
294     HITRACE_METER(HITRACE_TAG_APP);
295     sptr<IRemoteObject> token = data.ReadRemoteObject();
296     sptr<IRemoteObject> preToke = nullptr;
297     if (data.ReadBool()) {
298         preToke = data.ReadRemoteObject();
299     }
300     int32_t visibility = data.ReadInt32();
301     int32_t perceptibility = data.ReadInt32();
302     int32_t connectionState = data.ReadInt32();
303 
304     AbilityBehaviorAnalysis(token, preToke, visibility, perceptibility, connectionState);
305     return NO_ERROR;
306 }
307 
HandleKillProcessByAbilityToken(MessageParcel & data,MessageParcel & reply)308 ErrCode AmsMgrStub::HandleKillProcessByAbilityToken(MessageParcel &data, MessageParcel &reply)
309 {
310     HITRACE_METER(HITRACE_TAG_APP);
311     sptr<IRemoteObject> token = data.ReadRemoteObject();
312 
313     KillProcessByAbilityToken(token);
314     return NO_ERROR;
315 }
316 
HandleKillProcessesByUserId(MessageParcel & data,MessageParcel & reply)317 ErrCode AmsMgrStub::HandleKillProcessesByUserId(MessageParcel &data, MessageParcel &reply)
318 {
319     HITRACE_METER(HITRACE_TAG_APP);
320     int32_t userId = data.ReadInt32();
321 
322     KillProcessesByUserId(userId);
323     return NO_ERROR;
324 }
325 
HandleKillProcessesByPids(MessageParcel & data,MessageParcel & reply)326 ErrCode AmsMgrStub::HandleKillProcessesByPids(MessageParcel &data, MessageParcel &reply)
327 {
328     HITRACE_METER(HITRACE_TAG_APP);
329     auto size = data.ReadUint32();
330     if (size == 0 || size > MAX_KILL_PROCESS_PID_COUNT) {
331         TAG_LOGE(AAFwkTag::APPMGR, "Invalid size");
332         return ERR_INVALID_VALUE;
333     }
334     std::vector<int32_t> pids;
335     for (uint32_t i = 0; i < size; i++) {
336         pids.emplace_back(data.ReadInt32());
337     }
338 
339     KillProcessesByPids(pids);
340     return NO_ERROR;
341 }
342 
HandleAttachPidToParent(MessageParcel & data,MessageParcel & reply)343 ErrCode AmsMgrStub::HandleAttachPidToParent(MessageParcel &data, MessageParcel &reply)
344 {
345     HITRACE_METER(HITRACE_TAG_APP);
346     sptr<IRemoteObject> token = data.ReadRemoteObject();
347     sptr<IRemoteObject> callerToken = data.ReadRemoteObject();
348     AttachPidToParent(token, callerToken);
349     return NO_ERROR;
350 }
351 
HandleKillProcessWithAccount(MessageParcel & data,MessageParcel & reply)352 ErrCode AmsMgrStub::HandleKillProcessWithAccount(MessageParcel &data, MessageParcel &reply)
353 {
354     TAG_LOGI(AAFwkTag::APPMGR, "enter");
355 
356     HITRACE_METER(HITRACE_TAG_APP);
357 
358     std::string bundleName = data.ReadString();
359     int accountId = data.ReadInt32();
360     bool clearPageStack = data.ReadBool();
361 
362     TAG_LOGI(AAFwkTag::APPMGR,
363         "bundleName = %{public}s, accountId = %{public}d, clearPageStack = %{public}d",
364         bundleName.c_str(), accountId, clearPageStack);
365 
366     int32_t result = KillProcessWithAccount(bundleName, accountId, clearPageStack);
367     reply.WriteInt32(result);
368 
369     TAG_LOGI(AAFwkTag::APPMGR, "end");
370 
371     return NO_ERROR;
372 }
373 
HandleKillProcessesInBatch(MessageParcel & data,MessageParcel & reply)374 ErrCode AmsMgrStub::HandleKillProcessesInBatch(MessageParcel &data, MessageParcel &reply)
375 {
376     TAG_LOGI(AAFwkTag::APPMGR, "enter");
377 
378     HITRACE_METER(HITRACE_TAG_APP);
379 
380     auto size = data.ReadUint32();
381     TAG_LOGI(AAFwkTag::APPMGR, "pids.size=%{public}d", size);
382     if (size == 0 || size > MAX_KILL_PROCESS_PID_COUNT) {
383         TAG_LOGE(AAFwkTag::APPMGR, "Invalid size");
384         return ERR_INVALID_VALUE;
385     }
386     std::vector<int32_t> pids;
387     for (uint32_t i = 0; i < size; i++) {
388         pids.emplace_back(data.ReadInt32());
389     }
390 
391     int32_t result = KillProcessesInBatch(pids);
392     reply.WriteInt32(result);
393 
394     TAG_LOGI(AAFwkTag::APPMGR, "end");
395 
396     return NO_ERROR;
397 }
398 
HandleKillApplication(MessageParcel & data,MessageParcel & reply)399 ErrCode AmsMgrStub::HandleKillApplication(MessageParcel &data, MessageParcel &reply)
400 {
401     HITRACE_METER(HITRACE_TAG_APP);
402     std::string bundleName = data.ReadString();
403     bool clearPageStack = data.ReadBool();
404 
405     TAG_LOGI(AAFwkTag::APPMGR, "bundleName = %{public}s, clearPageStack = %{public}d",
406         bundleName.c_str(), clearPageStack);
407 
408     int32_t result = KillApplication(bundleName, clearPageStack);
409     reply.WriteInt32(result);
410     return NO_ERROR;
411 }
412 
HandleForceKillApplication(MessageParcel & data,MessageParcel & reply)413 ErrCode AmsMgrStub::HandleForceKillApplication(MessageParcel &data, MessageParcel &reply)
414 {
415     HITRACE_METER(HITRACE_TAG_APP);
416     std::string bundleName = data.ReadString();
417     int userId = data.ReadInt32();
418     int appIndex = data.ReadInt32();
419 
420     TAG_LOGI(AAFwkTag::APPMGR, "bundleName = %{public}s,userId=%{public}d,appIndex=%{public}d",
421         bundleName.c_str(), userId, appIndex);
422 
423     int32_t result = ForceKillApplication(bundleName, userId, appIndex);
424     reply.WriteInt32(result);
425     return NO_ERROR;
426 }
427 
HandleKillProcessesByAccessTokenId(MessageParcel & data,MessageParcel & reply)428 ErrCode AmsMgrStub::HandleKillProcessesByAccessTokenId(MessageParcel &data, MessageParcel &reply)
429 {
430     HITRACE_METER(HITRACE_TAG_APP);
431     int accessTokenId = data.ReadInt32();
432 
433     TAG_LOGI(AAFwkTag::APPMGR, "accessTokenId=%{public}d", accessTokenId);
434 
435     int32_t result = KillProcessesByAccessTokenId(accessTokenId);
436     reply.WriteInt32(result);
437     return NO_ERROR;
438 }
439 
HandleKillApplicationByUid(MessageParcel & data,MessageParcel & reply)440 ErrCode AmsMgrStub::HandleKillApplicationByUid(MessageParcel &data, MessageParcel &reply)
441 {
442     HITRACE_METER(HITRACE_TAG_APP);
443     std::string bundleName = data.ReadString();
444     int uid = data.ReadInt32();
445     std::string reason = data.ReadString();
446     TAG_LOGW(AAFwkTag::APPMGR, "KillApplicationByUid,callingPid=%{public}d", IPCSkeleton::GetCallingPid());
447     int32_t result = KillApplicationByUid(bundleName, uid, reason);
448     reply.WriteInt32(result);
449     return NO_ERROR;
450 }
451 
HandleKillApplicationSelf(MessageParcel & data,MessageParcel & reply)452 ErrCode AmsMgrStub::HandleKillApplicationSelf(MessageParcel &data, MessageParcel &reply)
453 {
454     HITRACE_METER(HITRACE_TAG_APP);
455     TAG_LOGW(AAFwkTag::APPMGR, "KillApplicationSelf,callingPid=%{public}d", IPCSkeleton::GetCallingPid());
456     bool clearPageStack = data.ReadBool();
457     std::string reason = data.ReadString();
458     int32_t result = KillApplicationSelf(clearPageStack, reason);
459     if (!reply.WriteInt32(result)) {
460         TAG_LOGE(AAFwkTag::APPMGR, "result write failed.");
461         return ERR_INVALID_VALUE;
462     }
463     return NO_ERROR;
464 }
465 
HandleAbilityAttachTimeOut(MessageParcel & data,MessageParcel & reply)466 int32_t AmsMgrStub::HandleAbilityAttachTimeOut(MessageParcel &data, MessageParcel &reply)
467 {
468     HITRACE_METER(HITRACE_TAG_APP);
469     sptr<IRemoteObject> token = data.ReadRemoteObject();
470     AbilityAttachTimeOut(token);
471     return NO_ERROR;
472 }
473 
HandlePrepareTerminate(MessageParcel & data,MessageParcel & reply)474 int32_t AmsMgrStub::HandlePrepareTerminate(MessageParcel &data, MessageParcel &reply)
475 {
476     sptr<IRemoteObject> token = data.ReadRemoteObject();
477     bool clearMissionFlag = data.ReadBool();
478     PrepareTerminate(token, clearMissionFlag);
479     return NO_ERROR;
480 }
481 
UpdateExtensionState(const sptr<IRemoteObject> & token,const ExtensionState state)482 void AmsMgrStub::UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state)
483 {}
484 
HandleGetRunningProcessInfoByToken(MessageParcel & data,MessageParcel & reply)485 int32_t AmsMgrStub::HandleGetRunningProcessInfoByToken(MessageParcel &data, MessageParcel &reply)
486 {
487     RunningProcessInfo processInfo;
488     auto token = data.ReadRemoteObject();
489     GetRunningProcessInfoByToken(token, processInfo);
490     if (reply.WriteParcelable(&processInfo)) {
491         TAG_LOGE(AAFwkTag::APPMGR, "process info write failed.");
492         return ERR_INVALID_VALUE;
493     }
494     return NO_ERROR;
495 }
496 
HandleSetAbilityForegroundingFlagToAppRecord(MessageParcel & data,MessageParcel & reply)497 int32_t AmsMgrStub::HandleSetAbilityForegroundingFlagToAppRecord(MessageParcel &data, MessageParcel &reply)
498 {
499     RunningProcessInfo processInfo;
500     auto pid = static_cast<pid_t>(data.ReadInt32());
501     SetAbilityForegroundingFlagToAppRecord(pid);
502     return NO_ERROR;
503 }
504 
HandleStartSpecifiedAbility(MessageParcel & data,MessageParcel & reply)505 int32_t AmsMgrStub::HandleStartSpecifiedAbility(MessageParcel &data, MessageParcel &reply)
506 {
507     AAFwk::Want *want = data.ReadParcelable<AAFwk::Want>();
508     if (want == nullptr) {
509         TAG_LOGE(AAFwkTag::APPMGR, "want is nullptr");
510         return ERR_INVALID_VALUE;
511     }
512 
513     AbilityInfo *abilityInfo = data.ReadParcelable<AbilityInfo>();
514     if (abilityInfo == nullptr) {
515         TAG_LOGE(AAFwkTag::APPMGR, "abilityInfo is nullptr.");
516         delete want;
517         return ERR_INVALID_VALUE;
518     }
519     StartSpecifiedAbility(*want, *abilityInfo, data.ReadInt32());
520     delete want;
521     delete abilityInfo;
522     return NO_ERROR;
523 }
524 
HandleRegisterStartSpecifiedAbilityResponse(MessageParcel & data,MessageParcel & reply)525 int32_t AmsMgrStub::HandleRegisterStartSpecifiedAbilityResponse(MessageParcel &data, MessageParcel &reply)
526 {
527     sptr<IRemoteObject> obj = data.ReadRemoteObject();
528     sptr<IStartSpecifiedAbilityResponse> response = iface_cast<IStartSpecifiedAbilityResponse>(obj);
529     RegisterStartSpecifiedAbilityResponse(response);
530     return NO_ERROR;
531 }
532 
HandleGetApplicationInfoByProcessID(MessageParcel & data,MessageParcel & reply)533 int32_t AmsMgrStub::HandleGetApplicationInfoByProcessID(MessageParcel &data, MessageParcel &reply)
534 {
535     HITRACE_METER(HITRACE_TAG_APP);
536     int32_t pid = data.ReadInt32();
537     AppExecFwk::ApplicationInfo application;
538     bool debug;
539     int32_t result = GetApplicationInfoByProcessID(pid, application, debug);
540     if (!reply.WriteInt32(result)) {
541         TAG_LOGE(AAFwkTag::APPMGR, "write result error.");
542         return ERR_INVALID_VALUE;
543     }
544     if (!reply.WriteParcelable(&application)) {
545         TAG_LOGE(AAFwkTag::APPMGR, "write application info failed");
546         return ERR_INVALID_VALUE;
547     }
548     if (!reply.WriteBool(debug)) {
549         TAG_LOGE(AAFwkTag::APPMGR, "write debug info failed");
550         return ERR_INVALID_VALUE;
551     }
552     return NO_ERROR;
553 }
554 
HandleNotifyAppMgrRecordExitReason(MessageParcel & data,MessageParcel & reply)555 int32_t AmsMgrStub::HandleNotifyAppMgrRecordExitReason(MessageParcel &data, MessageParcel &reply)
556 {
557     TAG_LOGD(AAFwkTag::APPMGR, "called");
558     int32_t pid = data.ReadInt32();
559     int32_t reason = data.ReadInt32();
560     std::string exitMsg = Str16ToStr8(data.ReadString16());
561     int32_t result = NotifyAppMgrRecordExitReason(pid, reason, exitMsg);
562     if (!reply.WriteInt32(result)) {
563         TAG_LOGE(AAFwkTag::APPMGR, "Write result failed.");
564         return IPC_PROXY_ERR;
565     }
566     return NO_ERROR;
567 }
568 
HandleUpdateApplicationInfoInstalled(MessageParcel & data,MessageParcel & reply)569 int32_t AmsMgrStub::HandleUpdateApplicationInfoInstalled(MessageParcel &data, MessageParcel &reply)
570 {
571     HITRACE_METER(HITRACE_TAG_APP);
572     std::string bundleName = data.ReadString();
573     int uid = data.ReadInt32();
574     int32_t result = UpdateApplicationInfoInstalled(bundleName, uid);
575     reply.WriteInt32(result);
576     return NO_ERROR;
577 }
578 
HandleSetCurrentUserId(MessageParcel & data,MessageParcel & reply)579 int32_t AmsMgrStub::HandleSetCurrentUserId(MessageParcel &data, MessageParcel &reply)
580 {
581     int32_t userId = data.ReadInt32();
582     SetCurrentUserId(userId);
583     return NO_ERROR;
584 }
585 
HandleSetEnableStartProcessFlagByUserId(MessageParcel & data,MessageParcel & reply)586 int32_t AmsMgrStub::HandleSetEnableStartProcessFlagByUserId(MessageParcel &data, MessageParcel &reply)
587 {
588     int32_t userId = data.ReadInt32();
589     bool enableStartProcess = data.ReadBool();
590     SetEnableStartProcessFlagByUserId(userId, enableStartProcess);
591     return NO_ERROR;
592 }
593 
HandleGetBundleNameByPid(MessageParcel & data,MessageParcel & reply)594 int32_t AmsMgrStub::HandleGetBundleNameByPid(MessageParcel &data, MessageParcel &reply)
595 {
596     int32_t pid = data.ReadInt32();
597     std::string bundleName;
598     int32_t uid;
599     GetBundleNameByPid(pid, bundleName, uid);
600 
601     reply.WriteString(bundleName);
602     reply.WriteInt32(uid);
603     return NO_ERROR;
604 }
605 
HandleRegisterAppDebugListener(MessageParcel & data,MessageParcel & reply)606 int32_t AmsMgrStub::HandleRegisterAppDebugListener(MessageParcel &data, MessageParcel &reply)
607 {
608     TAG_LOGD(AAFwkTag::APPMGR, "called");
609     auto appDebugLister = iface_cast<IAppDebugListener>(data.ReadRemoteObject());
610     if (appDebugLister == nullptr) {
611         TAG_LOGE(AAFwkTag::APPMGR, "App debug lister is null.");
612         return ERR_INVALID_VALUE;
613     }
614 
615     auto result = RegisterAppDebugListener(appDebugLister);
616     if (!reply.WriteInt32(result)) {
617         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result.");
618         return ERR_INVALID_VALUE;
619     }
620     return NO_ERROR;
621 }
622 
HandleUnregisterAppDebugListener(MessageParcel & data,MessageParcel & reply)623 int32_t AmsMgrStub::HandleUnregisterAppDebugListener(MessageParcel &data, MessageParcel &reply)
624 {
625     TAG_LOGD(AAFwkTag::APPMGR, "called");
626     auto appDebugLister = iface_cast<IAppDebugListener>(data.ReadRemoteObject());
627     if (appDebugLister == nullptr) {
628         TAG_LOGE(AAFwkTag::APPMGR, "App debug lister is nullptr.");
629         return ERR_INVALID_VALUE;
630     }
631 
632     auto result = UnregisterAppDebugListener(appDebugLister);
633     if (!reply.WriteInt32(result)) {
634         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result.");
635         return ERR_INVALID_VALUE;
636     }
637     return NO_ERROR;
638 }
639 
HandleAttachAppDebug(MessageParcel & data,MessageParcel & reply)640 int32_t AmsMgrStub::HandleAttachAppDebug(MessageParcel &data, MessageParcel &reply)
641 {
642     TAG_LOGD(AAFwkTag::APPMGR, "called");
643     auto bundleName = data.ReadString();
644     if (bundleName.empty()) {
645         TAG_LOGE(AAFwkTag::APPMGR, "Bundle name is empty.");
646         return ERR_INVALID_VALUE;
647     }
648 
649     auto result = AttachAppDebug(bundleName);
650     if (!reply.WriteInt32(result)) {
651         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result.");
652         return ERR_INVALID_VALUE;
653     }
654     return NO_ERROR;
655 }
656 
HandleDetachAppDebug(MessageParcel & data,MessageParcel & reply)657 int32_t AmsMgrStub::HandleDetachAppDebug(MessageParcel &data, MessageParcel &reply)
658 {
659     TAG_LOGD(AAFwkTag::APPMGR, "called");
660     auto bundleName = data.ReadString();
661     if (bundleName.empty()) {
662         TAG_LOGE(AAFwkTag::APPMGR, "Bundle name is empty.");
663         return ERR_INVALID_VALUE;
664     }
665 
666     auto result = DetachAppDebug(bundleName);
667     if (!reply.WriteInt32(result)) {
668         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result.");
669         return ERR_INVALID_VALUE;
670     }
671     return NO_ERROR;
672 }
673 
HandleSetAppWaitingDebug(MessageParcel & data,MessageParcel & reply)674 int32_t AmsMgrStub::HandleSetAppWaitingDebug(MessageParcel &data, MessageParcel &reply)
675 {
676     TAG_LOGD(AAFwkTag::APPMGR, "called");
677     auto bundleName = data.ReadString();
678     if (bundleName.empty()) {
679         TAG_LOGE(AAFwkTag::APPMGR, "Bundle name is empty.");
680         return ERR_INVALID_VALUE;
681     }
682     auto isPersist = data.ReadBool();
683     auto result = SetAppWaitingDebug(bundleName, isPersist);
684     if (!reply.WriteInt32(result)) {
685         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result.");
686         return ERR_INVALID_VALUE;
687     }
688     return NO_ERROR;
689 }
690 
HandleCancelAppWaitingDebug(MessageParcel & data,MessageParcel & reply)691 int32_t AmsMgrStub::HandleCancelAppWaitingDebug(MessageParcel &data, MessageParcel &reply)
692 {
693     TAG_LOGD(AAFwkTag::APPMGR, "called");
694     auto result = CancelAppWaitingDebug();
695     if (!reply.WriteInt32(result)) {
696         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result.");
697         return ERR_INVALID_VALUE;
698     }
699     return NO_ERROR;
700 }
701 
HandleGetWaitingDebugApp(MessageParcel & data,MessageParcel & reply)702 int32_t AmsMgrStub::HandleGetWaitingDebugApp(MessageParcel &data, MessageParcel &reply)
703 {
704     TAG_LOGD(AAFwkTag::APPMGR, "called");
705     std::vector<std::string> debugInfoList;
706     auto result = GetWaitingDebugApp(debugInfoList);
707     if (!reply.WriteInt32(result)) {
708         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result.");
709         return ERR_INVALID_VALUE;
710     }
711 
712     int32_t listSize = static_cast<int32_t>(debugInfoList.size());
713     if (listSize > MAX_APP_DEBUG_COUNT) {
714         TAG_LOGE(AAFwkTag::APPMGR, "Max app debug count is %{public}d.", listSize);
715         return ERR_INVALID_VALUE;
716     }
717 
718     if (!reply.WriteInt32(listSize)) {
719         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write list size.");
720         return ERR_INVALID_VALUE;
721     }
722 
723     if (!reply.WriteStringVector(debugInfoList)) {
724         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write string vector debug info list.");
725         return ERR_INVALID_VALUE;
726     }
727     return NO_ERROR;
728 }
729 
HandleIsWaitingDebugApp(MessageParcel & data,MessageParcel & reply)730 int32_t AmsMgrStub::HandleIsWaitingDebugApp(MessageParcel &data, MessageParcel &reply)
731 {
732     TAG_LOGD(AAFwkTag::APPMGR, "called");
733     auto bundleName = data.ReadString();
734     if (bundleName.empty()) {
735         TAG_LOGE(AAFwkTag::APPMGR, "Bundle name is empty.");
736         return ERR_INVALID_VALUE;
737     }
738 
739     auto result = IsWaitingDebugApp(bundleName);
740     if (!reply.WriteBool(result)) {
741         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result.");
742         return ERR_INVALID_VALUE;
743     }
744     return NO_ERROR;
745 }
746 
HandleSetKeepAliveEnableState(MessageParcel & data,MessageParcel & reply)747 int32_t AmsMgrStub::HandleSetKeepAliveEnableState(MessageParcel &data, MessageParcel &reply)
748 {
749     TAG_LOGD(AAFwkTag::APPMGR, "called");
750     auto bundleName = data.ReadString();
751     auto enable = data.ReadBool();
752     auto uid = data.ReadInt32();
753     SetKeepAliveEnableState(bundleName, enable, uid);
754     return NO_ERROR;
755 }
756 
HandleClearNonPersistWaitingDebugFlag(MessageParcel & data,MessageParcel & reply)757 int32_t AmsMgrStub::HandleClearNonPersistWaitingDebugFlag(MessageParcel &data, MessageParcel &reply)
758 {
759     TAG_LOGD(AAFwkTag::APPMGR, "called");
760     ClearNonPersistWaitingDebugFlag();
761     return NO_ERROR;
762 }
763 
HandleRegisterAbilityDebugResponse(MessageParcel & data,MessageParcel & reply)764 int32_t AmsMgrStub::HandleRegisterAbilityDebugResponse(MessageParcel &data, MessageParcel &reply)
765 {
766     TAG_LOGD(AAFwkTag::APPMGR, "called");
767     auto response = iface_cast<IAbilityDebugResponse>(data.ReadRemoteObject());
768     if (response == nullptr) {
769         TAG_LOGE(AAFwkTag::APPMGR, "Response is nullptr.");
770         return ERR_INVALID_VALUE;
771     }
772 
773     auto result = RegisterAbilityDebugResponse(response);
774     if (!reply.WriteInt32(result)) {
775         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result.");
776         return ERR_INVALID_VALUE;
777     }
778     return NO_ERROR;
779 }
780 
HandleIsAttachDebug(MessageParcel & data,MessageParcel & reply)781 int32_t AmsMgrStub::HandleIsAttachDebug(MessageParcel &data, MessageParcel &reply)
782 {
783     TAG_LOGD(AAFwkTag::APPMGR, "called");
784     auto bundleName = data.ReadString();
785     if (bundleName.empty()) {
786         TAG_LOGE(AAFwkTag::APPMGR, "Bundle name is empty.");
787         return ERR_INVALID_VALUE;
788     }
789 
790     auto result = IsAttachDebug(bundleName);
791     if (!reply.WriteBool(result)) {
792         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result.");
793         return ERR_INVALID_VALUE;
794     }
795     return NO_ERROR;
796 }
797 
HandleClearProcessByToken(MessageParcel & data,MessageParcel & reply)798 int32_t AmsMgrStub::HandleClearProcessByToken(MessageParcel &data, MessageParcel &reply)
799 {
800     HITRACE_METER(HITRACE_TAG_APP);
801     sptr<IRemoteObject> token = data.ReadRemoteObject();
802     ClearProcessByToken(token);
803     return NO_ERROR;
804 }
805 
HandleIsMemorySizeSufficent(MessageParcel & data,MessageParcel & reply)806 int32_t AmsMgrStub::HandleIsMemorySizeSufficent(MessageParcel &data, MessageParcel &reply)
807 {
808     auto result = IsMemorySizeSufficent();
809     if (!reply.WriteBool(result)) {
810         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result.");
811         return ERR_INVALID_VALUE;
812     }
813     return NO_ERROR;
814 }
815 
HandleAttachedToStatusBar(MessageParcel & data,MessageParcel & reply)816 ErrCode AmsMgrStub::HandleAttachedToStatusBar(MessageParcel &data, MessageParcel &reply)
817 {
818     HITRACE_METER(HITRACE_TAG_APP);
819     sptr<IRemoteObject> token = data.ReadRemoteObject();
820     AttachedToStatusBar(token);
821     return NO_ERROR;
822 }
823 
HandleBlockProcessCacheByPids(MessageParcel & data,MessageParcel & reply)824 ErrCode AmsMgrStub::HandleBlockProcessCacheByPids(MessageParcel &data, MessageParcel &reply)
825 {
826     HITRACE_METER(HITRACE_TAG_APP);
827     auto size = data.ReadUint32();
828     if (size == 0 || size > MAX_KILL_PROCESS_PID_COUNT) {
829         TAG_LOGE(AAFwkTag::APPMGR, "Invalid size.");
830         return ERR_INVALID_VALUE;
831     }
832     std::vector<int32_t> pids;
833     for (uint32_t i = 0; i < size; i++) {
834         pids.emplace_back(data.ReadInt32());
835     }
836 
837     BlockProcessCacheByPids(pids);
838     return NO_ERROR;
839 }
840 
HandleIsKilledForUpgradeWeb(MessageParcel & data,MessageParcel & reply)841 int32_t AmsMgrStub::HandleIsKilledForUpgradeWeb(MessageParcel &data, MessageParcel &reply)
842 {
843     TAG_LOGD(AAFwkTag::APPMGR, "Called.");
844     auto bundleName = data.ReadString();
845     if (bundleName.empty()) {
846         TAG_LOGE(AAFwkTag::APPMGR, "Bundle name is empty.");
847         return ERR_INVALID_VALUE;
848     }
849 
850     auto result = IsKilledForUpgradeWeb(bundleName);
851     if (!reply.WriteBool(result)) {
852         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result.");
853         return ERR_INVALID_VALUE;
854     }
855     return NO_ERROR;
856 }
857 
HandleCleanAbilityByUserRequest(MessageParcel & data,MessageParcel & reply)858 ErrCode AmsMgrStub::HandleCleanAbilityByUserRequest(MessageParcel &data, MessageParcel &reply)
859 {
860     HITRACE_METER(HITRACE_TAG_APP);
861     sptr<IRemoteObject> token = data.ReadRemoteObject();
862     auto result = CleanAbilityByUserRequest(token);
863     if (!reply.WriteBool(result)) {
864         TAG_LOGE(AAFwkTag::APPMGR, "fail to write the result.");
865         return ERR_INVALID_VALUE;
866     }
867     return NO_ERROR;
868 }
869 
HandleIsProcessContainsOnlyUIAbility(MessageParcel & data,MessageParcel & reply)870 int32_t AmsMgrStub::HandleIsProcessContainsOnlyUIAbility(MessageParcel &data, MessageParcel &reply)
871 {
872     auto pid = data.ReadUint32();
873 
874     auto result = IsProcessContainsOnlyUIAbility(pid);
875     if (!reply.WriteBool(result)) {
876         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result in HandleIsProcessContainsOnlyUIAbility.");
877         return ERR_INVALID_VALUE;
878     }
879     return NO_ERROR;
880 }
881 
HandleIsProcessAttached(MessageParcel & data,MessageParcel & reply)882 int32_t AmsMgrStub::HandleIsProcessAttached(MessageParcel &data, MessageParcel &reply)
883 {
884     HITRACE_METER(HITRACE_TAG_APP);
885     sptr<IRemoteObject> token = data.ReadRemoteObject();
886     auto isAttached = IsProcessAttached(token);
887     if (!reply.WriteBool(isAttached)) {
888         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result");
889         return ERR_INVALID_VALUE;
890     }
891     return NO_ERROR;
892 }
893 }  // namespace AppExecFwk
894 }  // namespace OHOS
895