1 /*
2 * Copyright (c) 2021-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 "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 #include "local_abilitys.h"
33
34 using namespace std;
35 namespace OHOS {
36 namespace {
37 const int32_t RETRY_TIME_OUT_NUMBER = 10;
38 const int32_t SLEEP_INTERVAL_TIME = 100;
39 const int32_t SLEEP_ONE_MILLI_SECOND_TIME = 1000;
40 }
GetSystemAbility(int32_t systemAbilityId)41 sptr<IRemoteObject> SystemAbilityManagerProxy::GetSystemAbility(int32_t systemAbilityId)
42 {
43 return GetSystemAbilityWrapper(systemAbilityId);
44 }
45
GetSystemAbility(int32_t systemAbilityId,const std::string & deviceId)46 sptr<IRemoteObject> SystemAbilityManagerProxy::GetSystemAbility(int32_t systemAbilityId,
47 const std::string& deviceId)
48 {
49 return GetSystemAbilityWrapper(systemAbilityId, deviceId);
50 }
51
GetSystemAbilityWrapper(int32_t systemAbilityId,const string & deviceId)52 sptr<IRemoteObject> SystemAbilityManagerProxy::GetSystemAbilityWrapper(int32_t systemAbilityId, const string& deviceId)
53 {
54 if (!CheckInputSysAbilityId(systemAbilityId)) {
55 HILOGW("GetSystemAbilityWrapper systemAbilityId invalid:%{public}d!", systemAbilityId);
56 return nullptr;
57 }
58
59 bool isExist = false;
60 int32_t timeout = RETRY_TIME_OUT_NUMBER;
61 HILOGD("GetSystemAbilityWrapper:Waiting for sa %{public}d, ", systemAbilityId);
62 do {
63 sptr<IRemoteObject> svc;
64 if (deviceId.empty()) {
65 svc = CheckSystemAbility(systemAbilityId, isExist);
66 if (!isExist) {
67 HILOGW("%{public}s:sa %{public}d is not exist", __func__, systemAbilityId);
68 usleep(SLEEP_ONE_MILLI_SECOND_TIME * SLEEP_INTERVAL_TIME);
69 continue;
70 }
71 } else {
72 svc = CheckSystemAbility(systemAbilityId, deviceId);
73 }
74
75 if (svc != nullptr) {
76 return svc;
77 }
78 usleep(SLEEP_ONE_MILLI_SECOND_TIME * SLEEP_INTERVAL_TIME);
79 } while (timeout--);
80 HILOGE("GetSystemAbilityWrapper sa %{public}d didn't start. Returning nullptr", systemAbilityId);
81 return nullptr;
82 }
83
CheckSystemAbilityWrapper(int32_t code,MessageParcel & data)84 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbilityWrapper(int32_t code, MessageParcel& data)
85 {
86 auto remote = Remote();
87 if (remote == nullptr) {
88 HILOGI("GetSystemAbilityWrapper remote is nullptr !");
89 return nullptr;
90 }
91 MessageParcel reply;
92 MessageOption option;
93 int32_t err = remote->SendRequest(code, data, reply, option);
94 if (err != ERR_NONE) {
95 return nullptr;
96 }
97 return reply.ReadRemoteObject();
98 }
99
CheckSystemAbility(int32_t systemAbilityId)100 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbility(int32_t systemAbilityId)
101 {
102 HILOGD("%{public}s called", __func__);
103 if (!CheckInputSysAbilityId(systemAbilityId)) {
104 HILOGW("systemAbilityId:%{public}d invalid!", systemAbilityId);
105 return nullptr;
106 }
107
108 auto proxy = LocalAbilitys::GetInstance().GetAbility(systemAbilityId);
109 if (proxy != nullptr) {
110 return proxy;
111 }
112
113 MessageParcel data;
114 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
115 return nullptr;
116 }
117 bool ret = data.WriteInt32(systemAbilityId);
118 if (!ret) {
119 HILOGW("CheckSystemAbility Write systemAbilityId failed!");
120 return nullptr;
121 }
122 return CheckSystemAbilityWrapper(
123 static_cast<uint32_t>(SamgrInterfaceCode::CHECK_SYSTEM_ABILITY_TRANSACTION), data);
124 }
125
CheckSystemAbility(int32_t systemAbilityId,const std::string & deviceId)126 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
127 {
128 if (!CheckInputSysAbilityId(systemAbilityId) || deviceId.empty()) {
129 HILOGW("CheckSystemAbility:systemAbilityId:%{public}d or deviceId is nullptr.", systemAbilityId);
130 return nullptr;
131 }
132
133 HILOGD("CheckSystemAbility: ability id is : %{public}d, deviceId is %{private}s", systemAbilityId,
134 deviceId.c_str());
135
136 auto remote = Remote();
137 if (remote == nullptr) {
138 HILOGE("CheckSystemAbility remote is nullptr !");
139 return nullptr;
140 }
141
142 MessageParcel data;
143 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
144 return nullptr;
145 }
146 bool ret = data.WriteInt32(systemAbilityId);
147 if (!ret) {
148 HILOGE("CheckSystemAbility parcel write name failed");
149 return nullptr;
150 }
151 ret = data.WriteString(deviceId);
152 if (!ret) {
153 HILOGE("CheckSystemAbility parcel write deviceId failed");
154 return nullptr;
155 }
156
157 return CheckSystemAbilityWrapper(
158 static_cast<uint32_t>(SamgrInterfaceCode::CHECK_REMOTE_SYSTEM_ABILITY_TRANSACTION), data);
159 }
160
CheckSystemAbility(int32_t systemAbilityId,bool & isExist)161 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbility(int32_t systemAbilityId, bool& isExist)
162 {
163 HILOGD("%{public}s called, ability id is %{public}d, isExist is %{public}d", __func__, systemAbilityId, isExist);
164 if (!CheckInputSysAbilityId(systemAbilityId)) {
165 HILOGW("CheckSystemAbility:systemAbilityId:%{public}d invalid!", systemAbilityId);
166 return nullptr;
167 }
168
169 auto proxy = LocalAbilitys::GetInstance().GetAbility(systemAbilityId);
170 if (proxy != nullptr) {
171 isExist = true;
172 return proxy;
173 }
174
175 auto remote = Remote();
176 if (remote == nullptr) {
177 HILOGE("CheckSystemAbility remote is nullptr !");
178 return nullptr;
179 }
180
181 MessageParcel data;
182 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
183 return nullptr;
184 }
185 bool ret = data.WriteInt32(systemAbilityId);
186 if (!ret) {
187 HILOGW("CheckSystemAbility Write systemAbilityId failed!");
188 return nullptr;
189 }
190
191 ret = data.WriteBool(isExist);
192 if (!ret) {
193 HILOGW("CheckSystemAbility Write isExist failed!");
194 return nullptr;
195 }
196
197 MessageParcel reply;
198 MessageOption option;
199 int32_t err = remote->SendRequest(
200 static_cast<uint32_t>(SamgrInterfaceCode::CHECK_SYSTEM_ABILITY_IMMEDIATELY_TRANSACTION), data, reply, option);
201 if (err != ERR_NONE) {
202 return nullptr;
203 }
204 sptr<IRemoteObject> irsp(reply.ReadRemoteObject());
205
206 ret = reply.ReadBool(isExist);
207 if (!ret) {
208 HILOGW("CheckSystemAbility Read isExist failed!");
209 return nullptr;
210 }
211
212 return irsp;
213 }
214
AddOnDemandSystemAbilityInfo(int32_t systemAbilityId,const std::u16string & localAbilityManagerName)215 int32_t SystemAbilityManagerProxy::AddOnDemandSystemAbilityInfo(int32_t systemAbilityId,
216 const std::u16string& localAbilityManagerName)
217 {
218 HILOGD("%{public}s called, system ability name is : %{public}d ", __func__, systemAbilityId);
219 if (!CheckInputSysAbilityId(systemAbilityId) || localAbilityManagerName.empty()) {
220 HILOGI("AddOnDemandSystemAbilityInfo invalid params!");
221 return ERR_INVALID_VALUE;
222 }
223
224 auto remote = Remote();
225 if (remote == nullptr) {
226 HILOGE("AddOnDemandSystemAbilityInfo remote is nullptr !");
227 return ERR_INVALID_OPERATION;
228 }
229
230 MessageParcel data;
231 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
232 return ERR_FLATTEN_OBJECT;
233 }
234 bool ret = data.WriteInt32(systemAbilityId);
235 if (!ret) {
236 HILOGW("AddOnDemandSystemAbilityInfo Write systemAbilityId failed!");
237 return ERR_FLATTEN_OBJECT;
238 }
239
240 ret = data.WriteString16(localAbilityManagerName);
241 if (!ret) {
242 HILOGW("AddOnDemandSystemAbilityInfo Write localAbilityManagerName failed!");
243 return ERR_FLATTEN_OBJECT;
244 }
245
246 MessageParcel reply;
247 MessageOption option;
248 int32_t err = remote->SendRequest(
249 static_cast<uint32_t>(SamgrInterfaceCode::ADD_ONDEMAND_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
250
251 HILOGI("%{public}s:add ondemand system ability %{public}d %{public}s, return %{public}d",
252 __func__, systemAbilityId, err ? "fail" : "succ", err);
253 if (err != ERR_NONE) {
254 return err;
255 }
256
257 int32_t result = 0;
258 ret = reply.ReadInt32(result);
259 if (!ret) {
260 HILOGW("AddOnDemandSystemAbilityInfo Read result failed!");
261 return ERR_FLATTEN_OBJECT;
262 }
263 return result;
264 }
265
RemoveSystemAbilityWrapper(int32_t code,MessageParcel & data)266 int32_t SystemAbilityManagerProxy::RemoveSystemAbilityWrapper(int32_t code, MessageParcel& data)
267 {
268 sptr<IRemoteObject> remote = Remote();
269 if (remote == nullptr) {
270 HILOGI("remote is nullptr !");
271 return ERR_INVALID_OPERATION;
272 }
273 MessageParcel reply;
274 MessageOption option;
275 int32_t err = remote->SendRequest(code, data, reply, option);
276 if (err != ERR_NONE) {
277 HILOGE("RemoveSystemAbility SendRequest error:%{public}d!", err);
278 return err;
279 }
280
281 int32_t result = 0;
282 bool ret = reply.ReadInt32(result);
283 if (!ret) {
284 HILOGW("RemoveSystemAbility Read result failed!");
285 return ERR_FLATTEN_OBJECT;
286 }
287
288 return result;
289 }
290
RemoveSystemAbility(int32_t systemAbilityId)291 int32_t SystemAbilityManagerProxy::RemoveSystemAbility(int32_t systemAbilityId)
292 {
293 HILOGD("%{public}s called, systemabilityId : %{public}d", __func__, systemAbilityId);
294 if (!CheckInputSysAbilityId(systemAbilityId)) {
295 HILOGW("systemAbilityId:%{public}d is invalid!", systemAbilityId);
296 return ERR_INVALID_VALUE;
297 }
298
299 MessageParcel data;
300 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
301 return ERR_FLATTEN_OBJECT;
302 }
303 bool ret = data.WriteInt32(systemAbilityId);
304 if (!ret) {
305 HILOGW("RemoveSystemAbility Write systemAbilityId failed!");
306 return ERR_FLATTEN_OBJECT;
307 }
308
309 int32_t result = RemoveSystemAbilityWrapper(
310 static_cast<uint32_t>(SamgrInterfaceCode::REMOVE_SYSTEM_ABILITY_TRANSACTION), data);
311 if (result == ERR_OK) {
312 LocalAbilitys::GetInstance().RemoveAbility(systemAbilityId);
313 }
314 return result;
315 }
316
ListSystemAbilities(unsigned int dumpFlags)317 std::vector<u16string> SystemAbilityManagerProxy::ListSystemAbilities(unsigned int dumpFlags)
318 {
319 HILOGD("%{public}s called", __func__);
320 std::vector<u16string> saNames;
321
322 sptr<IRemoteObject> remote = Remote();
323 if (remote == nullptr) {
324 HILOGI("remote is nullptr !");
325 return saNames;
326 }
327
328 MessageParcel data;
329 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
330 HILOGW("ListSystemAbilities write token failed!");
331 return saNames;
332 }
333 bool ret = data.WriteInt32(dumpFlags);
334 if (!ret) {
335 HILOGW("ListSystemAbilities write dumpFlags failed!");
336 return saNames;
337 }
338 MessageParcel reply;
339 MessageOption option;
340 int32_t err = remote->SendRequest(
341 static_cast<uint32_t>(SamgrInterfaceCode::LIST_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
342 if (err != ERR_NONE) {
343 HILOGW("ListSystemAbilities transact failed!");
344 return saNames;
345 }
346 if (reply.ReadInt32() != ERR_NONE) {
347 HILOGW("ListSystemAbilities remote failed!");
348 return saNames;
349 }
350 if (!reply.ReadString16Vector(&saNames)) {
351 HILOGW("ListSystemAbilities read reply failed");
352 saNames.clear();
353 }
354 return saNames;
355 }
356
SubscribeSystemAbility(int32_t systemAbilityId,const sptr<ISystemAbilityStatusChange> & listener)357 int32_t SystemAbilityManagerProxy::SubscribeSystemAbility(int32_t systemAbilityId,
358 const sptr<ISystemAbilityStatusChange>& listener)
359 {
360 HILOGI("%{public}s called, SubscribeSystemAbility systemAbilityId:%{public}d", __func__, systemAbilityId);
361 if (!CheckInputSysAbilityId(systemAbilityId) || listener == nullptr) {
362 HILOGE("SubscribeSystemAbility systemAbilityId:%{public}d or listener invalid!", systemAbilityId);
363 return ERR_INVALID_VALUE;
364 }
365
366 sptr<IRemoteObject> remote = Remote();
367 if (remote == nullptr) {
368 HILOGI("remote is nullptr !");
369 return ERR_INVALID_OPERATION;
370 }
371
372 MessageParcel data;
373 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
374 return ERR_FLATTEN_OBJECT;
375 }
376 bool ret = data.WriteInt32(systemAbilityId);
377 if (!ret) {
378 HILOGW("SubscribeSystemAbility Write systemAbilityId failed!");
379 return ERR_FLATTEN_OBJECT;
380 }
381
382 ret = data.WriteRemoteObject(listener->AsObject());
383 if (!ret) {
384 HILOGW("SubscribeSystemAbility Write listenerName failed!");
385 return ERR_FLATTEN_OBJECT;
386 }
387
388 MessageParcel reply;
389 MessageOption option;
390 int32_t err = remote->SendRequest(
391 static_cast<uint32_t>(SamgrInterfaceCode::SUBSCRIBE_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
392 if (err != ERR_NONE) {
393 HILOGE("SubscribeSystemAbility SendRequest error:%{public}d!", err);
394 return err;
395 }
396 HILOGI("SubscribeSystemAbility SendRequest succeed!");
397 int32_t result = 0;
398 ret = reply.ReadInt32(result);
399 if (!ret) {
400 HILOGW("SubscribeSystemAbility Read result failed!");
401 return ERR_FLATTEN_OBJECT;
402 }
403
404 return result;
405 }
406
UnSubscribeSystemAbility(int32_t systemAbilityId,const sptr<ISystemAbilityStatusChange> & listener)407 int32_t SystemAbilityManagerProxy::UnSubscribeSystemAbility(int32_t systemAbilityId,
408 const sptr<ISystemAbilityStatusChange>& listener)
409 {
410 HILOGI("%{public}s called, UnSubscribeSystemAbility systemAbilityId:%{public}d", __func__, systemAbilityId);
411 if (!CheckInputSysAbilityId(systemAbilityId) || listener == nullptr) {
412 HILOGE("UnSubscribeSystemAbility systemAbilityId:%{public}d or listener invalid!", systemAbilityId);
413 return ERR_INVALID_VALUE;
414 }
415
416 sptr<IRemoteObject> remote = Remote();
417 if (remote == nullptr) {
418 HILOGI("remote is nullptr !");
419 return ERR_INVALID_OPERATION;
420 }
421
422 MessageParcel data;
423 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
424 return ERR_FLATTEN_OBJECT;
425 }
426 bool ret = data.WriteInt32(systemAbilityId);
427 if (!ret) {
428 HILOGW("UnSubscribeSystemAbility Write systemAbilityId failed!");
429 return ERR_FLATTEN_OBJECT;
430 }
431
432 ret = data.WriteRemoteObject(listener->AsObject());
433 if (!ret) {
434 HILOGW("UnSubscribeSystemAbility Write listenerSaId failed!");
435 return ERR_FLATTEN_OBJECT;
436 }
437
438 MessageParcel reply;
439 MessageOption option;
440 int32_t err = remote->SendRequest(
441 static_cast<uint32_t>(SamgrInterfaceCode::UNSUBSCRIBE_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
442 if (err != ERR_NONE) {
443 HILOGE("UnSubscribeSystemAbility SendRequest error:%{public}d!", err);
444 return err;
445 }
446 HILOGI("UnSubscribeSystemAbility SendRequest succeed!");
447 int32_t result = 0;
448 ret = reply.ReadInt32(result);
449 if (!ret) {
450 HILOGW("UnSubscribeSystemAbility Read result failed!");
451 return ERR_FLATTEN_OBJECT;
452 }
453
454 return result;
455 }
456
LoadSystemAbility(int32_t systemAbilityId,const sptr<ISystemAbilityLoadCallback> & callback)457 int32_t SystemAbilityManagerProxy::LoadSystemAbility(int32_t systemAbilityId,
458 const sptr<ISystemAbilityLoadCallback>& callback)
459 {
460 if (!CheckInputSysAbilityId(systemAbilityId) || callback == nullptr) {
461 HILOGE("LoadSystemAbility systemAbilityId:%{public}d or callback invalid!", systemAbilityId);
462 return ERR_INVALID_VALUE;
463 }
464
465 sptr<IRemoteObject> remote = Remote();
466 if (remote == nullptr) {
467 HILOGE("LoadSystemAbility remote is null!");
468 return ERR_INVALID_OPERATION;
469 }
470
471 MessageParcel data;
472 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
473 HILOGW("LoadSystemAbility Write interface token failed!");
474 return ERR_FLATTEN_OBJECT;
475 }
476 bool ret = data.WriteInt32(systemAbilityId);
477 if (!ret) {
478 HILOGW("LoadSystemAbility Write systemAbilityId failed!");
479 return ERR_FLATTEN_OBJECT;
480 }
481 ret = data.WriteRemoteObject(callback->AsObject());
482 if (!ret) {
483 HILOGW("LoadSystemAbility Write callback failed!");
484 return ERR_FLATTEN_OBJECT;
485 }
486
487 MessageParcel reply;
488 MessageOption option;
489 int32_t err = remote->SendRequest(
490 static_cast<uint32_t>(SamgrInterfaceCode::LOAD_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
491 if (err != ERR_NONE) {
492 HILOGE("LoadSystemAbility systemAbilityId : %{public}d invalid error:%{public}d!", systemAbilityId, err);
493 return err;
494 }
495 HILOGI("LoadSystemAbility systemAbilityId : %{public}d, SendRequest succeed!", systemAbilityId);
496 int32_t result = 0;
497 ret = reply.ReadInt32(result);
498 if (!ret) {
499 HILOGW("LoadSystemAbility Read reply failed!");
500 return ERR_FLATTEN_OBJECT;
501 }
502 return result;
503 }
504
LoadSystemAbility(int32_t systemAbilityId,const std::string & deviceId,const sptr<ISystemAbilityLoadCallback> & callback)505 int32_t SystemAbilityManagerProxy::LoadSystemAbility(int32_t systemAbilityId, const std::string& deviceId,
506 const sptr<ISystemAbilityLoadCallback>& callback)
507 {
508 if (!CheckInputSysAbilityId(systemAbilityId) || deviceId.empty() || callback == nullptr) {
509 HILOGE("LoadSystemAbility systemAbilityId:%{public}d ,deviceId or callback invalid!", systemAbilityId);
510 return ERR_INVALID_VALUE;
511 }
512 sptr<IRemoteObject> remote = Remote();
513 if (remote == nullptr) {
514 HILOGE("LoadSystemAbility remote is null!");
515 return ERR_INVALID_OPERATION;
516 }
517
518 MessageParcel data;
519 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
520 HILOGW("LoadSystemAbility write interface token failed!");
521 return ERR_FLATTEN_OBJECT;
522 }
523 bool ret = data.WriteInt32(systemAbilityId);
524 if (!ret) {
525 HILOGW("LoadSystemAbility write systemAbilityId failed!");
526 return ERR_FLATTEN_OBJECT;
527 }
528 ret = data.WriteString(deviceId);
529 if (!ret) {
530 HILOGW("LoadSystemAbility write deviceId failed!");
531 return ERR_FLATTEN_OBJECT;
532 }
533 ret = data.WriteRemoteObject(callback->AsObject());
534 if (!ret) {
535 HILOGW("LoadSystemAbility Write callback failed!");
536 return ERR_FLATTEN_OBJECT;
537 }
538
539 MessageParcel reply;
540 MessageOption option;
541 int32_t err = remote->SendRequest(
542 static_cast<uint32_t>(SamgrInterfaceCode::LOAD_REMOTE_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
543 if (err != ERR_NONE) {
544 HILOGE("LoadSystemAbility systemAbilityId : %{public}d invalid error:%{public}d!", systemAbilityId, err);
545 return err;
546 }
547 HILOGD("LoadSystemAbility systemAbilityId : %{public}d for remote, SendRequest succeed!", systemAbilityId);
548 int32_t result = 0;
549 ret = reply.ReadInt32(result);
550 if (!ret) {
551 HILOGW("LoadSystemAbility read reply failed for remote!");
552 return ERR_FLATTEN_OBJECT;
553 }
554 return result;
555 }
556
UnloadSystemAbility(int32_t systemAbilityId)557 int32_t SystemAbilityManagerProxy::UnloadSystemAbility(int32_t systemAbilityId)
558 {
559 if (!CheckInputSysAbilityId(systemAbilityId)) {
560 HILOGE("UnloadSystemAbility systemAbilityId:%{public}d invalid!", systemAbilityId);
561 return ERR_INVALID_VALUE;
562 }
563
564 sptr<IRemoteObject> remote = Remote();
565 if (remote == nullptr) {
566 HILOGE("UnloadSystemAbility remote is null!");
567 return ERR_INVALID_OPERATION;
568 }
569
570 MessageParcel data;
571 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
572 HILOGW("UnloadSystemAbility Write interface token failed!");
573 return ERR_FLATTEN_OBJECT;
574 }
575 bool ret = data.WriteInt32(systemAbilityId);
576 if (!ret) {
577 HILOGW("UnloadSystemAbility Write systemAbilityId failed!");
578 return ERR_FLATTEN_OBJECT;
579 }
580
581 MessageParcel reply;
582 MessageOption option;
583 int32_t err = remote->SendRequest(
584 static_cast<uint32_t>(SamgrInterfaceCode::UNLOAD_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
585 if (err != ERR_NONE) {
586 HILOGE("UnloadSystemAbility systemAbilityId : %{public}d invalid error:%{public}d!", systemAbilityId, err);
587 return err;
588 }
589 HILOGI("UnloadSystemAbility systemAbilityId : %{public}d, SendRequest succeed!", systemAbilityId);
590 int32_t result = 0;
591 ret = reply.ReadInt32(result);
592 if (!ret) {
593 HILOGW("UnloadSystemAbility Read reply failed!");
594 return ERR_FLATTEN_OBJECT;
595 }
596 return result;
597 }
598
CancelUnloadSystemAbility(int32_t systemAbilityId)599 int32_t SystemAbilityManagerProxy::CancelUnloadSystemAbility(int32_t systemAbilityId)
600 {
601 if (!CheckInputSysAbilityId(systemAbilityId)) {
602 HILOGE("CancelUnloadSystemAbility systemAbilityId:%{public}d invalid!", systemAbilityId);
603 return ERR_INVALID_VALUE;
604 }
605
606 sptr<IRemoteObject> remote = Remote();
607 if (remote == nullptr) {
608 HILOGE("CancelUnloadSystemAbility remote is null!");
609 return ERR_INVALID_OPERATION;
610 }
611
612 MessageParcel data;
613 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
614 HILOGW("CancelUnloadSystemAbility Write interface token failed!");
615 return ERR_FLATTEN_OBJECT;
616 }
617 bool ret = data.WriteInt32(systemAbilityId);
618 if (!ret) {
619 HILOGW("CancelUnloadSystemAbility Write systemAbilityId failed!");
620 return ERR_FLATTEN_OBJECT;
621 }
622
623 MessageParcel reply;
624 MessageOption option;
625 int32_t err = remote->SendRequest(
626 static_cast<uint32_t>(SamgrInterfaceCode::CANCEL_UNLOAD_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
627 if (err != ERR_NONE) {
628 HILOGE("CancelUnloadSystemAbility systemAbilityId : %{public}d SendRequest failed, error:%{public}d!",
629 systemAbilityId, err);
630 return err;
631 }
632 HILOGI("CancelUnloadSystemAbility systemAbilityId : %{public}d, SendRequest succeed!", systemAbilityId);
633 int32_t result = 0;
634 ret = reply.ReadInt32(result);
635 if (!ret) {
636 HILOGW("CancelUnloadSystemAbility Read reply failed!");
637 return ERR_FLATTEN_OBJECT;
638 }
639 return result;
640 }
641
MarshalSAExtraProp(const SAExtraProp & extraProp,MessageParcel & data) const642 int32_t SystemAbilityManagerProxy::MarshalSAExtraProp(const SAExtraProp& extraProp, MessageParcel& data) const
643 {
644 if (!data.WriteBool(extraProp.isDistributed)) {
645 HILOGW("MarshalSAExtraProp Write isDistributed failed!");
646 return ERR_FLATTEN_OBJECT;
647 }
648 if (!data.WriteInt32(extraProp.dumpFlags)) {
649 HILOGW("MarshalSAExtraProp Write dumpFlags failed!");
650 return ERR_FLATTEN_OBJECT;
651 }
652 if (!data.WriteString16(extraProp.capability)) {
653 HILOGW("MarshalSAExtraProp Write capability failed!");
654 return ERR_FLATTEN_OBJECT;
655 }
656 if (!data.WriteString16(extraProp.permission)) {
657 HILOGW("MarshalSAExtraProp Write defPermission failed!");
658 return ERR_FLATTEN_OBJECT;
659 }
660 return ERR_OK;
661 }
662
AddSystemAbility(int32_t systemAbilityId,const sptr<IRemoteObject> & ability,const SAExtraProp & extraProp)663 int32_t SystemAbilityManagerProxy::AddSystemAbility(int32_t systemAbilityId, const sptr<IRemoteObject>& ability,
664 const SAExtraProp& extraProp)
665 {
666 HILOGD("%{public}s called, systemAbilityId is %{public}d", __func__, systemAbilityId);
667 if (!CheckInputSysAbilityId(systemAbilityId)) {
668 HILOGW("systemAbilityId:%{public}d invalid.", systemAbilityId);
669 return ERR_INVALID_VALUE;
670 }
671
672 MessageParcel data;
673 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
674 return ERR_FLATTEN_OBJECT;
675 }
676 if (!data.WriteInt32(systemAbilityId)) {
677 HILOGW("AddSystemAbility Write saId failed!");
678 return ERR_FLATTEN_OBJECT;
679 }
680 if (!data.WriteRemoteObject(ability)) {
681 HILOGW("AddSystemAbility Write ability failed!");
682 return ERR_FLATTEN_OBJECT;
683 }
684
685 int32_t ret = MarshalSAExtraProp(extraProp, data);
686 if (ret != ERR_OK) {
687 HILOGW("AddSystemAbility MarshalSAExtraProp failed!");
688 return ret;
689 }
690
691 int32_t result = AddSystemAbilityWrapper(
692 static_cast<uint32_t>(SamgrInterfaceCode::ADD_SYSTEM_ABILITY_TRANSACTION), data);
693 if (result == ERR_OK) {
694 LocalAbilitys::GetInstance().AddAbility(systemAbilityId, ability);
695 }
696 return result;
697 }
698
AddSystemAbilityWrapper(int32_t code,MessageParcel & data)699 int32_t SystemAbilityManagerProxy::AddSystemAbilityWrapper(int32_t code, MessageParcel& data)
700 {
701 sptr<IRemoteObject> remote = Remote();
702 if (remote == nullptr) {
703 HILOGI("remote is nullptr !");
704 return ERR_INVALID_OPERATION;
705 }
706
707 MessageParcel reply;
708 MessageOption option;
709 int32_t err = remote->SendRequest(code, data, reply, option);
710 if (err != ERR_NONE) {
711 HILOGE("AddSystemAbility SA invalid error:%{public}d!", err);
712 return err;
713 }
714 int32_t result = 0;
715 bool ret = reply.ReadInt32(result);
716 if (!ret) {
717 HILOGE("AddSystemAbility read result error!");
718 return ERR_FLATTEN_OBJECT;
719 }
720 return result;
721 }
722
AddSystemProcess(const u16string & procName,const sptr<IRemoteObject> & procObject)723 int32_t SystemAbilityManagerProxy::AddSystemProcess(const u16string& procName, const sptr<IRemoteObject>& procObject)
724 {
725 HILOGD("%{public}s called, process name is %{public}s", __func__, Str16ToStr8(procName).c_str());
726 if (procName.empty()) {
727 HILOGI("process name is invalid!");
728 return ERR_INVALID_VALUE;
729 }
730
731 MessageParcel data;
732 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
733 return ERR_FLATTEN_OBJECT;
734 }
735 if (!data.WriteString16(procName)) {
736 HILOGW("AddSystemProcess Write name failed!");
737 return ERR_FLATTEN_OBJECT;
738 }
739
740 if (!data.WriteRemoteObject(procObject)) {
741 HILOGW("AddSystemProcess Write ability failed!");
742 return ERR_FLATTEN_OBJECT;
743 }
744 return AddSystemAbilityWrapper(
745 static_cast<uint32_t>(SamgrInterfaceCode::ADD_SYSTEM_PROCESS_TRANSACTION), data);
746 }
747
GetRunningSystemProcess(std::list<SystemProcessInfo> & systemProcessInfos)748 int32_t SystemAbilityManagerProxy::GetRunningSystemProcess(std::list<SystemProcessInfo>& systemProcessInfos)
749 {
750 HILOGI("GetRunningSystemProcess called");
751 sptr<IRemoteObject> remote = Remote();
752 if (remote == nullptr) {
753 HILOGI("GetRunningSystemProcess remote is nullptr");
754 return ERR_INVALID_OPERATION;
755 }
756
757 MessageParcel data;
758 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
759 return ERR_FLATTEN_OBJECT;
760 }
761
762 MessageParcel reply;
763 MessageOption option;
764 int32_t err = remote->SendRequest(
765 static_cast<uint32_t>(SamgrInterfaceCode::GET_RUNNING_SYSTEM_PROCESS_TRANSACTION), data, reply, option);
766 if (err != ERR_NONE) {
767 HILOGE("GetRunningSystemProcess SendRequest error: %{public}d!", err);
768 return err;
769 }
770 HILOGI("GetRunningSystemProcess SendRequest succeed!");
771 int32_t result = 0;
772 bool ret = reply.ReadInt32(result);
773 if (!ret) {
774 HILOGW("SubscribeSystemProcess Read result failed!");
775 return ERR_FLATTEN_OBJECT;
776 }
777 if (result != ERR_OK) {
778 HILOGE("GetRunningSystemProcess failed: %{public}d!", result);
779 return result;
780 }
781 return ReadSystemProcessFromParcel(systemProcessInfos, reply);
782 }
783
ReadSystemProcessFromParcel(std::list<SystemProcessInfo> & systemProcessInfos,MessageParcel & reply)784 int32_t SystemAbilityManagerProxy::ReadSystemProcessFromParcel(std::list<SystemProcessInfo>& systemProcessInfos,
785 MessageParcel& reply)
786 {
787 int32_t size = 0;
788 bool ret = reply.ReadInt32(size);
789 if (!ret) {
790 HILOGW("GetRunningSystemProcess Read list size failed!");
791 return ERR_FLATTEN_OBJECT;
792 }
793 systemProcessInfos.clear();
794 if (size == 0) {
795 return ERR_OK;
796 }
797 if (static_cast<size_t>(size) > reply.GetReadableBytes() || size < 0) {
798 HILOGE("Failed to read system process list, size = %{public}d", size);
799 return ERR_FLATTEN_OBJECT;
800 }
801 for (int32_t i = 0; i < size; i++) {
802 SystemProcessInfo systemProcessInfo;
803 ret = reply.ReadString(systemProcessInfo.processName);
804 if (!ret) {
805 HILOGW("GetRunningSystemProcess Read processName failed!");
806 return ERR_FLATTEN_OBJECT;
807 }
808 ret = reply.ReadInt32(systemProcessInfo.pid);
809 if (!ret) {
810 HILOGW("GetRunningSystemProcess Read pid failed!");
811 return ERR_FLATTEN_OBJECT;
812 }
813 ret = reply.ReadInt32(systemProcessInfo.uid);
814 if (!ret) {
815 HILOGW("GetRunningSystemProcess Read uid failed!");
816 return ERR_FLATTEN_OBJECT;
817 }
818 systemProcessInfos.emplace_back(systemProcessInfo);
819 }
820 return ERR_OK;
821 }
822
SubscribeSystemProcess(const sptr<ISystemProcessStatusChange> & listener)823 int32_t SystemAbilityManagerProxy::SubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener)
824 {
825 HILOGI("SubscribeSystemProcess called");
826 if (listener == nullptr) {
827 HILOGE("SubscribeSystemProcess listener is nullptr");
828 return ERR_INVALID_VALUE;
829 }
830
831 sptr<IRemoteObject> remote = Remote();
832 if (remote == nullptr) {
833 HILOGI("SubscribeSystemProcess remote is nullptr");
834 return ERR_INVALID_OPERATION;
835 }
836
837 MessageParcel data;
838 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
839 return ERR_FLATTEN_OBJECT;
840 }
841 bool ret = data.WriteRemoteObject(listener->AsObject());
842 if (!ret) {
843 HILOGW("SubscribeSystemProcess Write listenerName failed");
844 return ERR_FLATTEN_OBJECT;
845 }
846
847 MessageParcel reply;
848 MessageOption option;
849 int32_t err = remote->SendRequest(
850 static_cast<uint32_t>(SamgrInterfaceCode::SUBSCRIBE_SYSTEM_PROCESS_TRANSACTION), data, reply, option);
851 if (err != ERR_NONE) {
852 HILOGE("SubscribeSystemProcess SendRequest error:%{public}d!", err);
853 return err;
854 }
855 HILOGI("SubscribeSystemProcesss SendRequest succeed!");
856 int32_t result = 0;
857 ret = reply.ReadInt32(result);
858 if (!ret) {
859 HILOGW("SubscribeSystemProcess Read result failed!");
860 return ERR_FLATTEN_OBJECT;
861 }
862 return result;
863 }
864
UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange> & listener)865 int32_t SystemAbilityManagerProxy::UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener)
866 {
867 HILOGI("UnSubscribeSystemProcess called");
868 if (listener == nullptr) {
869 HILOGE("UnSubscribeSystemProcess listener is nullptr");
870 return ERR_INVALID_VALUE;
871 }
872
873 sptr<IRemoteObject> remote = Remote();
874 if (remote == nullptr) {
875 HILOGI("UnSubscribeSystemProcess remote is nullptr");
876 return ERR_INVALID_OPERATION;
877 }
878
879 MessageParcel data;
880 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
881 return ERR_FLATTEN_OBJECT;
882 }
883 bool ret = data.WriteRemoteObject(listener->AsObject());
884 if (!ret) {
885 HILOGW("UnSubscribeSystemProcess Write listenerName failed");
886 return ERR_FLATTEN_OBJECT;
887 }
888
889 MessageParcel reply;
890 MessageOption option;
891 int32_t err = remote->SendRequest(
892 static_cast<uint32_t>(SamgrInterfaceCode::UNSUBSCRIBE_SYSTEM_PROCESS_TRANSACTION), data, reply, option);
893 if (err != ERR_NONE) {
894 HILOGE("UnSubscribeSystemProcess SendRequest error:%{public}d!", err);
895 return err;
896 }
897 HILOGI("UnSubscribeSystemProcess SendRequest succeed!");
898 int32_t result = 0;
899 ret = reply.ReadInt32(result);
900 if (!ret) {
901 HILOGW("UnSubscribeSystemProcess Read result failed!");
902 return ERR_FLATTEN_OBJECT;
903 }
904 return result;
905 }
906
GetOnDemandReasonExtraData(int64_t extraDataId,MessageParcel & extraDataParcel)907 int32_t SystemAbilityManagerProxy::GetOnDemandReasonExtraData(int64_t extraDataId, MessageParcel& extraDataParcel)
908 {
909 HILOGI("GetOnDemandReasonExtraData called");
910 sptr<IRemoteObject> remote = Remote();
911 if (remote == nullptr) {
912 HILOGE("GetOnDemandReasonExtraData remote is nullptr");
913 return ERR_INVALID_OPERATION;
914 }
915
916 MessageParcel data;
917 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
918 HILOGE("GetOnDemandReasonExtraData write interface token failed");
919 return ERR_FLATTEN_OBJECT;
920 }
921 if (!data.WriteInt64(extraDataId)) {
922 HILOGE("GetOnDemandReasonExtraData write extraDataId failed");
923 return ERR_FLATTEN_OBJECT;
924 }
925
926 MessageOption option;
927 int32_t err = remote->SendRequest(
928 static_cast<uint32_t>(SamgrInterfaceCode::GET_ONDEMAND_REASON_EXTRA_DATA_TRANSACTION),
929 data, extraDataParcel, option);
930 if (err != ERR_NONE) {
931 HILOGE("GetOnDemandReasonExtraData SendRequest error:%{public}d", err);
932 return err;
933 }
934 HILOGI("GetOnDemandReasonExtraData SendRequest succeed");
935 int32_t result = 0;
936 if (!extraDataParcel.ReadInt32(result)) {
937 HILOGE("GetOnDemandReasonExtraData read result failed");
938 return ERR_FLATTEN_OBJECT;
939 }
940 return result;
941 }
942
GetOnDemandPolicy(int32_t systemAbilityId,OnDemandPolicyType type,std::vector<SystemAbilityOnDemandEvent> & abilityOnDemandEvents)943 int32_t SystemAbilityManagerProxy::GetOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type,
944 std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents)
945 {
946 HILOGI("GetOnDemandPolicy called");
947 sptr<IRemoteObject> remote = Remote();
948 if (remote == nullptr) {
949 HILOGI("GetOnDemandPolicy remote is nullptr");
950 return ERR_INVALID_OPERATION;
951 }
952
953 MessageParcel data;
954 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
955 HILOGE("GetOnDemandPolicy write interface token failed!");
956 return ERR_FLATTEN_OBJECT;
957 }
958 if (!data.WriteInt32(systemAbilityId)) {
959 HILOGE("GetOnDemandPolicy write said failed!");
960 return ERR_FLATTEN_OBJECT;
961 }
962 if (!data.WriteInt32(static_cast<int32_t>(type))) {
963 HILOGE("GetOnDemandPolicy write type failed!");
964 return ERR_FLATTEN_OBJECT;
965 }
966
967 MessageParcel reply;
968 MessageOption option;
969 int32_t err = remote->SendRequest(
970 static_cast<uint32_t>(SamgrInterfaceCode::GET_ONDEAMND_POLICY_TRANSACTION), data, reply, option);
971 if (err != ERR_NONE) {
972 HILOGE("GetOnDemandPolicy SendRequest error: %{public}d!", err);
973 return err;
974 }
975 HILOGI("GetOnDemandPolicy SendRequest succeed!");
976 int32_t result = 0;
977 if (!reply.ReadInt32(result)) {
978 HILOGE("GetOnDemandPolicy Read result failed!");
979 return ERR_FLATTEN_OBJECT;
980 }
981 if (result != ERR_OK) {
982 HILOGE("GetOnDemandPolicy failed: %{public}d!", result);
983 return result;
984 }
985 if (!OnDemandEventToParcel::ReadOnDemandEventsFromParcel(abilityOnDemandEvents, reply)) {
986 HILOGE("GetOnDemandPolicy Read on demand events failed!");
987 return ERR_FLATTEN_OBJECT;
988 }
989 return ERR_OK;
990 }
991
UpdateOnDemandPolicy(int32_t systemAbilityId,OnDemandPolicyType type,const std::vector<SystemAbilityOnDemandEvent> & abilityOnDemandEvents)992 int32_t SystemAbilityManagerProxy::UpdateOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type,
993 const std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents)
994 {
995 HILOGI("UpdateOnDemandPolicy called");
996 sptr<IRemoteObject> remote = Remote();
997 if (remote == nullptr) {
998 HILOGI("UpdateOnDemandPolicy remote is nullptr");
999 return ERR_INVALID_OPERATION;
1000 }
1001
1002 MessageParcel data;
1003 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1004 HILOGE("UpdateOnDemandPolicy write interface token failed!");
1005 return ERR_FLATTEN_OBJECT;
1006 }
1007 if (!data.WriteInt32(systemAbilityId)) {
1008 HILOGE("UpdateOnDemandPolicy write said failed!");
1009 return ERR_FLATTEN_OBJECT;
1010 }
1011 if (!data.WriteInt32(static_cast<int32_t>(type))) {
1012 HILOGE("UpdateOnDemandPolicy write type failed!");
1013 return ERR_FLATTEN_OBJECT;
1014 }
1015 if (!OnDemandEventToParcel::WriteOnDemandEventsToParcel(abilityOnDemandEvents, data)) {
1016 HILOGW("UpdateOnDemandPolicy write on demand events failed!");
1017 return ERR_FLATTEN_OBJECT;
1018 }
1019
1020 MessageParcel reply;
1021 MessageOption option;
1022 int32_t err = remote->SendRequest(
1023 static_cast<uint32_t>(SamgrInterfaceCode::UPDATE_ONDEAMND_POLICY_TRANSACTION), data, reply, option);
1024 if (err != ERR_NONE) {
1025 HILOGE("UpdateOnDemandPolicy SendRequest error: %{public}d!", err);
1026 return err;
1027 }
1028 HILOGI("UpdateOnDemandPolicy SendRequest succeed!");
1029 int32_t result = 0;
1030 if (!reply.ReadInt32(result)) {
1031 HILOGE("UpdateOnDemandPolicy Read result failed!");
1032 return ERR_FLATTEN_OBJECT;
1033 }
1034 if (result != ERR_OK) {
1035 HILOGE("UpdateOnDemandPolicy failed: %{public}d!", result);
1036 }
1037 return result;
1038 }
1039 } // namespace OHOS
1040