1 /*
2  * Copyright (c) 2023 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_CLOUD_SYNC_SERVICE_CYCLE_TASK_H
17 #define OHOS_CLOUD_SYNC_SERVICE_CYCLE_TASK_H
18 #include <cstdint>
19 #include <memory>
20 #include <string>
21 #include <set>
22 
23 #include "cloud_pref_impl.h"
24 #include "dfs_error.h"
25 #include "data_sync_manager.h"
26 namespace OHOS {
27 namespace FileManagement {
28 namespace CloudSync {
29 class CycleTask {
30 public:
31     enum IntervalTime {
32         ONE_DAY = 24 * 60 * 60,
33         TWO_DAY = ONE_DAY * 2,
34         THREE_DAY = ONE_DAY * 3,
35         ONE_WEEK = ONE_DAY * 7,
36     };
37     CycleTask(std::string taskName,
38               std::set<std::string> bundleNames,
39               int32_t intervalTime,
40               std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager);
41     virtual ~CycleTask() = default;
42     std::string GetTaskName() const;
43     void RunTask(int32_t userId);
44     void SetRunnableBundleNames(std::shared_ptr<std::set<std::string>> &bundleNames);
45     std::shared_ptr<CloudFile::DataSyncManager> GetDataSyncManager() const;
46     static const std::string FILE_PATH;
47 
48 protected:
49     int32_t userId_;
50     std::string taskName_;
51     std::set<std::string> bundleNames_;
52     int32_t intervalTime_;
53     std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager_;
54 
55 private:
56     void GetLastRunTime(std::time_t &time);
57     void SetLastRunTime(std::time_t time);
58     bool IsEligibleToRun(std::time_t currentTime, std::string bundleName);
59     virtual int32_t RunTaskForBundle(int32_t userId, std::string bundleName) = 0;
60 
61     std::unique_ptr<CloudPrefImpl> cloudPrefImpl_;
62     std::shared_ptr<std::set<std::string>> runnableBundleNames_;
63 };
64 } // namespace CloudSync
65 } // namespace FileManagement
66 } // namespace OHOS
67 #endif // OHOS_CLOUD_SYNC_SERVICE_CYCLE_TASK_H