• 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_SYNCER_H
17 #define OHOS_CLOUD_SYNC_SERVICE_DATA_SYNCER_H
18 
19 #include <list>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "cloud_pref_impl.h"
25 #include "cloud_sync_constants.h"
26 #include "data_handler.h"
27 #include "data_sync/sync_state_manager.h"
28 #include "ipc/cloud_download_callback_manager.h"
29 #include "i_cloud_download_callback.h"
30 #include "sdk_helper.h"
31 #include "sysevent.h"
32 #include "task.h"
33 
34 namespace OHOS {
35 namespace FileManagement {
36 namespace CloudSync {
37 struct DownloadContext;
38 
39 class DataSyncer {
40 public:
41     enum CleanAction {
42         RETAIN_DATA = 0,
43         CLEAR_DATA
44     };
45     DataSyncer(const std::string bundleName, const int32_t userId);
46     virtual ~DataSyncer();
47 
48     /* sync */
49     virtual int32_t StartSync(bool forceFlag, SyncTriggerType triggerType);
50     virtual int32_t StopSync(SyncTriggerType triggerType);
51     virtual int32_t StartDownloadFile(const std::string path, const int32_t userId);
52     virtual int32_t StopDownloadFile(const std::string path, const int32_t userId);
53     int32_t RegisterDownloadFileCallback(const int32_t userId,
54                                          const sptr<ICloudDownloadCallback> downloadCallback);
55     int32_t UnregisterDownloadFileCallback(const int32_t userId);
56     virtual int32_t CleanCache(const std::string &uri);
57     /* properties */
58     std::string GetBundleName() const;
59     int32_t GetUserId() const;
60     SyncState GetSyncState() const;
61 
62     /*clean*/
63     virtual int32_t Init(const std::string bundleName, const int32_t userId);
64     virtual int32_t Clean(const int action);
65     virtual int32_t DisableCloud();
66 
67     /* sdk */
68     bool HasSdkHelper();
69     void SetSdkHelper(std::shared_ptr<SdkHelper> &sdkHelper);
70     virtual int32_t Lock();
71     virtual void Unlock();
72     virtual void ForceUnlock();
73 
74     void NotifyCurrentSyncState();
75 
76     /* optimizestorage */
77     virtual int32_t OptimizeStorage(const int32_t agingDays);
78     virtual int32_t DownloadThumb(const int32_t type);
79     virtual void StopDownloadThumb();
80 
81     void SaveSubscription();
82     void DeleteSubscription();
83     void UpdateErrorCode(int32_t code);
84     /* cloud sync result */
85     ErrorType GetErrorType();
86 
87 protected:
88     /* download */
89     int32_t Pull(std::shared_ptr<DataHandler> handler);
90 
91     /* upload */
92     int32_t Push(std::shared_ptr<DataHandler> handler);
93 
94     /*clean cloud files*/
95     int32_t CleanInner(std::shared_ptr<DataHandler> handler, const int action);
96     int32_t CancelDownload(std::shared_ptr<DataHandler> handler);
97 
98     int32_t DownloadThumbInner(std::shared_ptr<DataHandler> handler);
99     void ClearCursor();
100 
101     /* schedule */
102     virtual void Schedule() = 0;
103     virtual void ScheduleByType(SyncTriggerType syncTriggerType) = 0;
104     virtual void Reset() = 0;
105     virtual int32_t Complete(bool isNeedNotify = true) = 0;
106     void Abort();
107 
108     /* download source file */
109     int32_t DownloadInner(std::shared_ptr<DataHandler> handler,
110                           const std::string path,
111                           const int32_t userId);
112 
113     /* notify */
114     virtual int32_t CompletePull();
115     int32_t CompletePush();
116     void CompleteAll(bool isNeedNotify = true);
117 
118     void BeginClean();
119     void CompleteClean();
120 
121     void BeginDisableCloud();
122     void CompleteDisableCloud();
123 
124     void SyncStateChangedNotify(const CloudSyncState state, const ErrorType error);
125     void SetErrorCodeMask(ErrorType errorType);
126 
127     /* sync rule to be supplemented and imporved */
128     bool CheckScreenAndWifi();
129 
130     /* sys event */
131     virtual int32_t InitSysEventData();
132     virtual void FreeSysEventData();
133     virtual void ReportSysEvent(uint32_t code);
134     virtual void SetFullSyncSysEvent();
135     virtual void SetCheckSysEvent();
136 
137     /* identifier */
138     const std::string bundleName_;
139     const int32_t userId_;
140 
141     /* sdk */
142     std::shared_ptr<SdkHelper> sdkHelper_;
143     SdkLock lock_;
144 
145     uint32_t errorCode_{0};
146 
147     bool isDataChanged_{false};
148 
149     uint64_t startTime_;
150     SyncTriggerType triggerType_;
151     std::shared_ptr<bool> stopFlag_ = std::make_shared<bool>(false);
152 
153 private:
154     /* download */
155     void PullRecords(std::shared_ptr<TaskContext> context);
156     void PullDatabaseChanges(std::shared_ptr<TaskContext> context);
157     void PullRecordsWithId(std::shared_ptr<TaskContext> context, const std::vector<DriveKit::DKRecordId> &records,
158         bool retry);
159     void DownloadAssets(DownloadContext &ctx);
160     void DownloadThumbAssets(DownloadContext ctx);
161     void RetryDownloadRecords(std::shared_ptr<TaskContext> context);
162     /* dowload callback */
163     void OnFetchRecords(const std::shared_ptr<DriveKit::DKContext>, std::shared_ptr<const DriveKit::DKDatabase>,
164         std::shared_ptr<std::vector<DriveKit::DKRecord>>, DriveKit::DKQueryCursor,
165         const DriveKit::DKError &);
166     void OnFetchCheckRecords(const std::shared_ptr<DriveKit::DKContext>, std::shared_ptr<const DriveKit::DKDatabase>,
167         std::shared_ptr<std::vector<DriveKit::DKRecord>>, DriveKit::DKQueryCursor,
168         const DriveKit::DKError &);
169     void OnFetchRecordWithId(std::shared_ptr<DriveKit::DKContext>, std::shared_ptr<DriveKit::DKDatabase>,
170         DriveKit::DKRecordId, const DriveKit::DKRecord &, const DriveKit::DKError &);
171     int HandleOnFetchRecords(const std::shared_ptr<DownloadTaskContext> context,
172         std::shared_ptr<const DriveKit::DKDatabase> database,
173         std::shared_ptr<std::vector<DriveKit::DKRecord>> records, bool checkOrRetry);
174     void OnFetchDatabaseChanges(const std::shared_ptr<DriveKit::DKContext>,
175         std::shared_ptr<const DriveKit::DKDatabase>,
176         std::shared_ptr<std::vector<DriveKit::DKRecord>>, DriveKit::DKQueryCursor,
177         bool, const DriveKit::DKError &);
178     void FetchRecordsDownloadCallback(std::shared_ptr<DriveKit::DKContext> context,
179                                       std::shared_ptr<const DriveKit::DKDatabase> database,
180                                       const std::map<DriveKit::DKDownloadAsset, DriveKit::DKDownloadResult> &resultMap,
181                                       const DriveKit::DKError &err);
182     void FetchThumbDownloadCallback(std::shared_ptr<DriveKit::DKContext> context,
183                                       std::shared_ptr<const DriveKit::DKDatabase> database,
184                                       const std::map<DriveKit::DKDownloadAsset, DriveKit::DKDownloadResult> &resultMap,
185                                       const DriveKit::DKError &err);
186 
187     /* upload */
188     void CreateRecords(std::shared_ptr<TaskContext> context);
189     void DeleteRecords(std::shared_ptr<TaskContext> context);
190     void ModifyMdirtyRecords(std::shared_ptr<TaskContext> context);
191     void ModifyFdirtyRecords(std::shared_ptr<TaskContext> context);
192     void StopUploadAssets();
193 
194     /* upload callback */
195     void OnCreateRecords(std::shared_ptr<DriveKit::DKContext>,
196         std::shared_ptr<const DriveKit::DKDatabase>,
197         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
198         const DriveKit::DKError &);
199     void OnDeleteRecords(std::shared_ptr<DriveKit::DKContext>,
200         std::shared_ptr<const DriveKit::DKDatabase>,
201         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
202         const DriveKit::DKError &);
203     void OnModifyMdirtyRecords(std::shared_ptr<DriveKit::DKContext>,
204         std::shared_ptr<const DriveKit::DKDatabase>,
205         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
206         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
207         const DriveKit::DKError &);
208     void OnModifyFdirtyRecords(std::shared_ptr<DriveKit::DKContext>,
209         std::shared_ptr<const DriveKit::DKDatabase>,
210         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
211         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
212         const DriveKit::DKError &);
213 
214     /* transaction */
215     void BeginTransaction();
216     void EndTransaction();
217 
218     /* async task wrapper */
219     int32_t AsyncRun(std::shared_ptr<TaskContext> context,
220         void(DataSyncer::*f)(std::shared_ptr<TaskContext>));
221     template<typename T, typename RET, typename... ARGS>
222     std::function<RET(ARGS...)> AsyncCallback(RET(T::*f)(ARGS...));
223     /* state management */
224     SyncStateManager syncStateManager_;
225 
226     /* task management */
227     std::shared_ptr<TaskRunner> taskRunner_;
228 
229     /* download callback manager*/
230     CloudDownloadCallbackManager downloadCallbackMgr_;
231 
232     /* Current sync state */
233     CloudSyncState CurrentSyncState_{CloudSyncState::COMPLETED};
234 
235     /* Current error type */
236     ErrorType CurrentErrorType_{ErrorType::NO_ERROR};
237 };
238 } // namespace CloudSync
239 } // namespace FileManagement
240 } // namespace OHOS
241 #endif // OHOS_CLOUD_SYNC_SERVICE_DATA_SYNCER_H
242