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_common_log.h"
19 #include "hap_token_info_for_sync_parcel.h"
20 #include "iservice_registry.h"
21 #include "token_sync_manager_proxy.h"
22
23 namespace OHOS {
24 namespace Security {
25 namespace AccessToken {
26 namespace {
27 std::recursive_mutex g_instanceMutex;
28 } // namespace
29
GetInstance()30 TokenSyncManagerClient& TokenSyncManagerClient::GetInstance()
31 {
32 static TokenSyncManagerClient* instance = nullptr;
33 if (instance == nullptr) {
34 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
35 if (instance == nullptr) {
36 TokenSyncManagerClient* tmp = new (std::nothrow) TokenSyncManagerClient();
37 instance = std::move(tmp);
38 }
39 }
40 return *instance;
41 }
42
TokenSyncManagerClient()43 TokenSyncManagerClient::TokenSyncManagerClient()
44 {}
45
~TokenSyncManagerClient()46 TokenSyncManagerClient::~TokenSyncManagerClient()
47 {
48 LOGE(ATM_DOMAIN, ATM_TAG, "~TokenSyncManagerClient");
49 }
50
GetRemoteHapTokenInfo(const std::string & deviceID,AccessTokenID tokenID) const51 int TokenSyncManagerClient::GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID) const
52 {
53 LOGD(ATM_DOMAIN, ATM_TAG, "Called");
54 auto proxy = GetProxy();
55 if (proxy == nullptr) {
56 LOGE(ATM_DOMAIN, ATM_TAG, "Proxy is null");
57 return TOKEN_SYNC_IPC_ERROR;
58 }
59 return proxy->GetRemoteHapTokenInfo(deviceID, tokenID);
60 }
61
DeleteRemoteHapTokenInfo(AccessTokenID tokenID) const62 int TokenSyncManagerClient::DeleteRemoteHapTokenInfo(AccessTokenID tokenID) const
63 {
64 LOGD(ATM_DOMAIN, ATM_TAG, "Called");
65 auto proxy = GetProxy();
66 if (proxy == nullptr) {
67 LOGE(ATM_DOMAIN, ATM_TAG, "Proxy is null");
68 return TOKEN_SYNC_IPC_ERROR;
69 }
70 return proxy->DeleteRemoteHapTokenInfo(tokenID);
71 }
72
UpdateRemoteHapTokenInfo(const HapTokenInfoForSync & tokenInfo) const73 int TokenSyncManagerClient::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo) const
74 {
75 LOGD(ATM_DOMAIN, ATM_TAG, "Called");
76 auto proxy = GetProxy();
77 if (proxy == nullptr) {
78 LOGE(ATM_DOMAIN, ATM_TAG, "Proxy is null");
79 return TOKEN_SYNC_IPC_ERROR;
80 }
81 return proxy->UpdateRemoteHapTokenInfo(tokenInfo);
82 }
83
GetProxy() const84 sptr<ITokenSyncManager> TokenSyncManagerClient::GetProxy() const
85 {
86 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
87 if (sam == nullptr) {
88 LOGW(ATM_DOMAIN, ATM_TAG, "GetSystemAbilityManager is null");
89 return nullptr;
90 }
91
92 auto tokensyncSa = sam->GetSystemAbility(ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE);
93 if (tokensyncSa == nullptr) {
94 LOGW(ATM_DOMAIN, ATM_TAG, "GetSystemAbility %{public}d is null",
95 ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE);
96 return nullptr;
97 }
98
99 auto proxy = new (std::nothrow) TokenSyncManagerProxy(tokensyncSa);
100 if (proxy == nullptr) {
101 LOGW(ATM_DOMAIN, ATM_TAG, "Iface_cast get null");
102 return nullptr;
103 }
104 return proxy;
105 }
106 } // namespace AccessToken
107 } // namespace Security
108 } // namespace OHOS
109