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