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 "system_ability_manager_proxy.h"
17
18 #include <unistd.h>
19 #include <vector>
20
21 #include "errors.h"
22 #include "ipc_types.h"
23 #include "iremote_object.h"
24 #include "isystem_ability_load_callback.h"
25 #include "isystem_ability_status_change.h"
26 #include "message_option.h"
27 #include "message_parcel.h"
28 #include "refbase.h"
29 #include "sam_log.h"
30 #include "string_ex.h"
31
32 using namespace std;
33 namespace OHOS {
34 namespace {
35 const int32_t RETRY_TIME_OUT_NUMBER = 10;
36 const int32_t SLEEP_INTERVAL_TIME = 100;
37 const int32_t SLEEP_ONE_MILLI_SECOND_TIME = 1000;
38 }
GetSystemAbility(int32_t systemAbilityId)39 sptr<IRemoteObject> SystemAbilityManagerProxy::GetSystemAbility(int32_t systemAbilityId)
40 {
41 return GetSystemAbilityWrapper(systemAbilityId);
42 }
43
GetSystemAbility(int32_t systemAbilityId,const std::string & deviceId)44 sptr<IRemoteObject> SystemAbilityManagerProxy::GetSystemAbility(int32_t systemAbilityId,
45 const std::string& deviceId)
46 {
47 return GetSystemAbilityWrapper(systemAbilityId, deviceId);
48 }
49
GetSystemAbilityWrapper(int32_t systemAbilityId,const string & deviceId)50 sptr<IRemoteObject> SystemAbilityManagerProxy::GetSystemAbilityWrapper(int32_t systemAbilityId, const string& deviceId)
51 {
52 if (!CheckInputSysAbilityId(systemAbilityId)) {
53 HILOGW("GetSystemAbilityWrapper systemAbilityId invalid:%{public}d!", systemAbilityId);
54 return nullptr;
55 }
56
57 bool isExist = false;
58 int32_t timeout = RETRY_TIME_OUT_NUMBER;
59 HILOGD("GetSystemAbilityWrapper:Waiting for sa %{public}d, ", systemAbilityId);
60 do {
61 sptr<IRemoteObject> svc;
62 if (deviceId.empty()) {
63 svc = CheckSystemAbility(systemAbilityId, isExist);
64 if (!isExist) {
65 HILOGW("%{public}s:sa %{public}d is not exist", __func__, systemAbilityId);
66 usleep(SLEEP_ONE_MILLI_SECOND_TIME * SLEEP_INTERVAL_TIME);
67 continue;
68 }
69 } else {
70 svc = CheckSystemAbility(systemAbilityId, deviceId);
71 }
72
73 if (svc != nullptr) {
74 return svc;
75 }
76 usleep(SLEEP_ONE_MILLI_SECOND_TIME * SLEEP_INTERVAL_TIME);
77 } while (timeout--);
78 HILOGE("GetSystemAbilityWrapper sa %{public}d didn't start. Returning nullptr", systemAbilityId);
79 return nullptr;
80 }
81
CheckSystemAbilityWrapper(int32_t code,MessageParcel & data)82 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbilityWrapper(int32_t code, MessageParcel& data)
83 {
84 auto remote = Remote();
85 if (remote == nullptr) {
86 HILOGI("GetSystemAbilityWrapper remote is nullptr !");
87 return nullptr;
88 }
89 MessageParcel reply;
90 MessageOption option;
91 int32_t err = remote->SendRequest(code, data, reply, option);
92 if (err != ERR_NONE) {
93 return nullptr;
94 }
95 return reply.ReadRemoteObject();
96 }
97
CheckSystemAbility(int32_t systemAbilityId)98 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbility(int32_t systemAbilityId)
99 {
100 HILOGD("%{public}s called", __func__);
101 if (!CheckInputSysAbilityId(systemAbilityId)) {
102 HILOGW("systemAbilityId:%{public}d invalid!", systemAbilityId);
103 return nullptr;
104 }
105 MessageParcel data;
106 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
107 return nullptr;
108 }
109 bool ret = data.WriteInt32(systemAbilityId);
110 if (!ret) {
111 HILOGW("CheckSystemAbility Write systemAbilityId failed!");
112 return nullptr;
113 }
114 return CheckSystemAbilityWrapper(CHECK_SYSTEM_ABILITY_TRANSACTION, data);
115 }
116
CheckSystemAbility(int32_t systemAbilityId,const std::string & deviceId)117 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
118 {
119 if (!CheckInputSysAbilityId(systemAbilityId) || deviceId.empty()) {
120 HILOGW("CheckSystemAbility:systemAbilityId:%{public}d or deviceId is nullptr.", systemAbilityId);
121 return nullptr;
122 }
123
124 HILOGD("CheckSystemAbility: ability id is : %{public}d, deviceId is %{private}s", systemAbilityId,
125 deviceId.c_str());
126
127 auto remote = Remote();
128 if (remote == nullptr) {
129 HILOGE("CheckSystemAbility remote is nullptr !");
130 return nullptr;
131 }
132
133 MessageParcel data;
134 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
135 return nullptr;
136 }
137 bool ret = data.WriteInt32(systemAbilityId);
138 if (!ret) {
139 HILOGE("CheckSystemAbility parcel write name failed");
140 return nullptr;
141 }
142 ret = data.WriteString(deviceId);
143 if (!ret) {
144 HILOGE("CheckSystemAbility parcel write deviceId failed");
145 return nullptr;
146 }
147
148 return CheckSystemAbilityWrapper(CHECK_REMOTE_SYSTEM_ABILITY_TRANSACTION, data);
149 }
150
CheckSystemAbility(int32_t systemAbilityId,bool & isExist)151 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbility(int32_t systemAbilityId, bool& isExist)
152 {
153 HILOGD("%{public}s called, ability id is %{public}d, isExist is %{public}d", __func__, systemAbilityId, isExist);
154 if (!CheckInputSysAbilityId(systemAbilityId)) {
155 HILOGW("CheckSystemAbility:systemAbilityId:%{public}d invalid!", systemAbilityId);
156 return nullptr;
157 }
158
159 auto remote = Remote();
160 if (remote == nullptr) {
161 HILOGE("CheckSystemAbility remote is nullptr !");
162 return nullptr;
163 }
164
165 MessageParcel data;
166 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
167 return nullptr;
168 }
169 bool ret = data.WriteInt32(systemAbilityId);
170 if (!ret) {
171 HILOGW("CheckSystemAbility Write systemAbilityId failed!");
172 return nullptr;
173 }
174
175 ret = data.WriteBool(isExist);
176 if (!ret) {
177 HILOGW("CheckSystemAbility Write isExist failed!");
178 return nullptr;
179 }
180
181 MessageParcel reply;
182 MessageOption option;
183 int32_t err = remote->SendRequest(CHECK_SYSTEM_ABILITY_IMMEDIATELY_TRANSACTION, data, reply, option);
184 if (err != ERR_NONE) {
185 return nullptr;
186 }
187 sptr<IRemoteObject> irsp(reply.ReadRemoteObject());
188
189 ret = reply.ReadBool(isExist);
190 if (!ret) {
191 HILOGW("CheckSystemAbility Read isExist failed!");
192 return nullptr;
193 }
194
195 return irsp;
196 }
197
AddOnDemandSystemAbilityInfo(int32_t systemAbilityId,const std::u16string & localAbilityManagerName)198 int32_t SystemAbilityManagerProxy::AddOnDemandSystemAbilityInfo(int32_t systemAbilityId,
199 const std::u16string& localAbilityManagerName)
200 {
201 HILOGD("%{public}s called, system ability name is : %{public}d ", __func__, systemAbilityId);
202 if (!CheckInputSysAbilityId(systemAbilityId) || localAbilityManagerName.empty()) {
203 HILOGI("AddOnDemandSystemAbilityInfo invalid params!");
204 return ERR_INVALID_VALUE;
205 }
206
207 auto remote = Remote();
208 if (remote == nullptr) {
209 HILOGE("AddOnDemandSystemAbilityInfo remote is nullptr !");
210 return ERR_INVALID_OPERATION;
211 }
212
213 MessageParcel data;
214 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
215 return ERR_FLATTEN_OBJECT;
216 }
217 bool ret = data.WriteInt32(systemAbilityId);
218 if (!ret) {
219 HILOGW("AddOnDemandSystemAbilityInfo Write systemAbilityId failed!");
220 return ERR_FLATTEN_OBJECT;
221 }
222
223 ret = data.WriteString16(localAbilityManagerName);
224 if (!ret) {
225 HILOGW("AddOnDemandSystemAbilityInfo Write localAbilityManagerName failed!");
226 return ERR_FLATTEN_OBJECT;
227 }
228
229 MessageParcel reply;
230 MessageOption option;
231 int32_t err = remote->SendRequest(ADD_ONDEMAND_SYSTEM_ABILITY_TRANSACTION, data, reply, option);
232
233 HILOGI("%{public}s:add ondemand system ability %{public}d %{public}s, return %{public}d",
234 __func__, systemAbilityId, err ? "fail" : "succ", err);
235 if (err != ERR_NONE) {
236 return err;
237 }
238
239 int32_t result = 0;
240 ret = reply.ReadInt32(result);
241 if (!ret) {
242 HILOGW("AddOnDemandSystemAbilityInfo Read result failed!");
243 return ERR_FLATTEN_OBJECT;
244 }
245 return result;
246 }
247
RemoveSystemAbilityWrapper(int32_t code,MessageParcel & data)248 int32_t SystemAbilityManagerProxy::RemoveSystemAbilityWrapper(int32_t code, MessageParcel& data)
249 {
250 sptr<IRemoteObject> remote = Remote();
251 if (remote == nullptr) {
252 HILOGI("remote is nullptr !");
253 return ERR_INVALID_OPERATION;
254 }
255 MessageParcel reply;
256 MessageOption option;
257 int32_t err = remote->SendRequest(code, data, reply, option);
258 if (err != ERR_NONE) {
259 HILOGE("RemoveSystemAbility SendRequest error:%{public}d!", err);
260 return err;
261 }
262
263 int32_t result = 0;
264 bool ret = reply.ReadInt32(result);
265 if (!ret) {
266 HILOGW("RemoveSystemAbility Read result failed!");
267 return ERR_FLATTEN_OBJECT;
268 }
269
270 return result;
271 }
272
RemoveSystemAbility(int32_t systemAbilityId)273 int32_t SystemAbilityManagerProxy::RemoveSystemAbility(int32_t systemAbilityId)
274 {
275 HILOGD("%{public}s called, systemabilityId : %{public}d", __func__, systemAbilityId);
276 if (!CheckInputSysAbilityId(systemAbilityId)) {
277 HILOGW("systemAbilityId:%{public}d is invalid!", systemAbilityId);
278 return ERR_INVALID_VALUE;
279 }
280
281 MessageParcel data;
282 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
283 return ERR_FLATTEN_OBJECT;
284 }
285 bool ret = data.WriteInt32(systemAbilityId);
286 if (!ret) {
287 HILOGW("RemoveSystemAbility Write systemAbilityId failed!");
288 return ERR_FLATTEN_OBJECT;
289 }
290 return RemoveSystemAbilityWrapper(REMOVE_SYSTEM_ABILITY_TRANSACTION, data);
291 }
292
ListSystemAbilities(unsigned int dumpFlags)293 std::vector<u16string> SystemAbilityManagerProxy::ListSystemAbilities(unsigned int dumpFlags)
294 {
295 HILOGD("%{public}s called", __func__);
296 std::vector<u16string> saNames;
297
298 sptr<IRemoteObject> remote = Remote();
299 if (remote == nullptr) {
300 HILOGI("remote is nullptr !");
301 return saNames;
302 }
303
304 MessageParcel data;
305 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
306 HILOGW("ListSystemAbilities write token failed!");
307 return saNames;
308 }
309 bool ret = data.WriteInt32(dumpFlags);
310 if (!ret) {
311 HILOGW("ListSystemAbilities write dumpFlags failed!");
312 return saNames;
313 }
314 MessageParcel reply;
315 MessageOption option;
316 int32_t err = remote->SendRequest(LIST_SYSTEM_ABILITY_TRANSACTION, data, reply, option);
317 if (err != ERR_NONE) {
318 HILOGW("ListSystemAbilities transact failed!");
319 return saNames;
320 }
321 if (reply.ReadInt32() != ERR_NONE) {
322 HILOGW("ListSystemAbilities remote failed!");
323 return saNames;
324 }
325 if (!reply.ReadString16Vector(&saNames)) {
326 HILOGW("ListSystemAbilities read reply failed");
327 saNames.clear();
328 }
329 return saNames;
330 }
331
SubscribeSystemAbility(int32_t systemAbilityId,const sptr<ISystemAbilityStatusChange> & listener)332 int32_t SystemAbilityManagerProxy::SubscribeSystemAbility(int32_t systemAbilityId,
333 const sptr<ISystemAbilityStatusChange>& listener)
334 {
335 HILOGI("%{public}s called, SubscribeSystemAbility systemAbilityId:%{public}d", __func__, systemAbilityId);
336 if (!CheckInputSysAbilityId(systemAbilityId) || listener == nullptr) {
337 HILOGE("SubscribeSystemAbility systemAbilityId:%{public}d or listener invalid!", systemAbilityId);
338 return ERR_INVALID_VALUE;
339 }
340
341 sptr<IRemoteObject> remote = Remote();
342 if (remote == nullptr) {
343 HILOGI("remote is nullptr !");
344 return ERR_INVALID_OPERATION;
345 }
346
347 MessageParcel data;
348 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
349 return ERR_FLATTEN_OBJECT;
350 }
351 bool ret = data.WriteInt32(systemAbilityId);
352 if (!ret) {
353 HILOGW("SubscribeSystemAbility Write systemAbilityId failed!");
354 return ERR_FLATTEN_OBJECT;
355 }
356
357 ret = data.WriteRemoteObject(listener->AsObject());
358 if (!ret) {
359 HILOGW("SubscribeSystemAbility Write listenerName failed!");
360 return ERR_FLATTEN_OBJECT;
361 }
362
363 MessageParcel reply;
364 MessageOption option;
365 int32_t err = remote->SendRequest(SUBSCRIBE_SYSTEM_ABILITY_TRANSACTION, data, reply, option);
366 if (err != ERR_NONE) {
367 HILOGE("SubscribeSystemAbility SendRequest error:%{public}d!", err);
368 return err;
369 }
370 HILOGI("SubscribeSystemAbility SendRequest succeed!");
371 int32_t result = 0;
372 ret = reply.ReadInt32(result);
373 if (!ret) {
374 HILOGW("SubscribeSystemAbility Read result failed!");
375 return ERR_FLATTEN_OBJECT;
376 }
377
378 return result;
379 }
380
UnSubscribeSystemAbility(int32_t systemAbilityId,const sptr<ISystemAbilityStatusChange> & listener)381 int32_t SystemAbilityManagerProxy::UnSubscribeSystemAbility(int32_t systemAbilityId,
382 const sptr<ISystemAbilityStatusChange>& listener)
383 {
384 HILOGI("%{public}s called, UnSubscribeSystemAbility systemAbilityId:%{public}d", __func__, systemAbilityId);
385 if (!CheckInputSysAbilityId(systemAbilityId) || listener == nullptr) {
386 HILOGE("UnSubscribeSystemAbility systemAbilityId:%{public}d or listener invalid!", systemAbilityId);
387 return ERR_INVALID_VALUE;
388 }
389
390 sptr<IRemoteObject> remote = Remote();
391 if (remote == nullptr) {
392 HILOGI("remote is nullptr !");
393 return ERR_INVALID_OPERATION;
394 }
395
396 MessageParcel data;
397 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
398 return ERR_FLATTEN_OBJECT;
399 }
400 bool ret = data.WriteInt32(systemAbilityId);
401 if (!ret) {
402 HILOGW("UnSubscribeSystemAbility Write systemAbilityId failed!");
403 return ERR_FLATTEN_OBJECT;
404 }
405
406 ret = data.WriteRemoteObject(listener->AsObject());
407 if (!ret) {
408 HILOGW("UnSubscribeSystemAbility Write listenerSaId failed!");
409 return ERR_FLATTEN_OBJECT;
410 }
411
412 MessageParcel reply;
413 MessageOption option;
414 int32_t err = remote->SendRequest(UNSUBSCRIBE_SYSTEM_ABILITY_TRANSACTION, data, reply, option);
415 if (err != ERR_NONE) {
416 HILOGE("UnSubscribeSystemAbility SendRequest error:%{public}d!", err);
417 return err;
418 }
419 HILOGI("UnSubscribeSystemAbility SendRequest succeed!");
420 int32_t result = 0;
421 ret = reply.ReadInt32(result);
422 if (!ret) {
423 HILOGW("UnSubscribeSystemAbility Read result failed!");
424 return ERR_FLATTEN_OBJECT;
425 }
426
427 return result;
428 }
429
LoadSystemAbility(int32_t systemAbilityId,const sptr<ISystemAbilityLoadCallback> & callback)430 int32_t SystemAbilityManagerProxy::LoadSystemAbility(int32_t systemAbilityId,
431 const sptr<ISystemAbilityLoadCallback>& callback)
432 {
433 if (!CheckInputSysAbilityId(systemAbilityId) || callback == nullptr) {
434 HILOGE("LoadSystemAbility systemAbilityId:%{public}d or callback invalid!", systemAbilityId);
435 return ERR_INVALID_VALUE;
436 }
437
438 sptr<IRemoteObject> remote = Remote();
439 if (remote == nullptr) {
440 HILOGE("LoadSystemAbility remote is null!");
441 return ERR_INVALID_OPERATION;
442 }
443
444 MessageParcel data;
445 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
446 HILOGW("LoadSystemAbility Write interface token failed!");
447 return ERR_FLATTEN_OBJECT;
448 }
449 bool ret = data.WriteInt32(systemAbilityId);
450 if (!ret) {
451 HILOGW("LoadSystemAbility Write systemAbilityId failed!");
452 return ERR_FLATTEN_OBJECT;
453 }
454 ret = data.WriteRemoteObject(callback->AsObject());
455 if (!ret) {
456 HILOGW("LoadSystemAbility Write callback failed!");
457 return ERR_FLATTEN_OBJECT;
458 }
459
460 MessageParcel reply;
461 MessageOption option;
462 int32_t err = remote->SendRequest(LOAD_SYSTEM_ABILITY_TRANSACTION, data, reply, option);
463 if (err != ERR_NONE) {
464 HILOGE("LoadSystemAbility systemAbilityId : %{public}d invalid error:%{public}d!", systemAbilityId, err);
465 return err;
466 }
467 HILOGI("LoadSystemAbility systemAbilityId : %{public}d, SendRequest succeed!", systemAbilityId);
468 int32_t result = 0;
469 ret = reply.ReadInt32(result);
470 if (!ret) {
471 HILOGW("LoadSystemAbility Read reply failed!");
472 return ERR_FLATTEN_OBJECT;
473 }
474 return result;
475 }
476
LoadSystemAbility(int32_t systemAbilityId,const std::string & deviceId,const sptr<ISystemAbilityLoadCallback> & callback)477 int32_t SystemAbilityManagerProxy::LoadSystemAbility(int32_t systemAbilityId, const std::string& deviceId,
478 const sptr<ISystemAbilityLoadCallback>& callback)
479 {
480 if (!CheckInputSysAbilityId(systemAbilityId) || deviceId.empty() || callback == nullptr) {
481 HILOGE("LoadSystemAbility systemAbilityId:%{public}d ,deviceId or callback invalid!", systemAbilityId);
482 return ERR_INVALID_VALUE;
483 }
484 sptr<IRemoteObject> remote = Remote();
485 if (remote == nullptr) {
486 HILOGE("LoadSystemAbility remote is null!");
487 return ERR_INVALID_OPERATION;
488 }
489
490 MessageParcel data;
491 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
492 HILOGW("LoadSystemAbility write interface token failed!");
493 return ERR_FLATTEN_OBJECT;
494 }
495 bool ret = data.WriteInt32(systemAbilityId);
496 if (!ret) {
497 HILOGW("LoadSystemAbility write systemAbilityId failed!");
498 return ERR_FLATTEN_OBJECT;
499 }
500 ret = data.WriteString(deviceId);
501 if (!ret) {
502 HILOGW("LoadSystemAbility write deviceId failed!");
503 return ERR_FLATTEN_OBJECT;
504 }
505 ret = data.WriteRemoteObject(callback->AsObject());
506 if (!ret) {
507 HILOGW("LoadSystemAbility Write callback failed!");
508 return ERR_FLATTEN_OBJECT;
509 }
510
511 MessageParcel reply;
512 MessageOption option;
513 int32_t err = remote->SendRequest(LOAD_REMOTE_SYSTEM_ABILITY_TRANSACTION, data, reply, option);
514 if (err != ERR_NONE) {
515 HILOGE("LoadSystemAbility systemAbilityId : %{public}d invalid error:%{public}d!", systemAbilityId, err);
516 return err;
517 }
518 HILOGD("LoadSystemAbility systemAbilityId : %{public}d for remote, SendRequest succeed!", systemAbilityId);
519 int32_t result = 0;
520 ret = reply.ReadInt32(result);
521 if (!ret) {
522 HILOGW("LoadSystemAbility read reply failed for remote!");
523 return ERR_FLATTEN_OBJECT;
524 }
525 return result;
526 }
527
MarshalSAExtraProp(const SAExtraProp & extraProp,MessageParcel & data) const528 int32_t SystemAbilityManagerProxy::MarshalSAExtraProp(const SAExtraProp& extraProp, MessageParcel& data) const
529 {
530 if (!data.WriteBool(extraProp.isDistributed)) {
531 HILOGW("MarshalSAExtraProp Write isDistributed failed!");
532 return ERR_FLATTEN_OBJECT;
533 }
534 if (!data.WriteInt32(extraProp.dumpFlags)) {
535 HILOGW("MarshalSAExtraProp Write dumpFlags failed!");
536 return ERR_FLATTEN_OBJECT;
537 }
538 if (!data.WriteString16(extraProp.capability)) {
539 HILOGW("MarshalSAExtraProp Write capability failed!");
540 return ERR_FLATTEN_OBJECT;
541 }
542 if (!data.WriteString16(extraProp.permission)) {
543 HILOGW("MarshalSAExtraProp Write defPermission failed!");
544 return ERR_FLATTEN_OBJECT;
545 }
546 return ERR_OK;
547 }
548
AddSystemAbility(int32_t systemAbilityId,const sptr<IRemoteObject> & ability,const SAExtraProp & extraProp)549 int32_t SystemAbilityManagerProxy::AddSystemAbility(int32_t systemAbilityId, const sptr<IRemoteObject>& ability,
550 const SAExtraProp& extraProp)
551 {
552 HILOGD("%{public}s called, systemAbilityId is %{public}d", __func__, systemAbilityId);
553 if (!CheckInputSysAbilityId(systemAbilityId)) {
554 HILOGW("systemAbilityId:%{public}d invalid.", systemAbilityId);
555 return ERR_INVALID_VALUE;
556 }
557
558 MessageParcel data;
559 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
560 return ERR_FLATTEN_OBJECT;
561 }
562 if (!data.WriteInt32(systemAbilityId)) {
563 HILOGW("AddSystemAbility Write saId failed!");
564 return ERR_FLATTEN_OBJECT;
565 }
566 if (!data.WriteRemoteObject(ability)) {
567 HILOGW("AddSystemAbility Write ability failed!");
568 return ERR_FLATTEN_OBJECT;
569 }
570
571 int32_t ret = MarshalSAExtraProp(extraProp, data);
572 if (ret != ERR_OK) {
573 HILOGW("AddSystemAbility MarshalSAExtraProp failed!");
574 return ret;
575 }
576 return AddSystemAbilityWrapper(ADD_SYSTEM_ABILITY_TRANSACTION, data);
577 }
578
AddSystemAbilityWrapper(int32_t code,MessageParcel & data)579 int32_t SystemAbilityManagerProxy::AddSystemAbilityWrapper(int32_t code, MessageParcel& data)
580 {
581 sptr<IRemoteObject> remote = Remote();
582 if (remote == nullptr) {
583 HILOGI("remote is nullptr !");
584 return ERR_INVALID_OPERATION;
585 }
586
587 MessageParcel reply;
588 MessageOption option;
589 int32_t err = remote->SendRequest(code, data, reply, option);
590 if (err != ERR_NONE) {
591 HILOGE("AddSystemAbility SA invalid error:%{public}d!", err);
592 return err;
593 }
594 int32_t result = 0;
595 bool ret = reply.ReadInt32(result);
596 if (!ret) {
597 HILOGE("AddSystemAbility read result error!");
598 return ERR_FLATTEN_OBJECT;
599 }
600 return result;
601 }
602
AddSystemProcess(const u16string & procName,const sptr<IRemoteObject> & procObject)603 int32_t SystemAbilityManagerProxy::AddSystemProcess(const u16string& procName, const sptr<IRemoteObject>& procObject)
604 {
605 HILOGD("%{public}s called, process name is %{public}s", __func__, Str16ToStr8(procName).c_str());
606 if (procName.empty()) {
607 HILOGI("process name is invalid!");
608 return ERR_INVALID_VALUE;
609 }
610
611 MessageParcel data;
612 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
613 return ERR_FLATTEN_OBJECT;
614 }
615 if (!data.WriteString16(procName)) {
616 HILOGW("AddSystemProcess Write name failed!");
617 return ERR_FLATTEN_OBJECT;
618 }
619
620 if (!data.WriteRemoteObject(procObject)) {
621 HILOGW("AddSystemProcess Write ability failed!");
622 return ERR_FLATTEN_OBJECT;
623 }
624 return AddSystemAbilityWrapper(ADD_SYSTEM_PROCESS_TRANSACTION, data);
625 }
626 } // namespace OHOS
627