• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "ObjectAssetLoader"
16 
17 #include "object_asset_loader.h"
18 #include "block_data.h"
19 #include "cloud_sync_asset_manager.h"
20 #include "log_print.h"
21 #include "object_common.h"
22 #include "utils/anonymous.h"
23 
24 namespace OHOS::DistributedObject {
25 using namespace OHOS::FileManagement::CloudSync;
GetInstance()26 ObjectAssetLoader *ObjectAssetLoader::GetInstance()
27 {
28     static ObjectAssetLoader *loader = new ObjectAssetLoader();
29     return loader;
30 }
31 
SetThreadPool(std::shared_ptr<ExecutorPool> executors)32 void ObjectAssetLoader::SetThreadPool(std::shared_ptr<ExecutorPool> executors)
33 {
34     executors_ = executors;
35 }
36 
Transfer(const int32_t userId,const std::string & bundleName,const std::string & deviceId,const DistributedData::Asset & assetValue)37 bool ObjectAssetLoader::Transfer(const int32_t userId, const std::string &bundleName,
38     const std::string &deviceId, const DistributedData::Asset &assetValue)
39 {
40     AssetInfo assetInfo;
41     assetInfo.uri = assetValue.uri;
42     assetInfo.assetName = assetValue.name;
43     ZLOGI("Start transfer, bundleName: %{public}s, deviceId: %{public}s, assetName: %{public}s", bundleName.c_str(),
44           DistributedData::Anonymous::Change(deviceId).c_str(), assetInfo.assetName.c_str());
45     auto block = std::make_shared<BlockData<std::tuple<bool, int32_t>>>(WAIT_TIME, std::tuple{ true, OBJECT_SUCCESS });
46     auto res = CloudSyncAssetManager::GetInstance().DownloadFile(userId, bundleName, deviceId, assetInfo,
47         [block](const std::string &uri, int32_t status) {
48             block->SetValue({ false, status });
49         });
50     if (res != OBJECT_SUCCESS) {
51         ZLOGE("fail, res: %{public}d, name: %{public}s, deviceId: %{public}s, bundleName: %{public}s", res,
52             assetValue.name.c_str(), DistributedData::Anonymous::Change(deviceId).c_str(), bundleName.c_str());
53         return false;
54     }
55     auto [timeout, status] = block->GetValue();
56     if (timeout || status != OBJECT_SUCCESS) {
57         ZLOGE("fail, timeout: %{public}d, status: %{public}d, name: %{public}s, deviceId: %{public}s ", timeout,
58             status, assetValue.name.c_str(), DistributedData::Anonymous::Change(deviceId).c_str());
59         return false;
60     }
61     return true;
62 }
63 
TransferAssetsAsync(const int32_t userId,const std::string & bundleName,const std::string & deviceId,const std::vector<DistributedData::Asset> & assets,const std::function<void (bool success)> & callback)64 void ObjectAssetLoader::TransferAssetsAsync(const int32_t userId, const std::string& bundleName,
65     const std::string& deviceId, const std::vector<DistributedData::Asset>& assets,
66     const std::function<void(bool success)>& callback)
67 {
68     if (executors_ == nullptr) {
69         ZLOGE("executors is null, bundleName: %{public}s, deviceId: %{public}s, userId: %{public}d",
70             bundleName.c_str(), DistributedData::Anonymous::Change(deviceId).c_str(), userId);
71         callback(false);
72         return;
73     }
74     executors_->Execute([this, userId, bundleName, deviceId, assets, callback]() {
75         bool result = true;
76         for (auto& asset : assets) {
77             result &= Transfer(userId, bundleName, deviceId, asset);
78         }
79         callback(result);
80     });
81 }
82 } // namespace OHOS::DistributedObject