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_event_handler.h"
17 
18 #include "device_profile_errors.h"
19 #include "device_profile_log.h"
20 #include "device_profile_utils.h"
21 
22 namespace OHOS {
23 namespace DeviceProfile {
24 namespace {
25 const std::string TAG = "ProfileEventHandler";
26 }
27 
Init()28 bool ProfileEventHandler::Init()
29 {
30     auto runner = AppExecFwk::EventRunner::Create(handlerName_);
31     eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
32     if (eventHandler_ == nullptr) {
33         return false;
34     }
35     return true;
36 }
37 
Subscribe(const SubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & profileEventNotifier)38 int32_t ProfileEventHandler::Subscribe(const SubscribeInfo& subscribeInfo,
39     const sptr<IRemoteObject>& profileEventNotifier)
40 {
41     std::lock_guard<std::mutex> autoLock(notifierLock_);
42     auto iter = profileEventSubscribeInfos_.find(profileEventNotifier);
43     if (iter == profileEventSubscribeInfos_.end()) {
44         HILOGI("add subscribeInfo, deviceId = %{public}s", AnonymizeSubscribeInfo(subscribeInfo).c_str());
45         profileEventSubscribeInfos_.emplace(profileEventNotifier, std::move(subscribeInfo));
46         HILOGI("profileEventSubscribeInfos_ size = %{public}zu", profileEventSubscribeInfos_.size());
47         for (const auto& entry : profileEventSubscribeInfos_) {
48             const SubscribeInfo& subscribeInfo = entry.second;
49             HILOGI("subscribeInfo: deviceId = %{public}s", AnonymizeSubscribeInfo(subscribeInfo).c_str());
50         }
51     } else {
52         // just overwrite when the follow-ups subscribe with the same notifier
53         HILOGW("overwrite last subscribed info");
54         HILOGI("update subscribeInfo, deviceId = %{public}s", AnonymizeSubscribeInfo(subscribeInfo).c_str());
55         iter->second = std::move(subscribeInfo);
56         HILOGI("profileEventSubscribeInfos_ size = %{public}zu", profileEventSubscribeInfos_.size());
57         for (const auto& entry : profileEventSubscribeInfos_) {
58             HILOGI("subscribeInfo: deviceId = %{public}s", AnonymizeSubscribeInfo(entry.second).c_str());
59         }
60     }
61 
62     if (!isRegistered_) {
63         int32_t errCode = Register();
64         if (errCode != ERR_OK) {
65             HILOGE("errCode = %{public}d", errCode);
66             return ERR_DP_SUBSCRIBE_FAILED;
67         }
68         isRegistered_ = true;
69     }
70     return ERR_OK;
71 }
72 
Unsubscribe(const sptr<IRemoteObject> & profileEventNotifier)73 int32_t ProfileEventHandler::Unsubscribe(const sptr<IRemoteObject>& profileEventNotifier)
74 {
75     std::lock_guard<std::mutex> autoLock(notifierLock_);
76     auto iter = profileEventSubscribeInfos_.find(profileEventNotifier);
77     if (iter == profileEventSubscribeInfos_.end()) {
78         HILOGW("not subscribe yet");
79         return ERR_DP_NOT_SUBSCRIBED;
80     }
81     HILOGI("remove subscribeInfo, deviceId = %{public}s", AnonymizeSubscribeInfo(iter->second).c_str());
82     profileEventSubscribeInfos_.erase(iter);
83     HILOGI("profileEventSubscribeInfos_ size = %{public}zu", profileEventSubscribeInfos_.size());
84     for (const auto& entry : profileEventSubscribeInfos_) {
85         HILOGI("subscribeInfo: deviceId = %{public}s", AnonymizeSubscribeInfo(entry.second).c_str());
86     }
87     if (profileEventSubscribeInfos_.empty()) {
88         int32_t errCode = Unregister();
89         if (errCode != ERR_OK) {
90             HILOGE("errCode = %{public}d", errCode);
91             return ERR_DP_UNSUBSCRIBE_FAILED;
92         }
93         isRegistered_ = false;
94     }
95     return ERR_OK;
96 }
97 
OnSubscriberDied(const sptr<IRemoteObject> & profileEventNotifier)98 void ProfileEventHandler::OnSubscriberDied(const sptr<IRemoteObject>& profileEventNotifier)
99 {
100     HILOGD("called");
101     Unsubscribe(profileEventNotifier);
102 }
103 
IsRegistered() const104 bool ProfileEventHandler::IsRegistered() const
105 {
106     return isRegistered_;
107 }
108 
AnonymizeSubscribeInfo(const SubscribeInfo & subscribeInfo)109 std::string ProfileEventHandler::AnonymizeSubscribeInfo(const SubscribeInfo& subscribeInfo)
110 {
111     if (!subscribeInfo.extraInfo.contains("deviceId") || !subscribeInfo.extraInfo["deviceId"].is_string()) {
112         return "";
113     }
114     return DeviceProfileUtils::AnonymizeDeviceId(subscribeInfo.extraInfo["deviceId"].get<std::string>());
115 }
116 } // namespace DeviceProfile
117 } // namespace OHOS
118