• 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_SDK_HELPER_H
17 #define OHOS_CLOUD_SYNC_SERVICE_SDK_HELPER_H
18 
19 #include <functional>
20 #include <vector>
21 
22 #include "dk_database.h"
23 #include "drive_kit.h"
24 
25 namespace OHOS {
26 namespace FileManagement {
27 namespace CloudSync {
28 struct FetchCondition {
29     int32_t limitRes;
30     DriveKit::DKRecordType recordType;
31     DriveKit::DKFieldKeyArray desiredKeys;
32     DriveKit::DKFieldKeyArray fullKeys;
33 };
34 class SdkHelper {
35 public:
36     SdkHelper() = default;
37     ~SdkHelper() = default;
38 
39     /* init */
40     int32_t Init(const int32_t userId, const std::string &bundleName);
41 
42     /* lock */
43     int32_t GetLock(DriveKit::DKLock &lock);
44     void DeleteLock(DriveKit::DKLock &lock);
45 
46     /* record download */
47     using FetchRecordsCallback = std::function<void(const std::shared_ptr<DriveKit::DKContext>,
48         std::shared_ptr<const DriveKit::DKDatabase>,
49         std::shared_ptr<std::vector<DriveKit::DKRecord>>, DriveKit::DKQueryCursor,
50         const DriveKit::DKError &)>;
51 
52     using FetchDatabaseChangesCallback = std::function<void(const std::shared_ptr<DriveKit::DKContext>,
53         std::shared_ptr<const DriveKit::DKDatabase>,
54         std::shared_ptr<std::vector<DriveKit::DKRecord>>, DriveKit::DKQueryCursor,
55         bool, const DriveKit::DKError &)>;
56     using FetchRecordCallback = std::function<void(std::shared_ptr<DriveKit::DKContext>,
57         std::shared_ptr<DriveKit::DKDatabase>, DriveKit::DKRecordId, DriveKit::DKRecord &,
58         const DriveKit::DKError &)>;
59 
60     int32_t FetchRecords(std::shared_ptr<DriveKit::DKContext> context, FetchCondition &cond, DriveKit::DKQueryCursor,
61         FetchRecordsCallback callback);
62     int32_t FetchRecordWithId(std::shared_ptr<DriveKit::DKContext> context, FetchCondition &cond,
63         DriveKit::DKRecordId recordId, FetchRecordCallback callback);
64     int32_t FetchDatabaseChanges(std::shared_ptr<DriveKit::DKContext> context, FetchCondition &cond,
65         DriveKit::DKQueryCursor cursor, FetchDatabaseChangesCallback callback);
66 
67     /* record upload */
68     int32_t CreateRecords(std::shared_ptr<DriveKit::DKContext> context,
69         std::vector<DriveKit::DKRecord> &records,
70         std::function<void(std::shared_ptr<DriveKit::DKContext>,
71             std::shared_ptr<const DriveKit::DKDatabase>,
72             std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
73             const DriveKit::DKError &)> callback
74     );
75 
76     int32_t DeleteRecords(std::shared_ptr<DriveKit::DKContext> context,
77         std::vector<DriveKit::DKRecord> &records,
78         std::function<void(std::shared_ptr<DriveKit::DKContext>,
79             std::shared_ptr<const DriveKit::DKDatabase>,
80             std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
81             const DriveKit::DKError &)> callback
82     );
83 
84     int32_t ModifyRecords(std::shared_ptr<DriveKit::DKContext> context,
85         std::vector<DriveKit::DKRecord> &records, DriveKit::DKDatabase::ModifyRecordsCallback callback
86     );
87 
88     /* asset download */
89     int32_t DownloadAssets(std::shared_ptr<DriveKit::DKContext> context,
90         std::vector<DriveKit::DKDownloadAsset> &assetsToDownload, DriveKit::DKAssetPath downLoadPath,
91         DriveKit::DKDownloadId &id,
92         std::function<void(std::shared_ptr<DriveKit::DKContext>,
93                        std::shared_ptr<const DriveKit::DKDatabase>,
94                        const std::map<DriveKit::DKDownloadAsset, DriveKit::DKDownloadResult> &,
95                        const DriveKit::DKError &)> resultCallback,
96         std::function<void(std::shared_ptr<DriveKit::DKContext>, DriveKit::DKDownloadAsset, DriveKit::TotalSize,
97                            DriveKit::DownloadSize)> progressCallback);
98     int32_t DownloadAssets(DriveKit::DKDownloadAsset &assetsToDownload);
99 
100     int32_t CancelDownloadAssets(int32_t id);
101 
102     int32_t GetStartCursor(DriveKit::DKRecordType recordType, DriveKit::DKQueryCursor &cursor);
103 
104     std::shared_ptr<DriveKit::DKAssetReadSession> GetAssetReadSession(DriveKit::DKRecordType recordType,
105                                                                       DriveKit::DKRecordId recordId,
106                                                                       DriveKit::DKFieldKey assetKey,
107                                                                       DriveKit::DKAssetPath assetPath);
108     using SaveSubscriptionCallback = std::function<void(std::shared_ptr<DriveKit::DKContext>,
109         std::shared_ptr<DriveKit::DKContainer>, DriveKit::DKSubscriptionResult &)>;
110     using DelSubscriptionCallback = std::function<void(std::shared_ptr<DriveKit::DKContext>,
111         const DriveKit::DKError &)>;
112     using ChangesNotifyCallback = std::function<void(std::shared_ptr<DriveKit::DKContext>,
113         const DriveKit::DKError &)>;
114     int32_t SaveSubscription(SaveSubscriptionCallback callback);
115     int32_t DeleteSubscription(DelSubscriptionCallback callback);
116     int32_t ChangesNotify(ChangesNotifyCallback callback);
117     void Release();
118 private:
119     std::shared_ptr<DriveKit::DKContainer> container_;
120     std::shared_ptr<DriveKit::DKDatabase> database_;
121     std::shared_ptr<DriveKit::DKAssetsDownloader> downloader_;
122 };
123 
124 struct SdkLock {
125     DriveKit::DKLock lock = { 0 };
126     std::mutex mtx;
127     int count = 0;
128     uint32_t timerId{0};
129 };
130 } // namespace CloudSync
131 } // namespace FileManagement
132 } // namespace OHOS
133 #endif // OHOS_CLOUD_SYNC_SERVICE_SDK_HELPER_H
134