diff --git a/interfaces/ipc/include/dinput_sa_manager.h b/interfaces/ipc/include/dinput_sa_manager.h index 8178d6e..f2bbda6 100644 --- a/interfaces/ipc/include/dinput_sa_manager.h +++ b/interfaces/ipc/include/dinput_sa_manager.h @@ -18,6 +18,7 @@ #include #include +#include #include "i_distributed_source_input.h" #include "i_distributed_sink_input.h" @@ -46,6 +47,11 @@ public: bool HasDInputSinkProxy(); bool SetDInputSinkProxy(const sptr &remoteObject); void RegisterEventHandler(std::shared_ptr handler); + int32_t RestoreRegisterListenerAndCallback(); + void AddSimEventListenerToCache(sptr listener); + void RemoveSimEventListenerFromCache(sptr listener); + void AddSessionStateCbToCache(const sptr callback); + void RemoveSessionStateCbFromCache(); public: class SystemAbilityListener : public SystemAbilityStatusChangeStub { @@ -54,6 +60,12 @@ public: void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; }; +private: + std::mutex simEventListenerCacheMtx_; + std::set> simEventListenerCache_; + std::mutex sessionStateCbCacheMtx_; + sptr sessionStateCbCache_ = nullptr; + private: DInputSAManager() = default; ~DInputSAManager() = default; diff --git a/interfaces/ipc/include/distributed_input_client.h b/interfaces/ipc/include/distributed_input_client.h index 880fe43..4ab2627 100644 --- a/interfaces/ipc/include/distributed_input_client.h +++ b/interfaces/ipc/include/distributed_input_client.h @@ -115,7 +115,6 @@ public: void CheckSinkRegisterCallback(); void CheckSharingDhIdsCallback(); void CheckSinkScreenInfoCallback(); - int32_t RestoreRegisterListenerAndCallback(); public: class RegisterDInputCb : public OHOS::DistributedHardware::DistributedInput::RegisterDInputCallbackStub { @@ -191,8 +190,6 @@ private: sptr unregSimulationEventListener_ = nullptr; std::set> sharingDhIdListeners_; std::set> getSinkScreenInfosCallbacks_; - std::set> addSaListeners_; - sptr addSaCallback_ = nullptr; std::shared_ptr eventHandler_; @@ -223,8 +220,6 @@ private: std::mutex sharingDhIdsMtx_; // sharing local dhids std::set sharingDhIds_; - std::mutex addSaListenersMtx_; - std::mutex addSaCallbackMtx_; }; } // namespace DistributedInput } // namespace DistributedHardware diff --git a/interfaces/ipc/src/dinput_sa_manager.cpp b/interfaces/ipc/src/dinput_sa_manager.cpp index c7116d8..425b3eb 100644 --- a/interfaces/ipc/src/dinput_sa_manager.cpp +++ b/interfaces/ipc/src/dinput_sa_manager.cpp @@ -21,7 +21,6 @@ #include "constants_dinput.h" #include "dinput_errcode.h" #include "dinput_log.h" -#include "distributed_input_client.h" namespace OHOS { namespace DistributedHardware { @@ -80,10 +79,6 @@ void DInputSAManager::SystemAbilityListener::OnAddSystemAbility(int32_t systemAb DInputSAManager::GetInstance().eventHandler_->SendEvent(msgEvent, DINPUT_CLIENT_HANDLER_MSG_DELAY_TIME, AppExecFwk::EventQueue::Priority::IMMEDIATE); } - int32_t result = DistributedInputClient::GetInstance().RestoreRegisterListenerAndCallback(); - if (result != DH_SUCCESS) { - DHLOGE("source sa execute RestoreRegisterListenerAndCallback fail, result = %d", result); - } } if (systemAbilityId == DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID) { @@ -96,10 +91,6 @@ void DInputSAManager::SystemAbilityListener::OnAddSystemAbility(int32_t systemAb DInputSAManager::GetInstance().eventHandler_->SendEvent(msgEvent, DINPUT_CLIENT_HANDLER_MSG_DELAY_TIME, AppExecFwk::EventQueue::Priority::IMMEDIATE); } - int32_t result = DistributedInputClient::GetInstance().RestoreRegisterListenerAndCallback(); - if (result != DH_SUCCESS) { - DHLOGE("sink sa execute RestoreRegisterListenerAndCallback fail, result = %d", result); - } } DHLOGI("sa %d is added.", systemAbilityId); } @@ -284,6 +275,65 @@ bool DInputSAManager::SetDInputSinkProxy(const sptr &remoteObject } return true; } + +int32_t DInputSAManager::RestoreRegisterListenerAndCallback() +{ + DHLOGI("Restore RegisterPublisherListener"); + if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { + DHLOGE("RestoreRegisterSimulationEventListener proxy error, client fail"); + return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; + } + + int32_t result = DH_SUCCESS; + std::lock_guard lock(simEventListenerCacheMtx_); + for (const auto& listener : simEventListenerCache_) { + int32_t ret = DInputSAManager::GetInstance().dInputSourceProxy_->RegisterSimulationEventListener(listener); + if (ret != DH_SUCCESS) { + result = ret; + DHLOGE("SA execute RegisterSimulationEventListener fail, ret = %d", ret); + } + } + + DHLOGI("Restore RegisterSessionStateCb"); + if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { + DHLOGE("Restore RegisterSessionStateCb client fail."); + return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; + } + + { + std::lock_guard lock(sessionStateCbCacheMtx_); + int32_t ret = DInputSAManager::GetInstance().dInputSourceProxy_->RegisterSessionStateCb(sessionStateCbCache_); + if (ret != DH_SUCCESS) { + result = ret; + DHLOGE("SA execute RegisterSessionStateCb fail, ret = %d", ret); + } + return result; + } +} + +void DHFWKSAManager::AddSimEventListenerToCache(sptr listener) +{ + std::lock_guard simEventListenerLock(simEventListenerCacheMtx_); + simEventListenerCache_.insert(listener); +} + +void DHFWKSAManager::RemoveSimEventListenerFromCache(sptr listener) +{ + std::lock_guard simEventListenerLock(simEventListenerCacheMtx_); + simEventListenerCache_.erase(listener); +} + +void DHFWKSAManager::AddSessionStateCbToCache(const sptr callback) +{ + std::lock_guard sessionStateCbLock(sessionStateCbCacheMtx_); + sessionStateCbCache_ = callback; +} + +void DHFWKSAManager::() +{ + std::lock_guard sessionStateCbLock(sessionStateCbCacheMtx_); + sessionStateCbCache_ = nullptr; +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/ipc/src/distributed_input_client.cpp b/interfaces/ipc/src/distributed_input_client.cpp index 7102ff4..ec0c1af 100644 --- a/interfaces/ipc/src/distributed_input_client.cpp +++ b/interfaces/ipc/src/distributed_input_client.cpp @@ -28,6 +28,7 @@ #include "input_check_param.h" #include "softbus_bus_center.h" #include "white_list_util.h" +#include "dinput_sa_manager.h" namespace OHOS { namespace DistributedHardware { @@ -128,6 +129,10 @@ void DistributedInputClient::DInputClientEventHandler::ProcessEvent(const AppExe DHLOGI("DInputClientEventHandler ProcessEvent start eventId:%d.", eventId); if (eventId == DINPUT_CLIENT_CHECK_SOURCE_CALLBACK_REGISTER_MSG) { DistributedInputClient::GetInstance().CheckSourceRegisterCallback(); + int32_t result = DInputSAManager::GetInstance().RestoreRegisterListenerAndCallback(); + if (result != DH_SUCCESS) { + DHLOGE("source sa execute RestoreRegisterListenerAndCallback fail, result = %d", result); + } return; } @@ -581,10 +586,9 @@ int32_t DistributedInputClient::RegisterSimulationEventListener(sptrRegisterSimulationEventListener(listener); - std::lock_guard lock(addSaListenersMtx_); if (ret == DH_SUCCESS) { isSimulationEventCbReg = true; - addSaListeners_.insert(listener); + DInputSAManager::GetInstance().AddSimEventListenerToCache(listener); } else { isSimulationEventCbReg = false; regSimulationEventListener_ = listener; @@ -609,9 +613,7 @@ int32_t DistributedInputClient::UnregisterSimulationEventListener(sptr lock(addSaListenersMtx_); - addSaListeners_.erase(listener); + DInputSAManager::GetInstance().RemoveSimEventListenerFromCache(listener); return ret; } @@ -754,8 +756,7 @@ int32_t DistributedInputClient::RegisterSessionStateCb(sptr lock(addSaCallbackMtx_); - addSaCallback_ = (callback); + DInputSAManager::GetInstance().AddSessionStateCbToCache(listener); return DInputSAManager::GetInstance().dInputSourceProxy_->RegisterSessionStateCb(callback); } @@ -765,45 +766,9 @@ int32_t DistributedInputClient::UnregisterSessionStateCb() DHLOGE("DinputStart client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - std::lock_guard lock(addSaCallbackMtx_); - addSaCallback_ = nullptr; + DInputSAManager::GetInstance().RemoveSessionStateCbFromCache(listener); return DInputSAManager::GetInstance().dInputSourceProxy_->UnregisterSessionStateCb(); } - -int32_t DistributedInputClient::RestoreRegisterListenerAndCallback() -{ - DHLOGI("Restore RegisterPublisherListener"); - if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("RestoreRegisterSimulationEventListener proxy error, client fail"); - return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; - } - - int32_t result = DH_SUCCESS; - std::lock_guard lock(addSaListenersMtx_); - for (const auto& listener : addSaListeners_) { - int32_t ret = DInputSAManager::GetInstance().dInputSourceProxy_->RegisterSimulationEventListener(listener); - if (ret != DH_SUCCESS) { - result = ret; - DHLOGE("SA execute RegisterSimulationEventListener fail, ret = %d", ret); - } - } - - DHLOGI("Restore RegisterSessionStateCb"); - if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("Restore RegisterSessionStateCb client fail."); - return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; - } - - { - std::lock_guard lock(addSaCallbackMtx_); - int32_t ret = DInputSAManager::GetInstance().dInputSourceProxy_->RegisterSessionStateCb(addSaCallback_); - if (ret != DH_SUCCESS) { - result = ret; - DHLOGE("SA execute RegisterSessionStateCb fail, ret = %d", ret); - } - return result; - } -} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS