1 /*
2  * Copyright (c) 2022 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 #ifndef DISTRIBUTEDDATAMGR_OBJECT_ASSET_LOADER_H
16 #define DISTRIBUTEDDATAMGR_OBJECT_ASSET_LOADER_H
17 
18 #include <string>
19 #include "executor_pool.h"
20 #include "object_types.h"
21 #include "store/general_value.h"
22 #include "concurrent_map.h"
23 #include <unordered_set>
24 #include "asset/asset_send_callback_stub.h"
25 namespace OHOS {
26 namespace DistributedObject {
27 using AssetObj = Storage::DistributedFile::AssetObj;
28 using TransferFunc = std::function<void(bool success)>;
29 struct TransferTask {
30     TransferTask() = default;
31     std::unordered_set<std::string> downloadAssets;
32     TransferFunc callback;
33 };
34 
35 class ObjectAssetsSendListener : public Storage::DistributedFile::AssetSendCallbackStub {
36 public:
37     ObjectAssetsSendListener() =default;
38     ~ObjectAssetsSendListener() = default;
39     int32_t OnSendResult(const sptr<AssetObj> &assetObj, int32_t result) override;
40 };
41 
42 class ObjectAssetLoader {
43 public:
44     static ObjectAssetLoader *GetInstance();
45     void SetThreadPool(std::shared_ptr<ExecutorPool> executors);
46     bool Transfer(const int32_t userId, const std::string& bundleName, const std::string& deviceId,
47         const DistributedData::Asset& asset);
48     void TransferAssetsAsync(const int32_t userId, const std::string& bundleName, const std::string& deviceId,
49                              const std::vector<DistributedData::Asset>& assets, const TransferFunc& callback);
50     int32_t PushAsset(int32_t userId, const sptr<AssetObj> &assetObj,
51         const sptr<ObjectAssetsSendListener> &sendCallback);
52 private:
53     ObjectAssetLoader() = default;
54     ~ObjectAssetLoader() = default;
55     ObjectAssetLoader(const ObjectAssetLoader &) = delete;
56     ObjectAssetLoader &operator=(const ObjectAssetLoader &) = delete;
57     void FinishTask(const std::string& uri, bool result);
58     bool IsDownloading(const DistributedData::Asset& asset);
59     bool IsDownloaded(const DistributedData::Asset& asset);
60     void UpdateDownloaded(const DistributedData::Asset& asset);
61     static constexpr int WAIT_TIME = 60;
62     static constexpr int LAST_DOWNLOAD_ASSET_SIZE = 200;
63     std::shared_ptr<ExecutorPool> executors_;
64     std::mutex mutex_;
65     std::atomic_uint32_t taskSeq_ = 0;
66     std::queue<std::string> assetQueue_;
67     ConcurrentMap<uint32_t, TransferTask> tasks_;
68     ConcurrentMap<std::string, std::string> downloaded_;
69     ConcurrentMap<std::string, std::string> downloading_;
70 };
71 } // namespace DistributedObject
72 } // namespace OHOS
73 #endif // DISTRIBUTEDDATAMGR_OBJECT_ASSET_LOADER_H