• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 HibernateStatus HibernateController::Hibernate(bool clearMemory)
23 {
24     if (SystemSuspendController::GetInstance().Hibernate()) {
25         return HibernateStatus::HIBERNATE_SUCCESS;
26     }
27     return HibernateStatus::HIBERNATE_FAILURE;
28 }
29 
RegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback> & cb)30 void HibernateController::RegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& cb)
31 {
32     std::lock_guard<std::mutex> lock(mutex_);
33     callbacks_.insert(cb);
34 }
35 
UnregisterSyncHibernateCallback(const sptr<ISyncHibernateCallback> & cb)36 void HibernateController::UnregisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& cb)
37 {
38     std::lock_guard<std::mutex> lock(mutex_);
39     size_t eraseNum = callbacks_.erase(cb);
40     if (eraseNum == 0) {
41         POWER_HILOGE(FEATURE_SUSPEND, "Cannot remove the hibernate callback");
42     }
43 }
44 
PreHibernate()45 void HibernateController::PreHibernate()
46 {
47     for (const auto &cb : callbacks_) {
48         if (cb != nullptr) {
49             cb->OnSyncHibernate();
50         }
51     }
52     prepared_ = true;
53 }
54 
PostHibernate(bool hibernateResult)55 void HibernateController::PostHibernate(bool hibernateResult)
56 {
57     if (!prepared_) {
58         POWER_HILOGE(FEATURE_SUSPEND, "No need to run OnSyncWakeup");
59         return;
60     }
61     prepared_ = false;
62     for (const auto &cb : callbacks_) {
63         if (cb != nullptr) {
64             cb->OnSyncWakeup(hibernateResult);
65         }
66     }
67 }
68 } // namespace PowerMgr
69 } // namespace OHOS
70