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 #ifdef EMULATOR
58     return;
59 #endif
60 
61     constexpr int32_t MOVE_FILE_TIME_SERVICE = 5;
62     int status = WaitParameter("persist.kernel.move.finish", "true", MOVE_FILE_TIME_SERVICE);
63     if (status != 0) {
64         LOGE("wait move error, return value %{public}d.", status);
65         return;
66     }
67     for (const auto &task_data : cycleTasks_) {
68         task_data->RunTask(userId_);
69     }
70 }
71 
InitTasks()72 void CycleTaskRunner::InitTasks()
73 {
74     //push tasks here
75     cycleTasks_.push_back(std::make_shared<OptimizeStorageTask>(dataSyncManager_));
76     cycleTasks_.push_back(std::make_shared<SaveSubscriptionTask>(dataSyncManager_));
77     cycleTasks_.push_back(std::make_shared<ReportStatisticsTask>(dataSyncManager_));
78     cycleTasks_.push_back(std::make_shared<DatabaseBackupTask>(dataSyncManager_));
79 
80     //do periodic check task last
81     cycleTasks_.push_back(std::make_shared<PeriodicCheckTask>(dataSyncManager_));
82 }
83 
GetString(const string & key,string & val,NativeRdb::ResultSet & resultSet)84 static int32_t GetString(const string &key, string &val, NativeRdb::ResultSet &resultSet)
85 {
86     int32_t index;
87     int32_t err = resultSet.GetColumnIndex(key, index);
88     if (err != NativeRdb::E_OK) {
89         LOGE("result set get  %{public}s column index err %{public}d", key.c_str(), err);
90         return E_RDB;
91     }
92 
93     err = resultSet.GetString(index, val);
94     if (err != 0) {
95         LOGE("result set get string err %{public}d", err);
96         return E_RDB;
97     }
98 
99     return E_OK;
100 }
101 
SetRunableBundleNames()102 void CycleTaskRunner::SetRunableBundleNames()
103 {
104     std::shared_ptr<std::set<std::string>> runnableBundleNames = make_shared<std::set<std::string>>();
105     std::shared_ptr<NativeRdb::ResultSet> resultSet = nullptr;
106     int32_t ret = DataSyncerRdbStore::GetInstance().QueryDataSyncer(userId_, resultSet);
107     if (ret != 0 || resultSet == nullptr) {
108         LOGE("query data syncer fail %{public}d", ret);
109         return;
110     }
111     while (resultSet->GoToNextRow() == E_OK) {
112         string bundleName;
113         ret = GetString(BUNDLE_NAME, bundleName, *resultSet);
114         if (ret != E_OK) {
115             LOGE("get bundle name failed");
116             continue;
117         }
118         std::time_t currentTime = std::time(nullptr);
119         std::unique_ptr<CloudPrefImpl> cloudPrefImpl =
120             std::make_unique<CloudPrefImpl>(userId_, bundleName,  CycleTask::FILE_PATH);
121         std::time_t lastCheckTime;
122         if (cloudPrefImpl == nullptr) {
123             LOGE("cloudPrefImpl is nullptr");
124             continue;
125         }
126         cloudPrefImpl->GetLong("lastCheckTime", lastCheckTime);
127         if (lastCheckTime != 0 && difftime(currentTime, lastCheckTime) < CycleTask::ONE_DAY) {
128             continue;
129         }
130         bool cloudStatus = CloudStatus::IsCloudStatusOkay(bundleName, userId_);
131         if (!cloudStatus) {
132             LOGI(" %{public}s cloud status is not ok, skip task, ret is %{public}d", bundleName.c_str(), ret);
133             cloudPrefImpl->SetLong("lastCheckTime", currentTime);
134             continue;
135         }
136         cloudPrefImpl->Delete("lastCheckTime");
137         runnableBundleNames->insert(bundleName);
138     }
139 
140     for (auto task_data  : cycleTasks_) {
141         task_data->SetRunnableBundleNames(runnableBundleNames);
142     }
143 }
144 } // namespace CloudSync
145 } // namespace FileManagement
146 } // namespace OHOS