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