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 #define LOG_TAG "ObjectSnapshot"
17 #include "object_snapshot.h"
18 
19 #include "eventcenter/event_center.h"
20 #include "log_print.h"
21 #include "snapshot/bind_event.h"
22 #include "store/general_store.h"
23 #include "utils/anonymous.h"
24 namespace OHOS::DistributedObject {
25 using namespace OHOS::DistributedData;
Upload(Asset & asset)26 int32_t ObjectSnapshot::Upload(Asset& asset)
27 {
28     if (!IsBoundAsset(asset)) {
29         return 0;
30     }
31     return assetMachine_->DFAPostEvent(UPLOAD, changedAssets_[asset.uri], asset);
32 }
33 
IsBoundAsset(const Asset & asset)34 bool ObjectSnapshot::IsBoundAsset(const Asset& asset)
35 {
36     auto it = changedAssets_.find(asset.uri);
37     if (it != changedAssets_.end()) {
38         return true;
39     }
40     return false;
41 }
42 
Download(Asset & asset)43 int32_t ObjectSnapshot::Download(Asset& asset)
44 {
45     if (!IsBoundAsset(asset)) {
46         return 0;
47     }
48     return assetMachine_->DFAPostEvent(DOWNLOAD, changedAssets_[asset.uri], asset);
49 }
50 
GetAssetStatus(Asset & asset)51 TransferStatus ObjectSnapshot::GetAssetStatus(Asset& asset)
52 {
53     if (!IsBoundAsset(asset)) {
54         return STATUS_BUTT;
55     }
56     return changedAssets_[asset.uri].status;
57 }
58 
Uploaded(Asset & asset)59 int32_t ObjectSnapshot::Uploaded(Asset& asset)
60 {
61     if (!IsBoundAsset(asset)) {
62         return E_OK;
63     }
64     return assetMachine_->DFAPostEvent(UPLOAD_FINISHED, changedAssets_[asset.uri], asset);
65 }
66 
Downloaded(Asset & asset)67 int32_t ObjectSnapshot::Downloaded(Asset& asset)
68 {
69     if (!IsBoundAsset(asset)) {
70         return E_OK;
71     }
72     return assetMachine_->DFAPostEvent(DOWNLOAD_FINISHED, changedAssets_[asset.uri], asset);
73 }
74 
OnDataChanged(Asset & asset,const std::string & deviceId)75 int32_t ObjectSnapshot::OnDataChanged(Asset& asset, const std::string& deviceId)
76 {
77     if (!IsBoundAsset(asset)) {
78         return E_OK;
79     }
80     std::pair<std::string, Asset> newAsset{ deviceId, asset };
81     return assetMachine_->DFAPostEvent(REMOTE_CHANGED, changedAssets_[asset.uri], asset, newAsset);
82 }
83 
BindAsset(const Asset & asset,const DistributedData::AssetBindInfo & bindInfo,const StoreInfo & storeInfo)84 int32_t ObjectSnapshot::BindAsset(const Asset& asset, const DistributedData::AssetBindInfo& bindInfo,
85     const StoreInfo& storeInfo)
86 {
87     if (IsBoundAsset(asset)) {
88         ZLOGD("Asset is bound. asset.uri:%{public}s :", Anonymous::Change(asset.uri, true).c_str());
89         return E_OK;
90     }
91     changedAssets_[asset.uri] = ChangedAssetInfo(asset, bindInfo, storeInfo);
92     return E_OK;
93 }
94 
Transferred(Asset & asset)95 int32_t ObjectSnapshot::Transferred(Asset& asset)
96 {
97     if (!IsBoundAsset(asset)) {
98         return E_OK;
99     }
100     return assetMachine_->DFAPostEvent(TRANSFER_FINISHED, changedAssets_[asset.uri], asset);
101 }
~ObjectSnapshot()102 ObjectSnapshot::~ObjectSnapshot() {}
103 
ObjectSnapshot()104 ObjectSnapshot::ObjectSnapshot()
105 {
106     assetMachine_ = std::make_shared<ObjectAssetMachine>();
107 }
108 } // namespace OHOS::DistributedObject