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