• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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_runner.h"
17 #include "cloud_status.h"
18 #include "cycle_task.h"
19 #include "data_syncer_rdb_col.h"
20 #include "data_syncer_rdb_store.h"
21 #include "os_account_manager.h"
22 #include "parameter.h"
23 #include "result_set.h"
24 #include "tasks/database_backup_task.h"
25 #include "tasks/optimize_cache_task.h"
26 #include "tasks/optimize_storage_task.h"
27 #include "tasks/periodic_check_task.h"
28 #include "tasks/report_statistics_task.h"
29 #include "tasks/save_subscription_task.h"
30 #include "utils_log.h"
31 #include <memory>
32 
33 namespace OHOS {
34 namespace FileManagement {
35 namespace CloudSync {
36 using namespace std;
37 
CycleTaskRunner(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)38 CycleTaskRunner::CycleTaskRunner(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
39 {
40     dataSyncManager_ = dataSyncManager;
41     vector<int32_t> activeUsers;
42     if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUsers) != E_OK || activeUsers.empty()) {
43         LOGE("query active user failed");
44         return;
45     }
46     userId_ = activeUsers.front();
47     setUpTime_ = std::time(nullptr);
48     if (dataSyncManager_ == nullptr) {
49         LOGI("dataSyncManager is nullptr");
50         return;
51     }
52     InitTasks();
53     SetRunableBundleNames();
54 }
55 
StartTask()56 void CycleTaskRunner::StartTask()
57 {
58 #ifdef EMULATOR
59     return;
60 #endif
61 
62     constexpr int32_t MOVE_FILE_TIME_SERVICE = 5;
63     int status = WaitParameter("persist.kernel.move.finish", "true", MOVE_FILE_TIME_SERVICE);
64     if (status != 0) {
65         LOGE("wait move error, return value %{public}d.", status);
66         return;
67     }
68     for (const auto &task_data : cycleTasks_) {
69         task_data->RunTask(userId_);
70     }
71 }
72 
InitTasks()73 void CycleTaskRunner::InitTasks()
74 {
75     //push tasks here
76     cycleTasks_.push_back(std::make_shared<OptimizeCacheTask>(dataSyncManager_));
77     cycleTasks_.push_back(std::make_shared<OptimizeStorageTask>(dataSyncManager_));
78     cycleTasks_.push_back(std::make_shared<SaveSubscriptionTask>(dataSyncManager_));
79     cycleTasks_.push_back(std::make_shared<ReportStatisticsTask>(dataSyncManager_));
80     cycleTasks_.push_back(std::make_shared<DatabaseBackupTask>(dataSyncManager_));
81 
82     //do periodic check task last
83     cycleTasks_.push_back(std::make_shared<PeriodicCheckTask>(dataSyncManager_));
84 }
85 
GetString(const string & key,string & val,NativeRdb::ResultSet & resultSet)86 static int32_t GetString(const string &key, string &val, NativeRdb::ResultSet &resultSet)
87 {
88     int32_t index;
89     int32_t err = resultSet.GetColumnIndex(key, index);
90     if (err != NativeRdb::E_OK) {
91         LOGE("result set get  %{public}s column index err %{public}d", key.c_str(), err);
92         return E_RDB;
93     }
94 
95     err = resultSet.GetString(index, val);
96     if (err != 0) {
97         LOGE("result set get string err %{public}d", err);
98         return E_RDB;
99     }
100 
101     return E_OK;
102 }
103 
SetRunableBundleNames()104 void CycleTaskRunner::SetRunableBundleNames()
105 {
106     std::shared_ptr<std::set<std::string>> runnableBundleNames = make_shared<std::set<std::string>>();
107     std::shared_ptr<NativeRdb::ResultSet> resultSet = nullptr;
108     int32_t ret = DataSyncerRdbStore::GetInstance().QueryDataSyncer(userId_, resultSet);
109     if (ret != 0 || resultSet == nullptr) {
110         LOGE("query data syncer fail %{public}d", ret);
111         return;
112     }
113     while (resultSet->GoToNextRow() == E_OK) {
114         string bundleName;
115         ret = GetString(BUNDLE_NAME, bundleName, *resultSet);
116         if (ret != E_OK) {
117             LOGE("get bundle name failed");
118             continue;
119         }
120         std::time_t currentTime = std::time(nullptr);
121         std::unique_ptr<CloudPrefImpl> cloudPrefImpl =
122             std::make_unique<CloudPrefImpl>(userId_, bundleName,  CycleTask::FILE_PATH);
123         std::time_t lastCheckTime;
124         if (cloudPrefImpl == nullptr) {
125             LOGE("cloudPrefImpl is nullptr");
126             continue;
127         }
128         cloudPrefImpl->GetLong("lastCheckTime", lastCheckTime);
129         if (lastCheckTime != 0 && difftime(currentTime, lastCheckTime) < CycleTask::ONE_DAY) {
130             continue;
131         }
132         bool cloudStatus = CloudStatus::IsCloudStatusOkay(bundleName, userId_);
133         if (!cloudStatus) {
134             LOGI(" %{public}s cloud status is not ok, skip task, ret is %{public}d", bundleName.c_str(), ret);
135             cloudPrefImpl->SetLong("lastCheckTime", currentTime);
136             continue;
137         }
138         cloudPrefImpl->Delete("lastCheckTime");
139         runnableBundleNames->insert(bundleName);
140     }
141 
142     for (auto task_data  : cycleTasks_) {
143         task_data->SetRunnableBundleNames(runnableBundleNames);
144     }
145 }
146 } // namespace CloudSync
147 } // namespace FileManagement
148 } // namespace OHOS