From 4d04a640b2cebb9ee58157e3ff4c540e8cf39b3f Mon Sep 17 00:00:00 2001 From: zhouyan Date: Mon, 16 May 2022 09:49:42 +0800 Subject: [PATCH] =?UTF-8?q?tokensyna=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 | 3 + .../src/token_sync_load_callback.cpp | 66 ++++++++++ .../tokensync/src/token_sync_load_callback.h | 37 ++++++ .../src/token_sync_manager_client.cpp | 74 ++++++++--- .../tokensync/src/token_sync_manager_client.h | 19 ++- services/accesstokenmanager/BUILD.gn | 6 + .../device/atm_device_state_callback.h | 69 ++++++++++ .../service/accesstoken_manager_service.h | 6 +- .../src/device/atm_device_state_callback.cpp | 71 ++++++++++ .../service/accesstoken_manager_service.cpp | 54 +++++++- .../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 | 2 + .../src/remote/soft_bus_manager.cpp | 124 +++++++++++++----- .../service/token_sync_manager_service.cpp | 2 +- services/tokensyncmanager/token_sync.cfg | 10 +- 17 files changed, 491 insertions(+), 68 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..b8691777d 100644 --- a/interfaces/innerkits/tokensync/BUILD.gn +++ b/interfaces/innerkits/tokensync/BUILD.gn @@ -32,12 +32,15 @@ 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/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..98caffdd0 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,64 @@ 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_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_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..2ecb07e88 100644 --- a/services/accesstokenmanager/BUILD.gn +++ b/services/accesstokenmanager/BUILD.gn @@ -30,10 +30,14 @@ ohos_shared_library("accesstoken_manager_service") { "main/cpp/include/token", "main/cpp/include/permission", "main/cpp/include/database", + "main/cpp/include/device", "//base/security/access_token/frameworks/common/include", "//base/security/access_token/frameworks/accesstoken/include", "//base/security/access_token/interfaces/innerkits/accesstoken/include", + "//base/security/access_token/interfaces/innerkits/tokensync/src", + "//base/security/access_token/frameworks/tokensync/include", "//third_party/json/include", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", ] sources = [ @@ -44,6 +48,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 +88,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..193ef4ff7 --- /dev/null +++ b/services/accesstokenmanager/main/cpp/include/device/atm_device_state_callback.h @@ -0,0 +1,69 @@ +/* + * 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(); + + /** + * @brief node online callback + * + * @param deviceInfo node info + */ + void OnDeviceOnline(const DmDeviceInfo &deviceInfo) override; + + /** + * @brief node offline callback + * + * @param deviceInfo node info + */ + void OnDeviceOffline(const DmDeviceInfo &deviceInfo) override; + + /** + * @brief node ready callback + * + * @param deviceInfo node info + */ + void OnDeviceReady(const DmDeviceInfo &deviceInfo) override; + + /** + * @brief node changed callback + * + * @param deviceInfo node info + */ + void OnDeviceChanged(const DmDeviceInfo &deviceInfo) override; +}; +} // 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..2c96f4cd5 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" @@ -76,9 +77,12 @@ public: void DumpTokenInfo(std::string& dumpInfo) override; private: - bool Initialize() const; + bool Initialize(); + void ListenDeviceState(); ServiceRunningState state_; + std::mutex mutex_; + std::atomic_bool hasListened_ = false; }; } // 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..7bd61ed67 --- /dev/null +++ b/services/accesstokenmanager/main/cpp/src/device/atm_device_state_callback.cpp @@ -0,0 +1,71 @@ +/* + * 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 "accesstoken_log.h" +#include "token_sync_manager_client.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); +} + +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); +} +} // 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..cf04d9f9c 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,8 @@ #include "native_token_receptor.h" #include "permission_list_state.h" #include "permission_manager.h" +#include "atm_device_state_callback.h" +#include "device_manager.h" namespace OHOS { namespace Security { @@ -38,6 +42,8 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(DelayedSingleton::GetInstance().get()); +const std::string ACCESS_TOKEN_PACKAGE_NAME = "ohos.security.distributed_access_token"; +const int32_t RETRY_SLEEP_TIME_MS = 1000; AccessTokenManagerService::AccessTokenManagerService() : SystemAbility(SA_ID_ACCESSTOKEN_MANAGER_SERVICE, true), state_(ServiceRunningState::STATE_NOT_START) @@ -353,10 +359,56 @@ void AccessTokenManagerService::DumpTokenInfo(std::string& dumpInfo) AccessTokenInfoManager::GetInstance().DumpTokenInfo(dumpInfo); } -bool AccessTokenManagerService::Initialize() const +void AccessTokenManagerService::ListenDeviceState() +{ + std::function runner = [&]() { + auto retrySleepTime = std::chrono::milliseconds(RETRY_SLEEP_TIME_MS); + while (1) { + std::unique_lock lock(mutex_); + + if (!hasListened_) { + std::string packageName = ACCESS_TOKEN_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; + } + + hasListened_ = 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."); +} + +bool AccessTokenManagerService::Initialize() { AccessTokenInfoManager::GetInstance().Init(); NativeTokenReceptor::GetInstance().Init(); + ListenDeviceState(); // 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..1d0bbb530 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,7 @@ #include "remote_command_manager.h" #include "soft_bus_manager.h" #include "device_info_manager.h" +#include "softbus_bus_center.h" namespace OHOS { namespace Security { @@ -25,6 +26,7 @@ namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "SoftBusDeviceConnectionListener"}; } + SoftBusDeviceConnectionListener::SoftBusDeviceConnectionListener() { ACCESSTOKEN_LOG_DEBUG(LABEL, "SoftBusDeviceConnectionListener()"); diff --git a/services/tokensyncmanager/src/remote/soft_bus_manager.cpp b/services/tokensyncmanager/src/remote/soft_bus_manager.cpp index aedf422b8..aed3ef2f8 100644 --- a/services/tokensyncmanager/src/remote/soft_bus_manager.cpp +++ b/services/tokensyncmanager/src/remote/soft_bus_manager.cpp @@ -18,6 +18,8 @@ #include "device_info_manager.h" #include "parameter.h" +#include "softbus_bus_center.h" +#include "dm_device_info.h" namespace OHOS { namespace Security { @@ -35,8 +37,8 @@ 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::SESSION_NAME = "ohos.security.atm_channel"; +const std::string SoftBusManager::TOKEN_SYNC_PACKAGE_NAME = "ohos.security.distributed_token_sync"; +const std::string SoftBusManager::SESSION_NAME = "ohos.security.tsm_channel"; SoftBusManager::SoftBusManager() : isSoftBusServiceBindSuccess_(false), inited_(false), mutex_(), fulfillMutex_() { @@ -54,6 +56,84 @@ 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)); + } + + 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 +147,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 +188,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 +331,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 +353,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 +375,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..c91d158e8 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()"); } 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