diff --git a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp index 5e93729438346600d59f9ad56ee6536d259a59a4..eb11a9c9cbcf6cbd91fc0088ad287ac02ef80f9e 100644 --- a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp +++ b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp @@ -94,9 +94,9 @@ bool PreProcessUtils::GetNativeProcessNameByToken(int tokenId, std::string &proc return true; } -std::string PreProcessUtils::GetAppId(const std::string &bundleName) +bool PreProcessUtils::GetAppInfo(uint32_t tokenId, std::string &appId, std::string &bundleName) { - return "com.demo.test"; + return true; } std::string PreProcessUtils::GetLocalDeviceId() diff --git a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h index e79e2bb6c6ea9e034cf3aa6930f685bf63c04c70..c4b6eed10a0d136ae955c1ed51e0be9013426f7d 100644 --- a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h +++ b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h @@ -44,7 +44,7 @@ public: static bool GetDetailsFromUData(const UnifiedData &data, UDDetails &details); static Status GetSummaryFromDetails(const UDDetails &details, Summary &summary); static bool GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &bundleName); - static std::string GetAppId(const std::string &bundleName); + static bool GetAppInfo(uint32_t tokenId, std::string &appId, std::string &bundleName); static sptr GetBundleMgr(); }; } // namespace UDMF diff --git a/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp b/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp index c42f223a7aa6252a8f179c7f67ef1a64e2fb7421..eabe91a1c69cb351e0de7e8890594a6aaaaee113 100644 --- a/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp +++ b/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp @@ -25,6 +25,8 @@ public: static void TearDownTestCase(void) {} void SetUp() {} void TearDown() {} + static constexpr uint32_t TOKEN_ID = 5; + static constexpr char APP_ID[] = "test appId"; }; /** @@ -181,16 +183,18 @@ HWTEST_F(UdmfPreProcessUtilsTest, GetHtmlFileUris001, TestSize.Level1) } /** -* @tc.name: GetAppId001 -* @tc.desc: Abnormal test of GetAppId, samgrProxy is nullptr +* @tc.name: GetAppInfo001 +* @tc.desc: Abnormal test of GetAppInfo * @tc.type: FUNC * @tc.require: */ -HWTEST_F(UdmfPreProcessUtilsTest, GetAppId001, TestSize.Level1) +HWTEST_F(UdmfPreProcessUtilsTest, GetAppInfo001, TestSize.Level1) { - std::string bundleName = "test"; + uint32_t tokenId = TOKEN_ID; + std::string appId; + std::string bundleName; PreProcessUtils preProcessUtils; - std::string appId = preProcessUtils.GetAppId(bundleName); - EXPECT_EQ(appId, ""); + bool ret = preProcessUtils.GetAppInfo(tokenId, appId, bundleName); + EXPECT_FALSE(ret); } }; // namespace UDMF \ No newline at end of file diff --git a/services/distributeddataservice/service/test/udmf_service_impl_test.cpp b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp index 735df7c174105d019f4881c175e79f2eb6b1e448..435be606b5eff90409b7354bc05e47a3976a7256 100644 --- a/services/distributeddataservice/service/test/udmf_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp @@ -29,6 +29,7 @@ #include "text.h" #include "plain_text.h" #include "token_setproc.h" +#include "udmf_client.h" using namespace testing::ext; using namespace OHOS::DistributedData; @@ -86,6 +87,7 @@ public: void TearDown(){}; const std::string STORE_ID = "drag"; + static constexpr uint32_t TOKEN_ID = 5; }; /** @@ -465,163 +467,68 @@ HWTEST_F(UdmfServiceImplTest, CheckAppId002, TestSize.Level1) } /** - * @tc.name: ValidateAndProcessRuntimeData001 - * @tc.desc: invalid appId - * @tc.type: FUNC - */ -HWTEST_F(UdmfServiceImplTest, ValidateAndProcessRuntimeData001, TestSize.Level1) +* @tc.name: CheckDeleteDataPermission001 +* @tc.desc: runtime is null +* @tc.type: FUNC +*/ +HWTEST_F(UdmfServiceImplTest, CheckDeleteDataPermission001, TestSize.Level1) { - UnifiedData data; - std::shared_ptr record = std::make_shared(UDType::PLAIN_TEXT, "plainTextContent"); - data.AddRecord(record); - std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>(); - std::string tag = "this is a tag of test ValidateAndProcessRuntimeData001"; - properties->tag = tag; - data.SetProperties(std::move(properties)); - - std::vector<UnifiedData> dataSet = {data}; + std::string appId; std::shared_ptr<Runtime> runtime; - std::vector<UnifiedData> unifiedDataSet; - std::vector<std::string> deleteKeys; QueryOption query; - UdmfServiceImpl impl; - int32_t result = impl.ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); - EXPECT_EQ(result, UDMF::E_DB_ERROR); + bool ret = impl.CheckDeleteDataPermission(appId, runtime, query); + EXPECT_FALSE(ret); } /** - * @tc.name: ValidateAndProcessRuntimeData002 - * @tc.desc: Normal test - * @tc.type: FUNC - */ -HWTEST_F(UdmfServiceImplTest, ValidateAndProcessRuntimeData002, TestSize.Level1) +* @tc.name: CheckDeleteDataPermission002 +* @tc.desc: query.tokenId is invalid +* @tc.type: FUNC +*/ +HWTEST_F(UdmfServiceImplTest, CheckDeleteDataPermission002, TestSize.Level1) { - UnifiedData data; - data.runtime_ = std::make_shared<Runtime>(); - data.runtime_->tokenId = 1; - std::shared_ptr<UnifiedRecord> record = std::make_shared<PlainText>(UDType::PLAIN_TEXT, "plainTextContent"); - data.AddRecord(record); - std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>(); - std::string tag = "this is a tag of test ValidateAndProcessRuntimeData002"; - properties->tag = tag; - data.SetProperties(std::move(properties)); - - std::vector<UnifiedData> dataSet = {data}; - std::shared_ptr<Runtime> runtime; - std::vector<UnifiedData> unifiedDataSet; - std::vector<std::string> deleteKeys; + std::string appId; + std::shared_ptr<Runtime> runtime = std::make_shared<Runtime>(); + runtime->tokenId = TOKEN_ID; QueryOption query; - query.tokenId = 1; - UdmfServiceImpl impl; - int32_t result = impl.ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); - EXPECT_EQ(result, UDMF::E_OK); + bool ret = impl.CheckDeleteDataPermission(appId, runtime, query); + EXPECT_FALSE(ret); } /** - * @tc.name: ValidateAndProcessRuntimeData003 - * @tc.desc: invalid appId - * @tc.type: FUNC - */ -HWTEST_F(UdmfServiceImplTest, ValidateAndProcessRuntimeData003, TestSize.Level1) +* @tc.name: CheckDeleteDataPermission003 +* @tc.desc: Normal test +* @tc.type: FUNC +*/ +HWTEST_F(UdmfServiceImplTest, CheckDeleteDataPermission003, TestSize.Level1) { - UnifiedData data; - data.runtime_ = std::make_shared<Runtime>(); - data.runtime_->tokenId = 1; - data.runtime_ = std::make_shared<Runtime>(); - data.runtime_->appId = "appId"; - std::shared_ptr<UnifiedRecord> record = std::make_shared<PlainText>(UDType::PLAIN_TEXT, "plainTextContent"); - data.AddRecord(record); - std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>(); - std::string tag = "this is a tag of test ValidateAndProcessRuntimeData003"; - properties->tag = tag; - data.SetProperties(std::move(properties)); - - std::vector<UnifiedData> dataSet = {data}; - std::shared_ptr<Runtime> runtime; - std::vector<UnifiedData> unifiedDataSet; - std::vector<std::string> deleteKeys; + std::string appId; + std::shared_ptr<Runtime> runtime = std::make_shared<Runtime>(); + runtime->tokenId = TOKEN_ID; QueryOption query; - query.tokenId = 2; - + query.tokenId = TOKEN_ID; UdmfServiceImpl impl; - int32_t result = impl.ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); - EXPECT_EQ(result, UDMF::E_OK); + bool ret = impl.CheckDeleteDataPermission(appId, runtime, query); + EXPECT_TRUE(ret); } /** - * @tc.name: ValidateAndProcessRuntimeData004 - * @tc.desc: invalid appId - * @tc.type: FUNC - */ -HWTEST_F(UdmfServiceImplTest, ValidateAndProcessRuntimeData004, TestSize.Level1) +* @tc.name: CheckDeleteDataPermission004 +* @tc.desc: bundleName is empty +* @tc.type: FUNC +*/ +HWTEST_F(UdmfServiceImplTest, CheckDeleteDataPermission004, TestSize.Level1) { - UnifiedData data1; - data1.runtime_ = std::make_shared<Runtime>(); - data1.runtime_->tokenId = 1; - data1.runtime_ = std::make_shared<Runtime>(); - data1.runtime_->appId = "appId"; - std::shared_ptr<UnifiedRecord> record = std::make_shared<PlainText>(UDType::PLAIN_TEXT, "plainTextContent"); - data1.AddRecord(record); - std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>(); - std::string tag = "this is a tag of test ValidateAndProcessRuntimeData004"; - properties->tag = tag; - data1.SetProperties(std::move(properties)); - - UnifiedData data2; - data2.runtime_ = std::make_shared<Runtime>(); - data2.runtime_->tokenId = 1; - data2.runtime_ = std::make_shared<Runtime>(); - data2.runtime_->appId = "appId"; - data2.AddRecord(record); - data2.SetProperties(std::move(properties)); - - std::vector<UnifiedData> dataSet = { data1, data2 }; - std::shared_ptr<Runtime> runtime; - std::vector<UnifiedData> unifiedDataSet; - std::vector<std::string> deleteKeys; + std::string appId; + std::shared_ptr<Runtime> runtime = std::make_shared<Runtime>(); + runtime->appId = TOKEN_ID; QueryOption query; - query.tokenId = 2; - - UdmfServiceImpl impl; - int32_t result = impl.ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); - EXPECT_EQ(result, UDMF::E_OK); -} - -/** - * @tc.name: CloseStoreWhenCorrupted001 - * @tc.desc: Normal test of CloseStoreWhenCorrupted - * @tc.type: FUNC - */ -HWTEST_F(UdmfServiceImplTest, CloseStoreWhenCorrupted001, TestSize.Level1) -{ - std::string intention = "drag"; - StoreCache::GetInstance().CloseStores(); - auto store = StoreCache::GetInstance().GetStore(intention); - EXPECT_EQ(StoreCache::GetInstance().stores_.Size(), 1); - int32_t status = UDMF::E_OK; - UdmfServiceImpl impl; - impl.HandleDbError(intention, status); - EXPECT_EQ(StoreCache::GetInstance().stores_.Size(), 1); -} - -/** - * @tc.name: CloseStoreWhenCorrupted002 - * @tc.desc: Abnormal test of CloseStoreWhenCorrupted - * @tc.type: FUNC - */ -HWTEST_F(UdmfServiceImplTest, CloseStoreWhenCorrupted002, TestSize.Level1) -{ - std::string intention = "drag"; - StoreCache::GetInstance().CloseStores(); - auto store = StoreCache::GetInstance().GetStore(intention); - EXPECT_EQ(StoreCache::GetInstance().stores_.Size(), 1); - int32_t status = UDMF::E_DB_CORRUPTED; + query.tokenId = TOKEN_ID; UdmfServiceImpl impl; - impl.HandleDbError(intention, status); - EXPECT_EQ(StoreCache::GetInstance().stores_.Size(), 0); - EXPECT_EQ(status, UDMF::E_DB_ERROR); + bool ret = impl.CheckDeleteDataPermission(appId, runtime, query); + EXPECT_FALSE(ret); } }; // namespace DistributedDataTest }; // namespace OHOS::Test diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index 361358841df901527884fb0b52f47132b890af59..c0d6786c36a40fff83247ca2e877a28301ccf12b 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -44,6 +44,7 @@ constexpr const char *FILE_SCHEME = "file"; constexpr const char *TAG = "PreProcessUtils::"; constexpr const char *FILE_SCHEME_PREFIX = "file://"; constexpr const char *DOCS_LOCAL_TAG = "/docs/"; +constexpr uint32_t MAX_APP_ID_CACHE_SIZE = 10; static constexpr uint32_t DOCS_LOCAL_PATH_SUBSTR_START_INDEX = 1; static constexpr uint32_t VERIFY_URI_PERMISSION_MAX_SIZE = 500; constexpr const char *TEMP_UNIFIED_DATA_FLAG = "temp_udmf_file_flag"; @@ -56,6 +57,10 @@ using namespace Security::AccessToken; using namespace OHOS::AppFileService::ModuleRemoteFileShare; using namespace RadarReporter; +std::map<uint32_t, std::string> PreProcessUtils::tokenIdAppIdMap_; +std::queue<uint32_t> PreProcessUtils::insertionOrder_; +std::recursive_mutex PreProcessUtils::cacheMutex_; + int32_t PreProcessUtils::FillRuntimeInfo(UnifiedData &data, CustomOption &option) { auto it = UD_INTENTION_MAP.find(option.intention); @@ -63,15 +68,15 @@ int32_t PreProcessUtils::FillRuntimeInfo(UnifiedData &data, CustomOption &option return E_ERROR; } std::string bundleName; - if (!GetSpecificBundleNameByTokenId(option.tokenId, bundleName)) { - ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", option.tokenId); + std::string appId; + if (!GetAppInfo(option.tokenId, appId, bundleName)) { + ZLOGE("GetAppInfo failed, tokenid:%{public}u", option.tokenId); return E_ERROR; } std::string intention = it->second; UnifiedKey key(intention, bundleName, GenerateId()); Privilege privilege; privilege.tokenId = option.tokenId; - std::string appId = GetAppId(bundleName); Runtime runtime; runtime.key = key; runtime.privileges.emplace_back(privilege); @@ -140,6 +145,38 @@ bool PreProcessUtils::GetHapBundleNameByToken(int tokenId, std::string &bundleNa return false; } +bool PreProcessUtils::GetAppInfo(uint32_t tokenId, std::string &appId, std::string &bundleName) +{ + if (!GetSpecificBundleNameByTokenId(tokenId, bundleName)) { + ZLOGE("GetSpecificBundleNameByTokenId failed, tokenId = %{public}u.", tokenId); + return false; + } + { + std::lock_guard<std::recursive_mutex> lock(cacheMutex_); + auto it = tokenIdAppIdMap_.find(tokenId); + if (it != tokenIdAppIdMap_.end() && !it->second.empty()) { + appId = it->second; + return true; + } + } + sptr<AppExecFwk::IBundleMgr> bundleManager = GetBundleMgr(); + if (bundleManager == nullptr) { + ZLOGE("GetBundleMgr failed."); + return false; + } + int32_t userId = IPCSkeleton::GetCallingUid() / OHOS::AppExecFwk::Constants::BASE_USER_RANGE; + appId = bundleManager->GetAppIdByBundleName(bundleName, userId); + std::lock_guard<std::recursive_mutex> lock(cacheMutex_); + tokenIdAppIdMap_.insert(std::pair<uint32_t, std::string>(tokenId, appId)); + insertionOrder_.push(tokenId); + if (tokenIdAppIdMap_.size() > MAX_APP_ID_CACHE_SIZE) { + uint32_t insertionOrder = insertionOrder_.front(); + tokenIdAppIdMap_.erase(insertionOrder); + insertionOrder_.pop(); + } + return true; +} + bool PreProcessUtils::GetNativeProcessNameByToken(int tokenId, std::string &processName) { Security::AccessToken::NativeTokenInfo nativeInfo; @@ -151,32 +188,6 @@ bool PreProcessUtils::GetNativeProcessNameByToken(int tokenId, std::string &proc return true; } -std::string PreProcessUtils::GetAppId(const std::string &bundleName) -{ - auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (samgrProxy == nullptr) { - ZLOGE("Failed to get system ability mgr, bundleName:%{public}s", bundleName.c_str()); - return ""; - } - auto bundleMgrProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (bundleMgrProxy == nullptr) { - ZLOGE("Failed to Get BMS SA, bundleName:%{public}s", bundleName.c_str()); - return ""; - } - auto bundleManager = iface_cast<AppExecFwk::IBundleMgr>(bundleMgrProxy); - if (bundleManager == nullptr) { - ZLOGE("Failed to get bundle manager, bundleName:%{public}s", bundleName.c_str()); - return ""; - } - int32_t uid = IPCSkeleton::GetCallingUid(); - int32_t userId = uid / OHOS::AppExecFwk::Constants::BASE_USER_RANGE; - std::string appId = bundleManager->GetAppIdByBundleName(bundleName, userId); - if (appId.empty()) { - ZLOGE("GetAppIdByBundleName failed, bundleName:%{public}s, uid:%{public}d", bundleName.c_str(), uid); - } - return appId; -} - std::string PreProcessUtils::GetLocalDeviceId() { auto info = DistributedData::DeviceManagerAdapter::GetInstance().GetLocalDevice(); diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h index 366dc17c60e1f34450724393d1db11061dbb1e84..576c969f2997bd838e793bb69ae21538b0c99bac 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -15,6 +15,7 @@ #ifndef UDMF_PREPROCESS_UTILS_H #define UDMF_PREPROCESS_UTILS_H +#include <queue> #include "bundlemgr/bundle_mgr_proxy.h" #include "unified_data.h" @@ -44,7 +45,7 @@ public: static bool GetDetailsFromUData(const UnifiedData &data, UDDetails &details); static Status GetSummaryFromDetails(const UDDetails &details, Summary &summary); static bool GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &bundleName); - static std::string GetAppId(const std::string &bundleName); + static bool GetAppInfo(uint32_t tokenId, std::string &appId, std::string &bundleName); static sptr<AppExecFwk::IBundleMgr> GetBundleMgr(); private: static bool CheckUriAuthorization(const std::vector<std::string>& uris, uint32_t tokenId); @@ -52,6 +53,9 @@ private: static bool IsFileType(std::shared_ptr<UnifiedRecord> record); static std::string GetSdkVersionByToken(uint32_t tokenId); static bool GetSpecificBundleName(const std::string &bundleName, int32_t appIndex, std::string &specificBundleName); + static std::recursive_mutex cacheMutex_; + static std::map<uint32_t, std::string> tokenIdAppIdMap_; + static std::queue<uint32_t> insertionOrder_; }; } // namespace UDMF } // namespace OHOS diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index 3cc61d68cc33023f0c0d88419fcdf94b28c2ada2..1367954f355af80e85095b80516007b99585c660 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -400,7 +400,8 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi return E_INVALID_PARAMETERS; } std::string bundleName; - if (!PreProcessUtils::GetSpecificBundleNameByTokenId(query.tokenId, bundleName)) { + std::string appId; + if (!PreProcessUtils::GetAppInfo(query.tokenId, appId, bundleName)) { ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", query.tokenId); return E_ERROR; } @@ -429,8 +430,7 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi ZLOGE("Invalid parameter, runtime is nullptr."); return E_DB_ERROR; } - if (runtime->tokenId != query.tokenId && !HasDatahubPriviledge(bundleName) && - CheckAppId(runtime, bundleName) != E_OK) { + if (runtime->tokenId != query.tokenId && !HasDatahubPriviledge(bundleName) && CheckAppId(runtime, appId) != E_OK) { ZLOGE("Update failed: tokenId or appId mismatch, bundleName: %{public}s", bundleName.c_str()); return E_INVALID_PARAMETERS; } @@ -445,16 +445,14 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi return E_OK; } -int32_t UdmfServiceImpl::CheckAppId(std::shared_ptr<Runtime> runtime, const std::string &bundleName) +int32_t UdmfServiceImpl::CheckAppId(std::shared_ptr<Runtime> runtime, const std::string &appId) { if (runtime->appId.empty()) { ZLOGE("Update failed: Invalid parameter, runtime->appId is empty"); return E_INVALID_PARAMETERS; } - std::string appId = PreProcessUtils::GetAppId(bundleName); if (appId.empty() || appId != runtime->appId) { - ZLOGE("Update failed: runtime->appId %{public}s and bundleName appId %{public}s mismatch", - runtime->appId.c_str(), appId.c_str()); + ZLOGE("Update failed: runtime->appId and bundleName's appId mismatch"); return E_INVALID_PARAMETERS; } return E_OK; @@ -484,12 +482,15 @@ int32_t UdmfServiceImpl::DeleteData(const QueryOption &query, std::vector<Unifie ZLOGW("DataSet has no data, key: %{public}s, intention: %{public}d.", query.key.c_str(), query.intention); return E_OK; } - std::shared_ptr<Runtime> runtime; std::vector<std::string> deleteKeys; - status = ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); - if (status != E_OK) { - ZLOGE("ValidateAndProcessRuntimeData failed."); - return status; + std::string appId; + for (const auto &data : dataSet) { + auto runtime = data.GetRuntime(); + if (!CheckDeleteDataPermission(appId, runtime, query)) { + continue; + } + unifiedDataSet.push_back(data); + deleteKeys.emplace_back(UnifiedKey(runtime->key.key).GetKeyCommonPrefix()); } if (deleteKeys.empty()) { ZLOGE("No data to delete for this application"); @@ -505,36 +506,23 @@ int32_t UdmfServiceImpl::DeleteData(const QueryOption &query, std::vector<Unifie return E_OK; } -int32_t UdmfServiceImpl::ValidateAndProcessRuntimeData(const std::vector<UnifiedData> &dataSet, - std::shared_ptr<Runtime> runtime, std::vector<UnifiedData> &unifiedDataSet, const QueryOption &query, - std::vector<std::string> &deleteKeys) + +bool UdmfServiceImpl::CheckDeleteDataPermission(std::string &appId, const std::shared_ptr<Runtime> &runtime, + const QueryOption &query) { - std::string appId; - bool isFirstInvoke = false; - for (const auto &data : dataSet) { - runtime = data.GetRuntime(); - if (runtime == nullptr) { - ZLOGE("Invalid runtime."); - return E_DB_ERROR; - } - if (runtime->tokenId != query.tokenId) { - if (runtime->appId.empty()) { - continue; - } - if (!isFirstInvoke) { - std::string bundleName; - PreProcessUtils::GetHapBundleNameByToken(query.tokenId, bundleName); - appId = PreProcessUtils::GetAppId(bundleName); - isFirstInvoke = true; - } - if (appId.empty() || appId != runtime->appId) { - continue; - } - } - unifiedDataSet.push_back(data); - deleteKeys.emplace_back(UnifiedKey(runtime->key.key).GetKeyCommonPrefix()); + if (runtime == nullptr) { + ZLOGE("Invalid runtime."); + return false; } - return E_OK; + if (runtime->tokenId == query.tokenId) { + return true; + } + if (runtime->appId.empty()) { + ZLOGE("Delete data failed: Invalid parameter, runtime->appId is empty"); + return false; + } + std::string bundleName; + return PreProcessUtils::GetAppInfo(query.tokenId, appId, bundleName) && appId == runtime->appId; } int32_t UdmfServiceImpl::GetSummary(const QueryOption &query, Summary &summary) diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.h b/services/distributeddataservice/service/udmf/udmf_service_impl.h index 87d0af224a9c35ae3fd47a2fcba3f410e8ffc4a5..d0ce492c2e701a869ab5d60cb626694daebc5e64 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.h +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.h @@ -80,11 +80,11 @@ private: std::string FindIntentionMap(const Intention &queryintention); bool IsValidOptionsNonDrag(UnifiedKey &key, const std::string &intention); bool IsValidInput(const QueryOption &query, UnifiedData &unifiedData, UnifiedKey &key); - int32_t ValidateAndProcessRuntimeData(const std::vector<UnifiedData> &dataSet, std::shared_ptr<Runtime> runtime, - std::vector<UnifiedData> &unifiedDataSet, const QueryOption &query, std::vector<std::string> &deleteKeys); - int32_t CheckAppId(std::shared_ptr<Runtime> runtime, const std::string &bundleName); void CloseStoreWhenCorrupted(const std::string &intention, int32_t status); void HandleDbError(const std::string &intention, int32_t &status); + bool CheckDeleteDataPermission(std::string &appId, const std::shared_ptr<Runtime> &runtime, + const QueryOption &query); + int32_t CheckAppId(std::shared_ptr<Runtime> runtime, const std::string &appId); class Factory { public: Factory();