From 7efb41c2e577c8412e09ef2cc99f23b322cf2498 Mon Sep 17 00:00:00 2001 From: louzhihao Date: Sat, 5 Apr 2025 17:00:48 +0800 Subject: [PATCH] user Signed-off-by: louzhihao Change-Id: I0f361edc8cde113bdc7dd7336e3e32c581714d25 --- .../service/data_share/common/context.h | 1 + .../data_share/data_share_service_impl.cpp | 35 ++++++++++--------- .../data_share/data_share_service_impl.h | 2 +- ...d_config_from_data_proxy_node_strategy.cpp | 2 +- ...g_from_data_share_bundle_info_strategy.cpp | 2 +- .../general/load_config_common_strategy.cpp | 3 +- .../load_config_data_info_strategy.cpp | 4 +-- .../strategies/get_data_strategy.cpp | 2 +- .../strategies/publish_strategy.cpp | 4 +-- .../published_data_subscriber_manager.cpp | 22 +++++++----- .../published_data_subscriber_manager.h | 12 ++++--- .../rdb_subscriber_manager.cpp | 30 +++++++++------- .../rdb_subscriber_manager.h | 3 +- .../test/data_share_service_impl_test.cpp | 4 +-- .../data_share_subscriber_managers_test.cpp | 6 ++-- 15 files changed, 76 insertions(+), 56 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/context.h b/services/distributeddataservice/service/data_share/common/context.h index 62da611a2..d19ebf6d1 100644 --- a/services/distributeddataservice/service/data_share/common/context.h +++ b/services/distributeddataservice/service/data_share/common/context.h @@ -37,6 +37,7 @@ public: virtual ~Context() = default; std::string uri; int32_t currentUserId = -1; + int32_t visitedUserId = -1; int32_t appIndex = 0; int32_t haMode = 0; std::string permission; 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 02b2ce855..d8ba1344f 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -118,7 +118,8 @@ std::pair DataShareServiceImpl::InsertEx(const std::string &ur callingTokenId}, true); auto [errCode, ret] = dbDelegate->InsertEx(providerInfo.tableName, valuesBucket); if (errCode == E_OK && ret > 0) { - NotifyChange(uri); + // only notify specific userId + NotifyChange(uri, providerInfo.visitedUserId); RdbSubscriberManager::GetInstance().Emit(uri, providerInfo.visitedUserId, metaData); } else { ReportExcuteFault(callingTokenId, providerInfo, errCode, func); @@ -129,7 +130,7 @@ std::pair DataShareServiceImpl::InsertEx(const std::string &ur return ExecuteEx(uri, extUri, IPCSkeleton::GetCallingTokenID(), false, callBack); } -bool DataShareServiceImpl::NotifyChange(const std::string &uri) +bool DataShareServiceImpl::NotifyChange(const std::string &uri, int32_t userId) { RadarReporter::RadarReport report(RadarReporter::NOTIFY_OBSERVER_DATA_CHANGE, RadarReporter::NOTIFY_DATA_CHANGE, __FUNCTION__); @@ -139,8 +140,7 @@ bool DataShareServiceImpl::NotifyChange(const std::string &uri) report.SetError(RadarReporter::DATA_OBS_EMPTY_ERROR); return false; } - - ErrCode ret = obsMgrClient->NotifyChange(Uri(uri)); + ErrCode ret = obsMgrClient->NotifyChange(Uri(uri), userId); if (ret != ERR_OK) { ZLOGE("obsMgrClient->NotifyChange error return %{public}d", ret); report.SetError(RadarReporter::NOTIFY_ERROR); @@ -166,7 +166,7 @@ std::pair DataShareServiceImpl::UpdateEx(const std::string &ur callingTokenId}, true); auto [errCode, ret] = dbDelegate->UpdateEx(providerInfo.tableName, predicate, valuesBucket); if (errCode == E_OK && ret > 0) { - NotifyChange(uri); + NotifyChange(uri, providerInfo.visitedUserId); RdbSubscriberManager::GetInstance().Emit(uri, providerInfo.visitedUserId, metaData); } else { ReportExcuteFault(callingTokenId, providerInfo, errCode, func); @@ -194,7 +194,7 @@ std::pair DataShareServiceImpl::DeleteEx(const std::string &ur callingTokenId}, true); auto [errCode, ret] = dbDelegate->DeleteEx(providerInfo.tableName, predicate); if (errCode == E_OK && ret > 0) { - NotifyChange(uri); + NotifyChange(uri, providerInfo.visitedUserId); RdbSubscriberManager::GetInstance().Emit(uri, providerInfo.visitedUserId, metaData); } else { ReportExcuteFault(callingTokenId, providerInfo, errCode, func); @@ -251,7 +251,7 @@ int32_t DataShareServiceImpl::AddTemplate(const std::string &uri, const int64_t uri.c_str(), subscriberId, tpltId.bundleName_.c_str(), tplt.predicates_.size()); return templateStrategy_.Execute(context, [&uri, &tpltId, &tplt, &context]() -> int32_t { auto result = TemplateManager::GetInstance().Add( - Key(uri, tpltId.subscriberId_, tpltId.bundleName_), context->currentUserId, tplt); + Key(uri, tpltId.subscriberId_, tpltId.bundleName_), context->visitedUserId, tplt); RdbSubscriberManager::GetInstance().Emit(context->uri, tpltId.subscriberId_, tpltId.bundleName_, context); return result; }); @@ -270,7 +270,7 @@ int32_t DataShareServiceImpl::DelTemplate(const std::string &uri, const int64_t DistributedData::Anonymous::Change(uri).c_str(), subscriberId, tpltId.bundleName_.c_str()); return templateStrategy_.Execute(context, [&uri, &tpltId, &context]() -> int32_t { return TemplateManager::GetInstance().Delete( - Key(uri, tpltId.subscriberId_, tpltId.bundleName_), context->currentUserId); + Key(uri, tpltId.subscriberId_, tpltId.bundleName_), context->visitedUserId); }); } @@ -339,7 +339,7 @@ std::vector DataShareServiceImpl::Publish(const Data &data, con continue; } publishedData.emplace_back(context->uri, context->calledBundleName, item.subscriberId_); - userId = context->currentUserId; + userId = context->visitedUserId; } if (!publishedData.empty()) { PublishedDataSubscriberManager::GetInstance().Emit(publishedData, userId, callerBundleName); @@ -369,7 +369,8 @@ std::vector DataShareServiceImpl::SubscribeRdbData( auto context = std::make_shared(uri); results.emplace_back(uri, subscribeStrategy_.Execute(context, [&id, &observer, &context, this]() { return RdbSubscriberManager::GetInstance().Add( - Key(context->uri, id.subscriberId_, id.bundleName_), observer, context, binderInfo_.executors); + Key(context->uri, id.subscriberId_, id.bundleName_), + observer, context, binderInfo_.executors); })); } return results; @@ -437,7 +438,7 @@ std::vector DataShareServiceImpl::SubscribePublishedData(const result = subscribeStrategy_.Execute(context, [&subscriberId, &observer, &context]() { return PublishedDataSubscriberManager::GetInstance().Add( PublishedDataKey(context->uri, context->callerBundleName, subscriberId), observer, - context->callerTokenId); + context->callerTokenId, context->visitedUserId); }); results.emplace_back(uri, result); if (result == E_OK) { @@ -445,10 +446,10 @@ std::vector DataShareServiceImpl::SubscribePublishedData(const if (binderInfo_.executors != nullptr) { binderInfo_.executors->Execute([context, subscriberId]() { PublishedData::UpdateTimestamp( - context->uri, context->calledBundleName, subscriberId, context->currentUserId); + context->uri, context->calledBundleName, subscriberId, context->visitedUserId); }); } - userId = context->currentUserId; + userId = context->visitedUserId; } } if (!publishedKeys.empty()) { @@ -477,7 +478,7 @@ std::vector DataShareServiceImpl::UnsubscribePublishedData(cons if (result == E_OK && binderInfo_.executors != nullptr) { binderInfo_.executors->Execute([context, subscriberId]() { PublishedData::UpdateTimestamp( - context->uri, context->calledBundleName, subscriberId, context->currentUserId); + context->uri, context->calledBundleName, subscriberId, context->visitedUserId); }); } return result; @@ -510,7 +511,7 @@ std::vector DataShareServiceImpl::EnablePubSubs(const std::vect if (result == E_OK && binderInfo_.executors != nullptr) { binderInfo_.executors->Execute([context, subscriberId]() { PublishedData::UpdateTimestamp( - context->uri, context->calledBundleName, subscriberId, context->currentUserId); + context->uri, context->calledBundleName, subscriberId, context->visitedUserId); }); } results.emplace_back(uri, result); @@ -519,7 +520,7 @@ std::vector DataShareServiceImpl::EnablePubSubs(const std::vect if (PublishedDataSubscriberManager::GetInstance().IsNotifyOnEnabled(pKey, context->callerTokenId)) { publishedKeys.emplace_back(pKey); } - userId = context->currentUserId; + userId = context->visitedUserId; } } if (!publishedKeys.empty()) { @@ -548,7 +549,7 @@ std::vector DataShareServiceImpl::DisablePubSubs(const std::vec if (result == E_OK && binderInfo_.executors != nullptr) { binderInfo_.executors->Execute([context, subscriberId]() { PublishedData::UpdateTimestamp( - context->uri, context->calledBundleName, subscriberId, context->currentUserId); + context->uri, context->calledBundleName, subscriberId, context->visitedUserId); }); } return result; 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 e4d0a7d9d..49f285efe 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.h +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.h @@ -118,7 +118,7 @@ private: void RegisterDataShareServiceInfo(); void RegisterHandler(); bool SubscribeTimeChanged(); - bool NotifyChange(const std::string &uri); + bool NotifyChange(const std::string &uri, int32_t userId); bool GetCallerBundleName(std::string &bundleName); std::pair ExecuteEx(const std::string &uri, const std::string &extUri, const int32_t tokenId, bool isRead, ExecuteCallbackEx callback); diff --git a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp index c796152ca..e3e01974b 100644 --- a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp @@ -31,7 +31,7 @@ bool LoadConfigFromDataProxyNodeStrategy::operator()(std::shared_ptr co } context->type = PUBLISHED_DATA_TYPE; if (BundleMgrProxy::GetInstance()->GetBundleInfoFromBMS( - context->calledBundleName, context->currentUserId, context->bundleInfo) != E_OK) { + context->calledBundleName, context->visitedUserId, context->bundleInfo) != E_OK) { ZLOGE("GetBundleInfoFromBMS failed! bundleName: %{public}s", context->calledBundleName.c_str()); context->errCode = E_BUNDLE_NAME_NOT_EXIST; return false; diff --git a/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp index 16fe91511..cc8f284c4 100644 --- a/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp @@ -81,7 +81,7 @@ bool LoadConfigFromDataShareBundleInfoStrategy::operator()(std::shared_ptrGetBundleInfoFromBMS( - context->calledBundleName, context->currentUserId, context->bundleInfo) != E_OK) { + context->calledBundleName, context->visitedUserId, context->bundleInfo) != E_OK) { ZLOGE("GetBundleInfoFromBMS failed! bundleName: %{public}s", context->calledBundleName.c_str()); return false; } diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp index b8dece0fd..9a64d37c7 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp @@ -33,13 +33,14 @@ bool LoadConfigCommonStrategy::operator()(std::shared_ptr context) context->callerTokenId = IPCSkeleton::GetCallingTokenID(); } context->currentUserId = AccountDelegate::GetInstance()->GetUserByToken(context->callerTokenId); + context->visitedUserId = context->currentUserId; if (!URIUtils::GetAppIndexFromProxyURI(context->uri, context->appIndex)) { return false; } // sa, userId is in uri, caller token id is from first caller tokenId if (context->currentUserId == 0) { GetInfoFromProxyURI( - context->uri, context->currentUserId, context->callerTokenId, context->calledBundleName); + context->uri, context->visitedUserId, context->callerTokenId, context->calledBundleName); URIUtils::FormatUri(context->uri); } if (context->needAutoLoadCallerBundleName && context->callerBundleName.empty()) { diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp index d5283e4d3..095a04bdb 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp @@ -53,12 +53,12 @@ bool LoadConfigNormalDataInfoStrategy::operator()(std::shared_ptr conte } DistributedData::StoreMetaData metaData; if (!QueryMetaData( - context->calledBundleName, context->calledStoreName, metaData, context->currentUserId, context->appIndex)) { + context->calledBundleName, context->calledStoreName, metaData, context->visitedUserId, context->appIndex)) { // connect extension and retry AAFwk::WantParams wantParams; ExtensionConnectAdaptor::TryAndWait(context->uri, context->calledBundleName, wantParams); if (!QueryMetaData( - context->calledBundleName, context->calledStoreName, metaData, context->currentUserId, context->appIndex)) { + context->calledBundleName, context->calledStoreName, metaData, context->visitedUserId, context->appIndex)) { ZLOGE("QueryMetaData fail, %{public}s", DistributedData::Anonymous::Change(context->uri).c_str()); context->errCode = NativeRdb::E_DB_NOT_EXIST; return false; diff --git a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp index c32d5daff..b02436915 100644 --- a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp @@ -36,7 +36,7 @@ Data GetDataStrategy::Execute(std::shared_ptr context, int &errorCode) errorCode = context->errCode; return Data(); } - auto result = PublishedData::Query(context->calledBundleName, context->currentUserId); + auto result = PublishedData::Query(context->calledBundleName, context->visitedUserId); Data data; for (auto &item : result) { if (!CheckPermission(context, item.value.key)) { diff --git a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp index b0b7ca77a..a23d516b0 100644 --- a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp @@ -42,10 +42,10 @@ int32_t PublishStrategy::Execute(std::shared_ptr context, const Publish return -1; } PublishedDataItem::DataType value = item.GetData(); - PublishedDataNode node(context->uri, context->calledBundleName, item.subscriberId_, context->currentUserId, + PublishedDataNode node(context->uri, context->calledBundleName, item.subscriberId_, context->visitedUserId, PublishedDataNode::MoveTo(value)); PublishedData data(node, context->version); - auto [status, count] = delegate->Upsert(KvDBDelegate::DATA_TABLE, data); + auto [status, count] = delegate->Upsert(KvDBDelegate::DATA_TABLE, data); if (status != E_OK) { ZLOGE("db Upsert failed, %{public}s %{public}s %{public}d", context->calledBundleName.c_str(), DistributedData::Anonymous::Change(context->uri).c_str(), status); diff --git a/services/distributeddataservice/service/data_share/subscriber_managers/published_data_subscriber_manager.cpp b/services/distributeddataservice/service/data_share/subscriber_managers/published_data_subscriber_manager.cpp index 09dd531f9..b9bfc3c91 100644 --- a/services/distributeddataservice/service/data_share/subscriber_managers/published_data_subscriber_manager.cpp +++ b/services/distributeddataservice/service/data_share/subscriber_managers/published_data_subscriber_manager.cpp @@ -32,15 +32,16 @@ PublishedDataSubscriberManager &PublishedDataSubscriberManager::GetInstance() return manager; } -int PublishedDataSubscriberManager::Add( - const PublishedDataKey &key, const sptr observer, uint32_t firstCallerTokenId) +int PublishedDataSubscriberManager::Add(const PublishedDataKey &key, + const sptr observer, uint32_t firstCallerTokenId, int32_t userId) { publishedDataCache_.Compute( - key, [&observer, &firstCallerTokenId, this](const PublishedDataKey &key, std::vector &value) { + key, [&observer, &firstCallerTokenId, userId, this](const PublishedDataKey &key, + std::vector &value) { ZLOGI("add publish subscriber, uri %{public}s tokenId 0x%{public}x", DistributedData::Anonymous::Change(key.key).c_str(), firstCallerTokenId); value.emplace_back(observer, firstCallerTokenId, IPCSkeleton::GetCallingTokenID(), - IPCSkeleton::GetCallingPid()); + IPCSkeleton::GetCallingPid(), userId); return true; }); return E_OK; @@ -112,6 +113,7 @@ int PublishedDataSubscriberManager::Enable(const PublishedDataKey &key, uint32_t return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; } +// if arg observer is not null, notify that observer only; otherwise notify all observers void PublishedDataSubscriberManager::Emit(const std::vector &keys, int32_t userId, const std::string &ownerBundleName, const sptr observer) { @@ -133,7 +135,7 @@ void PublishedDataSubscriberManager::Emit(const std::vector &k publishedResult.erase(key); continue; } - PutInto(callbacks, val, key, observer); + PutInto(callbacks, val, key, observer, userId); break; } return false; @@ -157,7 +159,7 @@ void PublishedDataSubscriberManager::Emit(const std::vector &k void PublishedDataSubscriberManager::PutInto( std::map, std::vector> &callbacks, const std::vector &val, const PublishedDataKey &key, - const sptr observer) + const sptr observer, int32_t userId) { for (auto const &callback : val) { if (callback.enabled && callback.observer != nullptr) { @@ -165,6 +167,10 @@ void PublishedDataSubscriberManager::PutInto( if (observer != nullptr && callback.observer != observer) { continue; } + if (callback.userId != 0 && callback.userId != userId && userId != 0) { + ZLOGE("Not across user publish, from %{public}d to %{public}d", userId, callback.userId); + continue; + } callbacks[callback.observer].emplace_back(key); } } @@ -267,8 +273,8 @@ bool PublishedDataKey::operator!=(const PublishedDataKey &rhs) const } PublishedDataSubscriberManager::ObserverNode::ObserverNode(const sptr &observer, - uint32_t firstCallerTokenId, uint32_t callerTokenId, uint32_t callerPid) - : observer(observer), firstCallerTokenId(firstCallerTokenId), callerTokenId(callerTokenId), callerPid(callerPid) + uint32_t firstCallerTokenId, uint32_t callerTokenId, uint32_t callerPid, int32_t userId): observer(observer), + firstCallerTokenId(firstCallerTokenId), callerTokenId(callerTokenId), callerPid(callerPid), userId(userId) { } } // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/subscriber_managers/published_data_subscriber_manager.h b/services/distributeddataservice/service/data_share/subscriber_managers/published_data_subscriber_manager.h index 37356d7e4..27e002d96 100644 --- a/services/distributeddataservice/service/data_share/subscriber_managers/published_data_subscriber_manager.h +++ b/services/distributeddataservice/service/data_share/subscriber_managers/published_data_subscriber_manager.h @@ -26,7 +26,8 @@ #include "executor_pool.h" namespace OHOS::DataShare { struct PublishedDataKey { - PublishedDataKey(const std::string &key, const std::string &bundleName, int64_t subscriberId); + PublishedDataKey(const std::string &key, const std::string &bundleName, + int64_t subscriberId); bool operator<(const PublishedDataKey &rhs) const; bool operator>(const PublishedDataKey &rhs) const; bool operator<=(const PublishedDataKey &rhs) const; @@ -36,13 +37,14 @@ struct PublishedDataKey { std::string key; std::string bundleName; int64_t subscriberId; + int32_t userId; }; class PublishedDataSubscriberManager { public: static PublishedDataSubscriberManager &GetInstance(); int Add(const PublishedDataKey &key, const sptr observer, - uint32_t firstCallerTokenId); + uint32_t firstCallerTokenId, int32_t userId); int Delete(const PublishedDataKey &key, uint32_t firstCallerTokenId); void Delete(uint32_t callerTokenId, uint32_t callerPid); int Disable(const PublishedDataKey &key, uint32_t firstCallerTokenId); @@ -58,18 +60,20 @@ public: private: struct ObserverNode { ObserverNode(const sptr &observer, uint32_t firstCallerTokenId, - uint32_t callerTokenId = 0, uint32_t callerPid = 0); + uint32_t callerTokenId = 0, uint32_t callerPid = 0, int32_t userId = 0); sptr observer; uint32_t firstCallerTokenId; uint32_t callerTokenId; uint32_t callerPid; bool enabled = true; bool isNotifyOnEnabled = false; + int32_t userId = 0; }; PublishedDataSubscriberManager() = default; void PutInto(std::map, std::vector> &, - const std::vector &, const PublishedDataKey &, const sptr); + const std::vector &, const PublishedDataKey &, const sptr, + int32_t userId); ConcurrentMap> publishedDataCache_; }; } // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.cpp b/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.cpp index b03fc242d..e8d34c239 100644 --- a/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.cpp +++ b/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.cpp @@ -119,9 +119,9 @@ int RdbSubscriberManager::Add(const Key &key, const sptr ZLOGI("add subscriber, uri %{private}s tokenId 0x%{public}x", key.uri.c_str(), context->callerTokenId); auto callerTokenId = IPCSkeleton::GetCallingTokenID(); auto callerPid = IPCSkeleton::GetCallingPid(); - value.emplace_back(observer, context->callerTokenId, callerTokenId, callerPid); + value.emplace_back(observer, context->callerTokenId, callerTokenId, callerPid, context->visitedUserId); std::vector node; - node.emplace_back(observer, context->callerTokenId, callerTokenId, callerPid); + node.emplace_back(observer, context->callerTokenId, callerTokenId, callerPid, context->visitedUserId); ExecutorPool::Task task = [key, node, context, this]() { LoadConfigDataInfoStrategy loadDataInfo; if (!loadDataInfo(context)) { @@ -130,9 +130,9 @@ int RdbSubscriberManager::Add(const Key &key, const sptr return; } DistributedData::StoreMetaData metaData = RdbSubscriberManager::GenMetaDataFromContext(context); - Notify(key, context->currentUserId, node, metaData); + Notify(key, context->visitedUserId, node, metaData); if (GetEnableObserverCount(key) == 1) { - SchedulerManager::GetInstance().Start(key, context->currentUserId, metaData); + SchedulerManager::GetInstance().Start(key, context->visitedUserId, metaData); } }; executorPool->Execute(task); @@ -227,13 +227,13 @@ int RdbSubscriberManager::Enable(const Key &key, std::shared_ptr contex if (it->isNotifyOnEnabled) { std::vector node; node.emplace_back(it->observer, context->callerTokenId); - Notify(key, context->currentUserId, node, metaData); + Notify(key, context->visitedUserId, node, metaData); } } return true; }); if (isChanged) { - SchedulerManager::GetInstance().Enable(key, context->currentUserId, metaData); + SchedulerManager::GetInstance().Enable(key, context->visitedUserId, metaData); } return result ? E_OK : E_SUBSCRIBER_NOT_EXIST; } @@ -252,12 +252,12 @@ void RdbSubscriberManager::Emit(const std::string &uri, std::shared_ptr if (key.uri != uri) { return false; } - Notify(key, context->currentUserId, val, metaData); + Notify(key, context->visitedUserId, val, metaData); SetObserverNotifyOnEnabled(val); return false; }); SchedulerManager::GetInstance().Execute( - uri, context->currentUserId, metaData); + uri, context->visitedUserId, metaData); } void RdbSubscriberManager::Emit(const std::string &uri, int32_t userId, @@ -372,6 +372,12 @@ int RdbSubscriberManager::Notify(const Key &key, int32_t userId, const std::vect ZLOGI("emit, valSize: %{public}zu, dataSize:%{public}zu, uri:%{public}s,", val.size(), changeNode.data_.size(), DistributedData::Anonymous::Change(changeNode.uri_).c_str()); for (const auto &callback : val) { + // not notify across user + if (callback.userId != userId && userId != 0 && callback.userId != 0) { + ZLOGI("Not allow across notify, uri:%{public}s, from %{public}d to %{public}d.", + DistributedData::Anonymous::Change(changeNode.uri_).c_str(), userId, callback.userId); + continue; + } if (callback.enabled && callback.observer != nullptr) { callback.observer->OnChangeFromRdb(changeNode); } @@ -399,12 +405,12 @@ void RdbSubscriberManager::Emit(const std::string &uri, int64_t subscriberId, if (key.uri != uri || key.subscriberId != subscriberId) { return false; } - Notify(key, context->currentUserId, val, metaData); + Notify(key, context->visitedUserId, val, metaData); SetObserverNotifyOnEnabled(val); return false; }); Key executeKey(uri, subscriberId, bundleName); - SchedulerManager::GetInstance().Start(executeKey, context->currentUserId, metaData); + SchedulerManager::GetInstance().Start(executeKey, context->visitedUserId, metaData); } DistributedData::StoreMetaData RdbSubscriberManager::GenMetaDataFromContext(const std::shared_ptr context) @@ -418,8 +424,8 @@ DistributedData::StoreMetaData RdbSubscriberManager::GenMetaDataFromContext(cons } RdbSubscriberManager::ObserverNode::ObserverNode(const sptr &observer, - uint32_t firstCallerTokenId, uint32_t callerTokenId, uint32_t callerPid) - : observer(observer), firstCallerTokenId(firstCallerTokenId), callerTokenId(callerTokenId), callerPid(callerPid) + uint32_t firstCallerTokenId, uint32_t callerTokenId, uint32_t callerPid, int32_t userId): observer(observer), + firstCallerTokenId(firstCallerTokenId), callerTokenId(callerTokenId), callerPid(callerPid), userId(userId) { } } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.h b/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.h index 5bb2e82d8..8624397f8 100644 --- a/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.h +++ b/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.h @@ -73,13 +73,14 @@ public: private: struct ObserverNode { ObserverNode(const sptr &observer, uint32_t firstCallerTokenId, - uint32_t callerTokenId = 0, uint32_t callerPid = 0); + uint32_t callerTokenId = 0, uint32_t callerPid = 0, int32_t userId = 0); sptr observer; uint32_t firstCallerTokenId; uint32_t callerTokenId; uint32_t callerPid; bool enabled = true; bool isNotifyOnEnabled = false; + int32_t userId = 0; }; RdbSubscriberManager() = default; diff --git a/services/distributeddataservice/service/test/data_share_service_impl_test.cpp b/services/distributeddataservice/service/test/data_share_service_impl_test.cpp index 58efb56e4..65f79e4d5 100644 --- a/services/distributeddataservice/service/test/data_share_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/data_share_service_impl_test.cpp @@ -146,10 +146,10 @@ HWTEST_F(DataShareServiceImplTest, NotifyChange001, TestSize.Level1) { DataShareServiceImpl dataShareServiceImpl; std::string uri = SLIENT_ACCESS_URI; - auto result = dataShareServiceImpl.NotifyChange(uri); + auto result = dataShareServiceImpl.NotifyChange(uri, USER_TEST); EXPECT_EQ(result, true); - result = dataShareServiceImpl.NotifyChange(""); + result = dataShareServiceImpl.NotifyChange("", USER_TEST); EXPECT_EQ(result, false); } diff --git a/services/distributeddataservice/service/test/data_share_subscriber_managers_test.cpp b/services/distributeddataservice/service/test/data_share_subscriber_managers_test.cpp index 9b637f18a..467432cac 100644 --- a/services/distributeddataservice/service/test/data_share_subscriber_managers_test.cpp +++ b/services/distributeddataservice/service/test/data_share_subscriber_managers_test.cpp @@ -110,9 +110,9 @@ HWTEST_F(DataShareSubscriberManagersTest, Add, TestSize.Level1) nodes.emplace_back(node2); Template tpl(nodes, "select name1 as name from TBL00"); DataShare::Key key(DATA_SHARE_URI_TEST, tpltId.subscriberId_, tpltId.bundleName_); - auto result = TemplateManager::GetInstance().Add(key, context->currentUserId, tpl); + auto result = TemplateManager::GetInstance().Add(key, context->visitedUserId, tpl); EXPECT_EQ(result, DataShare::E_ERROR); - result = TemplateManager::GetInstance().Delete(key, context->currentUserId); + result = TemplateManager::GetInstance().Delete(key, context->visitedUserId); EXPECT_EQ(result, DataShare::E_ERROR); } @@ -210,7 +210,7 @@ HWTEST_F(DataShareSubscriberManagersTest, IsNotifyOnEnabled, TestSize.Level1) sptr observer; std::vector val; std::map, std::vector> callbacks; - PublishedDataSubscriberManager::GetInstance().PutInto(callbacks, val, key, observer); + PublishedDataSubscriberManager::GetInstance().PutInto(callbacks, val, key, observer, context->visitedUserId); std::vector publishedKeys; PublishedDataSubscriberManager::GetInstance().SetObserversNotifiedOnEnabled(publishedKeys); uint32_t tokenId = AccessTokenKit::GetHapTokenID(USER_TEST, BUNDLE_NAME_TEST, USER_TEST); -- Gitee