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