1 /* 2 * Copyright (C) 2021-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 #ifndef PASTE_BOARD_DATA_H 16 #define PASTE_BOARD_DATA_H 17 18 #include "paste_data_record.h" 19 20 namespace OHOS { 21 namespace MiscServices { 22 enum ShareOption : int32_t { InApp = 0, LocalDevice, CrossDevice }; 23 enum ScreenEvent : int32_t { Default = 0, ScreenLocked, ScreenUnlocked }; 24 struct API_EXPORT PasteDataProperty : public TLVWriteable, public TLVReadable { 25 PasteDataProperty() = default; 26 ~PasteDataProperty(); 27 explicit PasteDataProperty(const PasteDataProperty &property); 28 PasteDataProperty &operator=(const PasteDataProperty &property); 29 AAFwk::WantParams additions; 30 std::vector<std::string> mimeTypes; 31 std::string tag; 32 std::int64_t timestamp; 33 bool localOnly; 34 ShareOption shareOption; 35 uint32_t tokenId = 0; 36 bool isRemote = false; 37 std::string bundleName; 38 std::string setTime; 39 ScreenEvent screenStatus = ScreenEvent::Default; 40 41 bool EncodeTLV(WriteOnlyBuffer &buffer) const override; 42 bool DecodeTLV(ReadOnlyBuffer &buffer) override; 43 size_t CountTLV() const override; 44 }; 45 46 class API_EXPORT PasteData : public TLVWriteable, public TLVReadable, public Parcelable { 47 public: 48 static constexpr const std::uint32_t MAX_RECORD_NUM = 512; 49 PasteData(); 50 ~PasteData(); 51 PasteData(const PasteData &data); 52 PasteData &operator=(const PasteData &data); 53 explicit PasteData(std::vector<std::shared_ptr<PasteDataRecord>> records); 54 55 void AddHtmlRecord(const std::string &html); 56 void AddKvRecord(const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer); 57 void AddPixelMapRecord(std::shared_ptr<OHOS::Media::PixelMap> pixelMap); 58 void AddTextRecord(const std::string &text); 59 void AddUriRecord(const OHOS::Uri &uri); 60 void AddWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want); 61 void AddRecord(std::shared_ptr<PasteDataRecord> record); 62 void AddRecord(const PasteDataRecord &record); 63 std::vector<std::string> GetMimeTypes(); 64 std::shared_ptr<std::string> GetPrimaryHtml(); 65 std::shared_ptr<OHOS::Media::PixelMap> GetPrimaryPixelMap(); 66 std::shared_ptr<std::string> GetPrimaryText(); 67 std::shared_ptr<OHOS::Uri> GetPrimaryUri(); 68 std::shared_ptr<std::string> GetPrimaryMimeType(); 69 std::shared_ptr<OHOS::AAFwk::Want> GetPrimaryWant(); 70 std::shared_ptr<PasteDataRecord> GetRecordAt(std::size_t index) const; 71 std::shared_ptr<PasteDataRecord> GetRecordById(uint32_t recordId) const; 72 std::size_t GetRecordCount() const; 73 bool RemoveRecordAt(std::size_t number); 74 bool ReplaceRecordAt(std::size_t number, std::shared_ptr<PasteDataRecord> record); 75 bool HasMimeType(const std::string &mimeType); 76 PasteDataProperty GetProperty() const; 77 void SetProperty(const PasteDataProperty &property); 78 ShareOption GetShareOption(); 79 void SetShareOption(ShareOption shareOption); 80 uint32_t GetTokenId(); 81 void SetTokenId(uint32_t tokenId); 82 std::vector<std::shared_ptr<PasteDataRecord>> AllRecords() const; 83 bool IsDraggedData() const; 84 void SetDraggedDataFlag(bool isDraggedData); 85 bool IsLocalPaste() const; 86 void SetLocalPasteFlag(bool isLocalPaste); 87 88 void SetBundleName(const std::string &bundleName); 89 std::string GetBundleName() const; 90 void SetOriginAuthority(const std::string &bundleName); 91 std::string GetOriginAuthority() const; 92 void SetRemote(bool isRemote); 93 bool IsRemote(); 94 void SetTime(const std::string &time); 95 std::string GetTime(); 96 void SetScreenStatus(ScreenEvent screenStatus); 97 ScreenEvent GetScreenStatus(); 98 void SetTag(const std::string &tag); 99 std::string GetTag(); 100 void SetAdditions(const AAFwk::WantParams &additions); 101 void SetAddition(const std::string &key, AAFwk::IInterface *value); 102 void SetLocalOnly(bool localOnly); 103 bool GetLocalOnly(); 104 void SetFileSize(int64_t fileSize); 105 int64_t GetFileSize() const; 106 107 bool Marshalling(Parcel &parcel) const override; 108 static PasteData *Unmarshalling(Parcel &parcel); 109 bool EncodeTLV(WriteOnlyBuffer &buffer) const override; 110 bool DecodeTLV(ReadOnlyBuffer &buffer) override; 111 size_t CountTLV() const override; 112 113 bool IsValid() const; 114 void SetInvalid(); 115 116 void SetDelayData(bool isDelay); 117 bool IsDelayData() const; 118 void SetDelayRecord(bool isDelay); 119 bool IsDelayRecord() const; 120 void SetDataId(uint32_t dataId); 121 uint32_t GetDataId() const; 122 uint32_t GetRecordId() const; 123 void SetPasteId(const std::string &pasteId); 124 std::string GetPasteId() const; 125 std::string GetDeviceId() const; 126 127 static void ShareOptionToString(ShareOption shareOption, std::string &out); 128 static std::string sharePath; 129 static std::string WEBVIEW_PASTEDATA_TAG; 130 static const std::string DISTRIBUTEDFILES_TAG; 131 static const std::string PATH_SHARE; 132 static const std::string IMG_LOCAL_URI; 133 static const std::string SHARE_PATH_PREFIX; 134 static const std::string SHARE_PATH_PREFIX_ACCOUNT; 135 std::string deviceId_; 136 static const std::string DOCS_LOCAL_TAG; 137 static constexpr size_t URI_BATCH_SIZE = 500; 138 139 private: 140 void RefreshMimeProp(); 141 142 PasteDataProperty props_; 143 std::vector<std::shared_ptr<PasteDataRecord>> records_; 144 std::string originAuthority_; 145 bool valid_ = true; 146 bool isDraggedData_ = false; 147 bool isLocalPaste_ = false; // local in app paste 148 bool isDelayData_ = false; 149 std::string pasteId_; 150 bool isDelayRecord_ = false; 151 uint32_t dataId_ = 0; 152 uint32_t recordId_ = 0; 153 154 using DecodeFunc = std::function<bool(ReadOnlyBuffer &buffer, TLVHead &head)>; 155 std::map<uint16_t, DecodeFunc> decodeMap_; 156 void InitDecodeMap(); 157 }; 158 159 class IPasteDataProcessor { 160 public: 161 virtual ~IPasteDataProcessor() = default; 162 virtual int32_t Process(const std::string &data, std::string &result) = 0; 163 }; 164 } // namespace MiscServices 165 } // namespace OHOS 166 #endif // PASTE_BOARD_DATA_H 167