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 #ifndef USER_CHANGE_MONITOR_H
17 #define USER_CHANGE_MONITOR_H
18 
19 #include <shared_mutex>
20 
21 #include "macro_utils.h"
22 #include "notification_chain.h"
23 #include "runtime_context.h"
24 
25 namespace DistributedDB {
26 class UserChangeMonitor final {
27 public:
28     UserChangeMonitor();
29     ~UserChangeMonitor();
30 
31     DISABLE_COPY_ASSIGN_MOVE(UserChangeMonitor);
32 
33     // Start the UserChangeMonitor
34     int Start();
35 
36     // Stop the UserChangeMonitor
37     void Stop();
38 
39     // Register a user changed lister, it will be callback when user changed.
40     NotificationChain::Listener *RegisterUserChangedListener(const UserChangedAction &action, EventType event,
41         int &errCode);
42 
43     // Notify USER_CHANGE_EVENT.
44     void NotifyUserChanged() const;
45     static constexpr EventType USER_ACTIVE_EVENT = 3;
46     static constexpr EventType USER_NON_ACTIVE_EVENT = 4;
47     static constexpr EventType USER_ACTIVE_TO_NON_ACTIVE_EVENT = 5;
48 private:
49     // prepare notifier chain
50     int PrepareNotifierChain();
51 
52     mutable std::shared_mutex userChangeMonitorLock_;
53     NotificationChain *userNotifier_;
54     bool isStarted_ = false;
55 };
56 } // namespace DistributedDB
57 
58 #endif // USER_CHANGE_MONITOR_H