• 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     pid_t pid = IPCSkeleton::GetCallingPid();
35     auto uid = IPCSkeleton::GetCallingUid();
36     cachedRegister_.emplace(cb, std::make_pair(pid, uid));
37 }
38 
UnregisterSyncHibernateCallback(const sptr<ISyncHibernateCallback> & cb)39 void HibernateController::UnregisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& cb)
40 {
41     std::lock_guard<std::mutex> lock(mutex_);
42     size_t eraseNum = callbacks_.erase(cb);
43     if (eraseNum == 0) {
44         POWER_HILOGE(FEATURE_SUSPEND, "Cannot remove the hibernate callback");
45     }
46     auto iter = cachedRegister_.find(cb);
47     if (iter != cachedRegister_.end()) {
48         cachedRegister_.erase(iter);
49     }
50 }
51 
PreHibernate()52 void HibernateController::PreHibernate()
53 {
54     std::lock_guard<std::mutex> lock(mutex_);
55     for (const auto &cb : callbacks_) {
56         auto iter = cachedRegister_.find(cb);
57         auto pidUid = ((iter != cachedRegister_.end()) ? iter->second : std::make_pair(0, 0));
58         if (cb != nullptr) {
59             // PreHibernate calling callback pid uid
60             POWER_HILOGI(FEATURE_SUSPEND, "PreCb P=%{public}dU=%{public}d", pidUid.first, pidUid.second);
61             cb->OnSyncHibernate();
62         }
63     }
64     prepared_ = true;
65 }
66 
PostHibernate(bool hibernateResult)67 void HibernateController::PostHibernate(bool hibernateResult)
68 {
69     std::lock_guard<std::mutex> lock(mutex_);
70     if (!prepared_) {
71         POWER_HILOGE(FEATURE_SUSPEND, "No need to run OnSyncWakeup");
72         return;
73     }
74     prepared_ = false;
75     for (const auto &cb : callbacks_) {
76         auto iter = cachedRegister_.find(cb);
77         auto pidUid = ((iter != cachedRegister_.end()) ? iter->second : std::make_pair(0, 0));
78         if (cb != nullptr) {
79             // PostHibernate calling callback pid uid
80             POWER_HILOGI(FEATURE_SUSPEND, "PostCb P=%{public}dU=%{public}d", pidUid.first, pidUid.second);
81             cb->OnSyncWakeup(hibernateResult);
82         }
83     }
84 }
85 } // namespace PowerMgr
86 } // namespace OHOS
87