From 5d03e78a94cd8a7c1dfb724b024cdd277205cef0 Mon Sep 17 00:00:00 2001 From: caochuan Date: Fri, 5 Jul 2024 19:03:47 +0800 Subject: [PATCH] =?UTF-8?q?NewFeature:=20=E5=A2=9E=E5=8A=A0=E5=AF=B9Uri?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: caochuan Change-Id: I00705459b2f85501e9d26b05a2786ff57a9a8e11 --- .../file_access/include/file_access_helper.h | 5 +- .../file_access/include/ifile_access_helper.h | 49 +++++++++++++ .../libfile_access_extension_ability_kit.map | 4 + .../file_access/src/file_access_helper.cpp | 73 ++++++++++++++++++- 4 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 interfaces/inner_api/file_access/include/ifile_access_helper.h 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 01902b62..64021fe6 100644 --- a/interfaces/inner_api/file_access/include/file_access_helper.h +++ b/interfaces/inner_api/file_access/include/file_access_helper.h @@ -33,6 +33,7 @@ #include "refbase.h" #include "uri.h" #include "want.h" +#include "ifile_access_helper.h" using Uri = OHOS::Uri; @@ -53,7 +54,7 @@ struct ConnectInfo { sptr fileAccessExtConnection = nullptr; }; -class FileAccessHelper final : public std::enable_shared_from_this { +class FileAccessHelper final : public IFileAccessHelper, public std::enable_shared_from_this { public: ~FileAccessHelper(); // get all ability want info @@ -67,6 +68,7 @@ public: static std::shared_ptr Creator(const sptr &token, const std::vector &wants); + bool Init(const sptr &token); bool Release(); int Access(Uri &uri, bool &isExist); int OpenFile(Uri &uri, const int flags, int &fd); @@ -102,6 +104,7 @@ private: const std::unordered_map> &cMap); FileAccessHelper(const sptr &token, const std::unordered_map> &cMap); + FileAccessHelper(); std::shared_ptr GetConnectInfo(const std::string &bundleName); std::shared_ptr GetConnectExtensionInfo(Uri &uri); diff --git a/interfaces/inner_api/file_access/include/ifile_access_helper.h b/interfaces/inner_api/file_access/include/ifile_access_helper.h new file mode 100644 index 00000000..8cdc2491 --- /dev/null +++ b/interfaces/inner_api/file_access/include/ifile_access_helper.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef I_FILE_ACCESS_HELPER_H +#define I_FILE_ACCESS_HELPER_H + +#include +#include "file_access_extension_info.h" +#include "iremote_object.h" +#include "uri.h" + +using Uri = OHOS::Uri; + +namespace OHOS { +namespace FileAccessFwk { + +class IFileAccessHelper { +public: + virtual bool Init(const sptr &token) = 0; + virtual bool Release() = 0; + virtual int Mkdir(Uri &parent, const std::string &displayName, Uri &newDir) = 0; + virtual int OpenFile(Uri &uri, const int flags, int &fd) = 0; + virtual int CreateFile(Uri &parent, const std::string &displayName, Uri &newFile) = 0; + virtual int CopyFile(Uri &sourceUri, Uri &destUri, const std::string &fileName, Uri &newFileUri) = 0; + virtual int GetRoots(std::vector &rootInfoVec) = 0; + virtual int Delete(Uri &selectFile) = 0; + +protected: + virtual ~IFileAccessHelper() {} +}; + +extern "C" IFileAccessHelper* CreateFileAccessHelper(); +extern "C" void ReleaseFileAccessHelper(IFileAccessHelper* helper); + +} // namespace FileAccessFwk +} // namespace OHOS +#endif // I_FILE_ACCESS_HELPER_H \ No newline at end of file diff --git a/interfaces/inner_api/file_access/libfile_access_extension_ability_kit.map b/interfaces/inner_api/file_access/libfile_access_extension_ability_kit.map index 2bcb7a53..f7392959 100644 --- a/interfaces/inner_api/file_access/libfile_access_extension_ability_kit.map +++ b/interfaces/inner_api/file_access/libfile_access_extension_ability_kit.map @@ -23,6 +23,10 @@ *OHOS::FileAccessFwk::FileAccessExtStubImpl*; *OHOS::FileAccessFwk::FileAccessExtConnection*; }; + extern "C" { + CreateFileAccessHelper; + ReleaseFileAccessHelper; + }; local: *; }; \ No newline at end of file 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 57b42c8b..160ffd6b 100644 --- a/interfaces/inner_api/file_access/src/file_access_helper.cpp +++ b/interfaces/inner_api/file_access/src/file_access_helper.cpp @@ -13,14 +13,14 @@ * limitations under the License. */ +#define private public #include "file_access_helper.h" - +#undef private #include #include "bundle_constants.h" #include "bundle_mgr_proxy.h" #include "file_access_framework_errno.h" -#include "file_access_extension_info.h" #include "file_access_service_proxy.h" #include "hilog_wrapper.h" #include "hitrace_meter.h" @@ -123,6 +123,11 @@ FileAccessHelper::FileAccessHelper(const sptr &token, cMap_ = cMap; } +FileAccessHelper::FileAccessHelper() +{ + HILOG_INFO("Create FileAccessHelper by nothing"); +} + FileAccessHelper::~FileAccessHelper() { Release(); @@ -306,6 +311,54 @@ std::shared_ptr FileAccessHelper::Creator(const sptr &token) +{ + if (!token) { + HILOG_ERROR("FileAccessHelper::Init failed, token == nullptr"); + return false; + } + + std::vector wants; + if (FileAccessHelper::GetRegisteredFileAccessExtAbilityInfo(wants) != ERR_OK) { + HILOG_ERROR("FileAccessHelper::Init GetRegisteredFileAccessExtAbilityInfo failed"); + return false; + } + + std::unordered_map> cMap; + for (size_t i = 0; i < wants.size(); i++) { + sptr fileAccessExtConnection(new(std::nothrow) FileAccessExtConnection()); + if (fileAccessExtConnection == nullptr) { + HILOG_ERROR("FileAccessHelper::Init new fileAccessExtConnection fail"); + return false; + } + + if (!fileAccessExtConnection->IsExtAbilityConnected()) { + fileAccessExtConnection->ConnectFileExtAbility(wants[i], token); + } + + auto fileExtProxy = fileAccessExtConnection->GetFileExtProxy(); + if (fileExtProxy == nullptr) { + HILOG_ERROR("FileAccessHelper::Init get invalid fileExtProxy"); + return false; + } + + auto connectInfo = std::make_shared(); + if (!connectInfo) { + HILOG_ERROR("FileAccessHelper::Init, connectInfo == nullptr"); + return false; + } + + connectInfo->want = wants[i]; + connectInfo->fileAccessExtConnection = fileAccessExtConnection; + std::string bundleName = wants[i].GetElement().GetBundleName(); + cMap.insert(std::pair>(bundleName, connectInfo)); + } + + token_ = token; + cMap_ = cMap; + return true; +} + bool FileAccessHelper::Release() { for (auto iter = cMap_.begin(); iter != cMap_.end(); iter++) { @@ -1183,5 +1236,21 @@ int FileAccessHelper::MoveFile(Uri &sourceFile, Uri &targetParent, std::string & return ERR_OK; } + +IFileAccessHelper* CreateFileAccessHelper() +{ + FileAccessHelper *ptrFileAccessHelper = new (std::nothrow) FileAccessHelper(); + if (ptrFileAccessHelper == nullptr) { + HILOG_ERROR("CreateFileAccessHelper failed, new FileAccessHelper failed"); + } + return ptrFileAccessHelper; +} + +void ReleaseFileAccessHelper(IFileAccessHelper* helper) +{ + if (helper) { + delete (FileAccessHelper*)helper; + } +} } // namespace FileAccessFwk } // namespace OHOS \ No newline at end of file -- Gitee