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 #define LOG_TAG "ObjectDataListener"
17 
18 #include "object_data_listener.h"
19 #include "log_print.h"
20 #include "object_manager.h"
21 #include "object_radar_reporter.h"
22 #include "utils/anonymous.h"
23 namespace OHOS {
24 namespace DistributedObject {
ObjectDataListener()25 ObjectDataListener::ObjectDataListener()
26 {
27 }
28 
~ObjectDataListener()29 ObjectDataListener::~ObjectDataListener()
30 {
31 }
32 
OnChange(const DistributedDB::KvStoreChangedData & data)33 void ObjectDataListener::OnChange(const DistributedDB::KvStoreChangedData &data)
34 {
35     const auto &insertedDatas = data.GetEntriesInserted();
36     const auto &updatedDatas = data.GetEntriesUpdated();
37     std::map<std::string, std::vector<uint8_t>> changedData {};
38     for (const auto &entry : insertedDatas) {
39         std::string key(entry.key.begin(), entry.key.end());
40         changedData.insert_or_assign(std::move(key), entry.value);
41     }
42     for (const auto &entry : updatedDatas) {
43         std::string key(entry.key.begin(), entry.key.end());
44         changedData.insert_or_assign(std::move(key), entry.value);
45     }
46     DistributedObject::ObjectStoreManager::GetInstance()->NotifyChange(changedData);
47 }
48 
OnStart(const std::string & srcNetworkId,const std::string & dstNetworkId,const std::string & sessionId,const std::string & dstBundleName)49 int32_t ObjectAssetsRecvListener::OnStart(const std::string &srcNetworkId, const std::string &dstNetworkId,
50     const std::string &sessionId, const std::string &dstBundleName)
51 {
52     auto objectKey = dstBundleName + sessionId;
53     ZLOGI("OnStart, objectKey:%{public}s", objectKey.c_str());
54     ObjectStoreManager::GetInstance()->NotifyAssetsStart(objectKey, srcNetworkId);
55     return OBJECT_SUCCESS;
56 }
57 
OnFinished(const std::string & srcNetworkId,const sptr<AssetObj> & assetObj,int32_t result)58 int32_t ObjectAssetsRecvListener::OnFinished(const std::string &srcNetworkId, const sptr<AssetObj> &assetObj,
59     int32_t result)
60 {
61     if (assetObj == nullptr) {
62         ZLOGE("OnFinished error! status:%{public}d, srcNetworkId:%{public}s", result,
63             DistributedData::Anonymous::Change(srcNetworkId).c_str());
64         ObjectStore::RadarReporter::ReportStageError(std::string(__FUNCTION__), ObjectStore::DATA_RESTORE,
65             ObjectStore::ASSETS_RECV, ObjectStore::RADAR_FAILED, result);
66         return result;
67     }
68     auto objectKey = assetObj->dstBundleName_+assetObj->sessionId_;
69     ZLOGI("OnFinished, status:%{public}d objectKey:%{public}s, asset size:%{public}zu", result, objectKey.c_str(),
70         assetObj->uris_.size());
71     ObjectStoreManager::GetInstance()->NotifyAssetsReady(objectKey, assetObj->dstBundleName_, srcNetworkId);
72     return OBJECT_SUCCESS;
73 }
74 }  // namespace DistributedObject
75 }  // namespace OHOS
76