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 #include "cycle_task.h"
17 #include "utils_log.h"
18
19 namespace OHOS {
20 namespace FileManagement {
21 namespace CloudSync {
CycleTask(std::string taskName,std::set<std::string> bundleNames,int32_t intervalTime,std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)22 CycleTask::CycleTask(std::string taskName,
23 std::set<std::string> bundleNames,
24 int32_t intervalTime,
25 std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
26 : userId_(-1),
27 taskName_(taskName),
28 bundleNames_(bundleNames),
29 intervalTime_(intervalTime),
30 dataSyncManager_(dataSyncManager)
31 {
32 }
33
34 const std::string CycleTask::FILE_PATH = "CycleTask";
35
SetRunnableBundleNames(std::shared_ptr<std::set<std::string>> & bundleNames)36 void CycleTask::SetRunnableBundleNames(std::shared_ptr<std::set<std::string>> &bundleNames)
37 {
38 this->runnableBundleNames_ = bundleNames;
39 }
40
GetDataSyncManager() const41 std::shared_ptr<CloudFile::DataSyncManager> CycleTask::GetDataSyncManager() const
42 {
43 return this->dataSyncManager_;
44 }
45
GetTaskName() const46 std::string CycleTask::GetTaskName() const
47 {
48 return this->taskName_;
49 }
50
SetLastRunTime(std::time_t time)51 void CycleTask::SetLastRunTime(std::time_t time)
52 {
53 if (cloudPrefImpl_ == nullptr) {
54 LOGE(" cloudPrefImpl is nullptr");
55 return;
56 }
57 cloudPrefImpl_->SetLong("lastRunTime-" + taskName_, time);
58 }
59
GetLastRunTime(std::time_t & time)60 void CycleTask::GetLastRunTime(std::time_t &time)
61 {
62 if (cloudPrefImpl_ == nullptr) {
63 LOGE(" cloudPrefImpl is nullptr");
64 time = std::time(nullptr);
65 return;
66 }
67 cloudPrefImpl_->GetLong("lastRunTime-" + taskName_, time);
68 }
69
IsEligibleToRun(std::time_t currentTime,std::string bundleName)70 bool CycleTask::IsEligibleToRun(std::time_t currentTime, std::string bundleName)
71 {
72 std::time_t lastRuntime;
73 GetLastRunTime(lastRuntime);
74 if (lastRuntime == 0) {
75 SetLastRunTime(currentTime);
76 LOGE("skip first run, bundle name is %{public}s", bundleName.c_str());
77 return false;
78 }
79 if ((bundleNames_.find(bundleName) != bundleNames_.end() || bundleNames_.size() == 0) &&
80 difftime(currentTime, lastRuntime) >= intervalTime_) {
81 return true;
82 }
83 return false;
84 }
85
RunTask(int32_t userId)86 void CycleTask::RunTask(int32_t userId)
87 {
88 if (runnableBundleNames_ == nullptr) {
89 LOGE("runnableBundleNames_ is nullptr");
90 return;
91 }
92 userId_ = userId;
93 std::time_t currentTime = std::time(nullptr);
94 for (const auto &bundleName : *runnableBundleNames_) {
95 cloudPrefImpl_ = std::make_unique<CloudPrefImpl>(userId_, bundleName, FILE_PATH);
96 if (IsEligibleToRun(currentTime, bundleName)) {
97 LOGI("begin task, task name is %{public}s, bundle name is %{public}s",
98 taskName_.c_str(), bundleName.c_str());
99 int32_t ret = RunTaskForBundle(userId, bundleName);
100 if (ret == E_OK) {
101 SetLastRunTime(currentTime);
102 }
103 LOGI("end task, task name is %{public}s, bundle name is %{public}s, ret is %{public}d",
104 taskName_.c_str(), bundleName.c_str(), ret);
105 }
106 }
107 }
108 } // namespace CloudSync
109 } // namespace FileManagement
110 } // namespace OHOS