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