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 OnContinuousTaskStart(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)38void BackgroundTaskSubscriber::OnContinuousTaskStart( 39 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) {} 40 OnContinuousTaskStop(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)41void BackgroundTaskSubscriber::OnContinuousTaskStop( 42 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) {} 43 OnRemoteDied(const wptr<IRemoteObject> & object)44void BackgroundTaskSubscriber::OnRemoteDied(const wptr<IRemoteObject> &object) {} 45 GetImpl() const46const sptr<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl> BackgroundTaskSubscriber::GetImpl() const 47 { 48 return impl_; 49 } 50 BackgroundTaskSubscriberImpl(BackgroundTaskSubscriber & subscriber)51BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::BackgroundTaskSubscriberImpl( 52 BackgroundTaskSubscriber &subscriber) : subscriber_(subscriber) 53 { 54 recipient_ = new (std::nothrow) DeathRecipient(*this); 55 } 56 OnConnected()57void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnConnected() 58 { 59 if (GetBackgroundTaskMgrProxy()) { 60 proxy_->AsObject()->AddDeathRecipient(recipient_); 61 } 62 subscriber_.OnConnected(); 63 } 64 OnDisconnected()65void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnDisconnected() 66 { 67 if (GetBackgroundTaskMgrProxy()) { 68 proxy_->AsObject()->RemoveDeathRecipient(recipient_); 69 } 70 subscriber_.OnDisconnected(); 71 } 72 OnTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo> & info)73void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnTransientTaskStart( 74 const std::shared_ptr<TransientTaskAppInfo>& info) 75 { 76 subscriber_.OnTransientTaskStart(info); 77 } 78 OnTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo> & info)79void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnTransientTaskEnd( 80 const std::shared_ptr<TransientTaskAppInfo>& info) 81 { 82 subscriber_.OnTransientTaskEnd(info); 83 } 84 OnContinuousTaskStart(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)85void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnContinuousTaskStart( 86 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) 87 { 88 subscriber_.OnContinuousTaskStart(continuousTaskCallbackInfo); 89 } 90 OnContinuousTaskStop(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)91void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::OnContinuousTaskStop( 92 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) 93 { 94 subscriber_.OnContinuousTaskStop(continuousTaskCallbackInfo); 95 } 96 GetBackgroundTaskMgrProxy()97bool BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::GetBackgroundTaskMgrProxy() 98 { 99 if (proxy_) { 100 return true; 101 } 102 std::lock_guard<std::mutex> lock(mutex_); 103 sptr<ISystemAbilityManager> systemAbilityManager = 104 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 105 if (!systemAbilityManager) { 106 return false; 107 } 108 109 sptr<IRemoteObject> remoteObject = 110 systemAbilityManager->GetSystemAbility(BACKGROUND_TASK_MANAGER_SERVICE_ID); 111 if (!remoteObject) { 112 return false; 113 } 114 115 proxy_ = iface_cast<IBackgroundTaskMgr>(remoteObject); 116 if ((!proxy_) || (proxy_->AsObject() == nullptr)) { 117 return false; 118 } 119 return true; 120 } 121 DeathRecipient(BackgroundTaskSubscriberImpl & subscriberImpl)122BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::DeathRecipient::DeathRecipient( 123 BackgroundTaskSubscriberImpl &subscriberImpl) : subscriberImpl_(subscriberImpl) {} 124 ~DeathRecipient()125BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::DeathRecipient::~DeathRecipient() {} 126 OnRemoteDied(const wptr<IRemoteObject> & object)127void BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl::DeathRecipient::OnRemoteDied( 128 const wptr<IRemoteObject> &object) 129 { 130 subscriberImpl_.proxy_ = nullptr; 131 subscriberImpl_.subscriber_.OnRemoteDied(object); 132 } 133 } // namespace BackgroundTaskMgr 134 } // namespace OHOS