• 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 
16 #include <cinttypes>
17 #define LOG_TAG "ObjectDataListener"
18 
19 #include "object_data_listener.h"
20 #include "log_print.h"
21 #include "object_manager.h"
22 #include "object_radar_reporter.h"
23 #include "utils/anonymous.h"
24 namespace OHOS {
25 namespace DistributedObject {
26 constexpr int32_t PROGRESS_MAX = 100;
27 constexpr int32_t PROGRESS_INVALID = -1;
ObjectDataListener()28 ObjectDataListener::ObjectDataListener()
29 {
30 }
31 
~ObjectDataListener()32 ObjectDataListener::~ObjectDataListener()
33 {
34 }
35 
OnChange(const DistributedDB::KvStoreChangedData & data)36 void ObjectDataListener::OnChange(const DistributedDB::KvStoreChangedData &data)
37 {
38     const auto &insertedDatas = data.GetEntriesInserted();
39     const auto &updatedDatas = data.GetEntriesUpdated();
40     std::map<std::string, std::vector<uint8_t>> changedData {};
41     for (const auto &entry : insertedDatas) {
42         std::string key(entry.key.begin(), entry.key.end());
43         changedData.insert_or_assign(std::move(key), entry.value);
44     }
45     for (const auto &entry : updatedDatas) {
46         std::string key(entry.key.begin(), entry.key.end());
47         changedData.insert_or_assign(std::move(key), entry.value);
48     }
49     DistributedObject::ObjectStoreManager::GetInstance().NotifyChange(changedData);
50 }
51 
OnStart(const std::string & srcNetworkId,const std::string & dstNetworkId,const std::string & sessionId,const std::string & dstBundleName)52 int32_t ObjectAssetsRecvListener::OnStart(const std::string &srcNetworkId, const std::string &dstNetworkId,
53     const std::string &sessionId, const std::string &dstBundleName)
54 {
55     auto objectKey = dstBundleName + sessionId;
56     ZLOGI("OnStart, objectKey:%{public}s", DistributedData::Anonymous::Change(objectKey).c_str());
57     ObjectStoreManager::GetInstance().NotifyAssetsStart(objectKey, srcNetworkId);
58     ObjectStoreManager::GetInstance().NotifyAssetsRecvProgress(objectKey, 0);
59     return OBJECT_SUCCESS;
60 }
61 
OnFinished(const std::string & srcNetworkId,const sptr<AssetObj> & assetObj,int32_t result)62 int32_t ObjectAssetsRecvListener::OnFinished(const std::string &srcNetworkId, const sptr<AssetObj> &assetObj,
63     int32_t result)
64 {
65     if (assetObj == nullptr) {
66         ZLOGE("OnFinished error! status:%{public}d, srcNetworkId:%{public}s", result,
67             DistributedData::Anonymous::Change(srcNetworkId).c_str());
68         ObjectStore::RadarReporter::ReportStageError(std::string(__FUNCTION__), ObjectStore::DATA_RESTORE,
69             ObjectStore::ASSETS_RECV, ObjectStore::RADAR_FAILED, result);
70         return result;
71     }
72     auto objectKey = assetObj->dstBundleName_ + assetObj->sessionId_;
73     ZLOGI("OnFinished, status:%{public}d objectKey:%{public}s, asset size:%{public}zu", result,
74         DistributedData::Anonymous::Change(objectKey).c_str(), assetObj->uris_.size());
75     ObjectStoreManager::GetInstance().NotifyAssetsReady(objectKey, assetObj->dstBundleName_, srcNetworkId);
76     if (result != OBJECT_SUCCESS) {
77         ObjectStoreManager::GetInstance().NotifyAssetsRecvProgress(objectKey, PROGRESS_INVALID);
78     } else {
79         ObjectStoreManager::GetInstance().NotifyAssetsRecvProgress(objectKey, PROGRESS_MAX);
80     }
81     return OBJECT_SUCCESS;
82 }
83 
84 
OnRecvProgress(const std::string & srcNetworkId,const sptr<AssetObj> & assetObj,uint64_t totalBytes,uint64_t processBytes)85 int32_t ObjectAssetsRecvListener::OnRecvProgress(
86     const std::string &srcNetworkId, const sptr<AssetObj> &assetObj, uint64_t totalBytes, uint64_t processBytes)
87 {
88     if (assetObj == nullptr || totalBytes == 0) {
89         ZLOGE("OnRecvProgress error! srcNetworkId:%{public}s, totalBytes: %{public}" PRIu64,
90             DistributedData::Anonymous::Change(srcNetworkId).c_str(), totalBytes);
91         return OBJECT_INNER_ERROR;
92     }
93 
94     auto objectKey = assetObj->dstBundleName_ + assetObj->sessionId_;
95     ZLOGI("srcNetworkId: %{public}s, objectKey:%{public}s, totalBytes: %{public}" PRIu64
96           ", processBytes: %{public}" PRIu64 ".",
97         DistributedData::Anonymous::Change(srcNetworkId).c_str(),
98         DistributedData::Anonymous::Change(objectKey).c_str(), totalBytes, processBytes);
99     int32_t progress = static_cast<int32_t>((processBytes * 100.0 / totalBytes) * 0.9);
100     ObjectStoreManager::GetInstance().NotifyAssetsRecvProgress(objectKey, progress);
101     return OBJECT_SUCCESS;
102 }
103 }  // namespace DistributedObject
104 }  // namespace OHOS
105