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