From 837f4cddadae7048615ac5535d8b63b09f72cceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B7=8D?= Date: Thu, 26 Sep 2024 21:23:22 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=96=E9=83=A8?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李巍 --- .../implementation/src/dependency/softbus/softbus_session.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/implementation/src/dependency/softbus/softbus_session.cpp b/services/implementation/src/dependency/softbus/softbus_session.cpp index 73efad2e5..7e7a72d26 100644 --- a/services/implementation/src/dependency/softbus/softbus_session.cpp +++ b/services/implementation/src/dependency/softbus/softbus_session.cpp @@ -27,6 +27,7 @@ namespace OHOS { namespace DistributedHardware { std::shared_ptr SoftbusSession::sessionCallback_ = nullptr; constexpr const char* DM_HITRACE_AUTH_TO_OPPEN_SESSION = "DM_HITRACE_AUTH_TO_OPPEN_SESSION"; +constexpr int32_t DATA_LEN = 65535; static void OnShutdown(int32_t socket, ShutdownReason reason) { @@ -172,7 +173,7 @@ void SoftbusSession::OnSessionClosed(int sessionId) void SoftbusSession::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen) { - if (sessionId < 0 || data == nullptr || dataLen <= 0) { + if (sessionId < 0 || data == nullptr || dataLen <= 0 || dataLen > DATA_LEN) { LOGI("[SOFTBUS]fail to receive data from softbus with sessionId: %{public}d, dataLen: %{public}d.", sessionId, dataLen); return; -- Gitee From a56e00c255d47f7273069f89faa014f286ac0d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B7=8D?= Date: Thu, 10 Oct 2024 15:48:09 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0DM=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E5=92=8C=E7=9B=91=E5=90=AC=E8=AE=BE=E5=A4=87=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李巍 --- bundle.json | 1 + sa_profile/device_manager.cfg | 1 + services/service/BUILD.gn | 7 + .../devicenamemgr/account_boot_listener.h | 40 +++ .../devicenamemgr/local_device_name_mgr.h | 55 ++++ .../settings_data_event_monitor.h | 43 +++ .../include/ipc/standard/ipc_server_stub.h | 5 +- .../devicenamemgr/account_boot_listener.cpp | 99 +++++++ .../devicenamemgr/local_device_name_mgr.cpp | 259 ++++++++++++++++++ .../settings_data_event_monitor.cpp | 50 ++++ .../src/ipc/standard/ipc_server_stub.cpp | 12 +- .../ipcserverstub_fuzzer/BUILD.gn | 2 + test/unittest/BUILD.gn | 10 + 13 files changed, 578 insertions(+), 6 deletions(-) create mode 100644 services/service/include/devicenamemgr/account_boot_listener.h create mode 100644 services/service/include/devicenamemgr/local_device_name_mgr.h create mode 100644 services/service/include/devicenamemgr/settings_data_event_monitor.h create mode 100644 services/service/src/devicenamemgr/account_boot_listener.cpp create mode 100644 services/service/src/devicenamemgr/local_device_name_mgr.cpp create mode 100644 services/service/src/devicenamemgr/settings_data_event_monitor.cpp diff --git a/bundle.json b/bundle.json index 8e193b87c..014692de8 100644 --- a/bundle.json +++ b/bundle.json @@ -36,6 +36,7 @@ "cJSON", "c_utils", "common_event_service", + "data_share", "device_auth", "device_info_manager", "dsoftbus", diff --git a/sa_profile/device_manager.cfg b/sa_profile/device_manager.cfg index 0550cd7b9..2d24a868d 100644 --- a/sa_profile/device_manager.cfg +++ b/sa_profile/device_manager.cfg @@ -26,6 +26,7 @@ "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", "ohos.permission.ENABLE_DISTRIBUTED_HARDWARE", "ohos.permission.MANAGE_LOCAL_ACCOUNTS", + "ohos.permission.MANAGE_SECURE_SETTINGS", "ohos.permission.ACCESS_BLUETOOTH", "ohos.permission.MANAGE_BLUETOOTH", "ohos.permission.MANAGE_SETTINGS", diff --git a/services/service/BUILD.gn b/services/service/BUILD.gn index 60a6c439a..b05125f26 100644 --- a/services/service/BUILD.gn +++ b/services/service/BUILD.gn @@ -119,6 +119,7 @@ if (defined(ohos_lite)) { include_dirs = [ "include", "include/advertise", + "include/devicenamemgr", "include/discovery", "include/ipc", "include/ipc/standard", @@ -163,6 +164,9 @@ 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/local_device_name_mgr.cpp", + "src/devicenamemgr/settings_data_event_monitor.cpp", "src/discovery/discovery_filter.cpp", "src/discovery/discovery_manager.cpp", "src/ipc/standard/ipc_cmd_parser.cpp", @@ -204,6 +208,8 @@ if (defined(ohos_lite)) { "c_utils:utils", "common_event_service:cesfwk_core", "common_event_service:cesfwk_innerkits", + "data_share:datashare_common", + "data_share:datashare_consumer", "device_info_manager:distributed_device_profile_common", "device_info_manager:distributed_device_profile_sdk", "dsoftbus:softbus_client", @@ -217,6 +223,7 @@ if (defined(ohos_lite)) { "ipc:ipc_single", "openssl:libcrypto_shared", "os_account:libaccountkits", + "os_account:os_account_innerkits", "safwk:system_ability_fwk", "samgr:samgr_proxy", ] diff --git a/services/service/include/devicenamemgr/account_boot_listener.h b/services/service/include/devicenamemgr/account_boot_listener.h new file mode 100644 index 000000000..d6e18ec38 --- /dev/null +++ b/services/service/include/devicenamemgr/account_boot_listener.h @@ -0,0 +1,40 @@ +/* + * 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_ACCOUNT_BOOT_LISTENER_H +#define OHOS_ACCOUNT_BOOT_LISTENER_H +#include + +#include "local_device_name_mgr.h" +namespace OHOS { +namespace DistributedHardware { +class AccountBootListener { +public: + AccountBootListener(); + ~AccountBootListener(); + void RegisterAccountBootCb(); + void DoAccountBootProc(); +private: + /** + * @brief flag for is registered callback for account boot event + * true: has register the callback + * false: NOT register the callback + */ + std::atomic isRegAccountBootCb_; + std::shared_ptr localDeviceMgr_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_ACCOUNT_BOOT_LISTENER_H \ No newline at end of file diff --git a/services/service/include/devicenamemgr/local_device_name_mgr.h b/services/service/include/devicenamemgr/local_device_name_mgr.h new file mode 100644 index 000000000..28630fc4a --- /dev/null +++ b/services/service/include/devicenamemgr/local_device_name_mgr.h @@ -0,0 +1,55 @@ +/* + * 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_DEVICE_NAME_MGR_H +#define OHOS_DEVICE_NAME_MGR_H + +#include +#include +#include + +#include "datashare_helper.h" + +namespace OHOS { +namespace DistributedHardware { +class LocalDeviceNameMgr : public std::enable_shared_from_this { +public: + LocalDeviceNameMgr(); + virtual ~LocalDeviceNameMgr(); + int32_t QueryLocalDeviceName(); + void RegisterDeviceNameChangeCb(); + int32_t QueryLocalDisplayName(); + void RegisterDisplayNameChangeCb(); + +private: + std::shared_ptr GetDataShareHelper(); + int32_t GetDeviceNameFromDataShareHelper(std::shared_ptr dataShareHelper, + std::shared_ptr uri, const char *key, std::string &deviceName); + int32_t GetDefaultDeviceName(std::shared_ptr dataShareHelper, + std::string &deviceName); + int32_t GetUserDefinedDeviceName(std::shared_ptr dataShareHelper, + std::string &deviceName); + int32_t GetDisplayDeviceName(std::shared_ptr dataShareHelper, + std::string &deviceName); + int32_t GetActiveOsAccountIds(); +private: + std::mutex devNameMtx_; + std::string localDeviceName_; + std::string localDisplayName_; +}; +} // DistributedHardware +} // OHOS + +#endif // OHOS_DEVICE_NAME_MGR_H \ No newline at end of file diff --git a/services/service/include/devicenamemgr/settings_data_event_monitor.h b/services/service/include/devicenamemgr/settings_data_event_monitor.h new file mode 100644 index 000000000..b1be98102 --- /dev/null +++ b/services/service/include/devicenamemgr/settings_data_event_monitor.h @@ -0,0 +1,43 @@ +/* + * 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_SETTINGS_DATA_EVENT_MONITOR_H +#define OHOS_SETTINGS_DATA_EVENT_MONITOR_H + +#include "data_ability_observer_stub.h" +#include + +#include "local_device_name_mgr.h" + +namespace OHOS { +namespace DistributedHardware { +class LocalDeviceNameMgr; +enum class SettingsDataMonitorType : int32_t { + USER_DEFINED_DEVICE_NAME_MONITOR = 0, + DISPLAY_DEVICE_NAME_MONITOR = 1 +}; + +class SettingsDataEventMonitor : public AAFwk::DataAbilityObserverStub { +public: + SettingsDataEventMonitor(std::shared_ptr localDeviceNameMgr, + SettingsDataMonitorType monitorType); + void OnChange() override; +private: + std::weak_ptr localDeviceNameMgrWPtr_; + SettingsDataMonitorType monitorType_; +}; +} // DistributedHardware +} // OHOS +#endif // OHOS_SETTINGS_DATA_EVENT_MONITOR_H \ No newline at end of file diff --git a/services/service/include/ipc/standard/ipc_server_stub.h b/services/service/include/ipc/standard/ipc_server_stub.h index 77a83b211..6c3f819f2 100644 --- a/services/service/include/ipc/standard/ipc_server_stub.h +++ b/services/service/include/ipc/standard/ipc_server_stub.h @@ -27,9 +27,11 @@ #include "ipc_req.h" #include "ipc_rsp.h" #include "iremote_stub.h" -#include "dm_single_instance.h" #include "system_ability.h" +#include "account_boot_listener.h" +#include "dm_single_instance.h" + namespace OHOS { namespace DistributedHardware { enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; @@ -160,6 +162,7 @@ private: mutable std::mutex listenerLock_; std::map> appRecipient_; std::map> dmListener_; + std::shared_ptr accountBootListener_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/devicenamemgr/account_boot_listener.cpp b/services/service/src/devicenamemgr/account_boot_listener.cpp new file mode 100644 index 000000000..91979bc6f --- /dev/null +++ b/services/service/src/devicenamemgr/account_boot_listener.cpp @@ -0,0 +1,99 @@ +/* + * 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 "account_boot_listener.h" + +#include + +#include "parameter.h" + +#include "dm_log.h" +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) +#include "dm_thread_manager.h" +#endif + +namespace OHOS { +namespace DistributedHardware { +namespace { + const char * const BOOTEVENT_ACCOUNT_READY = "bootevent.account.ready"; + const char * const ACCOUNT_BOOT_EVENT = "account_boot_event"; +} + +static void AccountBootCb(const char *key, const char *value, void *context) +{ + if (strcmp(key, BOOTEVENT_ACCOUNT_READY) != 0 || strcmp(value, "true") != 0) { + return; + } + AccountBootListener *accountBootListener = static_cast(context); + + if (accountBootListener == nullptr) { + LOGE("accountBootListener is null"); + return; + } + LOGI("Trigger"); +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + ThreadManager::GetInstance().Submit(ACCOUNT_BOOT_EVENT, [=]() { + accountBootListener->DoAccountBootProc(); + }); +#else + std::thread dealThread([=]() { + accountBootListener->DoAccountBootProc(); + }); + int32_t ret = pthread_setname_np(dealThread.native_handle(), ACCOUNT_BOOT_EVENT); + if (ret != DM_OK) { + LOGE("dealThread setname failed."); + } + dealThread.detach(); +#endif +} + +AccountBootListener::AccountBootListener() : isRegAccountBootCb_(false), + localDeviceMgr_(std::make_shared()) +{ + LOGI("Ctor AccountBootListener"); +} + +AccountBootListener::~AccountBootListener() +{ + LOGI("Dtor AccountBootListener"); +} + +void AccountBootListener::RegisterAccountBootCb() +{ + if (isRegAccountBootCb_) { + return; + } + LOGI("start"); + int32_t ret = WatchParameter(BOOTEVENT_ACCOUNT_READY, AccountBootCb, this); + if (ret != 0) { + LOGE("watch account boot event fail"); + } + isRegAccountBootCb_ = true; +} + +void AccountBootListener::DoAccountBootProc() +{ + LOGI("start"); + if (localDeviceMgr_ == nullptr) { + LOGE("localDeviceMgr_ is null"); + return; + } + localDeviceMgr_->RegisterDeviceNameChangeCb(); + localDeviceMgr_->RegisterDisplayNameChangeCb(); + localDeviceMgr_->QueryLocalDeviceName(); + localDeviceMgr_->QueryLocalDisplayName(); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/service/src/devicenamemgr/local_device_name_mgr.cpp b/services/service/src/devicenamemgr/local_device_name_mgr.cpp new file mode 100644 index 000000000..58b80f376 --- /dev/null +++ b/services/service/src/devicenamemgr/local_device_name_mgr.cpp @@ -0,0 +1,259 @@ +/* + * 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 "local_device_name_mgr.h" + +#include "data_ability_observer_stub.h" +#include "datashare_helper.h" +#include "datashare_predicates.h" +#include "datashare_result_set.h" +#include "ohos_account_kits.h" +#include "os_account_manager.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "uri.h" + +#include "dm_constants.h" +#include "dm_log.h" +#include "settings_data_event_monitor.h" + +namespace OHOS { +namespace DistributedHardware { +namespace { + const std::string SETTINGS_DATA_BASE_URI = + "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true"; + const std::string SETTINGS_DATA_SECURE_URI = + "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_"; + constexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility"; + constexpr const char *SETTINGS_DATA_FIELD_KEYWORD = "KEYWORD"; + constexpr const char *SETTINGS_DATA_FIELD_VALUE = "VALUE"; + constexpr const char *PREDICATES_STRING = "settings.general.device_name"; + constexpr const char *USER_DEFINED_STRING = "settings.general.user_defined_device_name"; + constexpr const char *DISPLAY_DEVICE_NAME_STRING = "settings.general.display_device_name"; +} +LocalDeviceNameMgr::LocalDeviceNameMgr() : localDeviceName_(""), localDisplayName_("") +{ + LOGI("Ctor LocalDeviceNameMgr"); +} + +LocalDeviceNameMgr::~LocalDeviceNameMgr() +{ + std::lock_guard lock(devNameMtx_); + localDeviceName_ = ""; + localDisplayName_ = ""; + LOGI("Dtor LocalDeviceNameMgr"); +} + +std::shared_ptr LocalDeviceNameMgr::GetDataShareHelper() +{ + sptr saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saManager == nullptr) { + LOGE("saManager NULL"); + return nullptr; + } + + sptr remoteObject = saManager->GetSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); + if (remoteObject == nullptr) { + LOGE("remoteObject NULL"); + return nullptr; + } + return DataShare::DataShareHelper::Creator(remoteObject, SETTINGS_DATA_BASE_URI, SETTINGS_DATA_EXT_URI); +} + +int32_t LocalDeviceNameMgr::GetDeviceNameFromDataShareHelper( + std::shared_ptr dataShareHelper, std::shared_ptr uri, + const char *key, std::string &deviceName) +{ + int32_t numRows = 0; + std::string val; + std::vector columns; + columns.emplace_back(SETTINGS_DATA_FIELD_VALUE); + DataShare::DataSharePredicates predicates; + predicates.EqualTo(SETTINGS_DATA_FIELD_KEYWORD, key); + + auto resultSet = dataShareHelper->Query(*uri, predicates, columns); + if (resultSet == nullptr) { + LOGE("query fail."); + return ERR_DM_FAILED; + } + resultSet->GetRowCount(numRows); + if (numRows <= 0) { + LOGE("row zero."); + resultSet->Close(); + return ERR_DM_FAILED; + } + + int columnIndex; + resultSet->GoToFirstRow(); + resultSet->GetColumnIndex(SETTINGS_DATA_FIELD_VALUE, columnIndex); + if (resultSet->GetString(columnIndex, val) != DM_OK) { + LOGE("GetString val fail"); + resultSet->Close(); + return ERR_DM_FAILED; + } + deviceName = val; + LOGI("deviceName=%{public}s.", deviceName.c_str()); + resultSet->Close(); + return DM_OK; +} + +int32_t LocalDeviceNameMgr::GetDefaultDeviceName(std::shared_ptr dataShareHelper, + std::string &deviceName) +{ + std::shared_ptr uri = std::make_shared(SETTINGS_DATA_BASE_URI + "&key=" + PREDICATES_STRING); + LOGI("get default deviceName"); + return GetDeviceNameFromDataShareHelper(dataShareHelper, uri, PREDICATES_STRING, deviceName); +} + +int32_t LocalDeviceNameMgr::GetUserDefinedDeviceName(std::shared_ptr dataShareHelper, + std::string &deviceName) +{ + int32_t osAccountId = GetActiveOsAccountIds(); + if (osAccountId == ERR_DM_FAILED) { + return ERR_DM_FAILED; + } + std::string accountIdStr = std::to_string(osAccountId); + std::shared_ptr uri = std::make_shared(SETTINGS_DATA_SECURE_URI + accountIdStr + "?Proxy=true&key=" + + USER_DEFINED_STRING); + LOGI("get user defined deviceName, accountId=%{public}s", accountIdStr.c_str()); + return GetDeviceNameFromDataShareHelper(dataShareHelper, uri, USER_DEFINED_STRING, deviceName); +} + +int32_t LocalDeviceNameMgr::GetDisplayDeviceName(std::shared_ptr dataShareHelper, + std::string &deviceName) +{ + int32_t osAccountId = GetActiveOsAccountIds(); + if (osAccountId == ERR_DM_FAILED) { + return ERR_DM_FAILED; + } + std::string accountIdStr = std::to_string(osAccountId); + std::shared_ptr uri = std::make_shared(SETTINGS_DATA_SECURE_URI + accountIdStr + "?Proxy=true&key=" + + DISPLAY_DEVICE_NAME_STRING); + LOGI("get user defined deviceName, accountId=%{public}s", accountIdStr.c_str()); + return GetDeviceNameFromDataShareHelper(dataShareHelper, uri, DISPLAY_DEVICE_NAME_STRING, deviceName); +} + +int32_t LocalDeviceNameMgr::GetActiveOsAccountIds() +{ + std::vector accountId; + int32_t ret = OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(accountId); + if (ret != DM_OK || accountId.empty()) { + LOGE("QueryActiveOsAccountIds failed"); + return ERR_DM_FAILED; + } + LOGI("account id=%{public}d", accountId[0]); + return accountId[0]; +} + +int32_t LocalDeviceNameMgr::QueryLocalDeviceName() +{ + LOGI("start"); + auto dataShareHelper = GetDataShareHelper(); + if (dataShareHelper == nullptr) { + LOGE("dataShareHelper is null"); + return ERR_DM_FAILED; + } + std::string localDeviceName = ""; + int32_t ret = GetUserDefinedDeviceName(dataShareHelper, localDeviceName); + if (ret == DM_OK && !localDeviceName.empty()) { + std::lock_guard lock(devNameMtx_); + localDeviceName_ = localDeviceName; + dataShareHelper->Release(); + LOGI("get user defined deviceName=%{public}s", localDeviceName.c_str()); + return DM_OK; + } + ret = GetDefaultDeviceName(dataShareHelper, localDeviceName); + LOGI("get default deviceName=%{public}s", localDeviceName.c_str()); + dataShareHelper->Release(); + return DM_OK; +} + +void LocalDeviceNameMgr::RegisterDeviceNameChangeCb() +{ + LOGI("start"); + auto dataShareHelper = GetDataShareHelper(); + if (dataShareHelper == nullptr) { + LOGE("dataShareHelper is null"); + return; + } + + auto uri = std::make_shared(SETTINGS_DATA_BASE_URI + "&key=" + PREDICATES_STRING); + sptr settingDataObserver = + sptr(new SettingsDataEventMonitor(shared_from_this(), + SettingsDataMonitorType::USER_DEFINED_DEVICE_NAME_MONITOR)); + dataShareHelper->RegisterObserver(*uri, settingDataObserver); + + int32_t osAccountId = GetActiveOsAccountIds(); + if (osAccountId == ERR_DM_FAILED) { + LOGE("Get OsAccountId error"); + return; + } + std::string accountIdStr = std::to_string(osAccountId); + uri = std::make_shared(SETTINGS_DATA_SECURE_URI + accountIdStr + + "?Proxy=true&key=" + USER_DEFINED_STRING); + + dataShareHelper->RegisterObserver(*uri, settingDataObserver); + dataShareHelper->Release(); + LOGI("register device name change cb success"); +} + +int32_t LocalDeviceNameMgr::QueryLocalDisplayName() +{ + LOGI("start"); + auto dataShareHelper = GetDataShareHelper(); + if (dataShareHelper == nullptr) { + LOGE("dataShareHelper is null"); + return ERR_DM_FAILED; + } + std::string localDisplayName = ""; + int32_t ret = GetDisplayDeviceName(dataShareHelper, localDisplayName); + if (ret != DM_OK || localDisplayName.empty()) { + LOGE("get display device name failed"); + return ERR_DM_FAILED; + } + std::lock_guard lock(devNameMtx_); + localDisplayName_ = localDisplayName; + dataShareHelper->Release(); + LOGI("get display deviceName=%{public}s", localDisplayName.c_str()); + return DM_OK; +} + +void LocalDeviceNameMgr::RegisterDisplayNameChangeCb() +{ + LOGI("start"); + auto dataShareHelper = GetDataShareHelper(); + if (dataShareHelper == nullptr) { + LOGE("dataShareHelper is null"); + return; + } + + int32_t osAccountId = GetActiveOsAccountIds(); + if (osAccountId == ERR_DM_FAILED) { + LOGE("Get OsAccountId error"); + return; + } + std::string accountIdStr = std::to_string(osAccountId); + auto uri = std::make_shared(SETTINGS_DATA_SECURE_URI + accountIdStr + + "?Proxy=true&key=" + DISPLAY_DEVICE_NAME_STRING); + sptr settingDataObserver = + sptr(new SettingsDataEventMonitor(shared_from_this(), + SettingsDataMonitorType::DISPLAY_DEVICE_NAME_MONITOR)); + dataShareHelper->RegisterObserver(*uri, settingDataObserver); + dataShareHelper->Release(); + LOGI("register display device name change cb success"); +} + +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/service/src/devicenamemgr/settings_data_event_monitor.cpp b/services/service/src/devicenamemgr/settings_data_event_monitor.cpp new file mode 100644 index 000000000..4dbf931a8 --- /dev/null +++ b/services/service/src/devicenamemgr/settings_data_event_monitor.cpp @@ -0,0 +1,50 @@ +/* + * 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 "settings_data_event_monitor.h" + +#include "dm_log.h" + +namespace OHOS { +namespace DistributedHardware { +SettingsDataEventMonitor::SettingsDataEventMonitor(std::shared_ptr localDeviceNameMgr, + SettingsDataMonitorType monitorType) + : localDeviceNameMgrWPtr_(localDeviceNameMgr), monitorType_(monitorType) +{ + LOGI("Ctor SettingsDataEventMonitor, monitorType: %{public}d", (int32_t)monitorType); +} + +void SettingsDataEventMonitor::OnChange() +{ + if (localDeviceNameMgrWPtr_.expired()) { + LOGE("localDeviceNameMgrWPtr_ expired"); + return; + } + LOGI("Settings OnChange type: %{public}d", (int32_t)monitorType_); + std::shared_ptr localDevNameSPtr = localDeviceNameMgrWPtr_.lock(); + switch (monitorType_) { + case SettingsDataMonitorType::USER_DEFINED_DEVICE_NAME_MONITOR: + localDevNameSPtr->QueryLocalDeviceName(); + break; + case SettingsDataMonitorType::DISPLAY_DEVICE_NAME_MONITOR: + localDevNameSPtr->QueryLocalDisplayName(); + break; + default: + LOGE("unknwon monitor type"); + break; + } +} +} // DistributedHardware +} // OHOS \ No newline at end of file diff --git a/services/service/src/ipc/standard/ipc_server_stub.cpp b/services/service/src/ipc/standard/ipc_server_stub.cpp index 40a4db53d..a35726888 100644 --- a/services/service/src/ipc/standard/ipc_server_stub.cpp +++ b/services/service/src/ipc/standard/ipc_server_stub.cpp @@ -15,10 +15,6 @@ #include "ipc_server_stub.h" -#include "device_manager_ipc_interface_code.h" -#include "device_manager_service.h" -#include "dm_constants.h" -#include "dm_log.h" #include "if_system_ability_manager.h" #include "ipc_cmd_register.h" #include "ipc_skeleton.h" @@ -31,9 +27,13 @@ #include "mem_mgr_client.h" #include "mem_mgr_proxy.h" #endif // SUPPORT_MEMMGR + #include "string_ex.h" #include "system_ability_definition.h" - +#include "device_manager_ipc_interface_code.h" +#include "device_manager_service.h" +#include "dm_constants.h" +#include "dm_log.h" #include "multiple_user_connector.h" namespace OHOS { @@ -47,6 +47,7 @@ IpcServerStub::IpcServerStub() : SystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGE { registerToService_ = false; state_ = ServiceRunningState::STATE_NOT_START; + accountBootListener_ = std::make_shared(); } void IpcServerStub::OnStart() @@ -85,6 +86,7 @@ void IpcServerStub::OnAddSystemAbility(int32_t systemAbilityId, const std::strin return; } state_ = ServiceRunningState::STATE_RUNNING; + accountBootListener_->RegisterAccountBootCb(); return; } diff --git a/test/servicesfuzztest/ipcserverstub_fuzzer/BUILD.gn b/test/servicesfuzztest/ipcserverstub_fuzzer/BUILD.gn index 8f0bce417..0b4a67247 100644 --- a/test/servicesfuzztest/ipcserverstub_fuzzer/BUILD.gn +++ b/test/servicesfuzztest/ipcserverstub_fuzzer/BUILD.gn @@ -69,6 +69,8 @@ ohos_fuzztest("IpcServerStubFuzzTest") { external_deps = [ "c_utils:utils", + "data_share:datashare_common", + "data_share:datashare_consumer", "device_auth:deviceauth_sdk", "ipc:ipc_core", "ipc:ipc_single", diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 3641dee2f..508556c77 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -93,6 +93,8 @@ ohos_unittest("UTTest_pin_auth") { deps = [ ":device_manager_test_common" ] external_deps = [ + "data_share:datashare_common", + "data_share:datashare_consumer", "device_auth:deviceauth_sdk", "device_info_manager:distributed_device_profile_common", "device_info_manager:distributed_device_profile_sdk", @@ -174,6 +176,8 @@ ohos_unittest("UTTest_ipc_cmd_parser_service") { deps = [ ":device_manager_test_common" ] external_deps = [ + "data_share:datashare_common", + "data_share:datashare_consumer", "device_auth:deviceauth_sdk", "device_info_manager:distributed_device_profile_common", "device_info_manager:distributed_device_profile_sdk", @@ -672,6 +676,8 @@ ohos_unittest("UTTest_ipc_server_client_proxy") { deps = [ ":device_manager_test_common" ] external_deps = [ + "data_share:datashare_common", + "data_share:datashare_consumer", "device_auth:deviceauth_sdk", "device_info_manager:distributed_device_profile_common", "device_info_manager:distributed_device_profile_sdk", @@ -708,6 +714,8 @@ ohos_unittest("UTTest_ipc_server_stub") { deps = [ ":device_manager_test_common" ] external_deps = [ + "data_share:datashare_common", + "data_share:datashare_consumer", "device_auth:deviceauth_sdk", "device_info_manager:distributed_device_profile_common", "device_info_manager:distributed_device_profile_sdk", @@ -1150,6 +1158,8 @@ ohos_unittest("UTTest_discovery_filter") { deps = [ ":device_manager_test_common" ] external_deps = [ + "data_share:datashare_common", + "data_share:datashare_consumer", "device_auth:deviceauth_sdk", "device_info_manager:distributed_device_profile_common", "device_info_manager:distributed_device_profile_sdk", -- Gitee From f33eb48ac811131f32f6b54d89e2fc834e427bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B7=8D?= Date: Fri, 11 Oct 2024 10:38:47 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E4=BF=AE=E6=94=B9=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=A4=96?= =?UTF-8?q?=E9=83=A8=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/src/devicenamemgr/account_boot_listener.cpp | 6 ++---- test/unittest/BUILD.gn | 6 ++++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/services/service/src/devicenamemgr/account_boot_listener.cpp b/services/service/src/devicenamemgr/account_boot_listener.cpp index 91979bc6f..860a7d198 100644 --- a/services/service/src/devicenamemgr/account_boot_listener.cpp +++ b/services/service/src/devicenamemgr/account_boot_listener.cpp @@ -21,7 +21,7 @@ #include "dm_log.h" #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) -#include "dm_thread_manager.h" +#include "ffrt.h" #endif namespace OHOS { @@ -44,9 +44,7 @@ static void AccountBootCb(const char *key, const char *value, void *context) } LOGI("Trigger"); #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) - ThreadManager::GetInstance().Submit(ACCOUNT_BOOT_EVENT, [=]() { - accountBootListener->DoAccountBootProc(); - }); + ffrt::submit([=]() { accountBootListener->DoAccountBootProc(); }); #else std::thread dealThread([=]() { accountBootListener->DoAccountBootProc(); diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 508556c77..9c17755be 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -698,6 +698,8 @@ ohos_unittest("UTTest_ipc_server_listener") { deps = [ ":device_manager_test_common" ] external_deps = [ + "data_share:datashare_common", + "data_share:datashare_consumer", "device_auth:deviceauth_sdk", "googletest:gmock", ] @@ -1378,6 +1380,8 @@ ohos_unittest("UTTest_dm_publish_manager") { external_deps = [ "cJSON:cjson", + "data_share:datashare_common", + "data_share:datashare_consumer", "device_auth:deviceauth_sdk", "device_info_manager:distributed_device_profile_common", "device_info_manager:distributed_device_profile_sdk", @@ -1482,6 +1486,8 @@ ohos_static_library("device_manager_test_common") { "c_utils:utils", "common_event_service:cesfwk_core", "common_event_service:cesfwk_innerkits", + "data_share:datashare_common", + "data_share:datashare_consumer", "device_auth:deviceauth_sdk", "device_info_manager:distributed_device_profile_common", "device_info_manager:distributed_device_profile_sdk", -- Gitee From 41a4e2ff0caad35696b0c7edcaae083044928b89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B7=8D?= Date: Fri, 11 Oct 2024 10:38:47 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E8=A1=A5=E5=85=85Signed-off-by?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李巍 --- services/service/src/devicenamemgr/account_boot_listener.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/service/src/devicenamemgr/account_boot_listener.cpp b/services/service/src/devicenamemgr/account_boot_listener.cpp index 860a7d198..3c5bd1bc7 100644 --- a/services/service/src/devicenamemgr/account_boot_listener.cpp +++ b/services/service/src/devicenamemgr/account_boot_listener.cpp @@ -55,6 +55,7 @@ static void AccountBootCb(const char *key, const char *value, void *context) } dealThread.detach(); #endif + } AccountBootListener::AccountBootListener() : isRegAccountBootCb_(false), -- Gitee