diff --git a/services/service/BUILD.gn b/services/service/BUILD.gn index b6f6dde31e0a7c81f6e70b33786dbf49ab88c003..753be65d03053f33804fa03e3f3c18197a0dca55 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", @@ -205,7 +204,7 @@ 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", "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 d38ddefffe7f9f2627a45514ca2b164d3f91d179..811b23063cbaa947df56262bfcd69eb3c97967f7 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/account_boot_listener.h b/services/service/include/devicenamemgr/account_boot_listener.h index ce15c750e0c6477b29354a42522a7446b288adb4..a7aa629357932cb61d90e2441c9a5a0636f00f33 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/devicenamemgr/device_name_manager.h b/services/service/include/devicenamemgr/device_name_manager.h index 82894058f6bfcc083ad5c4d8e11a3a15ea7e048e..534023d95d7aaa94580f2ea3810d48adf6107358 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/ipc/standard/ipc_server_stub.h b/services/service/include/ipc/standard/ipc_server_stub.h index 5eb015eeeb9cbff6c6f03955499cad341ff8d83b..461c820ee8469d36b3b59402a036e0cc19ea9670 100644 --- a/services/service/include/ipc/standard/ipc_server_stub.h +++ b/services/service/include/ipc/standard/ipc_server_stub.h @@ -16,11 +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 "account_boot_listener.h" +#include "dm_device_info.h" #include "dm_single_instance.h" namespace OHOS { diff --git a/services/service/include/publishcommonevent/dm_data_share_common_event.h b/services/service/include/publishcommonevent/dm_datashare_common_event.h similarity index 92% rename from services/service/include/publishcommonevent/dm_data_share_common_event.h rename to services/service/include/publishcommonevent/dm_datashare_common_event.h index 7f0822e0ae298d3bf9ae20c38ad41b1ade794483..ac984fc82254c8deff52ee3568bff44c0dce5cb8 100644 --- a/services/service/include/publishcommonevent/dm_data_share_common_event.h +++ b/services/service/include/publishcommonevent/dm_datashare_common_event.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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 @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef OHOS_DATA_SHARE_COMMON_EVENT_H -#define OHOS_DATA_SHARE_COMMON_EVENT_H +#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" @@ -24,7 +24,7 @@ namespace DistributedHardware { using OHOS::EventFwk::CommonEventData; using OHOS::EventFwk::CommonEventSubscriber; using OHOS::EventFwk::CommonEventSubscribeInfo; -using DataShareEventCallback = std::function; +using DataShareEventCallback = std::function; class DmDataShareEventSubscriber : public CommonEventSubscriber { public: @@ -71,4 +71,4 @@ private: }; } // namespace DistributedHardware } // namespace OHOS -#endif // OHOS_DATA_SHARE_COMMON_EVENT_H +#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 b3b6aa7b5a528ad24617008c1605acdda0ab3c8c..f98b557bb288c5142f2039c4c75f448d52463590 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 8c5bed99c3fc311a2df3117f5af28477c100026b..0b3559f61d4a40e837b897153dbf4a9e8e9bc652 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 5ed3f75d68946407e834b3906bfaa76ec8be710f..ca4a35793380cf74add3c26373dd24b1eb532227 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_data_share_common_event.cpp b/services/service/src/publishcommonevent/dm_datashare_common_event.cpp similarity index 76% rename from services/service/src/publishcommonevent/dm_data_share_common_event.cpp rename to services/service/src/publishcommonevent/dm_datashare_common_event.cpp index cf22465c4bae73b2546d5988c1f2d73e0ab7d627..6c704bb9f9bb88b6eb6bebd2e36af3ffd42a9636 100644 --- a/services/service/src/publishcommonevent/dm_data_share_common_event.cpp +++ b/services/service/src/publishcommonevent/dm_datashare_common_event.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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 @@ -13,14 +13,13 @@ * limitations under the License. */ -#include "dm_data_share_common_event.h" +#include "dm_datashare_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" @@ -34,7 +33,7 @@ using OHOS::EventFwk::MatchingSkills; using OHOS::EventFwk::CommonEventManager; #if (defined(__LITEOS_M__) || defined(LITE_DEVICE)) -constexpr const char* DEAL_THREAD = "data_share"; +constexpr const char* DEAL_THREAD = "datashare_common_event"; #endif constexpr int32_t MAX_TRY_TIMES = 3; @@ -57,7 +56,7 @@ bool DmDataShareCommonEventManager::SubscribeDataShareCommonEvent(const std::vec } std::lock_guard locker(evenSubscriberMutex_); if (eventValidFlag_) { - LOGE("failed to subscribe data share commom eventName size: %{public}zu", eventNameVec.size()); + LOGE("failed to subscribe account commom eventName size: %{public}zu", eventNameVec.size()); return false; } @@ -81,18 +80,18 @@ bool DmDataShareCommonEventManager::SubscribeDataShareCommonEvent(const std::vec } while (counter_ != MAX_TRY_TIMES) { if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) { - LOGI("SubscribeDataShareEvent success."); + LOGI("SubscribeAccountEvent success."); counter_ = 0; break; } if (++counter_ == MAX_TRY_TIMES) { - LOGI("SubscribeDataShareEvent failed."); + LOGI("SubscribeAccountEvent failed."); } sleep(1); } eventNameVec_ = eventNameVec; eventValidFlag_ = true; - LOGI("success to subscribe data share commom event name size: %{public}zu", eventNameVec.size()); + LOGI("success to subscribe account commom event name size: %{public}zu", eventNameVec.size()); return true; } @@ -100,17 +99,17 @@ 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.", + 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 data share commom event name size: %{public}zu", eventNameVec_.size()); + LOGI("start to unsubscribe account 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()); + LOGE("failed to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); return false; } - LOGI("success to unsubscribe data share commom event name size: %{public}zu", eventNameVec_.size()); + LOGI("success to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); subscriber_ = nullptr; } if (statusChangeListener_ != nullptr) { @@ -127,7 +126,7 @@ bool DmDataShareCommonEventManager::UnsubscribeDataShareCommonEvent() statusChangeListener_ = nullptr; } - LOGI("success to unsubscribe data share commom event name size: %{public}zu", eventNameVec_.size()); + LOGI("success to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); eventValidFlag_ = false; return true; } @@ -135,14 +134,20 @@ bool DmDataShareCommonEventManager::UnsubscribeDataShareCommonEvent() 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) { + 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_();}); + ffrt::submit([=]() { callback_(receiveEvent); }); #else - std::thread dealThread(callback_); + std::thread dealThread([=]() { callback_(receiveEvent); }); int32_t ret = pthread_setname_np(dealThread.native_handle(), DEAL_THREAD); if (ret != DM_OK) { LOGE("dealThread setname failed."); @@ -159,13 +164,13 @@ void DmDataShareCommonEventManager::SystemAbilityStatusChangeListener::OnAddSyst return; } if (changeSubscriber_ == nullptr) { - LOGE("failed to subscribe data share commom event because changeSubscriber_ is nullptr."); + LOGE("failed to subscribe account commom event because changeSubscriber_ is nullptr."); return; } std::vector eventNameVec = changeSubscriber_->GetSubscriberEventNameVec(); - LOGI("start to subscribe data share commom eventName: %{public}zu", eventNameVec.size()); + LOGI("start to subscribe account commom eventName: %{public}zu", eventNameVec.size()); if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) { - LOGE("failed to subscribe data share commom event: %{public}zu", eventNameVec.size()); + LOGE("failed to subscribe account commom event: %{public}zu", eventNameVec.size()); } } @@ -175,4 +180,4 @@ void DmDataShareCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveS LOGI("systemAbility is removed with said: %{public}d.", systemAbilityId); } } // namespace DistributedHardware -} \ No newline at end of file +} // namespace OHOS diff --git a/test/unittest/UTTest_device_name_manager.cpp b/test/unittest/UTTest_device_name_manager.cpp index e7057f181134c553268dd51d4497e1ebeaaee2d6..f549e952a76c00d1e13072adcbf440394c3a4e90 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(); }