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 "lock_status_observer.h"
17 #include "log_print.h"
18 
19 namespace DistributedDB {
LockStatusObserver()20 LockStatusObserver::LockStatusObserver()
21     : lockStatusChangedNotifier_(nullptr),
22       isStarted_(false)
23 {}
24 
~LockStatusObserver()25 LockStatusObserver::~LockStatusObserver()
26 {
27     Stop();
28 }
29 
Start()30 int LockStatusObserver::Start()
31 {
32     if (isStarted_) {
33         return E_OK;
34     }
35 
36     int errCode = PrepareNotifierChain();
37     if (errCode != E_OK) {
38         LOGE("PrepareNotifierChain failed, errorCode = %d", errCode);
39         return errCode;
40     }
41     isStarted_ = true;
42     return E_OK;
43 }
44 
IsStarted() const45 bool LockStatusObserver::IsStarted() const
46 {
47     return isStarted_;
48 }
49 
Stop()50 void LockStatusObserver::Stop()
51 {
52     if (!isStarted_) {
53         return;
54     }
55 
56     lockStatusChangedNotifier_->UnRegisterEventType(LOCK_STATUS_CHANGE_EVENT);
57     RefObject::KillAndDecObjRef(lockStatusChangedNotifier_);
58     lockStatusChangedNotifier_ = nullptr;
59     isStarted_ = false;
60 }
61 
PrepareNotifierChain()62 int LockStatusObserver::PrepareNotifierChain()
63 {
64     if (lockStatusChangedNotifier_ != nullptr) {
65         return E_OK;
66     }
67 
68     lockStatusChangedNotifier_ = new (std::nothrow) NotificationChain();
69     if (lockStatusChangedNotifier_ == nullptr) {
70         LOGE("lockStatusChangedNotifier_ is nullptr");
71         return -E_OUT_OF_MEMORY;
72     }
73 
74     int errCode = lockStatusChangedNotifier_->RegisterEventType(LOCK_STATUS_CHANGE_EVENT);
75     if (errCode != E_OK) {
76         LOGE("RegisterEventType failed, errCode = %d", errCode);
77         RefObject::KillAndDecObjRef(lockStatusChangedNotifier_);
78         lockStatusChangedNotifier_ = nullptr;
79     }
80     return errCode;
81 }
82 
RegisterLockStatusChangedLister(const LockStatusNotifier & action,int & errCode) const83 NotificationChain::Listener *LockStatusObserver::RegisterLockStatusChangedLister(const LockStatusNotifier &action,
84     int &errCode) const
85 {
86     if (lockStatusChangedNotifier_ == nullptr) {
87         LOGE("lockStatusChangedNotifier_ is nullptr");
88         errCode = -E_NOT_INIT;
89         return nullptr;
90     }
91 
92     if (!action) {
93         LOGE("action is nullptr");
94         errCode = -E_INVALID_ARGS;
95         return nullptr;
96     }
97     return lockStatusChangedNotifier_->RegisterListener(LOCK_STATUS_CHANGE_EVENT, action, nullptr, errCode);
98 }
99 
OnStatusChange(bool isLocked) const100 void LockStatusObserver::OnStatusChange(bool isLocked) const
101 {
102     if (lockStatusChangedNotifier_ == nullptr) {
103         LOGE("lockStatusChangedNotifier_ is nullptr");
104         return;
105     }
106     lockStatusChangedNotifier_->NotifyEvent(LOCK_STATUS_CHANGE_EVENT, &isLocked);
107 }
108 }  // namespace DistributedDB
109