• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "ability_manager_collaborator_proxy.h"
17 #include "configuration.h"
18 #include "errors.h"
19 #include "hilog_wrapper.h"
20 
21 namespace OHOS {
22 namespace AAFwk {
NotifyStartAbility(const AppExecFwk::AbilityInfo & abilityInfo,int32_t userId,Want & want,uint64_t accessTokenIDEx)23 int32_t AbilityManagerCollaboratorProxy::NotifyStartAbility(
24     const AppExecFwk::AbilityInfo &abilityInfo, int32_t userId, Want &want, uint64_t accessTokenIDEx)
25 {
26     MessageParcel data;
27     MessageParcel reply;
28     MessageOption option(MessageOption::TF_SYNC);
29 
30     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
31         HILOG_ERROR("Write interface token failed.");
32         return ERR_INVALID_OPERATION;
33     }
34     if (!data.WriteParcelable(&abilityInfo)) {
35         HILOG_ERROR("abilityInfo write failed.");
36         return ERR_INVALID_OPERATION;
37     }
38     if (!data.WriteInt32(userId)) {
39         HILOG_ERROR("userId write failed.");
40         return ERR_INVALID_OPERATION;
41     }
42     if (!data.WriteParcelable(&want)) {
43         HILOG_ERROR("want write failed.");
44         return ERR_INVALID_OPERATION;
45     }
46     if (!data.WriteUint64(accessTokenIDEx)) {
47         HILOG_ERROR("accessTokenIDEx write failed.");
48         return ERR_INVALID_OPERATION;
49     }
50     int32_t ret = SendTransactCmd(IAbilityManagerCollaborator::NOTIFY_START_ABILITY, data, reply, option);
51     if (ret != NO_ERROR) {
52         HILOG_ERROR("Send request error: %{public}d", ret);
53         return ret;
54     }
55     ret = reply.ReadInt32();
56     if (ret != NO_ERROR) {
57         HILOG_ERROR("notify start ability failed");
58         return ERR_INVALID_OPERATION;
59     }
60     std::unique_ptr<Want> wantInfo(reply.ReadParcelable<Want>());
61     if (!wantInfo) {
62         HILOG_ERROR("readParcelableInfo failed");
63         return ERR_INVALID_OPERATION;
64     }
65     want = *wantInfo;
66     return NO_ERROR;
67 }
68 
NotifyMissionCreated(int32_t missionId,const Want & want)69 int32_t AbilityManagerCollaboratorProxy::NotifyMissionCreated(int32_t missionId, const Want &want)
70 {
71     MessageParcel data;
72     MessageParcel reply;
73     MessageOption option(MessageOption::TF_SYNC);
74 
75     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
76         HILOG_ERROR("Write interface token failed.");
77         return ERR_INVALID_OPERATION;
78     }
79     if (!data.WriteInt32(missionId)) {
80         HILOG_ERROR("missionId write failed.");
81         return ERR_INVALID_OPERATION;
82     }
83     if (!data.WriteParcelable(&want)) {
84         HILOG_ERROR("want write failed.");
85         return ERR_INVALID_OPERATION;
86     }
87     int32_t ret = SendTransactCmd(IAbilityManagerCollaborator::NOTIFY_MISSION_CREATED, data, reply, option);
88     if (ret != NO_ERROR) {
89         HILOG_ERROR("Send request error: %{public}d", ret);
90         return ret;
91     }
92     return NO_ERROR;
93 }
94 
NotifyMissionCreated(const sptr<SessionInfo> & sessionInfo)95 int32_t AbilityManagerCollaboratorProxy::NotifyMissionCreated(const sptr<SessionInfo> &sessionInfo)
96 {
97     MessageParcel data;
98     MessageParcel reply;
99     MessageOption option(MessageOption::TF_SYNC);
100 
101     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
102         HILOG_ERROR("Write interface token failed.");
103         return ERR_INVALID_OPERATION;
104     }
105     if (sessionInfo) {
106         if (!data.WriteBool(true) || !data.WriteParcelable(sessionInfo)) {
107             HILOG_ERROR("flag and sessionInfo write failed.");
108             return ERR_INVALID_OPERATION;
109         }
110     } else {
111         if (!data.WriteBool(false)) {
112             HILOG_ERROR("flag write failed.");
113             return ERR_INVALID_OPERATION;
114         }
115     }
116     int32_t ret = SendTransactCmd(IAbilityManagerCollaborator::NOTIFY_MISSION_CREATED_BY_SCB, data, reply, option);
117     if (ret != NO_ERROR) {
118         HILOG_ERROR("Send request error: %{public}d", ret);
119         return ret;
120     }
121     return NO_ERROR;
122 }
123 
NotifyLoadAbility(const AppExecFwk::AbilityInfo & abilityInfo,int32_t missionId,const Want & want)124 int32_t AbilityManagerCollaboratorProxy::NotifyLoadAbility(
125     const AppExecFwk::AbilityInfo &abilityInfo, int32_t missionId, const Want &want)
126 {
127     MessageParcel data;
128     MessageParcel reply;
129     MessageOption option(MessageOption::TF_SYNC);
130 
131     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
132         HILOG_ERROR("Write interface token failed.");
133         return ERR_INVALID_OPERATION;
134     }
135     if (!data.WriteParcelable(&abilityInfo)) {
136         HILOG_ERROR("abilityInfo write failed.");
137         return ERR_INVALID_OPERATION;
138     }
139     if (!data.WriteInt32(missionId)) {
140         HILOG_ERROR("missionId write failed.");
141         return ERR_INVALID_OPERATION;
142     }
143     if (!data.WriteParcelable(&want)) {
144         HILOG_ERROR("want write failed.");
145         return ERR_INVALID_OPERATION;
146     }
147     int32_t ret = SendTransactCmd(IAbilityManagerCollaborator::NOTIFY_LOAD_ABILITY, data, reply, option);
148     if (ret != NO_ERROR) {
149         HILOG_ERROR("Send request error: %{public}d", ret);
150         return ret;
151     }
152     return NO_ERROR;
153 }
154 
NotifyLoadAbility(const AppExecFwk::AbilityInfo & abilityInfo,const sptr<SessionInfo> & sessionInfo)155 int32_t AbilityManagerCollaboratorProxy::NotifyLoadAbility(
156     const AppExecFwk::AbilityInfo &abilityInfo, const sptr<SessionInfo> &sessionInfo)
157 {
158     MessageParcel data;
159     MessageParcel reply;
160     MessageOption option(MessageOption::TF_SYNC);
161 
162     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
163         HILOG_ERROR("Write interface token failed.");
164         return ERR_INVALID_OPERATION;
165     }
166     if (!data.WriteParcelable(&abilityInfo)) {
167         HILOG_ERROR("abilityInfo write failed.");
168         return ERR_INVALID_OPERATION;
169     }
170     if (sessionInfo) {
171         if (!data.WriteBool(true) || !data.WriteParcelable(sessionInfo)) {
172             HILOG_ERROR("flag and sessionInfo write failed.");
173             return ERR_INVALID_OPERATION;
174         }
175     } else {
176         if (!data.WriteBool(false)) {
177             HILOG_ERROR("flag write failed.");
178             return ERR_INVALID_OPERATION;
179         }
180     }
181     int32_t ret = SendTransactCmd(IAbilityManagerCollaborator::NOTIFY_LOAD_ABILITY_BY_SCB, data, reply, option);
182     if (ret != NO_ERROR) {
183         HILOG_ERROR("Send request error: %{public}d", ret);
184         return ret;
185     }
186     return NO_ERROR;
187 }
188 
NotifyMoveMissionToBackground(int32_t missionId)189 int32_t AbilityManagerCollaboratorProxy::NotifyMoveMissionToBackground(int32_t missionId)
190 {
191     MessageParcel data;
192     MessageParcel reply;
193     MessageOption option(MessageOption::TF_ASYNC);
194 
195     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
196         HILOG_ERROR("Write interface token failed.");
197         return ERR_INVALID_OPERATION;
198     }
199     if (!data.WriteInt32(missionId)) {
200         HILOG_ERROR("missionId write failed.");
201         return ERR_INVALID_OPERATION;
202     }
203     int32_t ret = SendTransactCmd(
204         IAbilityManagerCollaborator::NOTIFY_MOVE_MISSION_TO_BACKGROUND, data, reply, option);
205     if (ret != NO_ERROR) {
206         HILOG_ERROR("Send request error: %{public}d", ret);
207         return ret;
208     }
209     return NO_ERROR;
210 }
211 
NotifyPreloadAbility(const std::string & bundleName)212 int32_t AbilityManagerCollaboratorProxy::NotifyPreloadAbility(const std::string &bundleName)
213 {
214     MessageParcel data;
215     MessageParcel reply;
216     MessageOption option(MessageOption::TF_ASYNC);
217 
218     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
219         HILOG_ERROR("Write interface token failed.");
220         return ERR_INVALID_OPERATION;
221     }
222     if (!data.WriteString16(Str8ToStr16(bundleName))) {
223         HILOG_ERROR("bundleName write failed.");
224         return ERR_INVALID_OPERATION;
225     }
226     auto remote = Remote();
227     if (!remote) {
228         HILOG_ERROR("remote is nullptr");
229         return ERR_INVALID_OPERATION;
230     }
231     int32_t ret = remote->SendRequest(
232         IAbilityManagerCollaborator::NOTIFY_PRELOAD_ABILITY, data, reply, option);
233     if (ret != NO_ERROR) {
234         HILOG_ERROR("Send request error: %{public}d", ret);
235         return ret;
236     }
237     return NO_ERROR;
238 }
239 
NotifyMoveMissionToForeground(int32_t missionId)240 int32_t AbilityManagerCollaboratorProxy::NotifyMoveMissionToForeground(int32_t missionId)
241 {
242     MessageParcel data;
243     MessageParcel reply;
244     MessageOption option(MessageOption::TF_ASYNC);
245 
246     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
247         HILOG_ERROR("Write interface token failed.");
248         return ERR_INVALID_OPERATION;
249     }
250     if (!data.WriteInt32(missionId)) {
251         HILOG_ERROR("missionId write failed.");
252         return ERR_INVALID_OPERATION;
253     }
254     int32_t ret = SendTransactCmd(
255         IAbilityManagerCollaborator::NOTIFY_MOVE_MISSION_TO_FOREGROUND, data, reply, option);
256     if (ret != NO_ERROR) {
257         HILOG_ERROR("Send request error: %{public}d", ret);
258         return ret;
259     }
260     return NO_ERROR;
261 }
262 
NotifyTerminateMission(int32_t missionId)263 int32_t AbilityManagerCollaboratorProxy::NotifyTerminateMission(int32_t missionId)
264 {
265     MessageParcel data;
266     MessageParcel reply;
267     MessageOption option(MessageOption::TF_ASYNC);
268 
269     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
270         HILOG_ERROR("Write interface token failed.");
271         return ERR_INVALID_OPERATION;
272     }
273     if (!data.WriteInt32(missionId)) {
274         HILOG_ERROR("missionId write failed.");
275         return ERR_INVALID_OPERATION;
276     }
277     int32_t ret = SendTransactCmd(
278         IAbilityManagerCollaborator::NOTIFY_TERMINATE_MISSION, data, reply, option);
279     if (ret != NO_ERROR) {
280         HILOG_ERROR("Send request error: %{public}d", ret);
281         return ret;
282     }
283     return NO_ERROR;
284 }
285 
NotifyClearMission(int32_t missionId)286 int32_t AbilityManagerCollaboratorProxy::NotifyClearMission(int32_t missionId)
287 {
288     MessageParcel data;
289     MessageParcel reply;
290     MessageOption option(MessageOption::TF_SYNC);
291 
292     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
293         HILOG_ERROR("Write interface token failed.");
294         return ERR_INVALID_OPERATION;
295     }
296     if (!data.WriteInt32(missionId)) {
297         HILOG_ERROR("missionId write failed.");
298         return ERR_INVALID_OPERATION;
299     }
300     int32_t ret = SendTransactCmd(
301         IAbilityManagerCollaborator::NOTIFY_CLEAR_MISSION, data, reply, option);
302     if (ret != NO_ERROR) {
303         HILOG_ERROR("Send request error: %{public}d", ret);
304         return ret;
305     }
306     return NO_ERROR;
307 }
308 
NotifyRemoveShellProcess(int32_t pid,int32_t type,const std::string & reason)309 int32_t AbilityManagerCollaboratorProxy::NotifyRemoveShellProcess(int32_t pid, int32_t type, const std::string &reason)
310 {
311     MessageParcel data;
312     MessageParcel reply;
313     MessageOption option(MessageOption::TF_ASYNC);
314 
315     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
316         HILOG_ERROR("Write interface token failed.");
317         return ERR_INVALID_OPERATION;
318     }
319     if (!data.WriteInt32(pid)) {
320         HILOG_ERROR("pid write failed.");
321         return ERR_INVALID_OPERATION;
322     }
323     if (!data.WriteInt32(type)) {
324         HILOG_ERROR("type write failed.");
325         return ERR_INVALID_OPERATION;
326     }
327     if (!data.WriteString16(Str8ToStr16(reason))) {
328         HILOG_ERROR("reason write failed.");
329         return ERR_INVALID_OPERATION;
330     }
331     int32_t ret = SendTransactCmd(
332         IAbilityManagerCollaborator::NOTIFY_REMOVE_SHELL_PROCESS, data, reply, option);
333     if (ret != NO_ERROR) {
334         HILOG_ERROR("Send request error: %{public}d", ret);
335         return ret;
336     }
337     return NO_ERROR;
338 }
339 
UpdateMissionInfo(InnerMissionInfoDto & info)340 void AbilityManagerCollaboratorProxy::UpdateMissionInfo(InnerMissionInfoDto &info)
341 {
342     MessageParcel data;
343     MessageParcel reply;
344     MessageOption option;
345 
346     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
347         HILOG_ERROR("Write interface token failed.");
348         return;
349     }
350 
351     if (!data.WriteParcelable(&info)) {
352         HILOG_ERROR("write mission info failed.");
353         return;
354     }
355 
356     int32_t ret = SendTransactCmd(IAbilityManagerCollaborator::UPDATE_MISSION_INFO, data, reply, option);
357     if (ret != NO_ERROR) {
358         HILOG_ERROR("Send request error: %{public}d", ret);
359         return;
360     }
361 
362     std::unique_ptr<InnerMissionInfoDto> innerInfo(reply.ReadParcelable<InnerMissionInfoDto>());
363     if (!innerInfo) {
364         HILOG_ERROR("Get InnerMissionInfoDto error.");
365         return;
366     }
367     info = *innerInfo;
368     return;
369 }
370 
UpdateMissionInfo(sptr<SessionInfo> & sessionInfo)371 void AbilityManagerCollaboratorProxy::UpdateMissionInfo(sptr<SessionInfo> &sessionInfo)
372 {
373     MessageParcel data;
374     MessageParcel reply;
375     MessageOption option;
376 
377     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
378         HILOG_ERROR("Write interface token failed.");
379         return;
380     }
381 
382     if (sessionInfo) {
383         if (!data.WriteBool(true) || !data.WriteParcelable(sessionInfo)) {
384             HILOG_ERROR("flag and sessionInfo write failed.");
385             return;
386         }
387     } else {
388         if (!data.WriteBool(false)) {
389             HILOG_ERROR("flag write failed.");
390             return;
391         }
392     }
393 
394     int32_t ret = SendTransactCmd(IAbilityManagerCollaborator::UPDATE_MISSION_INFO_BY_SCB, data, reply, option);
395     if (ret != NO_ERROR) {
396         HILOG_ERROR("Send request error: %{public}d", ret);
397         return;
398     }
399 
400     sessionInfo = reply.ReadParcelable<SessionInfo>();
401     return;
402 }
403 
CheckCallAbilityPermission(const Want & want)404 int32_t AbilityManagerCollaboratorProxy::CheckCallAbilityPermission(const Want &want)
405 {
406     MessageParcel data;
407     MessageParcel reply;
408     MessageOption option;
409 
410     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
411         HILOG_ERROR("Write interface token failed.");
412         return ERR_INVALID_OPERATION;
413     }
414 
415     if (!data.WriteParcelable(&want)) {
416         HILOG_ERROR("want write failed.");
417         return ERR_INVALID_OPERATION;
418     }
419     int32_t ret = SendTransactCmd(IAbilityManagerCollaborator::CHECK_CALL_ABILITY_PERMISSION,
420         data, reply, option);
421     if (ret != NO_ERROR) {
422         HILOG_ERROR("Send request error: %{public}d", ret);
423         return ret;
424     }
425     return reply.ReadInt32();
426 }
427 
UpdateConfiguration(const AppExecFwk::Configuration & config,int32_t userId)428 bool AbilityManagerCollaboratorProxy::UpdateConfiguration(const AppExecFwk::Configuration &config, int32_t userId)
429 {
430     MessageParcel data;
431     MessageParcel reply;
432     MessageOption option(MessageOption::TF_ASYNC);
433     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
434         HILOG_ERROR("Write interface token failed.");
435         return false;
436     }
437     if (!data.WriteParcelable(&config)) {
438         HILOG_ERROR("Write config failed.");
439         return false;
440     }
441     if (!data.WriteInt32(userId)) {
442         HILOG_ERROR("Write usr failed.");
443         return false;
444     }
445     auto error = SendTransactCmd(IAbilityManagerCollaborator::UPDATE_CONFIGURATION, data, reply, option);
446     if (error != NO_ERROR) {
447         HILOG_ERROR("Send config error: %{public}d", error);
448         return true;
449     }
450     return true;
451 }
452 
OpenFile(const Uri & uri,uint32_t flag)453 int32_t AbilityManagerCollaboratorProxy::OpenFile(const Uri& uri, uint32_t flag)
454 {
455     MessageParcel data;
456     MessageParcel reply;
457     MessageOption option;
458     if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) {
459         HILOG_ERROR("Write interface token failed.");
460         return false;
461     }
462     if (!data.WriteParcelable(&uri)) {
463         HILOG_ERROR("Write uri failed.");
464         return false;
465     }
466     if (!data.WriteInt32(flag)) {
467         HILOG_ERROR("Write flag failed.");
468         return false;
469     }
470 
471     int32_t ret = SendTransactCmd(IAbilityManagerCollaborator::OPEN_FILE, data, reply, option);
472     if (ret != NO_ERROR) {
473         HILOG_ERROR("Send request error: %{public}d", ret);
474         return -1;
475     }
476     return reply.ReadFileDescriptor();
477 }
478 
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)479 int32_t AbilityManagerCollaboratorProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
480     MessageParcel &reply, MessageOption &option)
481 {
482     sptr<IRemoteObject> remote = Remote();
483     if (remote == nullptr) {
484         HILOG_ERROR("Remote is nullptr.");
485         return ERR_NULL_OBJECT;
486     }
487 
488     return remote->SendRequest(code, data, reply, option);
489 }
490 }   // namespace AAFwk
491 }   // namespace OHOS
492