1 /*
2  * Copyright (c) 2024 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 "hibernate_controller.h"
17 #include "power_log.h"
18 #include "system_suspend_controller.h"
19 
20 namespace OHOS {
21 namespace PowerMgr {
Hibernate(bool clearMemory)22 bool HibernateController::Hibernate(bool clearMemory)
23 {
24     return SystemSuspendController::GetInstance().Hibernate();
25 }
26 
RegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback> & cb)27 void HibernateController::RegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& cb)
28 {
29     std::lock_guard<std::mutex> lock(mutex_);
30     callbacks_.insert(cb);
31 }
32 
UnregisterSyncHibernateCallback(const sptr<ISyncHibernateCallback> & cb)33 void HibernateController::UnregisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& cb)
34 {
35     std::lock_guard<std::mutex> lock(mutex_);
36     size_t eraseNum = callbacks_.erase(cb);
37     if (eraseNum == 0) {
38         POWER_HILOGE(FEATURE_SUSPEND, "Cannot remove the hibernate callback");
39     }
40 }
41 
PreHibernate() const42 void HibernateController::PreHibernate() const
43 {
44     for (const auto &cb : callbacks_) {
45         if (cb != nullptr) {
46             cb->OnSyncHibernate();
47         }
48     }
49 }
50 
PostHibernate() const51 void HibernateController::PostHibernate() const
52 {
53     for (const auto &cb : callbacks_) {
54         if (cb != nullptr) {
55             cb->OnSyncWakeup();
56         }
57     }
58 }
59 } // namespace PowerMgr
60 } // namespace OHOS
61