• 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, DriveKit::DKCloudSyncDemon::DK_CLOUD_FILE);
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     if (!downloader_) {
49         downloader_ = database_->GetAssetsDownloader();
50         if (downloader_ == nullptr) {
51             LOGE("sdk helper get drivekit 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, server err %{public}d and dk errcor %{public}d", err.serverErrorCode, err.dkErrorCode);
63         if (static_cast<DriveKit::DKServerErrorCode>(err.serverErrorCode) ==
64             DriveKit::DKServerErrorCode::NETWORK_ERROR) {
65             return E_SYNC_FAILED_NETWORK_NOT_AVAILABLE;
66         }
67         return E_CLOUD_SDK;
68     }
69     return E_OK;
70 }
71 
DeleteLock(DriveKit::DKLock & lock)72 void SdkHelper::DeleteLock(DriveKit::DKLock &lock)
73 {
74     database_->DeleteLock(lock);
75 }
76 
FetchRecords(std::shared_ptr<DriveKit::DKContext> context,FetchCondition & cond,DriveKit::DKQueryCursor cursor,FetchRecordsCallback callback)77 int32_t SdkHelper::FetchRecords(std::shared_ptr<DriveKit::DKContext> context, FetchCondition &cond,
78     DriveKit::DKQueryCursor cursor, FetchRecordsCallback callback)
79 {
80     auto err = database_->FetchRecords(context, cond.recordType, cond.desiredKeys, cond.limitRes, cursor, callback);
81     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
82         LOGE("drivekit fetch records err %{public}d", err);
83         return E_CLOUD_SDK;
84     }
85     return E_OK;
86 }
87 
FetchRecordWithId(std::shared_ptr<DriveKit::DKContext> context,FetchCondition & cond,DriveKit::DKRecordId recordId,FetchRecordCallback callback)88 int32_t SdkHelper::FetchRecordWithId(std::shared_ptr<DriveKit::DKContext> context, FetchCondition &cond,
89     DriveKit::DKRecordId recordId, FetchRecordCallback callback)
90 {
91     auto err = database_->FetchRecordWithId(context, cond.recordType, recordId, cond.fullKeys, callback);
92     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
93         LOGE("drivekit fetch records err %{public}d", err);
94         return E_CLOUD_SDK;
95     }
96     return E_OK;
97 }
98 
FetchDatabaseChanges(std::shared_ptr<DriveKit::DKContext> context,FetchCondition & cond,DriveKit::DKQueryCursor cursor,FetchDatabaseChangesCallback callback)99 int32_t SdkHelper::FetchDatabaseChanges(std::shared_ptr<DriveKit::DKContext> context, FetchCondition &cond,
100     DriveKit::DKQueryCursor cursor, FetchDatabaseChangesCallback callback)
101 {
102     auto err = database_->FetchDatabaseChanges(context, cond.recordType, cond.desiredKeys, cond.limitRes, cursor,
103         callback);
104     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
105         LOGE("drivekit fetch records err %{public}d", err);
106         return E_CLOUD_SDK;
107     }
108     return E_OK;
109 }
110 
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)111 int32_t SdkHelper::CreateRecords(shared_ptr<DriveKit::DKContext> context,
112     vector<DriveKit::DKRecord> &records,
113     std::function<void(std::shared_ptr<DriveKit::DKContext>, std::shared_ptr<const DriveKit::DKDatabase>,
114         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
115         const DriveKit::DKError &)> callback)
116 {
117     auto errCode = database_->SaveRecords(context, move(records),
118         DriveKit::DKSavePolicy::DK_SAVE_IF_UNCHANGED, callback);
119     if (errCode != DriveKit::DKLocalErrorCode::NO_ERROR) {
120         LOGE("drivekit save records err %{public}d", errCode);
121         return E_CLOUD_SDK;
122     }
123     return E_OK;
124 }
125 
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)126 int32_t SdkHelper::DeleteRecords(shared_ptr<DriveKit::DKContext> context,
127     vector<DriveKit::DKRecord> &records,
128     std::function<void(std::shared_ptr<DriveKit::DKContext>, std::shared_ptr<const DriveKit::DKDatabase>,
129         std::shared_ptr<const std::map<DriveKit::DKRecordId, DriveKit::DKRecordOperResult>>,
130         const DriveKit::DKError &)> callback)
131 {
132     auto err = database_->DeleteRecords(context, move(records),
133         DriveKit::DKSavePolicy::DK_SAVE_IF_UNCHANGED, callback);
134     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
135         LOGE("drivekit deletes records err %{public}d", err);
136         return E_CLOUD_SDK;
137     }
138     return E_OK;
139 }
140 
ModifyRecords(shared_ptr<DriveKit::DKContext> context,vector<DriveKit::DKRecord> & records,DriveKit::DKDatabase::ModifyRecordsCallback callback)141 int32_t SdkHelper::ModifyRecords(shared_ptr<DriveKit::DKContext> context,
142     vector<DriveKit::DKRecord> &records, DriveKit::DKDatabase::ModifyRecordsCallback callback)
143 {
144     vector<DriveKit::DKRecord> null;
145     auto err = database_->ModifyRecords(context, move(records), move(null),
146         DriveKit::DKSavePolicy::DK_SAVE_IF_UNCHANGED, false, callback);
147     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
148         LOGE("drivekit modifies records err %{public}d", err);
149         return E_CLOUD_SDK;
150     }
151     return E_OK;
152 }
153 
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)154 int32_t SdkHelper::DownloadAssets(shared_ptr<DriveKit::DKContext> context,
155     std::vector<DriveKit::DKDownloadAsset> &assetsToDownload, DriveKit::DKAssetPath downLoadPath,
156     DriveKit::DKDownloadId &id,
157     std::function<void(std::shared_ptr<DriveKit::DKContext>,
158                        std::shared_ptr<const DriveKit::DKDatabase>,
159                        const std::map<DriveKit::DKDownloadAsset, DriveKit::DKDownloadResult> &,
160                        const DriveKit::DKError &)> resultCallback,
161     std::function<void(std::shared_ptr<DriveKit::DKContext>, DriveKit::DKDownloadAsset,
162                        DriveKit::TotalSize, DriveKit::DownloadSize)> progressCallback)
163 {
164     bool isPriority = false;
165     if (assetsToDownload.front().fieldKey == "content") {
166         isPriority = true;
167     }
168     auto result =
169         downloader_->DownLoadAssets(context, assetsToDownload, downLoadPath, id,
170                                     resultCallback, progressCallback, isPriority);
171     if (result != DriveKit::DKLocalErrorCode::NO_ERROR) {
172         LOGE("DownLoadAssets fail");
173         return E_CLOUD_SDK;
174     }
175     return E_OK;
176 }
177 
DownloadAssets(DriveKit::DKDownloadAsset & assetsToDownload)178 int32_t SdkHelper::DownloadAssets(DriveKit::DKDownloadAsset &assetsToDownload)
179 {
180     auto result = downloader_->DownLoadAssets(assetsToDownload);
181     if (result != DriveKit::DKLocalErrorCode::NO_ERROR) {
182         LOGE("DownLoadAssets fail ret %{public}d", static_cast<int>(result));
183         return E_CLOUD_SDK;
184     }
185     return E_OK;
186 }
187 
CancelDownloadAssets(int32_t id)188 int32_t SdkHelper::CancelDownloadAssets(int32_t id)
189 {
190     return static_cast<int32_t>(downloader_->CancelDownloadAssets(id, true));
191 }
192 
GetStartCursor(DriveKit::DKRecordType recordType,DriveKit::DKQueryCursor & cursor)193 int32_t SdkHelper::GetStartCursor(DriveKit::DKRecordType recordType, DriveKit::DKQueryCursor &cursor)
194 {
195     auto err = database_->GetStartCursor(recordType, cursor);
196     if (err.HasError()) {
197         LOGE("drivekit get start cursor server err %{public}d and dk errcor %{public}d", err.serverErrorCode,
198             err.dkErrorCode);
199         return E_CLOUD_SDK;
200     }
201     return E_OK;
202 }
203 
GetAssetReadSession(DriveKit::DKRecordType recordType,DriveKit::DKRecordId recordId,DriveKit::DKFieldKey assetKey,DriveKit::DKAssetPath assetPath)204 std::shared_ptr<DriveKit::DKAssetReadSession> SdkHelper::GetAssetReadSession(DriveKit::DKRecordType recordType,
205                                                                              DriveKit::DKRecordId recordId,
206                                                                              DriveKit::DKFieldKey assetKey,
207                                                                              DriveKit::DKAssetPath assetPath)
208 {
209     if (!database_)
210         return nullptr;
211     return database_->NewAssetReadSession(recordType, recordId, assetKey, assetPath);
212 }
213 
SaveSubscription(SaveSubscriptionCallback callback)214 int32_t SdkHelper::SaveSubscription(SaveSubscriptionCallback callback)
215 {
216     auto expireTime = std::chrono::duration_cast<std::chrono::milliseconds>
217         ((std::chrono::system_clock::now() + std::chrono::hours(INTERVAL)).time_since_epoch()).count();
218     DriveKit::DKSubscription subscription{expireTime};
219     auto err = container_->SaveSubscription(nullptr, subscription, callback);
220     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
221         LOGE("SaveSubscription fail ret %{public}d", err);
222         return E_CLOUD_SDK;
223     }
224     return E_OK;
225 }
226 
DeleteSubscription(DelSubscriptionCallback callback)227 int32_t SdkHelper::DeleteSubscription(DelSubscriptionCallback callback)
228 {
229     auto err = container_->DeleteSubscription(nullptr, "", callback);
230     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
231         LOGE("DeleteSubscription fail ret %{public}d", err);
232         return E_CLOUD_SDK;
233     }
234     return E_OK;
235 }
236 
ChangesNotify(ChangesNotifyCallback callback)237 int32_t SdkHelper::ChangesNotify(ChangesNotifyCallback callback)
238 {
239     auto err = container_->ChangesNotify(nullptr, "", callback);
240     if (err != DriveKit::DKLocalErrorCode::NO_ERROR) {
241         LOGE("ChangesNotify fail ret %{public}d", err);
242         return E_CLOUD_SDK;
243     }
244     return E_OK;
245 }
Release()246 void SdkHelper::Release()
247 {
248     LOGD("stop upload asset");
249     database_->Release();
250 }
251 } // namespace CloudSync
252 } // namespace FileManagement
253 } // namespace OHOS
254