1 /*
2  * Copyright (c) 2022-2024 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 <mutex>
21 #include <string>
22 #include <unordered_map>
23 #include <utility>
24 #include <vector>
25 
26 #include "bundle_mgr_interface.h"
27 #include "context.h"
28 #include "app_file_access_ext_connection.h"
29 #include "file_access_extension_info.h"
30 #include "ifile_access_ext_base.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 string = std::string;
42 
43 namespace {
44     static const std::string FILE_SCHEME_NAME = "file";
45     static const std::string MEDIA_BNUDLE_NAME_ALIAS = "media";
46     static const std::string MEDIA_BNUDLE_NAME = "com.ohos.medialibrary.medialibrarydata";
47     static const std::string EXTERNAL_BNUDLE_NAME_ALIAS = "docs";
48     static const std::string EXTERNAL_BNUDLE_NAME = "com.ohos.UserFile.ExternalFileManager";
49 }
50 
51 struct ConnectInfo {
52     AAFwk::Want want = {};
53     sptr<AppFileAccessExtConnection> fileAccessExtConnection = nullptr;
54 };
55 
56 class FileAccessHelper final : public std::enable_shared_from_this<FileAccessHelper> {
57 public:
58     ~FileAccessHelper();
59     // get all ability want info
60     static int GetRegisteredFileAccessExtAbilityInfo(std::vector<AAFwk::Want> &wantVec);
61     // create and connect all ability
62     static std::pair<std::shared_ptr<FileAccessHelper>, int>
63         Creator(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context);
64     // create and connect with want, if created, only connect with want
65     static std::pair<std::shared_ptr<FileAccessHelper>, int>
66         Creator(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const std::vector<AAFwk::Want> &wants);
67     static std::shared_ptr<FileAccessHelper> Creator(const sptr<IRemoteObject> &token,
68         const std::vector<AAFwk::Want> &wants);
69 
70     bool Release();
71     int Access(Uri &uri, bool &isExist);
72     int OpenFile(Uri &uri, const int flags, int &fd);
73     int CreateFile(Uri &parent, const std::string &displayName, Uri &newFile);
74     int Mkdir(Uri &parent, const std::string &displayName, Uri &newDir);
75     int Delete(Uri &selectFile);
76     int Move(Uri &sourceFile, Uri &targetParent, Uri &newFile);
77     int Copy(Uri &sourceUri, Uri &destUri, std::vector<Result> &copyResult, bool force = false);
78     int CopyFile(Uri &sourceUri, Uri &destUri, const std::string &fileName, Uri &newFileUri);
79     int Rename(Uri &sourceFile, const std::string &displayName, Uri &newFile);
80     int ListFile(const FileInfo &fileInfo, const int64_t offset, const FileFilter &filter, SharedMemoryInfo &memInfo);
81     int ScanFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, const FileFilter &filter,
82         std::vector<FileInfo> &fileInfoVec);
83     int Query(Uri &uri, std::string &metaJson);
84     int GetFileInfoFromUri(Uri &selectFile, FileInfo &fileInfo);
85     int GetFileInfoFromRelativePath(std::string &selectFile, FileInfo &fileInfo);
86     int GetRoots(std::vector<RootInfo> &rootInfoVec);
87     int RegisterNotify(Uri uri, bool notifyForDescendants, sptr<IFileAccessObserver> &observer);
88     int UnregisterNotify(Uri uri, sptr<IFileAccessObserver> &observer);
89     int UnregisterNotify(Uri uri);
90     int MoveItem(Uri &sourceFile, Uri &targetParent, std::vector<Result> &moveResult, bool force);
91     int MoveFile(Uri &sourceFile, Uri &targetParent, std::string &fileName, Uri &newFile);
92 private:
93     sptr<IFileAccessExtBase> GetProxyByUri(Uri &uri);
94     sptr<IFileAccessExtBase> GetProxyByBundleName(const std::string &bundleName);
95     bool GetProxy();
96     static sptr<AppExecFwk::IBundleMgr> GetBundleMgrProxy();
97     static std::pair<std::shared_ptr<FileAccessHelper>, int> DoCreatorHelper(const sptr<IRemoteObject> &token,
98         const std::vector<AAFwk::Want> &wants);
99     static bool IsEffectiveWants(const std::vector<AAFwk::Want> &wants);
100     static bool CompareWant(const AAFwk::Want& lhs, const AAFwk::Want& rhs);
101     FileAccessHelper(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
102         const std::unordered_map<std::string, std::shared_ptr<ConnectInfo>> &cMap);
103     FileAccessHelper(const sptr<IRemoteObject> &token,
104         const std::unordered_map<std::string, std::shared_ptr<ConnectInfo>> &cMap);
105 
106     std::shared_ptr<ConnectInfo> GetConnectInfo(const std::string &bundleName);
107     std::shared_ptr<ConnectExtensionInfo> GetConnectExtensionInfo(Uri &uri);
108 
109     int CopyOperation(Uri &sourceUri, Uri &destUri, std::vector<Result> &copyResult, bool force = false);
110     int CopyFileOperation(Uri &sourceUri, Uri &destUri, const std::string &fileName, Uri &newFileUri);
111     int IsDirectory(Uri &uri, bool &isDir);
112 
113     sptr<IRemoteObject> token_ = nullptr;
114     std::unordered_map<std::string, std::shared_ptr<ConnectInfo>> cMap_;
115 };
116 } // namespace FileAccessFwk
117 } // namespace OHOS
118 #endif // FILE_ACCESS_HELPER_H