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 "ams_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 "hitrace_meter.h"
27 #include "hilog_wrapper.h"
28 #include "iapp_state_callback.h"
29
30 namespace OHOS {
31 namespace AppExecFwk {
AmsMgrStub()32 AmsMgrStub::AmsMgrStub()
33 {
34 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::LOAD_ABILITY)] = &AmsMgrStub::HandleLoadAbility;
35 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::TERMINATE_ABILITY)] =
36 &AmsMgrStub::HandleTerminateAbility;
37 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::UPDATE_ABILITY_STATE)] =
38 &AmsMgrStub::HandleUpdateAbilityState;
39 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::UPDATE_EXTENSION_STATE)] =
40 &AmsMgrStub::HandleUpdateExtensionState;
41 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::REGISTER_APP_STATE_CALLBACK)] =
42 &AmsMgrStub::HandleRegisterAppStateCallback;
43 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::ABILITY_BEHAVIOR_ANALYSIS)] =
44 &AmsMgrStub::HandleAbilityBehaviorAnalysis;
45 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::KILL_PEOCESS_BY_ABILITY_TOKEN)] =
46 &AmsMgrStub::HandleKillProcessByAbilityToken;
47 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::KILL_PROCESSES_BY_USERID)] =
48 &AmsMgrStub::HandleKillProcessesByUserId;
49 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::KILL_PROCESS_WITH_ACCOUNT)] =
50 &AmsMgrStub::HandleKillProcessWithAccount;
51 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::KILL_APPLICATION)] = &AmsMgrStub::HandleKillApplication;
52 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::ABILITY_ATTACH_TIMEOUT)] =
53 &AmsMgrStub::HandleAbilityAttachTimeOut;
54 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::PREPARE_TERMINATE_ABILITY)] =
55 &AmsMgrStub::HandlePrepareTerminate;
56 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::KILL_APPLICATION_BYUID)] =
57 &AmsMgrStub::HandleKillApplicationByUid;
58 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::KILL_APPLICATION_SELF)] =
59 &AmsMgrStub::HandleKillApplicationSelf;
60 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::GET_RUNNING_PROCESS_INFO_BY_TOKEN)] =
61 &AmsMgrStub::HandleGetRunningProcessInfoByToken;
62 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::GET_RUNNING_PROCESS_INFO_BY_PID)] =
63 &AmsMgrStub::HandleGetRunningProcessInfoByPid;
64 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::START_SPECIFIED_ABILITY)] =
65 &AmsMgrStub::HandleStartSpecifiedAbility;
66 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::REGISTER_START_SPECIFIED_ABILITY_RESPONSE)] =
67 &AmsMgrStub::HandleRegisterStartSpecifiedAbilityResponse;
68 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::GET_APPLICATION_INFO_BY_PROCESS_ID)] =
69 &AmsMgrStub::HandleGetApplicationInfoByProcessID;
70 memberFuncMap_[static_cast<uint32_t>(IAmsMgr::Message::UPDATE_APPLICATION_INFO_INSTALLED)] =
71 &AmsMgrStub::HandleUpdateApplicationInfoInstalled;
72 }
73
~AmsMgrStub()74 AmsMgrStub::~AmsMgrStub()
75 {
76 memberFuncMap_.clear();
77 }
78
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)79 int AmsMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
80 {
81 HILOG_INFO("AmsMgrStub::OnReceived, code = %{public}u, flags= %{public}d.", code, option.GetFlags());
82 std::u16string descriptor = AmsMgrStub::GetDescriptor();
83 std::u16string remoteDescriptor = data.ReadInterfaceToken();
84 if (descriptor != remoteDescriptor) {
85 HILOG_ERROR("local descriptor is not equal to remote");
86 return ERR_INVALID_STATE;
87 }
88
89 auto itFunc = memberFuncMap_.find(code);
90 if (itFunc != memberFuncMap_.end()) {
91 auto memberFunc = itFunc->second;
92 if (memberFunc != nullptr) {
93 return (this->*memberFunc)(data, reply);
94 }
95 }
96 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
97 }
98
HandleLoadAbility(MessageParcel & data,MessageParcel & reply)99 ErrCode AmsMgrStub::HandleLoadAbility(MessageParcel &data, MessageParcel &reply)
100 {
101 HITRACE_METER(HITRACE_TAG_APP);
102 sptr<IRemoteObject> token = nullptr;
103 sptr<IRemoteObject> preToke = nullptr;
104 if (data.ReadBool()) {
105 token = data.ReadRemoteObject();
106 }
107 if (data.ReadBool()) {
108 preToke = data.ReadRemoteObject();
109 }
110 std::shared_ptr<AbilityInfo> abilityInfo(data.ReadParcelable<AbilityInfo>());
111 if (!abilityInfo) {
112 HILOG_ERROR("ReadParcelable<AbilityInfo> failed");
113 return ERR_APPEXECFWK_PARCEL_ERROR;
114 }
115
116 std::shared_ptr<ApplicationInfo> appInfo(data.ReadParcelable<ApplicationInfo>());
117 if (!appInfo) {
118 HILOG_ERROR("ReadParcelable<ApplicationInfo> failed");
119 return ERR_APPEXECFWK_PARCEL_ERROR;
120 }
121
122 std::shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
123 if (!want) {
124 HILOG_ERROR("ReadParcelable want failed");
125 return ERR_APPEXECFWK_PARCEL_ERROR;
126 }
127
128 LoadAbility(token, preToke, abilityInfo, appInfo, want);
129 return NO_ERROR;
130 }
131
HandleTerminateAbility(MessageParcel & data,MessageParcel & reply)132 ErrCode AmsMgrStub::HandleTerminateAbility(MessageParcel &data, MessageParcel &reply)
133 {
134 HITRACE_METER(HITRACE_TAG_APP);
135 sptr<IRemoteObject> token = data.ReadRemoteObject();
136 bool clearMissionFlag = data.ReadBool();
137 TerminateAbility(token, clearMissionFlag);
138 return NO_ERROR;
139 }
140
HandleUpdateAbilityState(MessageParcel & data,MessageParcel & reply)141 ErrCode AmsMgrStub::HandleUpdateAbilityState(MessageParcel &data, MessageParcel &reply)
142 {
143 HITRACE_METER(HITRACE_TAG_APP);
144 sptr<IRemoteObject> token = data.ReadRemoteObject();
145 int32_t state = data.ReadInt32();
146 UpdateAbilityState(token, static_cast<AbilityState>(state));
147 return NO_ERROR;
148 }
149
HandleUpdateExtensionState(MessageParcel & data,MessageParcel & reply)150 ErrCode AmsMgrStub::HandleUpdateExtensionState(MessageParcel &data, MessageParcel &reply)
151 {
152 sptr<IRemoteObject> token = data.ReadRemoteObject();
153 int32_t state = data.ReadInt32();
154 UpdateExtensionState(token, static_cast<ExtensionState>(state));
155 return NO_ERROR;
156 }
157
HandleRegisterAppStateCallback(MessageParcel & data,MessageParcel & reply)158 ErrCode AmsMgrStub::HandleRegisterAppStateCallback(MessageParcel &data, MessageParcel &reply)
159 {
160 HITRACE_METER(HITRACE_TAG_APP);
161 sptr<IAppStateCallback> callback = nullptr;
162 if (data.ReadBool()) {
163 sptr<IRemoteObject> obj = data.ReadRemoteObject();
164 callback = iface_cast<IAppStateCallback>(obj);
165 }
166 RegisterAppStateCallback(callback);
167 return NO_ERROR;
168 }
169
HandleAbilityBehaviorAnalysis(MessageParcel & data,MessageParcel & reply)170 ErrCode AmsMgrStub::HandleAbilityBehaviorAnalysis(MessageParcel &data, MessageParcel &reply)
171 {
172 HITRACE_METER(HITRACE_TAG_APP);
173 sptr<IRemoteObject> token = data.ReadRemoteObject();
174 sptr<IRemoteObject> preToke = nullptr;
175 if (data.ReadBool()) {
176 preToke = data.ReadRemoteObject();
177 }
178 int32_t visibility = data.ReadInt32();
179 int32_t Perceptibility = data.ReadInt32();
180 int32_t connectionState = data.ReadInt32();
181
182 AbilityBehaviorAnalysis(token, preToke, visibility, Perceptibility, connectionState);
183 return NO_ERROR;
184 }
185
HandleKillProcessByAbilityToken(MessageParcel & data,MessageParcel & reply)186 ErrCode AmsMgrStub::HandleKillProcessByAbilityToken(MessageParcel &data, MessageParcel &reply)
187 {
188 HITRACE_METER(HITRACE_TAG_APP);
189 sptr<IRemoteObject> token = data.ReadRemoteObject();
190
191 KillProcessByAbilityToken(token);
192 return NO_ERROR;
193 }
194
HandleKillProcessesByUserId(MessageParcel & data,MessageParcel & reply)195 ErrCode AmsMgrStub::HandleKillProcessesByUserId(MessageParcel &data, MessageParcel &reply)
196 {
197 HITRACE_METER(HITRACE_TAG_APP);
198 int32_t userId = data.ReadInt32();
199
200 KillProcessesByUserId(userId);
201 return NO_ERROR;
202 }
203
HandleKillProcessWithAccount(MessageParcel & data,MessageParcel & reply)204 ErrCode AmsMgrStub::HandleKillProcessWithAccount(MessageParcel &data, MessageParcel &reply)
205 {
206 HILOG_INFO("enter");
207
208 HITRACE_METER(HITRACE_TAG_APP);
209
210 std::string bundleName = data.ReadString();
211 int accountId = data.ReadInt32();
212
213 HILOG_INFO("bundleName = %{public}s, accountId = %{public}d", bundleName.c_str(), accountId);
214
215 int32_t result = KillProcessWithAccount(bundleName, accountId);
216 reply.WriteInt32(result);
217
218 HILOG_INFO("end");
219
220 return NO_ERROR;
221 }
222
HandleKillApplication(MessageParcel & data,MessageParcel & reply)223 ErrCode AmsMgrStub::HandleKillApplication(MessageParcel &data, MessageParcel &reply)
224 {
225 HITRACE_METER(HITRACE_TAG_APP);
226 std::string bundleName = data.ReadString();
227 int32_t result = KillApplication(bundleName);
228 reply.WriteInt32(result);
229 return NO_ERROR;
230 }
231
HandleKillApplicationByUid(MessageParcel & data,MessageParcel & reply)232 ErrCode AmsMgrStub::HandleKillApplicationByUid(MessageParcel &data, MessageParcel &reply)
233 {
234 HITRACE_METER(HITRACE_TAG_APP);
235 std::string bundleName = data.ReadString();
236 int uid = data.ReadInt32();
237 int32_t result = KillApplicationByUid(bundleName, uid);
238 reply.WriteInt32(result);
239 return NO_ERROR;
240 }
241
HandleKillApplicationSelf(MessageParcel & data,MessageParcel & reply)242 ErrCode AmsMgrStub::HandleKillApplicationSelf(MessageParcel &data, MessageParcel &reply)
243 {
244 HITRACE_METER(HITRACE_TAG_APP);
245 int32_t result = KillApplicationSelf();
246 if (!reply.WriteInt32(result)) {
247 HILOG_ERROR("result write failed.");
248 return ERR_INVALID_VALUE;
249 }
250 return NO_ERROR;
251 }
252
HandleAbilityAttachTimeOut(MessageParcel & data,MessageParcel & reply)253 int32_t AmsMgrStub::HandleAbilityAttachTimeOut(MessageParcel &data, MessageParcel &reply)
254 {
255 HITRACE_METER(HITRACE_TAG_APP);
256 sptr<IRemoteObject> token = data.ReadRemoteObject();
257 AbilityAttachTimeOut(token);
258 return NO_ERROR;
259 }
260
HandlePrepareTerminate(MessageParcel & data,MessageParcel & reply)261 int32_t AmsMgrStub::HandlePrepareTerminate(MessageParcel &data, MessageParcel &reply)
262 {
263 sptr<IRemoteObject> token = data.ReadRemoteObject();
264 PrepareTerminate(token);
265 return NO_ERROR;
266 }
267
UpdateExtensionState(const sptr<IRemoteObject> & token,const ExtensionState state)268 void AmsMgrStub::UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state)
269 {}
270
HandleGetRunningProcessInfoByToken(MessageParcel & data,MessageParcel & reply)271 int32_t AmsMgrStub::HandleGetRunningProcessInfoByToken(MessageParcel &data, MessageParcel &reply)
272 {
273 RunningProcessInfo processInfo;
274 auto token = data.ReadRemoteObject();
275 GetRunningProcessInfoByToken(token, processInfo);
276 if (reply.WriteParcelable(&processInfo)) {
277 HILOG_ERROR("process info write failed.");
278 return ERR_INVALID_VALUE;
279 }
280 return NO_ERROR;
281 }
282
HandleGetRunningProcessInfoByPid(MessageParcel & data,MessageParcel & reply)283 int32_t AmsMgrStub::HandleGetRunningProcessInfoByPid(MessageParcel &data, MessageParcel &reply)
284 {
285 RunningProcessInfo processInfo;
286 auto pid = static_cast<pid_t>(data.ReadInt32());
287 GetRunningProcessInfoByPid(pid, processInfo);
288 if (reply.WriteParcelable(&processInfo)) {
289 HILOG_ERROR("process info write failed.");
290 return ERR_INVALID_VALUE;
291 }
292 return NO_ERROR;
293 }
294
HandleStartSpecifiedAbility(MessageParcel & data,MessageParcel & reply)295 int32_t AmsMgrStub::HandleStartSpecifiedAbility(MessageParcel &data, MessageParcel &reply)
296 {
297 AAFwk::Want *want = data.ReadParcelable<AAFwk::Want>();
298 if (want == nullptr) {
299 HILOG_ERROR("want is nullptr");
300 return ERR_INVALID_VALUE;
301 }
302
303 AbilityInfo *abilityInfo = data.ReadParcelable<AbilityInfo>();
304 if (abilityInfo == nullptr) {
305 HILOG_ERROR("abilityInfo is nullptr.");
306 delete want;
307 return ERR_INVALID_VALUE;
308 }
309 StartSpecifiedAbility(*want, *abilityInfo);
310 delete want;
311 delete abilityInfo;
312 return NO_ERROR;
313 }
314
HandleRegisterStartSpecifiedAbilityResponse(MessageParcel & data,MessageParcel & reply)315 int32_t AmsMgrStub::HandleRegisterStartSpecifiedAbilityResponse(MessageParcel &data, MessageParcel &reply)
316 {
317 sptr<IRemoteObject> obj = data.ReadRemoteObject();
318 sptr<IStartSpecifiedAbilityResponse> response = iface_cast<IStartSpecifiedAbilityResponse>(obj);
319 RegisterStartSpecifiedAbilityResponse(response);
320 return NO_ERROR;
321 }
322
HandleGetApplicationInfoByProcessID(MessageParcel & data,MessageParcel & reply)323 int32_t AmsMgrStub::HandleGetApplicationInfoByProcessID(MessageParcel &data, MessageParcel &reply)
324 {
325 HITRACE_METER(HITRACE_TAG_APP);
326 int32_t pid = data.ReadInt32();
327 AppExecFwk::ApplicationInfo application;
328 bool debug;
329 int32_t result = GetApplicationInfoByProcessID(pid, application, debug);
330 if (!reply.WriteInt32(result)) {
331 HILOG_ERROR("write result error.");
332 return ERR_INVALID_VALUE;
333 }
334 if (!reply.WriteParcelable(&application)) {
335 HILOG_ERROR("write application info failed");
336 return ERR_INVALID_VALUE;
337 }
338 if (!reply.WriteBool(debug)) {
339 HILOG_ERROR("write debug info failed");
340 return ERR_INVALID_VALUE;
341 }
342 return NO_ERROR;
343 }
344
HandleUpdateApplicationInfoInstalled(MessageParcel & data,MessageParcel & reply)345 int32_t AmsMgrStub::HandleUpdateApplicationInfoInstalled(MessageParcel &data, MessageParcel &reply)
346 {
347 HITRACE_METER(HITRACE_TAG_APP);
348 std::string bundleName = data.ReadString();
349 int uid = data.ReadInt32();
350 int32_t result = UpdateApplicationInfoInstalled(bundleName, uid);
351 reply.WriteInt32(result);
352 return NO_ERROR;
353 }
354
355 } // namespace AppExecFwk
356 } // namespace OHOS
357