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 "gallery_data_syncer.h"
17
18 #include "dfs_error.h"
19 #include "utils_log.h"
20
21 namespace OHOS {
22 namespace FileManagement {
23 namespace CloudSync {
24 using namespace std;
25
CloudSyncTriggerFunc(const std::vector<std::string> & args)26 const std::string CloudSyncTriggerFunc(const std::vector<std::string> &args)
27 {
28 return "";
29 }
30
IsCallerSelfFunc(const std::vector<std::string> & args)31 const std::string IsCallerSelfFunc(const std::vector<std::string> &args)
32 {
33 return "false";
34 }
35
GalleryDataSyncer(const std::string bundleName,const int32_t userId)36 GalleryDataSyncer::GalleryDataSyncer(const std::string bundleName, const int32_t userId)
37 : DataSyncer(bundleName, userId)
38 {
39 }
40
Init(const std::string bundleName,const int32_t userId)41 int32_t GalleryDataSyncer::Init(const std::string bundleName, const int32_t userId)
42 {
43 /* rdb config */
44 NativeRdb::RdbStoreConfig config(DATABASE_NAME);
45 config.SetPath(DATA_APP_EL2 + to_string(userId) + DATABASE_DIR + DATABASE_NAME);
46 config.SetBundleName(BUNDLE_NAME);
47 config.SetReadConSize(CONNECT_SIZE);
48 config.SetSecurityLevel(NativeRdb::SecurityLevel::S3);
49 config.SetScalarFunction("cloud_sync_func", 0, CloudSyncTriggerFunc);
50 config.SetScalarFunction("is_caller_self_func", 0, IsCallerSelfFunc);
51
52 /*
53 * Just pass in any value but zero for parameter @version in GetRdbStore,
54 * since RdbCallback always return 0 in its callbacks.
55 */
56 int32_t err;
57 RdbCallback cb;
58 auto rdb_ = NativeRdb::RdbHelper::GetRdbStore(config, Media::MEDIA_RDB_VERSION, cb, err);
59 if (rdb_ == nullptr) {
60 LOGE("gallyer data syncer init rdb fail");
61 return E_RDB;
62 }
63
64 /* init handler */
65 fileHandler_ = make_shared<FileDataHandler>(userId, bundleName, rdb_);
66 albumHandler_ = make_shared<AlbumDataHandler>(userId, bundleName, rdb_);
67 return E_OK;
68 }
69
Clean(const int action)70 int32_t GalleryDataSyncer::Clean(const int action)
71 {
72 LOGD("gallery data sycner Clean");
73 /* file */
74 int32_t ret = CleanInner(fileHandler_, action);
75 if (ret != E_OK) {
76 LOGE("gallery data syncer file clean err %{public}d", ret);
77 }
78 /* album */
79 ret = CleanInner(albumHandler_, action);
80 if (ret != E_OK) {
81 LOGE("gallery data syncer album clean err %{public}d", ret);
82 }
83 DeleteSubscription();
84 return ret;
85 }
86
StartDownloadFile(const std::string path,const int32_t userId)87 int32_t GalleryDataSyncer::StartDownloadFile(const std::string path, const int32_t userId)
88 {
89 return DownloadInner(fileHandler_, path, userId);
90 }
91
StopDownloadFile(const std::string path,const int32_t userId)92 int32_t GalleryDataSyncer::StopDownloadFile(const std::string path, const int32_t userId)
93 {
94 return DataSyncer::StopDownloadFile(path, userId);
95 }
96
Schedule()97 void GalleryDataSyncer::Schedule()
98 {
99 LOGI("schedule to stage %{public}d", ++stage_);
100
101 int32_t ret = E_OK;
102 switch (stage_) {
103 case DOWNLOADALBUM: {
104 ret = DownloadAlbum();
105 break;
106 }
107 case DOWNLOADFILE: {
108 ret = DownloadFile();
109 break;
110 }
111 case COMPLETEPULL: {
112 ret = CompletePull();
113 break;
114 }
115 case UPLOADALBUM: {
116 ret = UploadAlbum();
117 break;
118 }
119 case UPLOADFILE: {
120 ret = UploadFile();
121 break;
122 }
123 case COMPLETEPUSH: {
124 ret = CompletePush();
125 break;
126 }
127 case END: {
128 ret = Complete();
129 break;
130 }
131 default: {
132 ret = E_SCHEDULE;
133 LOGE("schedule fail %{public}d", ret);
134 break;
135 }
136 }
137
138 /* if error takes place while schedule, just abort it */
139 if (ret != E_OK) {
140 Abort();
141 }
142 }
143
Reset()144 void GalleryDataSyncer::Reset()
145 {
146 /* reset stage in case of stop or restart */
147 stage_ = BEGIN;
148 /* restart a sync might need to update offset, etc */
149 fileHandler_->Reset();
150 albumHandler_->Reset();
151 }
152
DownloadAlbum()153 int32_t GalleryDataSyncer::DownloadAlbum()
154 {
155 SyncStateChangedNotify(CloudSyncState::DOWNLOADING, ErrorType::NO_ERROR);
156
157 LOGI("gallery data sycner download album");
158 int32_t ret = Pull(albumHandler_);
159 if (ret != E_OK) {
160 LOGE("gallery data syncer pull album err %{public}d", ret);
161 }
162 return ret;
163 }
164
DownloadFile()165 int32_t GalleryDataSyncer::DownloadFile()
166 {
167 LOGI("gallery data sycner download file");
168 int ret = Pull(fileHandler_);
169 if (ret != E_OK) {
170 LOGE("gallery data syncer pull file err %{public}d", ret);
171 }
172 return ret;
173 }
174
UploadAlbum()175 int32_t GalleryDataSyncer::UploadAlbum()
176 {
177 int32_t ret = Lock();
178 if (ret != E_OK) {
179 LOGE("gallery data syncer lock err %{public}d", ret);
180 return E_CLOUD_SDK;
181 }
182
183 SyncStateChangedNotify(CloudSyncState::UPLOADING, ErrorType::NO_ERROR);
184
185 LOGI("gallery data sycner upload album");
186 ret = Push(albumHandler_);
187 if (ret != E_OK) {
188 LOGE("gallery data syncer push album err %{public}d", ret);
189 }
190
191 return ret;
192 }
193
UploadFile()194 int32_t GalleryDataSyncer::UploadFile()
195 {
196 LOGI("gallery data sycner upload file");
197 int32_t ret = Push(fileHandler_);
198 if (ret != E_OK) {
199 LOGE("gallery data syncer push file err %{public}d", ret);
200 }
201 return ret;
202 }
203
Complete()204 int32_t GalleryDataSyncer::Complete()
205 {
206 LOGI("gallery data syncer complete all");
207 Unlock();
208 CompleteAll();
209 return E_OK;
210 }
211 } // namespace CloudSync
212 } // namespace FileManagement
213 } // namespace OHOS
214