From e7cc3d9327032a40a552fa7e7c9fa059d1f35217 Mon Sep 17 00:00:00 2001 From: zhouyan Date: Mon, 23 May 2022 17:08:04 +0800 Subject: [PATCH] =?UTF-8?q?tokensync=E5=8A=A8=E6=80=81=E6=8B=89=E8=B5=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhouyan --- interfaces/innerkits/tokensync/BUILD.gn | 4 + .../src/token_sync_load_callback.cpp | 66 ++++++++++ .../tokensync/src/token_sync_load_callback.h | 37 ++++++ .../src/token_sync_manager_client.cpp | 77 ++++++++--- .../tokensync/src/token_sync_manager_client.h | 19 ++- services/accesstokenmanager/BUILD.gn | 9 +- .../device/atm_device_state_callback.h | 51 +++++++ .../service/accesstoken_manager_service.h | 8 +- .../src/device/atm_device_state_callback.cpp | 91 +++++++++++++ .../service/accesstoken_manager_service.cpp | 86 +++++++++++- .../soft_bus_device_connection_listener.h | 1 - .../include/remote/soft_bus_manager.h | 13 +- services/tokensyncmanager/sa_profile/3504.xml | 2 +- .../soft_bus_device_connection_listener.cpp | 23 ++++ .../src/remote/soft_bus_manager.cpp | 124 +++++++++++++----- .../service/token_sync_manager_service.cpp | 7 +- services/tokensyncmanager/token_sync.cfg | 10 +- 17 files changed, 557 insertions(+), 71 deletions(-) create mode 100644 interfaces/innerkits/tokensync/src/token_sync_load_callback.cpp create mode 100644 interfaces/innerkits/tokensync/src/token_sync_load_callback.h create mode 100644 services/accesstokenmanager/main/cpp/include/device/atm_device_state_callback.h create mode 100644 services/accesstokenmanager/main/cpp/src/device/atm_device_state_callback.cpp diff --git a/interfaces/innerkits/tokensync/BUILD.gn b/interfaces/innerkits/tokensync/BUILD.gn index 3d425f645..24cb4826c 100644 --- a/interfaces/innerkits/tokensync/BUILD.gn +++ b/interfaces/innerkits/tokensync/BUILD.gn @@ -32,12 +32,16 @@ ohos_shared_library("libtokensync_sdk") { "src", "//base/security/access_token/frameworks/tokensync/include", "//base/security/access_token/frameworks/common/include", + "//base/security/access_token/interfaces/innerkits/tokensync/include", "//base/security/access_token/interfaces/innerkits/accesstoken/include", "//base/security/access_token/frameworks/accesstoken/include", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", ] sources = [ "src/token_sync_kit.cpp", + "src/token_sync_load_callback.cpp", "src/token_sync_manager_client.cpp", "src/token_sync_manager_proxy.cpp", ] diff --git a/interfaces/innerkits/tokensync/src/token_sync_load_callback.cpp b/interfaces/innerkits/tokensync/src/token_sync_load_callback.cpp new file mode 100644 index 000000000..efb947312 --- /dev/null +++ b/interfaces/innerkits/tokensync/src/token_sync_load_callback.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "token_sync_load_callback.h" + +#include "accesstoken_log.h" +#include "i_token_sync_manager.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "TokenSyncLoadCallBack"}; +} + +TokenSyncLoadCallback::TokenSyncLoadCallback() +{ + ACCESSTOKEN_LOG_INFO(LABEL, "enter"); +} + +void TokenSyncLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, + const sptr& remoteObject) +{ + if (systemAbilityId != ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE) { + ACCESSTOKEN_LOG_ERROR(LABEL, "start aystemabilityId is not TokenSync!"); + return; + } + + if (remoteObject == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "remoteObject is null."); + return; + } + + ACCESSTOKEN_LOG_DEBUG(LABEL, "OnLoadSystemAbilitySuccess start systemAbilityId: %{public}d success!", + systemAbilityId); + + TokenSyncManagerClient::GetInstance().FinishStartSASuccess(remoteObject); +} + +void TokenSyncLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + if (systemAbilityId != ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE) { + ACCESSTOKEN_LOG_DEBUG(LABEL, "start aystemabilityId is not TokenSync!"); + return; + } + + ACCESSTOKEN_LOG_DEBUG(LABEL, "OnLoadSystemAbilityFail systemAbilityId: %{public}d failed.", systemAbilityId); + + TokenSyncManagerClient::GetInstance().FinishStartSAFailed(); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/tokensync/src/token_sync_load_callback.h b/interfaces/innerkits/tokensync/src/token_sync_load_callback.h new file mode 100644 index 000000000..ba5c2f95a --- /dev/null +++ b/interfaces/innerkits/tokensync/src/token_sync_load_callback.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TOKEN_SYNC_LOAD_CALLBACK_H +#define TOKEN_SYNC_LOAD_CALLBACK_H + +#include "system_ability_load_callback_stub.h" +#include "i_token_sync_manager.h" +#include "token_sync_manager_client.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class TokenSyncLoadCallback : public SystemAbilityLoadCallbackStub { +public: + explicit TokenSyncLoadCallback(); + + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject); + void OnLoadSystemAbilityFail(int32_t systemAbilityId); +private: +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // TOKEN_SYNC_LOAD_CALLBACK_H diff --git a/interfaces/innerkits/tokensync/src/token_sync_manager_client.cpp b/interfaces/innerkits/tokensync/src/token_sync_manager_client.cpp index f11d38bfa..1eceb97c6 100644 --- a/interfaces/innerkits/tokensync/src/token_sync_manager_client.cpp +++ b/interfaces/innerkits/tokensync/src/token_sync_manager_client.cpp @@ -19,6 +19,7 @@ #include "hap_token_info_for_sync_parcel.h" #include "native_token_info_for_sync_parcel.h" #include "iservice_registry.h" +#include "token_sync_load_callback.h" namespace OHOS { namespace Security { @@ -39,7 +40,7 @@ TokenSyncManagerClient::TokenSyncManagerClient() TokenSyncManagerClient::~TokenSyncManagerClient() {} -int TokenSyncManagerClient::GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID) const +int TokenSyncManagerClient::GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID) { ACCESSTOKEN_LOG_DEBUG(LABEL, "%{public}s: called!", __func__); auto proxy = GetProxy(); @@ -50,7 +51,7 @@ int TokenSyncManagerClient::GetRemoteHapTokenInfo(const std::string& deviceID, A return proxy->GetRemoteHapTokenInfo(deviceID, tokenID); } -int TokenSyncManagerClient::DeleteRemoteHapTokenInfo(AccessTokenID tokenID) const +int TokenSyncManagerClient::DeleteRemoteHapTokenInfo(AccessTokenID tokenID) { ACCESSTOKEN_LOG_DEBUG(LABEL, "%{public}s: called!", __func__); auto proxy = GetProxy(); @@ -61,7 +62,7 @@ int TokenSyncManagerClient::DeleteRemoteHapTokenInfo(AccessTokenID tokenID) cons return proxy->DeleteRemoteHapTokenInfo(tokenID); } -int TokenSyncManagerClient::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo) const +int TokenSyncManagerClient::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo) { ACCESSTOKEN_LOG_DEBUG(LABEL, "%{public}s: called!", __func__); auto proxy = GetProxy(); @@ -72,23 +73,67 @@ int TokenSyncManagerClient::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& return proxy->UpdateRemoteHapTokenInfo(tokenInfo); } -sptr TokenSyncManagerClient::GetProxy() const +void TokenSyncManagerClient::LoadTokenSync() { - auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (sam == nullptr) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "GetSystemAbilityManager is null"); - return nullptr; - } - auto tokensyncSa = sam->GetSystemAbility(ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE); - if (tokensyncSa == nullptr) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "GetSystemAbility %{public}d is null", - ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE); - return nullptr; + if (remoteObject_ == nullptr) { + auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sam == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager return null"); + return; + } + + sptr ptrTokenSyncLoadCallback = new TokenSyncLoadCallback(); + + int32_t result = sam->LoadSystemAbility(ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE, + ptrTokenSyncLoadCallback); + if (result != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "LoadSystemAbility %{public}d failed", + ITokenSyncManager::SA_ID_TOKENSYNC_MANAGER_SERVICE); + return; + } + + std::unique_lock lock(tokenSyncMutex_); + // wait_for release lock and block until time out(60s) or match the condition with notice + auto waitStatus = tokenSyncCon_.wait_for(lock, std::chrono::milliseconds(TOKEN_SYNC_LOAD_SA_TIMEOUT_MS), + [this]() { return remoteObject_ != nullptr; }); + if (!waitStatus) { + // time out or loadcallback fail + ACCESSTOKEN_LOG_ERROR(LABEL, "tokensync load sa timeout"); + return; + } } +} + +void TokenSyncManagerClient::FinishStartSASuccess(const sptr &remoteObject) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "%{public}s: called!", __func__); + ACCESSTOKEN_LOG_INFO(LABEL, "get tokensync sa success."); + + remoteObject_ = remoteObject; + + // get lock which wait_for release and send a notice so that wait_for can out of block + std::unique_lock lock(tokenSyncMutex_); + + tokenSyncCon_.notify_one(); +} + +void TokenSyncManagerClient::FinishStartSAFailed() +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "%{public}s: called!", __func__); + ACCESSTOKEN_LOG_INFO(LABEL, "get tokensync sa failed."); + + // get lock which wait_for release and send a notice + std::unique_lock lock(tokenSyncMutex_); + tokenSyncCon_.notify_one(); +} + +sptr TokenSyncManagerClient::GetProxy() +{ + LoadTokenSync(); - auto proxy = iface_cast(tokensyncSa); + auto proxy = iface_cast(remoteObject_); if (proxy == nullptr) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "iface_cast get null"); + ACCESSTOKEN_LOG_ERROR(LABEL, "iface_cast get null"); return nullptr; } return proxy; diff --git a/interfaces/innerkits/tokensync/src/token_sync_manager_client.h b/interfaces/innerkits/tokensync/src/token_sync_manager_client.h index f416cba31..4f615ca01 100644 --- a/interfaces/innerkits/tokensync/src/token_sync_manager_client.h +++ b/interfaces/innerkits/tokensync/src/token_sync_manager_client.h @@ -17,6 +17,8 @@ #define ACCESSTOKEN_MANAGER_CLIENT_H #include +#include +#include #include "access_token.h" #include "hap_token_info.h" @@ -28,20 +30,29 @@ namespace Security { namespace AccessToken { class TokenSyncManagerClient final { public: + static const int TOKEN_SYNC_LOAD_SA_TIMEOUT_MS = 60000; + static TokenSyncManagerClient& GetInstance(); virtual ~TokenSyncManagerClient(); - int GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID) const; - int DeleteRemoteHapTokenInfo(AccessTokenID tokenID) const; - int UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo) const; + int GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID); + int DeleteRemoteHapTokenInfo(AccessTokenID tokenID); + int UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo); + void LoadTokenSync(); + void FinishStartSASuccess(const sptr &remoteObject); + void FinishStartSAFailed(); private: + std::condition_variable tokenSyncCon_; + std::mutex tokenSyncMutex_; + sptr remoteObject_ = nullptr; + TokenSyncManagerClient(); DISALLOW_COPY_AND_MOVE(TokenSyncManagerClient); - sptr GetProxy() const; + sptr GetProxy(); }; } // namespace AccessToken } // namespace Security diff --git a/services/accesstokenmanager/BUILD.gn b/services/accesstokenmanager/BUILD.gn index 8d620f191..ce41d71d6 100644 --- a/services/accesstokenmanager/BUILD.gn +++ b/services/accesstokenmanager/BUILD.gn @@ -30,9 +30,14 @@ ohos_shared_library("accesstoken_manager_service") { "main/cpp/include/token", "main/cpp/include/permission", "main/cpp/include/database", - "//base/security/access_token/frameworks/common/include", + "main/cpp/include/device", "//base/security/access_token/frameworks/accesstoken/include", + "//base/security/access_token/frameworks/common/include", + "//base/security/access_token/frameworks/tokensync/include", "//base/security/access_token/interfaces/innerkits/accesstoken/include", + "//base/security/access_token/interfaces/innerkits/tokensync/src", + "//base/security/access_token/services/tokensyncmanager/include/common", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", "//third_party/json/include", ] @@ -44,6 +49,7 @@ ohos_shared_library("accesstoken_manager_service") { "main/cpp/src/database/sqlite_storage.cpp", "main/cpp/src/database/statement.cpp", "main/cpp/src/database/variant_value.cpp", + "main/cpp/src/device/atm_device_state_callback.cpp", "main/cpp/src/permission/permission_definition_cache.cpp", "main/cpp/src/permission/permission_manager.cpp", "main/cpp/src/permission/permission_policy_set.cpp", @@ -83,6 +89,7 @@ ohos_shared_library("accesstoken_manager_service") { } external_deps = [ + "device_manager_base:devicemanagersdk", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", diff --git a/services/accesstokenmanager/main/cpp/include/device/atm_device_state_callback.h b/services/accesstokenmanager/main/cpp/include/device/atm_device_state_callback.h new file mode 100644 index 000000000..2fbc53008 --- /dev/null +++ b/services/accesstokenmanager/main/cpp/include/device/atm_device_state_callback.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ATM_DEVICE_STATE_CALLBACK_H +#define ATM_DEVICE_STATE_CALLBACK_H + +#include "device_manager_callback.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +using OHOS::DistributedHardware::DeviceStateCallback; +using OHOS::DistributedHardware::DmDeviceInfo; +using OHOS::DistributedHardware::DmInitCallback; + +class AtmDmInitCallback final : public DmInitCallback { + void OnRemoteDied() override + {} +}; + +class AtmDeviceStateCallback final : public DeviceStateCallback { +public: + AtmDeviceStateCallback(); + ~AtmDeviceStateCallback(); + + void OnDeviceOnline(const DmDeviceInfo &deviceInfo) override; + void OnDeviceOffline(const DmDeviceInfo &deviceInfo) override; + void OnDeviceReady(const DmDeviceInfo &deviceInfo) override; + void OnDeviceChanged(const DmDeviceInfo &deviceInfo) override; + bool GetListen(); + void SetListen(bool b); + +private: + std::atomic_bool hasListened_ = false; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // ATM_DEVICE_STATE_CALLBACK_H diff --git a/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_service.h b/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_service.h index cdb850124..aaf4f4d8f 100644 --- a/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_service.h +++ b/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_service.h @@ -18,6 +18,7 @@ #include #include +#include #include "accesstoken_manager_stub.h" #include "iremote_object.h" @@ -26,6 +27,7 @@ #include "system_ability.h" #include "hap_token_info.h" #include "access_token.h" +#include "atm_device_state_callback.h" namespace OHOS { namespace Security { @@ -76,9 +78,13 @@ public: void DumpTokenInfo(std::string& dumpInfo) override; private: - bool Initialize() const; + bool Initialize(); + void CreateDeviceListenner(); + void DestroyDeviceListenner(); ServiceRunningState state_; + std::mutex mutex_; + std::shared_ptr atmDSCallback_; }; } // namespace AccessToken } // namespace Security diff --git a/services/accesstokenmanager/main/cpp/src/device/atm_device_state_callback.cpp b/services/accesstokenmanager/main/cpp/src/device/atm_device_state_callback.cpp new file mode 100644 index 000000000..0e6d2a2f7 --- /dev/null +++ b/services/accesstokenmanager/main/cpp/src/device/atm_device_state_callback.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "atm_device_state_callback.h" + +#include + +#include "accesstoken_log.h" +#include "constant.h" +#include "device_manager.h" +#include "dm_device_info.h" +#include "token_sync_manager_client.h" +#include "accesstoken_manager_service.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "AtmDeviceStateCallback"}; +} + + +AtmDeviceStateCallback::AtmDeviceStateCallback() +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "AtmDeviceStateCallback()"); +} + +AtmDeviceStateCallback::~AtmDeviceStateCallback() +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "~AtmDeviceStateCallback()"); +} + +void AtmDeviceStateCallback::OnDeviceOnline(const DmDeviceInfo &deviceInfo) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "device online: deviceId is %{public}s, deviceName is %{public}s, " + "deviceTypeId is %{public}d, nreworkId is %{public}s.", deviceInfo.deviceId, deviceInfo.deviceName, + deviceInfo.deviceTypeId, deviceInfo.networkId); + + // when remote device online, start tokensync sa if not exsit + TokenSyncManagerClient::GetInstance().LoadTokenSync(); +} + +void AtmDeviceStateCallback::OnDeviceOffline(const DmDeviceInfo &deviceInfo) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "device offline: deviceId is %{public}s, deviceName is %{public}s, " + "deviceTypeId is %{public}d, nreworkId is %{public}s.", deviceInfo.deviceId, deviceInfo.deviceName, + deviceInfo.deviceTypeId, deviceInfo.networkId); + + // when offline, tokensync will exit, so hasListened_ should turn to flase + SetListen(false); +} + +void AtmDeviceStateCallback::OnDeviceReady(const DmDeviceInfo &deviceInfo) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "device ready: deviceId is %{public}s, deviceName is %{public}s, " + "deviceTypeId is %{public}d, nreworkId is %{public}s.", deviceInfo.deviceId, deviceInfo.deviceName, + deviceInfo.deviceTypeId, deviceInfo.networkId); +} + +void AtmDeviceStateCallback::OnDeviceChanged(const DmDeviceInfo &deviceInfo) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "device changed: deviceId is %{public}s, deviceName is %{public}s, " + "deviceTypeId is %{public}d, nreworkId is %{public}s.", deviceInfo.deviceId, deviceInfo.deviceName, + deviceInfo.deviceTypeId, deviceInfo.networkId); +} + +bool AtmDeviceStateCallback::GetListen() +{ + return hasListened_.load(); +} + +void AtmDeviceStateCallback::SetListen(bool b) +{ + hasListened_.store(b); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp b/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp index b1ebfed38..2103ae2ac 100644 --- a/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp +++ b/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp @@ -15,6 +15,8 @@ #include "accesstoken_manager_service.h" +#include + #include "access_token.h" #include "accesstoken_id_manager.h" #include "accesstoken_info_manager.h" @@ -26,6 +28,7 @@ #include "native_token_receptor.h" #include "permission_list_state.h" #include "permission_manager.h" +#include "device_manager.h" namespace OHOS { namespace Security { @@ -38,6 +41,8 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(DelayedSingleton::GetInstance().get()); +const std::string TOKEN_SYNC_PACKAGE_NAME = "ohos.security.distributed_token_sync"; +const int32_t RETRY_SLEEP_TIME_MS = 1000; AccessTokenManagerService::AccessTokenManagerService() : SystemAbility(SA_ID_ACCESSTOKEN_MANAGER_SERVICE, true), state_(ServiceRunningState::STATE_NOT_START) @@ -74,6 +79,7 @@ void AccessTokenManagerService::OnStop() { ACCESSTOKEN_LOG_INFO(LABEL, "stop service"); state_ = ServiceRunningState::STATE_NOT_START; + DestroyDeviceListenner(); } int AccessTokenManagerService::VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName) @@ -353,10 +359,86 @@ void AccessTokenManagerService::DumpTokenInfo(std::string& dumpInfo) AccessTokenInfoManager::GetInstance().DumpTokenInfo(dumpInfo); } -bool AccessTokenManagerService::Initialize() const +void AccessTokenManagerService::CreateDeviceListenner() +{ + std::function runner = [&]() { + auto retrySleepTime = std::chrono::milliseconds(RETRY_SLEEP_TIME_MS); + while (1) { + std::unique_lock lock(mutex_); + + if (!atmDSCallback_->GetListen()) { + std::string packageName = TOKEN_SYNC_PACKAGE_NAME; + std::shared_ptr ptrDmInitCallback = std::make_shared(); + + int ret = + DistributedHardware::DeviceManager::GetInstance().InitDeviceManager(packageName, ptrDmInitCallback); + if (ret != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: InitDeviceManager error, result: %{public}d", ret); + std::this_thread::sleep_for(retrySleepTime); + continue; + } + + ACCESSTOKEN_LOG_INFO(LABEL, "device manager init success."); + + std::string extra = ""; + std::shared_ptr ptrAtmDeviceStateCallback = + std::make_shared(); + ret = DistributedHardware::DeviceManager::GetInstance().RegisterDevStateCallback(packageName, extra, + ptrAtmDeviceStateCallback); + if (ret != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: RegisterDevStateCallback error, result: %{public}d", ret); + std::this_thread::sleep_for(retrySleepTime); + continue; + } + + atmDSCallback_->SetListen(true); + + ACCESSTOKEN_LOG_INFO(LABEL, "device state listenner register success."); + } + } + }; + + std::thread initThread(runner); + initThread.detach(); + + ACCESSTOKEN_LOG_DEBUG(LABEL, "start a thread to listen device state."); +} + +void AccessTokenManagerService::DestroyDeviceListenner() +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "destroy, init: %{public}d", atmDSCallback_->GetListen()); + + std::unique_lock lock(mutex_); + + if (!atmDSCallback_->GetListen()) { + ACCESSTOKEN_LOG_DEBUG(LABEL, "not listened, skip"); + return; + } + + std::string packageName = TOKEN_SYNC_PACKAGE_NAME; + + int ret = DistributedHardware::DeviceManager::GetInstance().UnRegisterDevStateCallback(packageName); + if (ret != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "UnRegisterDevStateCallback failed, code: %{public}d", ret); + } + + ret = DistributedHardware::DeviceManager::GetInstance().UnInitDeviceManager(packageName); + if (ret != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "UnInitDeviceManager failed, code: %{public}d", ret); + } + + atmDSCallback_->SetListen(false); + + ACCESSTOKEN_LOG_INFO(LABEL, "device state listenner unregister success."); +} + +bool AccessTokenManagerService::Initialize() { + atmDSCallback_ = std::make_shared(); + AccessTokenInfoManager::GetInstance().Init(); - NativeTokenReceptor::GetInstance().Init(); + NativeTokenReceptor::GetInstance().Init(); + CreateDeviceListenner(); // for start tokensync when remote devivce online return true; } } // namespace AccessToken diff --git a/services/tokensyncmanager/include/remote/soft_bus_device_connection_listener.h b/services/tokensyncmanager/include/remote/soft_bus_device_connection_listener.h index c8722cbe1..2a207dbaf 100644 --- a/services/tokensyncmanager/include/remote/soft_bus_device_connection_listener.h +++ b/services/tokensyncmanager/include/remote/soft_bus_device_connection_listener.h @@ -23,7 +23,6 @@ #include "accesstoken_log.h" #include "device_manager_callback.h" #include "dm_device_info.h" -#include "softbus_bus_center.h" namespace OHOS { namespace Security { diff --git a/services/tokensyncmanager/include/remote/soft_bus_manager.h b/services/tokensyncmanager/include/remote/soft_bus_manager.h index 4a4a5bc77..a29b79ca2 100644 --- a/services/tokensyncmanager/include/remote/soft_bus_manager.h +++ b/services/tokensyncmanager/include/remote/soft_bus_manager.h @@ -106,6 +106,8 @@ public: private: SoftBusManager(); + int DeviceInit(); + int SessionInit(); /** * @brief Fulfill local device info @@ -115,10 +117,19 @@ private: * @version 1.0 */ int FulfillLocalDeviceInfo(); + + /** + * @brief add all trusted device info. + * + * @since 1.0 + * @version 1.0 + */ + int AddTrustedDeviceInfo(); + std::string GetUuidByNodeId(const std::string &nodeId) const; std::string GetUdidByNodeId(const std::string &nodeId) const; - const static std::string ACCESS_TOKEN_PACKAGE_NAME; + const static std::string TOKEN_SYNC_PACKAGE_NAME; // soft bus session server opened flag bool isSoftBusServiceBindSuccess_; diff --git a/services/tokensyncmanager/sa_profile/3504.xml b/services/tokensyncmanager/sa_profile/3504.xml index 4293f1452..b0ef046f1 100644 --- a/services/tokensyncmanager/sa_profile/3504.xml +++ b/services/tokensyncmanager/sa_profile/3504.xml @@ -17,7 +17,7 @@ 3504 libtoken_sync_manager_service.z.so - true + false true 1 diff --git a/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp b/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp index 47143b362..514e96258 100644 --- a/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp +++ b/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp @@ -17,6 +17,9 @@ #include "remote_command_manager.h" #include "soft_bus_manager.h" #include "device_info_manager.h" +#include "softbus_bus_center.h" +#include "device_manager.h" +#include "dm_device_info.h" namespace OHOS { namespace Security { @@ -25,6 +28,9 @@ namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "SoftBusDeviceConnectionListener"}; } + +const std::string ACCESSTOKEN_PACKAGE_NAME = "ohos.security.distributed_access_token"; + SoftBusDeviceConnectionListener::SoftBusDeviceConnectionListener() { ACCESSTOKEN_LOG_DEBUG(LABEL, "SoftBusDeviceConnectionListener()"); @@ -72,6 +78,23 @@ void SoftBusDeviceConnectionListener::OnDeviceOffline(const DmDeviceInfo &info) RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid); RemoteCommandManager::GetInstance().NotifyDeviceOffline(udid); DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(networkId, DeviceIdType::NETWORK_ID); + + std::string packageName = ACCESSTOKEN_PACKAGE_NAME; + std::string extra = ""; + std::vector deviceList; + + int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(packageName, + extra, deviceList); + if (ret != Constant::SUCCESS) { + ACCESSTOKEN_LOG_ERROR(LABEL, "AddTrustedDeviceInfo: GetTrustedDeviceList error, result: %{public}d", ret); + return; + } + + if (deviceList.size() == 0) { + ACCESSTOKEN_LOG_INFO(LABEL, "there is no remote decice online, exit tokensync process"); + + exit(0); + } } else { ACCESSTOKEN_LOG_ERROR(LABEL, "uuid or udid is empty, offline failed."); } diff --git a/services/tokensyncmanager/src/remote/soft_bus_manager.cpp b/services/tokensyncmanager/src/remote/soft_bus_manager.cpp index aedf422b8..08a51d060 100644 --- a/services/tokensyncmanager/src/remote/soft_bus_manager.cpp +++ b/services/tokensyncmanager/src/remote/soft_bus_manager.cpp @@ -18,6 +18,9 @@ #include "device_info_manager.h" #include "parameter.h" +#include "softbus_bus_center.h" +#include "dm_device_info.h" +#include "remote_command_manager.h" namespace OHOS { namespace Security { @@ -35,7 +38,7 @@ static const int OPENSESSION_RETRY_INTERVAL_MS = 100; static const int UDID_MAX_LENGTH = 128; // udid/uuid max length } // namespace -const std::string SoftBusManager::ACCESS_TOKEN_PACKAGE_NAME = "ohos.security.distributed_access_token"; +const std::string SoftBusManager::TOKEN_SYNC_PACKAGE_NAME = "ohos.security.distributed_access_token"; const std::string SoftBusManager::SESSION_NAME = "ohos.security.atm_channel"; SoftBusManager::SoftBusManager() : isSoftBusServiceBindSuccess_(false), inited_(false), mutex_(), fulfillMutex_() @@ -54,6 +57,85 @@ SoftBusManager &SoftBusManager::GetInstance() return instance; } +int SoftBusManager::AddTrustedDeviceInfo() +{ + std::string packageName = TOKEN_SYNC_PACKAGE_NAME; + std::string extra = ""; + std::vector deviceList; + + int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(packageName, + extra, deviceList); + if (ret != Constant::SUCCESS) { + ACCESSTOKEN_LOG_ERROR(LABEL, "AddTrustedDeviceInfo: GetTrustedDeviceList error, result: %{public}d", ret); + return Constant::FAILURE; + } + + for (DmDeviceInfo device : deviceList) { + std::string uuid = GetUuidByNodeId(device.networkId); + std::string udid = GetUdidByNodeId(device.networkId); + if (uuid.empty() || udid.empty()) { + ACCESSTOKEN_LOG_ERROR(LABEL, "AddTrustedDeviceInfo: uuid or udid is empty, abort."); + continue; + } + + DeviceInfoManager::GetInstance().AddDeviceInfo(device.networkId, uuid, udid, device.deviceName, + std::to_string(device.deviceTypeId)); + RemoteCommandManager::GetInstance().NotifyDeviceOnline(udid); + } + + return Constant::SUCCESS; +} + +int SoftBusManager::DeviceInit() +{ + std::string packageName = TOKEN_SYNC_PACKAGE_NAME; + std::shared_ptr ptrDmInitCallback = std::make_shared(); + + int ret = DistributedHardware::DeviceManager::GetInstance().InitDeviceManager(packageName, ptrDmInitCallback); + if (ret != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: InitDeviceManager error, result: %{public}d", ret); + return ret; + } + + ret = AddTrustedDeviceInfo(); + if (ret != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: AddTrustedDeviceInfo error, result: %{public}d", ret); + return ret; + } + + std::string extra = ""; + std::shared_ptr ptrDeviceStateCallback = + std::make_shared(); + ret = DistributedHardware::DeviceManager::GetInstance().RegisterDevStateCallback(packageName, extra, + ptrDeviceStateCallback); + if (ret != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: RegisterDevStateCallback error, result: %{public}d", ret); + return ret; + } + + return ERR_OK; +} + +int SoftBusManager::SessionInit() +{ + // register session listener + ISessionListener sessionListener; + sessionListener.OnSessionOpened = SoftBusSessionListener::OnSessionOpened; + sessionListener.OnSessionClosed = SoftBusSessionListener::OnSessionClosed; + sessionListener.OnBytesReceived = SoftBusSessionListener::OnBytesReceived; + sessionListener.OnMessageReceived = SoftBusSessionListener::OnMessageReceived; + + int ret = ::CreateSessionServer(TOKEN_SYNC_PACKAGE_NAME.c_str(), SESSION_NAME.c_str(), &sessionListener); + ACCESSTOKEN_LOG_INFO(LABEL, "Initialize: createSessionServer, result: %{public}d", ret); + // REASON_EXIST + if ((ret != Constant::SUCCESS) && (ret != REASON_EXIST)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: CreateSessionServer error, result: %{public}d", ret); + return ret; + } + + return ERR_OK; +} + void SoftBusManager::Initialize() { bool inited = false; @@ -67,39 +149,15 @@ void SoftBusManager::Initialize() auto sleepTime = std::chrono::milliseconds(1000); while (1) { std::unique_lock lock(mutex_); - std::string packageName = ACCESS_TOKEN_PACKAGE_NAME; - std::shared_ptr ptrDmInitCallback = std::make_shared(); - int ret = - DistributedHardware::DeviceManager::GetInstance().InitDeviceManager(packageName, ptrDmInitCallback); + + int ret = DeviceInit(); if (ret != ERR_OK) { - ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: InitDeviceManager error, result: %{public}d", ret); std::this_thread::sleep_for(sleepTime); continue; } - std::string extra = ""; - std::shared_ptr ptrDeviceStateCallback = - std::make_shared(); - ret = DistributedHardware::DeviceManager::GetInstance().RegisterDevStateCallback(packageName, extra, - ptrDeviceStateCallback); + ret = SessionInit(); if (ret != ERR_OK) { - ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: RegisterDevStateCallback error, result: %{public}d", ret); - std::this_thread::sleep_for(sleepTime); - continue; - } - - // register session listener - ISessionListener sessionListener; - sessionListener.OnSessionOpened = SoftBusSessionListener::OnSessionOpened; - sessionListener.OnSessionClosed = SoftBusSessionListener::OnSessionClosed; - sessionListener.OnBytesReceived = SoftBusSessionListener::OnBytesReceived; - sessionListener.OnMessageReceived = SoftBusSessionListener::OnMessageReceived; - - ret = ::CreateSessionServer(ACCESS_TOKEN_PACKAGE_NAME.c_str(), SESSION_NAME.c_str(), &sessionListener); - ACCESSTOKEN_LOG_INFO(LABEL, "Initialize: createSessionServer, result: %{public}d", ret); - // REASON_EXIST - if ((ret != Constant::SUCCESS) && (ret != REASON_EXIST)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: CreateSessionServer error, result: %{public}d", ret); std::this_thread::sleep_for(sleepTime); continue; } @@ -132,12 +190,12 @@ void SoftBusManager::Destroy() } if (isSoftBusServiceBindSuccess_) { - int32_t ret = ::RemoveSessionServer(ACCESS_TOKEN_PACKAGE_NAME.c_str(), SESSION_NAME.c_str()); + int32_t ret = ::RemoveSessionServer(TOKEN_SYNC_PACKAGE_NAME.c_str(), SESSION_NAME.c_str()); ACCESSTOKEN_LOG_DEBUG(LABEL, "destroy, RemoveSessionServer: %{public}d", ret); isSoftBusServiceBindSuccess_ = false; } - std::string packageName = ACCESS_TOKEN_PACKAGE_NAME; + std::string packageName = TOKEN_SYNC_PACKAGE_NAME; int ret = DistributedHardware::DeviceManager::GetInstance().UnRegisterDevStateCallback(packageName); if (ret != ERR_OK) { ACCESSTOKEN_LOG_ERROR(LABEL, "UnRegisterDevStateCallback failed, code: %{public}d", ret); @@ -275,7 +333,7 @@ std::string SoftBusManager::GetUuidByNodeId(const std::string &nodeId) const return ""; } (void)memset_s(info, UDID_MAX_LENGTH + 1, 0, UDID_MAX_LENGTH + 1); - int32_t ret = ::GetNodeKeyInfo(ACCESS_TOKEN_PACKAGE_NAME.c_str(), nodeId.c_str(), + int32_t ret = ::GetNodeKeyInfo(TOKEN_SYNC_PACKAGE_NAME.c_str(), nodeId.c_str(), NodeDeviceInfoKey::NODE_KEY_UUID, info, UDID_MAX_LENGTH); if (ret != Constant::SUCCESS) { delete[] info; @@ -297,7 +355,7 @@ std::string SoftBusManager::GetUdidByNodeId(const std::string &nodeId) const return ""; } (void)memset_s(info, UDID_MAX_LENGTH + 1, 0, UDID_MAX_LENGTH + 1); - int32_t ret = ::GetNodeKeyInfo(ACCESS_TOKEN_PACKAGE_NAME.c_str(), nodeId.c_str(), + int32_t ret = ::GetNodeKeyInfo(TOKEN_SYNC_PACKAGE_NAME.c_str(), nodeId.c_str(), NodeDeviceInfoKey::NODE_KEY_UDID, info, UDID_MAX_LENGTH); if (ret != Constant::SUCCESS) { delete[] info; @@ -319,7 +377,7 @@ int SoftBusManager::FulfillLocalDeviceInfo() } NodeBasicInfo info; - int32_t ret = ::GetLocalNodeDeviceInfo(ACCESS_TOKEN_PACKAGE_NAME.c_str(), &info); + int32_t ret = ::GetLocalNodeDeviceInfo(TOKEN_SYNC_PACKAGE_NAME.c_str(), &info); if (ret != Constant::SUCCESS) { ACCESSTOKEN_LOG_ERROR(LABEL, "GetLocalNodeDeviceInfo error"); fulfillMutex_.unlock(); diff --git a/services/tokensyncmanager/src/service/token_sync_manager_service.cpp b/services/tokensyncmanager/src/service/token_sync_manager_service.cpp index adb43917b..748e2248c 100644 --- a/services/tokensyncmanager/src/service/token_sync_manager_service.cpp +++ b/services/tokensyncmanager/src/service/token_sync_manager_service.cpp @@ -34,7 +34,7 @@ const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(DelayedSingleton::GetInstance().get()); TokenSyncManagerService::TokenSyncManagerService() - : SystemAbility(SA_ID_TOKENSYNC_MANAGER_SERVICE, true), state_(ServiceRunningState::STATE_NOT_START) + : SystemAbility(SA_ID_TOKENSYNC_MANAGER_SERVICE, false), state_(ServiceRunningState::STATE_NOT_START) { ACCESSTOKEN_LOG_INFO(LABEL, "TokenSyncManagerService()"); } @@ -68,6 +68,7 @@ void TokenSyncManagerService::OnStop() { ACCESSTOKEN_LOG_INFO(LABEL, "stop service"); state_ = ServiceRunningState::STATE_NOT_START; + SoftBusManager::GetInstance().Destroy(); } std::shared_ptr TokenSyncManagerService::GetSendEventHandler() const @@ -174,7 +175,7 @@ bool TokenSyncManagerService::Initialize() sendHandler_ = std::make_shared(sendRunner_); if (!sendHandler_) { - ACCESSTOKEN_LOG_ERROR(LABEL, "sendHandler_ is nullpter."); + ACCESSTOKEN_LOG_ERROR(LABEL, "sendHandler_ is nullptr."); return false; } @@ -186,7 +187,7 @@ bool TokenSyncManagerService::Initialize() recvHandler_ = std::make_shared(recvRunner_); if (!recvHandler_) { - ACCESSTOKEN_LOG_ERROR(LABEL, "recvHandler_ is nullpter."); + ACCESSTOKEN_LOG_ERROR(LABEL, "recvHandler_ is nullptr."); return false; } diff --git a/services/tokensyncmanager/token_sync.cfg b/services/tokensyncmanager/token_sync.cfg index e61f7e5a9..d836ae772 100644 --- a/services/tokensyncmanager/token_sync.cfg +++ b/services/tokensyncmanager/token_sync.cfg @@ -1,15 +1,9 @@ { - "jobs" : [{ - "name" : "late-fs", - "cmds" : [ - "start token_sync_service" - ] - } - ], "services" : [{ "name" : "token_sync_service", "path" : ["/system/bin/sa_main", "/system/profile/token_sync_service.xml"], - "importance" : -20, + "ondemand" : true, + "start-mode" : "condition", "uid" : "system", "gid" : ["system"], "secon" : "u:r:token_sync_service:s0" -- Gitee