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 #ifndef PASTE_BOARD_RECORD_H 17 #define PASTE_BOARD_RECORD_H 18 19 #include <memory> 20 #include <string> 21 22 #include "common/constant.h" 23 #include "message_parcel.h" 24 #include "paste_data_entry.h" 25 #include "pixel_map.h" 26 #include "string_ex.h" 27 #include "tlv_object.h" 28 #include "unified_meta.h" 29 #include "uri.h" 30 #include "uri_handler.h" 31 #include "want.h" 32 namespace OHOS { 33 namespace MiscServices { 34 enum ResultCode : int32_t { OK = 0, IPC_NO_DATA, IPC_ERROR }; 35 class FileDescriptor { 36 public: 37 FileDescriptor() = default; 38 ~FileDescriptor(); 39 void SetFd(int32_t fd); 40 int32_t GetFd() const; 41 42 private: 43 int32_t fd_ = -1; 44 }; 45 46 class API_EXPORT PasteDataRecord : public TLVObject { 47 public: 48 PasteDataRecord(); 49 ~PasteDataRecord(); 50 PasteDataRecord(const PasteDataRecord &record); 51 PasteDataRecord(std::string mimeType, std::shared_ptr<std::string> htmlText, 52 std::shared_ptr<OHOS::AAFwk::Want> want, std::shared_ptr<std::string> plainText, 53 std::shared_ptr<OHOS::Uri> uri); 54 55 static std::shared_ptr<PasteDataRecord> NewHtmlRecord(const std::string &htmlText); 56 static std::shared_ptr<PasteDataRecord> NewWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want); 57 static std::shared_ptr<PasteDataRecord> NewPlaintTextRecord(const std::string &text); 58 static std::shared_ptr<PasteDataRecord> NewPixelMapRecord(std::shared_ptr<OHOS::Media::PixelMap> pixelMap); 59 static std::shared_ptr<PasteDataRecord> NewUriRecord(const OHOS::Uri &uri); 60 static std::shared_ptr<PasteDataRecord> NewKvRecord( 61 const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer); 62 63 bool isConvertUriFromRemote = false; 64 std::string GetMimeType() const; 65 std::shared_ptr<std::string> GetHtmlText() const; 66 std::shared_ptr<std::string> GetPlainText() const; 67 std::shared_ptr<OHOS::Media::PixelMap> GetPixelMap() const; 68 void ClearPixelMap(); 69 std::shared_ptr<OHOS::Uri> GetUri() const; 70 void SetUri(std::shared_ptr<OHOS::Uri> uri); 71 std::shared_ptr<OHOS::Uri> GetOrginUri() const; 72 std::shared_ptr<OHOS::AAFwk::Want> GetWant() const; 73 std::shared_ptr<MineCustomData> GetCustomData() const; 74 75 std::string ConvertToText() const; 76 77 bool Encode(std::vector<std::uint8_t> &buffer) override; 78 bool Decode(const std::vector<std::uint8_t> &buffer) override; 79 size_t Count() override; 80 bool WriteFd(MessageParcel &parcel, UriHandler &uriHandler, bool isClient); 81 bool ReadFd(MessageParcel &parcel, UriHandler &uriHandler); 82 bool NeedFd(const UriHandler &uriHandler); 83 void ReplaceShareUri(int32_t userId); 84 void SetConvertUri(const std::string &value); 85 std::string GetConvertUri() const; 86 void SetGrantUriPermission(bool hasPermission); 87 bool HasGrantUriPermission(); 88 89 void SetTextContent(const std::string& content); 90 std::string GetTextContent() const; 91 void SetDetails(const Details& details); 92 std::shared_ptr<Details> GetDetails() const; 93 void SetSystemDefinedContent(const Details& contents); 94 std::shared_ptr<Details> GetSystemDefinedContent() const; 95 int32_t GetUDType() const; 96 void SetUDType(int32_t type); 97 98 bool HasEmptyEntry() const; 99 void SetUDMFValue(const std::shared_ptr<EntryValue> &udmfValue); 100 std::shared_ptr<EntryValue> GetUDMFValue(); 101 void AddEntry(const std::string& utdType, std::shared_ptr<PasteDataEntry> value); 102 std::shared_ptr<PasteDataEntry> GetEntry(const std::string& utdType) const; 103 std::vector<std::shared_ptr<PasteDataEntry>> GetEntries() const; 104 std::vector<std::string> GetValidTypes(const std::vector<std::string>& types) const; 105 106 void SetDelayRecordFlag(bool isDelay); 107 bool IsDelayRecord() const; 108 void SetDataId(uint32_t dataId); 109 uint32_t GetDataId() const; 110 void SetRecordId(uint32_t recordId); 111 uint32_t GetRecordId() const; 112 113 class Builder { 114 public: 115 explicit Builder(const std::string &mimeType); 116 Builder &SetHtmlText(std::shared_ptr<std::string> htmlText); 117 Builder &SetWant(std::shared_ptr<OHOS::AAFwk::Want> want); 118 Builder &SetPlainText(std::shared_ptr<std::string> plainText); 119 Builder &SetUri(std::shared_ptr<OHOS::Uri> uri); 120 Builder &SetPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap); 121 Builder &SetCustomData(std::shared_ptr<MineCustomData> customData); 122 Builder &SetMimeType(std::string mimeType); 123 std::shared_ptr<PasteDataRecord> Build(); 124 125 private: 126 std::shared_ptr<PasteDataRecord> record_ = nullptr; 127 }; 128 129 private: CheckResult(ResultCode resultCode)130 inline static bool CheckResult(ResultCode resultCode) 131 { 132 return resultCode == ResultCode::OK; 133 } 134 std::string GetPassUri(); 135 void AddUriEntry(); 136 std::set<std::string> GetUdtTypes() const; 137 138 std::string mimeType_; 139 std::shared_ptr<std::string> htmlText_; 140 std::shared_ptr<OHOS::AAFwk::Want> want_; 141 std::shared_ptr<std::string> plainText_; 142 std::shared_ptr<OHOS::Uri> uri_; 143 std::string convertUri_; 144 std::shared_ptr<OHOS::Media::PixelMap> pixelMap_; 145 std::shared_ptr<MineCustomData> customData_; 146 bool hasGrantUriPermission_ = false; 147 std::shared_ptr<FileDescriptor> fd_; 148 using Func = std::function<void(bool &ret, const std::vector<std::uint8_t> &buffer, TLVHead &head)>; 149 std::map<uint16_t, Func> decodeMap; 150 void InitDecodeMap(); 151 152 int32_t udType_ = UDMF::UD_BUTT; 153 std::shared_ptr<Details> details_; 154 std::string textContent_; 155 std::shared_ptr<Details> systemDefinedContents_; 156 std::shared_ptr<EntryValue> udmfValue_; 157 std::vector<std::shared_ptr<PasteDataEntry>> entries_; 158 uint32_t dataId_ = 0; 159 uint32_t recordId_ = 0; 160 bool isDelay_ = false; 161 }; 162 } // namespace MiscServices 163 } // namespace OHOS 164 #endif // PASTE_BOARD_RECORD_H 165