diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.cpp b/services/distributeddataservice/service/data_share/common/db_delegate.cpp index 7753dbb0ce885bf8b38556aeea06c963ef868413..b6eb963ea188533079b0a3cff0f0f7518ad8f01e 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/db_delegate.cpp @@ -80,6 +80,35 @@ std::shared_ptr DBDelegate::Create(DistributedData::StoreMetaData &m return nullptr; } +bool DBDelegate::Delete(DistributedData::StoreMetaData &metaData) +{ + // check deactivating. + if (Account::GetInstance()->IsDeactivating(atoi(metaData.user.c_str()))) { + ZLOGW("user %{public}s is deactivating, storeName: %{public}s", metaData.user.c_str(), + StringUtils::GeneralAnonymous(metaData.GetStoreAlias()).c_str()); + return false; + } + + // delete function. + auto eraseFunc = [&metaData](auto &, std::map> &stores) -> bool { + auto it = stores.find(metaData.storeId); + if (it != stores.end()) { + stores.erase(it); + return true; + } + return false; + }; + + bool result = false; + if (metaData.isEncrypt) { + result = storesEncrypt_.Compute(metaData.tokenId, eraseFunc); + } else { + result = stores_.Compute(metaData.tokenId, eraseFunc); + } + + return result; +} + void DBDelegate::SetExecutorPool(std::shared_ptr executor) { executor_ = std::move(executor); diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.h b/services/distributeddataservice/service/data_share/common/db_delegate.h index 804a6763347184e02025e585734ca5906355997c..822cca830887dd0b1553fcdd0de6532058556206 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.h +++ b/services/distributeddataservice/service/data_share/common/db_delegate.h @@ -37,6 +37,7 @@ public: using Filter = std::function; static std::shared_ptr Create(DistributedData::StoreMetaData &metaData, const std::string &extUri = "", const std::string &backup = ""); + static bool Delete(DistributedData::StoreMetaData &metaData); virtual bool Init(const DistributedData::StoreMetaData &meta, int version, bool registerFunction, const std::string &extUri, const std::string &backup) = 0; static void Close(const Filter &filter); diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index d1b45596bad024345fbd5d991c57b8f3aa0c1475..1251d80a6e283e75174c4c8a2109b416205d8c77 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -611,6 +611,7 @@ int32_t DataShareServiceImpl::OnBind(const BindInfo &binderInfo) SubscribeConcurrentTask(); SubscribeTimeChanged(); SubscribeChange(); + SubscribeListen(); auto task = [](const std::string &bundleName, int32_t userId, const std::string &storeName) { return BundleMgrProxy::GetInstance()->IsConfigSilentProxy(bundleName, userId, storeName); }; @@ -619,6 +620,29 @@ int32_t DataShareServiceImpl::OnBind(const BindInfo &binderInfo) return E_OK; } +void DataShareServiceImpl::SubscribeListen() +{ + MetaDataManager::GetInstance().Subscribe( + StoreMetaData::GetPrefix({}), [this](const std::string &key, const std::string &value, int32_t flag) -> auto { + if (flag != MetaDataManager::DELETE) { + return false; + } + if (value.empty()) { + return false; + } + StoreMetaData meta; + if (!(StoreMetaData::Unmarshall(value, meta))) { + ZLOGE("SubscribeListen Unmarshall failed!"); + return false; + } + if (!(DBDelegate::Delete(meta))) { + ZLOGE("SubscribeListen DBDelegate Delete failed!"); + return false; + } + return true; + }, true); +} + void DataShareServiceImpl::SubscribeCommonEvent() { sptr systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.h b/services/distributeddataservice/service/data_share/data_share_service_impl.h index c86cf2fa26163fe1dae231d1ce204d46df9717f4..075158565d7a0eed5539ea8c25961137986dbd51 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.h +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.h @@ -128,6 +128,7 @@ private: }; void RegisterDataShareServiceInfo(); void RegisterHandler(); + void SubscribeListen(); bool SubscribeTimeChanged(); bool NotifyChange(const std::string &uri, int32_t userId); bool GetCallerBundleName(std::string &bundleName);