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