• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_event_handler.h"
17 
18 #include "device_profile_errors.h"
19 #include "device_profile_log.h"
20 
21 namespace OHOS {
22 namespace DeviceProfile {
23 namespace {
24 const std::string TAG = "ProfileEventHandler";
25 }
26 
Init()27 bool ProfileEventHandler::Init()
28 {
29     auto runner = AppExecFwk::EventRunner::Create(handlerName_);
30     eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
31     if (eventHandler_ == nullptr) {
32         return false;
33     }
34     return true;
35 }
36 
Subscribe(const SubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & profileEventNotifier)37 int32_t ProfileEventHandler::Subscribe(const SubscribeInfo& subscribeInfo,
38     const sptr<IRemoteObject>& profileEventNotifier)
39 {
40     std::lock_guard<std::mutex> autoLock(notifierLock_);
41     auto iter = profileEventSubscribeInfos_.find(profileEventNotifier);
42     if (iter == profileEventSubscribeInfos_.end()) {
43         profileEventSubscribeInfos_.emplace(profileEventNotifier, std::move(subscribeInfo));
44     } else {
45         // just overwrite when the follow-ups subscribe with the same notifier
46         HILOGW("overwrite last subscribed info");
47         iter->second = std::move(subscribeInfo);
48     }
49 
50     if (!isRegistered_) {
51         int32_t errCode = Register();
52         if (errCode != ERR_OK) {
53             HILOGE("errCode = %{public}d", errCode);
54             return ERR_DP_SUBSCRIBE_FAILED;
55         }
56         isRegistered_ = true;
57     }
58     return ERR_OK;
59 }
60 
Unsubscribe(const sptr<IRemoteObject> & profileEventNotifier)61 int32_t ProfileEventHandler::Unsubscribe(const sptr<IRemoteObject>& profileEventNotifier)
62 {
63     std::lock_guard<std::mutex> autoLock(notifierLock_);
64     auto iter = profileEventSubscribeInfos_.find(profileEventNotifier);
65     if (iter == profileEventSubscribeInfos_.end()) {
66         HILOGW("not subscribe yet");
67         return ERR_DP_NOT_SUBSCRIBED;
68     }
69     profileEventSubscribeInfos_.erase(iter);
70     if (profileEventSubscribeInfos_.empty()) {
71         int32_t errCode = Unregister();
72         if (errCode != ERR_OK) {
73             HILOGE("errCode = %{public}d", errCode);
74             return ERR_DP_UNSUBSCRIBE_FAILED;
75         }
76         isRegistered_ = false;
77     }
78     return ERR_OK;
79 }
80 
OnSubscriberDied(const sptr<IRemoteObject> & profileEventNotifier)81 void ProfileEventHandler::OnSubscriberDied(const sptr<IRemoteObject>& profileEventNotifier)
82 {
83     HILOGD("called");
84     Unsubscribe(profileEventNotifier);
85 }
86 
IsRegistered() const87 bool ProfileEventHandler::IsRegistered() const
88 {
89     return isRegistered_;
90 }
91 } // namespace DeviceProfile
92 } // namespace OHOS