diff --git a/services/distributeddataservice/adapter/communicator/src/communicator_context.cpp b/services/distributeddataservice/adapter/communicator/src/communicator_context.cpp index 76f63db9f18f27dedcacf9e5f37ec6601c610d08..ba1e14c5e47c2631e961169d91bbcc42cf6ae2e0 100644 --- a/services/distributeddataservice/adapter/communicator/src/communicator_context.cpp +++ b/services/distributeddataservice/adapter/communicator/src/communicator_context.cpp @@ -119,4 +119,19 @@ bool CommunicatorContext::IsSessionReady(const std::string &deviceId) } return devices_.Contains(deviceId); } + +void CommunicatorContext::SetUserChangeListener(const OnUserChange &userChangeListener) +{ + std::lock_guard userLockGard(userChangeMutex_); + userChangeListener_ = closeAbleCallback; +} + +void CommunicatorContext::ClearUserSession() +{ + std::lock_guard userLockGard(userChangeMutex_); + if (userChangeListener_) { + userChangeListener_(); + } + devices_.Clear(); +} } // namespace OHOS::DistributedData \ No newline at end of file diff --git a/services/distributeddataservice/adapter/communicator/src/softbus_adapter.h b/services/distributeddataservice/adapter/communicator/src/softbus_adapter.h index 08c572812639eef7555921feb2c7259d6562c21a..0d7973925752443af5cd1dcd6f801722fa489508 100644 --- a/services/distributeddataservice/adapter/communicator/src/softbus_adapter.h +++ b/services/distributeddataservice/adapter/communicator/src/softbus_adapter.h @@ -93,6 +93,7 @@ private: void StartCloseSessionTask(const std::string &deviceId); Task GetCloseSessionTask(); bool CloseSession(const std::string &networkId); + void CloseAllSession(); void GetExpireTime(std::shared_ptr &conn); std::pair OpenConnect(const std::shared_ptr &conn, const DeviceId &deviceId); std::shared_ptr GetConnect(const PipeInfo &pipeInfo, const DeviceId &deviceId, uint32_t qosType); diff --git a/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp b/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp index 81b9f74e4cf492bf50fd8eb15cdeade7abb8343c..fe0943689565090b05787b4293ae22f2e8efc55a 100644 --- a/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp +++ b/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp @@ -111,7 +111,9 @@ SoftBusAdapter::SoftBusAdapter() Context::GetInstance().SetSessionListener([this](const std::string &deviceId) { StartCloseSessionTask(deviceId); }); - + Context::GetInstance().SetUserChangeListener([this]() { + CloseAllSession(); + }); ConnectManager::GetInstance()->RegisterCloseSessionTask([this](const std::string &networkId) { return CloseSession(networkId); }); @@ -572,6 +574,11 @@ void SoftBusAdapter::OnDeviceChanged(const AppDistributedKv::DeviceInfo &info, return; } +void SoftBusAdapter::CloseAllSession() +{ + connects_.Clear(); +} + bool SoftBusAdapter::CloseSession(const std::string &networkId) { auto uuid = DmAdapter::GetInstance().GetUuidByNetworkId(networkId); diff --git a/services/distributeddataservice/adapter/include/communicator/communicator_context.h b/services/distributeddataservice/adapter/include/communicator/communicator_context.h index ce09ab58a92c4e58d37bf62b8f497cbb7af93ecb..0454dc282d672a476290ebcf3a073a061f75a516 100644 --- a/services/distributeddataservice/adapter/include/communicator/communicator_context.h +++ b/services/distributeddataservice/adapter/include/communicator/communicator_context.h @@ -30,6 +30,7 @@ public: using DevChangeListener = OHOS::AppDistributedKv::AppDeviceChangeListener; using DeviceInfo = OHOS::AppDistributedKv::DeviceInfo; using OnCloseAble = std::function; + using OnUserChange = std::function; API_EXPORT static CommunicatorContext &GetInstance(); API_EXPORT void SetThreadPool(std::shared_ptr executors); @@ -40,7 +41,8 @@ public: void NotifySessionClose(const std::string &deviceId); void SetSessionListener(const OnCloseAble &closeAbleCallback); bool IsSessionReady(const std::string &deviceId); - + void SetUserChangeListener(const OnUserChange &userChangeListener); + void ClearUserSession(); private: CommunicatorContext() = default; ~CommunicatorContext() = default; @@ -50,7 +52,9 @@ private: CommunicatorContext &operator=(CommunicatorContext &&) = delete; mutable std::mutex sessionMutex_; + mutable std::mutex userChangeMutex_; OnCloseAble closeListener_; + OnUserChange userChangeListener_; std::shared_ptr executors_; std::mutex mutex_; std::vector observers_; diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 2c26686d8ad61f6aa75db9a2d49f447d50e0375a..e7a05b6a80cdda4cadc320606d69a3c5041288cc 100755 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -887,6 +887,7 @@ void KvStoreDataService::NotifyAccountEvent(const AccountEventInfo &eventInfo) } return false; }); + CommunicatorContext::GetInstance().ClearUserSession(); break; } case AccountStatus::DEVICE_ACCOUNT_STOPPING: