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 #ifndef FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_INNERKITS_INCLUDE_BACKGROUND_TASK_SUBSCRIBER_H 17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_INNERKITS_INCLUDE_BACKGROUND_TASK_SUBSCRIBER_H 18 19 #include <iremote_broker.h> 20 21 #include "ibackground_task_mgr.h" 22 #include "background_task_subscriber_stub.h" 23 24 namespace OHOS { 25 namespace BackgroundTaskMgr { 26 class BackgroundTaskSubscriber { 27 public: 28 /** 29 * Default constructor used to create a instance. 30 */ 31 BackgroundTaskSubscriber(); 32 33 /** 34 * Default destructor. 35 */ 36 virtual ~BackgroundTaskSubscriber(); 37 38 /** 39 * Called back when the subscriber is connected to Background Task Manager Service. 40 */ 41 virtual void OnConnected(); 42 43 /** 44 * Called back when the subscriber is disconnected from Background Task Manager Service. 45 */ 46 virtual void OnDisconnected(); 47 48 /** 49 * Called back when a transient task start. 50 * 51 * @param info Transient task app info. 52 **/ 53 virtual void OnTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo>& info); 54 55 /** 56 * Called back when a transient task end. 57 * 58 * @param info Transient task app info. 59 **/ 60 virtual void OnTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo>& info); 61 62 /** 63 * Called back when a continuous task start. 64 * 65 * @param info Transient task app info. 66 **/ 67 virtual void OnContinuousTaskStart(const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo); 68 69 /** 70 * Called back when a continuous task end. 71 * 72 * @param info Transient task app info. 73 **/ 74 virtual void OnContinuousTaskStop(const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo); 75 76 /** 77 * Called back when the Background Task Manager Service has died. 78 */ 79 virtual void OnRemoteDied(const wptr<IRemoteObject> &object); 80 81 private: 82 class BackgroundTaskSubscriberImpl final : public BackgroundTaskSubscriberStub { 83 public: 84 class DeathRecipient final : public IRemoteObject::DeathRecipient { 85 public: 86 DeathRecipient(BackgroundTaskSubscriberImpl &subscriberImpl); 87 88 ~DeathRecipient(); 89 90 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 91 92 private: 93 BackgroundTaskSubscriberImpl &subscriberImpl_; 94 }; 95 96 public: 97 BackgroundTaskSubscriberImpl(BackgroundTaskSubscriber &subscriber); ~BackgroundTaskSubscriberImpl()98 ~BackgroundTaskSubscriberImpl() {}; 99 void OnConnected() override; 100 void OnDisconnected() override; 101 void OnTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo>& info) override; 102 void OnTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo>& info) override; 103 void OnContinuousTaskStart( 104 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) override; 105 void OnContinuousTaskStop( 106 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) override; 107 bool GetBackgroundTaskMgrProxy(); 108 109 public: 110 BackgroundTaskSubscriber &subscriber_; 111 sptr<DeathRecipient> recipient_ {nullptr}; 112 sptr<IBackgroundTaskMgr> proxy_ {nullptr}; 113 std::mutex mutex_ {}; 114 }; 115 116 private: 117 const sptr<BackgroundTaskSubscriberImpl> GetImpl() const; 118 119 private: 120 sptr<BackgroundTaskSubscriberImpl> impl_ {nullptr}; 121 122 friend class BackgroundTaskManager; 123 }; 124 } // namespace BackgroundTaskMgr 125 } // namespace OHOS 126 #endif // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_INNERKITS_INCLUDE_BACKGROUND_TASK_SUBSCRIBER_H