1 /*
2 * Copyright (c) 2021 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 "subscribe_manager.h"
22
23 namespace OHOS {
24 namespace DeviceProfile {
25 using namespace OHOS::DistributedKv;
26
27 namespace {
28 const std::string TAG = "ProfileSyncHandler";
29 }
30
SyncCompleted(const std::map<std::string,Status> & results)31 void ProfileSyncHandler::SyncCompleted(const std::map<std::string, Status>& results)
32 {
33 HILOGI("called");
34 SyncResult syncResults;
35 for (const auto& [deviceId, status] : results) {
36 HILOGD("deviceId = %{public}s, status = %{public}d",
37 DeviceProfileUtils::AnonymizeDeviceId(deviceId).c_str(), status);
38 SyncStatus syncStauts = (status == Status::SUCCESS) ? SUCCEEDED : FAILED;
39 syncResults.emplace(deviceId, syncStauts);
40 }
41
42 auto notifyTask = [this, syncResults = std::move(syncResults)]() {
43 NotifySyncCompleted(syncResults);
44 };
45 std::lock_guard<std::mutex> autoLock(notifierLock_);
46 if (eventHandler_ != nullptr && !eventHandler_->PostTask(notifyTask)) {
47 HILOGI("post task failed");
48 }
49 }
50
NotifySyncCompleted(const SyncResult & syncResults)51 void ProfileSyncHandler::NotifySyncCompleted(const SyncResult& syncResults)
52 {
53 {
54 std::lock_guard<std::mutex> autoLock(notifierLock_);
55 for (const auto& [notifier, _] : profileEventSubscribeInfos_) {
56 sptr<IProfileEventNotifier> profileEventNotifier = iface_cast<IProfileEventNotifier>(notifier);
57 if (profileEventNotifier == nullptr) {
58 HILOGE("cast to IProfileEventNotifier failed");
59 continue;
60 }
61 profileEventNotifier->OnSyncCompleted(syncResults);
62 }
63 }
64 DeviceProfileStorageManager::GetInstance().NotifySyncCompleted();
65 }
66
Register()67 int32_t ProfileSyncHandler::Register()
68 {
69 HILOGI("called");
70 return DeviceProfileStorageManager::GetInstance().RegisterSyncCallback(shared_from_this());
71 }
72
Unregister()73 int32_t ProfileSyncHandler::Unregister()
74 {
75 HILOGI("called");
76 return DeviceProfileStorageManager::GetInstance().UnRegisterSyncCallback();
77 }
78 } // namespace DeviceProfile
79 } // namespace OHOS
80