1 /*
2  * Copyright (c) 2022-2024 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 OHOS_FILEMGMT_BACKUP_SCHED_SCHEDULER_H
17 #define OHOS_FILEMGMT_BACKUP_SCHED_SCHEDULER_H
18 
19 #include <cstdint>
20 #include <refbase.h>
21 #include <set>
22 #include <shared_mutex>
23 #include <string>
24 #include <vector>
25 
26 #include "b_resources/b_constants.h"
27 #include "iremote_broker.h"
28 #include "thread_pool.h"
29 #include "timer.h"
30 
31 namespace OHOS::FileManagement::Backup {
32 class Service;
33 class SvcSessionManager;
34 
35 class SchedScheduler final : public virtual RefBase {
36 public:
37     /**
38      * @brief 给threadPool下发任务
39      *
40      */
41     void Sched(std::string bundleName = "");
42 
43     /**
44      * @brief 执行队列任务
45      *
46      * @param bundleName
47      */
48     void ExecutingQueueTasks(const std::string &bundleName);
49 
50     /**
51      * @brief 移除定时器信息
52      *
53      * @param bundleName 应用名称
54      */
55     void RemoveExtConn(const std::string &bundleName);
56 
57     /**
58      * @brief try unload service timer
59      *
60      */
61     void TryUnloadServiceTimer(bool force = false);
62 
63     /**
64      * @brief clear scheduler data
65      *
66      */
67     void ClearSchedulerData();
68 
69     /**
70      * @brief unload service
71      *
72      */
73     void TryUnloadService();
74 
75     void StartTimer();
76 
77 public:
SchedScheduler(wptr<Service> reversePtr,wptr<SvcSessionManager> sessionPtr)78     explicit SchedScheduler(wptr<Service> reversePtr, wptr<SvcSessionManager> sessionPtr)
79         : reversePtr_(reversePtr), sessionPtr_(sessionPtr)
80     {
81         threadPool_.Start(BConstants::SA_THREAD_POOL_COUNT);
82     }
83 
~SchedScheduler()84     ~SchedScheduler() override
85     {
86         extTime_.Shutdown();
87         bundleTimeVec_.clear();
88     }
89 
90 private:
91     mutable std::shared_mutex lock_;
92     OHOS::ThreadPool threadPool_;
93 
94     // 注册心跳信息
95     std::vector<std::tuple<std::string, uint32_t>> bundleTimeVec_;
96 
97     wptr<Service> reversePtr_;
98     wptr<SvcSessionManager> sessionPtr_;
99     Utils::Timer extTime_ {"backupSchedTimer"};
100 };
101 } // namespace OHOS::FileManagement::Backup
102 
103 #endif // OHOS_FILEMGMT_BACKUP_SCHED_SCHEDULER_H
104