• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 OHOS_STORAGE_DAEMON_STORAGE_DAEMON_PROVIDER_H
17 #define OHOS_STORAGE_DAEMON_STORAGE_DAEMON_PROVIDER_H
18 
19 #include "storage_daemon_stub.h"
20 #include "storage_service_constant.h"
21 #include "storage_daemon.h"
22 #include "system_ability_status_change_stub.h"
23 #include "utils/storage_statistics_radar.h"
24 #include <mutex>
25 #include <thread>
26 #include <vector>
27 
28 namespace OHOS {
29 namespace StorageDaemon {
30 class StorageDaemonProvider : public StorageDaemonStub {
31 public:
32     StorageDaemonProvider();
33     ~StorageDaemonProvider();
34 
35     virtual int32_t Shutdown() override;
36     virtual int32_t Mount(const std::string &volId, uint32_t flags) override;
37     virtual int32_t UMount(const std::string &volId) override;
38     virtual int32_t TryToFix(const std::string &volId, uint32_t flags) override;
39     virtual int32_t Check(const std::string &volId) override;
40     virtual int32_t Format(const std::string &volId, const std::string &fsType) override;
41     virtual int32_t Partition(const std::string &diskId, int32_t type) override;
42     virtual int32_t SetVolumeDescription(const std::string &volId, const std::string &description) override;
43     virtual int32_t QueryUsbIsInUse(const std::string &diskPath, bool &isInUse) override;
44 
45     virtual int32_t StartUser(int32_t userId) override;
46     virtual int32_t StopUser(int32_t userId) override;
47     virtual int32_t PrepareUserDirs(int32_t userId, uint32_t flags) override;
48     virtual int32_t DestroyUserDirs(int32_t userId, uint32_t flags) override;
49     virtual int32_t CompleteAddUser(int32_t userId) override;
50 
51     // fscrypt api, add fs mutex in KeyManager
52     virtual int32_t InitGlobalKey(void) override;
53     virtual int32_t InitGlobalUserKeys(void) override;
54     virtual int32_t GenerateUserKeys(uint32_t userId, uint32_t flags) override;
55     virtual int32_t DeleteUserKeys(uint32_t userId) override;
56     virtual int32_t UpdateUserAuth(uint32_t userId,
57                                    uint64_t secureUid,
58                                    const std::vector<uint8_t> &token,
59                                    const std::vector<uint8_t> &oldSecret,
60                                    const std::vector<uint8_t> &newSecret) override;
61     virtual int32_t UpdateUseAuthWithRecoveryKey(const std::vector<uint8_t> &authToken,
62                                                  const std::vector<uint8_t> &newSecret,
63                                                  uint64_t secureUid,
64                                                  uint32_t userId,
65                                                  const std::vector<std::vector<uint8_t>> &plainText) override;
66     virtual int32_t ActiveUserKey(uint32_t userId,
67                                   const std::vector<uint8_t> &token,
68                                   const std::vector<uint8_t> &secret) override;
69     virtual int32_t InactiveUserKey(uint32_t userId) override;
70     virtual int32_t UpdateKeyContext(uint32_t userId, bool needRemoveTmpKey = false) override;
71     virtual int32_t MountCryptoPathAgain(uint32_t userId) override;
72     virtual int32_t LockUserScreen(uint32_t userId) override;
73     virtual int32_t UnlockUserScreen(uint32_t userId,
74                                      const std::vector<uint8_t> &token,
75                                      const std::vector<uint8_t> &secret) override;
76     virtual int32_t GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus) override;
77     virtual int32_t GenerateAppkey(uint32_t userId, uint32_t hashId, std::string &keyId, bool needReSet) override;
78     virtual int32_t DeleteAppkey(uint32_t userId, const std::string &keyId) override;
79     virtual int32_t CreateRecoverKey(uint32_t userId,
80                                      uint32_t userType,
81                                      const std::vector<uint8_t> &token,
82                                      const std::vector<uint8_t> &secret) override;
83     virtual int32_t SetRecoverKey(const std::vector<uint8_t> &key) override;
84 
85     // app file share api
86     virtual int32_t CreateShareFile(const StorageFileRawData &rawData,
87                                     uint32_t tokenId,
88                                     uint32_t flag,
89                                     std::vector<int32_t> &funcResult) override;
90     virtual int32_t DeleteShareFile(uint32_t tokenId, const StorageFileRawData &uriList) override;
91 
92     virtual int32_t SetBundleQuota(const std::string &bundleName,
93                                    int32_t uid,
94                                    const std::string &bundleDataDirPath,
95                                    int32_t limitSizeMb) override;
96     virtual int32_t GetOccupiedSpace(int32_t idType, int32_t id, int64_t &size) override;
97     virtual int32_t UpdateMemoryPara(int32_t size, int32_t &oldSize) override;
98     virtual int32_t MountDfsDocs(int32_t userId,
99                                  const std::string &relativePath,
100                                  const std::string &networkId,
101                                  const std::string &deviceId) override;
102     virtual int32_t UMountDfsDocs(int32_t userId,
103                                   const std::string &relativePath,
104                                   const std::string &networkId,
105                                   const std::string &deviceId) override;
106     virtual int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false) override;
107     virtual int32_t GetUserNeedActiveStatus(uint32_t userId, bool &needActive) override;
108     // media fuse
109     virtual int32_t MountMediaFuse(int32_t userId, int32_t &devFd) override;
110     virtual int32_t UMountMediaFuse(int32_t userId) override;
111     // file mgr fuse
112     virtual int32_t MountFileMgrFuse(int32_t userId, const std::string &path, int32_t &fuseFd) override;
113     virtual int32_t UMountFileMgrFuse(int32_t userId, const std::string &path) override;
114     virtual int32_t IsFileOccupied(const std::string &path,
115                                    const std::vector<std::string> &inputList,
116                                    std::vector<std::string> &outputList,
117                                    bool &isOccupy) override;
118     virtual int32_t ResetSecretWithRecoveryKey(uint32_t userId,
119                                                uint32_t rkType,
120                                                const std::vector<uint8_t> &key) override;
121     // cross device
122     virtual int32_t MountDisShareFile(int32_t userId, const std::map<std::string, std::string> &shareFiles) override;
123     virtual int32_t UMountDisShareFile(int32_t userId, const std::string &networkId) override;
124     virtual int32_t RegisterUeceActivationCallback(
125         const sptr<StorageManager::IUeceActivationCallback> &ueceCallback) override;
126     virtual int32_t UnregisterUeceActivationCallback() override;
127     virtual int32_t InactiveUserPublicDirKey(uint32_t userId) override;
128     virtual int32_t QueryOccupiedSpaceForSa(const std::string &storageStatus) override;
129     virtual int32_t MountUsbFuse(const std::string &volumeId, std::string &fsUuid, int &fuseFd) override;
130     class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub {
131     public:
132         SystemAbilityStatusChangeListener() = default;
133         ~SystemAbilityStatusChangeListener() = default;
134         void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId);
135         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId);
136     };
137 
138     std::mutex mutex_;
139     std::mutex mutexStats_;
140     void StorageRadarThd(void);
141 
142 private:
143     std::atomic<bool> stopRadarReport_{false};
144     std::condition_variable execRadarReportCon_;
145     std::mutex onRadarReportLock_;
146     std::atomic<bool> isNeedUpdateRadarFile_{false};
147     std::thread callRadarStatisticReportThread_;
148     std::map<uint32_t, RadarStatisticInfo> opStatistics_;
149     std::chrono::time_point<std::chrono::system_clock> lastRadarReportTime_;
150     std::map<uint32_t, RadarStatisticInfo>::iterator GetUserStatistics(const uint32_t userId);
151     void GetTempStatistics(std::map<uint32_t, RadarStatisticInfo> &statistics);
152     int32_t RawDataToStringVec(const StorageFileRawData &rawData, std::vector<std::string> &stringVec);
153 };
154 } // namespace StorageDaemon
155 } // namespace OHOS
156 #endif // OHOS_STORAGE_DAEMON_STORAGE_DAEMON_PROVIDER_H
157