From 4c0f4cf6cb371377b16dbedd433e8929bd768645 Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Wed, 11 May 2022 15:15:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=94=A8=E6=88=B7=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hhchinasoft --- services/distributedfiledaemon/BUILD.gn | 2 + .../include/multiuser/os_account_observer.h | 12 ++++-- .../distributedfiledaemon/src/ipc/daemon.cpp | 29 +++++++-------- .../src/multiuser/os_account_observer.cpp | 37 ++++++++++++------- 4 files changed, 46 insertions(+), 34 deletions(-) mode change 100755 => 100644 services/distributedfiledaemon/BUILD.gn diff --git a/services/distributedfiledaemon/BUILD.gn b/services/distributedfiledaemon/BUILD.gn old mode 100755 new mode 100644 index bf933bc..cf2b26c --- a/services/distributedfiledaemon/BUILD.gn +++ b/services/distributedfiledaemon/BUILD.gn @@ -44,6 +44,8 @@ ohos_shared_library("libdistributedfiledaemon") { "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", ] external_deps = [ + "ability_base:want", + "common_event_service:cesfwk_innerkits", "dataclassification:data_transit_mgr", "dsoftbus_standard:softbus_client", "ipc:ipc_core", diff --git a/services/distributedfiledaemon/include/multiuser/os_account_observer.h b/services/distributedfiledaemon/include/multiuser/os_account_observer.h index a7cc9e0..719a526 100644 --- a/services/distributedfiledaemon/include/multiuser/os_account_observer.h +++ b/services/distributedfiledaemon/include/multiuser/os_account_observer.h @@ -20,20 +20,24 @@ #include #include +#include "common_event_manager.h" +#include "common_event_subscribe_info.h" +#include "common_event_subscriber.h" +#include "common_event_support.h" +#include "matching_skills.h" #include "mountpoint/mount_point.h" -#include "os_account_manager.h" namespace OHOS { namespace Storage { namespace DistributedFile { static constexpr int MOUNT_POINT_NUM = 2; -class OsAccountObserver final : public AccountSA::OsAccountSubscriber { +class OsAccountObserver final : public EventFwk::CommonEventSubscriber { public: OsAccountObserver() = default; ~OsAccountObserver(); - explicit OsAccountObserver(const AccountSA::OsAccountSubscribeInfo &subscribeInfo); + explicit OsAccountObserver(const EventFwk::CommonEventSubscribeInfo &subscribeInfo); - void OnAccountsChanged(const int &id) override; + virtual void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override; private: void RemoveMPInfo(const int id); void AddMPInfo(const int id, const std::string &relativePath); diff --git a/services/distributedfiledaemon/src/ipc/daemon.cpp b/services/distributedfiledaemon/src/ipc/daemon.cpp index 8f9da6a..206b729 100644 --- a/services/distributedfiledaemon/src/ipc/daemon.cpp +++ b/services/distributedfiledaemon/src/ipc/daemon.cpp @@ -15,8 +15,9 @@ #include "ipc/daemon.h" +#include "common_event_manager.h" +#include "common_event_support.h" #include "mountpoint/mount_manager.h" -#include "os_account_manager.h" #include "system_ability_definition.h" #include "utils_log.h" @@ -42,17 +43,15 @@ void Daemon::PublishSA() void Daemon::RegisterOsAccount() { - OHOS::AccountSA::OsAccountSubscribeInfo osAccountSubscribeInfo; - osAccountSubscribeInfo.SetOsAccountSubscribeType(OHOS::AccountSA::OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVED); - osAccountSubscribeInfo.SetName("distributed_file_service"); - - subScriber_ = std::make_shared(osAccountSubscribeInfo); - int ret = OHOS::AccountSA::OsAccountManager::SubscribeOsAccount(subScriber_); - if (ret != 0) { - LOGE("register os account fail ret %{public}d", ret); - return; - } - LOGI("register os account success, ret %{public}d", ret); + LOGI("register os account enter"); + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED); + EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills); + subScriber_ = std::make_shared(subscribeInfo); + bool subRet = EventFwk::CommonEventManager::SubscribeCommonEvent(subScriber_); + LOGI("register os account success %{public}d", subRet); } void Daemon::OnStart() @@ -79,10 +78,8 @@ void Daemon::OnStop() LOGI("Begin to stop"); state_ = ServiceRunningState::STATE_NOT_START; registerToService_ = false; - int32_t ret = OHOS::AccountSA::OsAccountManager::UnsubscribeOsAccount(subScriber_); - if (ret != 0) { - LOGI("UnsubscribeOsAccount failed, ret %{public}d", ret); - } + bool subRet = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subScriber_); + LOGI("unregister os account success %{public}d", subRet); subScriber_ = nullptr; LOGI("Stop finished successfully"); } diff --git a/services/distributedfiledaemon/src/multiuser/os_account_observer.cpp b/services/distributedfiledaemon/src/multiuser/os_account_observer.cpp index accf450..592f432 100644 --- a/services/distributedfiledaemon/src/multiuser/os_account_observer.cpp +++ b/services/distributedfiledaemon/src/multiuser/os_account_observer.cpp @@ -15,6 +15,8 @@ #include "multiuser/os_account_observer.h" +#include "common_event_manager.h" +#include "common_event_support.h" #include "device/device_manager_agent.h" #include "dfsu_mount_argument_descriptors.h" #include "utils_log.h" @@ -29,8 +31,8 @@ static const std::string ACCOUNT_LESS = "non_account"; static constexpr int DEFAULT_ACCOUNT = 100; } // namespace -OsAccountObserver::OsAccountObserver(const AccountSA::OsAccountSubscribeInfo &subscribeInfo) - : OsAccountSubscriber(subscribeInfo) +OsAccountObserver::OsAccountObserver(const EventFwk::CommonEventSubscribeInfo &subscribeInfo) + : EventFwk::CommonEventSubscriber(subscribeInfo) { LOGI("init first to create network of default user"); lock_guard lock(serializer_); @@ -52,20 +54,27 @@ void OsAccountObserver::AddMPInfo(const int id, const std::string &relativePath) mountPoints_[id].emplace_back(smp); } -void OsAccountObserver::OnAccountsChanged(const int &id) +void OsAccountObserver::OnReceiveEvent(const EventFwk::CommonEventData &eventData) { - LOGI("user id changed to %{public}d", id); - lock_guard lock(serializer_); - if (curUsrId != -1) { - // first stop curUsrId network - RemoveMPInfo(curUsrId); - } + const AAFwk::Want& want = eventData.GetWant(); + std::string action = want.GetAction(); + LOGI("AccountSubscriber: OnReceiveEvent action:%{public}s.", action.c_str()); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { + int32_t id = eventData.GetCode(); + LOGI("user id changed to %{public}d", id); + lock_guard lock(serializer_); + if (curUsrId != -1) { + // first stop curUsrId network + RemoveMPInfo(curUsrId); + } - // then start new network - curUsrId = id; - AddMPInfo(id, SAME_ACCOUNT); - AddMPInfo(id, ACCOUNT_LESS); - LOGI("user id %{public}d, add network done", curUsrId); + // then start new network + curUsrId = id; + AddMPInfo(id, SAME_ACCOUNT); + AddMPInfo(id, ACCOUNT_LESS); + LOGI("user id %{public}d, add network done", curUsrId); + } + LOGI("user changed OnReceiveEvent"); } void OsAccountObserver::RemoveMPInfo(const int id) -- Gitee