From a67d25fa025b7c460fa3d93d20e249a3d87c0314 Mon Sep 17 00:00:00 2001 From: wangkun Date: Thu, 10 Jun 2021 20:24:13 +0800 Subject: [PATCH 1/2] open L2 add powermgr Signed-off-by: wangkun --- services/distributeddataservice/app/BUILD.gn | 5 ++- .../app/src/backup_handler.cpp | 32 ++++++++++++++++++- .../app/src/backup_handler.h | 7 ++-- .../distributeddataservice/app/test/BUILD.gn | 8 +++++ 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 38b166f4c..fcdd00c7c 100755 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -102,13 +102,12 @@ ohos_shared_library("distributeddataservice") { "appexecfwk_standard:appexecfwk_base", "appexecfwk_standard:appexecfwk_core", - # "battery_manager:batterysrv_client", + "battery_manager_native:batterysrv_client", "aafwk_standard:base", "aafwk_standard:intent", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", - - # "power_manager:powermgr_client", + "power_manager_native:powermgr_client", "safwk:system_ability_fwk", "samgr_L2:samgr_proxy", "startup_l2:syspara", diff --git a/services/distributeddataservice/app/src/backup_handler.cpp b/services/distributeddataservice/app/src/backup_handler.cpp index e10aeb245..85962c626 100755 --- a/services/distributeddataservice/app/src/backup_handler.cpp +++ b/services/distributeddataservice/app/src/backup_handler.cpp @@ -21,6 +21,8 @@ #include #include #include "account_delegate.h" +#include "battery_info.h" +#include "battery_srv_client.h" #include "constant.h" #include "crypto_utils.h" #include "kv_store_delegate_manager.h" @@ -28,6 +30,8 @@ #include "kvstore_data_service.h" #include "log_print.h" #include "kvstore_meta_manager.h" +#include "power_mgr_client.h" +#include "time_utils.h" namespace OHOS::DistributedKv { using json = nlohmann::json; @@ -71,6 +75,7 @@ void BackupHandler::BackSchedule() SingleKvStoreBackup(entry.second); } } + backupSuccessTime_ = TimeUtils::CurrentTimeMicros(); }); } @@ -353,7 +358,32 @@ bool BackupHandler::FileExists(const std::string &path) bool BackupHandler::CheckNeedBackup() { - return false; + auto &batterySrvClient = PowerMgr::BatterySrvClient::GetInstance(); + auto chargingStatus = batterySrvClient.GetChargingStatus(); + if (chargingStatus != PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE) { + if (chargingStatus != PowerMgr::BatteryChargeState::CHARGE_STATE_FULL) { + ZLOGE("the device is not in charge state, chargingStatus=%{public}d.", chargingStatus); + return false; + } + auto batteryPluggedType = batterySrvClient.GetPluggedType(); + if (batteryPluggedType != PowerMgr::BatteryPluggedType::PLUGGED_TYPE_AC && + batteryPluggedType != PowerMgr::BatteryPluggedType::PLUGGED_TYPE_USB && + batteryPluggedType != PowerMgr::BatteryPluggedType::PLUGGED_TYPE_WIRELESS) { + ZLOGE("the device is not in charge full statue, the batteryPluggedType is %{public}d.", batteryPluggedType); + return false; + } + } + auto &powerMgrClient = PowerMgr::PowerMgrClient::GetInstance(); + if (powerMgrClient.IsScreenOn()) { + ZLOGE("the device screen is on."); + return false; + } + int64_t currentTime = TimeUtils::CurrentTimeMicros(); + if (currentTime - backupSuccessTime_ < 36000000 && backupSuccessTime_ > 0) { // 36000000 is 10 hours + ZLOGE("no more than 10 hours since the last backup success."); + return false; + } + return true; } std::string BackupHandler::GetHashedBackupName(const std::string &bundleName) diff --git a/services/distributeddataservice/app/src/backup_handler.h b/services/distributeddataservice/app/src/backup_handler.h index 408dbe99b..39485c91a 100755 --- a/services/distributeddataservice/app/src/backup_handler.h +++ b/services/distributeddataservice/app/src/backup_handler.h @@ -41,10 +41,13 @@ public: static bool FileExists(const std::string &path); static std::string GetHashedBackupName(const std::string &bundleName); private: - KvScheduler scheduler_ {}; + bool CheckNeedBackup(); + static std::string backupDirCe_; static std::string backupDirDe_; - bool CheckNeedBackup(); + + KvScheduler scheduler_ {}; + int64_t backupSuccessTime_ = 0; }; } // namespace OHOS::DistributedKv #endif // DISTRIBUTEDDATAMGR_BACKUP_HANDLER_H diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index 532e5ca46..8e087294f 100755 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -34,6 +34,7 @@ config("module_private_config") { "../src/flowctrl_manager", "../../../../interfaces/innerkits/distributeddata", "//third_party/json/single_include", + "//base/powermgr/power_manager/interfaces/innerkits/native/include", ] } @@ -65,6 +66,7 @@ ohos_unittest("KvStoreImplLogicalIsolationTest") { external_deps = [ "battery_manager_native:batterysrv_client", + "power_manager_native:powermgr_client", "hiviewdfx_hilog_native:libhilog", "huks_standard:libhukssdk", "ipc:ipc_core", @@ -116,6 +118,7 @@ ohos_unittest("KvStoreImplPhysicalIsolationTest") { external_deps = [ "battery_manager_native:batterysrv_client", + "power_manager_native:powermgr_client", "hiviewdfx_hilog_native:libhilog", "huks_standard:libhukssdk", "ipc:ipc_core", @@ -167,6 +170,7 @@ ohos_unittest("KvStoreDataServiceTest") { external_deps = [ "battery_manager_native:batterysrv_client", + "power_manager_native:powermgr_client", "hiviewdfx_hilog_native:libhilog", "huks_standard:libhukssdk", "ipc:ipc_core", @@ -218,6 +222,7 @@ ohos_unittest("KvStoreBackupTest") { external_deps = [ "battery_manager_native:batterysrv_client", + "power_manager_native:powermgr_client", "hiviewdfx_hilog_native:libhilog", "huks_standard:libhukssdk", "ipc:ipc_core", @@ -251,6 +256,7 @@ ohos_unittest("KvStoreFlowCtrlManagerTest") { external_deps = [ "battery_manager_native:batterysrv_client", + "power_manager_native:powermgr_client", "hiviewdfx_hilog_native:libhilog", "huks_standard:libhukssdk", "ipc:ipc_core", @@ -295,6 +301,7 @@ ohos_unittest("KvStoreSyncManagerTest") { external_deps = [ "battery_manager_native:batterysrv_client", + "power_manager_native:powermgr_client", "hiviewdfx_hilog_native:libhilog", "huks_standard:libhukssdk", "ipc:ipc_core", @@ -362,6 +369,7 @@ ohos_unittest("KvStoreUninstallerTest") { "aafwk_standard:base", "aafwk_standard:intent", "battery_manager_native:batterysrv_client", + "power_manager_native:powermgr_client", "hiviewdfx_hilog_native:libhilog", "huks_standard:libhukssdk", "ipc:ipc_core", -- Gitee From 4e10dfda142ece651fccd6849b3abe19071dba82 Mon Sep 17 00:00:00 2001 From: wangkun Date: Fri, 11 Jun 2021 16:05:49 +0800 Subject: [PATCH 2/2] Event interface adaptation Signed-off-by: wangkun --- .../adapter/account/BUILD.gn | 4 +- .../account/src/account_delegate_impl.cpp | 73 ++++++++++++ .../account/src/account_delegate_impl.h | 18 ++- .../adapter/broadcaster/BUILD.gn | 5 +- .../broadcaster/src/broadcast_sender_impl.cpp | 39 ++++++- .../app/src/uninstaller/BUILD.gn | 6 +- .../app/src/uninstaller/uninstaller.h | 4 +- .../app/src/uninstaller/uninstaller_impl.cpp | 104 +++++++++++++++++- .../app/src/uninstaller/uninstaller_impl.h | 21 +++- 9 files changed, 257 insertions(+), 17 deletions(-) diff --git a/services/distributeddataservice/adapter/account/BUILD.gn b/services/distributeddataservice/adapter/account/BUILD.gn index 8caa97f9e..67d8637c3 100755 --- a/services/distributeddataservice/adapter/account/BUILD.gn +++ b/services/distributeddataservice/adapter/account/BUILD.gn @@ -39,10 +39,10 @@ ohos_static_library("distributeddata_account_static") { "appexecfwk_standard:appexecfwk_base", # "ces:libcommonevent", - "aafwk_standard:base", - "aafwk_standard:intent", + "aafwk_standard:want", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "os_account_standard:libaccountkits", + "ces_standard:cesfwk_kits", ] } diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_impl.cpp b/services/distributeddataservice/adapter/account/src/account_delegate_impl.cpp index 89aa501be..ae07da2bc 100755 --- a/services/distributeddataservice/adapter/account/src/account_delegate_impl.cpp +++ b/services/distributeddataservice/adapter/account/src/account_delegate_impl.cpp @@ -25,6 +25,41 @@ namespace OHOS { namespace DistributedKv { +using namespace OHOS::EventFwk; +using namespace OHOS::AAFwk; +EventSubscriber::EventSubscriber(const CommonEventSubscribeInfo &info) : CommonEventSubscriber(info) {} + +void EventSubscriber::OnReceiveEvent(const CommonEventData &event) +{ + const auto want = event.GetWant(); + AccountEventInfo accountEventInfo {}; + std::string action = want.GetAction(); + ZLOGI("Want Action is %s", action.c_str()); + + if (action == CommonEventSupport::COMMON_EVENT_HWID_LOGIN) { + accountEventInfo.status = AccountStatus::HARMONY_ACCOUNT_LOGIN; + } else if (action == CommonEventSupport::COMMON_EVENT_HWID_LOGOUT) { + accountEventInfo.status = AccountStatus::HARMONY_ACCOUNT_LOGOUT; + } else if (action == CommonEventSupport::COMMON_EVENT_HWID_TOKEN_INVALID) { + accountEventInfo.status = AccountStatus::HARMONY_ACCOUNT_DELETE; + } else if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) { + accountEventInfo.status = AccountStatus::DEVICE_ACCOUNT_DELETE; + accountEventInfo.deviceAccountId = + std::to_string(want.GetIntParam(CommonEventSupport::COMMON_EVENT_USER_REMOVED, -1)); + if (accountEventInfo.deviceAccountId == "-1") { + return; + } + } else { + return; + } + eventCallback_(accountEventInfo); +} + +void EventSubscriber::SetEventCallback(EventCallback callback) +{ + eventCallback_ = callback; +} + AccountDelegate::BaseInstance AccountDelegate::getInstance_ = AccountDelegateImpl::GetBaseInstance; AccountDelegateImpl *AccountDelegateImpl::GetInstance() @@ -42,11 +77,49 @@ AccountDelegateImpl::~AccountDelegateImpl() { ZLOGE("destruct"); observerMap_.Clear(); + const auto result = CommonEventManager::UnSubscribeCommonEvent(eventSubscriber_); + if (!result) { + ZLOGE("Fail to unregister account event listener!"); + } } void AccountDelegateImpl::SubscribeAccountEvent() { + ZLOGI("Subscribe account event listener start."); + MatchingSkills matchingSkills; + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_HWID_LOGIN); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_HWID_LOGOUT); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_HWID_TOKEN_INVALID); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED); + CommonEventSubscribeInfo info(matchingSkills); + eventSubscriber_ = std::make_shared(info); + eventSubscriber_->SetEventCallback([&](AccountEventInfo &account) { + account.harmonyAccountId = GetCurrentHarmonyAccountId(); + NotifyAccountChanged(account); + }); + std::thread th = std::thread([&]() { + int tryTimes = 0; + constexpr int MAX_RETRY_TIME = 300; + constexpr int RETRY_WAIT_TIME_S = 1; + + // we use this method to make sure register success + while (tryTimes < MAX_RETRY_TIME) { + auto result = CommonEventManager::SubscribeCommonEvent(eventSubscriber_); + if (result) { + break; + } + + ZLOGE("EventManager: Fail to register subscriber, error:%d", result); + sleep(RETRY_WAIT_TIME_S); + tryTimes++; + } + if (tryTimes == MAX_RETRY_TIME) { + ZLOGE("EventManager: Fail to register subscriber!"); + } + ZLOGI("EventManager: Success to register subscriber."); + }); + th.detach(); } std::string AccountDelegateImpl::GetCurrentHarmonyAccountId(const std::string &bundleName) const diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_impl.h b/services/distributeddataservice/adapter/account/src/account_delegate_impl.h index 3c706bfcb..0059bedab 100755 --- a/services/distributeddataservice/adapter/account/src/account_delegate_impl.h +++ b/services/distributeddataservice/adapter/account/src/account_delegate_impl.h @@ -19,12 +19,27 @@ #include "account_delegate.h" #include #include +#include "common_event_manager.h" +#include "common_event_subscriber.h" +#include "common_event_support.h" #include "concurrent_map.h" -#include "ohos/aafwk/content/intent.h" #include "log_print.h" namespace OHOS { namespace DistributedKv { +using namespace OHOS::EventFwk; +using EventCallback = std::function; + +class EventSubscriber final : public CommonEventSubscriber { +public: + ~EventSubscriber() {} + explicit EventSubscriber(const CommonEventSubscribeInfo &info); + void SetEventCallback(EventCallback callback); + void OnReceiveEvent(const CommonEventData &event) override; +private: + EventCallback eventCallback_ {}; +}; + class AccountDelegateImpl final : public AccountDelegate { public: static AccountDelegateImpl *GetInstance(); @@ -39,6 +54,7 @@ private: void NotifyAccountChanged(const AccountEventInfo &accountEventInfo); ConcurrentMap> observerMap_ {}; + std::shared_ptr eventSubscriber_ {}; }; } // namespace DistributedKv } // namespace OHOS diff --git a/services/distributeddataservice/adapter/broadcaster/BUILD.gn b/services/distributeddataservice/adapter/broadcaster/BUILD.gn index ef3730e65..714a88514 100755 --- a/services/distributeddataservice/adapter/broadcaster/BUILD.gn +++ b/services/distributeddataservice/adapter/broadcaster/BUILD.gn @@ -20,6 +20,7 @@ ohos_static_library("distributeddata_broadcaster_static") { include_dirs = [ "../include/broadcaster", + "../include/log", "./src", "//utils/native/base/include", "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata/include", @@ -31,10 +32,10 @@ ohos_static_library("distributeddata_broadcaster_static") { external_deps = [ # "ces:libcommonevent", - "aafwk_standard:base", - "aafwk_standard:intent", + "aafwk_standard:want", "appexecfwk_standard:appexecfwk_base", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", + "ces_standard:cesfwk_kits", ] } diff --git a/services/distributeddataservice/adapter/broadcaster/src/broadcast_sender_impl.cpp b/services/distributeddataservice/adapter/broadcaster/src/broadcast_sender_impl.cpp index 6d8fddf19..b3fe7cdb2 100755 --- a/services/distributeddataservice/adapter/broadcaster/src/broadcast_sender_impl.cpp +++ b/services/distributeddataservice/adapter/broadcaster/src/broadcast_sender_impl.cpp @@ -16,13 +16,48 @@ #define LOG_TAG "BroadcastSenderImpl" #include "broadcast_sender_impl.h" +#include "common_event_manager.h" +#include "common_event_support.h" +#include "log_print.h" #include "ohos/aafwk/base/array_wrapper.h" #include "ohos/aafwk/base/string_wrapper.h" -#include "ohos/aafwk/content/intent.h" namespace OHOS::DistributedKv { -void BroadcastSenderImpl::SendEvent(const EventParams ¶ms) +using namespace OHOS::EventFwk; +using namespace OHOS::AAFwk; +class CommonEventSubscriberListener : public CommonEventSubscriber { +public: + explicit CommonEventSubscriberListener(const CommonEventSubscribeInfo &subscriberInfo); + virtual ~CommonEventSubscriberListener(){}; + virtual void OnReceiveEvent(const CommonEventData &data); +}; + +CommonEventSubscriberListener::CommonEventSubscriberListener(const CommonEventSubscribeInfo &subscriberInfo) + : CommonEventSubscriber(subscriberInfo) +{} + +void CommonEventSubscriberListener::OnReceiveEvent(const CommonEventData &data) { + ZLOGI("receive event."); +} +void BroadcastSenderImpl::SendEvent(const EventParams ¶ms) +{ + ZLOGI("SendEvent code."); + bool result = false; + WantParams parameters; + parameters.SetParam(PKG_NAME, String::Box(params.appId)); + Want want; + want.SetAction(ACTION_NAME).SetParams(parameters); + CommonEventData commonEventData(want); + MatchingSkills matchingSkills; + matchingSkills.AddEvent(ACTION_NAME); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + auto subscriberPtr = std::make_shared(subscribeInfo); + if (CommonEventManager::SubscribeCommonEvent(subscriberPtr)) { + result = CommonEventManager::PublishCommonEvent(commonEventData); + } + CommonEventManager::UnSubscribeCommonEvent(subscriberPtr); + ZLOGI("SendEvent result:%{public}d.", result); } } diff --git a/services/distributeddataservice/app/src/uninstaller/BUILD.gn b/services/distributeddataservice/app/src/uninstaller/BUILD.gn index f57f3316a..928e6da1a 100755 --- a/services/distributeddataservice/app/src/uninstaller/BUILD.gn +++ b/services/distributeddataservice/app/src/uninstaller/BUILD.gn @@ -33,6 +33,7 @@ ohos_static_library("distributeddata_uninstaller_static") { deps = [ "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb:distributeddb", "//utils/native/base:utils", ] @@ -40,11 +41,12 @@ ohos_static_library("distributeddata_uninstaller_static") { "appexecfwk_standard:appexecfwk_base", # "ces:libcommonevent", - "aafwk_standard:base", - "aafwk_standard:intent", + "aafwk_standard:want", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr_L2:samgr_proxy", + "ces_standard:cesfwk_kits", + "dmsfwk_L2:zuri" ] } diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller.h b/services/distributeddataservice/app/src/uninstaller/uninstaller.h index 9fdcb9d6c..8895d473e 100755 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller.h +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller.h @@ -19,13 +19,13 @@ #include #include -#include "ikvstore_data_service.h" +#include "kvstore_data_service.h" #include "visibility.h" namespace OHOS::DistributedKv { class Uninstaller { public: - KVSTORE_API virtual Status Init(IKvStoreDataService *kvStoreDataService) = 0; + KVSTORE_API virtual Status Init(KvStoreDataService *kvStoreDataService) = 0; KVSTORE_API virtual ~Uninstaller() {}; KVSTORE_API static Uninstaller &GetInstance(); }; diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index d8c7459d3..60104ce49 100755 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -16,19 +16,117 @@ #define LOG_TAG "UninstallerImpl" #include "uninstaller_impl.h" +#include +#include +#include "common_event_manager.h" +#include "common_event_support.h" +#include "kvstore_meta_manager.h" +#include "log_print.h" +#include "ohos/aafwk/base/array_wrapper.h" +#include "ohos/aafwk/base/string_wrapper.h" namespace OHOS::DistributedKv { +using namespace OHOS::EventFwk; +using namespace OHOS::AAFwk; +const std::string UninstallEventSubscriber::USER_ID = "userId"; const std::string PACKAGE_SCHEME = "package"; const std::string SCHEME_SPLIT = ":"; const std::string EXTRA_REPLACING = "intent.extra.REPLACING"; -UninstallerImpl::~UninstallerImpl() +UninstallEventSubscriber::UninstallEventSubscriber(const CommonEventSubscribeInfo &info, + UninstallEventCallback callback) + : CommonEventSubscriber(info), callback_(callback) +{} + +void UninstallEventSubscriber::OnReceiveEvent(const CommonEventData &event) { + ZLOGI("Intent Action Rec"); + Want want = event.GetWant(); + std::string action = want.GetAction(); + if (action != CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { + return; + } + + auto params = want.GetParams(); + auto ¶mMap = params.GetParams(); + for (auto &[key, value] : paramMap) { + size_t pos = key.rfind(EXTRA_REPLACING); + if (pos != std::string::npos) { + if (want.GetBoolParam(key, false)) { + ZLOGI("is update package!"); + return; + } + break; + } + } + auto uri = want.GetUri().ToString(); + if (uri.size() < (PACKAGE_SCHEME.size() + SCHEME_SPLIT.size())) { + ZLOGW("invalid intent Uri!"); + return; + } + std::string bundleName = uri.substr(PACKAGE_SCHEME.size() + SCHEME_SPLIT.size()); + ZLOGI("bundleName is %s", bundleName.c_str()); + + int userId = want.GetIntParam(USER_ID, -1); + callback_(bundleName, userId); } -Status UninstallerImpl::Init(IKvStoreDataService *kvStoreDataService) +UninstallerImpl::~UninstallerImpl() { - return Status::ERROR; + auto code = CommonEventManager::UnSubscribeCommonEvent(subscriber_); + if (!code) { + ZLOGW("unsubscribe failed code=%d", code); + } +} + +Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService) +{ + if (kvStoreDataService == nullptr) { + ZLOGW("kvStoreDataService is null."); + return Status::INVALID_ARGUMENT; + } + MatchingSkills matchingSkills; + matchingSkills.AddScheme(PACKAGE_SCHEME); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); + CommonEventSubscribeInfo info(matchingSkills); + auto callback = [kvStoreDataService](const std::string &bundleName, int userId) { + ZLOGI("uninstalled bundleName:%s, userId:%d", bundleName.c_str(), userId); + KvStoreMetaData kvStoreMetaData; + if (!KvStoreMetaManager::GetInstance().GetKvStoreMetaDataByBundleName(bundleName, kvStoreMetaData)) { + return; + } + if (!kvStoreMetaData.appId.empty() && !kvStoreMetaData.storeId.empty()) { + ZLOGI("Has been uninstalled bundleName:%s", bundleName.c_str()); + AppId appid = {kvStoreMetaData.bundleName}; + StoreId storeId = {kvStoreMetaData.storeId}; + kvStoreDataService->DeleteKvStore(appid, storeId, kvStoreMetaData.appId); + } + }; + subscriber_ = std::make_shared(info, callback); + std::thread th = std::thread([this] { + int tryTimes = 0; + constexpr int MAX_RETRY_TIME = 300; + constexpr int RETRY_WAIT_TIME_S = 1; + + // we use this method to make sure regist success + while (tryTimes < MAX_RETRY_TIME) { + auto result = CommonEventManager::SubscribeCommonEvent(subscriber_); + if (result) { + ZLOGI("EventManager: Success"); + break; + } else { + ZLOGE("EventManager: Fail to Register Subscriber, error:%d", result); + sleep(RETRY_WAIT_TIME_S); + } + tryTimes++; + } + if (MAX_RETRY_TIME == tryTimes) { + ZLOGE("EventManager: Fail to Register Subscriber!!!"); + } + ZLOGI("Register listener End!!"); + }); + th.detach(); + return Status::SUCCESS; } } diff --git a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h index 5a47a468a..a3c8bf064 100755 --- a/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h +++ b/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h @@ -16,16 +16,31 @@ #ifndef DISTRIBUTEDDATAMGR_UNINSTALLER_IMPL_H #define DISTRIBUTEDDATAMGR_UNINSTALLER_IMPL_H +#include "common_event_subscriber.h" #include "uninstaller.h" namespace OHOS::DistributedKv { -class UninstallerImpl : public Uninstaller { +using namespace OHOS::EventFwk; +using UninstallEventCallback = std::function; + +class UninstallEventSubscriber : public CommonEventSubscriber { public: - UninstallerImpl() {}; + UninstallEventSubscriber(const CommonEventSubscribeInfo &info, + UninstallEventCallback callback); + ~UninstallEventSubscriber() {}; + void OnReceiveEvent(const CommonEventData &event) override; +private: + static const std::string USER_ID; + UninstallEventCallback callback_; +}; +class UninstallerImpl : public Uninstaller { +public: ~UninstallerImpl(); - Status Init(IKvStoreDataService *kvStoreDataService) override; + Status Init(KvStoreDataService *kvStoreDataService) override; +private: + std::shared_ptr subscriber_ {}; }; } #endif // DISTRIBUTEDDATAMGR_UNINSTALLER_IMPL_H -- Gitee