• 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 "task.h"
32 
33 namespace OHOS {
34 namespace FileManagement {
35 namespace CloudSync {
36 enum class SyncTriggerType : int32_t {
37     APP_TRIGGER,
38     CLOUD_TRIGGER,
39     PENDING_TRIGGER,
40     BATTERY_OK_TRIGGER,
41     NETWORK_AVAIL_TRIGGER,
42 };
43 struct DownloadContext;
44 
45 class DataSyncer {
46 public:
47     DataSyncer(const std::string bundleName, const int32_t userId);
48     virtual ~DataSyncer();
49 
50     /* sync */
51     virtual int32_t StartSync(bool forceFlag, SyncTriggerType triggerType);
52     virtual int32_t StopSync(SyncTriggerType triggerType);
53     virtual int32_t StartDownloadFile(const std::string path, const int32_t userId);
54     virtual int32_t StopDownloadFile(const std::string path, const int32_t userId);
55     int32_t RegisterDownloadFileCallback(const int32_t userId,
56                                          const sptr<ICloudDownloadCallback> downloadCallback);
57     int32_t UnregisterDownloadFileCallback(const int32_t userId);
58     /* properties */
59     std::string GetBundleName() const;
60     int32_t GetUserId() const;
61     SyncState GetSyncState() const;
62 
63     /*clean*/
64     virtual int32_t Init(const std::string bundleName, const int32_t userId);
65     virtual int32_t Clean(const int action);
66 
67     /* sdk */
68     void SetSdkHelper(std::shared_ptr<SdkHelper> &sdkHelper);
69     int32_t Lock();
70     void Unlock();
71     void ForceUnlock();
72 
73     void NotifyCurrentSyncState();
74 
75 protected:
76     /* download */
77     int32_t Pull(std::shared_ptr<DataHandler> handler);
78 
79     /* upload */
80     int32_t Push(std::shared_ptr<DataHandler> handler);
81 
82     /*clean cloud files*/
83     int32_t CleanInner(std::shared_ptr<DataHandler> handler, const int action);
84 
85     void ClearCursor();
86 
87     void SaveSubscription();
88     void DeleteSubscription();
89     /* schedule */
90     virtual void Schedule() = 0;
91     virtual void Reset() = 0;
92     void Abort();
93 
94     /* download source file */
95     int32_t DownloadInner(std::shared_ptr<DataHandler> handler,
96                           const std::string path,
97                           const int32_t userId);
98 
99     /* notify */
100     int32_t CompletePull();
101     int32_t CompletePush();
102     void CompleteAll(bool isNeedNotify = true);
103 
104     void SyncStateChangedNotify(const CloudSyncState state, const ErrorType error);
105 
106 private:
107     /* download */
108     void PullRecords(std::shared_ptr<TaskContext> context);
109     void PullDatabaseChanges(std::shared_ptr<TaskContext> context);
110     void PullRecordsWithId(std::shared_ptr<TaskContext> context, const std::vector<DriveKit::DKRecordId> &records,
111         bool retry);
112     void DownloadAssets(DownloadContext &ctx);
113     void RetryDownloadRecords(std::shared_ptr<TaskContext> context);
114     /* dowload callback */
115     void OnFetchRecords(const std::shared_ptr<DriveKit::DKContext>, std::shared_ptr<const DriveKit::DKDatabase>,
116         std::shared_ptr<std::vector<DriveKit::DKRecord>>, DriveKit::DKQueryCursor,
117         const DriveKit::DKError &);
118     void OnFetchCheckRecords(const std::shared_ptr<DriveKit::DKContext>, std::shared_ptr<const DriveKit::DKDatabase>,
119         std::shared_ptr<std::vector<DriveKit::DKRecord>>, DriveKit::DKQueryCursor,
120         const DriveKit::DKError &);
121     void OnFetchRecordWithId(std::shared_ptr<DriveKit::DKContext>, std::shared_ptr<DriveKit::DKDatabase>,
122         DriveKit::DKRecordId, const DriveKit::DKRecord &, const DriveKit::DKError &);
123     int HandleOnFetchRecords(const std::shared_ptr<DownloadTaskContext> context,
124         std::shared_ptr<const DriveKit::DKDatabase> database,
125         std::shared_ptr<std::vector<DriveKit::DKRecord>> records, bool checkOrRetry);
126     void OnFetchDatabaseChanges(const std::shared_ptr<DriveKit::DKContext>,
127         std::shared_ptr<const DriveKit::DKDatabase>,
128         std::shared_ptr<std::vector<DriveKit::DKRecord>>, DriveKit::DKQueryCursor,
129         bool, const DriveKit::DKError &);
130 
131     /* upload */
132     void CreateRecords(std::shared_ptr<TaskContext> context);
133     void DeleteRecords(std::shared_ptr<TaskContext> context);
134     void ModifyMdirtyRecords(std::shared_ptr<TaskContext> context);
135     void ModifyFdirtyRecords(std::shared_ptr<TaskContext> context);
136 
137     /* upload callback */
138     void OnCreateRecords(std::shared_ptr<DriveKit::DKContext>,
139         std::shared_ptr<const DriveKit::DKDatabase>,
140         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
141         const DriveKit::DKError &);
142     void OnDeleteRecords(std::shared_ptr<DriveKit::DKContext>,
143         std::shared_ptr<const DriveKit::DKDatabase>,
144         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
145         const DriveKit::DKError &);
146     void OnModifyMdirtyRecords(std::shared_ptr<DriveKit::DKContext>,
147         std::shared_ptr<const DriveKit::DKDatabase>,
148         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
149         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
150         const DriveKit::DKError &);
151     void OnModifyFdirtyRecords(std::shared_ptr<DriveKit::DKContext>,
152         std::shared_ptr<const DriveKit::DKDatabase>,
153         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
154         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
155         const DriveKit::DKError &);
156 
157     /* transaction */
158     void BeginTransaction();
159     void EndTransaction();
160 
161     /* async task wrapper */
162     int32_t AsyncRun(std::shared_ptr<TaskContext> context,
163         void(DataSyncer::*f)(std::shared_ptr<TaskContext>));
164     template<typename T, typename RET, typename... ARGS>
165     std::function<RET(ARGS...)> AsyncCallback(RET(T::*f)(ARGS...));
166 
167     void SetErrorCodeMask(int32_t &errorCode, ErrorType errorType);
168     /* cloud sync result */
169     ErrorType GetErrorType(const int32_t code);
170 
171     /* identifier */
172     const std::string bundleName_;
173     const int32_t userId_;
174 
175     /* state management */
176     SyncStateManager syncStateManager_;
177 
178     /* task management */
179     std::shared_ptr<TaskRunner> taskRunner_;
180 
181     /* sdk */
182     std::shared_ptr<SdkHelper> sdkHelper_;
183     SdkLock lock_;
184 
185     int32_t errorCode_{0};
186 
187     /* download callback manager*/
188     CloudDownloadCallbackManager downloadCallbackMgr_;
189 
190     /* Current sync state */
191     CloudSyncState CurrentSyncState_{CloudSyncState::COMPLETED};
192 
193     /* Current error type */
194     ErrorType CurrentErrorType_{ErrorType::NO_ERROR};
195 };
196 } // namespace CloudSync
197 } // namespace FileManagement
198 } // namespace OHOS
199 #endif // OHOS_CLOUD_SYNC_SERVICE_DATA_SYNCER_H
200