From 93c047e86662521e7f616dcabea13ffbe185c546 Mon Sep 17 00:00:00 2001 From: guoyi Date: Thu, 13 Mar 2025 23:08:35 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8Edatashare?= =?UTF-8?q?=E3=80=81accountsys=E7=9A=84=E6=97=B6=E5=BA=8F=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- services/service/BUILD.gn | 1 + .../service/include/device_manager_service.h | 3 + .../devicenamemgr/device_name_manager.h | 8 +- .../dm_datashare_common_event.h | 74 +++++++ .../service/src/device_manager_service.cpp | 29 ++- .../src/devicenamemgr/device_name_manager.cpp | 82 ++++++-- .../src/ipc/standard/ipc_server_stub.cpp | 3 +- .../dm_datashare_common_event.cpp | 183 ++++++++++++++++++ test/unittest/UTTest_device_name_manager.cpp | 4 +- 9 files changed, 360 insertions(+), 27 deletions(-) create mode 100644 services/service/include/publishcommonevent/dm_datashare_common_event.h create mode 100644 services/service/src/publishcommonevent/dm_datashare_common_event.cpp diff --git a/services/service/BUILD.gn b/services/service/BUILD.gn index b6f6dde31..99e1a8b46 100644 --- a/services/service/BUILD.gn +++ b/services/service/BUILD.gn @@ -206,6 +206,7 @@ if (defined(ohos_lite)) { "src/pinholder/pin_holder_session.cpp", "src/publishcommonevent/dm_account_common_event.cpp", "src/publishcommonevent/dm_data_share_common_event.cpp", + "src/publishcommonevent/dm_datashare_common_event.cpp", "src/publishcommonevent/dm_package_common_event.cpp", "src/publishcommonevent/dm_screen_common_event.cpp", "src/relationshipsyncmgr/dm_comm_tool.cpp", diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index d38ddefff..811b23063 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -33,6 +33,7 @@ #include "i_dm_service_impl_ext_resident.h" #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) #include "dm_account_common_event.h" +#include "dm_datashare_common_event.h" #include "dm_package_common_event.h" #include "dm_screen_common_event.h" #include "relationship_sync_mgr.h" @@ -332,6 +333,7 @@ private: void SubscribePublishCommonEvent(); void QueryDependsSwitchState(); #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI + void SubscribeDataShareCommonEvent(); #endif private: @@ -357,6 +359,7 @@ private: #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) std::shared_ptr publshCommonEventManager_; #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI + std::shared_ptr dataShareCommonEventManager_; #endif std::string localNetWorkId_ = ""; std::shared_ptr timer_; diff --git a/services/service/include/devicenamemgr/device_name_manager.h b/services/service/include/devicenamemgr/device_name_manager.h index 82894058f..534023d95 100644 --- a/services/service/include/devicenamemgr/device_name_manager.h +++ b/services/service/include/devicenamemgr/device_name_manager.h @@ -25,9 +25,12 @@ namespace DistributedHardware { class DeviceNameManager { DECLARE_SINGLE_INSTANCE_BASE(DeviceNameManager); public: - int32_t Init(); int32_t UnInit(); + void DataShareReady(); + void AccountSysReady(int32_t userId); + int32_t InitDeviceNameWhenSoftBusReady(); + int32_t GetLocalDisplayDeviceName(int32_t maxNamelength, std::string &displayName); int32_t InitDeviceNameWhenUserSwitch(int32_t curUserId, int32_t preUserId); int32_t InitDeviceNameWhenLogout(); @@ -38,6 +41,7 @@ public: private: DeviceNameManager() = default; ~DeviceNameManager() = default; + bool DependsIsReady(); void RegisterDeviceNameChangeMonitor(int32_t curUserId, int32_t preUserId); void UnRegisterDeviceNameChangeMonitor(int32_t userId); void InitDeviceName(int32_t userId); @@ -71,6 +75,8 @@ private: std::string localMarketName_ = ""; std::mutex monitorMapMtx_; std::map> monitorMap_; + std::atomic isDataShareReady_ = false; + std::atomic isAccountSysReady_ = false; }; } // DistributedHardware } // OHOS diff --git a/services/service/include/publishcommonevent/dm_datashare_common_event.h b/services/service/include/publishcommonevent/dm_datashare_common_event.h new file mode 100644 index 000000000..ac984fc82 --- /dev/null +++ b/services/service/include/publishcommonevent/dm_datashare_common_event.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2025 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 OHOS_DM_DATASAHRE_COMMON_EVENT_H +#define OHOS_DM_DATASAHRE_COMMON_EVENT_H + +#include "common_event_manager.h" +#include "system_ability_status_change_stub.h" + +namespace OHOS { +namespace DistributedHardware { +using OHOS::EventFwk::CommonEventData; +using OHOS::EventFwk::CommonEventSubscriber; +using OHOS::EventFwk::CommonEventSubscribeInfo; +using DataShareEventCallback = std::function; + +class DmDataShareEventSubscriber : public CommonEventSubscriber { +public: + DmDataShareEventSubscriber(const CommonEventSubscribeInfo &subscribeInfo, const DataShareEventCallback &callback, + const std::vector &eventNameVec) : CommonEventSubscriber(subscribeInfo), + eventNameVec_(eventNameVec), callback_(callback) {} + ~DmDataShareEventSubscriber() override = default; + std::vector GetSubscriberEventNameVec() const; + void OnReceiveEvent(const CommonEventData &data) override; + +private: + std::vector eventNameVec_; + DataShareEventCallback callback_; +}; + +class DmDataShareCommonEventManager { +public: + DmDataShareCommonEventManager() = default; + ~DmDataShareCommonEventManager(); + bool SubscribeDataShareCommonEvent(const std::vector &eventNameVec, + const DataShareEventCallback &callback); + bool UnsubscribeDataShareCommonEvent(); + +private: + std::vector eventNameVec_; + bool eventValidFlag_ = false; + std::mutex evenSubscriberMutex_; + std::shared_ptr subscriber_ = nullptr; + sptr statusChangeListener_ = nullptr; + int32_t counter_ = 0; + +private: + class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { + public: + explicit SystemAbilityStatusChangeListener(std::shared_ptr DataShareSubscriber) + : changeSubscriber_(DataShareSubscriber) {} + ~SystemAbilityStatusChangeListener() = default; + void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + + private: + std::shared_ptr changeSubscriber_; + }; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_DATASAHRE_COMMON_EVENT_H diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index b3b6aa7b5..f98b557bb 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -115,6 +115,7 @@ int32_t DeviceManagerService::InitSoftbusListener() SubscribePublishCommonEvent(); QueryDependsSwitchState(); #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI + SubscribeDataShareCommonEvent(); #endif LOGI("SoftbusListener init success."); if (!IsDMServiceAdapterResidentLoad()) { @@ -160,6 +161,23 @@ void DeviceManagerService::SubscribePublishCommonEvent() return; } #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI +void DeviceManagerService::SubscribeDataShareCommonEvent() +{ + LOGI("DeviceManagerServiceImpl::SubscribeDataShareCommonEvent"); + if (dataShareCommonEventManager_ == nullptr) { + dataShareCommonEventManager_ = std::make_shared(); + } + DataShareEventCallback callback = [=](const auto &arg1) { + if (arg1 == CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY) { + DeviceNameManager::GetInstance().DataShareReady(); + } + }; + std::vector commonEventVec; + commonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY); + if (dataShareCommonEventManager_->SubscribeDataShareCommonEvent(commonEventVec, callback)) { + LOGI("subscribe datashare common event success"); + } +} #endif #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) @@ -1881,10 +1899,13 @@ void DeviceManagerService::AccountCommonEventCallback(const std::string commonEv DeviceNameManager::GetInstance().InitDeviceNameWhenNickChange(); } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_STOPPED && IsPC()) { HandleUserStopEvent(beforeUserId); - } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED && IsPC()) { - HandleUserSwitched(); - if (IsDMServiceAdapterResidentLoad()) { - dmServiceImplExtResident_->AccountUserSwitched(currentUserId, MultipleUserConnector::GetOhosAccountId()); + } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) { + DeviceNameManager::GetInstance().AccountSysReady(beforeUserId); + if (IsPC()) { + HandleUserSwitched(); + if (IsDMServiceAdapterResidentLoad()) { + dmServiceImplExtResident_->AccountUserSwitched(currentUserId, MultipleUserConnector::GetOhosAccountId()); + } } } else { LOGE("Invalied account common event."); diff --git a/services/service/src/devicenamemgr/device_name_manager.cpp b/services/service/src/devicenamemgr/device_name_manager.cpp index 8c5bed99c..fe8ca8e3f 100644 --- a/services/service/src/devicenamemgr/device_name_manager.cpp +++ b/services/service/src/devicenamemgr/device_name_manager.cpp @@ -19,6 +19,8 @@ #include "multiple_user_connector.h" #include "system_ability_definition.h" #include "device_manager_service.h" +#include "dm_anonymous.h" +#include "dm_constants.h" #include "parameter.h" namespace OHOS { @@ -65,16 +67,49 @@ constexpr int32_t DEFAULT_USER_ID = -1; IMPLEMENT_SINGLE_INSTANCE(DeviceNameManager); -int32_t DeviceNameManager::Init() +void DeviceNameManager::DataShareReady() { - LOGI("DeviceNameManager In"); + LOGI("In"); + isDataShareReady_ = true; + if (DependsIsReady()) { + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + InitDeviceName(userId); + } +} + +void DeviceNameManager::AccountSysReady(int32_t userId) +{ + LOGI("In userId : %{public}d", userId); + isAccountSysReady_ = true; + if ((userId != -1) &&DependsIsReady()) { + InitDeviceName(userId); + } +} + +bool DeviceNameManager::DependsIsReady() +{ + if (!isDataShareReady_) { + LOGE("data share not ready"); + return false; + } + if (!isAccountSysReady_) { + LOGE("Account system not ready"); + return false; + } if (GetRemoteObj() == nullptr) { LOGE("dm sa not publish"); - return ERR_DM_POINT_NULL; + return false; + } + return true; +} + +int32_t DeviceNameManager::InitDeviceNameWhenSoftBusReady() +{ + LOGI("In"); + if (DependsIsReady()) { + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + InitDeviceName(userId); } - int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); - InitDeviceName(userId); - RegisterDeviceNameChangeMonitor(userId, DEFAULT_USER_ID); return DM_OK; } @@ -93,44 +128,51 @@ int32_t DeviceNameManager::UnInit() int32_t DeviceNameManager::InitDeviceNameWhenUserSwitch(int32_t curUserId, int32_t preUserId) { + isAccountSysReady_ = true; LOGI("In"); - if (GetRemoteObj() == nullptr) { - LOGE("dm sa not publish"); - return ERR_DM_POINT_NULL; + if (DependsIsReady()) { + InitDeviceName(curUserId); + RegisterDeviceNameChangeMonitor(curUserId, preUserId); } - InitDeviceName(curUserId); - RegisterDeviceNameChangeMonitor(curUserId, preUserId); return DM_OK; } int32_t DeviceNameManager::InitDeviceNameWhenLogout() { LOGI("In"); - int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); - InitDeviceName(userId); + if (DependsIsReady()) { + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + InitDeviceName(userId); + } return DM_OK; } int32_t DeviceNameManager::InitDeviceNameWhenLogin() { LOGI("In"); - int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); - InitDeviceName(userId); + if (DependsIsReady()) { + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + InitDeviceName(userId); + } return DM_OK; } int32_t DeviceNameManager::InitDeviceNameWhenNickChange() { LOGI("In"); - int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); - InitDeviceName(userId); + if (DependsIsReady()) { + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + InitDeviceName(userId); + } return DM_OK; } int32_t DeviceNameManager::InitDeviceNameWhenNameChange(int32_t userId) { LOGI("In"); - InitDeviceName(userId); + if (DependsIsReady()) { + InitDeviceName(userId); + } return DM_OK; } @@ -208,6 +250,10 @@ void DeviceNameManager::UnRegisterDeviceNameChangeMonitor(int32_t userId) void DeviceNameManager::InitDeviceName(int32_t userId) { LOGI("In userId:%{public}d", userId); + if (userId == -1) { + LOGI("userId:%{public}d is invalid", userId); + return; + } std::string userDefinedDeviceName = ""; GetUserDefinedDeviceName(userId, userDefinedDeviceName); if (!userDefinedDeviceName.empty()) { diff --git a/services/service/src/ipc/standard/ipc_server_stub.cpp b/services/service/src/ipc/standard/ipc_server_stub.cpp index 5ed3f75d6..ca4a35793 100644 --- a/services/service/src/ipc/standard/ipc_server_stub.cpp +++ b/services/service/src/ipc/standard/ipc_server_stub.cpp @@ -85,8 +85,7 @@ void IpcServerStub::OnAddSystemAbility(int32_t systemAbilityId, const std::strin return; } state_ = ServiceRunningState::STATE_RUNNING; - int32_t ret = DeviceNameManager::GetInstance().Init(); - LOGI("int device name ret:%{public}d", ret); + DeviceNameManager::GetInstance().InitDeviceNameWhenSoftBusReady(); return; } diff --git a/services/service/src/publishcommonevent/dm_datashare_common_event.cpp b/services/service/src/publishcommonevent/dm_datashare_common_event.cpp new file mode 100644 index 000000000..6c704bb9f --- /dev/null +++ b/services/service/src/publishcommonevent/dm_datashare_common_event.cpp @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2025 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 "dm_datashare_common_event.h" + +#include +#include + +#include "common_event_support.h" +#include "dm_anonymous.h" +#include "dm_log.h" +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) +#include "ffrt.h" +#endif +#include "iservice_registry.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace DistributedHardware { +using OHOS::EventFwk::MatchingSkills; +using OHOS::EventFwk::CommonEventManager; + +#if (defined(__LITEOS_M__) || defined(LITE_DEVICE)) +constexpr const char* DEAL_THREAD = "datashare_common_event"; +#endif +constexpr int32_t MAX_TRY_TIMES = 3; + +std::vector DmDataShareEventSubscriber::GetSubscriberEventNameVec() const +{ + return eventNameVec_; +} + +DmDataShareCommonEventManager::~DmDataShareCommonEventManager() +{ + DmDataShareCommonEventManager::UnsubscribeDataShareCommonEvent(); +} + +bool DmDataShareCommonEventManager::SubscribeDataShareCommonEvent(const std::vector &eventNameVec, + const DataShareEventCallback &callback) +{ + if (eventNameVec.empty() || callback == nullptr) { + LOGE("eventNameVec is empty or callback is nullptr."); + return false; + } + std::lock_guard locker(evenSubscriberMutex_); + if (eventValidFlag_) { + LOGE("failed to subscribe account commom eventName size: %{public}zu", eventNameVec.size()); + return false; + } + + MatchingSkills matchingSkills; + for (auto &item : eventNameVec) { + matchingSkills.AddEvent(item); + } + CommonEventSubscribeInfo subscriberInfo(matchingSkills); + subscriber_ = std::make_shared(subscriberInfo, callback, eventNameVec); + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + LOGE("samgrProxy is nullptr"); + subscriber_ = nullptr; + return false; + } + statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(subscriber_); + if (statusChangeListener_ == nullptr) { + LOGE("statusChangeListener_ is nullptr"); + subscriber_ = nullptr; + return false; + } + while (counter_ != MAX_TRY_TIMES) { + if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) { + LOGI("SubscribeAccountEvent success."); + counter_ = 0; + break; + } + if (++counter_ == MAX_TRY_TIMES) { + LOGI("SubscribeAccountEvent failed."); + } + sleep(1); + } + eventNameVec_ = eventNameVec; + eventValidFlag_ = true; + LOGI("success to subscribe account commom event name size: %{public}zu", eventNameVec.size()); + return true; +} + +bool DmDataShareCommonEventManager::UnsubscribeDataShareCommonEvent() +{ + std::lock_guard locker(evenSubscriberMutex_); + if (!eventValidFlag_) { + LOGE("failed to unsubscribe account commom event name size: %{public}zu because event is invalid.", + eventNameVec_.size()); + return false; + } + if (subscriber_ != nullptr) { + LOGI("start to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); + if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) { + LOGE("failed to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); + return false; + } + LOGI("success to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); + subscriber_ = nullptr; + } + if (statusChangeListener_ != nullptr) { + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + LOGE("samgrProxy is nullptr"); + return false; + } + int32_t ret = samgrProxy->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_); + if (ret != ERR_OK) { + LOGE("failed to unsubscribe system ability COMMON_EVENT_SERVICE_ID ret:%{public}d", ret); + return false; + } + statusChangeListener_ = nullptr; + } + + LOGI("success to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); + eventValidFlag_ = false; + return true; +} + +void DmDataShareEventSubscriber::OnReceiveEvent(const CommonEventData &data) +{ + std::string receiveEvent = data.GetWant().GetAction(); + bool validEvent = false; + + if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY) { + validEvent = true; + } + LOGI("Received datashare event: %{public}s", receiveEvent.c_str()); + if (!validEvent) { + LOGE("Invalied account type event."); + return; + } +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + ffrt::submit([=]() { callback_(receiveEvent); }); +#else + std::thread dealThread([=]() { callback_(receiveEvent); }); + int32_t ret = pthread_setname_np(dealThread.native_handle(), DEAL_THREAD); + if (ret != DM_OK) { + LOGE("dealThread setname failed."); + } + dealThread.detach(); +#endif +} + +void DmDataShareCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility( + int32_t systemAbilityId, const std::string& deviceId) +{ + LOGI("systemAbility is added with said: %{public}d.", systemAbilityId); + if (systemAbilityId != COMMON_EVENT_SERVICE_ID) { + return; + } + if (changeSubscriber_ == nullptr) { + LOGE("failed to subscribe account commom event because changeSubscriber_ is nullptr."); + return; + } + std::vector eventNameVec = changeSubscriber_->GetSubscriberEventNameVec(); + LOGI("start to subscribe account commom eventName: %{public}zu", eventNameVec.size()); + if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) { + LOGE("failed to subscribe account commom event: %{public}zu", eventNameVec.size()); + } +} + +void DmDataShareCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility( + int32_t systemAbilityId, const std::string& deviceId) +{ + LOGI("systemAbility is removed with said: %{public}d.", systemAbilityId); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_device_name_manager.cpp b/test/unittest/UTTest_device_name_manager.cpp index e7057f181..f549e952a 100644 --- a/test/unittest/UTTest_device_name_manager.cpp +++ b/test/unittest/UTTest_device_name_manager.cpp @@ -83,7 +83,7 @@ HWTEST_F(DeviceNameManagerTest, Init_001, testing::ext::TestSize.Level0) DoAll(SetArgReferee<1>(subffixName), Return(DataShare::E_OK))); EXPECT_CALL(*resultSet, Close()).Times(AtLeast(1)); - DeviceNameManager::GetInstance().Init(); + DeviceNameManager::GetInstance().InitDeviceNameWhenSoftBusReady(); DeviceNameManager::GetInstance().UnInit(); } @@ -119,7 +119,7 @@ HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenUserSwitch_001, testing::ext:: DoAll(SetArgReferee<1>(subffixName), Return(DataShare::E_OK))); EXPECT_CALL(*resultSet, Close()).Times(AtLeast(1)); - DeviceNameManager::GetInstance().Init(); + DeviceNameManager::GetInstance().InitDeviceNameWhenSoftBusReady(); DeviceNameManager::GetInstance().InitDeviceNameWhenUserSwitch(curUserId, preUserId); DeviceNameManager::GetInstance().UnInit(); } -- Gitee From 3ac6bf322c96bea3653528ecbcf94b6e266d29a6 Mon Sep 17 00:00:00 2001 From: guoyi Date: Thu, 13 Mar 2025 23:36:36 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- services/service/src/devicenamemgr/device_name_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/service/src/devicenamemgr/device_name_manager.cpp b/services/service/src/devicenamemgr/device_name_manager.cpp index fe8ca8e3f..0b3559f61 100644 --- a/services/service/src/devicenamemgr/device_name_manager.cpp +++ b/services/service/src/devicenamemgr/device_name_manager.cpp @@ -81,7 +81,7 @@ void DeviceNameManager::AccountSysReady(int32_t userId) { LOGI("In userId : %{public}d", userId); isAccountSysReady_ = true; - if ((userId != -1) &&DependsIsReady()) { + if ((userId != -1) && DependsIsReady()) { InitDeviceName(userId); } } -- Gitee From 4e9c8d3e70228051e78ba2fa4eafbe941dcead2c Mon Sep 17 00:00:00 2001 From: guoyi Date: Thu, 13 Mar 2025 23:52:15 +0800 Subject: [PATCH 3/6] update Signed-off-by: guoyi --- services/service/BUILD.gn | 1 - 1 file changed, 1 deletion(-) diff --git a/services/service/BUILD.gn b/services/service/BUILD.gn index 99e1a8b46..a35d7d53d 100644 --- a/services/service/BUILD.gn +++ b/services/service/BUILD.gn @@ -205,7 +205,6 @@ if (defined(ohos_lite)) { "src/pinholder/pin_holder.cpp", "src/pinholder/pin_holder_session.cpp", "src/publishcommonevent/dm_account_common_event.cpp", - "src/publishcommonevent/dm_data_share_common_event.cpp", "src/publishcommonevent/dm_datashare_common_event.cpp", "src/publishcommonevent/dm_package_common_event.cpp", "src/publishcommonevent/dm_screen_common_event.cpp", -- Gitee From 0d98ef363a833587c6628b5b94a45a30e95e551f Mon Sep 17 00:00:00 2001 From: guoyi Date: Thu, 13 Mar 2025 23:56:11 +0800 Subject: [PATCH 4/6] update Signed-off-by: guoyi --- .../dm_data_share_common_event.h | 74 -------- .../dm_data_share_common_event.cpp | 178 ------------------ 2 files changed, 252 deletions(-) delete mode 100644 services/service/include/publishcommonevent/dm_data_share_common_event.h delete mode 100644 services/service/src/publishcommonevent/dm_data_share_common_event.cpp diff --git a/services/service/include/publishcommonevent/dm_data_share_common_event.h b/services/service/include/publishcommonevent/dm_data_share_common_event.h deleted file mode 100644 index 7f0822e0a..000000000 --- a/services/service/include/publishcommonevent/dm_data_share_common_event.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2024 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 OHOS_DATA_SHARE_COMMON_EVENT_H -#define OHOS_DATA_SHARE_COMMON_EVENT_H - -#include "common_event_manager.h" -#include "system_ability_status_change_stub.h" - -namespace OHOS { -namespace DistributedHardware { -using OHOS::EventFwk::CommonEventData; -using OHOS::EventFwk::CommonEventSubscriber; -using OHOS::EventFwk::CommonEventSubscribeInfo; -using DataShareEventCallback = std::function; - -class DmDataShareEventSubscriber : public CommonEventSubscriber { -public: - DmDataShareEventSubscriber(const CommonEventSubscribeInfo &subscribeInfo, const DataShareEventCallback &callback, - const std::vector &eventNameVec) : CommonEventSubscriber(subscribeInfo), - eventNameVec_(eventNameVec), callback_(callback) {} - ~DmDataShareEventSubscriber() override = default; - std::vector GetSubscriberEventNameVec() const; - void OnReceiveEvent(const CommonEventData &data) override; - -private: - std::vector eventNameVec_; - DataShareEventCallback callback_; -}; - -class DmDataShareCommonEventManager { -public: - DmDataShareCommonEventManager() = default; - ~DmDataShareCommonEventManager(); - bool SubscribeDataShareCommonEvent(const std::vector &eventNameVec, - const DataShareEventCallback &callback); - bool UnsubscribeDataShareCommonEvent(); - -private: - std::vector eventNameVec_; - bool eventValidFlag_ = false; - std::mutex evenSubscriberMutex_; - std::shared_ptr subscriber_ = nullptr; - sptr statusChangeListener_ = nullptr; - int32_t counter_ = 0; - -private: - class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { - public: - explicit SystemAbilityStatusChangeListener(std::shared_ptr DataShareSubscriber) - : changeSubscriber_(DataShareSubscriber) {} - ~SystemAbilityStatusChangeListener() = default; - void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; - void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; - - private: - std::shared_ptr changeSubscriber_; - }; -}; -} // namespace DistributedHardware -} // namespace OHOS -#endif // OHOS_DATA_SHARE_COMMON_EVENT_H diff --git a/services/service/src/publishcommonevent/dm_data_share_common_event.cpp b/services/service/src/publishcommonevent/dm_data_share_common_event.cpp deleted file mode 100644 index cf22465c4..000000000 --- a/services/service/src/publishcommonevent/dm_data_share_common_event.cpp +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright (c) 2024 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 "dm_data_share_common_event.h" - -#include -#include - -#include "common_event_support.h" -#include "dm_anonymous.h" -#include "dm_error_type.h" -#include "dm_log.h" -#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) -#include "ffrt.h" -#endif -#include "iservice_registry.h" -#include "system_ability_definition.h" - -namespace OHOS { -namespace DistributedHardware { -using OHOS::EventFwk::MatchingSkills; -using OHOS::EventFwk::CommonEventManager; - -#if (defined(__LITEOS_M__) || defined(LITE_DEVICE)) -constexpr const char* DEAL_THREAD = "data_share"; -#endif -constexpr int32_t MAX_TRY_TIMES = 3; - -std::vector DmDataShareEventSubscriber::GetSubscriberEventNameVec() const -{ - return eventNameVec_; -} - -DmDataShareCommonEventManager::~DmDataShareCommonEventManager() -{ - DmDataShareCommonEventManager::UnsubscribeDataShareCommonEvent(); -} - -bool DmDataShareCommonEventManager::SubscribeDataShareCommonEvent(const std::vector &eventNameVec, - const DataShareEventCallback &callback) -{ - if (eventNameVec.empty() || callback == nullptr) { - LOGE("eventNameVec is empty or callback is nullptr."); - return false; - } - std::lock_guard locker(evenSubscriberMutex_); - if (eventValidFlag_) { - LOGE("failed to subscribe data share commom eventName size: %{public}zu", eventNameVec.size()); - return false; - } - - MatchingSkills matchingSkills; - for (auto &item : eventNameVec) { - matchingSkills.AddEvent(item); - } - CommonEventSubscribeInfo subscriberInfo(matchingSkills); - subscriber_ = std::make_shared(subscriberInfo, callback, eventNameVec); - auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (samgrProxy == nullptr) { - LOGE("samgrProxy is nullptr"); - subscriber_ = nullptr; - return false; - } - statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(subscriber_); - if (statusChangeListener_ == nullptr) { - LOGE("statusChangeListener_ is nullptr"); - subscriber_ = nullptr; - return false; - } - while (counter_ != MAX_TRY_TIMES) { - if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) { - LOGI("SubscribeDataShareEvent success."); - counter_ = 0; - break; - } - if (++counter_ == MAX_TRY_TIMES) { - LOGI("SubscribeDataShareEvent failed."); - } - sleep(1); - } - eventNameVec_ = eventNameVec; - eventValidFlag_ = true; - LOGI("success to subscribe data share commom event name size: %{public}zu", eventNameVec.size()); - return true; -} - -bool DmDataShareCommonEventManager::UnsubscribeDataShareCommonEvent() -{ - std::lock_guard locker(evenSubscriberMutex_); - if (!eventValidFlag_) { - LOGE("failed to unsubscribe data share commom event name size: %{public}zu because event is invalid.", - eventNameVec_.size()); - return false; - } - if (subscriber_ != nullptr) { - LOGI("start to unsubscribe data share commom event name size: %{public}zu", eventNameVec_.size()); - if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) { - LOGE("failed to unsubscribe data share commom event name size: %{public}zu", eventNameVec_.size()); - return false; - } - LOGI("success to unsubscribe data share commom event name size: %{public}zu", eventNameVec_.size()); - subscriber_ = nullptr; - } - if (statusChangeListener_ != nullptr) { - auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (samgrProxy == nullptr) { - LOGE("samgrProxy is nullptr"); - return false; - } - int32_t ret = samgrProxy->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_); - if (ret != ERR_OK) { - LOGE("failed to unsubscribe system ability COMMON_EVENT_SERVICE_ID ret:%{public}d", ret); - return false; - } - statusChangeListener_ = nullptr; - } - - LOGI("success to unsubscribe data share commom event name size: %{public}zu", eventNameVec_.size()); - eventValidFlag_ = false; - return true; -} - -void DmDataShareEventSubscriber::OnReceiveEvent(const CommonEventData &data) -{ - std::string receiveEvent = data.GetWant().GetAction(); - LOGI("Received data share event: %{public}s", receiveEvent.c_str()); - if (receiveEvent != EventFwk::CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY) { - return; - } -#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) - ffrt::submit([=]() { callback_();}); -#else - std::thread dealThread(callback_); - int32_t ret = pthread_setname_np(dealThread.native_handle(), DEAL_THREAD); - if (ret != DM_OK) { - LOGE("dealThread setname failed."); - } - dealThread.detach(); -#endif -} - -void DmDataShareCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility( - int32_t systemAbilityId, const std::string& deviceId) -{ - LOGI("systemAbility is added with said: %{public}d.", systemAbilityId); - if (systemAbilityId != COMMON_EVENT_SERVICE_ID) { - return; - } - if (changeSubscriber_ == nullptr) { - LOGE("failed to subscribe data share commom event because changeSubscriber_ is nullptr."); - return; - } - std::vector eventNameVec = changeSubscriber_->GetSubscriberEventNameVec(); - LOGI("start to subscribe data share commom eventName: %{public}zu", eventNameVec.size()); - if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) { - LOGE("failed to subscribe data share commom event: %{public}zu", eventNameVec.size()); - } -} - -void DmDataShareCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility( - int32_t systemAbilityId, const std::string& deviceId) -{ - LOGI("systemAbility is removed with said: %{public}d.", systemAbilityId); -} -} // namespace DistributedHardware -} \ No newline at end of file -- Gitee From dd24251c51fce878bf64180768cea5cb3a3129f7 Mon Sep 17 00:00:00 2001 From: guoyi Date: Fri, 14 Mar 2025 00:16:57 +0800 Subject: [PATCH 5/6] update Signed-off-by: guoyi --- services/service/BUILD.gn | 1 - services/service/include/devicenamemgr/account_boot_listener.h | 1 - services/service/include/ipc/standard/ipc_server_stub.h | 1 - 3 files changed, 3 deletions(-) diff --git a/services/service/BUILD.gn b/services/service/BUILD.gn index a35d7d53d..753be65d0 100644 --- a/services/service/BUILD.gn +++ b/services/service/BUILD.gn @@ -188,7 +188,6 @@ if (defined(ohos_lite)) { "src/advertise/advertise_manager.cpp", "src/device_manager_service.cpp", "src/device_manager_service_listener.cpp", - "src/devicenamemgr/account_boot_listener.cpp", "src/devicenamemgr/device_name_change_monitor.cpp", "src/devicenamemgr/device_name_manager.cpp", "src/devicenamemgr/local_device_name_mgr.cpp", diff --git a/services/service/include/devicenamemgr/account_boot_listener.h b/services/service/include/devicenamemgr/account_boot_listener.h index ce15c750e..a7aa62935 100644 --- a/services/service/include/devicenamemgr/account_boot_listener.h +++ b/services/service/include/devicenamemgr/account_boot_listener.h @@ -16,7 +16,6 @@ #ifndef OHOS_ACCOUNT_BOOT_LISTENER_H #define OHOS_ACCOUNT_BOOT_LISTENER_H -#include "dm_data_share_common_event.h" #include "local_device_name_mgr.h" namespace OHOS { namespace DistributedHardware { diff --git a/services/service/include/ipc/standard/ipc_server_stub.h b/services/service/include/ipc/standard/ipc_server_stub.h index 5eb015eee..bab61a66b 100644 --- a/services/service/include/ipc/standard/ipc_server_stub.h +++ b/services/service/include/ipc/standard/ipc_server_stub.h @@ -20,7 +20,6 @@ #include "ipc_remote_broker.h" #include "system_ability.h" -#include "account_boot_listener.h" #include "dm_single_instance.h" namespace OHOS { -- Gitee From 95c75d7c36836d00822d4642520268dda9ddf9a6 Mon Sep 17 00:00:00 2001 From: guoyi Date: Fri, 14 Mar 2025 00:52:23 +0800 Subject: [PATCH 6/6] update Signed-off-by: guoyi --- .../service/include/ipc/standard/ipc_server_stub.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/services/service/include/ipc/standard/ipc_server_stub.h b/services/service/include/ipc/standard/ipc_server_stub.h index bab61a66b..461c820ee 100644 --- a/services/service/include/ipc/standard/ipc_server_stub.h +++ b/services/service/include/ipc/standard/ipc_server_stub.h @@ -16,10 +16,21 @@ #ifndef OHOS_DM_IPC_SERVER_STUB_H #define OHOS_DM_IPC_SERVER_STUB_H +#include +#include #include +#include +#include +#include +#include + #include "ipc_remote_broker.h" +#include "ipc_req.h" +#include "ipc_rsp.h" +#include "iremote_stub.h" #include "system_ability.h" +#include "dm_device_info.h" #include "dm_single_instance.h" namespace OHOS { -- Gitee