• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     LOGI("[UserChangeMonitor] RegisterUserChangedListener event=%d", event);
75     return userNotifier_->RegisterListener(event, action, nullptr, errCode);
76 }
77 
PrepareNotifierChain()78 int UserChangeMonitor::PrepareNotifierChain()
79 {
80     std::unique_lock<std::shared_mutex> lockGuard(userChangeMonitorLock_);
81     if (userNotifier_ != nullptr) {
82         return E_OK;
83     }
84     userNotifier_ = new (std::nothrow) NotificationChain();
85     if (userNotifier_ == nullptr) {
86         return -E_OUT_OF_MEMORY;
87     }
88     int errCode = userNotifier_->RegisterEventType(USER_ACTIVE_EVENT);
89     if (errCode != E_OK) {
90         goto ERROR_HANDLE;
91     }
92     errCode = userNotifier_->RegisterEventType(USER_NON_ACTIVE_EVENT);
93     if (errCode != E_OK) {
94         userNotifier_->UnRegisterEventType(USER_ACTIVE_EVENT);
95         goto ERROR_HANDLE;
96     }
97     errCode = userNotifier_->RegisterEventType(USER_ACTIVE_TO_NON_ACTIVE_EVENT);
98     if (errCode != E_OK) {
99         userNotifier_->UnRegisterEventType(USER_ACTIVE_EVENT);
100         userNotifier_->UnRegisterEventType(USER_NON_ACTIVE_EVENT);
101         goto ERROR_HANDLE;
102     }
103     return errCode;
104 ERROR_HANDLE:
105     RefObject::KillAndDecObjRef(userNotifier_);
106     userNotifier_ = nullptr;
107     return errCode;
108 }
109 
NotifyUserChanged() const110 void UserChangeMonitor::NotifyUserChanged() const
111 {
112     std::shared_lock<std::shared_mutex> lockGuard(userChangeMonitorLock_);
113     if (userNotifier_ == nullptr) {
114         LOGD("NotifyUNotifyUserChangedserChange fail, userChangedNotifier is null.");
115         return;
116     }
117     LOGI("[UserChangeMonitor] begin to notify event");
118     userNotifier_->NotifyEvent(USER_ACTIVE_EVENT, nullptr);
119     userNotifier_->NotifyEvent(USER_NON_ACTIVE_EVENT, nullptr);
120     userNotifier_->NotifyEvent(USER_ACTIVE_TO_NON_ACTIVE_EVENT, nullptr);
121 }
122 } // namespace DistributedDB