From e11fcc895c7939cb73ee5ee3f12fb22a8665ca48 Mon Sep 17 00:00:00 2001 From: renhongyujie Date: Wed, 27 Dec 2023 16:04:22 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B6=85=E5=A4=A7=E5=87=BD=E6=95=B0=E6=95=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: renhongyujie --- .../file_access/include/file_access_helper.h | 2 + .../file_access/src/file_access_helper.cpp | 157 ++++++------------ 2 files changed, 54 insertions(+), 105 deletions(-) diff --git a/interfaces/inner_api/file_access/include/file_access_helper.h b/interfaces/inner_api/file_access/include/file_access_helper.h index bf1e9ae4..9ca30934 100644 --- a/interfaces/inner_api/file_access/include/file_access_helper.h +++ b/interfaces/inner_api/file_access/include/file_access_helper.h @@ -68,6 +68,8 @@ public: Creator(const std::shared_ptr &context, const std::vector &wants); static std::shared_ptr Creator(const sptr &token, const std::vector &wants); + static std::pair, int> CreatorForConect(const sptr &token, + const std::vector &wants); bool Release(); int Access(Uri &uri, bool &isExist); diff --git a/interfaces/inner_api/file_access/src/file_access_helper.cpp b/interfaces/inner_api/file_access/src/file_access_helper.cpp index cfd22d1d..e151f128 100644 --- a/interfaces/inner_api/file_access/src/file_access_helper.cpp +++ b/interfaces/inner_api/file_access/src/file_access_helper.cpp @@ -174,63 +174,78 @@ static bool IsSystemApp() return OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(accessTokenIDEx); } -std::pair, int> FileAccessHelper::Creator( - const std::shared_ptr &context) +std::pair, int> FileAccessHelper::CreatorForConect( + const sptr &token, const std::vector &wants) { - UserAccessTracer trace; - trace.Start("Creator"); - if (context == nullptr) { - HILOG_ERROR("FileAccessHelper::Creator failed, context == nullptr"); - return {nullptr, EINVAL}; - } - if (!IsSystemApp()) { - HILOG_ERROR("FileAccessHelper::Creator check IsSystemAppByFullTokenID failed"); - return {nullptr, E_PERMISSION_SYS}; - } - sptr bm = FileAccessHelper::GetBundleMgrProxy(); - FileAccessHelper::wants_.clear(); std::unordered_map> cMap; - std::vector extensionInfos; - bool ret = bm->QueryExtensionAbilityInfos( - AppExecFwk::ExtensionAbilityType::FILEACCESS_EXTENSION, GetUserId(), extensionInfos); - if (!ret) { - HILOG_ERROR("FileAccessHelper::Creator QueryExtensionAbilityInfos failed"); - return {nullptr, E_GETINFO}; - } - for (size_t i = 0; i < extensionInfos.size(); i++) { - AAFwk::Want wantTem; - wantTem.SetElementName(extensionInfos[i].bundleName, extensionInfos[i].name); + for (size_t i = 0; i < wants.size(); i++) { sptr fileAccessExtConnection(new(std::nothrow) FileAccessExtConnection()); if (fileAccessExtConnection == nullptr) { HILOG_ERROR("new fileAccessExtConnection fail"); return {nullptr, E_GETRESULT}; } + if (!fileAccessExtConnection->IsExtAbilityConnected()) { - fileAccessExtConnection->ConnectFileExtAbility(wantTem, context->GetToken()); + fileAccessExtConnection->ConnectFileExtAbility(wants[i], token); } + sptr fileExtProxy = fileAccessExtConnection->GetFileExtProxy(); if (fileExtProxy == nullptr) { HILOG_ERROR("Creator get invalid fileExtProxy"); return {nullptr, E_CONNECT}; } + std::shared_ptr connectInfo = std::make_shared(); if (!connectInfo) { HILOG_ERROR("Creator, connectInfo == nullptr"); return {nullptr, E_GETRESULT}; } - FileAccessHelper::wants_.push_back(wantTem); - connectInfo->want = wantTem; + + connectInfo->want = wants[i]; connectInfo->fileAccessExtConnection = fileAccessExtConnection; - cMap.emplace(extensionInfos[i].bundleName, connectInfo); + string bundleName = FileAccessHelper::GetKeyOfWants(wants[i]); + if (bundleName.length() == 0) { + HILOG_ERROR("Creator GetKeyOfWants bundleName not found"); + return {nullptr, E_GETRESULT}; + } + cMap.insert(std::pair>(bundleName, connectInfo)); } - FileAccessHelper *ptrFileAccessHelper = new (std::nothrow) FileAccessHelper(context, cMap); + + FileAccessHelper *ptrFileAccessHelper = new (std::nothrow) FileAccessHelper(token, cMap); if (ptrFileAccessHelper == nullptr) { - HILOG_ERROR("FileAccessHelper::Creator failed, create FileAccessHelper failed"); + HILOG_ERROR("Creator failed, create FileAccessHelper failed"); return {nullptr, E_GETRESULT}; } + return {std::shared_ptr(ptrFileAccessHelper), ERR_OK}; } +std::pair, int> FileAccessHelper::Creator( + const std::shared_ptr &context) +{ + UserAccessTracer trace; + trace.Start("Creator"); + if (context == nullptr) { + HILOG_ERROR("FileAccessHelper::Creator failed, context == nullptr"); + return {nullptr, EINVAL}; + } + if (!IsSystemApp()) { + HILOG_ERROR("FileAccessHelper::Creator check IsSystemAppByFullTokenID failed"); + return {nullptr, E_PERMISSION_SYS}; + } + if (GetRegisteredFileAccessExtAbilityInfo(FileAccessHelper::wants_) != ERR_OK) { + HILOG_ERROR("GetRegisteredFileAccessExtAbilityInfo failed"); + return {nullptr, E_GETINFO}; + } + + std::pair, int> ptrFileAccessHelper = CreatorForConect(context->GetToken(), FileAccessHelper::wants_); + if (ptrFileAccessHelper.second != ERR_OK) { + return ptrFileAccessHelper; + } + + return {ptrFileAccessHelper.first, ERR_OK}; +} + std::pair, int> FileAccessHelper::Creator( const std::shared_ptr &context, const std::vector &wants) { @@ -256,46 +271,12 @@ std::pair, int> FileAccessHelper::Creator( return {nullptr, E_GETINFO}; } - std::unordered_map> cMap; - for (size_t i = 0; i < wants.size(); i++) { - sptr fileAccessExtConnection(new(std::nothrow) FileAccessExtConnection()); - if (fileAccessExtConnection == nullptr) { - HILOG_ERROR("new fileAccessExtConnection fail"); - return {nullptr, E_GETRESULT}; - } - - if (!fileAccessExtConnection->IsExtAbilityConnected()) { - fileAccessExtConnection->ConnectFileExtAbility(wants[i], context->GetToken()); - } - - sptr fileExtProxy = fileAccessExtConnection->GetFileExtProxy(); - if (fileExtProxy == nullptr) { - HILOG_ERROR("Creator get invalid fileExtProxy"); - return {nullptr, E_CONNECT}; - } - - std::shared_ptr connectInfo = std::make_shared(); - if (connectInfo == nullptr) { - HILOG_ERROR("Creator, connectInfo == nullptr"); - return {nullptr, E_GETRESULT}; - } - - connectInfo->want = wants[i]; - connectInfo->fileAccessExtConnection = fileAccessExtConnection; - string bundleName = FileAccessHelper::GetKeyOfWants(wants[i]); - if (bundleName.length() == 0) { - HILOG_ERROR("Creator GetKeyOfWants bundleName not found"); - return {nullptr, E_GETRESULT}; - } - cMap.insert(std::pair>(bundleName, connectInfo)); - } - FileAccessHelper *ptrFileAccessHelper = new (std::nothrow) FileAccessHelper(context, cMap); - if (ptrFileAccessHelper == nullptr) { - HILOG_ERROR("Creator failed, create FileAccessHelper failed"); - return {nullptr, E_GETRESULT}; + std::pair, int> ptrFileAccessHelper = CreatorForConect(context->GetToken(), wants); + if (ptrFileAccessHelper.second != ERR_OK) { + return ptrFileAccessHelper; } - return {std::shared_ptr(ptrFileAccessHelper), ERR_OK}; + return {ptrFileAccessHelper.first, ERR_OK}; } std::shared_ptr FileAccessHelper::Creator(const sptr &token, @@ -323,46 +304,12 @@ std::shared_ptr FileAccessHelper::Creator(const sptr> cMap; - for (size_t i = 0; i < wants.size(); i++) { - sptr fileAccessExtConnection(new(std::nothrow) FileAccessExtConnection()); - if (fileAccessExtConnection == nullptr) { - HILOG_ERROR("new fileAccessExtConnection fail"); - return nullptr; - } - - if (!fileAccessExtConnection->IsExtAbilityConnected()) { - fileAccessExtConnection->ConnectFileExtAbility(wants[i], token); - } - - sptr fileExtProxy = fileAccessExtConnection->GetFileExtProxy(); - if (fileExtProxy == nullptr) { - HILOG_ERROR("Creator get invalid fileExtProxy"); - return nullptr; - } - - std::shared_ptr connectInfo = std::make_shared(); - if (!connectInfo) { - HILOG_ERROR("Creator, connectInfo == nullptr"); - return nullptr; - } - - connectInfo->want = wants[i]; - connectInfo->fileAccessExtConnection = fileAccessExtConnection; - string bundleName = FileAccessHelper::GetKeyOfWants(wants[i]); - if (bundleName.length() == 0) { - HILOG_ERROR("Creator GetKeyOfWants bundleName not found"); - return nullptr; - } - cMap.insert(std::pair>(bundleName, connectInfo)); - } - FileAccessHelper *ptrFileAccessHelper = new (std::nothrow) FileAccessHelper(token, cMap); - if (ptrFileAccessHelper == nullptr) { - HILOG_ERROR("Creator failed, create FileAccessHelper failed"); + std::pair, int> ptrFileAccessHelper = CreatorForConect(token, wants); + if (ptrFileAccessHelper.second != ERR_OK) { return nullptr; } - return std::shared_ptr(ptrFileAccessHelper); + return ptrFileAccessHelper.first; } bool FileAccessHelper::Release() -- Gitee