• 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 DRIVE_KIT_ASSETS_DOWNLOADER_H
17 #define DRIVE_KIT_ASSETS_DOWNLOADER_H
18 
19 #include <functional>
20 #include <map>
21 
22 #include "dk_context.h"
23 #include "dk_error.h"
24 #include "dk_record.h"
25 #include "dk_result.h"
26 
27 namespace DriveKit {
28 using DKDownloadId = int64_t;
29 using DKAssetPath = std::string;
30 using TotalSize = int64_t;
31 using DownloadSize = int64_t;
32 class DKDatabase;
33 class DKDownloadResult : public DKResult {
34 public:
GetAsset()35     DKAsset GetAsset()
36     {
37         return asset_;
38     }
SetAsset(DKAsset asset)39     void SetAsset(DKAsset asset)
40     {
41         asset_ = asset;
42     }
43 
44 private:
45     DKAsset asset_;
46 };
47 struct DKDownloadAsset {
48     DKRecordType recordType;
49     DKRecordId recordId;
50     DKFieldKey fieldKey;
51     DKAsset asset;
52     DKAssetPath downLoadPath;
53     bool operator<(const DKDownloadAsset &downloadAsset) const
54     {
55         if (recordType != downloadAsset.recordType) {
56             return recordType < downloadAsset.recordType;
57         }
58         if (recordId != downloadAsset.recordId) {
59             return recordId < downloadAsset.recordId;
60         }
61         return fieldKey < downloadAsset.fieldKey;
62     }
63 };
64 
65 class DKAssetsDownloader {
66 public:
67     DKLocalErrorCode DownLoadAssets(
68         std::shared_ptr<DKContext> contex,
69         std::vector<DKDownloadAsset> &assetsToDownload,
70         DKAssetPath downLoadPath,
71         DKDownloadId &id,
72         std::function<void(std::shared_ptr<DKContext>,
73                            std::shared_ptr<const DKDatabase>,
74                            const std::map<DKDownloadAsset, DKDownloadResult> &,
75                            const DKError &)> resultCallback,
76         std::function<void(std::shared_ptr<DKContext>, DKDownloadAsset, TotalSize, DownloadSize)> progressCallback,
77         bool isPriority = false);
78     DKLocalErrorCode DownLoadAssets(DKDownloadAsset &assetsToDownload);
79 
80     DKLocalErrorCode CancelDownloadAssets(DKDownloadId id, bool isClearCache = false);
DKAssetsDownloader(std::shared_ptr<DKDatabase> database)81     DKAssetsDownloader(std::shared_ptr<DKDatabase> database)
82     {
83         database_ = database;
84     };
~DKAssetsDownloader()85     virtual ~DKAssetsDownloader(){};
86 
87 private:
88     std::shared_ptr<DKDatabase> database_;
89 };
90 } // namespace DriveKit
91 #endif
92