diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.cpp b/services/distributeddataservice/service/data_share/common/db_delegate.cpp index 9e77bcbb4dece55fc62f1ac907b7be2f95bbed01..b53cf56aef682690bb0125170827cc49fc2ca3c5 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/db_delegate.cpp @@ -77,6 +77,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..93900edd178c3103c49f8aab9dbe612e4fd686d4 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); @@ -129,7 +130,7 @@ public: static constexpr const char *DATA_TABLE = "data_"; static constexpr const char *PROXYDATA_TABLE = "proxydata_"; static std::shared_ptr GetInstance( - const std::string &dir = "", const std::shared_ptr &executors = nullptr); + const std::string &dir = "", const std::shared_ptr &executors = nullptr); virtual ~KvDBDelegate() = default; virtual std::pair Upsert(const std::string &collectionName, const KvData &value) = 0; virtual std::pair Delete(const std::string &collectionName, const std::string &filter) = 0; 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..3647c97012c36a98b3f7962bc92399d67c94ca9c 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.h +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.h @@ -137,6 +137,7 @@ private: int32_t GetBMSAndMetaDataStatus(const std::string &uri, const int32_t tokenId); void SubscribeCommonEvent(); void SubscribeConcurrentTask(); + void SubscribeListen(); static void InitSubEvent(); void AutoLaunch(const DistributedData::Event &event); void SubscribeChange();