• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 GetAllTransientTasks(int32_t &remainingQuota, std::vector<std::shared_ptr<DelaySuspendInfo>> &list);
66     ErrCode SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber);
67     ErrCode UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber);
68     ErrCode GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> &list);
69     ErrCode PauseTransientTaskTimeForInner(int32_t uid);
70     ErrCode StartTransientTaskTimeForInner(int32_t uid);
71     ErrCode SetBgTaskConfig(const std::string &configData, int32_t sourceType);
72     ErrCode ShellDump(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
73 
74     void ForceCancelSuspendDelay(int32_t requestId);
75     void HandleExpiredCallbackDeath(const wptr<IRemoteObject>& remote);
76     void HandleSubscriberDeath(const wptr<IRemoteObject>& remote);
77     void HandleRequestExpired(const int32_t requestId);
78     void HandleTransientTaskSuscriberTask(const shared_ptr<TransientTaskAppInfo>& appInfo,
79         const TransientTaskEventType type);
80     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId);
81     void HandleSuspendManagerDie();
82     void OnAppCacheStateChanged(int32_t uid, int32_t pid, const std::string &bundleName);
83     std::set<int32_t>& GetTransientPauseUid();
84 
85 private:
86     ErrCode IsCallingInfoLegal(int32_t uid, int32_t pid, std::string &name,
87         const sptr<IExpiredCallback>& callback);
88     bool GetBundleNamesForUid(int32_t uid, std::string &bundleName);
89     bool VerifyCallingInfo(int32_t uid, int32_t pid);
90     bool VerifyRequestIdLocked(const std::string& name, int32_t uid, int32_t requestId);
91     bool CheckProcessName();
92     ErrCode CancelSuspendDelayLocked(int32_t requestId);
93     void NotifyTransientTaskSuscriber(const shared_ptr<TransientTaskAppInfo>& appInfo,
94         const TransientTaskEventType type);
95     bool DumpAllRequestId(std::vector<std::string> &dumpInfo);
96     void DumpTaskTime(const std::vector<std::string> &dumpOption, bool pause, std::vector<std::string> &dumpInfo);
97     void SendLowBatteryEvent(std::vector<std::string> &dumpInfo);
98     void SendOkayBatteryEvent(std::vector<std::string> &dumpInfo);
99     void InitNecessaryState(const std::shared_ptr<AppExecFwk::EventRunner>& runner);
100 
101     std::atomic<bool> isReady_ {false};
102     std::mutex suscriberLock_;
103     sptr<SubscriberDeathRecipient> susriberDeathRecipient_ {nullptr};
104     std::mutex expiredCallbackLock_;
105     std::map<int32_t, sptr<IExpiredCallback>> expiredCallbackMap_;
106     std::map<int32_t, std::shared_ptr<KeyInfo>> keyInfoMap_;
107     sptr<ExpiredCallbackDeathRecipient> callbackDeathRecipient_ {nullptr};
108     std::list<sptr<IBackgroundTaskSubscriber>> subscriberList_;
109 
110     std::shared_ptr<TimerManager> timerManager_ {nullptr};
111     std::shared_ptr<Watchdog> watchdog_ {nullptr};
112     std::shared_ptr<InputManager> inputManager_ {nullptr};
113     std::shared_ptr<DeviceInfoManager> deviceInfoManeger_ {nullptr};
114     std::shared_ptr<DecisionMaker> decisionMaker_ {nullptr};
115     std::shared_ptr<AppExecFwk::EventHandler> handler_;
116     std::mutex transientUidLock_;
117     std::set<int32_t> transientPauseUid_ {};
118 };
119 
120 class SubscriberDeathRecipient final : public IRemoteObject::DeathRecipient {
121 public:
122     explicit SubscriberDeathRecipient(const wptr<BackgroundTaskMgrService>& service);
123     ~SubscriberDeathRecipient() override;
124     DISALLOW_COPY_AND_MOVE(SubscriberDeathRecipient);
125     void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
126 
127 private:
128     wptr<BackgroundTaskMgrService> service_;
129 };
130 
131 class ExpiredCallbackDeathRecipient final : public IRemoteObject::DeathRecipient {
132 public:
133     explicit ExpiredCallbackDeathRecipient(const wptr<BackgroundTaskMgrService>& service);
134     ~ExpiredCallbackDeathRecipient() override;
135     DISALLOW_COPY_AND_MOVE(ExpiredCallbackDeathRecipient);
136     void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
137 
138 private:
139     wptr<BackgroundTaskMgrService> service_;
140 };
141 }  // namespace BackgroundTaskMgr
142 }  // namespace OHOS
143 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_BG_TRANSIENT_TASK_MGR_H