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 #include "token_sync_manager_proxy.h"
23
24 namespace OHOS {
25 namespace Security {
26 namespace AccessToken {
27 namespace {
28 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "TokenSyncManagerClient"};
29 std::recursive_mutex g_instanceMutex;
30 } // namespace
31
GetInstance()32 TokenSyncManagerClient& TokenSyncManagerClient::GetInstance()
33 {
34 static TokenSyncManagerClient* instance = nullptr;
35 if (instance == nullptr) {
36 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
37 if (instance == nullptr) {
38 instance = new TokenSyncManagerClient();
39 }
40 }
41 return *instance;
42 }
43
TokenSyncManagerClient()44 TokenSyncManagerClient::TokenSyncManagerClient()
45 {}
46
~TokenSyncManagerClient()47 TokenSyncManagerClient::~TokenSyncManagerClient()
48 {
49 ACCESSTOKEN_LOG_ERROR(LABEL, "~TokenSyncManagerClient");
50 }
51
GetRemoteHapTokenInfo(const std::string & deviceID,AccessTokenID tokenID) const52 int TokenSyncManagerClient::GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID) const
53 {
54 ACCESSTOKEN_LOG_DEBUG(LABEL, "Called");
55 auto proxy = GetProxy();
56 if (proxy == nullptr) {
57 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
58 return TOKEN_SYNC_IPC_ERROR;
59 }
60 return proxy->GetRemoteHapTokenInfo(deviceID, tokenID);
61 }
62
DeleteRemoteHapTokenInfo(AccessTokenID tokenID) const63 int TokenSyncManagerClient::DeleteRemoteHapTokenInfo(AccessTokenID tokenID) const
64 {
65 ACCESSTOKEN_LOG_DEBUG(LABEL, "Called");
66 auto proxy = GetProxy();
67 if (proxy == nullptr) {
68 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
69 return TOKEN_SYNC_IPC_ERROR;
70 }
71 return proxy->DeleteRemoteHapTokenInfo(tokenID);
72 }
73
UpdateRemoteHapTokenInfo(const HapTokenInfoForSync & tokenInfo) const74 int TokenSyncManagerClient::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo) const
75 {
76 ACCESSTOKEN_LOG_DEBUG(LABEL, "Called");
77 auto proxy = GetProxy();
78 if (proxy == nullptr) {
79 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
80 return TOKEN_SYNC_IPC_ERROR;
81 }
82 return proxy->UpdateRemoteHapTokenInfo(tokenInfo);
83 }
84
GetProxy() const85 sptr<ITokenSyncManager> TokenSyncManagerClient::GetProxy() const
86 {
87 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
88 if (sam == nullptr) {
89 ACCESSTOKEN_LOG_WARN(LABEL, "GetSystemAbilityManager is null");
90 return nullptr;
91 }
92
93 auto tokensyncSa = sam->GetSystemAbility(ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE);
94 if (tokensyncSa == nullptr) {
95 ACCESSTOKEN_LOG_WARN(LABEL, "GetSystemAbility %{public}d is null",
96 ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE);
97 return nullptr;
98 }
99
100 auto proxy = new TokenSyncManagerProxy(tokensyncSa);
101 if (proxy == nullptr) {
102 ACCESSTOKEN_LOG_WARN(LABEL, "Iface_cast get null");
103 return nullptr;
104 }
105 return proxy;
106 }
107 } // namespace AccessToken
108 } // namespace Security
109 } // namespace OHOS
110