1 /*
2 * Copyright (c) 2023 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 "standby_service_client.h"
17
18 #include <message_parcel.h>
19
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22
23 #include "standby_service_log.h"
24 #include "standby_service_proxy.h"
25
26 namespace OHOS {
27 namespace DevStandbyMgr {
StandbyServiceClient()28 StandbyServiceClient::StandbyServiceClient() {}
29
~StandbyServiceClient()30 StandbyServiceClient::~StandbyServiceClient() {}
31
GetInstance()32 StandbyServiceClient& StandbyServiceClient::GetInstance()
33 {
34 static StandbyServiceClient StandbyServiceClient;
35 return StandbyServiceClient;
36 }
37
SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber> & subscriber)38 ErrCode StandbyServiceClient::SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber)
39 {
40 std::lock_guard<std::mutex> lock(mutex_);
41 if (!GetStandbyServiceProxy()) {
42 STANDBYSERVICE_LOGE("get standby service proxy failed");
43 return ERR_STANDBY_SERVICE_NOT_CONNECTED;
44 }
45 if (subscriber == nullptr) {
46 STANDBYSERVICE_LOGE("subscriber is nullptr");
47 return ERR_STANDBY_INVALID_PARAM;
48 }
49 return standbyServiceProxy_->SubscribeStandbyCallback(subscriber);
50 }
51
UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber> & subscriber)52 ErrCode StandbyServiceClient::UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber)
53 {
54 std::lock_guard<std::mutex> lock(mutex_);
55 if (!GetStandbyServiceProxy()) {
56 STANDBYSERVICE_LOGE("get standby service proxy failed");
57 return ERR_STANDBY_SERVICE_NOT_CONNECTED;
58 }
59 if (subscriber == nullptr) {
60 STANDBYSERVICE_LOGE("subscriber is nullptr");
61 return ERR_STANDBY_INVALID_PARAM;
62 }
63 return standbyServiceProxy_->UnsubscribeStandbyCallback(subscriber);
64 }
65
ApplyAllowResource(const sptr<ResourceRequest> & resourceRequest)66 ErrCode StandbyServiceClient::ApplyAllowResource(const sptr<ResourceRequest>& resourceRequest)
67 {
68 std::lock_guard<std::mutex> lock(mutex_);
69 if (!GetStandbyServiceProxy()) {
70 STANDBYSERVICE_LOGE("get standby service proxy failed");
71 return ERR_STANDBY_SERVICE_NOT_CONNECTED;
72 }
73 if (resourceRequest == nullptr) {
74 STANDBYSERVICE_LOGE("resource request is nullptr");
75 return ERR_STANDBY_INVALID_PARAM;
76 }
77 return standbyServiceProxy_->ApplyAllowResource(resourceRequest);
78 }
79
UnapplyAllowResource(const sptr<ResourceRequest> & resourceRequest)80 ErrCode StandbyServiceClient::UnapplyAllowResource(const sptr<ResourceRequest>& resourceRequest)
81 {
82 std::lock_guard<std::mutex> lock(mutex_);
83 if (!GetStandbyServiceProxy()) {
84 STANDBYSERVICE_LOGE("get standby service proxy failed");
85 return ERR_STANDBY_SERVICE_NOT_CONNECTED;
86 }
87 if (resourceRequest == nullptr) {
88 STANDBYSERVICE_LOGE("resource request is nullptr");
89 return ERR_STANDBY_INVALID_PARAM;
90 }
91 return standbyServiceProxy_->UnapplyAllowResource(resourceRequest);
92 }
93
GetAllowList(uint32_t allowType,std::vector<AllowInfo> & allowInfoArray,uint32_t reasonCode)94 ErrCode StandbyServiceClient::GetAllowList(uint32_t allowType, std::vector<AllowInfo>& allowInfoArray,
95 uint32_t reasonCode)
96 {
97 std::lock_guard<std::mutex> lock(mutex_);
98 if (!GetStandbyServiceProxy()) {
99 STANDBYSERVICE_LOGE("get standby service proxy failed");
100 return ERR_STANDBY_SERVICE_NOT_CONNECTED;
101 }
102 if (!allowInfoArray.empty()) {
103 STANDBYSERVICE_LOGW("allow info array is not empty");
104 allowInfoArray.clear();
105 }
106 return standbyServiceProxy_->GetAllowList(allowType, allowInfoArray, reasonCode);
107 }
108
IsDeviceInStandby(bool & isStandby)109 ErrCode StandbyServiceClient::IsDeviceInStandby(bool& isStandby)
110 {
111 std::lock_guard<std::mutex> lock(mutex_);
112 if (!GetStandbyServiceProxy()) {
113 STANDBYSERVICE_LOGE("get standby service proxy failed");
114 return ERR_STANDBY_SERVICE_NOT_CONNECTED;
115 }
116 return standbyServiceProxy_->IsDeviceInStandby(isStandby);
117 }
118
ReportWorkSchedulerStatus(bool started,int32_t uid,const std::string & bundleName)119 ErrCode StandbyServiceClient::ReportWorkSchedulerStatus(bool started, int32_t uid, const std::string& bundleName)
120 {
121 std::lock_guard<std::mutex> lock(mutex_);
122 if (!GetStandbyServiceProxy()) {
123 STANDBYSERVICE_LOGE("get standby service proxy failed");
124 return ERR_STANDBY_SERVICE_NOT_CONNECTED;
125 }
126 return standbyServiceProxy_->ReportWorkSchedulerStatus(started, uid, bundleName);
127 }
128
GetRestrictList(uint32_t restrictType,std::vector<AllowInfo> & restrictInfoList,uint32_t reasonCode)129 ErrCode StandbyServiceClient::GetRestrictList(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList,
130 uint32_t reasonCode)
131 {
132 std::lock_guard<std::mutex> lock(mutex_);
133 if (!GetStandbyServiceProxy()) {
134 STANDBYSERVICE_LOGE("get standby service proxy failed");
135 return ERR_STANDBY_SERVICE_NOT_CONNECTED;
136 }
137 if (!restrictInfoList.empty()) {
138 STANDBYSERVICE_LOGW("restrict info array is not empty");
139 restrictInfoList.clear();
140 }
141 return standbyServiceProxy_->GetRestrictList(restrictType, restrictInfoList, reasonCode);
142 }
143
IsStrategyEnabled(const std::string & strategyName,bool & isEnabled)144 ErrCode StandbyServiceClient::IsStrategyEnabled(const std::string& strategyName, bool& isEnabled)
145 {
146 std::lock_guard<std::mutex> lock(mutex_);
147 if (!GetStandbyServiceProxy()) {
148 STANDBYSERVICE_LOGE("get standby service proxy failed");
149 return ERR_STANDBY_SERVICE_NOT_CONNECTED;
150 }
151 return standbyServiceProxy_->IsStrategyEnabled(strategyName, isEnabled);
152 }
153
ReportDeviceStateChanged(DeviceStateType type,bool enabled)154 ErrCode StandbyServiceClient::ReportDeviceStateChanged(DeviceStateType type, bool enabled)
155 {
156 std::lock_guard<std::mutex> lock(mutex_);
157 STANDBYSERVICE_LOGI("device state changed, state type: %{public}d, enabled: %{public}d",
158 static_cast<int32_t>(type), enabled);
159 if (!GetStandbyServiceProxy()) {
160 STANDBYSERVICE_LOGE("get standby service proxy failed");
161 return ERR_STANDBY_SERVICE_NOT_CONNECTED;
162 }
163 return standbyServiceProxy_->ReportDeviceStateChanged(type, enabled);
164 }
165
GetStandbyServiceProxy()166 bool StandbyServiceClient::GetStandbyServiceProxy()
167 {
168 if (standbyServiceProxy_ != nullptr) {
169 return true;
170 }
171 sptr<ISystemAbilityManager> systemAbilityManager =
172 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
173 if (systemAbilityManager == nullptr) {
174 STANDBYSERVICE_LOGE("get standby service proxy failed");
175 return false;
176 }
177
178 sptr<IRemoteObject> remoteObject =
179 systemAbilityManager->GetSystemAbility(DEVICE_STANDBY_SERVICE_SYSTEM_ABILITY_ID);
180 if (remoteObject == nullptr) {
181 STANDBYSERVICE_LOGE("get standby service system ability failed");
182 return false;
183 }
184
185 standbyServiceProxy_ = iface_cast<IStandbyService>(remoteObject);
186 if ((standbyServiceProxy_ == nullptr) || (standbyServiceProxy_->AsObject() == nullptr)) {
187 STANDBYSERVICE_LOGE("standby service proxy iface_cast from remote Onject failed");
188 return false;
189 }
190
191 deathRecipient_ = new (std::nothrow) StandbyServiceDeathRecipient(*this);
192 if (deathRecipient_ == nullptr) {
193 return false;
194 }
195
196 standbyServiceProxy_->AsObject()->AddDeathRecipient(deathRecipient_);
197 return true;
198 }
199
ResetStandbyServiceClient()200 void StandbyServiceClient::ResetStandbyServiceClient()
201 {
202 std::lock_guard<std::mutex> lock(mutex_);
203 if ((standbyServiceProxy_ != nullptr)&& (standbyServiceProxy_->AsObject() != nullptr)) {
204 standbyServiceProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
205 }
206 standbyServiceProxy_ = nullptr;
207 }
208
StandbyServiceDeathRecipient(StandbyServiceClient & standbyServiceClient)209 StandbyServiceClient::StandbyServiceDeathRecipient::StandbyServiceDeathRecipient(
210 StandbyServiceClient& standbyServiceClient)
211 : standbyServiceClient_(standbyServiceClient) {}
212
~StandbyServiceDeathRecipient()213 StandbyServiceClient::StandbyServiceDeathRecipient::~StandbyServiceDeathRecipient() {}
214
OnRemoteDied(const wptr<IRemoteObject> & remote)215 void StandbyServiceClient::StandbyServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
216 {
217 standbyServiceClient_.ResetStandbyServiceClient();
218 }
219 } // namespace DevStandbyMgr
220 } // namespace OHOS