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