diff --git a/bundle.json b/bundle.json index 8e193b87c69894e7189d66c373d1bdbf1bb5e3fc..014692de816bbb23e089725f3c9032b0092b75f6 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 0550cd7b95f8454c58376cd18e206ac884d5cf8b..2d24a868d19a4abf226baa0d6fc6bf6dac05ddcd 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 30ded78bf361aac6155d053cc705767674957843..9730b6861196211ea30bef4d220c3218e37bd57d 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 MAX_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 > 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/BUILD.gn b/services/service/BUILD.gn index 60a6c439a7ff725c401a78abbd4d5f3d514e73a1..ee1da5e345dfbbdbf7eca7b63f5112a7e943915a 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", @@ -173,6 +177,7 @@ if (defined(ohos_lite)) { "src/pinholder/pin_holder.cpp", "src/pinholder/pin_holder_session.cpp", "src/publishcommonevent/dm_account_common_event.cpp", + "src/publishcommonevent/dm_data_share_common_event.cpp", "src/publishcommonevent/dm_package_common_event.cpp", "src/publishcommonevent/dm_screen_common_event.cpp", "src/relationshipsyncmgr/relationship_sync_mgr.cpp", @@ -204,6 +209,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 +224,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 0000000000000000000000000000000000000000..341dcd8dbe9167e6d874c61468c5d41feb170635 --- /dev/null +++ b/services/service/include/devicenamemgr/account_boot_listener.h @@ -0,0 +1,53 @@ +/* + * 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 "dm_data_share_common_event.h" +#include "local_device_name_mgr.h" +namespace OHOS { +namespace DistributedHardware { +enum class SaTriggerFlag : int32_t { + DM_SA_READY = 0, + DATA_SHARE_SA_REDDY = 1 +}; + +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 + * true: has register the callback + * false: NOT register the callback + */ + std::atomic isRegAccountBootCb_; + std::shared_ptr localDeviceMgr_; + std::atomic isDmSaReady_; + std::atomic isDataShareReady_; + static std::mutex depSaStatelock_; + std::shared_ptr dataShareCommonEventManager_; +}; +} // 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 0000000000000000000000000000000000000000..28630fc4a12c61b55ec4a668fd95346d9173e952 --- /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 0000000000000000000000000000000000000000..1ba6591bbd87663e64579ee024b36caa9f9d7f61 --- /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 + +#include "data_ability_observer_stub.h" +#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 77a83b2117ceea64494889404fc62e5e422a97b6..6c3f819f284cb0084c2ec1cf6b21ad6f54b32dc4 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/include/publishcommonevent/dm_data_share_common_event.h b/services/service/include/publishcommonevent/dm_data_share_common_event.h new file mode 100644 index 0000000000000000000000000000000000000000..f861486a9c0863839249a5381ade4ac5e2b90ba9 --- /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 new file mode 100644 index 0000000000000000000000000000000000000000..50de621fbc22edc190a5d4faadb234d91bc995f5 --- /dev/null +++ b/services/service/src/devicenamemgr/account_boot_listener.cpp @@ -0,0 +1,162 @@ +/* + * 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 "common_event_support.h" +#include "device_manager_service.h" +#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"; +#if (defined(__LITEOS_M__) || defined(LITE_DEVICE)) + const char * const ACCOUNT_BOOT_EVENT = "account_boot_event"; +#endif +} + +std::mutex AccountBootListener::depSaStatelock_; + +static void AccountBootCb(const char *key, const char *value, void *context) +{ + 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) { + 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()), isDmSaReady_(false), isDataShareReady_(false), + dataShareCommonEventManager_(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(); +} + +void AccountBootListener::SetSaTriggerFlag(SaTriggerFlag triggerFlag) +{ + LOGI("start"); + std::lock_guard lock(depSaStatelock_); + switch (triggerFlag) { + case SaTriggerFlag::DM_SA_READY: + isDmSaReady_ = true; + LOGI("DM SA ready!"); + break; + 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_)); + 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(depSaStatelock_); + isDataShareReady_ = true; + 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(); + } +} +} // 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 0000000000000000000000000000000000000000..84db17a396cd5ea68b25ad90123a24ca59ea5835 --- /dev/null +++ b/services/service/src/devicenamemgr/local_device_name_mgr.cpp @@ -0,0 +1,278 @@ +/* + * 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) +{ + 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; + 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) +{ + 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); +} + +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!"); + 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) +{ + 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!"); + 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 0000000000000000000000000000000000000000..421acfc7bf4f39a444fda0ad5b8fb988406f6fd4 --- /dev/null +++ b/services/service/src/devicenamemgr/settings_data_event_monitor.cpp @@ -0,0 +1,54 @@ +/* + * 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(); + if (localDevNameSPtr == nullptr) { + LOGE("localDevNameSPtr is null!"); + return; + } + 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 40a4db53d1b30a7b725a87b0defa5eb38125835e..0ad5e7f8f9784802093135217ff7438925c903f6 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_->SetSaTriggerFlag(SaTriggerFlag::DM_SA_READY); return; } @@ -112,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 0000000000000000000000000000000000000000..c7e43b10f55446d28477058896f75e6a180541d9 --- /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 diff --git a/test/servicesfuzztest/ipcserverstub_fuzzer/BUILD.gn b/test/servicesfuzztest/ipcserverstub_fuzzer/BUILD.gn index 8f0bce41791d822374f9939f607d94db399600e2..737b7b1877742235e9f11e1c384f918d23f86c06 100644 --- a/test/servicesfuzztest/ipcserverstub_fuzzer/BUILD.gn +++ b/test/servicesfuzztest/ipcserverstub_fuzzer/BUILD.gn @@ -68,7 +68,10 @@ ohos_fuzztest("IpcServerStubFuzzTest") { ] external_deps = [ + "ability_base:want", "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 3641dee2fb690c7b2552baa3729678d87c37de2c..9c17755becd993dc925631d7a8d3c0404f07d1f4 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",