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 #include "power_manager_client.h"
16 #include <unistd.h>
17
18 #include "accesstoken_log.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25 namespace {
26 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
27 LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "PowerMgrClient"
28 };
29 std::mutex g_instanceMutex;
30 } // namespace
31
GetInstance()32 PowerMgrClient& PowerMgrClient::GetInstance()
33 {
34 static PowerMgrClient* instance = nullptr;
35 if (instance == nullptr) {
36 std::lock_guard<std::mutex> lock(g_instanceMutex);
37 if (instance == nullptr) {
38 instance = new PowerMgrClient();
39 }
40 }
41 return *instance;
42 }
43
PowerMgrClient()44 PowerMgrClient::PowerMgrClient()
45 {}
46
~PowerMgrClient()47 PowerMgrClient::~PowerMgrClient()
48 {
49 std::lock_guard<std::mutex> lock(proxyMutex_);
50 ReleaseProxy();
51 }
52
IsScreenOn()53 bool PowerMgrClient::IsScreenOn()
54 {
55 ACCESSTOKEN_LOG_INFO(LABEL, "Entry");
56 auto proxy = GetProxy();
57 if (proxy == nullptr) {
58 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
59 return false;
60 }
61 return proxy->IsScreenOn();
62 }
63
InitProxy()64 void PowerMgrClient::InitProxy()
65 {
66 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
67 if (sam == nullptr) {
68 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null");
69 return;
70 }
71 auto powerManagerSa = sam->GetSystemAbility(POWER_MANAGER_SERVICE_ID);
72 if (powerManagerSa == nullptr) {
73 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null",
74 POWER_MANAGER_SERVICE_ID);
75 return;
76 }
77
78 serviceDeathObserver_ = sptr<PowerMgrDeathRecipient>::MakeSptr();
79 if (serviceDeathObserver_ != nullptr) {
80 powerManagerSa->AddDeathRecipient(serviceDeathObserver_);
81 }
82
83 proxy_ = new PowerMgrProxy(powerManagerSa);
84 if (proxy_ == nullptr) {
85 ACCESSTOKEN_LOG_ERROR(LABEL, "Iface_cast get null");
86 }
87 }
88
OnRemoteDiedHandle()89 void PowerMgrClient::OnRemoteDiedHandle()
90 {
91 std::lock_guard<std::mutex> lock(proxyMutex_);
92 ReleaseProxy();
93 }
94
GetProxy()95 sptr<IPowerMgr> PowerMgrClient::GetProxy()
96 {
97 std::lock_guard<std::mutex> lock(proxyMutex_);
98 if (proxy_ == nullptr) {
99 InitProxy();
100 }
101 return proxy_;
102 }
103
ReleaseProxy()104 void PowerMgrClient::ReleaseProxy()
105 {
106 if (proxy_ != nullptr && serviceDeathObserver_ != nullptr) {
107 proxy_->AsObject()->RemoveDeathRecipient(serviceDeathObserver_);
108 }
109 proxy_ = nullptr;
110 serviceDeathObserver_ = nullptr;
111 }
112
OnRemoteDied(const wptr<IRemoteObject> & object)113 void PowerMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
114 {
115 ACCESSTOKEN_LOG_INFO(LABEL, "OnRemoteDied");
116 PowerMgrClient::GetInstance().OnRemoteDiedHandle();
117 }
118 } // namespace AccessToken
119 } // namespace Security
120 } // namespace OHOS
121
122