• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #define MLOG_TAG "Media_Client"
16 
17 #include "cloud_media_data_handler.h"
18 
19 #include <string>
20 
21 #include "media_log.h"
22 #include "user_define_ipc_client.h"
23 #include "userfile_client.h"
24 #include "media_req_vo.h"
25 #include "media_resp_vo.h"
26 #include "media_empty_obj_vo.h"
27 #include "cloud_media_operation_code.h"
28 #include "medialibrary_errno.h"
29 #include "cloud_media_data_handler_factory.h"
30 #include "cloud_media_thread_limiter.h"
31 
32 namespace OHOS::Media::CloudSync {
33 // LCOV_EXCL_START
CloudMediaDataHandler(const std::string & tableName,int32_t cloudType,int32_t userId)34 CloudMediaDataHandler::CloudMediaDataHandler(const std::string &tableName, int32_t cloudType, int32_t userId)
35     : cloudType_(cloudType), userId_(userId), tableName_(tableName)
36 {
37     this->dataHandler_ = CloudMediaDataHandlerFactory().GetDataHandler(tableName, userId);
38     MEDIA_INFO_LOG("media-ipc userId: %{public}d", userId);
39 }
40 
GetCloudType() const41 int32_t CloudMediaDataHandler::GetCloudType() const
42 {
43     return this->cloudType_;
44 }
45 
SetUserId(const int32_t & userId)46 void CloudMediaDataHandler::SetUserId(const int32_t &userId)
47 {
48     this->userId_ = userId;
49 }
50 
GetUserId() const51 int32_t CloudMediaDataHandler::GetUserId() const
52 {
53     return this->userId_;
54 }
55 
SetCloudType(int32_t cloudType)56 void CloudMediaDataHandler::SetCloudType(int32_t cloudType)
57 {
58     this->cloudType_ = cloudType;
59 }
60 
GetTableName() const61 std::string CloudMediaDataHandler::GetTableName() const
62 {
63     return this->tableName_;
64 }
65 
SetTableName(const std::string & tableName)66 void CloudMediaDataHandler::SetTableName(const std::string &tableName)
67 {
68     this->tableName_ = tableName;
69     this->dataHandler_ = CloudMediaDataHandlerFactory().GetDataHandler(tableName, userId_);
70 }
71 
SetTraceId(const std::string & traceId)72 void CloudMediaDataHandler::SetTraceId(const std::string &traceId)
73 {
74     this->traceId_ = traceId;
75     if (this->dataHandler_ == nullptr) {
76         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
77         return;
78     }
79     this->dataHandler_->SetTraceId(traceId);
80 }
81 
GetTraceId() const82 std::string CloudMediaDataHandler::GetTraceId() const
83 {
84     return this->traceId_;
85 }
86 
GetCheckRecords(const std::vector<std::string> & cloudIds,std::unordered_map<std::string,CloudCheckData> & checkRecords)87 int32_t CloudMediaDataHandler::GetCheckRecords(
88     const std::vector<std::string> &cloudIds, std::unordered_map<std::string, CloudCheckData> &checkRecords)
89 {
90     MEDIA_INFO_LOG("OnCompletePush enter, cloudIds: %{public}zu", cloudIds.size());
91     if (this->dataHandler_ == nullptr) {
92         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
93         return E_IPC_INVAL_ARG;
94     }
95     return this->dataHandler_->GetCheckRecords(cloudIds, checkRecords);
96 }
97 
GetCreatedRecords(std::vector<MDKRecord> & records,int32_t size)98 int32_t CloudMediaDataHandler::GetCreatedRecords(std::vector<MDKRecord> &records, int32_t size)
99 {
100     MEDIA_INFO_LOG("GetCreatedRecords enter, records: %{public}zu", records.size());
101     if (this->dataHandler_ == nullptr) {
102         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
103         return E_IPC_INVAL_ARG;
104     }
105     return this->dataHandler_->GetCreatedRecords(records, size);
106 }
107 
GetMetaModifiedRecords(std::vector<MDKRecord> & records,int32_t size,int32_t dirtyType)108 int32_t CloudMediaDataHandler::GetMetaModifiedRecords(std::vector<MDKRecord> &records, int32_t size, int32_t dirtyType)
109 {
110     MEDIA_INFO_LOG("GetMetaModifiedRecords enter, records: %{public}zu", records.size());
111     if (this->dataHandler_ == nullptr) {
112         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
113         return E_IPC_INVAL_ARG;
114     }
115     return this->dataHandler_->GetMetaModifiedRecords(records, size, dirtyType);
116 }
117 
GetFileModifiedRecords(std::vector<MDKRecord> & records,int32_t size)118 int32_t CloudMediaDataHandler::GetFileModifiedRecords(std::vector<MDKRecord> &records, int32_t size)
119 {
120     MEDIA_INFO_LOG("GetFileModifiedRecords enter, records: %{public}zu", records.size());
121     if (this->dataHandler_ == nullptr) {
122         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
123         return E_IPC_INVAL_ARG;
124     }
125     return this->dataHandler_->GetFileModifiedRecords(records, size);
126 }
127 
GetDeletedRecords(std::vector<MDKRecord> & records,int32_t size)128 int32_t CloudMediaDataHandler::GetDeletedRecords(std::vector<MDKRecord> &records, int32_t size)
129 {
130     MEDIA_INFO_LOG("GetDeletedRecords enter, records: %{public}zu", records.size());
131     if (this->dataHandler_ == nullptr) {
132         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
133         return E_IPC_INVAL_ARG;
134     }
135     return this->dataHandler_->GetDeletedRecords(records, size);
136 }
137 
GetCopyRecords(std::vector<MDKRecord> & records,int32_t size)138 int32_t CloudMediaDataHandler::GetCopyRecords(std::vector<MDKRecord> &records, int32_t size)
139 {
140     MEDIA_INFO_LOG("GetCopyRecords enter, records: %{public}zu", records.size());
141     if (this->dataHandler_ == nullptr) {
142         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
143         return E_IPC_INVAL_ARG;
144     }
145     return this->dataHandler_->GetCopyRecords(records, size);
146 }
147 
OnCreateRecords(const std::map<std::string,MDKRecordOperResult> & map,int32_t & failSize)148 int32_t CloudMediaDataHandler::OnCreateRecords(const std::map<std::string, MDKRecordOperResult> &map, int32_t &failSize)
149 {
150     MEDIA_INFO_LOG("OnCreateRecords enter, map: %{public}zu", map.size());
151     if (this->dataHandler_ == nullptr) {
152         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
153         return E_IPC_INVAL_ARG;
154     }
155     CLOUD_SYNC_HANDLER_WRITE_LOCK;
156     return this->dataHandler_->OnCreateRecords(map, failSize);
157 }
158 
OnMdirtyRecords(const std::map<std::string,MDKRecordOperResult> & map,int32_t & failSize)159 int32_t CloudMediaDataHandler::OnMdirtyRecords(const std::map<std::string, MDKRecordOperResult> &map, int32_t &failSize)
160 {
161     MEDIA_INFO_LOG("OnMdirtyRecords enter, map: %{public}zu", map.size());
162     if (this->dataHandler_ == nullptr) {
163         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
164         return E_IPC_INVAL_ARG;
165     }
166     CLOUD_SYNC_HANDLER_WRITE_LOCK;
167     return this->dataHandler_->OnMdirtyRecords(map, failSize);
168 }
169 
OnFdirtyRecords(const std::map<std::string,MDKRecordOperResult> & map,int32_t & failSize)170 int32_t CloudMediaDataHandler::OnFdirtyRecords(const std::map<std::string, MDKRecordOperResult> &map, int32_t &failSize)
171 {
172     MEDIA_INFO_LOG("OnFdirtyRecords enter, map: %{public}zu", map.size());
173     if (this->dataHandler_ == nullptr) {
174         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
175         return E_IPC_INVAL_ARG;
176     }
177     CLOUD_SYNC_HANDLER_WRITE_LOCK;
178     return this->dataHandler_->OnFdirtyRecords(map, failSize);
179 }
180 
OnDeleteRecords(const std::map<std::string,MDKRecordOperResult> & map,int32_t & failSize)181 int32_t CloudMediaDataHandler::OnDeleteRecords(const std::map<std::string, MDKRecordOperResult> &map, int32_t &failSize)
182 {
183     MEDIA_INFO_LOG("OnDeleteRecords enter, map: %{public}zu", map.size());
184     if (this->dataHandler_ == nullptr) {
185         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
186         return E_IPC_INVAL_ARG;
187     }
188     CLOUD_SYNC_HANDLER_WRITE_LOCK;
189     return this->dataHandler_->OnDeleteRecords(map, failSize);
190 }
191 
OnCopyRecords(const std::map<std::string,MDKRecordOperResult> & map,int32_t & failSize)192 int32_t CloudMediaDataHandler::OnCopyRecords(const std::map<std::string, MDKRecordOperResult> &map, int32_t &failSize)
193 {
194     MEDIA_INFO_LOG("OnCopyRecords enter, map: %{public}zu", map.size());
195     if (this->dataHandler_ == nullptr) {
196         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
197         return E_IPC_INVAL_ARG;
198     }
199     CLOUD_SYNC_HANDLER_WRITE_LOCK;
200     return this->dataHandler_->OnCopyRecords(map, failSize);
201 }
202 
OnFetchRecords(const std::vector<MDKRecord> & records,std::vector<CloudMetaData> & newData,std::vector<CloudMetaData> & fdirtyData,std::vector<std::string> & failedRecords,std::vector<int32_t> & stats)203 int32_t CloudMediaDataHandler::OnFetchRecords(const std::vector<MDKRecord> &records,
204     std::vector<CloudMetaData> &newData, std::vector<CloudMetaData> &fdirtyData,
205     std::vector<std::string> &failedRecords, std::vector<int32_t> &stats)
206 {
207     if (this->dataHandler_ == nullptr) {
208         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
209         return E_IPC_INVAL_ARG;
210     }
211     CLOUD_SYNC_HANDLER_WRITE_LOCK;
212     return this->dataHandler_->OnFetchRecords(records, newData, fdirtyData, failedRecords, stats);
213 }
214 
OnDentryFileInsert(std::vector<MDKRecord> & records,std::vector<std::string> & failedRecords)215 int32_t CloudMediaDataHandler::OnDentryFileInsert(
216     std::vector<MDKRecord> &records, std::vector<std::string> &failedRecords)
217 {
218     if (this->dataHandler_ == nullptr) {
219         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
220         return E_IPC_INVAL_ARG;
221     }
222     CLOUD_SYNC_HANDLER_WRITE_LOCK;
223     return this->dataHandler_->OnDentryFileInsert(records, failedRecords);
224 }
225 
GetRetryRecords(std::vector<std::string> & records)226 int32_t CloudMediaDataHandler::GetRetryRecords(std::vector<std::string> &records)
227 {
228     if (this->dataHandler_ == nullptr) {
229         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
230         return E_IPC_INVAL_ARG;
231     }
232     return this->dataHandler_->GetRetryRecords(records);
233 }
234 
OnStartSync()235 int32_t CloudMediaDataHandler::OnStartSync()
236 {
237     if (this->dataHandler_ == nullptr) {
238         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
239         return E_IPC_INVAL_ARG;
240     }
241     CLOUD_SYNC_HANDLER_WRITE_LOCK;
242     return this->dataHandler_->OnStartSync();
243 }
244 
OnCompleteSync()245 int32_t CloudMediaDataHandler::OnCompleteSync()
246 {
247     if (this->dataHandler_ == nullptr) {
248         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
249         return E_IPC_INVAL_ARG;
250     }
251     CLOUD_SYNC_HANDLER_WRITE_LOCK;
252     return this->dataHandler_->OnCompleteSync();
253 }
254 
OnCompletePull()255 int32_t CloudMediaDataHandler::OnCompletePull()
256 {
257     if (this->dataHandler_ == nullptr) {
258         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
259         return E_IPC_INVAL_ARG;
260     }
261     CLOUD_SYNC_HANDLER_WRITE_LOCK;
262     return this->dataHandler_->OnCompletePull();
263 }
264 
OnCompletePush()265 int32_t CloudMediaDataHandler::OnCompletePush()
266 {
267     if (this->dataHandler_ == nullptr) {
268         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
269         return E_IPC_INVAL_ARG;
270     }
271     CLOUD_SYNC_HANDLER_WRITE_LOCK;
272     return this->dataHandler_->OnCompletePush();
273 }
274 
OnCompleteCheck()275 int32_t CloudMediaDataHandler::OnCompleteCheck()
276 {
277     if (this->dataHandler_ == nullptr) {
278         MEDIA_ERR_LOG("No data handler found! tableName: %{public}s", this->tableName_.c_str());
279         return E_IPC_INVAL_ARG;
280     }
281     CLOUD_SYNC_HANDLER_WRITE_LOCK;
282     return this->dataHandler_->OnCompleteCheck();
283 }
284 // LCOV_EXCL_STOP
285 }  // namespace OHOS::Media::CloudSync