1 /* 2 * Copyright (C) 2021 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 <chrono> 19 #include <cstddef> 20 #include <cstdint> 21 #include <memory> 22 #include <string> 23 #include <vector> 24 25 #include "parcel.h" 26 #include "paste_data_record.h" 27 #include "tlv_object.h" 28 #include "uri.h" 29 #include "want.h" 30 #include "want_params.h" 31 #include "pasteboard_hilog.h" 32 33 namespace OHOS { 34 namespace MiscServices { 35 enum ShareOption : int32_t { InApp = 0, LocalDevice, CrossDevice }; 36 struct PasteDataProperty : public TLVObject { 37 AAFwk::WantParams additions; 38 std::vector<std::string> mimeTypes; 39 std::string tag; 40 std::int64_t timestamp; 41 bool localOnly; 42 ShareOption shareOption; 43 uint32_t tokenId = 0; 44 bool isRemote = false; 45 std::string bundleName; 46 std::string setTime; 47 48 bool Encode(std::vector<std::uint8_t> &buffer) override; 49 bool Decode(const std::vector<std::uint8_t> &buffer) override; 50 size_t Count() override; 51 }; 52 53 class PasteData : public Parcelable, public TLVObject { 54 public: 55 static constexpr const std::uint32_t MAX_RECORD_NUM = 512; 56 PasteData(); 57 explicit PasteData(std::vector<std::shared_ptr<PasteDataRecord>> records); 58 59 void AddHtmlRecord(const std::string &html); 60 void AddKvRecord(const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer); 61 void AddPixelMapRecord(std::shared_ptr<OHOS::Media::PixelMap> pixelMap); 62 void AddTextRecord(const std::string &text); 63 void AddUriRecord(const OHOS::Uri &uri); 64 void AddWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want); 65 void AddRecord(std::shared_ptr<PasteDataRecord> record); 66 void AddRecord(PasteDataRecord &record); 67 std::vector<std::string> GetMimeTypes(); 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); 75 std::size_t GetRecordCount(); 76 bool RemoveRecordAt(std::size_t number); 77 bool ReplaceRecordAt(std::size_t number, std::shared_ptr<PasteDataRecord> record); 78 bool HasMimeType(const std::string &mimeType); 79 PasteDataProperty GetProperty() const; 80 ShareOption GetShareOption(); 81 void SetShareOption(ShareOption shareOption); 82 uint32_t GetTokenId(); 83 void SetTokenId(uint32_t tokenId); 84 std::vector<std::shared_ptr<PasteDataRecord>> AllRecords() const; 85 bool IsDraggedData() const; 86 void SetDraggedDataFlag(bool isDraggedData); 87 bool IsLocalPaste() const; 88 void SetLocalPasteFlag(bool isLocalPaste); 89 90 void SetBundleName(const std::string &bundleName); 91 void SetRemote(bool isRemote); 92 void SetTime(const std::string &time); 93 void SetTag(std::string &tag); 94 std::string GetTag(); 95 void SetAdditions(AAFwk::WantParams &additions); 96 97 virtual bool Marshalling(Parcel &parcel) const override; 98 static PasteData *Unmarshalling(Parcel &parcel); 99 bool Encode(std::vector<std::uint8_t> &buffer) override; 100 bool Decode(const std::vector<std::uint8_t> &buffer) override; 101 size_t Count() override; 102 bool WriteUriFd(MessageParcel &parcel, UriHandler &uriHandler, bool isClient = true); 103 bool ReadUriFd(MessageParcel &parcel, UriHandler &uriHandler); 104 void ReplaceShareUri(int32_t userId); 105 106 bool IsValid() const; 107 void SetInvalid(); 108 109 static void ShareOptionToString(ShareOption shareOption, std::string &out); 110 static std::string sharePath; 111 static const std::string SHARE_PATH_PREFIX; 112 static const std::string SHARE_PATH_PREFIX_ACCOUNT; 113 114 private: 115 bool MarshallingProps(Parcel &parcel) const; 116 static bool UnMarshalling(Parcel &parcel, PasteDataProperty &props); 117 void RefreshMimeProp(); 118 119 PasteDataProperty props_; 120 std::vector<std::shared_ptr<PasteDataRecord>> records_; 121 bool valid_ = true; 122 bool isDraggedData_ = false; 123 bool isLocalPaste_ = false; // local in app paste 124 }; 125 } // namespace MiscServices 126 } // namespace OHOS 127 #endif // PASTE_BOARD_DATA_H 128