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