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 <condition_variable>
19 #include <unistd.h>
20 #include <vector>
21 #include <dlfcn.h>
22
23 #include "errors.h"
24 #include "ipc_types.h"
25 #include "iremote_object.h"
26 #include "isystem_ability_load_callback.h"
27 #include "isystem_ability_status_change.h"
28 #include "message_option.h"
29 #include "message_parcel.h"
30 #include "refbase.h"
31 #include "sam_log.h"
32 #include "string_ex.h"
33
34 #include "local_abilitys.h"
35 #include "system_ability_load_callback_stub.h"
36
37 using namespace std;
38 namespace OHOS {
39 namespace {
40 const int32_t MAX_TIMEOUT = 4;
41 const int32_t MIN_TIMEOUT = 0;
42 const int32_t RETRY_TIME_OUT_NUMBER = 6;
43 const int32_t SLEEP_INTERVAL_TIME = 200;
44 const int32_t GET_SYSTEM_ABILITY_CODE = 1;
45 const int32_t CHECK_SYSTEM_ABILITY_CODE = 2;
46 const int32_t SLEEP_ONE_MILLI_SECOND_TIME = 1000;
47 constexpr const char* PARAM_KEY = "persist.samgr.cache.sa";
48 }
49 class SystemAbilityProxyCallback : public SystemAbilityLoadCallbackStub {
50 public:
51 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
52 const sptr<IRemoteObject> &remoteObject) override;
53 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
54 std::mutex callbackLock_;
55 std::condition_variable cv_;
56 sptr<IRemoteObject> loadproxy_;
57 };
58
59 static void* g_selfSoHandle = nullptr;
60
InitSamgrProxy()61 extern "C" __attribute__((constructor)) void InitSamgrProxy()
62 {
63 if (g_selfSoHandle != nullptr) {
64 return;
65 }
66 Dl_info info;
67 int ret = dladdr(reinterpret_cast<void *>(InitSamgrProxy), &info);
68 if (ret == 0) {
69 HILOGE("InitSamgrProxy dladdr fail");
70 return;
71 }
72
73 char path[PATH_MAX] = {'\0'};
74 if (realpath(info.dli_fname, path) == nullptr) {
75 HILOGE("InitSamgrProxy realpath fail");
76 return;
77 }
78 std::vector<std::string> strVector;
79 SplitStr(path, "/", strVector);
80 auto vectorSize = strVector.size();
81 if (vectorSize == 0) {
82 HILOGE("InitSamgrProxy SplitStr fail");
83 return;
84 }
85 auto& fileName = strVector[vectorSize - 1];
86 g_selfSoHandle = dlopen(fileName.c_str(), RTLD_LAZY);
87 if (g_selfSoHandle == nullptr) {
88 HILOGE("InitSamgrProxy dlopen fail");
89 return;
90 }
91 HILOGD("InitSamgrProxy::done");
92 }
93
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)94 void SystemAbilityProxyCallback::OnLoadSystemAbilitySuccess(
95 int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
96 {
97 std::lock_guard<std::mutex> lock(callbackLock_);
98 loadproxy_ = remoteObject;
99 cv_.notify_one();
100 HILOGI("LoadSystemAbility on load SA:%{public}d success!", systemAbilityId);
101 }
102
OnLoadSystemAbilityFail(int32_t systemAbilityId)103 void SystemAbilityProxyCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
104 {
105 std::lock_guard<std::mutex> lock(callbackLock_);
106 loadproxy_ = nullptr;
107 cv_.notify_one();
108 HILOGI("LoadSystemAbility on load SA:%{public}d failed!", systemAbilityId);
109 }
110
GetSystemAbility(int32_t systemAbilityId)111 sptr<IRemoteObject> SystemAbilityManagerProxy::GetSystemAbility(int32_t systemAbilityId)
112 {
113 if (IsOnDemandSystemAbility(systemAbilityId)) {
114 return GetSystemAbilityWrapper(systemAbilityId);
115 }
116
117 bool ret = SetKey(PARAM_KEY);
118 if (!ret) {
119 return GetSystemAbilityWrapper(systemAbilityId);
120 }
121 return QueryResult(systemAbilityId, GET_SYSTEM_ABILITY_CODE);
122 }
123
IsOnDemandSystemAbility(int32_t systemAbilityId)124 bool SystemAbilityManagerProxy::IsOnDemandSystemAbility(int32_t systemAbilityId)
125 {
126 {
127 std::lock_guard<std::mutex> autoLock(onDemandSaLock_);
128 if (!onDemandSystemAbilityIdsSet_.empty()) {
129 auto pos = onDemandSystemAbilityIdsSet_.find(systemAbilityId);
130 if (pos != onDemandSystemAbilityIdsSet_.end()) {
131 return true;
132 }
133 return false;
134 }
135 }
136 std::vector<int32_t> onDemandSystemAbilityIds;
137 GetOnDemandSystemAbilityIds(onDemandSystemAbilityIds);
138 {
139 std::lock_guard<std::mutex> autoLock(onDemandSaLock_);
140 for (auto onDemandSystemAbilityId : onDemandSystemAbilityIds) {
141 onDemandSystemAbilityIdsSet_.insert(onDemandSystemAbilityId);
142 }
143
144 auto pos = onDemandSystemAbilityIdsSet_.find(systemAbilityId);
145 if (pos != onDemandSystemAbilityIdsSet_.end()) {
146 return true;
147 }
148 return false;
149 }
150 }
151
Recompute(int32_t systemAbilityId,int32_t code)152 sptr<IRemoteObject> SystemAbilityManagerProxy::Recompute(int32_t systemAbilityId, int32_t code)
153 {
154 ClearCache();
155 if (code == GET_SYSTEM_ABILITY_CODE) {
156 return GetSystemAbilityWrapper(systemAbilityId);
157 }
158 return CheckSystemAbilityTransaction(systemAbilityId);
159 }
160
GetSystemAbility(int32_t systemAbilityId,const std::string & deviceId)161 sptr<IRemoteObject> SystemAbilityManagerProxy::GetSystemAbility(int32_t systemAbilityId,
162 const std::string& deviceId)
163 {
164 return GetSystemAbilityWrapper(systemAbilityId, deviceId);
165 }
166
GetSystemAbilityWrapper(int32_t systemAbilityId,const string & deviceId)167 sptr<IRemoteObject> SystemAbilityManagerProxy::GetSystemAbilityWrapper(int32_t systemAbilityId, const string& deviceId)
168 {
169 if (!CheckInputSysAbilityId(systemAbilityId)) {
170 HILOGW("GetSaWrap SA invalid:%{public}d!", systemAbilityId);
171 return nullptr;
172 }
173
174 bool isExist = false;
175 int32_t timeout = RETRY_TIME_OUT_NUMBER;
176 HILOGD("GetSaWrap:Waiting for SA:%{public}d, ", systemAbilityId);
177 do {
178 sptr<IRemoteObject> svc;
179 int32_t errCode = ERR_NONE;
180 if (deviceId.empty()) {
181 svc = CheckSystemAbility(systemAbilityId, isExist, errCode);
182 if (errCode == ERR_PERMISSION_DENIED) {
183 HILOGE("GetSaWrap SA:%{public}d selinux denied", systemAbilityId);
184 return nullptr;
185 }
186 if (!isExist) {
187 HILOGD("%{public}s:SA:%{public}d is not exist", __func__, systemAbilityId);
188 }
189 } else {
190 svc = CheckSystemAbility(systemAbilityId, deviceId, errCode);
191 if (errCode == ERR_PERMISSION_DENIED) {
192 HILOGE("GetSaWrap SA:%{public}d deviceId selinux denied", systemAbilityId);
193 return nullptr;
194 }
195 }
196
197 if (svc != nullptr) {
198 return svc;
199 }
200 if (timeout > 0) {
201 usleep(SLEEP_ONE_MILLI_SECOND_TIME * SLEEP_INTERVAL_TIME);
202 }
203 } while (timeout--);
204 HILOGE("GetSaWrap SA:%{public}d not start", systemAbilityId);
205 return nullptr;
206 }
207
CheckSystemAbilityWrapper(int32_t code,MessageParcel & data)208 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbilityWrapper(int32_t code, MessageParcel& data)
209 {
210 int32_t errCode = ERR_NONE;
211 return CheckSystemAbilityWrapper(code, data, errCode);
212 }
213
CheckSystemAbilityWrapper(int32_t code,MessageParcel & data,int32_t & errCode)214 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbilityWrapper(int32_t code, MessageParcel& data,
215 int32_t& errCode)
216 {
217 auto remote = Remote();
218 if (remote == nullptr) {
219 HILOGI("CheckSaWrap remote is nullptr !");
220 return nullptr;
221 }
222 MessageParcel reply;
223 MessageOption option;
224 int32_t err = remote->SendRequest(code, data, reply, option);
225 if (err != ERR_NONE) {
226 errCode = err;
227 return nullptr;
228 }
229 return reply.ReadRemoteObject();
230 }
231
CheckSystemAbility(int32_t systemAbilityId)232 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbility(int32_t systemAbilityId)
233 {
234 HILOGD("%{public}s called", __func__);
235 if (!CheckInputSysAbilityId(systemAbilityId)) {
236 HILOGW("SA:%{public}d invalid!", systemAbilityId);
237 return nullptr;
238 }
239
240 if (IsOnDemandSystemAbility(systemAbilityId)) {
241 return CheckSystemAbilityTransaction(systemAbilityId);
242 }
243
244 bool ret = SetKey(PARAM_KEY);
245 if (!ret) {
246 return CheckSystemAbilityTransaction(systemAbilityId);
247 }
248 return QueryResult(systemAbilityId, CHECK_SYSTEM_ABILITY_CODE);
249 }
250
CheckSystemAbilityTransaction(int32_t systemAbilityId)251 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbilityTransaction(int32_t systemAbilityId)
252 {
253 MessageParcel data;
254 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
255 return nullptr;
256 }
257 bool ret = data.WriteInt32(systemAbilityId);
258 if (!ret) {
259 HILOGW("CheckSystemAbility Write SAId failed!");
260 return nullptr;
261 }
262 return CheckSystemAbilityWrapper(
263 static_cast<uint32_t>(SamgrInterfaceCode::CHECK_SYSTEM_ABILITY_TRANSACTION), data);
264 }
265
CheckSystemAbility(int32_t systemAbilityId,const std::string & deviceId)266 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
267 {
268 int32_t errCode = ERR_NONE;
269 return CheckSystemAbility(systemAbilityId, deviceId, errCode);
270 }
271
CheckSystemAbility(int32_t systemAbilityId,const std::string & deviceId,int32_t & errCode)272 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbility(int32_t systemAbilityId, const std::string& deviceId,
273 int32_t& errCode)
274 {
275 if (!CheckInputSysAbilityId(systemAbilityId) || deviceId.empty()) {
276 HILOGW("CheckSystemAbility:SA:%{public}d or deviceId is nullptr.", systemAbilityId);
277 return nullptr;
278 }
279
280 HILOGD("CheckSystemAbility: SA:%{public}d.", systemAbilityId);
281
282 auto remote = Remote();
283 if (remote == nullptr) {
284 HILOGE("CheckSystemAbility remote is nullptr !");
285 return nullptr;
286 }
287
288 MessageParcel data;
289 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
290 return nullptr;
291 }
292 bool ret = data.WriteInt32(systemAbilityId);
293 if (!ret) {
294 HILOGE("CheckSystemAbility parcel write name failed");
295 return nullptr;
296 }
297 ret = data.WriteString(deviceId);
298 if (!ret) {
299 HILOGE("CheckSystemAbility parcel write deviceId failed");
300 return nullptr;
301 }
302
303 return CheckSystemAbilityWrapper(
304 static_cast<uint32_t>(SamgrInterfaceCode::CHECK_REMOTE_SYSTEM_ABILITY_TRANSACTION), data, errCode);
305 }
306
CheckSystemAbility(int32_t systemAbilityId,bool & isExist)307 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbility(int32_t systemAbilityId, bool& isExist)
308 {
309 int32_t errCode = ERR_NONE;
310 return CheckSystemAbility(systemAbilityId, isExist, errCode);
311 }
CheckSystemAbility(int32_t systemAbilityId,bool & isExist,int32_t & errCode)312 sptr<IRemoteObject> SystemAbilityManagerProxy::CheckSystemAbility(int32_t systemAbilityId, bool& isExist,
313 int32_t& errCode)
314 {
315 HILOGD("%{public}s called, SA:%{public}d, isExist is %{public}d", __func__, systemAbilityId, isExist);
316 if (!CheckInputSysAbilityId(systemAbilityId)) {
317 HILOGW("CheckSystemAbility:SA:%{public}d invalid!", systemAbilityId);
318 return nullptr;
319 }
320
321 auto proxy = LocalAbilitys::GetInstance().GetAbility(systemAbilityId);
322 if (proxy != nullptr) {
323 isExist = true;
324 return proxy;
325 }
326
327 auto remote = Remote();
328 if (remote == nullptr) {
329 HILOGE("CheckSystemAbility remote is nullptr !");
330 return nullptr;
331 }
332
333 MessageParcel data;
334 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
335 return nullptr;
336 }
337 bool ret = data.WriteInt32(systemAbilityId);
338 if (!ret) {
339 HILOGW("CheckSystemAbility Write SAId failed!");
340 return nullptr;
341 }
342
343 ret = data.WriteBool(isExist);
344 if (!ret) {
345 HILOGW("CheckSystemAbility Write isExist failed!");
346 return nullptr;
347 }
348
349 MessageParcel reply;
350 MessageOption option;
351 int32_t err = remote->SendRequest(
352 static_cast<uint32_t>(SamgrInterfaceCode::CHECK_SYSTEM_ABILITY_IMMEDIATELY_TRANSACTION), data, reply, option);
353 if (err != ERR_NONE) {
354 errCode = err;
355 return nullptr;
356 }
357
358 sptr<IRemoteObject> irsp = reply.ReadRemoteObject();
359 if (irsp == nullptr) {
360 HILOGW("CheckSystemAbility read remote object failed");
361 return nullptr;
362 }
363
364 ret = reply.ReadBool(isExist);
365 if (!ret) {
366 HILOGW("CheckSystemAbility Read isExist failed!");
367 return nullptr;
368 }
369
370 return irsp;
371 }
372
AddOnDemandSystemAbilityInfo(int32_t systemAbilityId,const std::u16string & localAbilityManagerName)373 int32_t SystemAbilityManagerProxy::AddOnDemandSystemAbilityInfo(int32_t systemAbilityId,
374 const std::u16string& localAbilityManagerName)
375 {
376 HILOGD("%{public}s called, SA:%{public}d ", __func__, systemAbilityId);
377 if (!CheckInputSysAbilityId(systemAbilityId) || localAbilityManagerName.empty()) {
378 HILOGI("AddOnDemandSystemAbilityInfo invalid params!");
379 return ERR_INVALID_VALUE;
380 }
381
382 auto remote = Remote();
383 if (remote == nullptr) {
384 HILOGE("AddOnDemandSystemAbilityInfo remote is nullptr !");
385 return ERR_INVALID_OPERATION;
386 }
387
388 MessageParcel data;
389 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
390 return ERR_FLATTEN_OBJECT;
391 }
392 bool ret = data.WriteInt32(systemAbilityId);
393 if (!ret) {
394 HILOGW("AddOnDemandSystemAbilityInfo Write SAId failed!");
395 return ERR_FLATTEN_OBJECT;
396 }
397
398 ret = data.WriteString16(localAbilityManagerName);
399 if (!ret) {
400 HILOGW("AddOnDemandSystemAbilityInfo Write localAbilityManagerName failed!");
401 return ERR_FLATTEN_OBJECT;
402 }
403
404 MessageParcel reply;
405 MessageOption option;
406 int32_t err = remote->SendRequest(
407 static_cast<uint32_t>(SamgrInterfaceCode::ADD_ONDEMAND_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
408
409 HILOGI("AddOnDemandSaInfo SA:%{public}d %{public}s,rtn:%{public}d", systemAbilityId, err ? "fail" : "suc", err);
410 if (err != ERR_NONE) {
411 return err;
412 }
413
414 int32_t result = 0;
415 ret = reply.ReadInt32(result);
416 if (!ret) {
417 HILOGW("AddOnDemandSystemAbilityInfo Read result failed!");
418 return ERR_FLATTEN_OBJECT;
419 }
420 return result;
421 }
422
RemoveSystemAbilityWrapper(int32_t code,MessageParcel & data)423 int32_t SystemAbilityManagerProxy::RemoveSystemAbilityWrapper(int32_t code, MessageParcel& data)
424 {
425 sptr<IRemoteObject> remote = Remote();
426 if (remote == nullptr) {
427 HILOGI("remote is nullptr !");
428 return ERR_INVALID_OPERATION;
429 }
430 MessageParcel reply;
431 MessageOption option;
432 int32_t err = remote->SendRequest(code, data, reply, option);
433 if (err != ERR_NONE) {
434 HILOGE("RemoveSystemAbility SendRequest error:%{public}d!", err);
435 return err;
436 }
437
438 int32_t result = 0;
439 bool ret = reply.ReadInt32(result);
440 if (!ret) {
441 HILOGW("RemoveSystemAbility Read result failed!");
442 return ERR_FLATTEN_OBJECT;
443 }
444
445 return result;
446 }
447
RemoveSystemAbility(int32_t systemAbilityId)448 int32_t SystemAbilityManagerProxy::RemoveSystemAbility(int32_t systemAbilityId)
449 {
450 HILOGD("%{public}s called, SA:%{public}d", __func__, systemAbilityId);
451 if (!CheckInputSysAbilityId(systemAbilityId)) {
452 HILOGW("SA:%{public}d is invalid!", systemAbilityId);
453 return ERR_INVALID_VALUE;
454 }
455
456 MessageParcel data;
457 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
458 return ERR_FLATTEN_OBJECT;
459 }
460 bool ret = data.WriteInt32(systemAbilityId);
461 if (!ret) {
462 HILOGW("RemoveSystemAbility Write SAId failed!");
463 return ERR_FLATTEN_OBJECT;
464 }
465
466 int32_t result = RemoveSystemAbilityWrapper(
467 static_cast<uint32_t>(SamgrInterfaceCode::REMOVE_SYSTEM_ABILITY_TRANSACTION), data);
468 if (result == ERR_OK) {
469 LocalAbilitys::GetInstance().RemoveAbility(systemAbilityId);
470 }
471 return result;
472 }
473
ListSystemAbilities(unsigned int dumpFlags)474 std::vector<u16string> SystemAbilityManagerProxy::ListSystemAbilities(unsigned int dumpFlags)
475 {
476 HILOGD("%{public}s called", __func__);
477 std::vector<u16string> saNames;
478
479 sptr<IRemoteObject> remote = Remote();
480 if (remote == nullptr) {
481 HILOGI("remote is nullptr !");
482 return saNames;
483 }
484
485 MessageParcel data;
486 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
487 HILOGW("ListSystemAbilities write token failed!");
488 return saNames;
489 }
490 bool ret = data.WriteInt32(dumpFlags);
491 if (!ret) {
492 HILOGW("ListSystemAbilities write dumpFlags failed!");
493 return saNames;
494 }
495 MessageParcel reply;
496 MessageOption option;
497 int32_t err = remote->SendRequest(
498 static_cast<uint32_t>(SamgrInterfaceCode::LIST_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
499 if (err != ERR_NONE) {
500 HILOGW("ListSystemAbilities transact failed!");
501 return saNames;
502 }
503 if (reply.ReadInt32() != ERR_NONE) {
504 HILOGW("ListSystemAbilities remote failed!");
505 return saNames;
506 }
507 if (!reply.ReadString16Vector(&saNames)) {
508 HILOGW("ListSystemAbilities read reply failed");
509 saNames.clear();
510 }
511 return saNames;
512 }
513
SubscribeSystemAbility(int32_t systemAbilityId,const sptr<ISystemAbilityStatusChange> & listener)514 int32_t SystemAbilityManagerProxy::SubscribeSystemAbility(int32_t systemAbilityId,
515 const sptr<ISystemAbilityStatusChange>& listener)
516 {
517 HILOGD("%{public}s called, SA:%{public}d", __func__, systemAbilityId);
518 if (!CheckInputSysAbilityId(systemAbilityId) || listener == nullptr) {
519 HILOGE("SubscribeSystemAbility SA:%{public}d or listener invalid!", systemAbilityId);
520 return ERR_INVALID_VALUE;
521 }
522
523 sptr<IRemoteObject> remote = Remote();
524 if (remote == nullptr) {
525 HILOGI("remote is nullptr !");
526 return ERR_INVALID_OPERATION;
527 }
528
529 MessageParcel data;
530 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
531 return ERR_FLATTEN_OBJECT;
532 }
533 bool ret = data.WriteInt32(systemAbilityId);
534 if (!ret) {
535 HILOGW("SubscribeSystemAbility Write saId failed!");
536 return ERR_FLATTEN_OBJECT;
537 }
538
539 ret = data.WriteRemoteObject(listener->AsObject());
540 if (!ret) {
541 HILOGW("SubscribeSystemAbility Write listenerName failed!");
542 return ERR_FLATTEN_OBJECT;
543 }
544
545 MessageParcel reply;
546 MessageOption option;
547 int32_t err = remote->SendRequest(
548 static_cast<uint32_t>(SamgrInterfaceCode::SUBSCRIBE_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
549 if (err != ERR_NONE) {
550 HILOGE("SubscribeSystemAbility SendRequest error:%{public}d!", err);
551 return err;
552 }
553 HILOGD("SubscribeSystemAbility SendRequest succeed!");
554 int32_t result = 0;
555 ret = reply.ReadInt32(result);
556 if (!ret) {
557 HILOGW("SubscribeSystemAbility Read result failed!");
558 return ERR_FLATTEN_OBJECT;
559 }
560
561 return result;
562 }
563
UnSubscribeSystemAbility(int32_t systemAbilityId,const sptr<ISystemAbilityStatusChange> & listener)564 int32_t SystemAbilityManagerProxy::UnSubscribeSystemAbility(int32_t systemAbilityId,
565 const sptr<ISystemAbilityStatusChange>& listener)
566 {
567 HILOGD("%{public}s called, SA:%{public}d", __func__, systemAbilityId);
568 if (!CheckInputSysAbilityId(systemAbilityId) || listener == nullptr) {
569 HILOGE("UnSubscribeSystemAbility SA:%{public}d or listener invalid!", systemAbilityId);
570 return ERR_INVALID_VALUE;
571 }
572
573 sptr<IRemoteObject> remote = Remote();
574 if (remote == nullptr) {
575 HILOGI("remote is nullptr !");
576 return ERR_INVALID_OPERATION;
577 }
578
579 MessageParcel data;
580 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
581 return ERR_FLATTEN_OBJECT;
582 }
583 bool ret = data.WriteInt32(systemAbilityId);
584 if (!ret) {
585 HILOGW("UnSubscribeSystemAbility Write SAId failed!");
586 return ERR_FLATTEN_OBJECT;
587 }
588
589 ret = data.WriteRemoteObject(listener->AsObject());
590 if (!ret) {
591 HILOGW("UnSubscribeSystemAbility Write listenerSaId failed!");
592 return ERR_FLATTEN_OBJECT;
593 }
594
595 MessageParcel reply;
596 MessageOption option;
597 int32_t err = remote->SendRequest(
598 static_cast<uint32_t>(SamgrInterfaceCode::UNSUBSCRIBE_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
599 if (err != ERR_NONE) {
600 HILOGE("UnSubscribeSystemAbility SendRequest error:%{public}d!", err);
601 return err;
602 }
603 HILOGD("UnSubscribeSystemAbility SendRequest succeed!");
604 int32_t result = 0;
605 ret = reply.ReadInt32(result);
606 if (!ret) {
607 HILOGW("UnSubscribeSystemAbility Read result failed!");
608 return ERR_FLATTEN_OBJECT;
609 }
610
611 return result;
612 }
613
LoadSystemAbility(int32_t systemAbilityId,int32_t timeout)614 sptr<IRemoteObject> SystemAbilityManagerProxy::LoadSystemAbility(int32_t systemAbilityId, int32_t timeout)
615 {
616 if (timeout < MIN_TIMEOUT) {
617 timeout = MIN_TIMEOUT;
618 } else if (timeout > MAX_TIMEOUT) {
619 timeout = MAX_TIMEOUT;
620 }
621 sptr<SystemAbilityProxyCallback> callback = new SystemAbilityProxyCallback();
622 std::unique_lock<std::mutex> lock(callback->callbackLock_);
623 int32_t ret = LoadSystemAbility(systemAbilityId, callback);
624 if (ret != ERR_OK) {
625 HILOGE("LoadSystemAbility failed!");
626 return nullptr;
627 }
628 auto waitStatus = callback->cv_.wait_for(lock, std::chrono::seconds(timeout),
629 [&callback]() { return callback->loadproxy_ != nullptr; });
630 if (!waitStatus) {
631 HILOGE("LoadSystemAbility SA:%{public}d timeout", systemAbilityId);
632 return nullptr;
633 }
634 return callback->loadproxy_;
635 }
636
LoadSystemAbility(int32_t systemAbilityId,const sptr<ISystemAbilityLoadCallback> & callback)637 int32_t SystemAbilityManagerProxy::LoadSystemAbility(int32_t systemAbilityId,
638 const sptr<ISystemAbilityLoadCallback>& callback)
639 __attribute__((no_sanitize("cfi")))
640 {
641 if (!CheckInputSysAbilityId(systemAbilityId) || callback == nullptr) {
642 HILOGE("LoadSystemAbility SA:%{public}d or callback invalid!", systemAbilityId);
643 return ERR_INVALID_VALUE;
644 }
645
646 sptr<IRemoteObject> remote = Remote();
647 if (remote == nullptr) {
648 HILOGE("LoadSystemAbility remote is null!");
649 return ERR_INVALID_OPERATION;
650 }
651
652 MessageParcel data;
653 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
654 HILOGW("LoadSystemAbility Write interface token failed!");
655 return ERR_FLATTEN_OBJECT;
656 }
657 bool ret = data.WriteInt32(systemAbilityId);
658 if (!ret) {
659 HILOGW("LoadSystemAbility Write SAId failed!");
660 return ERR_FLATTEN_OBJECT;
661 }
662 ret = data.WriteRemoteObject(callback->AsObject());
663 if (!ret) {
664 HILOGW("LoadSystemAbility Write callback failed!");
665 return ERR_FLATTEN_OBJECT;
666 }
667
668 MessageParcel reply;
669 MessageOption option;
670 int32_t err = remote->SendRequest(
671 static_cast<uint32_t>(SamgrInterfaceCode::LOAD_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
672 if (err != ERR_NONE) {
673 HILOGE("LoadSystemAbility SA:%{public}d invalid error:%{public}d!", systemAbilityId, err);
674 return err;
675 }
676 HILOGD("LoadSystemAbility SA:%{public}d, SendRequest succeed!", systemAbilityId);
677 int32_t result = 0;
678 ret = reply.ReadInt32(result);
679 if (!ret) {
680 HILOGW("LoadSystemAbility Read reply failed!");
681 return ERR_FLATTEN_OBJECT;
682 }
683 return result;
684 }
685
LoadSystemAbility(int32_t systemAbilityId,const std::string & deviceId,const sptr<ISystemAbilityLoadCallback> & callback)686 int32_t SystemAbilityManagerProxy::LoadSystemAbility(int32_t systemAbilityId, const std::string& deviceId,
687 const sptr<ISystemAbilityLoadCallback>& callback)
688 {
689 if (!CheckInputSysAbilityId(systemAbilityId) || deviceId.empty() || callback == nullptr) {
690 HILOGE("LoadSystemAbility SA:%{public}d ,deviceId or callback invalid!", systemAbilityId);
691 return ERR_INVALID_VALUE;
692 }
693 sptr<IRemoteObject> remote = Remote();
694 if (remote == nullptr) {
695 HILOGE("LoadSystemAbility remote is null!");
696 return ERR_INVALID_OPERATION;
697 }
698
699 MessageParcel data;
700 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
701 HILOGW("LoadSystemAbility write interface token failed!");
702 return ERR_FLATTEN_OBJECT;
703 }
704 bool ret = data.WriteInt32(systemAbilityId);
705 if (!ret) {
706 HILOGW("LoadSystemAbility write SAId failed!");
707 return ERR_FLATTEN_OBJECT;
708 }
709 ret = data.WriteString(deviceId);
710 if (!ret) {
711 HILOGW("LoadSystemAbility write deviceId failed!");
712 return ERR_FLATTEN_OBJECT;
713 }
714 ret = data.WriteRemoteObject(callback->AsObject());
715 if (!ret) {
716 HILOGW("LoadSystemAbility Write callback failed!");
717 return ERR_FLATTEN_OBJECT;
718 }
719
720 MessageParcel reply;
721 MessageOption option;
722 int32_t err = remote->SendRequest(
723 static_cast<uint32_t>(SamgrInterfaceCode::LOAD_REMOTE_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
724 if (err != ERR_NONE) {
725 HILOGE("LoadSystemAbility SA:%{public}d invalid error:%{public}d!", systemAbilityId, err);
726 return err;
727 }
728 HILOGD("LoadSystemAbility SA:%{public}d for remote, SendRequest succeed!", systemAbilityId);
729 int32_t result = 0;
730 ret = reply.ReadInt32(result);
731 if (!ret) {
732 HILOGW("LoadSystemAbility read reply failed for remote!");
733 return ERR_FLATTEN_OBJECT;
734 }
735 return result;
736 }
737
UnloadSystemAbility(int32_t systemAbilityId)738 int32_t SystemAbilityManagerProxy::UnloadSystemAbility(int32_t systemAbilityId)
739 {
740 if (!CheckInputSysAbilityId(systemAbilityId)) {
741 HILOGE("UnloadSystemAbility SA:%{public}d invalid!", systemAbilityId);
742 return ERR_INVALID_VALUE;
743 }
744
745 sptr<IRemoteObject> remote = Remote();
746 if (remote == nullptr) {
747 HILOGE("UnloadSystemAbility remote is null!");
748 return ERR_INVALID_OPERATION;
749 }
750
751 MessageParcel data;
752 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
753 HILOGW("UnloadSystemAbility Write interface token failed!");
754 return ERR_FLATTEN_OBJECT;
755 }
756 bool ret = data.WriteInt32(systemAbilityId);
757 if (!ret) {
758 HILOGW("UnloadSystemAbility Write systemAbilityId failed!");
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::UNLOAD_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
766 if (err != ERR_NONE) {
767 HILOGE("UnloadSystemAbility SA:%{public}d invalid error:%{public}d!", systemAbilityId, err);
768 return err;
769 }
770 HILOGD("UnloadSystemAbility SA:%{public}d, SendRequest succeed!", systemAbilityId);
771 int32_t result = 0;
772 ret = reply.ReadInt32(result);
773 if (!ret) {
774 HILOGW("UnloadSystemAbility Read reply failed!");
775 return ERR_FLATTEN_OBJECT;
776 }
777 return result;
778 }
779
CancelUnloadSystemAbility(int32_t systemAbilityId)780 int32_t SystemAbilityManagerProxy::CancelUnloadSystemAbility(int32_t systemAbilityId)
781 {
782 if (!CheckInputSysAbilityId(systemAbilityId)) {
783 HILOGE("CancelUnloadSystemAbility SA:%{public}d invalid!", systemAbilityId);
784 return ERR_INVALID_VALUE;
785 }
786
787 sptr<IRemoteObject> remote = Remote();
788 if (remote == nullptr) {
789 HILOGE("CancelUnloadSystemAbility remote is null!");
790 return ERR_INVALID_OPERATION;
791 }
792
793 MessageParcel data;
794 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
795 HILOGW("CancelUnloadSystemAbility Write interface token failed!");
796 return ERR_FLATTEN_OBJECT;
797 }
798 bool ret = data.WriteInt32(systemAbilityId);
799 if (!ret) {
800 HILOGW("CancelUnloadSystemAbility Write SAId failed!");
801 return ERR_FLATTEN_OBJECT;
802 }
803
804 MessageParcel reply;
805 MessageOption option;
806 int32_t err = remote->SendRequest(
807 static_cast<uint32_t>(SamgrInterfaceCode::CANCEL_UNLOAD_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
808 if (err != ERR_NONE) {
809 HILOGE("CancelUnloadSystemAbility SA:%{public}d SendRequest failed, error:%{public}d!",
810 systemAbilityId, err);
811 return err;
812 }
813 HILOGD("CancelUnloadSystemAbility SA:%{public}d, SendRequest succeed!", systemAbilityId);
814 int32_t result = 0;
815 ret = reply.ReadInt32(result);
816 if (!ret) {
817 HILOGW("CancelUnloadSystemAbility Read reply failed!");
818 return ERR_FLATTEN_OBJECT;
819 }
820 return result;
821 }
822
UnloadAllIdleSystemAbility()823 int32_t SystemAbilityManagerProxy::UnloadAllIdleSystemAbility()
824 {
825 HILOGI("UnloadAllIdleSystemAbility called");
826 sptr<IRemoteObject> remote = Remote();
827 if (remote == nullptr) {
828 HILOGE("UnloadAllIdleSystemAbility remote is nullptr");
829 return ERR_INVALID_OPERATION;
830 }
831
832 MessageParcel data;
833 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
834 HILOGE("UnloadAllIdleSystemAbility write interface token failed");
835 return ERR_FLATTEN_OBJECT;
836 }
837
838 MessageParcel reply;
839 MessageOption option(MessageOption::TF_ASYNC);
840 int32_t err = remote->SendRequest(
841 static_cast<uint32_t>(SamgrInterfaceCode::UNLOAD_ALL_IDLE_SYSTEM_ABILITY_TRANSACTION), data, reply, option);
842 if (err != ERR_NONE) {
843 HILOGE("UnloadAllIdleSystemAbility SendRequest error:%{public}d", err);
844 return err;
845 }
846 HILOGD("UnloadAllIdleSystemAbility SendRequest succeed");
847 return ERR_OK;
848 }
849
MarshalSAExtraProp(const SAExtraProp & extraProp,MessageParcel & data) const850 int32_t SystemAbilityManagerProxy::MarshalSAExtraProp(const SAExtraProp& extraProp, MessageParcel& data) const
851 {
852 if (!data.WriteBool(extraProp.isDistributed)) {
853 HILOGW("MarshalSAExtraProp Write isDistributed failed!");
854 return ERR_FLATTEN_OBJECT;
855 }
856 if (!data.WriteInt32(extraProp.dumpFlags)) {
857 HILOGW("MarshalSAExtraProp Write dumpFlags failed!");
858 return ERR_FLATTEN_OBJECT;
859 }
860 if (!data.WriteString16(extraProp.capability)) {
861 HILOGW("MarshalSAExtraProp Write capability failed!");
862 return ERR_FLATTEN_OBJECT;
863 }
864 if (!data.WriteString16(extraProp.permission)) {
865 HILOGW("MarshalSAExtraProp Write defPermission failed!");
866 return ERR_FLATTEN_OBJECT;
867 }
868 return ERR_OK;
869 }
870
AddSystemAbility(int32_t systemAbilityId,const sptr<IRemoteObject> & ability,const SAExtraProp & extraProp)871 int32_t SystemAbilityManagerProxy::AddSystemAbility(int32_t systemAbilityId, const sptr<IRemoteObject>& ability,
872 const SAExtraProp& extraProp)
873 {
874 HILOGD("%{public}s called, SA:%{public}d", __func__, systemAbilityId);
875 if (!CheckInputSysAbilityId(systemAbilityId)) {
876 HILOGW("SA:%{public}d invalid.", systemAbilityId);
877 return ERR_INVALID_VALUE;
878 }
879
880 MessageParcel data;
881 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
882 return ERR_FLATTEN_OBJECT;
883 }
884 if (!data.WriteInt32(systemAbilityId)) {
885 HILOGW("AddSystemAbility Write saId failed!");
886 return ERR_FLATTEN_OBJECT;
887 }
888 if (!data.WriteRemoteObject(ability)) {
889 HILOGW("AddSystemAbility Write ability failed!");
890 return ERR_FLATTEN_OBJECT;
891 }
892
893 int32_t ret = MarshalSAExtraProp(extraProp, data);
894 if (ret != ERR_OK) {
895 HILOGW("AddSystemAbility MarshalSAExtraProp failed!");
896 return ret;
897 }
898
899 int32_t result = AddSystemAbilityWrapper(
900 static_cast<uint32_t>(SamgrInterfaceCode::ADD_SYSTEM_ABILITY_TRANSACTION), data);
901 if (result == ERR_OK) {
902 LocalAbilitys::GetInstance().AddAbility(systemAbilityId, ability);
903 }
904 return result;
905 }
906
AddSystemAbilityWrapper(int32_t code,MessageParcel & data)907 int32_t SystemAbilityManagerProxy::AddSystemAbilityWrapper(int32_t code, MessageParcel& data)
908 {
909 sptr<IRemoteObject> remote = Remote();
910 if (remote == nullptr) {
911 HILOGI("remote is nullptr !");
912 return ERR_INVALID_OPERATION;
913 }
914
915 MessageParcel reply;
916 MessageOption option;
917 int32_t err = remote->SendRequest(code, data, reply, option);
918 if (err != ERR_NONE) {
919 HILOGE("AddSystemAbility SA invalid error:%{public}d!", err);
920 return err;
921 }
922 int32_t result = 0;
923 bool ret = reply.ReadInt32(result);
924 if (!ret) {
925 HILOGE("AddSystemAbility read result error!");
926 return ERR_FLATTEN_OBJECT;
927 }
928 return result;
929 }
930
AddSystemProcess(const u16string & procName,const sptr<IRemoteObject> & procObject)931 int32_t SystemAbilityManagerProxy::AddSystemProcess(const u16string& procName, const sptr<IRemoteObject>& procObject)
932 {
933 HILOGD("%{public}s called, process name is %{public}s", __func__, Str16ToStr8(procName).c_str());
934 if (procName.empty()) {
935 HILOGI("process name is invalid!");
936 return ERR_INVALID_VALUE;
937 }
938
939 MessageParcel data;
940 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
941 return ERR_FLATTEN_OBJECT;
942 }
943 if (!data.WriteString16(procName)) {
944 HILOGW("AddSystemProcess Write name failed!");
945 return ERR_FLATTEN_OBJECT;
946 }
947
948 if (!data.WriteRemoteObject(procObject)) {
949 HILOGW("AddSystemProcess Write ability failed!");
950 return ERR_FLATTEN_OBJECT;
951 }
952 return AddSystemAbilityWrapper(
953 static_cast<uint32_t>(SamgrInterfaceCode::ADD_SYSTEM_PROCESS_TRANSACTION), data);
954 }
955
GetSystemProcessInfo(int32_t systemAbilityId,SystemProcessInfo & systemProcessInfo)956 int32_t SystemAbilityManagerProxy::GetSystemProcessInfo(int32_t systemAbilityId, SystemProcessInfo& systemProcessInfo)
957 {
958 HILOGD("GetSystemProcessInfo called");
959 sptr<IRemoteObject> remote = Remote();
960 if (remote == nullptr) {
961 HILOGI("GetSystemProcessInfo remote is nullptr");
962 return ERR_INVALID_OPERATION;
963 }
964
965 MessageParcel data;
966 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
967 return ERR_FLATTEN_OBJECT;
968 }
969 if (!data.WriteInt32(systemAbilityId)) {
970 HILOGW("GetSystemProcessInfo Write saId failed!");
971 return ERR_FLATTEN_OBJECT;
972 }
973 MessageParcel reply;
974 MessageOption option;
975 int32_t err = remote->SendRequest(
976 static_cast<uint32_t>(SamgrInterfaceCode::GET_SYSTEM_PROCESS_INFO_TRANSACTION), data, reply, option);
977 if (err != ERR_NONE) {
978 HILOGE("GetSystemProcessInfo SendRequest error: %{public}d!", err);
979 return err;
980 }
981 HILOGD("GetSystemProcessInfo SendRequest succeed!");
982 int32_t result = 0;
983 bool ret = reply.ReadInt32(result);
984 if (!ret) {
985 HILOGW("GetSystemProcessInfo Read result failed!");
986 return ERR_FLATTEN_OBJECT;
987 }
988 if (result != ERR_OK) {
989 HILOGE("GetSystemProcessInfo failed: %{public}d!", result);
990 return result;
991 }
992 return ReadProcessInfoFromParcel(reply, systemProcessInfo);
993 }
994
ReadProcessInfoFromParcel(MessageParcel & reply,SystemProcessInfo & systemProcessInfo)995 int32_t SystemAbilityManagerProxy::ReadProcessInfoFromParcel(MessageParcel& reply,
996 SystemProcessInfo& systemProcessInfo)
997 {
998 bool ret = reply.ReadString(systemProcessInfo.processName);
999 if (!ret) {
1000 HILOGW("GetSystemProcessInfo Read processName failed!");
1001 return ERR_FLATTEN_OBJECT;
1002 }
1003 ret = reply.ReadInt32(systemProcessInfo.pid);
1004 if (!ret) {
1005 HILOGW("GetSystemProcessInfo Read pid failed!");
1006 return ERR_FLATTEN_OBJECT;
1007 }
1008 ret = reply.ReadInt32(systemProcessInfo.uid);
1009 if (!ret) {
1010 HILOGW("GetSystemProcessInfo Read uid failed!");
1011 return ERR_FLATTEN_OBJECT;
1012 }
1013 return ERR_OK;
1014 }
1015
GetRunningSystemProcess(std::list<SystemProcessInfo> & systemProcessInfos)1016 int32_t SystemAbilityManagerProxy::GetRunningSystemProcess(std::list<SystemProcessInfo>& systemProcessInfos)
1017 {
1018 HILOGD("GetRunningSystemProcess called");
1019 sptr<IRemoteObject> remote = Remote();
1020 if (remote == nullptr) {
1021 HILOGI("GetRunningSystemProcess remote is nullptr");
1022 return ERR_INVALID_OPERATION;
1023 }
1024
1025 MessageParcel data;
1026 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1027 return ERR_FLATTEN_OBJECT;
1028 }
1029
1030 MessageParcel reply;
1031 MessageOption option;
1032 int32_t err = remote->SendRequest(
1033 static_cast<uint32_t>(SamgrInterfaceCode::GET_RUNNING_SYSTEM_PROCESS_TRANSACTION), data, reply, option);
1034 if (err != ERR_NONE) {
1035 HILOGE("GetRunningSystemProcess SendRequest error: %{public}d!", err);
1036 return err;
1037 }
1038 HILOGD("GetRunningSystemProcess SendRequest succeed!");
1039 int32_t result = 0;
1040 bool ret = reply.ReadInt32(result);
1041 if (!ret) {
1042 HILOGW("GetRunningSystemProcess Read result failed!");
1043 return ERR_FLATTEN_OBJECT;
1044 }
1045 if (result != ERR_OK) {
1046 HILOGE("GetRunningSystemProcess failed: %{public}d!", result);
1047 return result;
1048 }
1049 return ReadSystemProcessFromParcel(reply, systemProcessInfos);
1050 }
1051
ReadSystemProcessFromParcel(MessageParcel & reply,std::list<SystemProcessInfo> & systemProcessInfos)1052 int32_t SystemAbilityManagerProxy::ReadSystemProcessFromParcel(MessageParcel& reply,
1053 std::list<SystemProcessInfo>& systemProcessInfos)
1054 {
1055 int32_t size = 0;
1056 bool ret = reply.ReadInt32(size);
1057 if (!ret) {
1058 HILOGW("GetRunningSystemProcess Read list size failed!");
1059 return ERR_FLATTEN_OBJECT;
1060 }
1061 systemProcessInfos.clear();
1062 if (size == 0) {
1063 return ERR_OK;
1064 }
1065 if (static_cast<size_t>(size) > reply.GetReadableBytes() || size < 0) {
1066 HILOGE("Failed to read proc list, size=%{public}d", size);
1067 return ERR_FLATTEN_OBJECT;
1068 }
1069 for (int32_t i = 0; i < size; i++) {
1070 SystemProcessInfo systemProcessInfo;
1071 ret = reply.ReadString(systemProcessInfo.processName);
1072 if (!ret) {
1073 HILOGW("GetRunningSystemProcess Read processName failed!");
1074 return ERR_FLATTEN_OBJECT;
1075 }
1076 ret = reply.ReadInt32(systemProcessInfo.pid);
1077 if (!ret) {
1078 HILOGW("GetRunningSystemProcess Read pid failed!");
1079 return ERR_FLATTEN_OBJECT;
1080 }
1081 ret = reply.ReadInt32(systemProcessInfo.uid);
1082 if (!ret) {
1083 HILOGW("GetRunningSystemProcess Read uid failed!");
1084 return ERR_FLATTEN_OBJECT;
1085 }
1086 systemProcessInfos.emplace_back(systemProcessInfo);
1087 }
1088 return ERR_OK;
1089 }
1090
SubscribeSystemProcess(const sptr<ISystemProcessStatusChange> & listener)1091 int32_t SystemAbilityManagerProxy::SubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener)
1092 {
1093 HILOGD("SubscribeSystemProcess called");
1094 if (listener == nullptr) {
1095 HILOGE("SubscribeSystemProcess listener is nullptr");
1096 return ERR_INVALID_VALUE;
1097 }
1098
1099 sptr<IRemoteObject> remote = Remote();
1100 if (remote == nullptr) {
1101 HILOGI("SubscribeSystemProcess remote is nullptr");
1102 return ERR_INVALID_OPERATION;
1103 }
1104
1105 MessageParcel data;
1106 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1107 return ERR_FLATTEN_OBJECT;
1108 }
1109 bool ret = data.WriteRemoteObject(listener->AsObject());
1110 if (!ret) {
1111 HILOGW("SubscribeSystemProcess Write listenerName failed");
1112 return ERR_FLATTEN_OBJECT;
1113 }
1114
1115 MessageParcel reply;
1116 MessageOption option;
1117 int32_t err = remote->SendRequest(
1118 static_cast<uint32_t>(SamgrInterfaceCode::SUBSCRIBE_SYSTEM_PROCESS_TRANSACTION), data, reply, option);
1119 if (err != ERR_NONE) {
1120 HILOGE("SubscribeSystemProcess SendRequest error:%{public}d!", err);
1121 return err;
1122 }
1123 HILOGD("SubscribeSystemProcesss SendRequest succeed!");
1124 int32_t result = 0;
1125 ret = reply.ReadInt32(result);
1126 if (!ret) {
1127 HILOGW("SubscribeSystemProcess Read result failed!");
1128 return ERR_FLATTEN_OBJECT;
1129 }
1130 return result;
1131 }
1132
UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange> & listener)1133 int32_t SystemAbilityManagerProxy::UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener)
1134 {
1135 HILOGD("UnSubscribeSystemProcess called");
1136 if (listener == nullptr) {
1137 HILOGE("UnSubscribeSystemProcess listener is nullptr");
1138 return ERR_INVALID_VALUE;
1139 }
1140
1141 sptr<IRemoteObject> remote = Remote();
1142 if (remote == nullptr) {
1143 HILOGI("UnSubscribeSystemProcess remote is nullptr");
1144 return ERR_INVALID_OPERATION;
1145 }
1146
1147 MessageParcel data;
1148 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1149 return ERR_FLATTEN_OBJECT;
1150 }
1151 bool ret = data.WriteRemoteObject(listener->AsObject());
1152 if (!ret) {
1153 HILOGW("UnSubscribeSystemProcess Write listenerName failed");
1154 return ERR_FLATTEN_OBJECT;
1155 }
1156
1157 MessageParcel reply;
1158 MessageOption option;
1159 int32_t err = remote->SendRequest(
1160 static_cast<uint32_t>(SamgrInterfaceCode::UNSUBSCRIBE_SYSTEM_PROCESS_TRANSACTION), data, reply, option);
1161 if (err != ERR_NONE) {
1162 HILOGE("UnSubscribeSystemProcess SendRequest error:%{public}d!", err);
1163 return err;
1164 }
1165 HILOGD("UnSubscribeSystemProcess SendRequest succeed!");
1166 int32_t result = 0;
1167 ret = reply.ReadInt32(result);
1168 if (!ret) {
1169 HILOGW("UnSubscribeSystemProcess Read result failed!");
1170 return ERR_FLATTEN_OBJECT;
1171 }
1172 return result;
1173 }
1174
GetOnDemandReasonExtraData(int64_t extraDataId,MessageParcel & extraDataParcel)1175 int32_t SystemAbilityManagerProxy::GetOnDemandReasonExtraData(int64_t extraDataId, MessageParcel& extraDataParcel)
1176 {
1177 HILOGD("GetOnDemandReasonExtraData called");
1178 sptr<IRemoteObject> remote = Remote();
1179 if (remote == nullptr) {
1180 HILOGE("GetOnDemandReasonExtraData remote is nullptr");
1181 return ERR_INVALID_OPERATION;
1182 }
1183
1184 MessageParcel data;
1185 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1186 HILOGE("GetOnDemandReasonExtraData write interface token failed");
1187 return ERR_FLATTEN_OBJECT;
1188 }
1189 if (!data.WriteInt64(extraDataId)) {
1190 HILOGE("GetOnDemandReasonExtraData write extraDataId failed");
1191 return ERR_FLATTEN_OBJECT;
1192 }
1193
1194 MessageOption option;
1195 int32_t err = remote->SendRequest(
1196 static_cast<uint32_t>(SamgrInterfaceCode::GET_ONDEMAND_REASON_EXTRA_DATA_TRANSACTION),
1197 data, extraDataParcel, option);
1198 if (err != ERR_NONE) {
1199 HILOGE("GetOnDemandReasonExtraData SendRequest error:%{public}d", err);
1200 return err;
1201 }
1202 HILOGD("GetOnDemandReasonExtraData SendRequest succeed");
1203 int32_t result = 0;
1204 if (!extraDataParcel.ReadInt32(result)) {
1205 HILOGE("GetOnDemandReasonExtraData read result failed");
1206 return ERR_FLATTEN_OBJECT;
1207 }
1208 return result;
1209 }
1210
GetOnDemandPolicy(int32_t systemAbilityId,OnDemandPolicyType type,std::vector<SystemAbilityOnDemandEvent> & abilityOnDemandEvents)1211 int32_t SystemAbilityManagerProxy::GetOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type,
1212 std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents)
1213 {
1214 HILOGD("GetOnDemandPolicy called");
1215 sptr<IRemoteObject> remote = Remote();
1216 if (remote == nullptr) {
1217 HILOGI("GetOnDemandPolicy remote is nullptr");
1218 return ERR_INVALID_OPERATION;
1219 }
1220
1221 MessageParcel data;
1222 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1223 HILOGE("GetOnDemandPolicy write interface token failed!");
1224 return ERR_FLATTEN_OBJECT;
1225 }
1226 if (!data.WriteInt32(systemAbilityId)) {
1227 HILOGE("GetOnDemandPolicy write said failed!");
1228 return ERR_FLATTEN_OBJECT;
1229 }
1230 if (!data.WriteInt32(static_cast<int32_t>(type))) {
1231 HILOGE("GetOnDemandPolicy write type failed!");
1232 return ERR_FLATTEN_OBJECT;
1233 }
1234
1235 MessageParcel reply;
1236 MessageOption option;
1237 int32_t err = remote->SendRequest(
1238 static_cast<uint32_t>(SamgrInterfaceCode::GET_ONDEAMND_POLICY_TRANSACTION), data, reply, option);
1239 if (err != ERR_NONE) {
1240 HILOGE("GetOnDemandPolicy SendRequest error: %{public}d!", err);
1241 return err;
1242 }
1243 HILOGD("GetOnDemandPolicy SendRequest succeed!");
1244 int32_t result = 0;
1245 if (!reply.ReadInt32(result)) {
1246 HILOGE("GetOnDemandPolicy Read result failed!");
1247 return ERR_FLATTEN_OBJECT;
1248 }
1249 if (result != ERR_OK) {
1250 HILOGE("GetOnDemandPolicy failed: %{public}d!", result);
1251 return result;
1252 }
1253 if (!OnDemandEventToParcel::ReadOnDemandEventsFromParcel(abilityOnDemandEvents, reply)) {
1254 HILOGE("GetOnDemandPolicy Read on demand events failed!");
1255 return ERR_FLATTEN_OBJECT;
1256 }
1257 return ERR_OK;
1258 }
1259
GetOnDemandSystemAbilityIds(std::vector<int32_t> & systemAbilityIds)1260 int32_t SystemAbilityManagerProxy::GetOnDemandSystemAbilityIds(std::vector<int32_t>& systemAbilityIds)
1261 {
1262 HILOGD("GetOnDemandSystemAbilityIds called");
1263 sptr<IRemoteObject> remote = Remote();
1264 if (remote == nullptr) {
1265 HILOGI("GetOnDemandSystemAbilityIds remote is nullptr");
1266 return ERR_INVALID_OPERATION;
1267 }
1268
1269 MessageParcel data;
1270 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1271 HILOGE("GetOnDemandPolicy write interface token failed!");
1272 return ERR_FLATTEN_OBJECT;
1273 }
1274
1275 MessageParcel reply;
1276 MessageOption option;
1277 int32_t err = remote->SendRequest(
1278 static_cast<uint32_t>(SamgrInterfaceCode::GET_ONDEMAND_SYSTEM_ABILITY_IDS_TRANSACTION), data, reply, option);
1279 if (err != ERR_NONE) {
1280 HILOGE("GetOnDemandSystemAbilityIds SendRequest error: %{public}d!", err);
1281 return err;
1282 }
1283 HILOGD("GetOnDemandSystemAbilityIds SendRequest succeed!");
1284 int32_t result = 0;
1285 if (!reply.ReadInt32(result)) {
1286 HILOGE("GetOnDemandSystemAbilityIds Read result failed!");
1287 return ERR_FLATTEN_OBJECT;
1288 }
1289 if (result != ERR_OK) {
1290 HILOGE("GetOnDemandSystemAbilityIds failed: %{public}d!", result);
1291 return result;
1292 }
1293 if (!reply.ReadInt32Vector(&systemAbilityIds)) {
1294 HILOGW("GetOnDemandSystemAbilityIds SAIds read reply failed");
1295 systemAbilityIds.clear();
1296 return ERR_FLATTEN_OBJECT;
1297 }
1298 return ERR_OK;
1299 }
1300
UpdateOnDemandPolicy(int32_t systemAbilityId,OnDemandPolicyType type,const std::vector<SystemAbilityOnDemandEvent> & abilityOnDemandEvents)1301 int32_t SystemAbilityManagerProxy::UpdateOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type,
1302 const std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents)
1303 {
1304 HILOGD("UpdateOnDemandPolicy called");
1305 sptr<IRemoteObject> remote = Remote();
1306 if (remote == nullptr) {
1307 HILOGI("UpdateOnDemandPolicy remote is nullptr");
1308 return ERR_INVALID_OPERATION;
1309 }
1310
1311 MessageParcel data;
1312 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1313 HILOGE("UpdateOnDemandPolicy write interface token failed!");
1314 return ERR_FLATTEN_OBJECT;
1315 }
1316 if (!data.WriteInt32(systemAbilityId)) {
1317 HILOGE("UpdateOnDemandPolicy write said failed!");
1318 return ERR_FLATTEN_OBJECT;
1319 }
1320 if (!data.WriteInt32(static_cast<int32_t>(type))) {
1321 HILOGE("UpdateOnDemandPolicy write type failed!");
1322 return ERR_FLATTEN_OBJECT;
1323 }
1324 if (!OnDemandEventToParcel::WriteOnDemandEventsToParcel(abilityOnDemandEvents, data)) {
1325 HILOGW("UpdateOnDemandPolicy write on demand events failed!");
1326 return ERR_FLATTEN_OBJECT;
1327 }
1328
1329 MessageParcel reply;
1330 MessageOption option;
1331 int32_t err = remote->SendRequest(
1332 static_cast<uint32_t>(SamgrInterfaceCode::UPDATE_ONDEAMND_POLICY_TRANSACTION), data, reply, option);
1333 if (err != ERR_NONE) {
1334 HILOGE("UpdateOnDemandPolicy SendRequest error: %{public}d!", err);
1335 return err;
1336 }
1337 HILOGD("UpdateOnDemandPolicy SendRequest succeed!");
1338 int32_t result = 0;
1339 if (!reply.ReadInt32(result)) {
1340 HILOGE("UpdateOnDemandPolicy Read result failed!");
1341 return ERR_FLATTEN_OBJECT;
1342 }
1343 if (result != ERR_OK) {
1344 HILOGE("UpdateOnDemandPolicy failed: %{public}d!", result);
1345 }
1346 return result;
1347 }
1348
SendStrategy(int32_t type,std::vector<int32_t> & systemAbilityIds,int32_t level,std::string & action)1349 int32_t SystemAbilityManagerProxy::SendStrategy(int32_t type, std::vector<int32_t>& systemAbilityIds,
1350 int32_t level, std::string& action)
1351 {
1352 HILOGD("SendStrategy called");
1353 sptr<IRemoteObject> remote = Remote();
1354 if (remote == nullptr) {
1355 HILOGI("SendStrategy remote is nullptr");
1356 return ERR_INVALID_OPERATION;
1357 }
1358
1359 MessageParcel data;
1360 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1361 HILOGE("SendStrategy write interface token failed!");
1362 return ERR_FLATTEN_OBJECT;
1363 }
1364 if (!data.WriteInt32(type)) {
1365 HILOGE("SendStrategy write type failed!");
1366 return ERR_FLATTEN_OBJECT;
1367 }
1368 if (!data.WriteInt32Vector(systemAbilityIds)) {
1369 HILOGE("SendStrategy write said failed!");
1370 return ERR_FLATTEN_OBJECT;
1371 }
1372 if (!data.WriteInt32(level)) {
1373 HILOGE("SendStrategy write level failed!");
1374 return ERR_FLATTEN_OBJECT;
1375 }
1376 if (!data.WriteString(action)) {
1377 HILOGW("SendStrategy write action failed!");
1378 return ERR_FLATTEN_OBJECT;
1379 }
1380
1381 MessageParcel reply;
1382 MessageOption option;
1383 int32_t err = remote->SendRequest(
1384 static_cast<uint32_t>(SamgrInterfaceCode::SEND_STRATEGY_TRANASACTION), data, reply, option);
1385 if (err != ERR_NONE) {
1386 HILOGE("SendStrategy SendRequest error: %{public}d!", err);
1387 return err;
1388 }
1389 HILOGD("SendStrategy SendRequest succeed!");
1390 int32_t result = 0;
1391 if (!reply.ReadInt32(result)) {
1392 HILOGE("SendStrategy Read result failed!");
1393 return ERR_FLATTEN_OBJECT;
1394 }
1395 if (result != ERR_OK) {
1396 HILOGE("SendStrategy failed: %{public}d!", result);
1397 }
1398 return result;
1399 }
1400
ListExtensionSendReq(const std::string & extension,SamgrInterfaceCode cmd,MessageParcel & reply,MessageOption & option)1401 int32_t SystemAbilityManagerProxy::ListExtensionSendReq(const std::string& extension,
1402 SamgrInterfaceCode cmd, MessageParcel& reply, MessageOption& option)
1403 {
1404 sptr<IRemoteObject> remote = Remote();
1405 if (remote == nullptr) {
1406 HILOGE("remote is nullptr !");
1407 return ERR_INVALID_OPERATION;
1408 }
1409 MessageParcel data;
1410 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1411 HILOGW("%{public}s write token failed!", __func__);
1412 return ERR_FLATTEN_OBJECT;
1413 }
1414 if (!data.WriteString(extension)) {
1415 HILOGW("%{public}s write extension failed!", __func__);
1416 return ERR_FLATTEN_OBJECT;
1417 }
1418 int32_t err = remote->SendRequest(
1419 static_cast<uint32_t>(cmd), data, reply, option);
1420 if (err != ERR_NONE) {
1421 HILOGW("%{public}s transact failed!", __func__);
1422 return err;
1423 }
1424 int32_t result;
1425 if (!reply.ReadInt32(result)) {
1426 HILOGW("%{public}s Read result failed!", __func__);
1427 return ERR_FLATTEN_OBJECT;
1428 }
1429 return result;
1430 }
1431
GetExtensionSaIds(const std::string & extension,std::vector<int32_t> & saIds)1432 int32_t SystemAbilityManagerProxy::GetExtensionSaIds(const std::string& extension, std::vector<int32_t>& saIds)
1433 {
1434 HILOGD("%{public}s called", __func__);
1435
1436 MessageParcel reply;
1437 MessageOption option;
1438 int32_t ret = ListExtensionSendReq(extension,
1439 SamgrInterfaceCode::GET_EXTENSION_SA_IDS_TRANSCATION, reply, option);
1440 if (ret != ERR_OK) {
1441 return ret;
1442 }
1443 if (!reply.ReadInt32Vector(&saIds)) {
1444 HILOGW("%{public}s read reply failed", __func__);
1445 return ERR_FLATTEN_OBJECT;
1446 }
1447 return ERR_OK;
1448 }
1449
GetExtensionRunningSaList(const std::string & extension,std::vector<sptr<IRemoteObject>> & saList)1450 int32_t SystemAbilityManagerProxy::GetExtensionRunningSaList(const std::string& extension,
1451 std::vector<sptr<IRemoteObject>>& saList)
1452 {
1453 HILOGD("%{public}s called", __func__);
1454
1455 MessageParcel reply;
1456 MessageOption option;
1457 int32_t ret = ListExtensionSendReq(extension,
1458 SamgrInterfaceCode::GET_EXTERNSION_SA_LIST_TRANSCATION, reply, option);
1459 if (ret != ERR_OK) {
1460 return ret;
1461 }
1462 int32_t size;
1463 if (!reply.ReadInt32(size)) {
1464 HILOGW("%{public}s read reply failed", __func__);
1465 return ERR_FLATTEN_OBJECT;
1466 }
1467 for (int32_t i = 0; i < size; ++i) {
1468 sptr<IRemoteObject> obj = reply.ReadRemoteObject();
1469 if (obj == nullptr) {
1470 HILOGW("%{public}s read reply loop(%{public}d) size(%{public}d) failed", __func__, i, size);
1471 saList.clear();
1472 return ERR_FLATTEN_OBJECT;
1473 }
1474 saList.emplace_back(obj);
1475 }
1476 return ERR_OK;
1477 }
1478
GetRunningSaExtensionInfoList(const std::string & extension,std::vector<SaExtensionInfo> & infoList)1479 int32_t SystemAbilityManagerProxy::GetRunningSaExtensionInfoList(const std::string& extension,
1480 std::vector<SaExtensionInfo>& infoList)
1481 {
1482 HILOGD("%{public}s called", __func__);
1483 MessageParcel reply;
1484 MessageOption option;
1485 int32_t ret = ListExtensionSendReq(extension,
1486 SamgrInterfaceCode::GET_SA_EXTENSION_INFO_TRANSCATION, reply, option);
1487 if (ret != ERR_OK) {
1488 return ret;
1489 }
1490 int32_t size = 0;
1491 if (!reply.ReadInt32(size)) {
1492 HILOGW("get SaExtInfoList read reply size failed");
1493 return ERR_FLATTEN_OBJECT;
1494 }
1495 for (int32_t i = 0; i < size; ++i) {
1496 SaExtensionInfo tmp;
1497 if (!reply.ReadInt32(tmp.saId)) {
1498 HILOGW("get SaExtInfoList read reply id failed");
1499 infoList.clear();
1500 return ERR_FLATTEN_OBJECT;
1501 }
1502 tmp.processObj = reply.ReadRemoteObject();
1503 if (tmp.processObj == nullptr) {
1504 HILOGW("get SaExtInfoList read reply loop:%{public}d size:%{public}d failed", i, size);
1505 infoList.clear();
1506 return ERR_FLATTEN_OBJECT;
1507 }
1508 infoList.emplace_back(tmp);
1509 }
1510 return ERR_OK;
1511 }
1512
GetCommonEventExtraDataIdlist(int32_t saId,std::vector<int64_t> & extraDataIdList,const std::string & eventName)1513 int32_t SystemAbilityManagerProxy::GetCommonEventExtraDataIdlist(int32_t saId, std::vector<int64_t>& extraDataIdList,
1514 const std::string& eventName)
1515 {
1516 HILOGD("getExtraIdList called");
1517 sptr<IRemoteObject> remote = Remote();
1518 if (remote == nullptr) {
1519 HILOGE("getExtraIdList remote is nullptr");
1520 return ERR_INVALID_OPERATION;
1521 }
1522
1523 MessageParcel data;
1524 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1525 HILOGE("getExtraIdList write token failed!");
1526 return ERR_FLATTEN_OBJECT;
1527 }
1528 if (!data.WriteInt32(saId)) {
1529 HILOGE("getExtraIdList write said failed!");
1530 return ERR_FLATTEN_OBJECT;
1531 }
1532 if (!data.WriteString(eventName)) {
1533 HILOGW("getExtraIdList write eventname failed!");
1534 return ERR_FLATTEN_OBJECT;
1535 }
1536
1537 MessageParcel reply;
1538 MessageOption option;
1539 int32_t err = remote->SendRequest(
1540 static_cast<uint32_t>(SamgrInterfaceCode::GET_COMMON_EVENT_EXTRA_ID_LIST_TRANSCATION), data, reply, option);
1541 if (err != ERR_NONE) {
1542 HILOGE("getExtraIdList SendRequest error: %{public}d!", err);
1543 return err;
1544 }
1545 HILOGD("getExtraIdList SendRequest succeed!");
1546 int32_t result = 0;
1547 if (!reply.ReadInt32(result)) {
1548 HILOGE("getExtraIdList Read result failed!");
1549 return ERR_FLATTEN_OBJECT;
1550 }
1551 if (result != ERR_OK) {
1552 HILOGE("getExtraIdList failed: %{public}d!", result);
1553 return result;
1554 }
1555 if (!reply.ReadInt64Vector(&extraDataIdList)) {
1556 HILOGW("getExtraIdList read idlist failed");
1557 extraDataIdList.clear();
1558 return ERR_FLATTEN_OBJECT;
1559 }
1560 return ERR_OK;
1561 }
1562 } // namespace OHOS
1563