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 #include "user_change_monitor.h"
17
18 #include "db_errno.h"
19 #include "log_print.h"
20
21 namespace DistributedDB {
UserChangeMonitor()22 UserChangeMonitor::UserChangeMonitor()
23 : userNotifier_(nullptr),
24 isStarted_(false)
25 {
26 }
27
~UserChangeMonitor()28 UserChangeMonitor::~UserChangeMonitor()
29 {
30 Stop();
31 }
32
Start()33 int UserChangeMonitor::Start()
34 {
35 if (isStarted_) {
36 return E_OK;
37 }
38
39 int errCode = PrepareNotifierChain();
40 if (errCode != E_OK) {
41 return errCode;
42 }
43 isStarted_ = true;
44 return E_OK;
45 }
46
Stop()47 void UserChangeMonitor::Stop()
48 {
49 if (!isStarted_) {
50 return;
51 }
52 if (userNotifier_ != nullptr) {
53 userNotifier_->UnRegisterEventType(USER_ACTIVE_EVENT);
54 userNotifier_->UnRegisterEventType(USER_NON_ACTIVE_EVENT);
55 userNotifier_->UnRegisterEventType(USER_ACTIVE_TO_NON_ACTIVE_EVENT);
56 RefObject::KillAndDecObjRef(userNotifier_);
57 userNotifier_ = nullptr;
58 }
59 isStarted_ = false;
60 }
61
RegisterUserChangedListener(const UserChangedAction & action,EventType event,int & errCode)62 NotificationChain::Listener *UserChangeMonitor::RegisterUserChangedListener(const UserChangedAction &action,
63 EventType event, int &errCode)
64 {
65 std::shared_lock<std::shared_mutex> lockGuard(userChangeMonitorLock_);
66 if (action == nullptr) {
67 errCode = -E_INVALID_ARGS;
68 return nullptr;
69 }
70 if (userNotifier_ == nullptr) {
71 errCode = -E_NOT_INIT;
72 return nullptr;
73 }
74 return userNotifier_->RegisterListener(event, action, nullptr, errCode);
75 }
76
PrepareNotifierChain()77 int UserChangeMonitor::PrepareNotifierChain()
78 {
79 std::unique_lock<std::shared_mutex> lockGuard(userChangeMonitorLock_);
80 if (userNotifier_ != nullptr) {
81 return E_OK;
82 }
83 userNotifier_ = new (std::nothrow) NotificationChain();
84 if (userNotifier_ == nullptr) {
85 return -E_OUT_OF_MEMORY;
86 }
87 int errCode = userNotifier_->RegisterEventType(USER_ACTIVE_EVENT);
88 if (errCode != E_OK) {
89 goto ERROR_HANDLE;
90 }
91 errCode = userNotifier_->RegisterEventType(USER_NON_ACTIVE_EVENT);
92 if (errCode != E_OK) {
93 userNotifier_->UnRegisterEventType(USER_ACTIVE_EVENT);
94 goto ERROR_HANDLE;
95 }
96 errCode = userNotifier_->RegisterEventType(USER_ACTIVE_TO_NON_ACTIVE_EVENT);
97 if (errCode != E_OK) {
98 userNotifier_->UnRegisterEventType(USER_ACTIVE_EVENT);
99 userNotifier_->UnRegisterEventType(USER_NON_ACTIVE_EVENT);
100 goto ERROR_HANDLE;
101 }
102 return errCode;
103 ERROR_HANDLE:
104 RefObject::KillAndDecObjRef(userNotifier_);
105 userNotifier_ = nullptr;
106 return errCode;
107 }
108
NotifyUserChanged() const109 void UserChangeMonitor::NotifyUserChanged() const
110 {
111 std::shared_lock<std::shared_mutex> lockGuard(userChangeMonitorLock_);
112 if (userNotifier_ == nullptr) {
113 LOGD("NotifyUNotifyUserChangedserChange fail, userChangedNotifier is null.");
114 return;
115 }
116 LOGI("[UserChangeMonitor] begin to notify event");
117 userNotifier_->NotifyEvent(USER_ACTIVE_EVENT, nullptr);
118 userNotifier_->NotifyEvent(USER_NON_ACTIVE_EVENT, nullptr);
119 userNotifier_->NotifyEvent(USER_ACTIVE_TO_NON_ACTIVE_EVENT, nullptr);
120 }
121 } // namespace DistributedDB