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