1 /*
2 * Copyright (c) 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 "token_sync_manager_client.h"
17
18 #include "accesstoken_log.h"
19 #include "hap_token_info_for_sync_parcel.h"
20 #include "native_token_info_for_sync_parcel.h"
21 #include "iservice_registry.h"
22
23 namespace OHOS {
24 namespace Security {
25 namespace AccessToken {
26 namespace {
27 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "TokenSyncManagerClient"};
28 } // namespace
29
GetInstance()30 TokenSyncManagerClient& TokenSyncManagerClient::GetInstance()
31 {
32 static TokenSyncManagerClient instance;
33 return instance;
34 }
35
TokenSyncManagerClient()36 TokenSyncManagerClient::TokenSyncManagerClient()
37 {}
38
~TokenSyncManagerClient()39 TokenSyncManagerClient::~TokenSyncManagerClient()
40 {}
41
GetRemoteHapTokenInfo(const std::string & deviceID,AccessTokenID tokenID) const42 int TokenSyncManagerClient::GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID) const
43 {
44 ACCESSTOKEN_LOG_DEBUG(LABEL, "called");
45 auto proxy = GetProxy();
46 if (proxy == nullptr) {
47 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
48 return TOKEN_SYNC_IPC_ERROR;
49 }
50 return proxy->GetRemoteHapTokenInfo(deviceID, tokenID);
51 }
52
DeleteRemoteHapTokenInfo(AccessTokenID tokenID) const53 int TokenSyncManagerClient::DeleteRemoteHapTokenInfo(AccessTokenID tokenID) const
54 {
55 ACCESSTOKEN_LOG_DEBUG(LABEL, "called");
56 auto proxy = GetProxy();
57 if (proxy == nullptr) {
58 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
59 return TOKEN_SYNC_IPC_ERROR;
60 }
61 return proxy->DeleteRemoteHapTokenInfo(tokenID);
62 }
63
UpdateRemoteHapTokenInfo(const HapTokenInfoForSync & tokenInfo) const64 int TokenSyncManagerClient::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo) const
65 {
66 ACCESSTOKEN_LOG_DEBUG(LABEL, "called");
67 auto proxy = GetProxy();
68 if (proxy == nullptr) {
69 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
70 return TOKEN_SYNC_IPC_ERROR;
71 }
72 return proxy->UpdateRemoteHapTokenInfo(tokenInfo);
73 }
74
GetProxy() const75 sptr<ITokenSyncManager> TokenSyncManagerClient::GetProxy() const
76 {
77 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
78 if (sam == nullptr) {
79 ACCESSTOKEN_LOG_WARN(LABEL, "GetSystemAbilityManager is null");
80 return nullptr;
81 }
82
83 auto tokensyncSa = sam->GetSystemAbility(ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE);
84 if (tokensyncSa == nullptr) {
85 ACCESSTOKEN_LOG_WARN(LABEL, "GetSystemAbility %{public}d is null",
86 ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE);
87 return nullptr;
88 }
89
90 auto proxy = iface_cast<ITokenSyncManager>(tokensyncSa);
91 if (proxy == nullptr) {
92 ACCESSTOKEN_LOG_WARN(LABEL, "iface_cast get null");
93 return nullptr;
94 }
95 return proxy;
96 }
97 } // namespace AccessToken
98 } // namespace Security
99 } // namespace OHOS
100