1 /* 2 * Copyright (c) 2022 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 "background_task_subscriber.h" 17 18 #include "iservice_registry.h" 19 #include "system_ability_definition.h" 20 21 namespace OHOS { 22 namespace BackgroundTaskMgr { BackgroundTaskSubscriber()23BackgroundTaskSubscriber::BackgroundTaskSubscriber() 24 { 25 impl_ = new (std::nothrow) BackgroundTaskSubscriberImpl(*this); 26 }; 27 ~BackgroundTaskSubscriber()28BackgroundTaskSubscriber::~BackgroundTaskSubscriber() {} 29 OnConnected()30void BackgroundTaskSubscriber::OnConnected() {} 31 OnDisconnected()32void BackgroundTaskSubscriber::OnDisconnected() {} 33 OnTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo> & info)34void BackgroundTaskSubscriber::OnTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo>& info) {} 35 OnTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo> & info)36void BackgroundTaskSubscriber::OnTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo>& info) {} 37 OnAppTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo> & info)38void BackgroundTaskSubscriber::OnAppTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo>& info) {} 39 OnAppTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo> & info)40void BackgroundTaskSubscriber::OnAppTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo>& info) {} 41 OnContinuousTaskStart(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)42void BackgroundTaskSubscriber::OnContinuousTaskStart( 43 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) {} 44 OnContinuousTaskStop(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)45void BackgroundTaskSubscriber::OnContinuousTaskStop( 46 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) {} 47 OnAppContinuousTaskStop(int32_t uid)48void BackgroundTaskSubscriber::OnAppContinuousTaskStop(int32_t uid) {} 49 OnRemoteDied(const wptr<IRemoteObject> & object)50void BackgroundTaskSubscriber::OnRemoteDied(const wptr<IRemoteObject> &object) {} 51 OnProcEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)52void BackgroundTaskSubscriber::OnProcEfficiencyResourcesApply( 53 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) {} 54 OnProcEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)55void BackgroundTaskSubscriber::OnProcEfficiencyResourcesReset( 56 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) {} 57 OnAppEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)58void BackgroundTaskSubscriber::OnAppEfficiencyResourcesApply( 59 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) {} 60 OnAppEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)61void BackgroundTaskSubscriber::OnAppEfficiencyResourcesReset( 62 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) {} 63 GetImpl() const64const sptr<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl> BackgroundTaskSubscriber::GetImpl() const 65 { 66 return impl_; 67 } 68 BackgroundTaskSubscriberImpl(BackgroundTaskSubscriber & subscriber)69BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::BackgroundTaskSubscriberImpl( 70 BackgroundTaskSubscriber &subscriber) : subscriber_(subscriber) 71 { 72 recipient_ = new (std::nothrow) DeathRecipient(*this); 73 } 74 OnConnected()75void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnConnected() 76 { 77 if (GetBackgroundTaskMgrProxy() && recipient_ != nullptr) { 78 proxy_->AsObject()->AddDeathRecipient(recipient_); 79 } 80 subscriber_.OnConnected(); 81 } 82 OnDisconnected()83void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnDisconnected() 84 { 85 if (GetBackgroundTaskMgrProxy() && recipient_ != nullptr) { 86 proxy_->AsObject()->RemoveDeathRecipient(recipient_); 87 } 88 subscriber_.OnDisconnected(); 89 } 90 OnAppEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)91void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnAppEfficiencyResourcesApply( 92 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) 93 { 94 subscriber_.OnAppEfficiencyResourcesApply(resourceInfo); 95 } 96 OnAppEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)97void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnAppEfficiencyResourcesReset( 98 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) 99 { 100 subscriber_.OnAppEfficiencyResourcesReset(resourceInfo); 101 } 102 OnProcEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)103void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnProcEfficiencyResourcesApply( 104 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) 105 { 106 subscriber_.OnProcEfficiencyResourcesApply(resourceInfo); 107 } 108 OnProcEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)109void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnProcEfficiencyResourcesReset( 110 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) 111 { 112 subscriber_.OnProcEfficiencyResourcesReset(resourceInfo); 113 } 114 OnTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo> & info)115void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnTransientTaskStart( 116 const std::shared_ptr<TransientTaskAppInfo>& info) 117 { 118 subscriber_.OnTransientTaskStart(info); 119 } 120 OnTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo> & info)121void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnTransientTaskEnd( 122 const std::shared_ptr<TransientTaskAppInfo>& info) 123 { 124 subscriber_.OnTransientTaskEnd(info); 125 } 126 OnAppTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo> & info)127void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnAppTransientTaskStart( 128 const std::shared_ptr<TransientTaskAppInfo>& info) 129 { 130 subscriber_.OnAppTransientTaskStart(info); 131 } 132 OnAppTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo> & info)133void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnAppTransientTaskEnd( 134 const std::shared_ptr<TransientTaskAppInfo>& info) 135 { 136 subscriber_.OnAppTransientTaskEnd(info); 137 } 138 OnContinuousTaskStart(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)139void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnContinuousTaskStart( 140 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) 141 { 142 subscriber_.OnContinuousTaskStart(continuousTaskCallbackInfo); 143 } 144 OnContinuousTaskStop(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)145void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnContinuousTaskStop( 146 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) 147 { 148 subscriber_.OnContinuousTaskStop(continuousTaskCallbackInfo); 149 } 150 OnAppContinuousTaskStop(int32_t uid)151void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnAppContinuousTaskStop(int32_t uid) 152 { 153 subscriber_.OnAppContinuousTaskStop(uid); 154 } 155 GetBackgroundTaskMgrProxy()156bool BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::GetBackgroundTaskMgrProxy() 157 { 158 if (proxy_) { 159 return true; 160 } 161 std::lock_guard<std::mutex> lock(mutex_); 162 sptr<ISystemAbilityManager> systemAbilityManager = 163 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 164 if (!systemAbilityManager) { 165 return false; 166 } 167 168 sptr<IRemoteObject> remoteObject = 169 systemAbilityManager->GetSystemAbility(BACKGROUND_TASK_MANAGER_SERVICE_ID); 170 if (!remoteObject) { 171 return false; 172 } 173 174 proxy_ = iface_cast<IBackgroundTaskMgr>(remoteObject); 175 if ((!proxy_) || (proxy_->AsObject() == nullptr)) { 176 return false; 177 } 178 return true; 179 } 180 DeathRecipient(BackgroundTaskSubscriberImpl & subscriberImpl)181BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::DeathRecipient::DeathRecipient( 182 BackgroundTaskSubscriberImpl &subscriberImpl) : subscriberImpl_(subscriberImpl) {} 183 ~DeathRecipient()184BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::DeathRecipient::~DeathRecipient() {} 185 OnRemoteDied(const wptr<IRemoteObject> & object)186void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::DeathRecipient::OnRemoteDied( 187 const wptr<IRemoteObject> &object) 188 { 189 subscriberImpl_.proxy_ = nullptr; 190 subscriberImpl_.subscriber_.OnRemoteDied(object); 191 } 192 } // namespace BackgroundTaskMgr 193 } // namespace OHOS