• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (C) 2021 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "paste_data.h"
18 
19 #include "int_wrapper.h"
20 #include "long_wrapper.h"
21 #include "pasteboard_common.h"
22 #include "pasteboard_hilog.h"
23 
24 using namespace std::chrono;
25 using namespace OHOS::Media;
26 
27 namespace OHOS {
28 namespace MiscServices {
29 enum TAG_PASTEBOARD : uint16_t {
30     TAG_PROPS = TAG_BUFF + 1,
31     TAG_RECORDS,
32     TAG_DRAGGED_DATA_FLAG,
33     TAG_LOCAL_PASTE_FLAG,
34     TAG_DELAY_DATA_FLAG,
35     TAG_DEVICE_ID,
36     TAG_PASTE_ID,
37     TAG_DELAY_RECORD_FLAG,
38     TAG_DATA_ID,
39     TAG_RECORD_ID,
40 };
41 enum TAG_PROPERTY : uint16_t {
42     TAG_ADDITIONS = TAG_BUFF + 1,
43     TAG_MIMETYPES,
44     TAG_TAG,
45     TAG_LOCAL_ONLY,
46     TAG_TIMESTAMP,
47     TAG_SHAREOPTION,
48     TAG_TOKENID,
49     TAG_ISREMOTE,
50     TAG_BUNDLENAME,
51     TAG_SETTIME,
52     TAG_SCREENSTATUS,
53 };
54 
55 std::string PasteData::sharePath = "";
56 std::string PasteData::WEBVIEW_PASTEDATA_TAG = "WebviewPasteDataTag";
57 const std::string PasteData::DISTRIBUTEDFILES_TAG = "distributedfiles";
58 const std::string PasteData::PATH_SHARE = "/data/storage/el2/share/r/";
59 const std::string PasteData::IMG_LOCAL_URI = "file:///";
60 const std::string PasteData::SHARE_PATH_PREFIX = "/mnt/hmdfs/";
61 const std::string PasteData::SHARE_PATH_PREFIX_ACCOUNT = "/account/merge_view/services/";
62 const std::string PasteData::DOCS_LOCAL_TAG = "/docs/";
63 const char *REMOTE_FILE_SIZE = "remoteFileSize";
64 const char *REMOTE_FILE_SIZE_LONG = "remoteFileSizeLong";
65 
PasteData()66 PasteData::PasteData()
67 {
68     props_.timestamp = steady_clock::now().time_since_epoch().count();
69     props_.localOnly = false;
70     props_.shareOption = ShareOption::CrossDevice;
71     InitDecodeMap();
72 }
73 
~PasteData()74 PasteData::~PasteData()
75 {
76     records_.clear();
77     decodeMap_.clear();
78 }
79 
PasteData(const PasteData & data)80 PasteData::PasteData(const PasteData &data)
81     : originAuthority_(data.originAuthority_), valid_(data.valid_), isDraggedData_(data.isDraggedData_),
82       isLocalPaste_(data.isLocalPaste_), isDelayData_(data.isDelayData_), pasteId_(data.pasteId_),
83       isDelayRecord_(data.isDelayRecord_), dataId_(data.dataId_), recordId_(data.recordId_)
84 {
85     this->props_ = data.props_;
86     for (const auto &item : data.records_) {
87         this->records_.emplace_back(std::make_shared<PasteDataRecord>(*item));
88     }
89     InitDecodeMap();
90 }
91 
PasteData(std::vector<std::shared_ptr<PasteDataRecord>> records)92 PasteData::PasteData(std::vector<std::shared_ptr<PasteDataRecord>> records) : records_{ std::move(records) }
93 {
94     for (const auto &item : records_) {
95         item->SetRecordId(++recordId_);
96     }
97     props_.timestamp = steady_clock::now().time_since_epoch().count();
98     props_.localOnly = false;
99     props_.shareOption = ShareOption::CrossDevice;
100     InitDecodeMap();
101 }
102 
operator =(const PasteData & data)103 PasteData &PasteData::operator=(const PasteData &data)
104 {
105     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "PasteData copy");
106     if (this == &data) {
107         return *this;
108     }
109     this->originAuthority_ = data.originAuthority_;
110     this->valid_ = data.valid_;
111     this->isDraggedData_ = data.isDraggedData_;
112     this->isLocalPaste_ = data.isLocalPaste_;
113     this->isDelayData_ = data.isDelayData_;
114     this->isDelayRecord_ = data.isDelayRecord_;
115     this->dataId_ = data.dataId_;
116     this->props_ = data.props_;
117     this->records_.clear();
118     this->deviceId_ = data.deviceId_;
119     this->pasteId_ = data.pasteId_;
120     for (const auto &item : data.records_) {
121         this->records_.emplace_back(std::make_shared<PasteDataRecord>(*item));
122     }
123     this->recordId_ = data.GetRecordId();
124     return *this;
125 }
126 
GetProperty() const127 PasteDataProperty PasteData::GetProperty() const
128 {
129     return PasteDataProperty(props_);
130 }
131 
SetProperty(const PasteDataProperty & property)132 void PasteData::SetProperty(const PasteDataProperty &property)
133 {
134     this->props_ = property;
135 }
136 
AddHtmlRecord(const std::string & html)137 void PasteData::AddHtmlRecord(const std::string &html)
138 {
139     this->AddRecord(PasteDataRecord::NewHtmlRecord(html));
140 }
141 
AddPixelMapRecord(std::shared_ptr<PixelMap> pixelMap)142 void PasteData::AddPixelMapRecord(std::shared_ptr<PixelMap> pixelMap)
143 {
144     this->AddRecord(PasteDataRecord::NewPixelMapRecord(std::move(pixelMap)));
145 }
146 
AddWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want)147 void PasteData::AddWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want)
148 {
149     this->AddRecord(PasteDataRecord::NewWantRecord(std::move(want)));
150 }
151 
AddTextRecord(const std::string & text)152 void PasteData::AddTextRecord(const std::string &text)
153 {
154     this->AddRecord(PasteDataRecord::NewPlainTextRecord(text));
155 }
156 
AddUriRecord(const OHOS::Uri & uri)157 void PasteData::AddUriRecord(const OHOS::Uri &uri)
158 {
159     this->AddRecord(PasteDataRecord::NewUriRecord(uri));
160 }
161 
AddKvRecord(const std::string & mimeType,const std::vector<uint8_t> & arrayBuffer)162 void PasteData::AddKvRecord(const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer)
163 {
164     AddRecord(PasteDataRecord::NewKvRecord(mimeType, arrayBuffer));
165 }
166 
AddRecord(std::shared_ptr<PasteDataRecord> record)167 void PasteData::AddRecord(std::shared_ptr<PasteDataRecord> record)
168 {
169     PASTEBOARD_CHECK_AND_RETURN_LOGE(record != nullptr, PASTEBOARD_MODULE_CLIENT, "record is null");
170     record->SetRecordId(++recordId_);
171 
172     if (PasteBoardCommon::IsPasteboardService()) {
173         props_.mimeTypes.emplace_back(record->GetMimeType());
174         records_.emplace_back(std::move(record));
175     } else {
176         props_.mimeTypes.insert(props_.mimeTypes.begin(), record->GetMimeType());
177         records_.insert(records_.begin(), std::move(record));
178     }
179 }
180 
AddRecord(const PasteDataRecord & record)181 void PasteData::AddRecord(const PasteDataRecord &record)
182 {
183     this->AddRecord(std::make_shared<PasteDataRecord>(record));
184 }
185 
GetMimeTypes()186 std::vector<std::string> PasteData::GetMimeTypes()
187 {
188     std::set<std::string> mimeTypes;
189     for (const auto &item : records_) {
190         if (item->GetFrom() > 0 && item->GetRecordId() != item->GetFrom()) {
191             continue;
192         }
193         auto itemTypes = item->GetMimeTypes();
194         mimeTypes.insert(itemTypes.begin(), itemTypes.end());
195     }
196     return std::vector<std::string>(mimeTypes.begin(), mimeTypes.end());
197 }
198 
GetPrimaryHtml()199 std::shared_ptr<std::string> PasteData::GetPrimaryHtml()
200 {
201     for (const auto &item : records_) {
202         std::shared_ptr<std::string> primary = item->GetHtmlText();
203         if (primary) {
204             return primary;
205         }
206     }
207     return nullptr;
208 }
209 
GetPrimaryPixelMap()210 std::shared_ptr<PixelMap> PasteData::GetPrimaryPixelMap()
211 {
212     for (const auto &item : records_) {
213         std::shared_ptr<PixelMap> primary = item->GetPixelMap();
214         if (primary) {
215             return primary;
216         }
217     }
218     return nullptr;
219 }
220 
GetPrimaryWant()221 std::shared_ptr<OHOS::AAFwk::Want> PasteData::GetPrimaryWant()
222 {
223     for (const auto &item : records_) {
224         std::shared_ptr<OHOS::AAFwk::Want> primary = item->GetWant();
225         if (primary) {
226             return primary;
227         }
228     }
229     return nullptr;
230 }
231 
GetPrimaryText()232 std::shared_ptr<std::string> PasteData::GetPrimaryText()
233 {
234     for (const auto &item : records_) {
235         std::shared_ptr<std::string> primary = item->GetPlainText();
236         if (primary) {
237             return primary;
238         }
239     }
240     return nullptr;
241 }
242 
GetPrimaryUri()243 std::shared_ptr<OHOS::Uri> PasteData::GetPrimaryUri()
244 {
245     for (const auto &item : records_) {
246         std::shared_ptr<OHOS::Uri> primary = item->GetUri();
247         if (primary) {
248             return primary;
249         }
250     }
251     return nullptr;
252 }
253 
GetPrimaryMimeType()254 std::shared_ptr<std::string> PasteData::GetPrimaryMimeType()
255 {
256     if (records_.empty()) {
257         return nullptr;
258     }
259     return std::make_shared<std::string>(records_.front()->GetMimeType());
260 }
261 
GetRecordAt(std::size_t index) const262 std::shared_ptr<PasteDataRecord> PasteData::GetRecordAt(std::size_t index) const
263 {
264     if (records_.size() > index) {
265         return records_[index];
266     } else {
267         return nullptr;
268     }
269 }
270 
GetRecordById(uint32_t recordId) const271 std::shared_ptr<PasteDataRecord> PasteData::GetRecordById(uint32_t recordId) const
272 {
273     for (const auto &record : records_) {
274         if (record != nullptr && record->GetRecordId() == recordId) {
275             return record;
276         }
277     }
278     return nullptr;
279 }
280 
GetRecordCount() const281 std::size_t PasteData::GetRecordCount() const
282 {
283     return records_.size();
284 }
285 
GetShareOption()286 ShareOption PasteData::GetShareOption()
287 {
288     return props_.shareOption;
289 }
290 
SetShareOption(ShareOption shareOption)291 void PasteData::SetShareOption(ShareOption shareOption)
292 {
293     props_.shareOption = shareOption;
294     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "shareOption = %{public}d.", shareOption);
295 }
296 
GetTokenId()297 std::uint32_t PasteData::GetTokenId()
298 {
299     return props_.tokenId;
300 }
301 
SetTokenId(uint32_t tokenId)302 void PasteData::SetTokenId(uint32_t tokenId)
303 {
304     props_.tokenId = tokenId;
305 }
306 
RemoveRecordAt(std::size_t number)307 bool PasteData::RemoveRecordAt(std::size_t number)
308 {
309     if (records_.size() > number) {
310         records_.erase(records_.begin() + static_cast<std::int64_t>(number));
311         RefreshMimeProp();
312         return true;
313     } else {
314         return false;
315     }
316 }
317 
ReplaceRecordAt(std::size_t number,std::shared_ptr<PasteDataRecord> record)318 bool PasteData::ReplaceRecordAt(std::size_t number, std::shared_ptr<PasteDataRecord> record)
319 {
320     if (record == nullptr) {
321         return false;
322     }
323     if (records_.size() > number) {
324         records_[number] = std::move(record);
325         RefreshMimeProp();
326         return true;
327     } else {
328         return false;
329     }
330 }
331 
HasMimeType(const std::string & mimeType)332 bool PasteData::HasMimeType(const std::string &mimeType)
333 {
334     for (const auto &item : records_) {
335         auto itemTypes = item->GetMimeTypes();
336         if (itemTypes.find(mimeType) != itemTypes.end()) {
337             return true;
338         }
339     }
340     return false;
341 }
342 
AllRecords() const343 std::vector<std::shared_ptr<PasteDataRecord>> PasteData::AllRecords() const
344 {
345     return this->records_;
346 }
347 
IsDraggedData() const348 bool PasteData::IsDraggedData() const
349 {
350     return isDraggedData_;
351 }
352 
IsLocalPaste() const353 bool PasteData::IsLocalPaste() const
354 {
355     return isLocalPaste_;
356 }
357 
SetDraggedDataFlag(bool isDraggedData)358 void PasteData::SetDraggedDataFlag(bool isDraggedData)
359 {
360     isDraggedData_ = isDraggedData;
361 }
362 
SetLocalPasteFlag(bool isLocalPaste)363 void PasteData::SetLocalPasteFlag(bool isLocalPaste)
364 {
365     isLocalPaste_ = isLocalPaste;
366 }
367 
SetRemote(bool isRemote)368 void PasteData::SetRemote(bool isRemote)
369 {
370     props_.isRemote = isRemote;
371 }
372 
IsRemote()373 bool PasteData::IsRemote()
374 {
375     return props_.isRemote;
376 }
377 
SetBundleName(const std::string & bundleName)378 void PasteData::SetBundleName(const std::string &bundleName)
379 {
380     props_.bundleName = bundleName;
381 }
382 
GetBundleName() const383 std::string PasteData::GetBundleName() const
384 {
385     return props_.bundleName;
386 }
387 
SetOriginAuthority(const std::string & bundleName)388 void PasteData::SetOriginAuthority(const std::string &bundleName)
389 {
390     originAuthority_ = bundleName;
391 }
392 
GetOriginAuthority() const393 std::string PasteData::GetOriginAuthority() const
394 {
395     return originAuthority_;
396 }
397 
SetTime(const std::string & setTime)398 void PasteData::SetTime(const std::string &setTime)
399 {
400     props_.setTime = setTime;
401 }
402 
GetTime()403 std::string PasteData::GetTime()
404 {
405     return props_.setTime;
406 }
407 
SetScreenStatus(ScreenEvent screenStatus)408 void PasteData::SetScreenStatus(ScreenEvent screenStatus)
409 {
410     props_.screenStatus = screenStatus;
411 }
412 
GetScreenStatus()413 ScreenEvent PasteData::GetScreenStatus()
414 {
415     return props_.screenStatus;
416 }
417 
SetTag(const std::string & tag)418 void PasteData::SetTag(const std::string &tag)
419 {
420     props_.tag = tag;
421 }
422 
GetTag()423 std::string PasteData::GetTag()
424 {
425     return props_.tag;
426 }
427 
SetAdditions(const AAFwk::WantParams & additions)428 void PasteData::SetAdditions(const AAFwk::WantParams &additions)
429 {
430     props_.additions = additions;
431 }
432 
SetAddition(const std::string & key,AAFwk::IInterface * value)433 void PasteData::SetAddition(const std::string &key, AAFwk::IInterface *value)
434 {
435     PASTEBOARD_CHECK_AND_RETURN_LOGE(value != nullptr, PASTEBOARD_MODULE_CLIENT, "value is null");
436     props_.additions.SetParam(key, value);
437 }
438 
SetFileSize(int64_t fileSize)439 void PasteData::SetFileSize(int64_t fileSize)
440 {
441     int32_t fileIntSize = (fileSize > INT_MAX) ? INT_MAX : static_cast<int32_t>(fileSize);
442     SetAddition(REMOTE_FILE_SIZE, AAFwk::Integer::Box(fileIntSize));
443     SetAddition(REMOTE_FILE_SIZE_LONG, AAFwk::Long::Box(fileSize));
444 }
445 
GetFileSize() const446 int64_t PasteData::GetFileSize() const
447 {
448     int64_t fileSize = 0L;
449     auto value = props_.additions.GetParam(REMOTE_FILE_SIZE_LONG);
450     AAFwk::ILong *ao = AAFwk::ILong::Query(value);
451     if (ao != nullptr) {
452         fileSize = AAFwk::Long::Unbox(ao);
453     } else {
454         fileSize = props_.additions.GetIntParam(REMOTE_FILE_SIZE, -1);
455     }
456     return fileSize;
457 }
458 
SetLocalOnly(bool localOnly)459 void PasteData::SetLocalOnly(bool localOnly)
460 {
461     props_.localOnly = localOnly;
462 }
463 
GetLocalOnly()464 bool PasteData::GetLocalOnly()
465 {
466     return props_.localOnly;
467 }
468 
RefreshMimeProp()469 void PasteData::RefreshMimeProp()
470 {
471     std::vector<std::string> mimeTypes;
472     for (const auto &record : records_) {
473         if (record == nullptr) {
474             continue;
475         }
476         mimeTypes.emplace_back(record->GetMimeType());
477     }
478     props_.mimeTypes = mimeTypes;
479 }
480 
EncodeTLV(WriteOnlyBuffer & buffer) const481 bool PasteData::EncodeTLV(WriteOnlyBuffer &buffer) const
482 {
483     bool ret = buffer.Write(TAG_PROPS, props_);
484     ret = ret && buffer.Write(TAG_RECORDS, records_);
485     ret = ret && buffer.Write(TAG_DRAGGED_DATA_FLAG, isDraggedData_);
486     ret = ret && buffer.Write(TAG_LOCAL_PASTE_FLAG, isLocalPaste_);
487     ret = ret && buffer.Write(TAG_DELAY_DATA_FLAG, isDelayData_);
488     ret = ret && buffer.Write(TAG_DEVICE_ID, deviceId_);
489     ret = ret && buffer.Write(TAG_PASTE_ID, pasteId_);
490     ret = ret && buffer.Write(TAG_DELAY_RECORD_FLAG, isDelayRecord_);
491     ret = ret && buffer.Write(TAG_DATA_ID, dataId_);
492     ret = ret && buffer.Write(TAG_RECORD_ID, recordId_);
493     return ret;
494 }
495 
InitDecodeMap()496 void PasteData::InitDecodeMap()
497 {
498     decodeMap_ = {
499         {TAG_PROPS,
500             [&](ReadOnlyBuffer &buffer, TLVHead &head) { return buffer.ReadValue(props_, head); }},
501         {TAG_RECORDS,
502             [&](ReadOnlyBuffer &buffer, TLVHead &head) { return buffer.ReadValue(records_, head); }},
503         {TAG_DRAGGED_DATA_FLAG,
504             [&](ReadOnlyBuffer &buffer, TLVHead &head) { return buffer.ReadValue(isDraggedData_, head); }},
505         {TAG_LOCAL_PASTE_FLAG,
506             [&](ReadOnlyBuffer &buffer, TLVHead &head) { return buffer.ReadValue(isLocalPaste_, head); }},
507         {TAG_DELAY_DATA_FLAG,
508             [&](ReadOnlyBuffer &buffer, TLVHead &head) { return buffer.ReadValue(isDelayData_, head); }},
509         {TAG_DEVICE_ID,
510             [&](ReadOnlyBuffer &buffer, TLVHead &head) { return buffer.ReadValue(deviceId_, head); }},
511         {TAG_PASTE_ID,
512             [&](ReadOnlyBuffer &buffer, TLVHead &head) { return buffer.ReadValue(pasteId_, head); }},
513         {TAG_DELAY_RECORD_FLAG,
514             [&](ReadOnlyBuffer &buffer, TLVHead &head) { return buffer.ReadValue(isDelayRecord_, head); }},
515         {TAG_DATA_ID,
516             [&](ReadOnlyBuffer &buffer, TLVHead &head) { return buffer.ReadValue(dataId_, head); }},
517         {TAG_RECORD_ID,
518             [&](ReadOnlyBuffer &buffer, TLVHead &head) { return buffer.ReadValue(recordId_, head); }},
519     };
520 }
521 
DecodeTLV(ReadOnlyBuffer & buffer)522 bool PasteData::DecodeTLV(ReadOnlyBuffer &buffer)
523 {
524     for (; buffer.IsEnough();) {
525         TLVHead head{};
526         bool ret = buffer.ReadHead(head);
527         PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(ret, false, PASTEBOARD_MODULE_COMMON, "read head failed");
528 
529         const auto it = decodeMap_.find(head.tag);
530         if (it == decodeMap_.end()) {
531             ret = buffer.Skip(head.len);
532         } else {
533             ret = it->second(buffer, head);
534         }
535 
536         PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(ret, false, PASTEBOARD_MODULE_COMMON,
537             "read value failed, tag=%{public}hu, len=%{public}u", head.tag, head.len);
538     }
539     return true;
540 }
541 
CountTLV() const542 size_t PasteData::CountTLV() const
543 {
544     size_t expectSize = 0;
545     expectSize += TLVCountable::Count(props_);
546     expectSize += TLVCountable::Count(records_);
547     expectSize += TLVCountable::Count(isDraggedData_);
548     expectSize += TLVCountable::Count(isLocalPaste_);
549     expectSize += TLVCountable::Count(isDelayData_);
550     expectSize += TLVCountable::Count(deviceId_);
551     expectSize += TLVCountable::Count(pasteId_);
552     expectSize += TLVCountable::Count(isDelayRecord_);
553     expectSize += TLVCountable::Count(dataId_);
554     expectSize += TLVCountable::Count(recordId_);
555     return expectSize;
556 }
557 
Marshalling(Parcel & parcel) const558 bool PasteData::Marshalling(Parcel &parcel) const
559 {
560     std::vector<uint8_t> pasteDataTlv(0);
561     bool ret = Encode(pasteDataTlv);
562     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(ret, false, PASTEBOARD_MODULE_COMMON, "encode failed");
563 
564     ret = parcel.WriteUInt8Vector(pasteDataTlv);
565     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(ret, false, PASTEBOARD_MODULE_COMMON, "write vector failed");
566     return true;
567 }
568 
Unmarshalling(Parcel & parcel)569 PasteData *PasteData::Unmarshalling(Parcel &parcel)
570 {
571     std::vector<uint8_t> pasteDataTlv(0);
572     bool ret = parcel.ReadUInt8Vector(&pasteDataTlv);
573     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(ret, nullptr, PASTEBOARD_MODULE_COMMON, "read vector failed");
574     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(!pasteDataTlv.empty(), nullptr, PASTEBOARD_MODULE_COMMON, "vector empty");
575 
576     PasteData *pasteData = new (std::nothrow) PasteData();
577     if (!pasteData->Decode(pasteDataTlv)) {
578         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_COMMON, "decode failed");
579         delete pasteData;
580         pasteData = nullptr;
581     }
582     return pasteData;
583 }
584 
IsValid() const585 bool PasteData::IsValid() const
586 {
587     return valid_;
588 }
589 
SetInvalid()590 void PasteData::SetInvalid()
591 {
592     valid_ = false;
593 }
594 
SetDelayData(bool isDelay)595 void PasteData::SetDelayData(bool isDelay)
596 {
597     isDelayData_ = isDelay;
598 }
599 
IsDelayData() const600 bool PasteData::IsDelayData() const
601 {
602     return isDelayData_;
603 }
604 
SetDelayRecord(bool isDelay)605 void PasteData::SetDelayRecord(bool isDelay)
606 {
607     isDelayRecord_ = isDelay;
608 }
609 
IsDelayRecord() const610 bool PasteData::IsDelayRecord() const
611 {
612     return isDelayRecord_;
613 }
614 
SetDataId(uint32_t dataId)615 void PasteData::SetDataId(uint32_t dataId)
616 {
617     dataId_ = dataId;
618 }
619 
GetDataId() const620 uint32_t PasteData::GetDataId() const
621 {
622     return dataId_;
623 }
624 
GetRecordId() const625 uint32_t PasteData::GetRecordId() const
626 {
627     return recordId_;
628 }
629 
PasteDataProperty(const PasteDataProperty & property)630 PasteDataProperty::PasteDataProperty(const PasteDataProperty &property)
631     : tag(property.tag), timestamp(property.timestamp), localOnly(property.localOnly),
632       shareOption(property.shareOption), tokenId(property.tokenId), isRemote(property.isRemote),
633       bundleName(property.bundleName), setTime(property.setTime), screenStatus(property.screenStatus)
634 {
635     this->additions = property.additions;
636     std::copy(property.mimeTypes.begin(), property.mimeTypes.end(), std::back_inserter(this->mimeTypes));
637 }
638 
~PasteDataProperty()639 PasteDataProperty::~PasteDataProperty()
640 {
641     mimeTypes.clear();
642 }
643 
operator =(const PasteDataProperty & property)644 PasteDataProperty &PasteDataProperty::operator=(const PasteDataProperty &property)
645 {
646     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "PasteDataProperty copy");
647     if (this == &property) {
648         return *this;
649     }
650     this->tag = property.tag;
651     this->timestamp = property.timestamp;
652     this->localOnly = property.localOnly;
653     this->shareOption = property.shareOption;
654     this->tokenId = property.tokenId;
655     this->isRemote = property.isRemote;
656     this->bundleName = property.bundleName;
657     this->setTime = property.setTime;
658     this->screenStatus = property.screenStatus;
659     this->additions = property.additions;
660     this->mimeTypes.clear();
661     std::copy(property.mimeTypes.begin(), property.mimeTypes.end(), std::back_inserter(this->mimeTypes));
662     return *this;
663 }
664 
EncodeTLV(WriteOnlyBuffer & buffer) const665 bool PasteDataProperty::EncodeTLV(WriteOnlyBuffer &buffer) const
666 {
667     bool ret = buffer.Write(TAG_ADDITIONS, TLVUtils::Parcelable2Raw(&additions));
668     ret = ret && buffer.Write(TAG_MIMETYPES, mimeTypes);
669     ret = ret && buffer.Write(TAG_TAG, tag);
670     ret = ret && buffer.Write(TAG_LOCAL_ONLY, localOnly);
671     ret = ret && buffer.Write(TAG_TIMESTAMP, timestamp);
672     ret = ret && buffer.Write(TAG_SHAREOPTION, static_cast<int32_t>(shareOption));
673     ret = ret && buffer.Write(TAG_TOKENID, tokenId);
674     ret = ret && buffer.Write(TAG_ISREMOTE, isRemote);
675     ret = ret && buffer.Write(TAG_BUNDLENAME, bundleName);
676     ret = ret && buffer.Write(TAG_SETTIME, setTime);
677     ret = ret && buffer.Write(TAG_SCREENSTATUS, static_cast<int32_t>(screenStatus));
678     return ret;
679 }
680 
DecodeTLV(ReadOnlyBuffer & buffer)681 bool PasteDataProperty::DecodeTLV(ReadOnlyBuffer &buffer)
682 {
683     for (; buffer.IsEnough();) {
684         TLVHead head{};
685         bool ret = buffer.ReadHead(head);
686         PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(ret, false, PASTEBOARD_MODULE_COMMON, "read head failed");
687         if (head.tag == TAG_ADDITIONS) {
688             RawMem rawMem{};
689             ret = buffer.ReadValue(rawMem, head);
690             auto buff = TLVUtils::Raw2Parcelable<AAFwk::WantParams>(rawMem);
691             additions = (buff != nullptr) ? *buff : additions;
692         } else if (head.tag == TAG_MIMETYPES) {
693             ret = buffer.ReadValue(mimeTypes, head);
694         } else if (head.tag == TAG_TAG) {
695             ret = buffer.ReadValue(tag, head);
696         } else if (head.tag == TAG_LOCAL_ONLY) {
697             ret = buffer.ReadValue(localOnly, head);
698         } else if (head.tag == TAG_TIMESTAMP) {
699             ret = buffer.ReadValue(timestamp, head);
700         } else if (head.tag == TAG_SHAREOPTION) {
701             ret = buffer.ReadValue((int32_t &)shareOption, head);
702         } else if (head.tag == TAG_TOKENID) {
703             ret = buffer.ReadValue(tokenId, head);
704         } else if (head.tag == TAG_ISREMOTE) {
705             ret = buffer.ReadValue(isRemote, head);
706         } else if (head.tag == TAG_BUNDLENAME) {
707             ret = buffer.ReadValue(bundleName, head);
708         } else if (head.tag == TAG_SETTIME) {
709             ret = buffer.ReadValue(setTime, head);
710         } else if (head.tag == TAG_SCREENSTATUS) {
711             ret = buffer.ReadValue((int32_t &)screenStatus, head);
712         } else {
713             ret = buffer.Skip(head.len);
714         }
715         PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(ret, false, PASTEBOARD_MODULE_COMMON,
716             "read value failed, tag=%{public}hu, len=%{public}u", head.tag, head.len);
717     }
718     return true;
719 }
720 
CountTLV() const721 size_t PasteDataProperty::CountTLV() const
722 {
723     size_t expectedSize = 0;
724     expectedSize += TLVCountable::Count(TLVUtils::Parcelable2Raw(&additions));
725     expectedSize += TLVCountable::Count(mimeTypes);
726     expectedSize += TLVCountable::Count(tag);
727     expectedSize += TLVCountable::Count(localOnly);
728     expectedSize += TLVCountable::Count(timestamp);
729     expectedSize += TLVCountable::Count(shareOption);
730     expectedSize += TLVCountable::Count(tokenId);
731     expectedSize += TLVCountable::Count(isRemote);
732     expectedSize += TLVCountable::Count(bundleName);
733     expectedSize += TLVCountable::Count(setTime);
734     expectedSize += TLVCountable::Count(screenStatus);
735     return expectedSize;
736 }
737 
ShareOptionToString(ShareOption shareOption,std::string & out)738 void PasteData::ShareOptionToString(ShareOption shareOption, std::string &out)
739 {
740     if (shareOption == ShareOption::InApp) {
741         out = "InAPP";
742     } else if (shareOption == ShareOption::LocalDevice) {
743         out = "LocalDevice";
744     } else {
745         out = "CrossDevice";
746     }
747 }
748 
GetDeviceId() const749 std::string PasteData::GetDeviceId() const
750 {
751     return deviceId_;
752 }
753 
SetPasteId(const std::string & pasteId)754 void PasteData::SetPasteId(const std::string &pasteId)
755 {
756     pasteId_ = pasteId;
757 }
758 
GetPasteId() const759 std::string PasteData::GetPasteId() const
760 {
761     return pasteId_;
762 }
763 } // namespace MiscServices
764 } // namespace OHOS