• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_CLOUD_SYNC_SERVICE_DATA_HANDLER_H
17 #define OHOS_CLOUD_SYNC_SERVICE_DATA_HANDLER_H
18 
19 #include <mutex>
20 
21 #include "cloud_pref_impl.h"
22 #include "sdk_helper.h"
23 #include "sysevent.h"
24 #include "values_bucket.h"
25 
26 namespace OHOS {
27 namespace FileManagement {
28 namespace CloudSync {
29 struct OnFetchParams {
30     int32_t totalPullCount{0};
31     std::vector<DriveKit::DKDownloadAsset> assetsToDownload{};
32     std::vector<NativeRdb::ValuesBucket> insertFiles{};
33     std::map<std::string, std::set<int>> recordAlbumMaps{};
34 };
35 const static std::string DOWNLOAD_THUMB_LIMIT = "download_thumb_limit";
36 const static std::string BATCH_NO = "batch_no";
37 const static std::string RECORD_SIZE = "record_size";
38 const static std::string CHECKING_FLAG = "checking_flag";
39 class DataHandler {
40 public:
41     enum DownloadThmType {
42         SYNC_TRIGGER = 1,
43         SCREENOFF_TRIGGER
44     };
45     DataHandler(int32_t userId, const std::string &bundleName, const std::string &table);
46     /* download */
47     virtual void GetFetchCondition(FetchCondition &cond) = 0;
48     virtual int32_t OnFetchRecords(std::shared_ptr<std::vector<DriveKit::DKRecord>> &records,
49                                    OnFetchParams &params) = 0;
50     virtual int32_t GetRetryRecords(std::vector<DriveKit::DKRecordId> &records) = 0;
51     virtual int32_t GetCheckRecords(std::vector<DriveKit::DKRecordId> &checkRecords,
52                                     const std::shared_ptr<std::vector<DriveKit::DKRecord>> &records);
53     virtual int32_t GetAssetsToDownload(std::vector<DriveKit::DKDownloadAsset> &outAssetsToDownload);
54     virtual int32_t GetThumbToDownload(std::vector<DriveKit::DKDownloadAsset> &outAssetsToDownload);
55     virtual int32_t GetDownloadAsset(std::string cloudId,
56                                      std::vector<DriveKit::DKDownloadAsset> &outAssetsToDownload);
57     /* upload */
58     virtual int32_t GetCreatedRecords(std::vector<DriveKit::DKRecord> &records) = 0;
59     virtual int32_t GetDeletedRecords(std::vector<DriveKit::DKRecord> &records) = 0;
60     virtual int32_t GetMetaModifiedRecords(std::vector<DriveKit::DKRecord> &records) = 0;
61     virtual int32_t GetFileModifiedRecords(std::vector<DriveKit::DKRecord> &records);
62 
63     /* upload callback */
64     virtual int32_t OnCreateRecords(const std::map<DriveKit::DKRecordId,
65         DriveKit::DKRecordOperResult> &map) = 0;
66     virtual int32_t OnDeleteRecords(const std::map<DriveKit::DKRecordId,
67         DriveKit::DKRecordOperResult> &map) = 0;
68     virtual int32_t OnModifyMdirtyRecords(const std::map<DriveKit::DKRecordId,
69         DriveKit::DKRecordOperResult> &map) = 0;
70     virtual int32_t OnModifyFdirtyRecords(const std::map<DriveKit::DKRecordId,
71         DriveKit::DKRecordOperResult> &map);
72     virtual int32_t OnDownloadSuccess(const DriveKit::DKDownloadAsset &asset);
73     virtual int32_t OnDownloadAssets(const std::map<DriveKit::DKDownloadAsset, DriveKit::DKDownloadResult> &resultMap);
74     virtual int32_t OnDownloadAssets(const DriveKit::DKDownloadAsset &asset);
75     virtual int32_t OnDownloadAssetsFailure(const std::vector<DriveKit::DKDownloadAsset> &assets);
76 
77     /*clean*/
78     virtual int32_t Clean(const int action);
79 
80     /* cursor */
81     virtual void GetStartCursor(DriveKit::DKQueryCursor &cursor);
82     virtual void GetNextCursor(DriveKit::DKQueryCursor &cursor);
83     virtual void SetTempStartCursor(const DriveKit::DKQueryCursor &cursor);
84     virtual void GetTempStartCursor(DriveKit::DKQueryCursor &cursor);
85     virtual void SetTempNextCursor(const DriveKit::DKQueryCursor &cursor, bool isFinish);
86     virtual int32_t GetBatchNo();
87     virtual bool IsPullRecords();
88     virtual void ClearCursor();
89     virtual void FinishPull(const int32_t barchNo);
90     virtual void SetRecordSize(const int32_t recordSize);
91     virtual int32_t GetRecordSize();
92     virtual bool GetCheckFlag();
93     virtual void SetChecking();
94     void SetDownloadType(const int32_t type);
95     int32_t GetDownloadType();
96     virtual int32_t HandleDetailcode(DriveKit::DKDetailErrorCode detailCode);
97 
98 protected:
99     int32_t OnRecordFailed(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry);
100     void GetReturn(const int32_t error, int32_t &retCode);
101 
102     /* cursor */
103     DriveKit::DKQueryCursor startCursor_ = "";
104     DriveKit::DKQueryCursor nextCursor_;
105     DriveKit::DKQueryCursor tempStartCursor_;
106     DriveKit::DKQueryCursor tempNextCursor_;
107     int32_t batchNo_{0};
108     int32_t recordSize_{0};
109     int32_t downloadThumbLimit_{0};
110     bool isFinish_{false};
111     std::map<int32_t, DriveKit::DKQueryCursor> cursorMap_;
112     std::map<int32_t, bool> cursorFinishMap_;
113     bool isChecking_{false};
114     int32_t downloadType_{0};
115 
116     std::mutex mutex_;
117 
118     /* cloud preference impl */
119     CloudPrefImpl cloudPrefImpl_;
120 
121 private:
122     int32_t HandleCloudSpaceNotEnough();
123     int32_t HandleNotSupportSync();
124     int32_t HandleNetworkErr();
125 };
126 } // namespace CloudSync
127 } // namespace FileManagement
128 } // namespace OHOS
129 #endif // OHOS_CLOUD_SYNC_SERVICE_DATA_HANDLER_H
130