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 the app has transient task. 64 * 65 * @param info App info of transient task. 66 **/ 67 virtual void OnAppTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo>& info); 68 69 /** 70 * Called back when the app does not have transient task. 71 * 72 * @param info App info transient task . 73 **/ 74 virtual void OnAppTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo>& info); 75 76 /** 77 * Called back when a continuous task start. 78 * 79 * @param info Transient task app info. 80 **/ 81 virtual void OnContinuousTaskStart(const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo); 82 83 /** 84 * Called back when a continuous task end. 85 * 86 * @param info Transient task app info. 87 **/ 88 virtual void OnContinuousTaskStop(const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo); 89 90 /** 91 * Called back when the app does not have continuous task. 92 * 93 * @param uid App uid. 94 **/ 95 virtual void OnAppContinuousTaskStop(int32_t uid); 96 97 /** 98 * Called back when the Background Task Manager Service has died. 99 */ 100 virtual void OnRemoteDied(const wptr<IRemoteObject> &object); 101 102 /** 103 * @brief Apply or unapply efficiency resources of App. 104 * 105 * @param resourceInfo Request params. 106 */ 107 virtual void OnAppEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> &resourceInfo); 108 109 /** 110 * @brief Called back when the efficiency resources of App reset. 111 * 112 * @param resourceInfo Request params. 113 */ 114 virtual void OnAppEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> &resourceInfo); 115 116 /** 117 * @brief Apply or unapply efficiency resources of process. 118 * 119 * @param resourceInfo Request params. 120 */ 121 virtual void OnProcEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> &resourceInfo); 122 123 /** 124 * @brief Called back when the efficiency resources of process reset. 125 * 126 * @param resourceInfo Request params. 127 */ 128 virtual void OnProcEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> &resourceInfo); 129 130 private: 131 class BackgroundTaskSubscriberImpl final : public BackgroundTaskSubscriberStub { 132 public: 133 class DeathRecipient final : public IRemoteObject::DeathRecipient { 134 public: 135 explicit DeathRecipient(BackgroundTaskSubscriberImpl &subscriberImpl); 136 137 ~DeathRecipient() override; 138 139 /** 140 * @brief Called back when remote object has died. 141 * 142 * @param object Obiect which has died. 143 */ 144 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 145 146 private: 147 BackgroundTaskSubscriberImpl &subscriberImpl_; 148 }; 149 150 public: 151 explicit BackgroundTaskSubscriberImpl(BackgroundTaskSubscriber &subscriber); ~BackgroundTaskSubscriberImpl()152 ~BackgroundTaskSubscriberImpl() {} 153 154 /** 155 * @brief Called back when the subscriber is connected to Background Task Manager Service. 156 */ 157 void OnConnected() override; 158 159 /** 160 * @brief Called back when the subscriber is disconnected from Background Task Manager Service. 161 */ 162 void OnDisconnected() override; 163 164 /** 165 * @brief Called back when a transient task start. 166 * 167 * @param info Transient task app info. 168 */ 169 void OnTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo>& info) override; 170 171 /** 172 * Called back when the app has transient task. 173 * 174 * @param info App info of transient task. 175 **/ 176 void OnAppTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo>& info) override; 177 178 /** 179 * Called back when the app does not have transient task. 180 * 181 * @param info App info transient task . 182 **/ 183 void OnAppTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo>& info) override; 184 185 /** 186 * @brief Called back when a transient task end. 187 * 188 * @param info Transient task app info. 189 */ 190 void OnTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo>& info) override; 191 192 /** 193 * @brief Called back when a continuous task start. 194 * 195 * @param continuousTaskCallbackInfo Continuous task app info. 196 */ 197 void OnContinuousTaskStart( 198 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) override; 199 200 /** 201 * @brief Called back when a continuous task stop. 202 * 203 * @param continuousTaskCallbackInfo Continuous task app info. 204 */ 205 void OnContinuousTaskStop( 206 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) override; 207 208 /** 209 * Called back when the app does not have continuous task. 210 * 211 * @param uid App uid. 212 */ 213 void OnAppContinuousTaskStop(int32_t uid) override; 214 215 /** 216 * @brief Get managed proxy of background tasks. 217 * 218 * @return True if success, else false. 219 */ 220 bool GetBackgroundTaskMgrProxy(); 221 222 /** 223 * @brief Apply or unapply efficiency resources of App. 224 * 225 * @param resourceInfo Request params. 226 */ 227 void OnAppEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) override; 228 229 /** 230 * @brief Called back when the efficiency resources of App reset. 231 * 232 * @param resourceInfo Request params. 233 */ 234 void OnAppEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) override; 235 236 /** 237 * @brief Apply or unapply efficiency resources of process. 238 * 239 * @param resourceInfo Request params. 240 */ 241 void OnProcEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) override; 242 243 /** 244 * @brief Called back when the efficiency resources of process reset. 245 * 246 * @param resourceInfo Request params. 247 */ 248 void OnProcEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) override; 249 250 public: 251 BackgroundTaskSubscriber &subscriber_; 252 sptr<DeathRecipient> recipient_ {nullptr}; 253 sptr<IBackgroundTaskMgr> proxy_ {nullptr}; 254 std::mutex mutex_ {}; 255 }; 256 257 private: 258 const sptr<BackgroundTaskSubscriberImpl> GetImpl() const; 259 260 private: 261 sptr<BackgroundTaskSubscriberImpl> impl_ {nullptr}; 262 263 friend class BackgroundTaskManager; 264 }; 265 } // namespace BackgroundTaskMgr 266 } // namespace OHOS 267 #endif // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_INNERKITS_INCLUDE_BACKGROUND_TASK_SUBSCRIBER_H