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 = "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
UnloadProcess(const std::vector<std::u16string> & processList)845 int32_t SystemAbilityManagerProxy::UnloadProcess(const std::vector<std::u16string>& processList)
846 {
847 HILOGI("UnloadProcess called");
848 sptr<IRemoteObject> remote = Remote();
849 if (remote == nullptr) {
850 HILOGE("UnloadProcess remote is nullptr");
851 return ERR_INVALID_OPERATION;
852 }
853
854 MessageParcel data;
855 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
856 HILOGE("UnloadProcess write interface token failed");
857 return ERR_FLATTEN_OBJECT;
858 }
859 if (!data.WriteString16Vector(processList)) {
860 HILOGW("UnloadProcess Write processList failed!");
861 return ERR_FLATTEN_OBJECT;
862 }
863 MessageParcel reply;
864 MessageOption option(MessageOption::TF_ASYNC);
865 int32_t err = remote->SendRequest(
866 static_cast<uint32_t>(SamgrInterfaceCode::UNLOAD_IDLE_PROCESS_BYLIST), data, reply, option);
867 if (err != ERR_NONE) {
868 HILOGE("UnloadProcess SendRequest error:%{public}d", err);
869 return err;
870 }
871 HILOGD("UnloadProcess SendRequest succeed");
872 return ERR_OK;
873 }
874
GetLruIdleSystemAbilityProc(std::vector<IdleProcessInfo> & procInfos)875 int32_t SystemAbilityManagerProxy::GetLruIdleSystemAbilityProc(std::vector<IdleProcessInfo>& procInfos)
876 {
877 HILOGI("GetLruIdleSystempAbilityProc called");
878 sptr<IRemoteObject> remote = Remote();
879 if (remote == nullptr) {
880 HILOGE("GetLruIdleSystempAbilityProc remote is nullptr");
881 return ERR_INVALID_OPERATION;
882 }
883
884 MessageParcel data;
885 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
886 HILOGE("GetLruIdleSystempAbilityProc write interface token failed");
887 return ERR_FLATTEN_OBJECT;
888 }
889
890 MessageParcel reply;
891 MessageOption option;
892 int32_t err = remote->SendRequest(
893 static_cast<uint32_t>(SamgrInterfaceCode::GET_LRU_IDLE_SYSTEM_ABILITY_PROCESS_TRANSACTION),
894 data, reply, option);
895 if (err != ERR_NONE) {
896 HILOGE("GetLruIdleSystempAbilityProc SendRequest error:%{public}d", err);
897 return err;
898 }
899 int32_t result = 0;
900 bool ret = reply.ReadInt32(result);
901 if (!ret) {
902 HILOGE("GetLruIdleSystempAbilityProc read resule failed");
903 return ERR_FLATTEN_OBJECT;
904 }
905 if (result != ERR_OK) {
906 HILOGE("GetLruIdleSystempAbilityProc failed: %{public}d!", result);
907 return result;
908 }
909 return ReadIdleProcessInfoFromParcel(reply, procInfos);
910 }
911
ReadIdleProcessInfoFromParcel(MessageParcel & reply,std::vector<IdleProcessInfo> & procInfos)912 int32_t SystemAbilityManagerProxy::ReadIdleProcessInfoFromParcel(MessageParcel& reply,
913 std::vector<IdleProcessInfo>& procInfos)
914 {
915 int32_t size = 0;
916 bool ret = reply.ReadInt32(size);
917 if (!ret) {
918 HILOGE("ReadIdleProcessInfoFromParcel read size failed");
919 return ERR_FLATTEN_OBJECT;
920 }
921 procInfos.clear();
922 if (size == 0) {
923 return ERR_OK;
924 }
925 if (static_cast<size_t>(size) > reply.GetReadableBytes() || size < 0) {
926 HILOGE("Failed to read proc list, size=%{public}d", size);
927 return ERR_FLATTEN_OBJECT;
928 }
929 for (int32_t i = 0; i < size; i++) {
930 IdleProcessInfo info;
931 ret = reply.ReadInt32(info.pid);
932 if (!ret) {
933 HILOGW("ReadIdleProcessInfoFromParcel Read pid failed!");
934 return ERR_FLATTEN_OBJECT;
935 }
936 ret = reply.ReadString16(info.processName);
937 if (!ret) {
938 HILOGW("ReadIdleProcessInfoFromParcel Read processName failed!");
939 return ERR_FLATTEN_OBJECT;
940 }
941 ret = reply.ReadInt64(info.lastIdleTime);
942 if (!ret) {
943 HILOGW("ReadIdleProcessInfoFromParcel Read uid failed!");
944 return ERR_FLATTEN_OBJECT;
945 }
946 procInfos.emplace_back(info);
947 }
948 return ERR_OK;
949 }
950
MarshalSAExtraProp(const SAExtraProp & extraProp,MessageParcel & data) const951 int32_t SystemAbilityManagerProxy::MarshalSAExtraProp(const SAExtraProp& extraProp, MessageParcel& data) const
952 {
953 if (!data.WriteBool(extraProp.isDistributed)) {
954 HILOGW("MarshalSAExtraProp Write isDistributed failed!");
955 return ERR_FLATTEN_OBJECT;
956 }
957 if (!data.WriteInt32(extraProp.dumpFlags)) {
958 HILOGW("MarshalSAExtraProp Write dumpFlags failed!");
959 return ERR_FLATTEN_OBJECT;
960 }
961 if (!data.WriteString16(extraProp.capability)) {
962 HILOGW("MarshalSAExtraProp Write capability failed!");
963 return ERR_FLATTEN_OBJECT;
964 }
965 if (!data.WriteString16(extraProp.permission)) {
966 HILOGW("MarshalSAExtraProp Write defPermission failed!");
967 return ERR_FLATTEN_OBJECT;
968 }
969 return ERR_OK;
970 }
971
AddSystemAbility(int32_t systemAbilityId,const sptr<IRemoteObject> & ability,const SAExtraProp & extraProp)972 int32_t SystemAbilityManagerProxy::AddSystemAbility(int32_t systemAbilityId, const sptr<IRemoteObject>& ability,
973 const SAExtraProp& extraProp)
974 {
975 HILOGD("%{public}s called, SA:%{public}d", __func__, systemAbilityId);
976 if (!CheckInputSysAbilityId(systemAbilityId)) {
977 HILOGW("SA:%{public}d invalid.", systemAbilityId);
978 return ERR_INVALID_VALUE;
979 }
980
981 MessageParcel data;
982 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
983 return ERR_FLATTEN_OBJECT;
984 }
985 if (!data.WriteInt32(systemAbilityId)) {
986 HILOGW("AddSystemAbility Write saId failed!");
987 return ERR_FLATTEN_OBJECT;
988 }
989 if (!data.WriteRemoteObject(ability)) {
990 HILOGW("AddSystemAbility Write ability failed!");
991 return ERR_FLATTEN_OBJECT;
992 }
993
994 int32_t ret = MarshalSAExtraProp(extraProp, data);
995 if (ret != ERR_OK) {
996 HILOGW("AddSystemAbility MarshalSAExtraProp failed!");
997 return ret;
998 }
999
1000 int32_t result = AddSystemAbilityWrapper(
1001 static_cast<uint32_t>(SamgrInterfaceCode::ADD_SYSTEM_ABILITY_TRANSACTION), data);
1002 if (result == ERR_OK) {
1003 LocalAbilitys::GetInstance().AddAbility(systemAbilityId, ability);
1004 }
1005 return result;
1006 }
1007
AddSystemAbilityWrapper(int32_t code,MessageParcel & data)1008 int32_t SystemAbilityManagerProxy::AddSystemAbilityWrapper(int32_t code, MessageParcel& data)
1009 {
1010 sptr<IRemoteObject> remote = Remote();
1011 if (remote == nullptr) {
1012 HILOGI("remote is nullptr !");
1013 return ERR_INVALID_OPERATION;
1014 }
1015
1016 MessageParcel reply;
1017 MessageOption option;
1018 int32_t err = remote->SendRequest(code, data, reply, option);
1019 if (err != ERR_NONE) {
1020 HILOGE("AddSystemAbility SA invalid error:%{public}d!", err);
1021 return err;
1022 }
1023 int32_t result = 0;
1024 bool ret = reply.ReadInt32(result);
1025 if (!ret) {
1026 HILOGE("AddSystemAbility read result error!");
1027 return ERR_FLATTEN_OBJECT;
1028 }
1029 return result;
1030 }
1031
AddSystemProcess(const u16string & procName,const sptr<IRemoteObject> & procObject)1032 int32_t SystemAbilityManagerProxy::AddSystemProcess(const u16string& procName, const sptr<IRemoteObject>& procObject)
1033 {
1034 HILOGD("%{public}s called, process name is %{public}s", __func__, Str16ToStr8(procName).c_str());
1035 if (procName.empty()) {
1036 HILOGI("process name is invalid!");
1037 return ERR_INVALID_VALUE;
1038 }
1039
1040 MessageParcel data;
1041 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1042 return ERR_FLATTEN_OBJECT;
1043 }
1044 if (!data.WriteString16(procName)) {
1045 HILOGW("AddSystemProcess Write name failed!");
1046 return ERR_FLATTEN_OBJECT;
1047 }
1048
1049 if (!data.WriteRemoteObject(procObject)) {
1050 HILOGW("AddSystemProcess Write ability failed!");
1051 return ERR_FLATTEN_OBJECT;
1052 }
1053 return AddSystemAbilityWrapper(
1054 static_cast<uint32_t>(SamgrInterfaceCode::ADD_SYSTEM_PROCESS_TRANSACTION), data);
1055 }
1056
GetSystemProcessInfo(int32_t systemAbilityId,SystemProcessInfo & systemProcessInfo)1057 int32_t SystemAbilityManagerProxy::GetSystemProcessInfo(int32_t systemAbilityId, SystemProcessInfo& systemProcessInfo)
1058 {
1059 HILOGD("GetSystemProcessInfo called");
1060 sptr<IRemoteObject> remote = Remote();
1061 if (remote == nullptr) {
1062 HILOGI("GetSystemProcessInfo remote is nullptr");
1063 return ERR_INVALID_OPERATION;
1064 }
1065
1066 MessageParcel data;
1067 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1068 return ERR_FLATTEN_OBJECT;
1069 }
1070 if (!data.WriteInt32(systemAbilityId)) {
1071 HILOGW("GetSystemProcessInfo Write saId failed!");
1072 return ERR_FLATTEN_OBJECT;
1073 }
1074 MessageParcel reply;
1075 MessageOption option;
1076 int32_t err = remote->SendRequest(
1077 static_cast<uint32_t>(SamgrInterfaceCode::GET_SYSTEM_PROCESS_INFO_TRANSACTION), data, reply, option);
1078 if (err != ERR_NONE) {
1079 HILOGE("GetSystemProcessInfo SendRequest error: %{public}d!", err);
1080 return err;
1081 }
1082 HILOGD("GetSystemProcessInfo SendRequest succeed!");
1083 int32_t result = 0;
1084 bool ret = reply.ReadInt32(result);
1085 if (!ret) {
1086 HILOGW("GetSystemProcessInfo Read result failed!");
1087 return ERR_FLATTEN_OBJECT;
1088 }
1089 if (result != ERR_OK) {
1090 HILOGE("GetSystemProcessInfo failed: %{public}d!", result);
1091 return result;
1092 }
1093 return ReadProcessInfoFromParcel(reply, systemProcessInfo);
1094 }
1095
ReadProcessInfoFromParcel(MessageParcel & reply,SystemProcessInfo & systemProcessInfo)1096 int32_t SystemAbilityManagerProxy::ReadProcessInfoFromParcel(MessageParcel& reply,
1097 SystemProcessInfo& systemProcessInfo)
1098 {
1099 bool ret = reply.ReadString(systemProcessInfo.processName);
1100 if (!ret) {
1101 HILOGW("GetSystemProcessInfo Read processName failed!");
1102 return ERR_FLATTEN_OBJECT;
1103 }
1104 ret = reply.ReadInt32(systemProcessInfo.pid);
1105 if (!ret) {
1106 HILOGW("GetSystemProcessInfo Read pid failed!");
1107 return ERR_FLATTEN_OBJECT;
1108 }
1109 ret = reply.ReadInt32(systemProcessInfo.uid);
1110 if (!ret) {
1111 HILOGW("GetSystemProcessInfo Read uid failed!");
1112 return ERR_FLATTEN_OBJECT;
1113 }
1114 return ERR_OK;
1115 }
1116
GetRunningSystemProcess(std::list<SystemProcessInfo> & systemProcessInfos)1117 int32_t SystemAbilityManagerProxy::GetRunningSystemProcess(std::list<SystemProcessInfo>& systemProcessInfos)
1118 {
1119 HILOGD("GetRunningSystemProcess called");
1120 sptr<IRemoteObject> remote = Remote();
1121 if (remote == nullptr) {
1122 HILOGI("GetRunningSystemProcess remote is nullptr");
1123 return ERR_INVALID_OPERATION;
1124 }
1125
1126 MessageParcel data;
1127 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1128 return ERR_FLATTEN_OBJECT;
1129 }
1130
1131 MessageParcel reply;
1132 MessageOption option;
1133 int32_t err = remote->SendRequest(
1134 static_cast<uint32_t>(SamgrInterfaceCode::GET_RUNNING_SYSTEM_PROCESS_TRANSACTION), data, reply, option);
1135 if (err != ERR_NONE) {
1136 HILOGE("GetRunningSystemProcess SendRequest error: %{public}d!", err);
1137 return err;
1138 }
1139 HILOGD("GetRunningSystemProcess SendRequest succeed!");
1140 int32_t result = 0;
1141 bool ret = reply.ReadInt32(result);
1142 if (!ret) {
1143 HILOGW("GetRunningSystemProcess Read result failed!");
1144 return ERR_FLATTEN_OBJECT;
1145 }
1146 if (result != ERR_OK) {
1147 HILOGE("GetRunningSystemProcess failed: %{public}d!", result);
1148 return result;
1149 }
1150 return ReadSystemProcessFromParcel(reply, systemProcessInfos);
1151 }
1152
ReadSystemProcessFromParcel(MessageParcel & reply,std::list<SystemProcessInfo> & systemProcessInfos)1153 int32_t SystemAbilityManagerProxy::ReadSystemProcessFromParcel(MessageParcel& reply,
1154 std::list<SystemProcessInfo>& systemProcessInfos)
1155 {
1156 int32_t size = 0;
1157 bool ret = reply.ReadInt32(size);
1158 if (!ret) {
1159 HILOGW("GetRunningSystemProcess Read list size failed!");
1160 return ERR_FLATTEN_OBJECT;
1161 }
1162 systemProcessInfos.clear();
1163 if (size == 0) {
1164 return ERR_OK;
1165 }
1166 if (static_cast<size_t>(size) > reply.GetReadableBytes() || size < 0) {
1167 HILOGE("Failed to read proc list, size=%{public}d", size);
1168 return ERR_FLATTEN_OBJECT;
1169 }
1170 for (int32_t i = 0; i < size; i++) {
1171 SystemProcessInfo systemProcessInfo;
1172 ret = reply.ReadString(systemProcessInfo.processName);
1173 if (!ret) {
1174 HILOGW("GetRunningSystemProcess Read processName failed!");
1175 return ERR_FLATTEN_OBJECT;
1176 }
1177 ret = reply.ReadInt32(systemProcessInfo.pid);
1178 if (!ret) {
1179 HILOGW("GetRunningSystemProcess Read pid failed!");
1180 return ERR_FLATTEN_OBJECT;
1181 }
1182 ret = reply.ReadInt32(systemProcessInfo.uid);
1183 if (!ret) {
1184 HILOGW("GetRunningSystemProcess Read uid failed!");
1185 return ERR_FLATTEN_OBJECT;
1186 }
1187 systemProcessInfos.emplace_back(systemProcessInfo);
1188 }
1189 return ERR_OK;
1190 }
1191
SubscribeSystemProcess(const sptr<ISystemProcessStatusChange> & listener)1192 int32_t SystemAbilityManagerProxy::SubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener)
1193 {
1194 HILOGD("SubscribeSystemProcess called");
1195 if (listener == nullptr) {
1196 HILOGE("SubscribeSystemProcess listener is nullptr");
1197 return ERR_INVALID_VALUE;
1198 }
1199
1200 sptr<IRemoteObject> remote = Remote();
1201 if (remote == nullptr) {
1202 HILOGI("SubscribeSystemProcess remote is nullptr");
1203 return ERR_INVALID_OPERATION;
1204 }
1205
1206 MessageParcel data;
1207 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1208 return ERR_FLATTEN_OBJECT;
1209 }
1210 bool ret = data.WriteRemoteObject(listener->AsObject());
1211 if (!ret) {
1212 HILOGW("SubscribeSystemProcess Write listenerName failed");
1213 return ERR_FLATTEN_OBJECT;
1214 }
1215
1216 MessageParcel reply;
1217 MessageOption option;
1218 int32_t err = remote->SendRequest(
1219 static_cast<uint32_t>(SamgrInterfaceCode::SUBSCRIBE_SYSTEM_PROCESS_TRANSACTION), data, reply, option);
1220 if (err != ERR_NONE) {
1221 HILOGE("SubscribeSystemProcess SendRequest error:%{public}d!", err);
1222 return err;
1223 }
1224 HILOGD("SubscribeSystemProcesss SendRequest succeed!");
1225 int32_t result = 0;
1226 ret = reply.ReadInt32(result);
1227 if (!ret) {
1228 HILOGW("SubscribeSystemProcess Read result failed!");
1229 return ERR_FLATTEN_OBJECT;
1230 }
1231 return result;
1232 }
1233
UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange> & listener)1234 int32_t SystemAbilityManagerProxy::UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener)
1235 {
1236 HILOGD("UnSubscribeSystemProcess called");
1237 if (listener == nullptr) {
1238 HILOGE("UnSubscribeSystemProcess listener is nullptr");
1239 return ERR_INVALID_VALUE;
1240 }
1241
1242 sptr<IRemoteObject> remote = Remote();
1243 if (remote == nullptr) {
1244 HILOGI("UnSubscribeSystemProcess remote is nullptr");
1245 return ERR_INVALID_OPERATION;
1246 }
1247
1248 MessageParcel data;
1249 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1250 return ERR_FLATTEN_OBJECT;
1251 }
1252 bool ret = data.WriteRemoteObject(listener->AsObject());
1253 if (!ret) {
1254 HILOGW("UnSubscribeSystemProcess Write listenerName failed");
1255 return ERR_FLATTEN_OBJECT;
1256 }
1257
1258 MessageParcel reply;
1259 MessageOption option;
1260 int32_t err = remote->SendRequest(
1261 static_cast<uint32_t>(SamgrInterfaceCode::UNSUBSCRIBE_SYSTEM_PROCESS_TRANSACTION), data, reply, option);
1262 if (err != ERR_NONE) {
1263 HILOGE("UnSubscribeSystemProcess SendRequest error:%{public}d!", err);
1264 return err;
1265 }
1266 HILOGD("UnSubscribeSystemProcess SendRequest succeed!");
1267 int32_t result = 0;
1268 ret = reply.ReadInt32(result);
1269 if (!ret) {
1270 HILOGW("UnSubscribeSystemProcess Read result failed!");
1271 return ERR_FLATTEN_OBJECT;
1272 }
1273 return result;
1274 }
1275
GetOnDemandReasonExtraData(int64_t extraDataId,MessageParcel & extraDataParcel)1276 int32_t SystemAbilityManagerProxy::GetOnDemandReasonExtraData(int64_t extraDataId, MessageParcel& extraDataParcel)
1277 {
1278 HILOGD("GetOnDemandReasonExtraData called");
1279 sptr<IRemoteObject> remote = Remote();
1280 if (remote == nullptr) {
1281 HILOGE("GetOnDemandReasonExtraData remote is nullptr");
1282 return ERR_INVALID_OPERATION;
1283 }
1284
1285 MessageParcel data;
1286 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1287 HILOGE("GetOnDemandReasonExtraData write interface token failed");
1288 return ERR_FLATTEN_OBJECT;
1289 }
1290 if (!data.WriteInt64(extraDataId)) {
1291 HILOGE("GetOnDemandReasonExtraData write extraDataId failed");
1292 return ERR_FLATTEN_OBJECT;
1293 }
1294
1295 MessageOption option;
1296 int32_t err = remote->SendRequest(
1297 static_cast<uint32_t>(SamgrInterfaceCode::GET_ONDEMAND_REASON_EXTRA_DATA_TRANSACTION),
1298 data, extraDataParcel, option);
1299 if (err != ERR_NONE) {
1300 HILOGE("GetOnDemandReasonExtraData SendRequest error:%{public}d", err);
1301 return err;
1302 }
1303 HILOGD("GetOnDemandReasonExtraData SendRequest succeed");
1304 int32_t result = 0;
1305 if (!extraDataParcel.ReadInt32(result)) {
1306 HILOGE("GetOnDemandReasonExtraData read result failed");
1307 return ERR_FLATTEN_OBJECT;
1308 }
1309 return result;
1310 }
1311
GetOnDemandPolicy(int32_t systemAbilityId,OnDemandPolicyType type,std::vector<SystemAbilityOnDemandEvent> & abilityOnDemandEvents)1312 int32_t SystemAbilityManagerProxy::GetOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type,
1313 std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents)
1314 {
1315 HILOGD("GetOnDemandPolicy called");
1316 sptr<IRemoteObject> remote = Remote();
1317 if (remote == nullptr) {
1318 HILOGI("GetOnDemandPolicy remote is nullptr");
1319 return ERR_INVALID_OPERATION;
1320 }
1321
1322 MessageParcel data;
1323 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1324 HILOGE("GetOnDemandPolicy write interface token failed!");
1325 return ERR_FLATTEN_OBJECT;
1326 }
1327 if (!data.WriteInt32(systemAbilityId)) {
1328 HILOGE("GetOnDemandPolicy write said failed!");
1329 return ERR_FLATTEN_OBJECT;
1330 }
1331 if (!data.WriteInt32(static_cast<int32_t>(type))) {
1332 HILOGE("GetOnDemandPolicy write type failed!");
1333 return ERR_FLATTEN_OBJECT;
1334 }
1335
1336 MessageParcel reply;
1337 MessageOption option;
1338 int32_t err = remote->SendRequest(
1339 static_cast<uint32_t>(SamgrInterfaceCode::GET_ONDEAMND_POLICY_TRANSACTION), data, reply, option);
1340 if (err != ERR_NONE) {
1341 HILOGE("GetOnDemandPolicy SendRequest error: %{public}d!", err);
1342 return err;
1343 }
1344 HILOGD("GetOnDemandPolicy SendRequest succeed!");
1345 int32_t result = 0;
1346 if (!reply.ReadInt32(result)) {
1347 HILOGE("GetOnDemandPolicy Read result failed!");
1348 return ERR_FLATTEN_OBJECT;
1349 }
1350 if (result != ERR_OK) {
1351 HILOGE("GetOnDemandPolicy failed: %{public}d!", result);
1352 return result;
1353 }
1354 if (!OnDemandEventToParcel::ReadOnDemandEventsFromParcel(abilityOnDemandEvents, reply)) {
1355 HILOGE("GetOnDemandPolicy Read on demand events failed!");
1356 return ERR_FLATTEN_OBJECT;
1357 }
1358 return ERR_OK;
1359 }
1360
GetOnDemandSystemAbilityIds(std::vector<int32_t> & systemAbilityIds)1361 int32_t SystemAbilityManagerProxy::GetOnDemandSystemAbilityIds(std::vector<int32_t>& systemAbilityIds)
1362 {
1363 HILOGD("GetOnDemandSystemAbilityIds called");
1364 sptr<IRemoteObject> remote = Remote();
1365 if (remote == nullptr) {
1366 HILOGI("GetOnDemandSystemAbilityIds remote is nullptr");
1367 return ERR_INVALID_OPERATION;
1368 }
1369
1370 MessageParcel data;
1371 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1372 HILOGE("GetOnDemandPolicy write interface token 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::GET_ONDEMAND_SYSTEM_ABILITY_IDS_TRANSACTION), data, reply, option);
1380 if (err != ERR_NONE) {
1381 HILOGE("GetOnDemandSystemAbilityIds SendRequest error: %{public}d!", err);
1382 return err;
1383 }
1384 HILOGD("GetOnDemandSystemAbilityIds SendRequest succeed!");
1385 int32_t result = 0;
1386 if (!reply.ReadInt32(result)) {
1387 HILOGE("GetOnDemandSystemAbilityIds Read result failed!");
1388 return ERR_FLATTEN_OBJECT;
1389 }
1390 if (result != ERR_OK) {
1391 HILOGE("GetOnDemandSystemAbilityIds failed: %{public}d!", result);
1392 return result;
1393 }
1394 if (!reply.ReadInt32Vector(&systemAbilityIds)) {
1395 HILOGW("GetOnDemandSystemAbilityIds SAIds read reply failed");
1396 systemAbilityIds.clear();
1397 return ERR_FLATTEN_OBJECT;
1398 }
1399 return ERR_OK;
1400 }
1401
UpdateOnDemandPolicy(int32_t systemAbilityId,OnDemandPolicyType type,const std::vector<SystemAbilityOnDemandEvent> & abilityOnDemandEvents)1402 int32_t SystemAbilityManagerProxy::UpdateOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type,
1403 const std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents)
1404 {
1405 HILOGD("UpdateOnDemandPolicy called");
1406 sptr<IRemoteObject> remote = Remote();
1407 if (remote == nullptr) {
1408 HILOGI("UpdateOnDemandPolicy remote is nullptr");
1409 return ERR_INVALID_OPERATION;
1410 }
1411
1412 MessageParcel data;
1413 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1414 HILOGE("UpdateOnDemandPolicy write interface token failed!");
1415 return ERR_FLATTEN_OBJECT;
1416 }
1417 if (!data.WriteInt32(systemAbilityId)) {
1418 HILOGE("UpdateOnDemandPolicy write said failed!");
1419 return ERR_FLATTEN_OBJECT;
1420 }
1421 if (!data.WriteInt32(static_cast<int32_t>(type))) {
1422 HILOGE("UpdateOnDemandPolicy write type failed!");
1423 return ERR_FLATTEN_OBJECT;
1424 }
1425 if (!OnDemandEventToParcel::WriteOnDemandEventsToParcel(abilityOnDemandEvents, data)) {
1426 HILOGW("UpdateOnDemandPolicy write on demand events failed!");
1427 return ERR_FLATTEN_OBJECT;
1428 }
1429
1430 MessageParcel reply;
1431 MessageOption option;
1432 int32_t err = remote->SendRequest(
1433 static_cast<uint32_t>(SamgrInterfaceCode::UPDATE_ONDEAMND_POLICY_TRANSACTION), data, reply, option);
1434 if (err != ERR_NONE) {
1435 HILOGE("UpdateOnDemandPolicy SendRequest error: %{public}d!", err);
1436 return err;
1437 }
1438 HILOGD("UpdateOnDemandPolicy SendRequest succeed!");
1439 int32_t result = 0;
1440 if (!reply.ReadInt32(result)) {
1441 HILOGE("UpdateOnDemandPolicy Read result failed!");
1442 return ERR_FLATTEN_OBJECT;
1443 }
1444 if (result != ERR_OK) {
1445 HILOGE("UpdateOnDemandPolicy failed: %{public}d!", result);
1446 }
1447 return result;
1448 }
1449
SendStrategy(int32_t type,std::vector<int32_t> & systemAbilityIds,int32_t level,std::string & action)1450 int32_t SystemAbilityManagerProxy::SendStrategy(int32_t type, std::vector<int32_t>& systemAbilityIds,
1451 int32_t level, std::string& action)
1452 {
1453 HILOGD("SendStrategy called");
1454 sptr<IRemoteObject> remote = Remote();
1455 if (remote == nullptr) {
1456 HILOGI("SendStrategy remote is nullptr");
1457 return ERR_INVALID_OPERATION;
1458 }
1459
1460 MessageParcel data;
1461 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1462 HILOGE("SendStrategy write interface token failed!");
1463 return ERR_FLATTEN_OBJECT;
1464 }
1465 if (!data.WriteInt32(type)) {
1466 HILOGE("SendStrategy write type failed!");
1467 return ERR_FLATTEN_OBJECT;
1468 }
1469 if (!data.WriteInt32Vector(systemAbilityIds)) {
1470 HILOGE("SendStrategy write said failed!");
1471 return ERR_FLATTEN_OBJECT;
1472 }
1473 if (!data.WriteInt32(level)) {
1474 HILOGE("SendStrategy write level failed!");
1475 return ERR_FLATTEN_OBJECT;
1476 }
1477 if (!data.WriteString(action)) {
1478 HILOGW("SendStrategy write action failed!");
1479 return ERR_FLATTEN_OBJECT;
1480 }
1481
1482 MessageParcel reply;
1483 MessageOption option;
1484 int32_t err = remote->SendRequest(
1485 static_cast<uint32_t>(SamgrInterfaceCode::SEND_STRATEGY_TRANASACTION), data, reply, option);
1486 if (err != ERR_NONE) {
1487 HILOGE("SendStrategy SendRequest error: %{public}d!", err);
1488 return err;
1489 }
1490 HILOGD("SendStrategy SendRequest succeed!");
1491 int32_t result = 0;
1492 if (!reply.ReadInt32(result)) {
1493 HILOGE("SendStrategy Read result failed!");
1494 return ERR_FLATTEN_OBJECT;
1495 }
1496 if (result != ERR_OK) {
1497 HILOGE("SendStrategy failed: %{public}d!", result);
1498 }
1499 return result;
1500 }
1501
ListExtensionSendReq(const std::string & extension,SamgrInterfaceCode cmd,MessageParcel & reply,MessageOption & option)1502 int32_t SystemAbilityManagerProxy::ListExtensionSendReq(const std::string& extension,
1503 SamgrInterfaceCode cmd, MessageParcel& reply, MessageOption& option)
1504 {
1505 sptr<IRemoteObject> remote = Remote();
1506 if (remote == nullptr) {
1507 HILOGE("remote is nullptr !");
1508 return ERR_INVALID_OPERATION;
1509 }
1510 MessageParcel data;
1511 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1512 HILOGW("%{public}s write token failed!", __func__);
1513 return ERR_FLATTEN_OBJECT;
1514 }
1515 if (!data.WriteString(extension)) {
1516 HILOGW("%{public}s write extension failed!", __func__);
1517 return ERR_FLATTEN_OBJECT;
1518 }
1519 int32_t err = remote->SendRequest(
1520 static_cast<uint32_t>(cmd), data, reply, option);
1521 if (err != ERR_NONE) {
1522 HILOGW("%{public}s transact failed!", __func__);
1523 return err;
1524 }
1525 int32_t result;
1526 if (!reply.ReadInt32(result)) {
1527 HILOGW("%{public}s Read result failed!", __func__);
1528 return ERR_FLATTEN_OBJECT;
1529 }
1530 return result;
1531 }
1532
GetExtensionSaIds(const std::string & extension,std::vector<int32_t> & saIds)1533 int32_t SystemAbilityManagerProxy::GetExtensionSaIds(const std::string& extension, std::vector<int32_t>& saIds)
1534 {
1535 HILOGD("%{public}s called", __func__);
1536
1537 MessageParcel reply;
1538 MessageOption option;
1539 int32_t ret = ListExtensionSendReq(extension,
1540 SamgrInterfaceCode::GET_EXTENSION_SA_IDS_TRANSCATION, reply, option);
1541 if (ret != ERR_OK) {
1542 return ret;
1543 }
1544 if (!reply.ReadInt32Vector(&saIds)) {
1545 HILOGW("%{public}s read reply failed", __func__);
1546 return ERR_FLATTEN_OBJECT;
1547 }
1548 return ERR_OK;
1549 }
1550
GetExtensionRunningSaList(const std::string & extension,std::vector<sptr<IRemoteObject>> & saList)1551 int32_t SystemAbilityManagerProxy::GetExtensionRunningSaList(const std::string& extension,
1552 std::vector<sptr<IRemoteObject>>& saList)
1553 {
1554 HILOGD("%{public}s called", __func__);
1555
1556 MessageParcel reply;
1557 MessageOption option;
1558 int32_t ret = ListExtensionSendReq(extension,
1559 SamgrInterfaceCode::GET_EXTERNSION_SA_LIST_TRANSCATION, reply, option);
1560 if (ret != ERR_OK) {
1561 return ret;
1562 }
1563 int32_t size = 0;
1564 if (!reply.ReadInt32(size)) {
1565 HILOGW("%{public}s read reply failed", __func__);
1566 return ERR_FLATTEN_OBJECT;
1567 }
1568 if (size > LAST_SYS_ABILITY_ID) {
1569 HILOGW("get ExtensionRunningSa size failed, size:%{public}d.", size);
1570 return ERR_FLATTEN_OBJECT;
1571 }
1572 for (int32_t i = 0; i < size; ++i) {
1573 sptr<IRemoteObject> obj = reply.ReadRemoteObject();
1574 if (obj == nullptr) {
1575 HILOGW("%{public}s read reply loop(%{public}d) size(%{public}d) failed", __func__, i, size);
1576 saList.clear();
1577 return ERR_FLATTEN_OBJECT;
1578 }
1579 saList.emplace_back(obj);
1580 }
1581 return ERR_OK;
1582 }
1583
GetRunningSaExtensionInfoList(const std::string & extension,std::vector<SaExtensionInfo> & infoList)1584 int32_t SystemAbilityManagerProxy::GetRunningSaExtensionInfoList(const std::string& extension,
1585 std::vector<SaExtensionInfo>& infoList)
1586 {
1587 HILOGD("%{public}s called", __func__);
1588 MessageParcel reply;
1589 MessageOption option;
1590 int32_t ret = ListExtensionSendReq(extension,
1591 SamgrInterfaceCode::GET_SA_EXTENSION_INFO_TRANSCATION, reply, option);
1592 if (ret != ERR_OK) {
1593 return ret;
1594 }
1595 int32_t size = 0;
1596 if (!reply.ReadInt32(size)) {
1597 HILOGW("get SaExtInfoList read reply size failed");
1598 return ERR_FLATTEN_OBJECT;
1599 }
1600 if (size > LAST_SYS_ABILITY_ID) {
1601 HILOGW("get RunningSaExtensionInfo size failed, size:%{public}d.", size);
1602 return ERR_FLATTEN_OBJECT;
1603 }
1604 for (int32_t i = 0; i < size; ++i) {
1605 SaExtensionInfo tmp;
1606 if (!reply.ReadInt32(tmp.saId)) {
1607 HILOGW("get SaExtInfoList read reply id failed");
1608 infoList.clear();
1609 return ERR_FLATTEN_OBJECT;
1610 }
1611 tmp.processObj = reply.ReadRemoteObject();
1612 if (tmp.processObj == nullptr) {
1613 HILOGW("get SaExtInfoList read reply loop:%{public}d size:%{public}d failed", i, size);
1614 infoList.clear();
1615 return ERR_FLATTEN_OBJECT;
1616 }
1617 infoList.emplace_back(tmp);
1618 }
1619 return ERR_OK;
1620 }
1621
GetCommonEventExtraDataIdlist(int32_t saId,std::vector<int64_t> & extraDataIdList,const std::string & eventName)1622 int32_t SystemAbilityManagerProxy::GetCommonEventExtraDataIdlist(int32_t saId, std::vector<int64_t>& extraDataIdList,
1623 const std::string& eventName)
1624 {
1625 HILOGD("getExtraIdList called");
1626 sptr<IRemoteObject> remote = Remote();
1627 if (remote == nullptr) {
1628 HILOGE("getExtraIdList remote is nullptr");
1629 return ERR_INVALID_OPERATION;
1630 }
1631
1632 MessageParcel data;
1633 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1634 HILOGE("getExtraIdList write token failed!");
1635 return ERR_FLATTEN_OBJECT;
1636 }
1637 if (!data.WriteInt32(saId)) {
1638 HILOGE("getExtraIdList write said failed!");
1639 return ERR_FLATTEN_OBJECT;
1640 }
1641 if (!data.WriteString(eventName)) {
1642 HILOGW("getExtraIdList write eventname failed!");
1643 return ERR_FLATTEN_OBJECT;
1644 }
1645
1646 MessageParcel reply;
1647 MessageOption option;
1648 int32_t err = remote->SendRequest(
1649 static_cast<uint32_t>(SamgrInterfaceCode::GET_COMMON_EVENT_EXTRA_ID_LIST_TRANSCATION), data, reply, option);
1650 if (err != ERR_NONE) {
1651 HILOGE("getExtraIdList SendRequest error: %{public}d!", err);
1652 return err;
1653 }
1654 HILOGD("getExtraIdList SendRequest succeed!");
1655 int32_t result = 0;
1656 if (!reply.ReadInt32(result)) {
1657 HILOGE("getExtraIdList Read result failed!");
1658 return ERR_FLATTEN_OBJECT;
1659 }
1660 if (result != ERR_OK) {
1661 HILOGE("getExtraIdList failed: %{public}d!", result);
1662 return result;
1663 }
1664 if (!reply.ReadInt64Vector(&extraDataIdList)) {
1665 HILOGW("getExtraIdList read idlist failed");
1666 extraDataIdList.clear();
1667 return ERR_FLATTEN_OBJECT;
1668 }
1669 return ERR_OK;
1670 }
1671
GetLocalAbilityManagerProxy(int32_t systemAbilityId)1672 sptr<IRemoteObject> SystemAbilityManagerProxy::GetLocalAbilityManagerProxy(int32_t systemAbilityId)
1673 {
1674 if (!CheckInputSysAbilityId(systemAbilityId)) {
1675 HILOGW("GetLocalAbilityManagerProxy SA invalid:%{public}d!", systemAbilityId);
1676 return nullptr;
1677 }
1678
1679 MessageParcel data;
1680 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
1681 HILOGE("GetLocalAbilityManagerProxy write token failed!");
1682 return nullptr;
1683 }
1684 if (!data.WriteInt32(systemAbilityId)) {
1685 HILOGE("GetLocalAbilityManagerProxy write said failed!");
1686 return nullptr;
1687 }
1688
1689 auto remote = Remote();
1690 if (remote == nullptr) {
1691 HILOGI("GetLocalAbilityManagerProxy remote is nullptr");
1692 return nullptr;
1693 }
1694
1695 MessageParcel reply;
1696 MessageOption option;
1697 int32_t err = remote->SendRequest(
1698 static_cast<uint32_t>(SamgrInterfaceCode::GET_LOCAL_ABILITY_MANAGER_PROXY_TRANSCATION), data, reply, option);
1699 if (err != ERR_NONE) {
1700 HILOGE("GetLocalAbilityManagerProxy SendRequest error: %{public}d!", err);
1701 return nullptr;
1702 }
1703 return reply.ReadRemoteObject();
1704 }
1705 } // namespace OHOS
1706