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_SERVICES_TRANSIENT_TASK_INCLUDE_BG_TRANSIENT_TASK_MGR_H 17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_BG_TRANSIENT_TASK_MGR_H 18 19 #include <functional> 20 #include <mutex> 21 #include <memory> 22 #include <set> 23 24 #include <iremote_object.h> 25 #include <nocopyable.h> 26 #include <singleton.h> 27 #include <string_ex.h> 28 #include <system_ability.h> 29 30 #include "background_task_mgr_stub.h" 31 #include "decision_maker.h" 32 #include "delay_suspend_info.h" 33 #include "device_info_manager.h" 34 #include "event_handler.h" 35 #include "event_info.h" 36 #include "ibackground_task_mgr.h" 37 #include "iexpired_callback.h" 38 #include "ibackground_task_subscriber.h" 39 #include "timer_manager.h" 40 #include "transient_task_app_info.h" 41 #include "watchdog.h" 42 43 namespace OHOS { 44 namespace BackgroundTaskMgr { 45 class SubscriberDeathRecipient; 46 class ExpiredCallbackDeathRecipient; 47 48 enum class TransientTaskEventType: uint32_t { 49 TASK_START, 50 TASK_END, 51 APP_TASK_START, 52 APP_TASK_END, 53 }; 54 class BgTransientTaskMgr { 55 DECLARE_DELAYED_SINGLETON(BgTransientTaskMgr); 56 57 public: 58 void Init(); 59 ErrCode RequestSuspendDelay(const std::u16string& reason, 60 const sptr<IExpiredCallback>& callback, std::shared_ptr<DelaySuspendInfo> &delayInfo); 61 ErrCode CancelSuspendDelay(int32_t requestId); 62 ErrCode GetRemainingDelayTime(int32_t requestId, int32_t &delayTime); 63 ErrCode SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber); 64 ErrCode UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber); 65 ErrCode GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> &list); 66 ErrCode ShellDump(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo); 67 68 void ForceCancelSuspendDelay(int32_t requestId); 69 void HandleExpiredCallbackDeath(const wptr<IRemoteObject>& remote); 70 void HandleSubscriberDeath(const wptr<IRemoteObject>& remote); 71 void HandleRequestExpired(const int32_t requestId); 72 void HandleTransientTaskSuscriberTask(const shared_ptr<TransientTaskAppInfo>& appInfo, 73 const TransientTaskEventType type); 74 75 private: 76 ErrCode IsCallingInfoLegal(int32_t uid, int32_t pid, std::string &name, 77 const sptr<IExpiredCallback>& callback); 78 bool GetBundleNamesForUid(int32_t uid, std::string &bundleName); 79 bool VerifyCallingInfo(int32_t uid, int32_t pid); 80 bool VerifyRequestIdLocked(const std::string& name, int32_t uid, int32_t requestId); 81 ErrCode CancelSuspendDelayLocked(int32_t requestId); 82 void NotifyTransientTaskSuscriber(const shared_ptr<TransientTaskAppInfo>& appInfo, 83 const TransientTaskEventType type); 84 bool DumpAllRequestId(std::vector<std::string> &dumpInfo); 85 void SendLowBatteryEvent(std::vector<std::string> &dumpInfo); 86 void SendOkayBatteryEvent(std::vector<std::string> &dumpInfo); 87 void InitNecessaryState(); 88 89 std::atomic<bool> isReady_ {false}; 90 std::mutex suscriberLock_; 91 sptr<SubscriberDeathRecipient> susriberDeathRecipient_ {nullptr}; 92 std::mutex expiredCallbackLock_; 93 std::map<int32_t, sptr<IExpiredCallback>> expiredCallbackMap_; 94 std::map<int32_t, std::shared_ptr<KeyInfo>> keyInfoMap_; 95 sptr<ExpiredCallbackDeathRecipient> callbackDeathRecipient_ {nullptr}; 96 std::list<sptr<IBackgroundTaskSubscriber>> subscriberList_; 97 98 std::shared_ptr<TimerManager> timerManager_ {nullptr}; 99 std::shared_ptr<Watchdog> watchdog_ {nullptr}; 100 std::shared_ptr<InputManager> inputManager_ {nullptr}; 101 std::shared_ptr<DeviceInfoManager> deviceInfoManeger_ {nullptr}; 102 std::shared_ptr<DecisionMaker> decisionMaker_ {nullptr}; 103 std::shared_ptr<AppExecFwk::EventRunner> runner_; 104 std::shared_ptr<AppExecFwk::EventHandler> handler_; 105 }; 106 107 class SubscriberDeathRecipient final : public IRemoteObject::DeathRecipient { 108 public: 109 explicit SubscriberDeathRecipient(const wptr<BackgroundTaskMgrService>& service); 110 ~SubscriberDeathRecipient() override; 111 DISALLOW_COPY_AND_MOVE(SubscriberDeathRecipient); 112 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 113 114 private: 115 wptr<BackgroundTaskMgrService> service_; 116 }; 117 118 class ExpiredCallbackDeathRecipient final : public IRemoteObject::DeathRecipient { 119 public: 120 explicit ExpiredCallbackDeathRecipient(const wptr<BackgroundTaskMgrService>& service); 121 ~ExpiredCallbackDeathRecipient() override; 122 DISALLOW_COPY_AND_MOVE(ExpiredCallbackDeathRecipient); 123 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 124 125 private: 126 wptr<BackgroundTaskMgrService> service_; 127 }; 128 } // namespace BackgroundTaskMgr 129 } // namespace OHOS 130 #endif // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_BG_TRANSIENT_TASK_MGR_H