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