• 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_CORE_INCLUDE_BACKGROUND_TASK_MGR_SERVICE_H
17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CORE_INCLUDE_BACKGROUND_TASK_MGR_SERVICE_H
18 
19 #include <ctime>
20 #include <list>
21 #include <memory>
22 #include <mutex>
23 
24 #include "event_handler.h"
25 #include "event_runner.h"
26 #include "refbase.h"
27 #include "singleton.h"
28 #include "system_ability.h"
29 #include "system_ability_definition.h"
30 
31 #include "background_task_mgr_stub.h"
32 #include "bg_continuous_task_mgr.h"
33 #include "bg_transient_task_mgr.h"
34 #include "bgtaskmgr_inner_errors.h"
35 #include "bg_efficiency_resources_mgr.h"
36 
37 namespace OHOS {
38 namespace BackgroundTaskMgr {
39 enum class ServiceRunningState {
40     STATE_NOT_START,
41     STATE_RUNNING
42 };
43 
44 class BackgroundTaskMgrService final : public SystemAbility, public BackgroundTaskMgrStub,
45     public std::enable_shared_from_this<BackgroundTaskMgrService> {
46     DISALLOW_COPY_AND_MOVE(BackgroundTaskMgrService);
47     DECLARE_SYSTEM_ABILITY(BackgroundTaskMgrService);
48     DECLARE_DELAYED_SINGLETON(BackgroundTaskMgrService);
49 public:
50     BackgroundTaskMgrService(const int32_t systemAbilityId, bool runOnCreate);
51     void OnStart() final;
52     void OnStop() final;
53 
54     ErrCode RequestSuspendDelay(const std::u16string& reason,
55         const sptr<IExpiredCallback>& callback, std::shared_ptr<DelaySuspendInfo> &delayInfo) override;
56     ErrCode CancelSuspendDelay(int32_t requestId) override;
57     ErrCode GetRemainingDelayTime(int32_t requestId, int32_t &delayTime) override;
58     ErrCode StartBackgroundRunning(const sptr<ContinuousTaskParam> &taskParam) override;
59     ErrCode StopBackgroundRunning(const std::string &abilityName, const sptr<IRemoteObject> &abilityToken) override;
60     ErrCode SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber) override;
61     ErrCode UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber) override;
62     ErrCode GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> &list) override;
63     ErrCode GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list) override;
64     ErrCode ApplyEfficiencyResources(const sptr<EfficiencyResourceInfo> &resourceInfo) override;
65     ErrCode ResetAllEfficiencyResources() override;
66     ErrCode GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> &appList,
67         std::vector<std::shared_ptr<ResourceCallbackInfo>> &procList) override;
68     ErrCode StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType) override;
69     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
70 
71     void ForceCancelSuspendDelay(int32_t requestId);
72     void HandleRequestExpired(const int32_t requestId);
73     void HandleExpiredCallbackDeath(const wptr<IRemoteObject>& remote);
74     void HandleSubscriberDeath(const wptr<IRemoteObject>& remote);
75 
76 private:
77     void Init();
78     void DumpUsage(std::string &result);
79     bool CheckCallingToken();
80     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
81     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
82 
83 private:
84     ServiceRunningState state_ {ServiceRunningState::STATE_NOT_START};
85 };
86 }  // namespace BackgroundTaskMgr
87 }  // namespace OHOS
88 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CORE_INCLUDE_BACKGROUND_TASK_MGR_SERVICE_H