• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CONTINUOUS_TASK_INCLUDE_BG_CONTINUOUS_TASK_MGR_H
17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CONTINUOUS_TASK_INCLUDE_BG_CONTINUOUS_TASK_MGR_H
18 
19 #include <memory>
20 #include <mutex>
21 
22 #include "ipc_skeleton.h"
23 #include "iremote_object.h"
24 #include "resource_manager.h"
25 #include "singleton.h"
26 
27 #include "app_state_observer.h"
28 #include "bgtaskmgr_inner_errors.h"
29 #include "bundle_info.h"
30 #include "continuous_task_callback_info.h"
31 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE
32 #include "task_notification_subscriber.h"
33 #endif
34 #include "continuous_task_param.h"
35 #include "continuous_task_record.h"
36 #include "ibackground_task_subscriber.h"
37 #include "remote_death_recipient.h"
38 #include "system_event_observer.h"
39 #include "config_change_observer.h"
40 
41 namespace OHOS {
42 namespace BackgroundTaskMgr {
43 class BackgroundTaskMgrService;
44 class DataStorageHelper;
45 
46 enum class ContinuousTaskEventTriggerType: uint32_t {
47     TASK_START,
48     TASK_CANCEL,
49 };
50 
51 struct CachedBundleInfo {
52     std::unordered_map<std::string, uint32_t> abilityBgMode_ {};
53     std::string appName_ {""};
54 };
55 
56 class BgContinuousTaskMgr : public DelayedSingleton<BgContinuousTaskMgr>,
57                             public std::enable_shared_from_this<BgContinuousTaskMgr> {
58 public:
59     ErrCode StartBackgroundRunning(const sptr<ContinuousTaskParam> &taskParam);
60     ErrCode StopBackgroundRunning(const std::string &abilityName);
61     ErrCode AddSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber);
62     ErrCode RemoveSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber);
63     ErrCode ShellDump(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
64     ErrCode GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list);
65     bool StopContinuousTaskByUser(const std::string &mapKey);
66     void OnAccountsStateChanged(int32_t id);
67     void OnBundleInfoChanged(const std::string &action, const std::string &bundleName, int32_t uid);
68     void OnAbilityStateChanged(int32_t uid, const std::string &abilityName);
69     void OnProcessDied(int32_t uid, int32_t pid);
70     void OnRemoteSubscriberDied(const wptr<IRemoteObject> &object);
71     bool Init();
72     void InitNecessaryState();
73     void InitRequiredResourceInfo();
74     void Clear();
75     int32_t GetBgTaskUid();
76     void StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType);
77     void OnConfigurationChanged(const AppExecFwk::Configuration &configuration);
78 
79 private:
80     ErrCode StartBackgroundRunningInner(std::shared_ptr<ContinuousTaskRecord> &continuousTaskRecordPtr);
81     ErrCode StopBackgroundRunningInner(int32_t uid, const std::string &abilityName);
82     ErrCode AddSubscriberInner(const sptr<IBackgroundTaskSubscriber> &subscriber);
83     ErrCode RemoveSubscriberInner(const sptr<IBackgroundTaskSubscriber> &subscriber);
84     ErrCode ShellDumpInner(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
85     ErrCode SendContinuousTaskNotification(std::shared_ptr<ContinuousTaskRecord> &ContinuousTaskRecordPtr);
86     ErrCode GetContinuousTaskAppsInner(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list);
87     void HandlePersistenceData();
88     void CheckPersistenceData(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses,
89         const std::set<std::string> &allLabels);
90     void DumpAllTaskInfo(std::vector<std::string> &dumpInfo);
91     void DumpCancelTask(const std::vector<std::string> &dumpOption, bool cleanAll);
92     bool RemoveContinuousTaskRecord(const std::string &mapKey);
93     bool AddAppNameInfos(const AppExecFwk::BundleInfo &bundleInfo, CachedBundleInfo &cachedBundleInfo);
94     uint32_t GetBackgroundModeInfo(int32_t uid, const std::string &abilityName);
95     bool AddAbilityBgModeInfos(const AppExecFwk::BundleInfo &bundleInfo, CachedBundleInfo &cachedBundleInfo);
96     bool RegisterNotificationSubscriber();
97     bool RegisterSysCommEventListener();
98     bool RegisterAppStateObserver();
99     bool RegisterConfigurationObserver();
100     bool GetNotificationPrompt();
101     bool SetCachedBundleInfo(int32_t uid, int32_t userId, const std::string &bundleName, const std::string &appName);
102     void HandleStopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType);
103     void RemoveSpecifiedBgTask(uint32_t taskType);
104     void OnRemoteSubscriberDiedInner(const wptr<IRemoteObject> &object);
105     void OnContinuousTaskChanged(const std::shared_ptr<ContinuousTaskRecord> continuousTaskInfo,
106         ContinuousTaskEventTriggerType changeEventType);
107     ErrCode CheckBgmodeType(uint32_t configuredBgMode, uint32_t requestedBgModeId, bool isNewApi, int32_t uid);
108     int32_t RefreshTaskRecord();
109     void HandleAppContinuousTaskStop(int32_t uid);
110     bool checkPidCondition(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses, int32_t pid);
111     bool checkNotificationCondition(const std::set<std::string> &notificationLabels, const std::string &label);
112     std::shared_ptr<Global::Resource::ResourceManager> GetBundleResMgr(const AppExecFwk::BundleInfo &bundleInfo);
113     std::string GetMainAbilityLabel(const std::string &bundleName, int32_t userId);
114 
115 private:
116     std::atomic<bool> isSysReady_ {false};
117     std::string deviceType_ {""};
118     int32_t bgTaskUid_ {-1};
119     std::shared_ptr<AppExecFwk::EventRunner> runner_ {nullptr};
120     std::shared_ptr<AppExecFwk::EventHandler> handler_ {nullptr};
121     std::unordered_map<std::string, std::shared_ptr<ContinuousTaskRecord>> continuousTaskInfosMap_ {};
122 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE
123     std::shared_ptr<TaskNotificationSubscriber> subscriber_ {nullptr};
124 #endif
125     std::shared_ptr<SystemEventObserver> systemEventListener_ {nullptr};
126     std::shared_ptr<AppStateObserver> appStateObserver_ {nullptr};
127     sptr<AppExecFwk::IConfigurationObserver> configChangeObserver_ {nullptr};
128     std::list<sptr<IBackgroundTaskSubscriber>> bgTaskSubscribers_ {};
129     std::map<sptr<IRemoteObject>, sptr<RemoteDeathRecipient>> subscriberRecipients_ {};
130     std::unordered_map<int32_t, CachedBundleInfo> cachedBundleInfos_ {};
131     std::vector<std::string> continuousTaskText_ {};
132 
133     DECLARE_DELAYED_SINGLETON(BgContinuousTaskMgr);
134 };
135 }  // namespace BackgroundTaskMgr
136 }  // namespace OHOS
137 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CONTINUOUS_TASK_INCLUDE_BG_CONTINUOUS_TASK_MGR_H