• 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 #include "sdk_helper.h"
17 
18 #include <thread>
19 
20 #include "dfs_error.h"
21 #include "utils_log.h"
22 
23 namespace OHOS {
24 namespace FileManagement {
25 namespace CloudSync {
26 using namespace std;
27 
28 static const uint64_t INTERVAL = 30 * 24;
Init(const int32_t userId,const std::string & bundleName)29 int32_t SdkHelper::Init(const int32_t userId, const std::string &bundleName)
30 {
31     auto driveKit = DriveKit::DriveKitNative::GetInstance(userId);
32     if (driveKit == nullptr) {
33         LOGE("sdk helper get drive kit instance fail");
34         return E_CLOUD_SDK;
35     }
36 
37     container_ = driveKit->GetDefaultContainer(bundleName);
38     if (container_ == nullptr) {
39         LOGE("sdk helper get drive kit container fail");
40         return E_CLOUD_SDK;
41     }
42 
43     database_ = container_->GetPrivateDatabase();
44     if (database_ == nullptr) {
45         LOGE("sdk helper get drive kit database fail");
46         return E_CLOUD_SDK;
47     }
48 
49     downloader_ = database_->GetAssetsDownloader();
50     if (downloader_ == nullptr) {
51         LOGE("sdk helper get drive kit downloader_ fail");
52         return E_CLOUD_SDK;
53     }
54 
55     return E_OK;
56 }
57 
GetLock(DriveKit::DKLock & lock)58 int32_t SdkHelper::GetLock(DriveKit::DKLock &lock)
59 {
60     auto err = database_->GetLock(lock);
61     if (err.HasError()) {
62         LOGE("get sdk lock err");
63         return E_CLOUD_SDK;
64     }
65     return E_OK;
66 }
67 
DeleteLock(DriveKit::DKLock & lock)68 void SdkHelper::DeleteLock(DriveKit::DKLock &lock)
69 {
70     database_->DeleteLock(lock);
71 }
72 
FetchRecords(std::shared_ptr<DriveKit::DKContext> context,FetchCondition & cond,DriveKit::DKQueryCursor cursor,FetchRecordsCallback callback)73 int32_t SdkHelper::FetchRecords(std::shared_ptr<DriveKit::DKContext> context, FetchCondition &cond,
74     DriveKit::DKQueryCursor cursor, FetchRecordsCallback callback)
75 {
76     auto err = database_->FetchRecords(context, cond.recordType, cond.desiredKeys, cond.limitRes, cursor, callback);
77     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
78         LOGE("drivekit fetch records err %{public}d", err);
79         return E_CLOUD_SDK;
80     }
81     return E_OK;
82 }
83 
FetchRecordWithId(std::shared_ptr<DriveKit::DKContext> context,FetchCondition & cond,DriveKit::DKRecordId recordId,FetchRecordCallback callback)84 int32_t SdkHelper::FetchRecordWithId(std::shared_ptr<DriveKit::DKContext> context, FetchCondition &cond,
85     DriveKit::DKRecordId recordId, FetchRecordCallback callback)
86 {
87     auto err = database_->FetchRecordWithId(context, cond.recordType, recordId, cond.fullKeys, callback);
88     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
89         LOGE("drivekit fetch records err %{public}d", err);
90         return E_CLOUD_SDK;
91     }
92     return E_OK;
93 }
94 
FetchDatabaseChanges(std::shared_ptr<DriveKit::DKContext> context,FetchCondition & cond,DriveKit::DKQueryCursor cursor,FetchDatabaseChangesCallback callback)95 int32_t SdkHelper::FetchDatabaseChanges(std::shared_ptr<DriveKit::DKContext> context, FetchCondition &cond,
96     DriveKit::DKQueryCursor cursor, FetchDatabaseChangesCallback callback)
97 {
98     auto err = database_->FetchDatabaseChanges(context, cond.recordType, cond.desiredKeys, cond.limitRes, cursor,
99         callback);
100     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
101         LOGE("drivekit fetch records err %{public}d", err);
102         return E_CLOUD_SDK;
103     }
104     return E_OK;
105 }
106 
CreateRecords(shared_ptr<DriveKit::DKContext> context,vector<DriveKit::DKRecord> & records,std::function<void (std::shared_ptr<DriveKit::DKContext>,std::shared_ptr<const DriveKit::DKDatabase>,std::shared_ptr<const std::map<DriveKit::DKRecordId,DriveKit::DKRecordOperResult>>,const DriveKit::DKError &)> callback)107 int32_t SdkHelper::CreateRecords(shared_ptr<DriveKit::DKContext> context,
108     vector<DriveKit::DKRecord> &records,
109     std::function<void(std::shared_ptr<DriveKit::DKContext>, std::shared_ptr<const DriveKit::DKDatabase>,
110         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
111         const DriveKit::DKError &)> callback)
112 {
113     auto errCode = database_->SaveRecords(context, move(records),
114         DriveKit::DKSavePolicy::DK_SAVE_IF_UNCHANGED, callback);
115     if (errCode != DriveKit::DKLocalErrorCode::NO_ERROR) {
116         LOGE("drivekit save records err %{public}d", errCode);
117         return E_CLOUD_SDK;
118     }
119     return E_OK;
120 }
121 
DeleteRecords(shared_ptr<DriveKit::DKContext> context,vector<DriveKit::DKRecord> & records,std::function<void (std::shared_ptr<DriveKit::DKContext>,std::shared_ptr<const DriveKit::DKDatabase>,std::shared_ptr<const std::map<DriveKit::DKRecordId,DriveKit::DKRecordOperResult>>,const DriveKit::DKError &)> callback)122 int32_t SdkHelper::DeleteRecords(shared_ptr<DriveKit::DKContext> context,
123     vector<DriveKit::DKRecord> &records,
124     std::function<void(std::shared_ptr<DriveKit::DKContext>, std::shared_ptr<const DriveKit::DKDatabase>,
125         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
126         const DriveKit::DKError &)> callback)
127 {
128     auto err = database_->DeleteRecords(context, move(records),
129         DriveKit::DKSavePolicy::DK_SAVE_IF_UNCHANGED, callback);
130     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
131         LOGE("drivekit deletes records err %{public}d", err);
132         return E_CLOUD_SDK;
133     }
134     return E_OK;
135 }
136 
ModifyRecords(shared_ptr<DriveKit::DKContext> context,vector<DriveKit::DKRecord> & records,DriveKit::DKDatabase::ModifyRecordsCallback callback)137 int32_t SdkHelper::ModifyRecords(shared_ptr<DriveKit::DKContext> context,
138     vector<DriveKit::DKRecord> &records, DriveKit::DKDatabase::ModifyRecordsCallback callback)
139 {
140     vector<DriveKit::DKRecord> null;
141     auto err = database_->ModifyRecords(context, move(records), move(null),
142         DriveKit::DKSavePolicy::DK_SAVE_IF_UNCHANGED, true, callback);
143     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
144         LOGE("drivekit modifies records err %{public}d", err);
145         return E_CLOUD_SDK;
146     }
147     return E_OK;
148 }
149 
DownloadAssets(shared_ptr<DriveKit::DKContext> context,std::vector<DriveKit::DKDownloadAsset> & assetsToDownload,DriveKit::DKAssetPath downLoadPath,DriveKit::DKDownloadId & id,std::function<void (std::shared_ptr<DriveKit::DKContext>,std::shared_ptr<const DriveKit::DKDatabase>,const std::map<DriveKit::DKDownloadAsset,DriveKit::DKDownloadResult> &,const DriveKit::DKError &)> resultCallback,std::function<void (std::shared_ptr<DriveKit::DKContext>,DriveKit::DKDownloadAsset,DriveKit::TotalSize,DriveKit::DownloadSize)> progressCallback)150 int32_t SdkHelper::DownloadAssets(shared_ptr<DriveKit::DKContext> context,
151     std::vector<DriveKit::DKDownloadAsset> &assetsToDownload, DriveKit::DKAssetPath downLoadPath,
152     DriveKit::DKDownloadId &id,
153     std::function<void(std::shared_ptr<DriveKit::DKContext>,
154                        std::shared_ptr<const DriveKit::DKDatabase>,
155                        const std::map<DriveKit::DKDownloadAsset, DriveKit::DKDownloadResult> &,
156                        const DriveKit::DKError &)> resultCallback,
157     std::function<void(std::shared_ptr<DriveKit::DKContext>, DriveKit::DKDownloadAsset,
158                        DriveKit::TotalSize, DriveKit::DownloadSize)> progressCallback)
159 {
160     auto result =
161         downloader_->DownLoadAssets(context, assetsToDownload, downLoadPath, id, resultCallback, progressCallback);
162     if (result != DriveKit::DKLocalErrorCode::NO_ERROR) {
163         LOGE("DownLoadAssets fail");
164         return E_CLOUD_SDK;
165     }
166     return E_OK;
167 }
168 
DownloadAssets(DriveKit::DKDownloadAsset & assetsToDownload)169 int32_t SdkHelper::DownloadAssets(DriveKit::DKDownloadAsset &assetsToDownload)
170 {
171     auto result = downloader_->DownLoadAssets(assetsToDownload);
172     if (result != DriveKit::DKLocalErrorCode::NO_ERROR) {
173         LOGE("DownLoadAssets fail ret %{public}d", static_cast<int>(result));
174         return E_CLOUD_SDK;
175     }
176     return E_OK;
177 }
178 
CancelDownloadAssets(int32_t id)179 int32_t SdkHelper::CancelDownloadAssets(int32_t id)
180 {
181     return E_OK;
182 }
183 
GetStartCursor(DriveKit::DKRecordType recordType,DriveKit::DKQueryCursor & cursor)184 int32_t SdkHelper::GetStartCursor(DriveKit::DKRecordType recordType, DriveKit::DKQueryCursor &cursor)
185 {
186     auto err = database_->GetStartCursor(recordType, cursor);
187     if (err.HasError()) {
188         LOGE("drivekit get start cursor server err %{public}d and dk errcor %{public}d", err.serverErrorCode,
189             err.dkErrorCode);
190         return E_CLOUD_SDK;
191     }
192     return E_OK;
193 }
194 
GetAssetReadSession(DriveKit::DKRecordType recordType,DriveKit::DKRecordId recordId,DriveKit::DKFieldKey assetKey,DriveKit::DKAssetPath assetPath)195 std::shared_ptr<DriveKit::DKAssetReadSession> SdkHelper::GetAssetReadSession(DriveKit::DKRecordType recordType,
196                                                                              DriveKit::DKRecordId recordId,
197                                                                              DriveKit::DKFieldKey assetKey,
198                                                                              DriveKit::DKAssetPath assetPath)
199 {
200     if (!database_)
201         return nullptr;
202     return database_->NewAssetReadSession(recordType, recordId, assetKey, assetPath);
203 }
204 
SaveSubscription(SaveSubscriptionCallback callback)205 int32_t SdkHelper::SaveSubscription(SaveSubscriptionCallback callback)
206 {
207     auto expireTime = std::chrono::duration_cast<std::chrono::milliseconds>
208         ((std::chrono::system_clock::now() + std::chrono::hours(INTERVAL)).time_since_epoch()).count();
209     DriveKit::DKSubscription subscription{expireTime};
210     auto err = container_->SaveSubscription(nullptr, subscription, callback);
211     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
212         LOGE("SaveSubscription fail ret %{public}d", err);
213         return E_CLOUD_SDK;
214     }
215     return E_OK;
216 }
217 
DeleteSubscription(DelSubscriptionCallback callback)218 int32_t SdkHelper::DeleteSubscription(DelSubscriptionCallback callback)
219 {
220     auto err = container_->DeleteSubscription(nullptr, "", callback);
221     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
222         LOGE("DeleteSubscription fail ret %{public}d", err);
223         return E_CLOUD_SDK;
224     }
225     return E_OK;
226 }
227 } // namespace CloudSync
228 } // namespace FileManagement
229 } // namespace OHOS
230