• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 <charconv>
17 #include <iservice_registry.h>
18 #include <thread>
19 #include <regex>
20 
21 #include "convert_utils.h"
22 #include "ffrt/ffrt_utils.h"
23 #include "hitrace_meter.h"
24 #include "pasteboard_copy.h"
25 #include "pasteboard_deduplicate_memory.h"
26 #include "pasteboard_error.h"
27 #include "pasteboard_event_dfx.h"
28 #include "pasteboard_hilog.h"
29 #include "pasteboard_load_callback.h"
30 #include "pasteboard_pattern.h"
31 #include "pasteboard_progress.h"
32 #include "pasteboard_signal_callback.h"
33 #include "pasteboard_time.h"
34 #include "pasteboard_utils.h"
35 #include "pasteboard_web_controller.h"
36 #include "pasteboard_samgr_listener.h"
37 #include "pasteboard_service_loader.h"
38 #include "system_ability_definition.h"
39 #include "system_ability_status_change_stub.h"
40 #include "nlohmann/json.hpp"
41 using namespace OHOS::Media;
42 using json = nlohmann::json;
43 
44 namespace OHOS {
45 namespace MiscServices {
46 constexpr const int32_t HITRACE_GETPASTEDATA = 0;
47 std::string g_progressKey;
48 constexpr int32_t PASTEBOARD_PROGRESS_UPDATE_PERCENT = 5;
49 constexpr int32_t UPDATE_PERCENT_WITHOUT_FILE = 10;
50 constexpr int32_t PASTEBOARD_PROGRESS_TWENTY_PERCENT = 20;
51 constexpr int32_t PASTEBOARD_PROGRESS_FINISH_PERCENT = 100;
52 constexpr int32_t PASTEBOARD_PROGRESS_SLEEP_TIME = 100; // ms
53 constexpr int32_t SLEEP_TIME_WITHOUT_FILE = 50; // ms
54 constexpr int32_t PASTEBOARD_PROGRESS_RETRY_TIMES = 10;
55 constexpr int64_t REPORT_DUPLICATE_TIMEOUT = 2 * 60 * 1000; // 2 minutes
56 constexpr uint32_t JSON_INDENT = 4;
57 constexpr uint32_t RECORD_DISPLAY_UPPERBOUND = 3;
58 static constexpr int32_t HAP_PULL_UP_TIME = 500; // ms
59 static constexpr int32_t HAP_MIN_SHOW_TIME = 300; // ms
60 static sptr<PasteboardSaMgrListener> saCallback_ = nullptr;
61 constexpr const char *ERROR_CODE = "ERROR_CODE";
62 constexpr const char *DIS_SYNC_TIME = "DIS_SYNC_TIME";
63 constexpr const char *PACKAGE_NAME = "PACKAGE_NAME";
64 constexpr const char *PASTEDATA_SUMMARY = "PASTEDATA_SUMMARY";
65 std::mutex PasteboardClient::instanceLock_;
66 std::atomic<bool> PasteboardClient::remoteTask_(false);
67 std::atomic<bool> PasteboardClient::isPasting_(false);
68 std::atomic<uint64_t> PasteboardClient::progressStartTime_;
69 constexpr int64_t MIN_ASHMEM_DATA_SIZE = 32 * 1024; // 32K
70 constexpr uid_t ANCO_SERVICE_BROKER_UID = 5557;
71 
72 struct RadarReportIdentity {
73     pid_t pid;
74     int32_t errorCode;
75 };
76 
operator ==(const RadarReportIdentity & lhs,const RadarReportIdentity & rhs)77 bool operator==(const RadarReportIdentity &lhs, const RadarReportIdentity &rhs)
78 {
79     return lhs.pid == rhs.pid && lhs.errorCode == rhs.errorCode;
80 }
81 
PasteboardClient()82 PasteboardClient::PasteboardClient()
83 {
84     auto proxyService = GetPasteboardService();
85     if (proxyService == nullptr) {
86         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "proxyService is null");
87     }
88 }
89 
~PasteboardClient()90 PasteboardClient::~PasteboardClient()
91 {
92 }
93 
GetInstance()94 PasteboardClient *PasteboardClient::GetInstance()
95 {
96     static PasteboardClient instance;
97     return &instance;
98 }
99 
CreateHtmlTextRecord(const std::string & htmlText)100 std::shared_ptr<PasteDataRecord> PasteboardClient::CreateHtmlTextRecord(const std::string &htmlText)
101 {
102     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "New text record");
103     return PasteDataRecord::NewHtmlRecord(htmlText);
104 }
105 
CreateWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want)106 std::shared_ptr<PasteDataRecord> PasteboardClient::CreateWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want)
107 {
108     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "New want record");
109     return PasteDataRecord::NewWantRecord(std::move(want));
110 }
111 
CreatePlainTextRecord(const std::string & text)112 std::shared_ptr<PasteDataRecord> PasteboardClient::CreatePlainTextRecord(const std::string &text)
113 {
114     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "New text record");
115     return PasteDataRecord::NewPlainTextRecord(text);
116 }
117 
CreatePixelMapRecord(std::shared_ptr<PixelMap> pixelMap)118 std::shared_ptr<PasteDataRecord> PasteboardClient::CreatePixelMapRecord(std::shared_ptr<PixelMap> pixelMap)
119 {
120     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "New pixelMap record");
121     return PasteDataRecord::NewPixelMapRecord(std::move(pixelMap));
122 }
123 
CreateUriRecord(const OHOS::Uri & uri)124 std::shared_ptr<PasteDataRecord> PasteboardClient::CreateUriRecord(const OHOS::Uri &uri)
125 {
126     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "New uri record");
127     return PasteDataRecord::NewUriRecord(uri);
128 }
129 
CreateKvRecord(const std::string & mimeType,const std::vector<uint8_t> & arrayBuffer)130 std::shared_ptr<PasteDataRecord> PasteboardClient::CreateKvRecord(
131     const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer)
132 {
133     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "New kv record");
134     return PasteDataRecord::NewKvRecord(mimeType, arrayBuffer);
135 }
136 
CreateMultiDelayRecord(std::vector<std::string> mimeTypes,const std::shared_ptr<UDMF::EntryGetter> entryGetter)137 std::shared_ptr<PasteDataRecord> PasteboardClient::CreateMultiDelayRecord(
138     std::vector<std::string> mimeTypes, const std::shared_ptr<UDMF::EntryGetter> entryGetter)
139 {
140     return PasteDataRecord::NewMultiTypeDelayRecord(mimeTypes, entryGetter);
141 }
142 
CreateHtmlData(const std::string & htmlText)143 std::shared_ptr<PasteData> PasteboardClient::CreateHtmlData(const std::string &htmlText)
144 {
145     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "New htmlText data");
146     auto pasteData = std::make_shared<PasteData>();
147     pasteData->AddHtmlRecord(htmlText);
148     return pasteData;
149 }
150 
CreateWantData(std::shared_ptr<OHOS::AAFwk::Want> want)151 std::shared_ptr<PasteData> PasteboardClient::CreateWantData(std::shared_ptr<OHOS::AAFwk::Want> want)
152 {
153     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "New want data");
154     auto pasteData = std::make_shared<PasteData>();
155     pasteData->AddWantRecord(std::move(want));
156     return pasteData;
157 }
158 
CreatePlainTextData(const std::string & text)159 std::shared_ptr<PasteData> PasteboardClient::CreatePlainTextData(const std::string &text)
160 {
161     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "New plain data");
162     auto pasteData = std::make_shared<PasteData>();
163     pasteData->AddTextRecord(text);
164     return pasteData;
165 }
166 
CreatePixelMapData(std::shared_ptr<PixelMap> pixelMap)167 std::shared_ptr<PasteData> PasteboardClient::CreatePixelMapData(std::shared_ptr<PixelMap> pixelMap)
168 {
169     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "New pixelMap data");
170     auto pasteData = std::make_shared<PasteData>();
171     pasteData->AddPixelMapRecord(std::move(pixelMap));
172     return pasteData;
173 }
174 
CreateUriData(const OHOS::Uri & uri)175 std::shared_ptr<PasteData> PasteboardClient::CreateUriData(const OHOS::Uri &uri)
176 {
177     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "New uri data");
178     auto pasteData = std::make_shared<PasteData>();
179     pasteData->AddUriRecord(uri);
180     return pasteData;
181 }
182 
CreateKvData(const std::string & mimeType,const std::vector<uint8_t> & arrayBuffer)183 std::shared_ptr<PasteData> PasteboardClient::CreateKvData(
184     const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer)
185 {
186     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "New Kv data");
187     auto pasteData = std::make_shared<PasteData>();
188     pasteData->AddKvRecord(mimeType, arrayBuffer);
189     return pasteData;
190 }
191 
CreateMultiTypeData(std::shared_ptr<std::map<std::string,std::shared_ptr<EntryValue>>> typeValueMap,const std::string & recordMimeType)192 std::shared_ptr<PasteData> PasteboardClient::CreateMultiTypeData(
193     std::shared_ptr<std::map<std::string, std::shared_ptr<EntryValue>>> typeValueMap, const std::string &recordMimeType)
194 {
195     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "New multiType data");
196     auto pasteData = std::make_shared<PasteData>();
197     pasteData->AddRecord(PasteDataRecord::NewMultiTypeRecord(std::move(typeValueMap), recordMimeType));
198     return pasteData;
199 }
200 
CreateMultiTypeDelayData(std::vector<std::string> mimeTypes,std::shared_ptr<UDMF::EntryGetter> entryGetter)201 std::shared_ptr<PasteData> PasteboardClient::CreateMultiTypeDelayData(std::vector<std::string> mimeTypes,
202     std::shared_ptr<UDMF::EntryGetter> entryGetter)
203 {
204     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "New multiTypeDelay data");
205     auto pasteData = std::make_shared<PasteData>();
206     pasteData->AddRecord(PasteDataRecord::NewMultiTypeDelayRecord(mimeTypes, entryGetter));
207     return pasteData;
208 }
209 
GetChangeCount(uint32_t & changeCount)210 int32_t PasteboardClient::GetChangeCount(uint32_t &changeCount)
211 {
212     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "GetChangeCount start.");
213     auto proxyService = GetPasteboardService();
214     if (proxyService == nullptr) {
215         changeCount = 0;
216         return static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR);
217     }
218     return ConvertErrCode(proxyService->GetChangeCount(changeCount));
219 }
220 
SubscribeEntityObserver(EntityType entityType,uint32_t expectedDataLength,const sptr<EntityRecognitionObserver> & observer)221 int32_t PasteboardClient::SubscribeEntityObserver(
222     EntityType entityType, uint32_t expectedDataLength, const sptr<EntityRecognitionObserver> &observer)
223 {
224     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT,
225         "SubscribeEntityObserver start, type is %{public}u, length is %{public}u.", static_cast<uint32_t>(entityType),
226         expectedDataLength);
227     if (observer == nullptr) {
228         return static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR);
229     }
230     auto proxyService = GetPasteboardService();
231     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr,
232         static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR),
233         PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
234     return ConvertErrCode(proxyService->SubscribeEntityObserver(entityType, expectedDataLength, observer));
235 }
236 
UnsubscribeEntityObserver(EntityType entityType,uint32_t expectedDataLength,const sptr<EntityRecognitionObserver> & observer)237 int32_t PasteboardClient::UnsubscribeEntityObserver(
238     EntityType entityType, uint32_t expectedDataLength, const sptr<EntityRecognitionObserver> &observer)
239 {
240     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT,
241         "UnsubscribeEntityObserver start, type is %{public}u, length is %{public}u.", static_cast<uint32_t>(entityType),
242         expectedDataLength);
243     if (observer == nullptr) {
244         return static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR);
245     }
246     auto proxyService = GetPasteboardService();
247     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr,
248         static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR),
249         PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
250     return ConvertErrCode(proxyService->UnsubscribeEntityObserver(entityType, expectedDataLength, observer));
251 }
252 
GetRecordValueByType(uint32_t dataId,uint32_t recordId,PasteDataEntry & value)253 int32_t PasteboardClient::GetRecordValueByType(uint32_t dataId, uint32_t recordId, PasteDataEntry &value)
254 {
255     return PasteboardServiceLoader::GetInstance().GetRecordValueByType(dataId, recordId, value);
256 }
257 
Clear()258 void PasteboardClient::Clear()
259 {
260     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "Clear start.");
261     auto proxyService = GetPasteboardService();
262     PASTEBOARD_CHECK_AND_RETURN_LOGE(proxyService != nullptr, PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
263     proxyService->Clear();
264     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "Clear end.");
265     return;
266 }
267 
CloseSharedMemFd(int fd)268 void PasteboardClient::CloseSharedMemFd(int fd)
269 {
270     if (fd >= 0) {
271         PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "CloseSharedMemFd:%{public}d", fd);
272         close(fd);
273     }
274 }
275 
GetPasteData(PasteData & pasteData)276 int32_t PasteboardClient::GetPasteData(PasteData &pasteData)
277 {
278     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "enter");
279     pid_t pid = getpid();
280     std::string currentPid = std::to_string(pid);
281     std::string currentId = PasteData::CreatePasteId("GetPasteData", getSequenceId_++);
282     pasteData.SetPasteId(currentId);
283     RADAR_REPORT(RadarReporter::DFX_GET_PASTEBOARD, RadarReporter::DFX_GET_BIZ_SCENE, RadarReporter::DFX_SUCCESS,
284         RadarReporter::BIZ_STATE, RadarReporter::DFX_BEGIN, RadarReporter::CONCURRENT_ID, currentId,
285         PACKAGE_NAME, currentPid);
286     StartAsyncTrace(HITRACE_TAG_MISC, "PasteboardClient::GetPasteData", HITRACE_GETPASTEDATA);
287     auto proxyService = GetPasteboardService();
288     if (proxyService == nullptr) {
289         RADAR_REPORT(RadarReporter::DFX_GET_PASTEBOARD, RadarReporter::DFX_CHECK_GET_SERVER, RadarReporter::DFX_FAILED,
290             RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::CONCURRENT_ID, currentId,
291             PACKAGE_NAME, currentPid, ERROR_CODE, static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR));
292         return static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR);
293     }
294     int32_t syncTime = 0;
295     int fd = -1;
296     int64_t rawDataSize = 0;
297     std::vector<uint8_t> recvTLV;
298     int32_t ret = proxyService->GetPasteData(fd, rawDataSize, recvTLV, pasteData.GetPasteId(), syncTime);
299     int32_t bizStage = (syncTime == 0) ? RadarReporter::DFX_LOCAL_PASTE_END : RadarReporter::DFX_DISTRIBUTED_PASTE_END;
300     ret = ConvertErrCode(ret);
301     int32_t result = ProcessPasteData<PasteData>(pasteData, rawDataSize, fd, recvTLV);
302     PasteboardWebController::GetInstance().RetainUri(pasteData);
303     PasteboardWebController::GetInstance().RebuildWebviewPasteData(pasteData);
304     if (ret != static_cast<int32_t>(PasteboardError::E_OK)) {
305         GetDataReport(pasteData, syncTime, currentId, currentPid, ret);
306         return ret;
307     } else if (result == static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR)) {
308         GetDataReport(pasteData, syncTime, currentId, currentPid, result);
309         return result;
310     }
311     GetDataReport(pasteData, syncTime, currentId, currentPid, ret);
312     return static_cast<int32_t>(PasteboardError::E_OK);
313 }
314 
GetDataReport(PasteData & pasteData,int32_t syncTime,const std::string & currentId,const std::string & currentPid,int32_t ret)315 void PasteboardClient::GetDataReport(PasteData &pasteData, int32_t syncTime, const std::string &currentId,
316     const std::string &currentPid, int32_t ret)
317 {
318     static DeduplicateMemory<RadarReportIdentity> reportMemory(REPORT_DUPLICATE_TIMEOUT);
319     int32_t bizStage = (syncTime == 0) ? RadarReporter::DFX_LOCAL_PASTE_END : RadarReporter::DFX_DISTRIBUTED_PASTE_END;
320     FinishAsyncTrace(HITRACE_TAG_MISC, "PasteboardClient::GetPasteData", HITRACE_GETPASTEDATA);
321     std::string pasteDataInfoSummary = GetPasteDataInfoSummary(pasteData);
322     if (ret == static_cast<int32_t>(PasteboardError::E_OK)) {
323         if (pasteData.deviceId_.empty()) {
324             RADAR_REPORT(RadarReporter::DFX_GET_PASTEBOARD, bizStage, RadarReporter::DFX_SUCCESS,
325                 RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::CONCURRENT_ID, currentId,
326                 PACKAGE_NAME, currentPid, DIS_SYNC_TIME, syncTime,
327                 PASTEDATA_SUMMARY, pasteDataInfoSummary);
328         } else {
329             RADAR_REPORT(RadarReporter::DFX_GET_PASTEBOARD, bizStage, RadarReporter::DFX_SUCCESS,
330                 RadarReporter::CONCURRENT_ID, currentId, PACKAGE_NAME, currentPid,
331                 DIS_SYNC_TIME, syncTime, PASTEDATA_SUMMARY,
332                 pasteDataInfoSummary);
333         }
334     } else if (ret != static_cast<int32_t>(PasteboardError::TASK_PROCESSING) &&
335                !reportMemory.IsDuplicate({.pid = getpid(), .errorCode = ret})) {
336         RADAR_REPORT(RadarReporter::DFX_GET_PASTEBOARD, bizStage, RadarReporter::DFX_FAILED,
337             RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::CONCURRENT_ID, currentId,
338             PACKAGE_NAME, currentPid, DIS_SYNC_TIME, syncTime,
339             ERROR_CODE, ret, PASTEDATA_SUMMARY, pasteDataInfoSummary);
340     } else {
341         RADAR_REPORT(RadarReporter::DFX_GET_PASTEBOARD, bizStage, RadarReporter::DFX_CANCELLED,
342             RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::CONCURRENT_ID, currentId,
343             PACKAGE_NAME, currentPid, DIS_SYNC_TIME, syncTime,
344             ERROR_CODE, ret, PASTEDATA_SUMMARY, pasteDataInfoSummary);
345     }
346     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "leave, ret=%{public}d", ret);
347 }
348 
GetProgressByProgressInfo(std::shared_ptr<GetDataParams> params)349 void PasteboardClient::GetProgressByProgressInfo(std::shared_ptr<GetDataParams> params)
350 {
351     PASTEBOARD_CHECK_AND_RETURN_LOGE(params != nullptr, PASTEBOARD_MODULE_CLIENT, "params is null!");
352 
353     if (params->info == nullptr) {
354         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "params->info is null!");
355         return;
356     }
357     std::unique_lock<std::mutex> lock(instanceLock_);
358     std::string progressKey = g_progressKey;
359     lock.unlock();
360     std::string currentValue = std::to_string(params->info->percentage);
361     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "pasteboard progress percent = %{public}s", currentValue.c_str());
362     PasteBoardProgress::UpdateValue(progressKey, currentValue);
363 }
364 
SetProgressWithoutFile(std::string & progressKey,std::shared_ptr<GetDataParams> params)365 int32_t PasteboardClient::SetProgressWithoutFile(std::string &progressKey, std::shared_ptr<GetDataParams> params)
366 {
367     int progressValue = PASTEBOARD_PROGRESS_TWENTY_PERCENT;
368     while (progressValue < PASTEBOARD_PROGRESS_FINISH_PERCENT && !remoteTask_.load()) {
369         uint64_t currentTimeMicros = PasteBoardTime::GetCurrentTimeMicros();
370         if (currentTimeMicros >= progressStartTime_) {
371             uint64_t duration = currentTimeMicros - progressStartTime_;
372             if (duration >= (HAP_PULL_UP_TIME + HAP_MIN_SHOW_TIME) || duration < HAP_PULL_UP_TIME) {
373                 UpdateProgress(params, PASTEBOARD_PROGRESS_FINISH_PERCENT);
374                 break;
375             }
376         }
377         if (ProgressSignalClient::GetInstance().CheckCancelIfNeed()) {
378             PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "progress cancel success!");
379             return static_cast<int32_t>(PasteboardError::E_OK);
380         }
381         std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_WITHOUT_FILE));
382         progressValue += UPDATE_PERCENT_WITHOUT_FILE;
383         UpdateProgress(params, progressValue);
384     }
385     return static_cast<int32_t>(PasteboardError::E_OK);
386 }
387 
ProgressSmoothToTwentyPercent(PasteData & pasteData,std::string & progressKey,std::shared_ptr<GetDataParams> params)388 void PasteboardClient::ProgressSmoothToTwentyPercent(PasteData &pasteData, std::string &progressKey,
389     std::shared_ptr<GetDataParams> params)
390 {
391     if (pasteData.GetRecordCount() <= 0) {
392         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "no pasteData, progress no need to twenty");
393         return;
394     }
395     int progressValue = 0;
396     bool hasUri = (pasteData.GetPrimaryUri() != nullptr);
397     while (progressValue < PASTEBOARD_PROGRESS_TWENTY_PERCENT && !remoteTask_.load()) {
398         uint64_t currentTimeMicros = PasteBoardTime::GetCurrentTimeMicros();
399         if (currentTimeMicros >= progressStartTime_) {
400             uint64_t duration = currentTimeMicros - progressStartTime_;
401             if (duration >= (HAP_PULL_UP_TIME + HAP_MIN_SHOW_TIME) || duration < HAP_PULL_UP_TIME) {
402                 UpdateProgress(
403                     params, hasUri ? PASTEBOARD_PROGRESS_TWENTY_PERCENT : PASTEBOARD_PROGRESS_FINISH_PERCENT);
404                 break;
405             }
406         }
407         if (ProgressSignalClient::GetInstance().CheckCancelIfNeed()) {
408             return;
409         }
410         std::this_thread::sleep_for(std::chrono::milliseconds(PASTEBOARD_PROGRESS_SLEEP_TIME));
411         progressValue += PASTEBOARD_PROGRESS_UPDATE_PERCENT;
412         UpdateProgress(params, progressValue);
413     }
414 }
415 
UpdateProgress(std::shared_ptr<GetDataParams> params,int progressValue)416 void PasteboardClient::UpdateProgress(std::shared_ptr<GetDataParams> params, int progressValue)
417 {
418     if (params == nullptr) {
419         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "params is null!");
420         return;
421     }
422     if (params->info != nullptr) {
423         params->info->percentage = progressValue;
424     }
425     if (params->listener.ProgressNotify != nullptr) {
426         params->listener.ProgressNotify(params);
427     }
428 }
429 
OnProgressAbnormal(int32_t result)430 void PasteboardClient::OnProgressAbnormal(int32_t result)
431 {
432     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "The progress is reported abnormal.");
433     remoteTask_.store(true);
434 }
435 
GetPasteDataFromService(PasteData & pasteData,PasteDataFromServiceInfo & pasteDataFromServiceInfo,std::string progressKey,std::shared_ptr<GetDataParams> params)436 int32_t PasteboardClient::GetPasteDataFromService(PasteData &pasteData,
437     PasteDataFromServiceInfo &pasteDataFromServiceInfo, std::string progressKey, std::shared_ptr<GetDataParams> params)
438 {
439     auto proxyService = GetPasteboardService();
440     std::string pasteDataInfoSummary = GetPasteDataInfoSummary(pasteData);
441     if (proxyService == nullptr) {
442         RADAR_REPORT(RadarReporter::DFX_GET_PASTEBOARD, RadarReporter::DFX_CHECK_GET_SERVER, RadarReporter::DFX_FAILED,
443             RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::CONCURRENT_ID,
444             pasteDataFromServiceInfo.currentId, PACKAGE_NAME, pasteDataFromServiceInfo.currentPid,
445             ERROR_CODE, static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR),
446             PASTEDATA_SUMMARY, pasteDataInfoSummary);
447         return static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR);
448     }
449     int32_t syncTime = 0;
450     int fd = -1;
451     int64_t rawDataSize = 0;
452     std::vector<uint8_t> recvTLV(0);
453     int32_t ret = proxyService->GetPasteData(fd, rawDataSize, recvTLV, pasteData.GetPasteId(), syncTime);
454     int32_t bizStage = (syncTime == 0) ? RadarReporter::DFX_LOCAL_PASTE_END : RadarReporter::DFX_DISTRIBUTED_PASTE_END;
455     ret = ConvertErrCode(ret);
456     int32_t result = ProcessPasteData<PasteData>(pasteData, rawDataSize, fd, recvTLV);
457     ProgressSmoothToTwentyPercent(pasteData, progressKey, params);
458     PasteboardWebController::GetInstance().RetainUri(pasteData);
459     if (ret != static_cast<int32_t>(PasteboardError::E_OK)) {
460         ProcessRadarReport(ret, pasteData, pasteDataFromServiceInfo, syncTime);
461         return ret;
462     } else if (result == static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR)) {
463         ProcessRadarReport(result, pasteData, pasteDataFromServiceInfo, syncTime);
464         return result;
465     }
466     ProcessRadarReport(ret, pasteData, pasteDataFromServiceInfo, syncTime);
467     return static_cast<int32_t>(PasteboardError::E_OK);
468 }
469 
470 template<typename T>
ProcessPasteData(T & data,int64_t rawDataSize,int fd,const std::vector<uint8_t> & recvTLV)471 int32_t PasteboardClient::ProcessPasteData(T &data, int64_t rawDataSize, int fd,
472     const std::vector<uint8_t> &recvTLV)
473 {
474     int32_t ret = static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR);
475     if (fd < 0) {
476         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "fail fd:%{public}d", fd);
477         return ret;
478     }
479     MessageParcelWarp messageReply;
480     if (rawDataSize <= 0 || rawDataSize > messageReply.GetRawDataSize()) {
481         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Invalid raw data size:%{public}" PRId64, rawDataSize);
482         CloseSharedMemFd(fd);
483         return ret;
484     }
485     bool result = false;
486     MessageParcel parcelData;
487     if (rawDataSize > MIN_ASHMEM_DATA_SIZE) {
488         parcelData.WriteInt64(rawDataSize);
489         parcelData.WriteFileDescriptor(fd);
490         CloseSharedMemFd(fd);
491         const uint8_t *rawData =
492             reinterpret_cast<const uint8_t *>(messageReply.ReadRawData(parcelData, static_cast<size_t>(rawDataSize)));
493         if (rawData == nullptr) {
494             PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "mmap failed, size=%{public}" PRId64, rawDataSize);
495             return ret;
496         }
497         std::vector<uint8_t> pasteDataTlv(rawData, rawData + rawDataSize);
498         result = data.Decode(pasteDataTlv);
499     } else {
500         result = data.Decode(recvTLV);
501         CloseSharedMemFd(fd);
502     }
503     if (!result) {
504         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to decode pastedata in TLV");
505         return ret;
506     }
507     return static_cast<int32_t>(PasteboardError::E_OK);
508 }
509 
ProcessRadarReport(int32_t ret,PasteData & pasteData,PasteDataFromServiceInfo & pasteDataFromServiceInfo,int32_t syncTime)510 void PasteboardClient::ProcessRadarReport(int32_t ret, PasteData &pasteData,
511     PasteDataFromServiceInfo &pasteDataFromServiceInfo, int32_t syncTime)
512 {
513     int32_t bizStage = (syncTime == 0) ? RadarReporter::DFX_LOCAL_PASTE_END : RadarReporter::DFX_DISTRIBUTED_PASTE_END;
514     static DeduplicateMemory<RadarReportIdentity> reportMemory(REPORT_DUPLICATE_TIMEOUT);
515     std::string pasteDataInfoSummary = GetPasteDataInfoSummary(pasteData);
516     if (ret == static_cast<int32_t>(PasteboardError::E_OK)) {
517         if (pasteData.deviceId_.empty()) {
518             RADAR_REPORT(RadarReporter::DFX_GET_PASTEBOARD, bizStage, RadarReporter::DFX_SUCCESS,
519                 RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::CONCURRENT_ID,
520                 pasteDataFromServiceInfo.currentId, PACKAGE_NAME, pasteDataFromServiceInfo.currentPid,
521                 DIS_SYNC_TIME, syncTime, PASTEDATA_SUMMARY,
522                 pasteDataInfoSummary);
523         } else {
524             RADAR_REPORT(RadarReporter::DFX_GET_PASTEBOARD, bizStage, RadarReporter::DFX_SUCCESS,
525                 RadarReporter::CONCURRENT_ID, pasteDataFromServiceInfo.currentId, PACKAGE_NAME,
526                 pasteDataFromServiceInfo.currentPid, DIS_SYNC_TIME, syncTime,
527                 PASTEDATA_SUMMARY, pasteDataInfoSummary);
528         }
529     } else if (ret != static_cast<int32_t>(PasteboardError::TASK_PROCESSING) &&
530                !reportMemory.IsDuplicate({.pid = pasteDataFromServiceInfo.pid, .errorCode = ret})) {
531         RADAR_REPORT(RadarReporter::DFX_GET_PASTEBOARD, bizStage, RadarReporter::DFX_FAILED, RadarReporter::BIZ_STATE,
532             RadarReporter::DFX_END, RadarReporter::CONCURRENT_ID, pasteDataFromServiceInfo.currentId,
533             PACKAGE_NAME, pasteDataFromServiceInfo.currentPid, DIS_SYNC_TIME, syncTime,
534             ERROR_CODE, ret, PASTEDATA_SUMMARY, pasteDataInfoSummary);
535     } else {
536         RADAR_REPORT(RadarReporter::DFX_GET_PASTEBOARD, bizStage, RadarReporter::DFX_CANCELLED,
537             RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::CONCURRENT_ID,
538             pasteDataFromServiceInfo.currentId, PACKAGE_NAME, pasteDataFromServiceInfo.currentPid,
539             DIS_SYNC_TIME, syncTime, ERROR_CODE, ret, PASTEDATA_SUMMARY,
540             pasteDataInfoSummary);
541     }
542 }
543 
ProgressRadarReport(PasteData & pasteData,PasteDataFromServiceInfo & pasteDataFromServiceInfo)544 void PasteboardClient::ProgressRadarReport(PasteData &pasteData, PasteDataFromServiceInfo &pasteDataFromServiceInfo)
545 {
546     pasteDataFromServiceInfo.pid = getpid();
547     pasteDataFromServiceInfo.currentPid = std::to_string(pasteDataFromServiceInfo.pid);
548     pasteDataFromServiceInfo.currentId = PasteData::CreatePasteId("GetDataWithProgress", getSequenceId_++);
549     pasteData.SetPasteId(pasteDataFromServiceInfo.currentId);
550     RADAR_REPORT(RadarReporter::DFX_GET_PASTEBOARD, RadarReporter::DFX_GET_BIZ_SCENE, RadarReporter::DFX_SUCCESS,
551         RadarReporter::BIZ_STATE, RadarReporter::DFX_BEGIN,
552         RadarReporter::CONCURRENT_ID, pasteDataFromServiceInfo.currentId,
553         PACKAGE_NAME, pasteDataFromServiceInfo.currentPid);
554 }
555 
ProgressAfterTwentyPercent(PasteData & pasteData,std::shared_ptr<GetDataParams> params,std::string progressKey)556 int32_t PasteboardClient::ProgressAfterTwentyPercent(PasteData &pasteData, std::shared_ptr<GetDataParams> params,
557     std::string progressKey)
558 {
559     if (pasteData.GetRecordCount() <= 0) {
560         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "no pasteData, no need progress");
561         return static_cast<int32_t>(PasteboardError::NO_DATA_ERROR);
562     }
563     int32_t ret = 0;
564     bool hasUri = (pasteData.GetPrimaryUri() != nullptr);
565     if (hasUri) {
566         ret = PasteBoardCopyFile::GetInstance().CopyPasteData(pasteData, params);
567     } else {
568         ret = SetProgressWithoutFile(progressKey, params);
569     }
570     return ret;
571 }
572 
CheckProgressParam(std::shared_ptr<GetDataParams> params)573 int32_t PasteboardClient::CheckProgressParam(std::shared_ptr<GetDataParams> params)
574 {
575     if (params == nullptr) {
576         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Invalid param!");
577         return static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR);
578     }
579     if (isPasting_) {
580         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "task copying!");
581         return static_cast<int32_t>(PasteboardError::TASK_PROCESSING);
582     }
583     ProgressSignalClient::GetInstance().Init();
584     return static_cast<int32_t>(PasteboardError::E_OK);
585 }
586 
GetDataWithProgress(PasteData & pasteData,std::shared_ptr<GetDataParams> params)587 int32_t PasteboardClient::GetDataWithProgress(PasteData &pasteData, std::shared_ptr<GetDataParams> params)
588 {
589     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "GetDataWithProgress start.");
590     int32_t ret = CheckProgressParam(params);
591     if (ret != static_cast<int32_t>(PasteboardError::E_OK)) {
592         return ret;
593     }
594     progressStartTime_ = PasteBoardTime::GetCurrentTimeMicros();
595     isPasting_.store(true);
596     std::string progressKey;
597     std::string keyDefaultValue = "0";
598     std::shared_ptr<FFRTTimer> ffrtTimer;
599     ffrtTimer = std::make_shared<FFRTTimer>("pasteboard_progress");
600     if (params->progressIndicator != NONE_PROGRESS_INDICATOR) {
601         PasteBoardProgress::InsertValue(progressKey, keyDefaultValue); // 0%
602         std::unique_lock<std::mutex> lock(instanceLock_);
603         g_progressKey = progressKey;
604         lock.unlock();
605         params->listener.ProgressNotify = GetProgressByProgressInfo;
606         if (ffrtTimer != nullptr) {
607             FFRTTask task = [this, progressKey] {
608                 ShowProgress(progressKey);
609             };
610             ffrtTimer->SetTimer(progressKey, task, HAP_PULL_UP_TIME);
611         }
612     }
613     PasteDataFromServiceInfo pasteDataFromServiceInfo;
614     ProgressRadarReport(pasteData, pasteDataFromServiceInfo);
615     StartAsyncTrace(HITRACE_TAG_MISC, "PasteboardClient::GetDataWithProgress", HITRACE_GETPASTEDATA);
616     ret = GetPasteDataFromService(pasteData, pasteDataFromServiceInfo, progressKey, params);
617     if (ret != static_cast<int32_t>(PasteboardError::E_OK)) {
618         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "GetPasteDataFromService is failed: ret=%{public}d.", ret);
619         remoteTask_.store(false);
620         isPasting_.store(false);
621         return ret;
622     }
623     FinishAsyncTrace(HITRACE_TAG_MISC, "PasteboardClient::GetDataWithProgress", HITRACE_GETPASTEDATA);
624     ret = ProgressAfterTwentyPercent(pasteData, params, progressKey);
625     PasteboardWebController::GetInstance().RebuildWebviewPasteData(pasteData);
626     if (ffrtTimer != nullptr) {
627         ffrtTimer->CancelTimer(progressKey);
628     }
629     if (remoteTask_.load()) {
630         ret = static_cast<int32_t>(PasteboardError::PROGRESS_ABNORMAL);
631         remoteTask_.store(false);
632     }
633     isPasting_.store(false);
634     return ret;
635 }
636 
GetUnifiedDataWithProgress(UDMF::UnifiedData & unifiedData,std::shared_ptr<GetDataParams> params)637 int32_t PasteboardClient::GetUnifiedDataWithProgress(UDMF::UnifiedData &unifiedData,
638     std::shared_ptr<GetDataParams> params)
639 {
640     StartAsyncTrace(HITRACE_TAG_MISC, "PasteboardClient::GetUnifiedDataWithProgress", HITRACE_GETPASTEDATA);
641     PasteData pasteData;
642     int32_t ret = GetDataWithProgress(pasteData, params);
643     unifiedData = *(ConvertUtils::Convert(pasteData));
644     FinishAsyncTrace(HITRACE_TAG_MISC, "PasteboardClient::GetUnifiedDataWithProgress", HITRACE_GETPASTEDATA);
645     return ret;
646 }
647 
GetUnifiedData(UDMF::UnifiedData & unifiedData)648 int32_t PasteboardClient::GetUnifiedData(UDMF::UnifiedData &unifiedData)
649 {
650     StartAsyncTrace(HITRACE_TAG_MISC, "PasteboardClient::GetUnifiedData", HITRACE_GETPASTEDATA);
651     PasteData pasteData;
652     int32_t ret = GetPasteData(pasteData);
653     unifiedData = *(PasteboardUtils::GetInstance().Convert(pasteData));
654     FinishAsyncTrace(HITRACE_TAG_MISC, "PasteboardClient::GetUnifiedData", HITRACE_GETPASTEDATA);
655     return ret;
656 }
657 
GetUdsdData(UDMF::UnifiedData & unifiedData)658 int32_t PasteboardClient::GetUdsdData(UDMF::UnifiedData &unifiedData)
659 {
660     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "enter");
661     StartAsyncTrace(HITRACE_TAG_MISC, "PasteboardClient::GetUdsdData", HITRACE_GETPASTEDATA);
662     PasteData pasteData;
663     int32_t ret = GetPasteData(pasteData);
664     unifiedData = *(ConvertUtils::Convert(pasteData));
665     FinishAsyncTrace(HITRACE_TAG_MISC, "PasteboardClient::GetUdsdData", HITRACE_GETPASTEDATA);
666     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "leave, ret=%{public}d", ret);
667     return ret;
668 }
669 
HasPasteData()670 bool PasteboardClient::HasPasteData()
671 {
672     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "HasPasteData start.");
673     auto proxyService = GetPasteboardService();
674     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr, false,
675         PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
676     bool ret = false;
677     int32_t errCode = proxyService->HasPasteData(ret);
678     if (errCode != ERR_OK) {
679         return false;
680     }
681     return ret;
682 }
683 
CreateGetterAgent(sptr<PasteboardDelayGetterClient> & delayGetterAgent,std::shared_ptr<PasteboardDelayGetter> & delayGetter,sptr<PasteboardEntryGetterClient> & entryGetterAgent,std::map<uint32_t,std::shared_ptr<UDMF::EntryGetter>> & entryGetters,PasteData & pasteData)684 void PasteboardClient::CreateGetterAgent(sptr<PasteboardDelayGetterClient> &delayGetterAgent,
685     std::shared_ptr<PasteboardDelayGetter> &delayGetter, sptr<PasteboardEntryGetterClient> &entryGetterAgent,
686     std::map<uint32_t, std::shared_ptr<UDMF::EntryGetter>> &entryGetters, PasteData &pasteData)
687 {
688     if (delayGetter != nullptr) {
689         pasteData.SetDelayData(true);
690         delayGetterAgent = new (std::nothrow) PasteboardDelayGetterClient(delayGetter);
691     }
692     if (!(entryGetters.empty())) {
693         pasteData.SetDelayRecord(true);
694         entryGetterAgent = new (std::nothrow) PasteboardEntryGetterClient(entryGetters);
695     }
696     if (pasteData.IsDelayData() && delayGetterAgent == nullptr) {
697         pasteData.SetDelayData(false);
698     }
699     if (pasteData.IsDelayRecord() && entryGetterAgent == nullptr) {
700         pasteData.SetDelayRecord(false);
701     }
702 }
703 
WritePasteData(PasteData & pasteData,std::vector<uint8_t> & buffer,int & fd,int64_t & tlvSize,MessageParcelWarp & messageData,MessageParcel & parcelPata)704 int32_t PasteboardClient::WritePasteData(PasteData &pasteData, std::vector<uint8_t> &buffer, int &fd,
705     int64_t &tlvSize, MessageParcelWarp &messageData, MessageParcel &parcelPata)
706 {
707     std::vector<uint8_t> pasteDataTlv(0);
708     bool result = pasteData.Encode(pasteDataTlv);
709     if (!result) {
710         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "paste data encode failed.");
711         return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
712     }
713     tlvSize = static_cast<int64_t>(pasteDataTlv.size());
714     if (tlvSize > MIN_ASHMEM_DATA_SIZE) {
715         if (!messageData.WriteRawData(parcelPata, pasteDataTlv.data(), pasteDataTlv.size())) {
716             PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to WriteRawData");
717             return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
718         }
719         fd = messageData.GetWriteDataFd();
720         pasteDataTlv.clear();
721     } else {
722         fd = messageData.CreateTmpFd();
723         if (fd < 0) {
724             PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Failed to create tmp fd");
725             return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
726         }
727     }
728     buffer = std::move(pasteDataTlv);
729     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "set: fd:%{public}d, size:%{public}" PRId64, fd, tlvSize);
730     return static_cast<int32_t>(PasteboardError::E_OK);
731 }
732 
SetPasteData(PasteData & pasteData,std::shared_ptr<PasteboardDelayGetter> delayGetter,std::map<uint32_t,std::shared_ptr<UDMF::EntryGetter>> entryGetters)733 int32_t PasteboardClient::SetPasteData(PasteData &pasteData, std::shared_ptr<PasteboardDelayGetter> delayGetter,
734     std::map<uint32_t, std::shared_ptr<UDMF::EntryGetter>> entryGetters)
735 {
736     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "enter");
737     RADAR_REPORT(RadarReporter::DFX_SET_PASTEBOARD, RadarReporter::DFX_SET_BIZ_SCENE, RadarReporter::DFX_SUCCESS,
738         RadarReporter::BIZ_STATE, RadarReporter::DFX_BEGIN);
739     auto proxyService = GetPasteboardService();
740     if (proxyService == nullptr) {
741         RADAR_REPORT(RadarReporter::DFX_SET_PASTEBOARD, RadarReporter::DFX_CHECK_SET_SERVER, RadarReporter::DFX_FAILED,
742             RadarReporter::BIZ_STATE, RadarReporter::DFX_END, ERROR_CODE,
743             static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR));
744         return static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR);
745     }
746     sptr<PasteboardDelayGetterClient> delayGetterAgent;
747     sptr<PasteboardEntryGetterClient> entryGetterAgent;
748     CreateGetterAgent(delayGetterAgent, delayGetter, entryGetterAgent, entryGetters, pasteData);
749     std::vector<uint8_t> pasteDataTlv(0);
750     int fd = -1;
751     int64_t tlvSize = 0;
752     MessageParcelWarp messageData;
753     MessageParcel parcelPata;
754     int32_t ret = WritePasteData(pasteData, pasteDataTlv, fd, tlvSize, messageData, parcelPata);
755     if (ret != static_cast<int32_t>(PasteboardError::E_OK)) {
756         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Write data failed, size=%{public}" PRId64, tlvSize);
757         return ret;
758     }
759     if (delayGetterAgent != nullptr && entryGetterAgent != nullptr) {
760         ret = proxyService->SetPasteData(fd, tlvSize, pasteDataTlv, delayGetterAgent, entryGetterAgent);
761     } else if (delayGetterAgent != nullptr && entryGetterAgent == nullptr) {
762         ret = proxyService->SetPasteDataDelayData(fd, tlvSize, pasteDataTlv, delayGetterAgent);
763     } else if (delayGetterAgent == nullptr && entryGetterAgent != nullptr) {
764         ret = proxyService->SetPasteDataEntryData(fd, tlvSize, pasteDataTlv, entryGetterAgent);
765     } else {
766         ret = proxyService->SetPasteDataOnly(fd, tlvSize, pasteDataTlv);
767     }
768     std::string pasteDataInfoSummary = GetPasteDataInfoSummary(pasteData);
769     ret = ConvertErrCode(ret);
770     if (ret == static_cast<int32_t>(PasteboardError::E_OK)) {
771         RADAR_REPORT(RadarReporter::DFX_SET_PASTEBOARD, RadarReporter::DFX_SET_BIZ_SCENE, RadarReporter::DFX_SUCCESS,
772             RadarReporter::BIZ_STATE, RadarReporter::DFX_END, PASTEDATA_SUMMARY, pasteDataInfoSummary);
773     } else {
774         RADAR_REPORT(RadarReporter::DFX_SET_PASTEBOARD, RadarReporter::DFX_SET_BIZ_SCENE, RadarReporter::DFX_FAILED,
775             RadarReporter::BIZ_STATE, RadarReporter::DFX_END,
776             ERROR_CODE, ret, PASTEDATA_SUMMARY, pasteDataInfoSummary);
777     }
778     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "leave, ret=%{public}d", ret);
779     return ret;
780 }
781 
SetUnifiedData(const UDMF::UnifiedData & unifiedData,std::shared_ptr<PasteboardDelayGetter> delayGetter)782 int32_t PasteboardClient::SetUnifiedData(
783     const UDMF::UnifiedData &unifiedData, std::shared_ptr<PasteboardDelayGetter> delayGetter)
784 {
785     auto pasteData = PasteboardUtils::GetInstance().Convert(unifiedData);
786     return SetPasteData(*pasteData, delayGetter);
787 }
788 
SetUdsdData(const UDMF::UnifiedData & unifiedData)789 int32_t PasteboardClient::SetUdsdData(const UDMF::UnifiedData &unifiedData)
790 {
791     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "enter");
792     auto pasteData = ConvertUtils::Convert(unifiedData);
793     std::map<uint32_t, std::shared_ptr<UDMF::EntryGetter>> entryGetters;
794     for (auto record : unifiedData.GetRecords()) {
795         if (record != nullptr && record->GetEntryGetter() != nullptr) {
796             entryGetters.emplace(record->GetRecordId(), record->GetEntryGetter());
797         }
798     }
799     int32_t ret = SetPasteData(*pasteData, nullptr, entryGetters);
800     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "leave, ret=%{public}d", ret);
801     return ret;
802 }
803 
SubscribePasteboardSA()804 void PasteboardClient::SubscribePasteboardSA()
805 {
806     PASTEBOARD_CHECK_AND_RETURN_LOGD(
807         getuid() != ANCO_SERVICE_BROKER_UID, PASTEBOARD_MODULE_CLIENT, "ignore,uid:%{public}u.", getuid());
808     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
809     PASTEBOARD_CHECK_AND_RETURN_LOGE(samgrProxy != nullptr, PASTEBOARD_MODULE_CLIENT, "get samgr fail.");
810     std::lock_guard<std::mutex> lock(saListenerMutex_);
811     PASTEBOARD_CHECK_AND_RETURN_LOGD(!isSubscribeSa_, PASTEBOARD_MODULE_CLIENT, "already subscribe sa.");
812     if (saCallback_ == nullptr) {
813         saCallback_ = sptr<PasteboardSaMgrListener>::MakeSptr();
814     }
815     PASTEBOARD_CHECK_AND_RETURN_LOGE(saCallback_ != nullptr, PASTEBOARD_MODULE_CLIENT, "Create saCallback failed!");
816     auto ret = samgrProxy->SubscribeSystemAbility(PASTEBOARD_SERVICE_ID, saCallback_);
817     PASTEBOARD_CHECK_AND_RETURN_LOGE(
818         ret == ERR_OK, PASTEBOARD_MODULE_CLIENT, "subscribe pasteboard sa failed! ret %{public}d.", ret);
819     isSubscribeSa_ = true;
820 }
821 
UnSubscribePasteboardSA()822 void PasteboardClient::UnSubscribePasteboardSA()
823 {
824     PASTEBOARD_CHECK_AND_RETURN_LOGD(
825         getuid() != ANCO_SERVICE_BROKER_UID, PASTEBOARD_MODULE_CLIENT, "ignore,uid:%{public}u.", getuid());
826     std::lock_guard<std::mutex> lock(saListenerMutex_);
827     PASTEBOARD_CHECK_AND_RETURN_LOGD(saCallback_ != nullptr, PASTEBOARD_MODULE_CLIENT, "saCallback is nullptr");
828     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
829     auto tempCallback = saCallback_;
830     saCallback_ = nullptr;
831     isSubscribeSa_ = false;
832     PASTEBOARD_CHECK_AND_RETURN_LOGE(samgrProxy != nullptr, PASTEBOARD_MODULE_CLIENT, "get samgr fail");
833     int32_t ret = samgrProxy->UnSubscribeSystemAbility(PASTEBOARD_SERVICE_ID, tempCallback);
834     PASTEBOARD_CHECK_AND_RETURN_LOGE(
835         ret == ERR_OK, PASTEBOARD_MODULE_CLIENT, "unSubscribe pasteboard sa failed! ret %{public}d.", ret);
836 }
837 
ReleaseSaListener()838 void PasteboardClient::ReleaseSaListener()
839 {
840     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "start.");
841     UnSubscribePasteboardSA();
842     PasteboardServiceLoader::GetInstance().ReleaseDeathRecipient();
843 }
844 
Resubscribe()845 void PasteboardClient::Resubscribe()
846 {
847     auto proxyService = GetPasteboardService();
848     PASTEBOARD_CHECK_AND_RETURN_LOGE(proxyService != nullptr, PASTEBOARD_MODULE_CLIENT,
849         "proxyService is null");
850     std::lock_guard<std::mutex> lock(observerSetMutex_);
851     for (auto it = observerSet_.begin(); it != observerSet_.end(); ++it) {
852         proxyService->ResubscribeObserver(it->first, it->second);
853     }
854 }
855 
Subscribe(PasteboardObserverType type,sptr<PasteboardObserver> callback)856 bool PasteboardClient::Subscribe(PasteboardObserverType type, sptr<PasteboardObserver> callback)
857 {
858     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
859     if (callback == nullptr) {
860         PASTEBOARD_HILOGW(PASTEBOARD_MODULE_CLIENT, "callback is null");
861         return false;
862     }
863     auto proxyService = GetPasteboardService();
864     {
865         std::lock_guard<std::mutex> lock(observerSetMutex_);
866         observerSet_.insert(std::make_pair(type, callback));
867     }
868     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr, false,
869         PASTEBOARD_MODULE_CLIENT, "proxyService is null");
870     int32_t ret = proxyService->SubscribeObserver(type, callback);
871     SubscribePasteboardSA();
872     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(ret == ERR_OK, false, PASTEBOARD_MODULE_CLIENT,
873         "subscribe failed, ret=%{public}d", ret);
874     return true;
875 }
876 
AddPasteboardChangedObserver(sptr<PasteboardObserver> callback)877 void PasteboardClient::AddPasteboardChangedObserver(sptr<PasteboardObserver> callback)
878 {
879     Subscribe(PasteboardObserverType::OBSERVER_LOCAL, callback);
880 }
881 
AddPasteboardEventObserver(sptr<PasteboardObserver> callback)882 void PasteboardClient::AddPasteboardEventObserver(sptr<PasteboardObserver> callback)
883 {
884     Subscribe(PasteboardObserverType::OBSERVER_EVENT, callback);
885 }
886 
Unsubscribe(PasteboardObserverType type,sptr<PasteboardObserver> callback)887 void PasteboardClient::Unsubscribe(PasteboardObserverType type, sptr<PasteboardObserver> callback)
888 {
889     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "start.");
890     auto proxyService = GetPasteboardService();
891     PASTEBOARD_CHECK_AND_RETURN_LOGE(proxyService != nullptr, PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
892     if (callback == nullptr) {
893         PASTEBOARD_HILOGW(PASTEBOARD_MODULE_CLIENT, "remove all.");
894         {
895             std::lock_guard<std::mutex> lock(observerSetMutex_);
896             observerSet_.clear();
897         }
898         proxyService->UnsubscribeAllObserver(type);
899         UnSubscribePasteboardSA();
900         PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
901         return;
902     }
903     {
904         std::lock_guard<std::mutex> lock(observerSetMutex_);
905         observerSet_.erase(std::make_pair(type, callback));
906         if (observerSet_.size() == 0) {
907             UnSubscribePasteboardSA();
908         }
909     }
910     proxyService->UnsubscribeObserver(type, callback);
911     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "end.");
912 }
913 
RemovePasteboardChangedObserver(sptr<PasteboardObserver> callback)914 void PasteboardClient::RemovePasteboardChangedObserver(sptr<PasteboardObserver> callback)
915 {
916     Unsubscribe(PasteboardObserverType::OBSERVER_LOCAL, callback);
917 }
918 
RemovePasteboardEventObserver(sptr<PasteboardObserver> callback)919 void PasteboardClient::RemovePasteboardEventObserver(sptr<PasteboardObserver> callback)
920 {
921     Unsubscribe(PasteboardObserverType::OBSERVER_EVENT, callback);
922 }
923 
SubscribeDisposableObserver(const sptr<PasteboardDisposableObserver> & observer,const std::string & targetBundleName,DisposableType type,uint32_t maxLength)924 int32_t PasteboardClient::SubscribeDisposableObserver(const sptr<PasteboardDisposableObserver> &observer,
925     const std::string &targetBundleName, DisposableType type, uint32_t maxLength)
926 {
927     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(observer != nullptr,
928         static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR), PASTEBOARD_MODULE_CLIENT,
929         "param invalid, observer is null");
930     int32_t typeInt = static_cast<int32_t>(type);
931     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(typeInt >= 0 && typeInt < static_cast<int32_t>(DisposableType::MAX),
932         static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR), PASTEBOARD_MODULE_CLIENT,
933         "param invalid, type=%{public}d", typeInt);
934 
935     auto proxyService = GetPasteboardService();
936     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr,
937         static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR), PASTEBOARD_MODULE_CLIENT,
938         "proxyService is null");
939 
940     int32_t ret = proxyService->SubscribeDisposableObserver(observer, targetBundleName, type, maxLength);
941     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(ret == ERR_OK, ret, PASTEBOARD_MODULE_CLIENT,
942         "subscribe failed, ret=%{public}d", ret);
943     return ERR_OK;
944 }
945 
SetGlobalShareOption(const std::map<uint32_t,ShareOption> & globalShareOptions)946 int32_t PasteboardClient::SetGlobalShareOption(const std::map<uint32_t, ShareOption> &globalShareOptions)
947 {
948     auto proxyService = GetPasteboardService();
949     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr,
950         static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR),
951         PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
952     std::unordered_map<uint32_t, int32_t> shareOptions = {};
953     for (const auto &pair : globalShareOptions) {
954         shareOptions[pair.first] = static_cast<int32_t>(pair.second);
955     }
956     int32_t ret = proxyService->SetGlobalShareOption(shareOptions);
957     ret = ConvertErrCode(ret);
958     if (ret == static_cast<int32_t>(PasteboardError::E_OK)) {
959         ret = ERR_OK;
960     }
961     return ret;
962 }
963 
RemoveGlobalShareOption(const std::vector<uint32_t> & tokenIds)964 int32_t PasteboardClient::RemoveGlobalShareOption(const std::vector<uint32_t> &tokenIds)
965 {
966     auto proxyService = GetPasteboardService();
967     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr,
968         static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR),
969         PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
970     int32_t ret = proxyService->RemoveGlobalShareOption(tokenIds);
971     ret = ConvertErrCode(ret);
972     if (ret == static_cast<int32_t>(PasteboardError::E_OK)) {
973         ret = ERR_OK;
974     }
975     return ret;
976 }
977 
GetGlobalShareOption(const std::vector<uint32_t> & tokenIds)978 std::map<uint32_t, ShareOption> PasteboardClient::GetGlobalShareOption(const std::vector<uint32_t> &tokenIds)
979 {
980     auto proxyService = GetPasteboardService();
981     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr, {},
982         PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
983     std::unordered_map<uint32_t, int32_t> funcResult = {};
984     int32_t ret = proxyService->GetGlobalShareOption(tokenIds, funcResult);
985     if (ret != ERR_OK) {
986         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "GetGlobalShareOption failed, ret=%{public}d", ret);
987         return {};
988     }
989     std::map<uint32_t, ShareOption> result;
990     for (const auto &pair : funcResult) {
991         result[pair.first] = static_cast<ShareOption>(pair.second);
992     }
993     return result;
994 }
995 
SetAppShareOptions(const ShareOption & shareOptions)996 int32_t PasteboardClient::SetAppShareOptions(const ShareOption &shareOptions)
997 {
998     auto proxyService = GetPasteboardService();
999     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr,
1000         static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR),
1001         PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
1002     return proxyService->SetAppShareOptions(shareOptions);
1003 }
1004 
RemoveAppShareOptions()1005 int32_t PasteboardClient::RemoveAppShareOptions()
1006 {
1007     auto proxyService = GetPasteboardService();
1008     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr,
1009         static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR),
1010         PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
1011     int32_t ret = proxyService->RemoveAppShareOptions();
1012     ret = ConvertErrCode(ret);
1013     if (ret == static_cast<int32_t>(PasteboardError::E_OK)) {
1014         ret = ERR_OK;
1015     }
1016     return ret;
1017 }
1018 
IsRemoteData()1019 bool PasteboardClient::IsRemoteData()
1020 {
1021     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "IsRemoteData start.");
1022     auto proxyService = GetPasteboardService();
1023     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr, false,
1024         PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
1025     bool ret = false;
1026     int32_t retCode = proxyService->IsRemoteData(ret);
1027     if (retCode != ERR_OK) {
1028         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "IsRemoteData failed, retCode=%{public}d", retCode);
1029         return false;
1030     }
1031     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "IsRemoteData end.");
1032     return ret;
1033 }
1034 
GetDataSource(std::string & bundleName)1035 int32_t PasteboardClient::GetDataSource(std::string &bundleName)
1036 {
1037     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "GetDataSource start.");
1038     auto proxyService = GetPasteboardService();
1039     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr,
1040         static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR),
1041         PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
1042     int32_t ret = proxyService->GetDataSource(bundleName);
1043     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "GetDataSource end.");
1044     return ConvertErrCode(ret);
1045 }
1046 
GetMimeTypes()1047 std::vector<std::string> PasteboardClient::GetMimeTypes()
1048 {
1049     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "GetMimeTypes start.");
1050     auto proxyService = GetPasteboardService();
1051     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr, {},
1052         PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
1053     std::vector<std::string> mimeTypes = {};
1054     int32_t ret = proxyService->GetMimeTypes(mimeTypes);
1055     if (ret != ERR_OK) {
1056         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "GetMimeTypes failed, ret=%{public}d", ret);
1057         return {};
1058     }
1059     return mimeTypes;
1060 }
1061 
HasDataType(const std::string & mimeType)1062 bool PasteboardClient::HasDataType(const std::string &mimeType)
1063 {
1064     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "HasDataType start.");
1065     auto proxyService = GetPasteboardService();
1066     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr, false,
1067         PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
1068     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(!mimeType.empty(), false, PASTEBOARD_MODULE_CLIENT, "parameter is invalid");
1069     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "type is %{public}s", mimeType.c_str());
1070     bool ret = false;
1071     int32_t retCode = proxyService->HasDataType(mimeType, ret);
1072     if (retCode != ERR_OK) {
1073         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "HasDataType failed, retCode=%{public}d", retCode);
1074         return false;
1075     }
1076     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "HasDataType end.");
1077     return ret;
1078 }
1079 
DetectPatterns(const std::set<Pattern> & patternsToCheck)1080 std::set<Pattern> PasteboardClient::DetectPatterns(const std::set<Pattern> &patternsToCheck)
1081 {
1082     if (!PatternDetection::IsValid(patternsToCheck)) {
1083         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "Invalid number in Pattern set!");
1084         return {};
1085     }
1086 
1087     auto proxyService = GetPasteboardService();
1088     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(proxyService != nullptr, {},
1089         PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
1090     std::vector<Pattern> patterns(patternsToCheck.begin(), patternsToCheck.end());
1091     std::vector<Pattern> funcResult = {};
1092     int32_t ret = proxyService->DetectPatterns(patterns, funcResult);
1093     if (ret != ERR_OK) {
1094         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "DetectPatterns failed, ret=%{public}d", ret);
1095         return {};
1096     }
1097     std::set<Pattern> result(funcResult.begin(), funcResult.end());
1098     return result;
1099 }
1100 
GetPasteboardService()1101 sptr<IPasteboardService> PasteboardClient::GetPasteboardService()
1102 {
1103     return PasteboardServiceLoader::GetInstance().GetPasteboardService();
1104 }
1105 
PasteStart(const std::string & pasteId)1106 void PasteboardClient::PasteStart(const std::string &pasteId)
1107 {
1108     RADAR_REPORT(RadarReporter::DFX_GET_PASTEBOARD, RadarReporter::DFX_DISTRIBUTED_FILE_START,
1109         RadarReporter::DFX_SUCCESS, RadarReporter::CONCURRENT_ID, pasteId);
1110     auto proxyService = GetPasteboardService();
1111     PASTEBOARD_CHECK_AND_RETURN_LOGE(proxyService != nullptr, PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
1112     proxyService->PasteStart(pasteId);
1113 }
1114 
PasteComplete(const std::string & deviceId,const std::string & pasteId)1115 void PasteboardClient::PasteComplete(const std::string &deviceId, const std::string &pasteId)
1116 {
1117     auto proxyService = GetPasteboardService();
1118     PASTEBOARD_CHECK_AND_RETURN_LOGE(proxyService != nullptr, PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
1119     proxyService->PasteComplete(deviceId, pasteId);
1120 }
1121 
HandleSignalValue(const std::string & signalValue)1122 int32_t PasteboardClient::HandleSignalValue(const std::string &signalValue)
1123 {
1124     int32_t progressStatusValue = 0;
1125     std::shared_ptr<ProgressReportListener> progressReport = std::make_shared<ProgressReportListener>();
1126     progressReport->OnProgressFail = OnProgressAbnormal;
1127 
1128     static const std::regex numberRegex(R"(^[+-]?\d+$)");
1129     if (!std::regex_match(signalValue, numberRegex)) {
1130         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "progressStatusValue invalid = %{public}s", signalValue.c_str());
1131         return static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR);
1132     }
1133     auto ret = std::from_chars(signalValue.data(), signalValue.data() + signalValue.size(), progressStatusValue);
1134     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(ret.ec == std::errc(),
1135         static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR), PASTEBOARD_MODULE_CLIENT,
1136         "progress invalid status: %{public}s", signalValue.c_str());
1137 
1138     if (progressStatusValue == NORMAL_PASTE) {
1139         return static_cast<int32_t>(PasteboardError::E_OK);
1140     }
1141     if (progressStatusValue == CANCEL_PASTE) {
1142         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "progress cancel paste");
1143         ProgressSignalClient::GetInstance().Cancel();
1144     } else if (progressStatusValue == PASTE_TIME_OUT) {
1145         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "progress time out");
1146         progressReport->OnProgressFail(static_cast<int32_t>(PasteboardError::PROGRESS_PASTE_TIME_OUT));
1147     } else {
1148         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "progress invalid status: %{public}s", signalValue.c_str());
1149         progressReport->OnProgressFail(static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR));
1150     }
1151     return static_cast<int32_t>(PasteboardError::E_OK);
1152 }
1153 
ShowProgress(const std::string & progressKey)1154 void PasteboardClient::ShowProgress(const std::string &progressKey)
1155 {
1156     auto proxyService = GetPasteboardService();
1157     PASTEBOARD_CHECK_AND_RETURN_LOGE(proxyService != nullptr, PASTEBOARD_MODULE_CLIENT, "proxyService is nullptr");
1158     sptr<PasteboardSignalCallback> callback = new PasteboardSignalCallback();
1159     proxyService->ShowProgress(progressKey, callback);
1160 }
1161 
GetPasteDataInfoSummary(const PasteData & pasteData)1162 std::string PasteboardClient::GetPasteDataInfoSummary(const PasteData &pasteData)
1163 {
1164     // Deal with pasteData info
1165     json RadarReportInfoInJson = {
1166         {"PasteBundle", pasteData.GetBundleName().empty() ? "/" : pasteData.GetBundleName()},
1167         {"PasteDataSize", pasteData.CountTLV()},
1168         {"RecordCount", pasteData.GetRecordCount()},
1169         {"IsRemote", pasteData.IsRemote()},
1170         {"IsDelayData", pasteData.IsDelayData()},
1171         {"IsDelayRecord", pasteData.IsDelayRecord()}
1172     };
1173 
1174     // Deal with Records info
1175     RadarReportInfoInJson["recordList"] = json::array();
1176     for (size_t i = 0; i < pasteData.GetRecordCount(); ++i) {
1177         // set a record display upperbound
1178         if (i >= RECORD_DISPLAY_UPPERBOUND) {
1179             break;
1180         }
1181         auto record = pasteData.GetRecordAt(i);
1182         if (record == nullptr) {
1183             break;
1184         }
1185         json recordInfo = {
1186             {"PrimaryType", record->GetMimeType()},
1187             {"MimeTypes", record->GetMimeTypes()}
1188         };
1189         RadarReportInfoInJson["recordList"].emplace_back(recordInfo);
1190     }
1191 
1192     // To string and return
1193     return RadarReportInfoInJson.dump(JSON_INDENT);
1194 }
1195 
ConvertErrCode(int32_t errCode)1196 int32_t PasteboardClient::ConvertErrCode(int32_t errCode)
1197 {
1198     switch (errCode) {
1199         case ERR_INVALID_VALUE: // fall-through
1200         case ERR_INVALID_DATA:
1201             return static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR);
1202         case ERR_OK:
1203             return static_cast<int32_t>(PasteboardError::E_OK);
1204         default:
1205             return errCode;
1206     }
1207 }
1208 } // namespace MiscServices
1209 } // namespace OHOS
1210