• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "profile_sync_handler.h"
17 
18 #include "device_profile_log.h"
19 #include "device_profile_storage_manager.h"
20 #include "device_profile_utils.h"
21 #include "dp_radar_helper.h"
22 #include "subscribe_manager.h"
23 
24 namespace OHOS {
25 namespace DeviceProfile {
26 using namespace OHOS::DistributedKv;
27 
28 namespace {
29 const std::string TAG = "ProfileSyncHandler";
30 }
31 
SyncCompleted(const std::map<std::string,Status> & results)32 void ProfileSyncHandler::SyncCompleted(const std::map<std::string, Status>& results)
33 {
34     HILOGI("called");
35     SyncResult syncResults;
36     for (const auto& [deviceId, status] : results) {
37         HILOGD("deviceId = %{public}s, status = %{public}d",
38             DeviceProfileUtils::AnonymizeDeviceId(deviceId).c_str(), status);
39         SyncStatus syncStauts = (status == Status::SUCCESS) ? SUCCEEDED : FAILED;
40         syncResults.emplace(deviceId, syncStauts);
41     }
42 
43     auto notifyTask = [this, syncResults = std::move(syncResults)]() {
44         NotifySyncCompleted(syncResults);
45     };
46     std::lock_guard<std::mutex> autoLock(notifierLock_);
47     if (eventHandler_ != nullptr && !eventHandler_->PostTask(notifyTask)) {
48         HILOGI("post task failed");
49     }
50 }
51 
NotifySyncCompleted(const SyncResult & syncResults)52 void ProfileSyncHandler::NotifySyncCompleted(const SyncResult& syncResults)
53 {
54     struct RadarInfo info = {
55         .funcName = "NotifySyncCompleted",
56         .stageRes = static_cast<int32_t>(StageRes::STAGE_SUCC),
57         .bizState = static_cast<int32_t>(BizState::BIZ_STATE_END),
58         .hostName = kvNAME,
59     };
60     if (!DpRadarHelper::GetInstance().ReportSyncDataCb(info)) {
61         HILOGE("ReportSyncDataCb failed");
62     }
63     {
64         std::lock_guard<std::mutex> autoLock(notifierLock_);
65         HILOGI("profileEventSubscribeInfos size %{public}zu", profileEventSubscribeInfos_.size());
66         auto iter = profileEventSubscribeInfos_.begin();
67         while (iter != profileEventSubscribeInfos_.end()) {
68             sptr<IProfileEventNotifier> profileEventNotifier = iface_cast<IProfileEventNotifier>(iter->first);
69             if (profileEventNotifier == nullptr) {
70                 HILOGE("cast to IProfileEventNotifier failed");
71                 iter++;
72             } else {
73                 profileEventNotifier->OnSyncCompleted(syncResults);
74                 HILOGI("sync remove, deviceId = %{public}s",
75                     ProfileEventHandler::AnonymizeSubscribeInfo(iter->second).c_str());
76                 profileEventSubscribeInfos_.erase(iter++);
77                 HILOGI("profileEventSubscribeInfos_ size = %{public}zu", profileEventSubscribeInfos_.size());
78                 for (const auto& entry : profileEventSubscribeInfos_) {
79                     HILOGI("subscribeInfo: deviceId = %{public}s",
80                         ProfileEventHandler::AnonymizeSubscribeInfo(entry.second).c_str());
81                 }
82             }
83         }
84     }
85     DeviceProfileStorageManager::GetInstance().NotifySyncCompleted();
86 }
87 
Register()88 int32_t ProfileSyncHandler::Register()
89 {
90     HILOGI("called");
91     return DeviceProfileStorageManager::GetInstance().RegisterSyncCallback(shared_from_this());
92 }
93 
Unregister()94 int32_t ProfileSyncHandler::Unregister()
95 {
96     HILOGI("called");
97     return DeviceProfileStorageManager::GetInstance().UnRegisterSyncCallback();
98 }
99 } // namespace DeviceProfile
100 } // namespace OHOS
101