From ead1afa184087f8b4bee1e934ca1d8b7e4a05f4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B7=8D?= Date: Fri, 11 Oct 2024 11:00:41 +0800 Subject: [PATCH 1/9] =?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=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 + .../dependency/softbus/softbus_session.cpp | 3 +- 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 | 98 +++++++ .../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 | 16 ++ 14 files changed, 585 insertions(+), 7 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/implementation/src/dependency/softbus/softbus_session.cpp b/services/implementation/src/dependency/softbus/softbus_session.cpp index 30ded78bf..808872474 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) { @@ -179,7 +180,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; 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..3c5bd1bc7 --- /dev/null +++ b/services/service/src/devicenamemgr/account_boot_listener.cpp @@ -0,0 +1,98 @@ +/* + * 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 "ffrt.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)) + ffrt::submit([=]() { 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..9c17755be 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", @@ -692,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", ] @@ -708,6 +716,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 +1160,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", @@ -1368,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", @@ -1472,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 74fb1070147b109c54da7d4b3fc89637abf33936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B7=8D?= Date: Fri, 11 Oct 2024 11:35:49 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E7=9A=84=E7=A9=BA=E8=A1=8C?= 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 deletion(-) diff --git a/services/service/src/devicenamemgr/account_boot_listener.cpp b/services/service/src/devicenamemgr/account_boot_listener.cpp index 3c5bd1bc7..860a7d198 100644 --- a/services/service/src/devicenamemgr/account_boot_listener.cpp +++ b/services/service/src/devicenamemgr/account_boot_listener.cpp @@ -55,7 +55,6 @@ static void AccountBootCb(const char *key, const char *value, void *context) } dealThread.detach(); #endif - } AccountBootListener::AccountBootListener() : isRegAccountBootCb_(false), -- Gitee From 77688075b1cf8d618af45e81dbba40047cef9a7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B7=8D?= Date: Sat, 12 Oct 2024 11:57:17 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E6=9E=84=E5=BB=BA=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= 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 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/service/src/devicenamemgr/account_boot_listener.cpp b/services/service/src/devicenamemgr/account_boot_listener.cpp index 860a7d198..60b56ad35 100644 --- a/services/service/src/devicenamemgr/account_boot_listener.cpp +++ b/services/service/src/devicenamemgr/account_boot_listener.cpp @@ -28,7 +28,9 @@ namespace OHOS { namespace DistributedHardware { namespace { const char * const BOOTEVENT_ACCOUNT_READY = "bootevent.account.ready"; +#if (defined(__LITEOS_M__) || defined(LITE_DEVICE)) const char * const ACCOUNT_BOOT_EVENT = "account_boot_event"; +#endif } static void AccountBootCb(const char *key, const char *value, void *context) -- Gitee From 6431c10843097117ff70801275a1b8b6d422af2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B7=8D?= Date: Tue, 15 Oct 2024 22:04:00 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0COMMON=5FEVENT=5FDATA=5FS?= =?UTF-8?q?HARE=5FREADY=E7=9B=91=E5=90=AC=EF=BC=8C=E7=A1=AE=E4=BF=9Ddata?= =?UTF-8?q?=5Fshare=E6=88=90=E5=8A=9F=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李巍 --- services/service/BUILD.gn | 1 + .../devicenamemgr/account_boot_listener.h | 12 ++ .../dm_data_share_common_event.h | 83 ++++++++ .../devicenamemgr/account_boot_listener.cpp | 59 +++++- .../src/ipc/standard/ipc_server_stub.cpp | 3 +- .../dm_data_share_common_event.cpp | 180 ++++++++++++++++++ 6 files changed, 336 insertions(+), 2 deletions(-) create mode 100644 services/service/include/publishcommonevent/dm_data_share_common_event.h create mode 100644 services/service/src/publishcommonevent/dm_data_share_common_event.cpp diff --git a/services/service/BUILD.gn b/services/service/BUILD.gn index b05125f26..a00da9fff 100644 --- a/services/service/BUILD.gn +++ b/services/service/BUILD.gn @@ -179,6 +179,7 @@ if (defined(ohos_lite)) { "src/publishcommonevent/dm_account_common_event.cpp", "src/publishcommonevent/dm_package_common_event.cpp", "src/publishcommonevent/dm_screen_common_event.cpp", + "src/publishcommonevent/dm_data_share_common_event.cpp", "src/relationshipsyncmgr/relationship_sync_mgr.cpp", "src/softbus/mine_softbus_listener.cpp", "src/softbus/softbus_listener.cpp", diff --git a/services/service/include/devicenamemgr/account_boot_listener.h b/services/service/include/devicenamemgr/account_boot_listener.h index d6e18ec38..8c894c6f9 100644 --- a/services/service/include/devicenamemgr/account_boot_listener.h +++ b/services/service/include/devicenamemgr/account_boot_listener.h @@ -17,15 +17,23 @@ #define OHOS_ACCOUNT_BOOT_LISTENER_H #include +#include "dm_data_share_common_event.h" #include "local_device_name_mgr.h" namespace OHOS { namespace DistributedHardware { +typedef enum SaTriggerFlag { + DM_SA_READY, + DATA_SHARE_SA_REDDY +} SaTriggerFlag; class AccountBootListener { public: AccountBootListener(); ~AccountBootListener(); void RegisterAccountBootCb(); void DoAccountBootProc(); + void SetSaTriggerFlag(SaTriggerFlag triggerFlag); + void InitDataShareEvent(); + void DataShareCallback(); private: /** * @brief flag for is registered callback for account boot event @@ -34,6 +42,10 @@ private: */ std::atomic isRegAccountBootCb_; std::shared_ptr localDeviceMgr_; + std::atomic isDmSaReady_; + std::atomic isDataShareReady_; + static std::mutex lock_; + std::shared_ptr dataShareCommonEventManager_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/include/publishcommonevent/dm_data_share_common_event.h b/services/service/include/publishcommonevent/dm_data_share_common_event.h new file mode 100644 index 000000000..f861486a9 --- /dev/null +++ b/services/service/include/publishcommonevent/dm_data_share_common_event.h @@ -0,0 +1,83 @@ +/* + * 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 +#include +#include +#include + +#include "common_event_data.h" +#include "common_event_manager.h" +#include "common_event_subscribe_info.h" +#include "common_event_subscriber.h" +#include "matching_skills.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/devicenamemgr/account_boot_listener.cpp b/services/service/src/devicenamemgr/account_boot_listener.cpp index 60b56ad35..fec2dc7bc 100644 --- a/services/service/src/devicenamemgr/account_boot_listener.cpp +++ b/services/service/src/devicenamemgr/account_boot_listener.cpp @@ -17,6 +17,8 @@ #include +#include "common_event_support.h" +#include "device_manager_service.h" #include "parameter.h" #include "dm_log.h" @@ -33,6 +35,8 @@ namespace { #endif } +std::mutex AccountBootListener::lock_; + static void AccountBootCb(const char *key, const char *value, void *context) { if (strcmp(key, BOOTEVENT_ACCOUNT_READY) != 0 || strcmp(value, "true") != 0) { @@ -60,7 +64,8 @@ static void AccountBootCb(const char *key, const char *value, void *context) } AccountBootListener::AccountBootListener() : isRegAccountBootCb_(false), - localDeviceMgr_(std::make_shared()) + localDeviceMgr_(std::make_shared()), isDmSaReady_(false), isDataShareReady_(false), + dataShareCommonEventManager_(std::make_shared()) { LOGI("Ctor AccountBootListener"); } @@ -95,5 +100,57 @@ void AccountBootListener::DoAccountBootProc() localDeviceMgr_->QueryLocalDeviceName(); localDeviceMgr_->QueryLocalDisplayName(); } + +void AccountBootListener::SetSaTriggerFlag(SaTriggerFlag triggerFlag) +{ + LOGI("start"); + std::lock_guard lock(lock_); + switch (triggerFlag) { + case DM_SA_READY: + isDmSaReady_ = true; + LOGI("DM SA ready!"); + break; + case DATA_SHARE_SA_REDDY: + LOGI("DATA SHARE SA ready!"); + this->InitDataShareEvent(); + break; + default: + break; + } + if (isDmSaReady_ && isDataShareReady_) { + LOGI("dm and data_share is ready!"); + this->RegisterAccountBootCb(); + } +} + +void AccountBootListener::InitDataShareEvent() +{ + LOGI("Start"); + if (dataShareCommonEventManager_ == nullptr) { + dataShareCommonEventManager_ = std::make_shared(); + } + DataShareEventCallback callback = [=]() { + this->DataShareCallback(); + }; + std::vector dataShareEventVec; + dataShareEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY); + if (dataShareCommonEventManager_->SubscribeDataShareCommonEvent(dataShareEventVec, callback)) { + LOGI("Success"); + } + return; +} + +void AccountBootListener::DataShareCallback() +{ + LOGI("Start"); + std::lock_guard lock(lock_); + isDataShareReady_ = true; + LOGI("isDmSaReady_:%{public}d", std::atomic_load(&isDmSaReady_)); + LOGI("isDataShareReady_:%{public}d", std::atomic_load(&isDataShareReady_)); + if (isDmSaReady_ && isDataShareReady_) { + LOGI("dm and data_share is ready!"); + this->RegisterAccountBootCb(); + } +} } // namespace DistributedHardware } // namespace 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 a35726888..0ad5e7f8f 100644 --- a/services/service/src/ipc/standard/ipc_server_stub.cpp +++ b/services/service/src/ipc/standard/ipc_server_stub.cpp @@ -86,7 +86,7 @@ void IpcServerStub::OnAddSystemAbility(int32_t systemAbilityId, const std::strin return; } state_ = ServiceRunningState::STATE_RUNNING; - accountBootListener_->RegisterAccountBootCb(); + accountBootListener_->SetSaTriggerFlag(SaTriggerFlag::DM_SA_READY); return; } @@ -114,6 +114,7 @@ void IpcServerStub::OnAddSystemAbility(int32_t systemAbilityId, const std::strin #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) if (systemAbilityId == DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID) { KVAdapterManager::GetInstance().ReInit(); + accountBootListener_->SetSaTriggerFlag(SaTriggerFlag::DATA_SHARE_SA_REDDY); return; } #endif diff --git a/services/service/src/publishcommonevent/dm_data_share_common_event.cpp b/services/service/src/publishcommonevent/dm_data_share_common_event.cpp new file mode 100644 index 000000000..c7e43b10f --- /dev/null +++ b/services/service/src/publishcommonevent/dm_data_share_common_event.cpp @@ -0,0 +1,180 @@ +/* + * 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_constants.h" +#include "dm_log.h" +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) +#include "ffrt.h" +#endif +#include "iservice_registry.h" +#include "multiple_user_connector.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) +{ + LOGI("start"); + 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 b04d473106fad102e8408f595b3ea2e6dd633475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B7=8D?= Date: Wed, 16 Oct 2024 11:03:51 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李巍 --- .../service/include/devicenamemgr/settings_data_event_monitor.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/service/include/devicenamemgr/settings_data_event_monitor.h b/services/service/include/devicenamemgr/settings_data_event_monitor.h index b1be98102..1ba6591bb 100644 --- a/services/service/include/devicenamemgr/settings_data_event_monitor.h +++ b/services/service/include/devicenamemgr/settings_data_event_monitor.h @@ -16,9 +16,9 @@ #ifndef OHOS_SETTINGS_DATA_EVENT_MONITOR_H #define OHOS_SETTINGS_DATA_EVENT_MONITOR_H -#include "data_ability_observer_stub.h" #include +#include "data_ability_observer_stub.h" #include "local_device_name_mgr.h" namespace OHOS { -- Gitee From 1d012d9e4953eb64c99da220af7fa2ab0bfbba55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B7=8D?= Date: Wed, 16 Oct 2024 12:04:35 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E4=BF=AE=E6=94=B9build.gn=E5=AD=97?= =?UTF-8?q?=E5=85=B8=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李巍 --- services/service/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/service/BUILD.gn b/services/service/BUILD.gn index a00da9fff..ee1da5e34 100644 --- a/services/service/BUILD.gn +++ b/services/service/BUILD.gn @@ -177,9 +177,9 @@ 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_package_common_event.cpp", "src/publishcommonevent/dm_screen_common_event.cpp", - "src/publishcommonevent/dm_data_share_common_event.cpp", "src/relationshipsyncmgr/relationship_sync_mgr.cpp", "src/softbus/mine_softbus_listener.cpp", "src/softbus/softbus_listener.cpp", -- Gitee From e5887256cb30859fb6027e3b476282dc730baef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B7=8D?= Date: Wed, 16 Oct 2024 17:27:27 +0800 Subject: [PATCH 7/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0fuzz=E5=A4=96=E9=83=A8?= =?UTF-8?q?=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李巍 --- test/servicesfuzztest/ipcserverstub_fuzzer/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/test/servicesfuzztest/ipcserverstub_fuzzer/BUILD.gn b/test/servicesfuzztest/ipcserverstub_fuzzer/BUILD.gn index 0b4a67247..737b7b187 100644 --- a/test/servicesfuzztest/ipcserverstub_fuzzer/BUILD.gn +++ b/test/servicesfuzztest/ipcserverstub_fuzzer/BUILD.gn @@ -68,6 +68,7 @@ ohos_fuzztest("IpcServerStubFuzzTest") { ] external_deps = [ + "ability_base:want", "c_utils:utils", "data_share:datashare_common", "data_share:datashare_consumer", -- Gitee From 53493b038e5884173064013892f32d34a6d2a2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B7=8D?= Date: Thu, 17 Oct 2024 15:55:35 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李巍 --- .../src/devicenamemgr/account_boot_listener.cpp | 10 ++++++++-- .../src/devicenamemgr/local_device_name_mgr.cpp | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/services/service/src/devicenamemgr/account_boot_listener.cpp b/services/service/src/devicenamemgr/account_boot_listener.cpp index fec2dc7bc..87947975b 100644 --- a/services/service/src/devicenamemgr/account_boot_listener.cpp +++ b/services/service/src/devicenamemgr/account_boot_listener.cpp @@ -39,6 +39,10 @@ std::mutex AccountBootListener::lock_; static void AccountBootCb(const char *key, const char *value, void *context) { + if (key == nullptr || value == nullptr) { + LOGE("key or value is null, param is error!"); + return; + } if (strcmp(key, BOOTEVENT_ACCOUNT_READY) != 0 || strcmp(value, "true") != 0) { return; } @@ -117,6 +121,8 @@ void AccountBootListener::SetSaTriggerFlag(SaTriggerFlag triggerFlag) default: break; } + LOGI("isDmSaReady_:%{public}d,isDataShareReady_:%{public}d", + std::atomic_load(&isDmSaReady_),std::atomic_load(&isDataShareReady_)); if (isDmSaReady_ && isDataShareReady_) { LOGI("dm and data_share is ready!"); this->RegisterAccountBootCb(); @@ -145,8 +151,8 @@ void AccountBootListener::DataShareCallback() LOGI("Start"); std::lock_guard lock(lock_); isDataShareReady_ = true; - LOGI("isDmSaReady_:%{public}d", std::atomic_load(&isDmSaReady_)); - LOGI("isDataShareReady_:%{public}d", std::atomic_load(&isDataShareReady_)); + LOGI("isDmSaReady_:%{public}d,isDataShareReady_:%{public}d", + std::atomic_load(&isDmSaReady_),std::atomic_load(&isDataShareReady_)); if (isDmSaReady_ && isDataShareReady_) { LOGI("dm and data_share is ready!"); this->RegisterAccountBootCb(); diff --git a/services/service/src/devicenamemgr/local_device_name_mgr.cpp b/services/service/src/devicenamemgr/local_device_name_mgr.cpp index 58b80f376..8f2c803ee 100644 --- a/services/service/src/devicenamemgr/local_device_name_mgr.cpp +++ b/services/service/src/devicenamemgr/local_device_name_mgr.cpp @@ -76,6 +76,10 @@ int32_t LocalDeviceNameMgr::GetDeviceNameFromDataShareHelper( std::shared_ptr dataShareHelper, std::shared_ptr uri, const char *key, std::string &deviceName) { + if (uri == nullptr || key == nullptr) { + LOGE("uri or key is null, param is error!"); + return ERR_DM_FAILED; + } int32_t numRows = 0; std::string val; std::vector columns; @@ -122,6 +126,7 @@ int32_t LocalDeviceNameMgr::GetUserDefinedDeviceName(std::shared_ptr Date: Sat, 19 Oct 2024 16:00:33 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李巍 --- .../dependency/softbus/softbus_session.cpp | 4 ++-- .../devicenamemgr/account_boot_listener.h | 11 +++++----- .../devicenamemgr/account_boot_listener.cpp | 22 +++++++++---------- .../devicenamemgr/local_device_name_mgr.cpp | 17 ++++++++++++-- .../settings_data_event_monitor.cpp | 4 ++++ 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/services/implementation/src/dependency/softbus/softbus_session.cpp b/services/implementation/src/dependency/softbus/softbus_session.cpp index 808872474..9730b6861 100644 --- a/services/implementation/src/dependency/softbus/softbus_session.cpp +++ b/services/implementation/src/dependency/softbus/softbus_session.cpp @@ -27,7 +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; +constexpr int32_t MAX_DATA_LEN = 65535; static void OnShutdown(int32_t socket, ShutdownReason reason) { @@ -180,7 +180,7 @@ void SoftbusSession::OnSessionClosed(int sessionId) void SoftbusSession::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen) { - if (sessionId < 0 || data == nullptr || dataLen <= 0 || dataLen > DATA_LEN) { + if (sessionId < 0 || data == nullptr || dataLen <= 0 || dataLen > MAX_DATA_LEN) { LOGI("[SOFTBUS]fail to receive data from softbus with sessionId: %{public}d, dataLen: %{public}d.", sessionId, dataLen); return; diff --git a/services/service/include/devicenamemgr/account_boot_listener.h b/services/service/include/devicenamemgr/account_boot_listener.h index 8c894c6f9..341dcd8db 100644 --- a/services/service/include/devicenamemgr/account_boot_listener.h +++ b/services/service/include/devicenamemgr/account_boot_listener.h @@ -21,10 +21,11 @@ #include "local_device_name_mgr.h" namespace OHOS { namespace DistributedHardware { -typedef enum SaTriggerFlag { - DM_SA_READY, - DATA_SHARE_SA_REDDY -} SaTriggerFlag; +enum class SaTriggerFlag : int32_t { + DM_SA_READY = 0, + DATA_SHARE_SA_REDDY = 1 +}; + class AccountBootListener { public: AccountBootListener(); @@ -44,7 +45,7 @@ private: std::shared_ptr localDeviceMgr_; std::atomic isDmSaReady_; std::atomic isDataShareReady_; - static std::mutex lock_; + static std::mutex depSaStatelock_; std::shared_ptr dataShareCommonEventManager_; }; } // namespace DistributedHardware diff --git a/services/service/src/devicenamemgr/account_boot_listener.cpp b/services/service/src/devicenamemgr/account_boot_listener.cpp index 87947975b..50de621fb 100644 --- a/services/service/src/devicenamemgr/account_boot_listener.cpp +++ b/services/service/src/devicenamemgr/account_boot_listener.cpp @@ -35,12 +35,12 @@ namespace { #endif } -std::mutex AccountBootListener::lock_; +std::mutex AccountBootListener::depSaStatelock_; static void AccountBootCb(const char *key, const char *value, void *context) { - if (key == nullptr || value == nullptr) { - LOGE("key or value is null, param is error!"); + if (key == nullptr || value == nullptr || context == nullptr) { + LOGE("key or value or context is null, param is error!"); return; } if (strcmp(key, BOOTEVENT_ACCOUNT_READY) != 0 || strcmp(value, "true") != 0) { @@ -108,21 +108,21 @@ void AccountBootListener::DoAccountBootProc() void AccountBootListener::SetSaTriggerFlag(SaTriggerFlag triggerFlag) { LOGI("start"); - std::lock_guard lock(lock_); + std::lock_guard lock(depSaStatelock_); switch (triggerFlag) { - case DM_SA_READY: + case SaTriggerFlag::DM_SA_READY: isDmSaReady_ = true; LOGI("DM SA ready!"); break; - case DATA_SHARE_SA_REDDY: + case SaTriggerFlag::DATA_SHARE_SA_REDDY: LOGI("DATA SHARE SA ready!"); this->InitDataShareEvent(); break; default: break; } - LOGI("isDmSaReady_:%{public}d,isDataShareReady_:%{public}d", - std::atomic_load(&isDmSaReady_),std::atomic_load(&isDataShareReady_)); + LOGI("isDmSaReady_: %{public}d, isDataShareReady_: %{public}d", + std::atomic_load(&isDmSaReady_), std::atomic_load(&isDataShareReady_)); if (isDmSaReady_ && isDataShareReady_) { LOGI("dm and data_share is ready!"); this->RegisterAccountBootCb(); @@ -149,10 +149,10 @@ void AccountBootListener::InitDataShareEvent() void AccountBootListener::DataShareCallback() { LOGI("Start"); - std::lock_guard lock(lock_); + std::lock_guard lock(depSaStatelock_); isDataShareReady_ = true; - LOGI("isDmSaReady_:%{public}d,isDataShareReady_:%{public}d", - std::atomic_load(&isDmSaReady_),std::atomic_load(&isDataShareReady_)); + LOGI("isDmSaReady_: %{public}d, isDataShareReady_: %{public}d", + std::atomic_load(&isDmSaReady_), std::atomic_load(&isDataShareReady_)); if (isDmSaReady_ && isDataShareReady_) { LOGI("dm and data_share is ready!"); this->RegisterAccountBootCb(); diff --git a/services/service/src/devicenamemgr/local_device_name_mgr.cpp b/services/service/src/devicenamemgr/local_device_name_mgr.cpp index 8f2c803ee..84db17a39 100644 --- a/services/service/src/devicenamemgr/local_device_name_mgr.cpp +++ b/services/service/src/devicenamemgr/local_device_name_mgr.cpp @@ -43,6 +43,7 @@ namespace { 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"); @@ -76,8 +77,8 @@ int32_t LocalDeviceNameMgr::GetDeviceNameFromDataShareHelper( std::shared_ptr dataShareHelper, std::shared_ptr uri, const char *key, std::string &deviceName) { - if (uri == nullptr || key == nullptr) { - LOGE("uri or key is null, param is error!"); + if (dataShareHelper == nullptr || uri == nullptr || key == nullptr) { + LOGE("dataShareHelper or uri or key is null, param is error!"); return ERR_DM_FAILED; } int32_t numRows = 0; @@ -116,6 +117,10 @@ int32_t LocalDeviceNameMgr::GetDeviceNameFromDataShareHelper( int32_t LocalDeviceNameMgr::GetDefaultDeviceName(std::shared_ptr dataShareHelper, std::string &deviceName) { + if (dataShareHelper == nullptr) { + LOGE("dataShareHelper is null, param is error!"); + return ERR_DM_FAILED; + } 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); @@ -124,6 +129,10 @@ int32_t LocalDeviceNameMgr::GetDefaultDeviceName(std::shared_ptr dataShareHelper, std::string &deviceName) { + if (dataShareHelper == nullptr) { + LOGE("dataShareHelper is null, param is error!"); + return ERR_DM_FAILED; + } int32_t osAccountId = GetActiveOsAccountIds(); if (osAccountId == ERR_DM_FAILED) { LOGE("osAccountId acquire fail!"); @@ -139,6 +148,10 @@ int32_t LocalDeviceNameMgr::GetUserDefinedDeviceName(std::shared_ptr dataShareHelper, std::string &deviceName) { + if (dataShareHelper == nullptr) { + LOGE("dataShareHelper is null, param is error!"); + return ERR_DM_FAILED; + } int32_t osAccountId = GetActiveOsAccountIds(); if (osAccountId == ERR_DM_FAILED) { LOGE("osAccountId acquire fail!"); diff --git a/services/service/src/devicenamemgr/settings_data_event_monitor.cpp b/services/service/src/devicenamemgr/settings_data_event_monitor.cpp index 4dbf931a8..421acfc7b 100644 --- a/services/service/src/devicenamemgr/settings_data_event_monitor.cpp +++ b/services/service/src/devicenamemgr/settings_data_event_monitor.cpp @@ -34,6 +34,10 @@ void SettingsDataEventMonitor::OnChange() } LOGI("Settings OnChange type: %{public}d", (int32_t)monitorType_); std::shared_ptr localDevNameSPtr = localDeviceNameMgrWPtr_.lock(); + if (localDevNameSPtr == nullptr) { + LOGE("localDevNameSPtr is null!"); + return; + } switch (monitorType_) { case SettingsDataMonitorType::USER_DEFINED_DEVICE_NAME_MONITOR: localDevNameSPtr->QueryLocalDeviceName(); -- Gitee