• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "file_access_notify_agent.h"
30 #include "ifile_access_ext_base.h"
31 #include "inotify_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 SCHEME_NAME = "datashare";
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 int32_t READ = 0;
48     static const int32_t WRITE = 1;
49     static const int32_t WRITE_READ = 2;
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 Rename(Uri &sourceFile, const std::string &displayName, Uri &newFile);
79     int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, const FileFilter &filter,
80         std::vector<FileInfo> &fileInfoVec);
81     int ScanFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, const FileFilter &filter,
82         std::vector<FileInfo> &fileInfoVec);
83     int UriToFileInfo(Uri &selectFile, FileInfo &fileInfo);
84     int GetRoots(std::vector<RootInfo> &rootInfoVec);
85     int On(std::shared_ptr<INotifyCallback> &callback);
86     int Off();
87 private:
88     sptr<IFileAccessExtBase> GetProxyByUri(Uri &uri);
89     bool GetProxy();
90     static sptr<AppExecFwk::IBundleMgr> GetBundleMgrProxy();
91     FileAccessHelper(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
92         const std::unordered_map<std::string, std::shared_ptr<ConnectInfo>> &cMap);
93     FileAccessHelper(const sptr<IRemoteObject> &token,
94         const std::unordered_map<std::string, std::shared_ptr<ConnectInfo>> &cMap);
95 
96     void AddFileAccessDeathRecipient(const sptr<IRemoteObject> &token);
97     void OnSchedulerDied(const wptr<IRemoteObject> &remote);
98 
99     std::shared_ptr<ConnectInfo> GetConnectInfo(const std::string &bundleName);
100 
101     sptr<IRemoteObject> token_ = nullptr;
102     std::unordered_map<std::string, std::shared_ptr<ConnectInfo>> cMap_;
103 
104     static std::vector<AAFwk::Want> wants_;
105     static std::string GetKeyOfWants(const AAFwk::Want &want);
106 
107     sptr<IRemoteObject::DeathRecipient> callerDeathRecipient_ = nullptr;
108 
109     std::mutex notifyAgentMutex_;
110     sptr<IFileAccessNotify> notifyAgent_ = nullptr;
111 };
112 
113 class FileAccessDeathRecipient : public IRemoteObject::DeathRecipient {
114 public:
115     using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>;
116     explicit FileAccessDeathRecipient(RemoteDiedHandler handler);
117     virtual ~FileAccessDeathRecipient();
118     virtual void OnRemoteDied(const wptr<IRemoteObject> &remote);
119 
120 private:
121     RemoteDiedHandler handler_;
122 };
123 } // namespace FileAccessFwk
124 } // namespace OHOS
125 #endif // FILE_ACCESS_HELPER_H
126