From cd6ae775b972806ff7b16c0aa63e38498c596ce4 Mon Sep 17 00:00:00 2001 From: zph Date: Fri, 30 May 2025 19:36:50 +0800 Subject: [PATCH 1/3] update Signed-off-by: zph --- .../udmf/preprocess/preprocess_utils.cpp | 65 ++++++++++++++++++- .../udmf/preprocess/preprocess_utils.h | 10 +++ .../service/udmf/udmf_service_impl.cpp | 4 +- 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index 9d553405e..a14da77d1 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -28,6 +28,10 @@ #include "remote_file_share.h" #include "utils/crypto.h" #include "uri_permission_manager_client.h" + +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" namespace OHOS { namespace UDMF { static constexpr int ID_LEN = 32; @@ -50,6 +54,7 @@ using namespace Security::AccessToken; using namespace OHOS::AppFileService::ModuleRemoteFileShare; using namespace RadarReporter; +sptr PreProcessUtils::proxy_ = nullptr; int32_t PreProcessUtils::RuntimeDataImputation(UnifiedData &data, CustomOption &option) { auto it = UD_INTENTION_MAP.find(option.intention); @@ -57,7 +62,8 @@ int32_t PreProcessUtils::RuntimeDataImputation(UnifiedData &data, CustomOption & return E_ERROR; } std::string bundleName; - GetHapBundleNameByToken(option.tokenId, bundleName); + GetAlterableBundleNameByTokenId(option.tokenId, bundleName); + ZLOGE("zzz bundleName = %{public}s.", bundleName.c_str()); std::string intention = it->second; UnifiedKey key(intention, bundleName, GenerateId()); Privilege privilege; @@ -486,5 +492,62 @@ std::string PreProcessUtils::GetSdkVersionByToken(uint32_t tokenId) } return std::to_string(hapTokenInfo.apiVersion); } + +sptr PreProcessUtils::CheckBMS() +{ + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + ZLOGE("Failed to get system ability mgr."); + return nullptr; + } + return systemAbilityManager->CheckSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); +} + +sptr PreProcessUtils::GetBundleMgrProxy() +{ + if (proxy_ != nullptr) { + return iface_cast(proxy_); + } + proxy_ = CheckBMS(); + if (proxy_ == nullptr) { + ZLOGE("BMS service not ready to complete."); + return nullptr; + } + return iface_cast(proxy_); +} + +bool PreProcessUtils::GetAlterableBundleNameByTokenId(uint32_t tokenId, std::string &bundleName) +{ + auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId); + if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) { + Security::AccessToken::HapTokenInfo hapInfo; + auto ret = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo); + if (ret != Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) { + ZLOGE("GetHapTokenInfo failed, ret is %{public}d.", ret); + return false; + } + return GetDirByBundleNameAndAppIndex(hapInfo.bundleName, hapInfo.instIndex, bundleName); + } + return false; +} + +bool PreProcessUtils::GetDirByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex, std::string &dirName) +{ + // if get dir name failed, set dirName as bundleName + dirName = bundleName; + auto bmsClient = GetBundleMgrProxy(); + if (bmsClient == nullptr) { + ZLOGE("bundleMgrClient is nullptr."); + return false; + } + // com.examp.test 0 -> com.example.test-appclone-0 + auto bmsRet = bmsClient->GetDirByBundleNameAndAppIndex(bundleName, appIndex, dirName); + if (bmsRet != ERR_OK) { + ZLOGE("GetDirByBundleNameAndAppIndex failed, ret:%{public}d", bmsRet); + return false; + } + return true; +} } // namespace UDMF } // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h index 1532f4c49..4cba4d5cf 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -17,6 +17,9 @@ #include "unified_data.h" +#include "bundle_info.h" +#include "bundlemgr/bundle_mgr_proxy.h" + namespace OHOS { namespace UDMF { class PreProcessUtils { @@ -42,11 +45,18 @@ public: static void SetRecordUid(UnifiedData &data); static bool GetDetailsFromUData(const UnifiedData &data, UDDetails &details); static Status GetSummaryFromDetails(const UDDetails &details, Summary &summary); + static bool GetAlterableBundleNameByTokenId(uint32_t tokenId, std::string &bundleName); private: static bool CheckUriAuthorization(const std::vector& uris, uint32_t tokenId); static int32_t GetDfsUrisFromLocal(const std::vector &uris, int32_t userId, UnifiedData &data); static bool IsFileType(std::shared_ptr record); static std::string GetSdkVersionByToken(uint32_t tokenId); + + static bool GetDirByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex, std::string &dirName); + static sptr CheckBMS(); + static sptr GetBundleMgrProxy(); + + static sptr proxy_; }; } // 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 f48bb4953..bc078d8ca 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -401,7 +401,7 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi return E_INVALID_PARAMETERS; } std::string bundleName; - PreProcessUtils::GetHapBundleNameByToken(query.tokenId, bundleName); + PreProcessUtils::GetAlterableBundleNameByTokenId(query.tokenId, bundleName); if (key.bundleName != bundleName && !HasDatahubPriviledge(bundleName)) { ZLOGE("update data failed by %{public}s, key: %{public}s.", bundleName.c_str(), query.key.c_str()); return E_INVALID_PARAMETERS; @@ -1041,7 +1041,7 @@ int32_t UdmfServiceImpl::SetDelayInfo(const DataLoadInfo &dataLoadInfo, sptr(IPCSkeleton::GetCallingTokenID()); - PreProcessUtils::GetHapBundleNameByToken(tokenId, bundleName); + PreProcessUtils::GetAlterableBundleNameByTokenId(tokenId, bundleName); UnifiedKey udkey(UD_INTENTION_MAP.at(UD_INTENTION_DRAG), bundleName, dataLoadInfo.sequenceKey); key = udkey.GetUnifiedKey(); dataLoadCallback_.Insert(key, iface_cast(iUdmfNotifier)); -- Gitee From 857080ec46bba06321be3ba17fbd82cbf036fc27 Mon Sep 17 00:00:00 2001 From: lvcong_oh Date: Fri, 6 Jun 2025 12:32:36 +0000 Subject: [PATCH 2/3] Update log print --- .../service/udmf/preprocess/preprocess_utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index a14da77d1..4ce63673f 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -63,7 +63,7 @@ int32_t PreProcessUtils::RuntimeDataImputation(UnifiedData &data, CustomOption & } std::string bundleName; GetAlterableBundleNameByTokenId(option.tokenId, bundleName); - ZLOGE("zzz bundleName = %{public}s.", bundleName.c_str()); + ZLOGI("bundleName = %{public}s.", bundleName.c_str()); std::string intention = it->second; UnifiedKey key(intention, bundleName, GenerateId()); Privilege privilege; -- Gitee From e8e25a044db835949ec4538253fd0ca68243f4e5 Mon Sep 17 00:00:00 2001 From: zph Date: Sat, 7 Jun 2025 15:45:17 +0800 Subject: [PATCH 3/3] update Signed-off-by: zph --- .../udmf/preprocess/preprocess_utils.cpp | 84 ++++++++----------- .../udmf/preprocess/preprocess_utils.h | 8 -- .../service/udmf/udmf_service_impl.cpp | 10 ++- 3 files changed, 44 insertions(+), 58 deletions(-) diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index a14da77d1..631fb4a67 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -18,20 +18,20 @@ #include +#include "bundle_info.h" +#include "bundlemgr/bundle_mgr_proxy.h" #include "dds_trace.h" #include "udmf_radar_reporter.h" #include "accesstoken_kit.h" #include "device_manager_adapter.h" +#include "iservice_registry.h" #include "log_print.h" +#include "system_ability_definition.h" #include "udmf_radar_reporter.h" #include "udmf_utils.h" #include "remote_file_share.h" #include "utils/crypto.h" #include "uri_permission_manager_client.h" - -#include "if_system_ability_manager.h" -#include "iservice_registry.h" -#include "system_ability_definition.h" namespace OHOS { namespace UDMF { static constexpr int ID_LEN = 32; @@ -54,7 +54,6 @@ using namespace Security::AccessToken; using namespace OHOS::AppFileService::ModuleRemoteFileShare; using namespace RadarReporter; -sptr PreProcessUtils::proxy_ = nullptr; int32_t PreProcessUtils::RuntimeDataImputation(UnifiedData &data, CustomOption &option) { auto it = UD_INTENTION_MAP.find(option.intention); @@ -62,8 +61,10 @@ int32_t PreProcessUtils::RuntimeDataImputation(UnifiedData &data, CustomOption & return E_ERROR; } std::string bundleName; - GetAlterableBundleNameByTokenId(option.tokenId, bundleName); - ZLOGE("zzz bundleName = %{public}s.", bundleName.c_str()); + if (GetAlterableBundleNameByTokenId(option.tokenId, bundleName)) { + ZLOGE("GetAlterableBundleNameByTokenId failed."); + return E_ERROR; + } std::string intention = it->second; UnifiedKey key(intention, bundleName, GenerateId()); Privilege privilege; @@ -493,58 +494,45 @@ std::string PreProcessUtils::GetSdkVersionByToken(uint32_t tokenId) return std::to_string(hapTokenInfo.apiVersion); } -sptr PreProcessUtils::CheckBMS() -{ - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (systemAbilityManager == nullptr) { - ZLOGE("Failed to get system ability mgr."); - return nullptr; - } - return systemAbilityManager->CheckSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); -} - -sptr PreProcessUtils::GetBundleMgrProxy() -{ - if (proxy_ != nullptr) { - return iface_cast(proxy_); - } - proxy_ = CheckBMS(); - if (proxy_ == nullptr) { - ZLOGE("BMS service not ready to complete."); - return nullptr; - } - return iface_cast(proxy_); -} - bool PreProcessUtils::GetAlterableBundleNameByTokenId(uint32_t tokenId, std::string &bundleName) { - auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId); - if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) { - Security::AccessToken::HapTokenInfo hapInfo; - auto ret = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo); - if (ret != Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) { - ZLOGE("GetHapTokenInfo failed, ret is %{public}d.", ret); - return false; - } + Security::AccessToken::HapTokenInfo hapInfo; + if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) + == Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) { return GetDirByBundleNameAndAppIndex(hapInfo.bundleName, hapInfo.instIndex, bundleName); } + if (UTILS::IsTokenNative()) { + ZLOGD("TypeATokenTypeEnum is TOKEN_HAP"); + std::string processName; + if (GetNativeProcessNameByToken(tokenId, processName)) { + bundleName = processName; + return true; + } + } + ZLOGE("Get bundle name faild."); return false; } bool PreProcessUtils::GetDirByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex, std::string &dirName) { - // if get dir name failed, set dirName as bundleName - dirName = bundleName; - auto bmsClient = GetBundleMgrProxy(); - if (bmsClient == nullptr) { - ZLOGE("bundleMgrClient is nullptr."); + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + ZLOGE("Failed to get system ability mgr."); + return false; + } + auto bundleMgrProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleMgrProxy == nullptr) { + ZLOGE("Failed to Get BMS SA."); + return false; + } + auto bundleManager = iface_cast(bundleMgrProxy); + if (bundleManager == nullptr) { + ZLOGE("Failed to get bundle manager"); return false; } - // com.examp.test 0 -> com.example.test-appclone-0 - auto bmsRet = bmsClient->GetDirByBundleNameAndAppIndex(bundleName, appIndex, dirName); - if (bmsRet != ERR_OK) { - ZLOGE("GetDirByBundleNameAndAppIndex failed, ret:%{public}d", bmsRet); + auto ret = bundleManager->GetDirByBundleNameAndAppIndex(bundleName, appIndex, dirName); + if (ret != ERR_OK) { + ZLOGE("GetDirByBundleNameAndAppIndex failed, ret:%{public}d", ret); return false; } return true; diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h index 4cba4d5cf..36bc7a0ee 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -17,9 +17,6 @@ #include "unified_data.h" -#include "bundle_info.h" -#include "bundlemgr/bundle_mgr_proxy.h" - namespace OHOS { namespace UDMF { class PreProcessUtils { @@ -51,12 +48,7 @@ private: static int32_t GetDfsUrisFromLocal(const std::vector &uris, int32_t userId, UnifiedData &data); static bool IsFileType(std::shared_ptr record); static std::string GetSdkVersionByToken(uint32_t tokenId); - static bool GetDirByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex, std::string &dirName); - static sptr CheckBMS(); - static sptr GetBundleMgrProxy(); - - static sptr proxy_; }; } // 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 bc078d8ca..0e69d1f84 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -401,7 +401,10 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi return E_INVALID_PARAMETERS; } std::string bundleName; - PreProcessUtils::GetAlterableBundleNameByTokenId(query.tokenId, bundleName); + if (PreProcessUtils::GetAlterableBundleNameByTokenId(query.tokenId, bundleName)) { + ZLOGE("GetAlterableBundleNameByTokenId failed."); + return E_ERROR; + } if (key.bundleName != bundleName && !HasDatahubPriviledge(bundleName)) { ZLOGE("update data failed by %{public}s, key: %{public}s.", bundleName.c_str(), query.key.c_str()); return E_INVALID_PARAMETERS; @@ -1041,7 +1044,10 @@ int32_t UdmfServiceImpl::SetDelayInfo(const DataLoadInfo &dataLoadInfo, sptr(IPCSkeleton::GetCallingTokenID()); - PreProcessUtils::GetAlterableBundleNameByTokenId(tokenId, bundleName); + if (PreProcessUtils::GetAlterableBundleNameByTokenId(tokenId, bundleName)) { + ZLOGE("GetAlterableBundleNameByTokenId failed."); + return E_ERROR; + } UnifiedKey udkey(UD_INTENTION_MAP.at(UD_INTENTION_DRAG), bundleName, dataLoadInfo.sequenceKey); key = udkey.GetUnifiedKey(); dataLoadCallback_.Insert(key, iface_cast(iUdmfNotifier)); -- Gitee