1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FILE_ACCESS_HELPER_H 17 #define FILE_ACCESS_HELPER_H 18 19 #include <functional> 20 #include <unordered_map> 21 #include <string> 22 #include <vector> 23 #include <utility> 24 25 #include "bundle_mgr_interface.h" 26 #include "context.h" 27 #include "file_access_ext_connection.h" 28 #include "file_access_extension_info.h" 29 #include "ifile_access_ext_base.h" 30 #include "image_source.h" 31 #include "iobserver_callback.h" 32 #include "iremote_object.h" 33 #include "refbase.h" 34 #include "uri.h" 35 #include "want.h" 36 37 using Uri = OHOS::Uri; 38 39 namespace OHOS { 40 namespace FileAccessFwk { 41 using namespace Media; 42 using string = std::string; 43 44 namespace { 45 static const std::string FILE_SCHEME_NAME = "file"; 46 static const std::string MEDIA_BNUDLE_NAME_ALIAS = "media"; 47 static const std::string MEDIA_BNUDLE_NAME = "com.ohos.medialibrary.medialibrarydata"; 48 static const std::string EXTERNAL_BNUDLE_NAME_ALIAS = "docs"; 49 static const std::string EXTERNAL_BNUDLE_NAME = "com.ohos.UserFile.ExternalFileManager"; 50 } 51 52 struct ConnectInfo { 53 AAFwk::Want want = {}; 54 sptr<FileAccessExtConnection> fileAccessExtConnection = nullptr; 55 }; 56 57 class FileAccessHelper final : public std::enable_shared_from_this<FileAccessHelper> { 58 public: 59 ~FileAccessHelper() = default; 60 // get all ability want info 61 static int GetRegisteredFileAccessExtAbilityInfo(std::vector<AAFwk::Want> &wantVec); 62 // create and connect all ability 63 static std::pair<std::shared_ptr<FileAccessHelper>, int> 64 Creator(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context); 65 // create and connect with want, if created, only connect with want 66 static std::pair<std::shared_ptr<FileAccessHelper>, int> 67 Creator(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const std::vector<AAFwk::Want> &wants); 68 static std::shared_ptr<FileAccessHelper> Creator(const sptr<IRemoteObject> &token, 69 const std::vector<AAFwk::Want> &wants); 70 71 bool Release(); 72 int Access(Uri &uri, bool &isExist); 73 int OpenFile(Uri &uri, const int flags, int &fd); 74 int CreateFile(Uri &parent, const std::string &displayName, Uri &newFile); 75 int Mkdir(Uri &parent, const std::string &displayName, Uri &newDir); 76 int Delete(Uri &selectFile); 77 int Move(Uri &sourceFile, Uri &targetParent, Uri &newFile); 78 int Copy(Uri &sourceUri, Uri &destUri, std::vector<CopyResult> ©Result, bool force = false); 79 int Rename(Uri &sourceFile, const std::string &displayName, Uri &newFile); 80 int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, const FileFilter &filter, 81 std::vector<FileInfo> &fileInfoVec); 82 int ScanFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, const FileFilter &filter, 83 std::vector<FileInfo> &fileInfoVec); 84 int GetThumbnail(Uri &uri, ThumbnailSize &thumbnailSize, std::shared_ptr<PixelMap> &pixelMap); 85 int Query(Uri &uri, std::string &metaJson); 86 int GetFileInfoFromUri(Uri &selectFile, FileInfo &fileInfo); 87 int GetFileInfoFromRelativePath(std::string &selectFile, FileInfo &fileInfo); 88 int GetRoots(std::vector<RootInfo> &rootInfoVec); 89 int RegisterNotify(Uri uri, bool notifyForDescendants, sptr<IFileAccessObserver> &observer); 90 int UnregisterNotify(Uri uri, sptr<IFileAccessObserver> &observer); 91 int UnregisterNotify(Uri uri); 92 private: 93 int StartWatcher(Uri &uri); 94 int StopWatcher(Uri &uri, bool isUnregisterAll); 95 sptr<IFileAccessExtBase> GetProxyByUri(Uri &uri); 96 sptr<IFileAccessExtBase> GetProxyByBundleName(const std::string &bundleName); 97 bool GetProxy(); 98 static sptr<AppExecFwk::IBundleMgr> GetBundleMgrProxy(); 99 FileAccessHelper(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, 100 const std::unordered_map<std::string, std::shared_ptr<ConnectInfo>> &cMap); 101 FileAccessHelper(const sptr<IRemoteObject> &token, 102 const std::unordered_map<std::string, std::shared_ptr<ConnectInfo>> &cMap); 103 104 void AddFileAccessDeathRecipient(const sptr<IRemoteObject> &token); 105 void OnSchedulerDied(const wptr<IRemoteObject> &remote); 106 107 std::shared_ptr<ConnectInfo> GetConnectInfo(const std::string &bundleName); 108 109 int CopyOperation(Uri &sourceUri, Uri &destUri, std::vector<CopyResult> ©Result, bool force = false); 110 int IsDirectory(Uri &uri, bool &isDir); 111 112 sptr<IRemoteObject> token_ = nullptr; 113 std::unordered_map<std::string, std::shared_ptr<ConnectInfo>> cMap_; 114 115 static std::vector<AAFwk::Want> wants_; 116 static std::string GetKeyOfWants(const AAFwk::Want &want); 117 118 sptr<IRemoteObject::DeathRecipient> callerDeathRecipient_ = nullptr; 119 }; 120 121 class FileAccessDeathRecipient : public IRemoteObject::DeathRecipient { 122 public: 123 using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>; 124 explicit FileAccessDeathRecipient(RemoteDiedHandler handler); 125 virtual ~FileAccessDeathRecipient(); 126 virtual void OnRemoteDied(const wptr<IRemoteObject> &remote); 127 128 private: 129 RemoteDiedHandler handler_; 130 }; 131 } // namespace FileAccessFwk 132 } // namespace OHOS 133 #endif // FILE_ACCESS_HELPER_H 134