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 #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 "paste_data_record.h" 26 #include "pasteboard_hilog.h" 27 #include "tlv_object.h" 28 #include "uri.h" 29 #include "want.h" 30 #include "want_params.h" 31 32 namespace OHOS { 33 namespace MiscServices { 34 enum ShareOption : int32_t { InApp = 0, LocalDevice, CrossDevice }; 35 struct PasteDataProperty : public TLVObject { 36 PasteDataProperty() = default; 37 PasteDataProperty(const PasteDataProperty &property); 38 AAFwk::WantParams additions; 39 std::vector<std::string> mimeTypes; 40 std::string tag; 41 std::int64_t timestamp; 42 bool localOnly; 43 ShareOption shareOption; 44 uint32_t tokenId = 0; 45 bool isRemote = false; 46 std::string bundleName; 47 std::string setTime; 48 49 bool Encode(std::vector<std::uint8_t> &buffer) override; 50 bool Decode(const std::vector<std::uint8_t> &buffer) override; 51 bool DecodeTag(const std::vector<std::uint8_t> &buffer); 52 size_t Count() override; 53 }; 54 55 class PasteData : public TLVObject { 56 public: 57 static constexpr const std::uint32_t MAX_RECORD_NUM = 512; 58 PasteData(); 59 PasteData(const PasteData &data); 60 explicit PasteData(std::vector<std::shared_ptr<PasteDataRecord>> records); 61 62 void AddHtmlRecord(const std::string &html); 63 void AddKvRecord(const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer); 64 void AddPixelMapRecord(std::shared_ptr<OHOS::Media::PixelMap> pixelMap); 65 void AddTextRecord(const std::string &text); 66 void AddUriRecord(const OHOS::Uri &uri); 67 void AddWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want); 68 void AddRecord(std::shared_ptr<PasteDataRecord> record); 69 void AddRecord(PasteDataRecord &record); 70 std::vector<std::string> GetMimeTypes(); 71 std::shared_ptr<std::string> GetPrimaryHtml(); 72 std::shared_ptr<OHOS::Media::PixelMap> GetPrimaryPixelMap(); 73 std::shared_ptr<std::string> GetPrimaryText(); 74 std::shared_ptr<OHOS::Uri> GetPrimaryUri(); 75 std::shared_ptr<std::string> GetPrimaryMimeType(); 76 std::shared_ptr<OHOS::AAFwk::Want> GetPrimaryWant(); 77 std::shared_ptr<PasteDataRecord> GetRecordAt(std::size_t index); 78 std::size_t GetRecordCount(); 79 bool RemoveRecordAt(std::size_t number); 80 bool ReplaceRecordAt(std::size_t number, std::shared_ptr<PasteDataRecord> record); 81 bool HasMimeType(const std::string &mimeType); 82 PasteDataProperty GetProperty() const; 83 ShareOption GetShareOption(); 84 void SetShareOption(ShareOption shareOption); 85 uint32_t GetTokenId(); 86 void SetTokenId(uint32_t tokenId); 87 std::vector<std::shared_ptr<PasteDataRecord>> AllRecords() const; 88 bool IsDraggedData() const; 89 void SetDraggedDataFlag(bool isDraggedData); 90 bool IsLocalPaste() const; 91 void SetLocalPasteFlag(bool isLocalPaste); 92 93 void SetBundleName(const std::string &bundleName); 94 std::string GetBundleName() const; 95 void SetOrginAuthority(const std::string &bundleName); 96 std::string GetOrginAuthority() const; 97 void SetRemote(bool isRemote); 98 bool IsRemote(); 99 void SetTime(const std::string &time); 100 std::string GetTime(); 101 void SetTag(std::string &tag); 102 std::string GetTag(); 103 void SetAdditions(AAFwk::WantParams &additions); 104 void SetAddition(const std::string &key, AAFwk::IInterface *value); 105 void SetLocalOnly(bool localOnly); 106 bool GetLocalOnly(); 107 108 bool Encode(std::vector<std::uint8_t> &buffer) override; 109 bool Decode(const std::vector<std::uint8_t> &buffer) override; 110 size_t Count() override; 111 bool WriteUriFd(MessageParcel &parcel, UriHandler &uriHandler, bool isClient = true); 112 bool ReadUriFd(MessageParcel &parcel, UriHandler &uriHandler); 113 void ReplaceShareUri(int32_t userId); 114 115 bool IsValid() const; 116 void SetInvalid(); 117 118 static void ShareOptionToString(ShareOption shareOption, std::string &out); 119 static std::string sharePath; 120 static std::string WEBVIEW_PASTEDATA_TAG; 121 static const std::string DISTRIBUTEDFILES_TAG; 122 static const std::string PATH_SHARE; 123 static const std::string FILE_SCHEME_PREFIX; 124 static const std::string IMG_LOCAL_URI; 125 static const std::string SHARE_PATH_PREFIX; 126 static const std::string SHARE_PATH_PREFIX_ACCOUNT; 127 static const std::string REMOTE_FILE_SIZE; 128 129 private: 130 void RefreshMimeProp(); 131 132 PasteDataProperty props_; 133 std::vector<std::shared_ptr<PasteDataRecord>> records_; 134 std::string orginAuthority_; 135 bool valid_ = true; 136 bool isDraggedData_ = false; 137 bool isLocalPaste_ = false; // local in app paste 138 }; 139 } // namespace MiscServices 140 } // namespace OHOS 141 #endif // PASTE_BOARD_DATA_H 142