From 84b807ab72ea55d5282003645c2441b04583938b Mon Sep 17 00:00:00 2001 From: chenshuo Date: Thu, 7 Nov 2024 16:30:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=A4=9A=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=AE=A1=E7=90=86=E4=B8=8E=E4=BA=8B=E4=BB=B6=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenshuo --- .../multiusermanager/multi_user_manager.h | 49 +++++ .../dp_account_common_event.h | 83 ++++++++ .../multiusermanager/multi_user_manager.cpp | 81 ++++++++ .../dp_account_common_event.cpp | 181 ++++++++++++++++++ 4 files changed, 394 insertions(+) create mode 100644 services/core/include/multiusermanager/multi_user_manager.h create mode 100644 services/core/include/publishcommonevent/dp_account_common_event.h create mode 100644 services/core/src/multiusermanager/multi_user_manager.cpp create mode 100644 services/core/src/publishcommonevent/dp_account_common_event.cpp diff --git a/services/core/include/multiusermanager/multi_user_manager.h b/services/core/include/multiusermanager/multi_user_manager.h new file mode 100644 index 00000000..0605aabb --- /dev/null +++ b/services/core/include/multiusermanager/multi_user_manager.h @@ -0,0 +1,49 @@ +/* + * 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_DP_MULTIPLE_USER_CONNECTOR_H +#define OHOS_DP_MULTIPLE_USER_CONNECTOR_H + +#include +#include +#include +#include + +#include "iremote_object.h" +#include "distributed_device_profile_constants.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class MultiUserManager { + DECLARE_SINGLE_INSTANCE(MultiUserManager); +public: + int32_t Init(); + + int32_t UnInit(); + + int32_t GetCurrentForegroundUserID(); + + void SetCurrentForegroundUserID(int32_t userId); + + int32_t GetForegroundUserIDFromOs(int32_t& foregroundId); + +private: + int32_t foregroundUserId_ = DEFAULT_USER_ID; + std::mutex foregroundUserIdLock_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DP_MULTIPLE_USER_CONNECTOR_H diff --git a/services/core/include/publishcommonevent/dp_account_common_event.h b/services/core/include/publishcommonevent/dp_account_common_event.h new file mode 100644 index 00000000..17aed149 --- /dev/null +++ b/services/core/include/publishcommonevent/dp_account_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_DP_ACCOUNT_COMMON_EVENT_H +#define OHOS_DP_ACCOUNT_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 DistributedDeviceProfile { +using OHOS::EventFwk::CommonEventData; +using OHOS::EventFwk::CommonEventSubscriber; +using OHOS::EventFwk::CommonEventSubscribeInfo; +using AccountEventCallback = std::function; + +class DpAccountEventSubscriber : public CommonEventSubscriber { +public: + DpAccountEventSubscriber(const CommonEventSubscribeInfo &subscribeInfo, const AccountEventCallback &callback, + const std::vector &eventNameVec) : CommonEventSubscriber(subscribeInfo), + eventNameVec_(eventNameVec), callback_(callback) {} + ~DpAccountEventSubscriber() override = default; + std::vector GetSubscriberEventNameVec() const; + void OnReceiveEvent(const CommonEventData &data) override; + +private: + std::vector eventNameVec_; + AccountEventCallback callback_; +}; + +class DpAccountCommonEventManager { +public: + DpAccountCommonEventManager() = default; + ~DpAccountCommonEventManager(); + bool SubscribeAccountCommonEvent(const std::vector &eventNameVec, + const AccountEventCallback &callback); + bool UnsubscribeAccountCommonEvent(); + +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 AccountSubscriber) + : changeSubscriber_(AccountSubscriber) {} + ~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_DP_ACCOUNT_COMMON_EVENT_H diff --git a/services/core/src/multiusermanager/multi_user_manager.cpp b/services/core/src/multiusermanager/multi_user_manager.cpp new file mode 100644 index 00000000..7bfdb8e8 --- /dev/null +++ b/services/core/src/multiusermanager/multi_user_manager.cpp @@ -0,0 +1,81 @@ +/* + * 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 "multi_user_manager.h" + +#include "device_profile_manager.h" +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "profile_utils.h" + +#include "account_info.h" +#include "ohos_account_kits.h" +#include "os_account_manager.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(MultiUserManager); + +namespace { + const std::string TAG = "MultiUserManager"; +} + +int32_t MultiUserManager::Init() +{ + HILOGI("Init"); + int32_t foregroundId = DEFAULT_USER_ID; + int32_t res = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(foregroundId); + if (res != DP_SUCCESS || foregroundId == DEFAULT_USER_ID) { + HILOGE("GetForegroundId failed, res: %{public}d", res); + } + HILOGI("current foregroundId = %{public}d", foregroundId); + SetCurrentForegroundUserID(foregroundId); + + return DP_SUCCESS; +} + +int32_t MultiUserManager::UnInit() +{ + return DP_SUCCESS; +} + +void MultiUserManager::SetCurrentForegroundUserID(int32_t userId) +{ + std::lock_guard lock(foregroundUserIdLock_); + DeviceProfileManager::GetInstance().OnUserChange(foregroundUserId_, userId); + foregroundUserId_ = userId; +} + +int32_t MultiUserManager::GetCurrentForegroundUserID() +{ + std::lock_guard lock(foregroundUserIdLock_); + return foregroundUserId_; +} + +int32_t MultiUserManager::GetForegroundUserIDFromOs(int32_t& foregroundId) +{ + foregroundId = DEFAULT_USER_ID; + int32_t res = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(foregroundId); + if (res != DP_SUCCESS || foregroundId == DEFAULT_USER_ID) { + HILOGE("GetForegroundId failed, res:%{public}d", res); + return DP_GET_FOREGROUND_ID_FAIL; + } + HILOGI("GetForegroundUserIDFromOs foregroundId = %{public}d", foregroundId); + return DP_SUCCESS; +} + +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/core/src/publishcommonevent/dp_account_common_event.cpp b/services/core/src/publishcommonevent/dp_account_common_event.cpp new file mode 100644 index 00000000..3070a0a3 --- /dev/null +++ b/services/core/src/publishcommonevent/dp_account_common_event.cpp @@ -0,0 +1,181 @@ +/* + * 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 "dp_account_common_event.h" + +#include +#include + +#include "common_event_support.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "ffrt.h" +#include "iservice_registry.h" +#include "multi_user_manager.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using OHOS::EventFwk::MatchingSkills; +using OHOS::EventFwk::CommonEventManager; + +constexpr int32_t MAX_TRY_TIMES = 3; + +namespace { + const std::string TAG = "AccountCommonEvent"; +} + +std::vector DpAccountEventSubscriber::GetSubscriberEventNameVec() const +{ + return eventNameVec_; +} + +DpAccountCommonEventManager::~DpAccountCommonEventManager() +{ + DpAccountCommonEventManager::UnsubscribeAccountCommonEvent(); +} + +bool DpAccountCommonEventManager::SubscribeAccountCommonEvent(const std::vector &eventNameVec, + const AccountEventCallback &callback) +{ + if (eventNameVec.empty() || callback == nullptr) { + HILOGE("eventNameVec is empty or callback is nullptr."); + return false; + } + std::lock_guard locker(evenSubscriberMutex_); + if (eventValidFlag_) { + HILOGE("failed to subscribe account commom eventName size: %{public}zu", eventNameVec.size()); + return false; + } + + MatchingSkills matchingSkills; + for (auto &item : eventNameVec) { + matchingSkills.AddEvent(item); + } + CommonEventSubscribeInfo subscriberInfo(matchingSkills); + subscriber_ = std::make_shared(subscriberInfo, callback, eventNameVec); + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + HILOGE("samgrProxy is nullptr"); + subscriber_ = nullptr; + return false; + } + statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(subscriber_); + if (statusChangeListener_ == nullptr) { + HILOGE("statusChangeListener_ is nullptr"); + subscriber_ = nullptr; + return false; + } + while (counter_ != MAX_TRY_TIMES) { + if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) { + HILOGI("SubscribeAccountEvent success."); + counter_ = 0; + break; + } + if (++counter_ == MAX_TRY_TIMES) { + HILOGE("SubscribeAccountEvent failed."); + } + sleep(1); + } + eventNameVec_ = eventNameVec; + eventValidFlag_ = true; + HILOGI("success to subscribe account commom event name size: %{public}zu", eventNameVec.size()); + return true; +} + +bool DpAccountCommonEventManager::UnsubscribeAccountCommonEvent() +{ + std::lock_guard locker(evenSubscriberMutex_); + if (!eventValidFlag_) { + HILOGE("failed to unsubscribe account commom event name size: %{public}zu because event is invalid.", + eventNameVec_.size()); + return false; + } + if (subscriber_ != nullptr) { + HILOGI("start to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); + if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) { + HILOGE("failed to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); + return false; + } + HILOGI("success to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); + subscriber_ = nullptr; + } + if (statusChangeListener_ != nullptr) { + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + HILOGE("samgrProxy is nullptr"); + return false; + } + int32_t ret = samgrProxy->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_); + if (ret != ERR_OK) { + HILOGE("failed to unsubscribe system ability COMMON_EVENT_SERVICE_ID ret:%{public}d", ret); + return false; + } + statusChangeListener_ = nullptr; + } + + HILOGI("success to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); + eventValidFlag_ = false; + return true; +} + +void DpAccountEventSubscriber::OnReceiveEvent(const CommonEventData &data) +{ + std::string receiveEvent = data.GetWant().GetAction(); + HILOGI("Received account event: %{public}s", receiveEvent.c_str()); + int32_t userId = data.GetCode(); + bool accountValidEvent = false; + if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED || + receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) { + userId = data.GetCode(); + accountValidEvent = true; + } + if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT || + receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN) { + userId = data.GetWant().GetIntParam("userId", 0); + accountValidEvent = true; + } + if (userId <= 0 || !accountValidEvent) { + HILOGE("Invalid account type event."); + return; + } + ffrt::submit([=]() { callback_(userId, receiveEvent); }); +} + +void DpAccountCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility( + int32_t systemAbilityId, const std::string& deviceId) +{ + HILOGI("systemAbility is added with said: %{public}d.", systemAbilityId); + if (systemAbilityId != COMMON_EVENT_SERVICE_ID) { + return; + } + if (changeSubscriber_ == nullptr) { + HILOGE("failed to subscribe account commom event because changeSubscriber_ is nullptr."); + return; + } + std::vector eventNameVec = changeSubscriber_->GetSubscriberEventNameVec(); + HILOGI("start to subscribe account commom eventName: %{public}zu", eventNameVec.size()); + if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) { + HILOGE("failed to subscribe account commom event: %{public}zu", eventNameVec.size()); + } +} + +void DpAccountCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility( + int32_t systemAbilityId, const std::string& deviceId) +{ + HILOGI("systemAbility is removed with said: %{public}d.", systemAbilityId); +} +} // namespace DistributedHardware +} // namespace OHOS -- Gitee From 7380249b90eb7da8a6dfd0cb8b7fb7057975997e Mon Sep 17 00:00:00 2001 From: chenshuo Date: Thu, 7 Nov 2024 16:30:28 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=A4=9A=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=AE=A1=E7=90=86=E4=B8=8E=E4=BA=8B=E4=BB=B6=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenshuo --- .../multiusermanager/multi_user_manager.h | 49 +++++ .../dp_account_common_event.h | 83 ++++++++ .../multiusermanager/multi_user_manager.cpp | 81 ++++++++ .../dp_account_common_event.cpp | 181 ++++++++++++++++++ 4 files changed, 394 insertions(+) create mode 100644 services/core/include/multiusermanager/multi_user_manager.h create mode 100644 services/core/include/publishcommonevent/dp_account_common_event.h create mode 100644 services/core/src/multiusermanager/multi_user_manager.cpp create mode 100644 services/core/src/publishcommonevent/dp_account_common_event.cpp diff --git a/services/core/include/multiusermanager/multi_user_manager.h b/services/core/include/multiusermanager/multi_user_manager.h new file mode 100644 index 00000000..0605aabb --- /dev/null +++ b/services/core/include/multiusermanager/multi_user_manager.h @@ -0,0 +1,49 @@ +/* + * 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_DP_MULTIPLE_USER_CONNECTOR_H +#define OHOS_DP_MULTIPLE_USER_CONNECTOR_H + +#include +#include +#include +#include + +#include "iremote_object.h" +#include "distributed_device_profile_constants.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class MultiUserManager { + DECLARE_SINGLE_INSTANCE(MultiUserManager); +public: + int32_t Init(); + + int32_t UnInit(); + + int32_t GetCurrentForegroundUserID(); + + void SetCurrentForegroundUserID(int32_t userId); + + int32_t GetForegroundUserIDFromOs(int32_t& foregroundId); + +private: + int32_t foregroundUserId_ = DEFAULT_USER_ID; + std::mutex foregroundUserIdLock_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DP_MULTIPLE_USER_CONNECTOR_H diff --git a/services/core/include/publishcommonevent/dp_account_common_event.h b/services/core/include/publishcommonevent/dp_account_common_event.h new file mode 100644 index 00000000..17aed149 --- /dev/null +++ b/services/core/include/publishcommonevent/dp_account_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_DP_ACCOUNT_COMMON_EVENT_H +#define OHOS_DP_ACCOUNT_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 DistributedDeviceProfile { +using OHOS::EventFwk::CommonEventData; +using OHOS::EventFwk::CommonEventSubscriber; +using OHOS::EventFwk::CommonEventSubscribeInfo; +using AccountEventCallback = std::function; + +class DpAccountEventSubscriber : public CommonEventSubscriber { +public: + DpAccountEventSubscriber(const CommonEventSubscribeInfo &subscribeInfo, const AccountEventCallback &callback, + const std::vector &eventNameVec) : CommonEventSubscriber(subscribeInfo), + eventNameVec_(eventNameVec), callback_(callback) {} + ~DpAccountEventSubscriber() override = default; + std::vector GetSubscriberEventNameVec() const; + void OnReceiveEvent(const CommonEventData &data) override; + +private: + std::vector eventNameVec_; + AccountEventCallback callback_; +}; + +class DpAccountCommonEventManager { +public: + DpAccountCommonEventManager() = default; + ~DpAccountCommonEventManager(); + bool SubscribeAccountCommonEvent(const std::vector &eventNameVec, + const AccountEventCallback &callback); + bool UnsubscribeAccountCommonEvent(); + +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 AccountSubscriber) + : changeSubscriber_(AccountSubscriber) {} + ~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_DP_ACCOUNT_COMMON_EVENT_H diff --git a/services/core/src/multiusermanager/multi_user_manager.cpp b/services/core/src/multiusermanager/multi_user_manager.cpp new file mode 100644 index 00000000..7bfdb8e8 --- /dev/null +++ b/services/core/src/multiusermanager/multi_user_manager.cpp @@ -0,0 +1,81 @@ +/* + * 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 "multi_user_manager.h" + +#include "device_profile_manager.h" +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "profile_utils.h" + +#include "account_info.h" +#include "ohos_account_kits.h" +#include "os_account_manager.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(MultiUserManager); + +namespace { + const std::string TAG = "MultiUserManager"; +} + +int32_t MultiUserManager::Init() +{ + HILOGI("Init"); + int32_t foregroundId = DEFAULT_USER_ID; + int32_t res = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(foregroundId); + if (res != DP_SUCCESS || foregroundId == DEFAULT_USER_ID) { + HILOGE("GetForegroundId failed, res: %{public}d", res); + } + HILOGI("current foregroundId = %{public}d", foregroundId); + SetCurrentForegroundUserID(foregroundId); + + return DP_SUCCESS; +} + +int32_t MultiUserManager::UnInit() +{ + return DP_SUCCESS; +} + +void MultiUserManager::SetCurrentForegroundUserID(int32_t userId) +{ + std::lock_guard lock(foregroundUserIdLock_); + DeviceProfileManager::GetInstance().OnUserChange(foregroundUserId_, userId); + foregroundUserId_ = userId; +} + +int32_t MultiUserManager::GetCurrentForegroundUserID() +{ + std::lock_guard lock(foregroundUserIdLock_); + return foregroundUserId_; +} + +int32_t MultiUserManager::GetForegroundUserIDFromOs(int32_t& foregroundId) +{ + foregroundId = DEFAULT_USER_ID; + int32_t res = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(foregroundId); + if (res != DP_SUCCESS || foregroundId == DEFAULT_USER_ID) { + HILOGE("GetForegroundId failed, res:%{public}d", res); + return DP_GET_FOREGROUND_ID_FAIL; + } + HILOGI("GetForegroundUserIDFromOs foregroundId = %{public}d", foregroundId); + return DP_SUCCESS; +} + +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/core/src/publishcommonevent/dp_account_common_event.cpp b/services/core/src/publishcommonevent/dp_account_common_event.cpp new file mode 100644 index 00000000..0bce39f7 --- /dev/null +++ b/services/core/src/publishcommonevent/dp_account_common_event.cpp @@ -0,0 +1,181 @@ +/* + * 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 "dp_account_common_event.h" + +#include +#include + +#include "common_event_support.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "ffrt.h" +#include "iservice_registry.h" +#include "multi_user_manager.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using OHOS::EventFwk::MatchingSkills; +using OHOS::EventFwk::CommonEventManager; + +constexpr int32_t MAX_TRY_TIMES = 3; + +namespace { + const std::string TAG = "AccountCommonEvent"; +} + +std::vector DpAccountEventSubscriber::GetSubscriberEventNameVec() const +{ + return eventNameVec_; +} + +DpAccountCommonEventManager::~DpAccountCommonEventManager() +{ + DpAccountCommonEventManager::UnsubscribeAccountCommonEvent(); +} + +bool DpAccountCommonEventManager::SubscribeAccountCommonEvent(const std::vector &eventNameVec, + const AccountEventCallback &callback) +{ + if (eventNameVec.empty() || callback == nullptr) { + HILOGE("eventNameVec is empty or callback is nullptr."); + return false; + } + std::lock_guard locker(evenSubscriberMutex_); + if (eventValidFlag_) { + HILOGE("failed to subscribe account commom eventName size: %{public}zu", eventNameVec.size()); + return false; + } + + MatchingSkills matchingSkills; + for (auto &item : eventNameVec) { + matchingSkills.AddEvent(item); + } + CommonEventSubscribeInfo subscriberInfo(matchingSkills); + subscriber_ = std::make_shared(subscriberInfo, callback, eventNameVec); + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + HILOGE("samgrProxy is nullptr"); + subscriber_ = nullptr; + return false; + } + statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(subscriber_); + if (statusChangeListener_ == nullptr) { + HILOGE("statusChangeListener_ is nullptr"); + subscriber_ = nullptr; + return false; + } + while (counter_ != MAX_TRY_TIMES) { + if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) { + HILOGI("SubscribeAccountEvent success."); + counter_ = 0; + break; + } + if (++counter_ == MAX_TRY_TIMES) { + HILOGE("SubscribeAccountEvent failed."); + } + sleep(1); + } + eventNameVec_ = eventNameVec; + eventValidFlag_ = true; + HILOGI("success to subscribe account commom event name size: %{public}zu", eventNameVec.size()); + return true; +} + +bool DpAccountCommonEventManager::UnsubscribeAccountCommonEvent() +{ + std::lock_guard locker(evenSubscriberMutex_); + if (!eventValidFlag_) { + HILOGE("failed to unsubscribe account commom event name size: %{public}zu because event is invalid.", + eventNameVec_.size()); + return false; + } + if (subscriber_ != nullptr) { + HILOGI("start to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); + if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) { + HILOGE("failed to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); + return false; + } + HILOGI("success to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); + subscriber_ = nullptr; + } + if (statusChangeListener_ != nullptr) { + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + HILOGE("samgrProxy is nullptr"); + return false; + } + int32_t ret = samgrProxy->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_); + if (ret != ERR_OK) { + HILOGE("failed to unsubscribe system ability COMMON_EVENT_SERVICE_ID ret:%{public}d", ret); + return false; + } + statusChangeListener_ = nullptr; + } + + HILOGI("success to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size()); + eventValidFlag_ = false; + return true; +} + +void DpAccountEventSubscriber::OnReceiveEvent(const CommonEventData &data) +{ + std::string receiveEvent = data.GetWant().GetAction(); + HILOGI("Received account event: %{public}s", receiveEvent.c_str()); + int32_t userId = data.GetCode(); + bool isValidEvent = false; + if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED || + receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) { + userId = data.GetCode(); + isValidEvent = true; + } + if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT || + receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN) { + userId = data.GetWant().GetIntParam("userId", 0); + isValidEvent = true; + } + if (userId <= 0 || !isValidEvent) { + HILOGE("Invalid account type event."); + return; + } + ffrt::submit([=]() { callback_(userId, receiveEvent); }); +} + +void DpAccountCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility( + int32_t systemAbilityId, const std::string& deviceId) +{ + HILOGI("systemAbility is added with said: %{public}d.", systemAbilityId); + if (systemAbilityId != COMMON_EVENT_SERVICE_ID) { + return; + } + if (changeSubscriber_ == nullptr) { + HILOGE("failed to subscribe account commom event because changeSubscriber_ is nullptr."); + return; + } + std::vector eventNameVec = changeSubscriber_->GetSubscriberEventNameVec(); + HILOGI("start to subscribe account commom eventName: %{public}zu", eventNameVec.size()); + if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) { + HILOGE("failed to subscribe account commom event: %{public}zu", eventNameVec.size()); + } +} + +void DpAccountCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility( + int32_t systemAbilityId, const std::string& deviceId) +{ + HILOGI("systemAbility is removed with said: %{public}d.", systemAbilityId); +} +} // namespace DistributedHardware +} // namespace OHOS -- Gitee