• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "accesstoken_manager_client.h"
17 
18 #include "accesstoken_log.h"
19 #include "access_token_error.h"
20 #include "accesstoken_manager_proxy.h"
21 #include "hap_token_info.h"
22 #include "hap_token_info_for_sync_parcel.h"
23 #include "iservice_registry.h"
24 #include "native_token_info_for_sync_parcel.h"
25 #include "native_token_info.h"
26 #include "permission_state_change_callback.h"
27 
28 namespace OHOS {
29 namespace Security {
30 namespace AccessToken {
31 namespace {
32 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
33     LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "AccessTokenManagerClient"
34 };
35 } // namespace
36 static const uint32_t MAX_CALLBACK_MAP_SIZE = 200;
37 
GetInstance()38 AccessTokenManagerClient& AccessTokenManagerClient::GetInstance()
39 {
40     static AccessTokenManagerClient instance;
41     return instance;
42 }
43 
AccessTokenManagerClient()44 AccessTokenManagerClient::AccessTokenManagerClient()
45 {}
46 
~AccessTokenManagerClient()47 AccessTokenManagerClient::~AccessTokenManagerClient()
48 {}
49 
VerifyAccessToken(AccessTokenID tokenID,const std::string & permissionName)50 int AccessTokenManagerClient::VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName)
51 {
52     auto proxy = GetProxy();
53     if (proxy == nullptr) {
54         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
55         return PERMISSION_DENIED;
56     }
57     return proxy->VerifyAccessToken(tokenID, permissionName);
58 }
59 
GetDefPermission(const std::string & permissionName,PermissionDef & permissionDefResult)60 int AccessTokenManagerClient::GetDefPermission(
61     const std::string& permissionName, PermissionDef& permissionDefResult)
62 {
63     auto proxy = GetProxy();
64     if (proxy == nullptr) {
65         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
66         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
67     }
68     PermissionDefParcel permissionDefParcel;
69     int result = proxy->GetDefPermission(permissionName, permissionDefParcel);
70     permissionDefResult = permissionDefParcel.permissionDef;
71     return result;
72 }
73 
GetDefPermissions(AccessTokenID tokenID,std::vector<PermissionDef> & permList)74 int AccessTokenManagerClient::GetDefPermissions(AccessTokenID tokenID, std::vector<PermissionDef>& permList)
75 {
76     auto proxy = GetProxy();
77     if (proxy == nullptr) {
78         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
79         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
80     }
81     std::vector<PermissionDefParcel> parcelList;
82     int result = proxy->GetDefPermissions(tokenID, parcelList);
83     for (const auto& permParcel : parcelList) {
84         PermissionDef perm = permParcel.permissionDef;
85         permList.emplace_back(perm);
86     }
87     return result;
88 }
89 
GetReqPermissions(AccessTokenID tokenID,std::vector<PermissionStateFull> & reqPermList,bool isSystemGrant)90 int AccessTokenManagerClient::GetReqPermissions(
91     AccessTokenID tokenID, std::vector<PermissionStateFull>& reqPermList, bool isSystemGrant)
92 {
93     auto proxy = GetProxy();
94     if (proxy == nullptr) {
95         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
96         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
97     }
98     std::vector<PermissionStateFullParcel> parcelList;
99     int result = proxy->GetReqPermissions(tokenID, parcelList, isSystemGrant);
100     for (const auto& permParcel : parcelList) {
101         PermissionStateFull perm = permParcel.permStatFull;
102         reqPermList.emplace_back(perm);
103     }
104     return result;
105 }
106 
GetPermissionFlag(AccessTokenID tokenID,const std::string & permissionName,int & flag)107 int AccessTokenManagerClient::GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName, int& flag)
108 {
109     auto proxy = GetProxy();
110     if (proxy == nullptr) {
111         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
112         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
113     }
114     return proxy->GetPermissionFlag(tokenID, permissionName, flag);
115 }
116 
GetSelfPermissionsState(std::vector<PermissionListState> & permList)117 PermissionOper AccessTokenManagerClient::GetSelfPermissionsState(std::vector<PermissionListState>& permList)
118 {
119     auto proxy = GetProxy();
120     if (proxy == nullptr) {
121         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null.");
122         return INVALID_OPER;
123     }
124 
125     size_t len = permList.size();
126     if (len == 0) {
127         ACCESSTOKEN_LOG_DEBUG(LABEL, "len is zero.");
128         return PASS_OPER;
129     }
130 
131     std::vector<PermissionListStateParcel> parcelList;
132 
133     for (const auto& perm : permList) {
134         PermissionListStateParcel permParcel;
135         permParcel.permsState = perm;
136         parcelList.emplace_back(permParcel);
137     }
138     PermissionOper result = proxy->GetSelfPermissionsState(parcelList);
139 
140     for (uint32_t i = 0; i < len; i++) {
141         PermissionListState perm = parcelList[i].permsState;
142         permList[i].state = perm.state;
143     }
144     return result;
145 }
146 
GrantPermission(AccessTokenID tokenID,const std::string & permissionName,int flag)147 int AccessTokenManagerClient::GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag)
148 {
149     auto proxy = GetProxy();
150     if (proxy == nullptr) {
151         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
152         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
153     }
154     return proxy->GrantPermission(tokenID, permissionName, flag);
155 }
156 
RevokePermission(AccessTokenID tokenID,const std::string & permissionName,int flag)157 int AccessTokenManagerClient::RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag)
158 {
159     auto proxy = GetProxy();
160     if (proxy == nullptr) {
161         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
162         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
163     }
164     return proxy->RevokePermission(tokenID, permissionName, flag);
165 }
166 
ClearUserGrantedPermissionState(AccessTokenID tokenID)167 int AccessTokenManagerClient::ClearUserGrantedPermissionState(AccessTokenID tokenID)
168 {
169     auto proxy = GetProxy();
170     if (proxy == nullptr) {
171         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
172         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
173     }
174     return proxy->ClearUserGrantedPermissionState(tokenID);
175 }
176 
CreatePermStateChangeCallback(const std::shared_ptr<PermStateChangeCallbackCustomize> & customizedCb,sptr<PermissionStateChangeCallback> & callback)177 int32_t AccessTokenManagerClient::CreatePermStateChangeCallback(
178     const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb,
179     sptr<PermissionStateChangeCallback>& callback)
180 {
181     std::lock_guard<std::mutex> lock(callbackMutex_);
182     if (callbackMap_.size() == MAX_CALLBACK_MAP_SIZE) {
183         ACCESSTOKEN_LOG_ERROR(LABEL, "the maximum number of callback has been reached");
184         return AccessTokenError::ERR_EXCEEDED_MAXNUM_REGISTRATION_LIMIT;
185     }
186 
187     auto goalCallback = callbackMap_.find(customizedCb);
188     if (goalCallback != callbackMap_.end()) {
189         ACCESSTOKEN_LOG_ERROR(LABEL, "already has the same callback");
190         return AccessTokenError::ERR_PARAM_INVALID;
191     } else {
192         callback = new (std::nothrow) PermissionStateChangeCallback(customizedCb);
193         if (!callback) {
194             ACCESSTOKEN_LOG_ERROR(LABEL, "memory allocation for callback failed!");
195             return AccessTokenError::ERR_SA_WORK_ABNORMAL;
196         }
197     }
198     return RET_SUCCESS;
199 }
200 
RegisterPermStateChangeCallback(const std::shared_ptr<PermStateChangeCallbackCustomize> & customizedCb)201 int32_t AccessTokenManagerClient::RegisterPermStateChangeCallback(
202     const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb)
203 {
204     if (customizedCb == nullptr) {
205         ACCESSTOKEN_LOG_ERROR(LABEL, "customizedCb is nullptr");
206         return AccessTokenError::ERR_PARAM_INVALID;
207     }
208 
209     sptr<PermissionStateChangeCallback> callback = nullptr;
210     int32_t result = CreatePermStateChangeCallback(customizedCb, callback);
211     if (result != RET_SUCCESS) {
212         return result;
213     }
214     auto proxy = GetProxy();
215     if (proxy == nullptr) {
216         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
217         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
218     }
219 
220     PermStateChangeScopeParcel scopeParcel;
221     customizedCb->GetScope(scopeParcel.scope);
222 
223     if (scopeParcel.scope.permList.size() > PERMS_LIST_SIZE_MAX ||
224         scopeParcel.scope.tokenIDs.size() > TOKENIDS_LIST_SIZE_MAX) {
225         return AccessTokenError::ERR_PARAM_INVALID;
226     }
227     result = proxy->RegisterPermStateChangeCallback(scopeParcel, callback->AsObject());
228     if (result == RET_SUCCESS) {
229         std::lock_guard<std::mutex> lock(callbackMutex_);
230         callbackMap_[customizedCb] = callback;
231     }
232     return result;
233 }
234 
UnRegisterPermStateChangeCallback(const std::shared_ptr<PermStateChangeCallbackCustomize> & customizedCb)235 int32_t AccessTokenManagerClient::UnRegisterPermStateChangeCallback(
236     const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb)
237 {
238     auto proxy = GetProxy();
239     if (proxy == nullptr) {
240         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
241         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
242     }
243 
244     std::lock_guard<std::mutex> lock(callbackMutex_);
245     auto goalCallback = callbackMap_.find(customizedCb);
246     if (goalCallback == callbackMap_.end()) {
247         ACCESSTOKEN_LOG_ERROR(LABEL, "goalCallback already is not exist");
248         return AccessTokenError::ERR_INTERFACE_NOT_USED_TOGETHER;
249     }
250 
251     int32_t result = proxy->UnRegisterPermStateChangeCallback(goalCallback->second->AsObject());
252     if (result == RET_SUCCESS) {
253         callbackMap_.erase(goalCallback);
254     }
255     return result;
256 }
257 
AllocHapToken(const HapInfoParams & info,const HapPolicyParams & policy)258 AccessTokenIDEx AccessTokenManagerClient::AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy)
259 {
260     AccessTokenIDEx res = { 0 };
261     auto proxy = GetProxy();
262     if (proxy == nullptr) {
263         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
264         return res;
265     }
266     HapInfoParcel hapInfoParcel;
267     HapPolicyParcel hapPolicyParcel;
268     hapInfoParcel.hapInfoParameter = info;
269     hapPolicyParcel.hapPolicyParameter = policy;
270 
271     return proxy->AllocHapToken(hapInfoParcel, hapPolicyParcel);
272 }
273 
DeleteToken(AccessTokenID tokenID)274 int AccessTokenManagerClient::DeleteToken(AccessTokenID tokenID)
275 {
276     auto proxy = GetProxy();
277     if (proxy == nullptr) {
278         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
279         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
280     }
281     return proxy->DeleteToken(tokenID);
282 }
283 
GetTokenType(AccessTokenID tokenID)284 ATokenTypeEnum AccessTokenManagerClient::GetTokenType(AccessTokenID tokenID)
285 {
286     auto proxy = GetProxy();
287     if (proxy == nullptr) {
288         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
289         return TOKEN_INVALID;
290     }
291     return static_cast<ATokenTypeEnum>(proxy->GetTokenType(tokenID));
292 }
293 
CheckNativeDCap(AccessTokenID tokenID,const std::string & dcap)294 int AccessTokenManagerClient::CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap)
295 {
296     auto proxy = GetProxy();
297     if (proxy == nullptr) {
298         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
299         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
300     }
301     return proxy->CheckNativeDCap(tokenID, dcap);
302 }
303 
GetHapTokenID(int userID,const std::string & bundleName,int instIndex)304 AccessTokenID AccessTokenManagerClient::GetHapTokenID(int userID, const std::string& bundleName, int instIndex)
305 {
306     auto proxy = GetProxy();
307     if (proxy == nullptr) {
308         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
309         return INVALID_TOKENID;
310     }
311     return proxy->GetHapTokenID(userID, bundleName, instIndex);
312 }
313 
AllocLocalTokenID(const std::string & remoteDeviceID,AccessTokenID remoteTokenID)314 AccessTokenID AccessTokenManagerClient::AllocLocalTokenID(
315     const std::string& remoteDeviceID, AccessTokenID remoteTokenID)
316 {
317     auto proxy = GetProxy();
318     if (proxy == nullptr) {
319         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
320         return INVALID_TOKENID;
321     }
322     return proxy->AllocLocalTokenID(remoteDeviceID, remoteTokenID);
323 }
324 
UpdateHapToken(AccessTokenID tokenID,const std::string & appIDDesc,int32_t apiVersion,const HapPolicyParams & policy)325 int AccessTokenManagerClient::UpdateHapToken(
326     AccessTokenID tokenID, const std::string& appIDDesc, int32_t apiVersion, const HapPolicyParams& policy)
327 {
328     auto proxy = GetProxy();
329     if (proxy == nullptr) {
330         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
331         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
332     }
333     HapPolicyParcel hapPolicyParcel;
334     hapPolicyParcel.hapPolicyParameter = policy;
335     return proxy->UpdateHapToken(tokenID, appIDDesc, apiVersion, hapPolicyParcel);
336 }
337 
GetHapTokenInfo(AccessTokenID tokenID,HapTokenInfo & hapTokenInfoRes)338 int AccessTokenManagerClient::GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes)
339 {
340     auto proxy = GetProxy();
341     if (proxy == nullptr) {
342         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
343         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
344     }
345     HapTokenInfoParcel hapTokenInfoParcel;
346     int res = proxy->GetHapTokenInfo(tokenID, hapTokenInfoParcel);
347 
348     hapTokenInfoRes = hapTokenInfoParcel.hapTokenInfoParams;
349     return res;
350 }
351 
GetNativeTokenInfo(AccessTokenID tokenID,NativeTokenInfo & nativeTokenInfoRes)352 int AccessTokenManagerClient::GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes)
353 {
354     auto proxy = GetProxy();
355     if (proxy == nullptr) {
356         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
357         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
358     }
359     NativeTokenInfoParcel nativeTokenInfoParcel;
360     int res = proxy->GetNativeTokenInfo(tokenID, nativeTokenInfoParcel);
361     nativeTokenInfoRes = nativeTokenInfoParcel.nativeTokenInfoParams;
362     return res;
363 }
364 
ReloadNativeTokenInfo()365 int32_t AccessTokenManagerClient::ReloadNativeTokenInfo()
366 {
367     auto proxy = GetProxy();
368     if (proxy == nullptr) {
369         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
370         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
371     }
372     return proxy->ReloadNativeTokenInfo();
373 }
374 
GetNativeTokenId(const std::string & processName)375 AccessTokenID AccessTokenManagerClient::GetNativeTokenId(const std::string& processName)
376 {
377     auto proxy = GetProxy();
378     if (proxy == nullptr) {
379         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
380         return INVALID_TOKENID;
381     }
382     return proxy->GetNativeTokenId(processName);
383 }
384 
385 #ifdef TOKEN_SYNC_ENABLE
GetHapTokenInfoFromRemote(AccessTokenID tokenID,HapTokenInfoForSync & hapSync)386 int AccessTokenManagerClient::GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync)
387 {
388     auto proxy = GetProxy();
389     if (proxy == nullptr) {
390         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
391         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
392     }
393 
394     HapTokenInfoForSyncParcel hapSyncParcel;
395     int res = proxy->GetHapTokenInfoFromRemote(tokenID, hapSyncParcel);
396     hapSync = hapSyncParcel.hapTokenInfoForSyncParams;
397     return res;
398 }
399 
GetAllNativeTokenInfo(std::vector<NativeTokenInfoForSync> & nativeTokenInfosRes)400 int AccessTokenManagerClient::GetAllNativeTokenInfo(std::vector<NativeTokenInfoForSync>& nativeTokenInfosRes)
401 {
402     auto proxy = GetProxy();
403     if (proxy == nullptr) {
404         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
405         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
406     }
407 
408     std::vector<NativeTokenInfoForSyncParcel> parcelList;
409     int result = proxy->GetAllNativeTokenInfo(parcelList);
410     for (const auto& nativeTokenParcel : parcelList) {
411         NativeTokenInfoForSync native = nativeTokenParcel.nativeTokenInfoForSyncParams;
412         nativeTokenInfosRes.emplace_back(native);
413     }
414 
415     return result;
416 }
417 
SetRemoteHapTokenInfo(const std::string & deviceID,const HapTokenInfoForSync & hapSync)418 int AccessTokenManagerClient::SetRemoteHapTokenInfo(const std::string& deviceID, const HapTokenInfoForSync& hapSync)
419 {
420     auto proxy = GetProxy();
421     if (proxy == nullptr) {
422         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
423         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
424     }
425 
426     HapTokenInfoForSyncParcel hapSyncParcel;
427     hapSyncParcel.hapTokenInfoForSyncParams = hapSync;
428 
429     int res = proxy->SetRemoteHapTokenInfo(deviceID, hapSyncParcel);
430     return res;
431 }
432 
SetRemoteNativeTokenInfo(const std::string & deviceID,const std::vector<NativeTokenInfoForSync> & nativeTokenInfoList)433 int AccessTokenManagerClient::SetRemoteNativeTokenInfo(const std::string& deviceID,
434     const std::vector<NativeTokenInfoForSync>& nativeTokenInfoList)
435 {
436     auto proxy = GetProxy();
437     if (proxy == nullptr) {
438         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
439         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
440     }
441     std::vector<NativeTokenInfoForSyncParcel> nativeTokenInfoParcels;
442     for (const auto& native : nativeTokenInfoList) {
443         NativeTokenInfoForSyncParcel nativeTokenInfoForSyncParcel;
444         nativeTokenInfoForSyncParcel.nativeTokenInfoForSyncParams = native;
445         nativeTokenInfoParcels.emplace_back(nativeTokenInfoForSyncParcel);
446     }
447     PermissionStateFullParcel permStateParcel;
448     int res = proxy->SetRemoteNativeTokenInfo(deviceID, nativeTokenInfoParcels);
449     return res;
450 }
451 
DeleteRemoteToken(const std::string & deviceID,AccessTokenID tokenID)452 int AccessTokenManagerClient::DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID)
453 {
454     auto proxy = GetProxy();
455     if (proxy == nullptr) {
456         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
457         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
458     }
459 
460     int res = proxy->DeleteRemoteToken(deviceID, tokenID);
461     return res;
462 }
463 
GetRemoteNativeTokenID(const std::string & deviceID,AccessTokenID tokenID)464 AccessTokenID AccessTokenManagerClient::GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID)
465 {
466     auto proxy = GetProxy();
467     if (proxy == nullptr) {
468         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
469         return INVALID_TOKENID;
470     }
471 
472     AccessTokenID res = proxy->GetRemoteNativeTokenID(deviceID, tokenID);
473     return res;
474 }
475 
DeleteRemoteDeviceTokens(const std::string & deviceID)476 int AccessTokenManagerClient::DeleteRemoteDeviceTokens(const std::string& deviceID)
477 {
478     auto proxy = GetProxy();
479     if (proxy == nullptr) {
480         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
481         return AccessTokenError::ERR_SA_WORK_ABNORMAL;
482     }
483 
484     int res = proxy->DeleteRemoteDeviceTokens(deviceID);
485     return res;
486 }
487 #endif
488 
DumpTokenInfo(AccessTokenID tokenID,std::string & dumpInfo)489 void AccessTokenManagerClient::DumpTokenInfo(AccessTokenID tokenID, std::string& dumpInfo)
490 {
491     auto proxy = GetProxy();
492     if (proxy == nullptr) {
493         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
494         return;
495     }
496     proxy->DumpTokenInfo(tokenID, dumpInfo);
497 }
498 
InitProxy()499 void AccessTokenManagerClient::InitProxy()
500 {
501     std::lock_guard<std::mutex> lock(proxyMutex_);
502     if (proxy_ == nullptr) {
503         auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
504         if (sam == nullptr) {
505             ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null");
506             return;
507         }
508         auto accesstokenSa = sam->GetSystemAbility(IAccessTokenManager::SA_ID_ACCESSTOKEN_MANAGER_SERVICE);
509         if (accesstokenSa == nullptr) {
510             ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null",
511                 IAccessTokenManager::SA_ID_ACCESSTOKEN_MANAGER_SERVICE);
512             return;
513         }
514 
515         serviceDeathObserver_ = new (std::nothrow) AccessTokenDeathRecipient();
516         if (serviceDeathObserver_ != nullptr) {
517             accesstokenSa->AddDeathRecipient(serviceDeathObserver_);
518         }
519         proxy_ = iface_cast<IAccessTokenManager>(accesstokenSa);
520         if (proxy_ == nullptr) {
521             ACCESSTOKEN_LOG_ERROR(LABEL, "iface_cast get null");
522         }
523     }
524 }
525 
OnRemoteDiedHandle()526 void AccessTokenManagerClient::OnRemoteDiedHandle()
527 {
528     {
529         std::lock_guard<std::mutex> lock(proxyMutex_);
530         proxy_ = nullptr;
531     }
532     InitProxy();
533 }
534 
GetProxy()535 sptr<IAccessTokenManager> AccessTokenManagerClient::GetProxy()
536 {
537     if (proxy_ == nullptr) {
538         InitProxy();
539     }
540     return proxy_;
541 }
542 } // namespace AccessToken
543 } // namespace Security
544 } // namespace OHOS
545