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 7d0086efbb6f6bde656e59d58858e05602d2ac90..c2e13cb184c18618f8a04d1a48918ee4895937ec 100644 --- a/services/tokensyncmanager/include/remote/soft_bus_device_connection_listener.h +++ b/services/tokensyncmanager/include/remote/soft_bus_device_connection_listener.h @@ -18,6 +18,7 @@ #include #include +#include #include #include "accesstoken_log.h" @@ -64,6 +65,9 @@ public: * @param deviceInfo node info */ void OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo) override; + +private: + std::mutex exitMutex_; }; } // namespace AccessToken } // namespace Security 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 1f3e1ea26b830db8b011fa49bdeec7f97bb9dba5..f6b97a2c04f8b1775b1884b48240ee4536cd67eb 100644 --- a/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp +++ b/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp @@ -14,6 +14,9 @@ */ #include "soft_bus_device_connection_listener.h" + +#include + #include "remote_command_manager.h" #include "soft_bus_manager.h" #include "device_info_manager.h" @@ -21,6 +24,8 @@ #include "constant_common.h" #include "device_manager.h" #include "dm_device_info.h" +#include "token_sync_event_handler.h" +#include "token_sync_manager_service.h" namespace OHOS { namespace Security { @@ -73,36 +78,58 @@ void SoftBusDeviceConnectionListener::OnDeviceOffline(const DmDeviceInfo &info) std::string uuid = DeviceInfoManager::GetInstance().ConvertToUniversallyUniqueIdOrFetch(networkId); std::string udid = DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(networkId); - ACCESSTOKEN_LOG_INFO(LABEL, - "networkId: %{public}s, uuid: %{public}s, udid: %{public}s", - networkId.c_str(), - uuid.c_str(), - ConstantCommon::EncryptDevId(udid).c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "networkId: %{public}s, uuid: %{public}s, udid: %{public}s", networkId.c_str(), + uuid.c_str(), ConstantCommon::EncryptDevId(udid).c_str()); - if (uuid != "" && udid != "") { - RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid); - RemoteCommandManager::GetInstance().NotifyDeviceOffline(udid); - DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(networkId, DeviceIdType::NETWORK_ID); + if (uuid == "" || udid == "") { + ACCESSTOKEN_LOG_ERROR(LABEL, "uuid or udid is empty, offline failed."); + return; + } - std::string packageName = ACCESSTOKEN_PACKAGE_NAME; - std::string extra = ""; - std::vector deviceList; + RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid); + RemoteCommandManager::GetInstance().NotifyDeviceOffline(udid); + DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(networkId, DeviceIdType::NETWORK_ID); - int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(packageName, - extra, deviceList); - if (ret != Constant::SUCCESS) { - ACCESSTOKEN_LOG_ERROR(LABEL, "GetTrustedDeviceList error, result: %{public}d", ret); - return; - } + std::function delayed = ([=]() { + ACCESSTOKEN_LOG_INFO(LABEL, "delayed task entry."); + bool sleeped = false; + std::vector deviceList; + std::unique_lock lock(exitMutex_); + + do { + std::string packageName = ACCESSTOKEN_PACKAGE_NAME; + std::string extra = ""; + + int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(packageName, + extra, deviceList); + if (ret != Constant::SUCCESS) { + ACCESSTOKEN_LOG_ERROR(LABEL, "GetTrustedDeviceList error, result: %{public}d", ret); + return; + } + if (deviceList.empty() && sleeped == false) { + ACCESSTOKEN_LOG_INFO(LABEL, "delayed sleep."); + sleep(10000); + sleeped = true; + } else { + sleeped = false; + } + } while (sleeped == true); if (deviceList.empty()) { - ACCESSTOKEN_LOG_INFO(LABEL, "there is no remote decice online, exit tokensync process"); + + ACCESSTOKEN_LOG_INFO(LABEL, "there is no remote device online, exit tokensync process"); + SoftBusManager::GetInstance().Destroy(); + std::shared_ptr handler = + DelayedSingleton::GetInstance()->GetSendEventHandler(); + while ((handler != nullptr) && (!handler->IsIdle())) { + sleep(10); + } exit(0); } - } else { - ACCESSTOKEN_LOG_ERROR(LABEL, "uuid or udid is empty, offline failed."); - } + }); + + std::thread threadObj(delayed); } void SoftBusDeviceConnectionListener::OnDeviceReady(const DmDeviceInfo &info)