• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef PASTE_BOARD_RECORD_H
17 #define PASTE_BOARD_RECORD_H
18 
19 #include <memory>
20 #include <string>
21 
22 #include "parcel.h"
23 #include "pixel_map.h"
24 #include "string_ex.h"
25 #include "tlv_object.h"
26 #include "uri.h"
27 #include "want.h"
28 #include "uri_handler.h"
29 #include "message_parcel.h"
30 
31 namespace OHOS {
32 namespace MiscServices {
33 namespace {
34 const std::string MIMETYPE_PIXELMAP = "pixelMap";
35 const std::string MIMETYPE_TEXT_HTML = "text/html";
36 const std::string MIMETYPE_TEXT_PLAIN = "text/plain";
37 const std::string MIMETYPE_TEXT_URI = "text/uri";
38 const std::string MIMETYPE_TEXT_WANT = "text/want";
39 } // namespace
40 
41 enum ResultCode : int32_t { OK = 0, IPC_NO_DATA, IPC_ERROR };
42 
43 class MineCustomData : public Parcelable, public TLVObject {
44 public:
45     MineCustomData() = default;
46     std::map<std::string, std::vector<uint8_t>> GetItemData();
47     void AddItemData(const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer);
48     virtual bool Marshalling(Parcel &parcel) const override;
49     static MineCustomData *Unmarshalling(Parcel &parcel);
50     bool Encode(std::vector<std::uint8_t> &buffer) override;
51     bool Decode(const std::vector<std::uint8_t> &buffer) override;
52     size_t Count() override;
53 
54 private:
55     std::map<std::string, std::vector<uint8_t>> itemData_;
56 };
57 
58 class FileDescriptor {
59 public:
60     FileDescriptor() = default;
61     ~FileDescriptor();
62     void SetFd(int32_t fd);
63     int32_t GetFd() const;
64 
65 private:
66     int32_t fd_ = -1;
67 };
68 
69 class PasteDataRecord : public Parcelable, public TLVObject {
70 public:
71     PasteDataRecord();
72     PasteDataRecord(std::string mimeType, std::shared_ptr<std::string> htmlText,
73         std::shared_ptr<OHOS::AAFwk::Want> want, std::shared_ptr<std::string> plainText,
74         std::shared_ptr<OHOS::Uri> uri);
75 
76     static std::shared_ptr<PasteDataRecord> NewHtmlRecord(const std::string &htmlText);
77     static std::shared_ptr<PasteDataRecord> NewWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want);
78     static std::shared_ptr<PasteDataRecord> NewPlaintTextRecord(const std::string &text);
79     static std::shared_ptr<PasteDataRecord> NewPixelMapRecord(std::shared_ptr<OHOS::Media::PixelMap> pixelMap);
80     static std::shared_ptr<PasteDataRecord> NewUriRecord(const OHOS::Uri &uri);
81     static std::shared_ptr<PasteDataRecord> NewKvRecord(const std::string &mimeType,
82         const std::vector<uint8_t> &arrayBuffer);
83 
84     std::string GetMimeType() const;
85     std::shared_ptr<std::string> GetHtmlText() const;
86     std::shared_ptr<std::string> GetPlainText() const;
87     std::shared_ptr<OHOS::Media::PixelMap> GetPixelMap() const;
88     std::shared_ptr<OHOS::Uri> GetUri() const;
89     std::shared_ptr<OHOS::AAFwk::Want> GetWant() const;
90     std::shared_ptr<MineCustomData> GetCustomData() const;
91 
92     std::string ConvertToText() const;
93 
94     virtual bool Marshalling(Parcel &parcel) const override;
95     static PasteDataRecord *Unmarshalling(Parcel &parcel);
96     bool Encode(std::vector<std::uint8_t> &buffer) override;
97     bool Decode(const std::vector<std::uint8_t> &buffer) override;
98     size_t Count() override;
99     bool WriteFd(MessageParcel &parcel, UriHandler &uriHandler, bool isClient);
100     bool ReadFd(MessageParcel &parcel, UriHandler &uriHandler);
101     bool NeedFd(const UriHandler &uriHandler);
102     void ReplaceShareUri(int32_t userId);
103     void SetConvertUri(const std::string &value);
104 
105     class Builder {
106     public:
107         explicit Builder(const std::string &mimeType);
108         Builder &SetHtmlText(std::shared_ptr<std::string> htmlText);
109         Builder &SetWant(std::shared_ptr<OHOS::AAFwk::Want> want);
110         Builder &SetPlainText(std::shared_ptr<std::string> plainText);
111         Builder &SetUri(std::shared_ptr<OHOS::Uri> uri);
112         Builder &SetPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap);
113         Builder &SetCustomData(std::shared_ptr<MineCustomData> customData);
114         Builder &SetMimeType(std::string mimeType);
115         std::shared_ptr<PasteDataRecord> Build();
116 
117     private:
118         std::shared_ptr<PasteDataRecord> record_ = nullptr;
119     };
120 
121 private:
122     static bool Marshalling(Parcel &parcel, std::shared_ptr<std::string> item);
123     static bool Marshalling(Parcel &parcel, std::shared_ptr<Parcelable> item);
124     template<typename T>
125     static ResultCode UnMarshalling(Parcel &parcel, std::shared_ptr<T> &item);
126     static ResultCode UnMarshalling(Parcel &parcel, std::shared_ptr<std::string> &item);
CheckResult(ResultCode resultCode)127     inline static bool CheckResult(ResultCode resultCode)
128     {
129         return resultCode == ResultCode::OK;
130     }
131     std::string GetPassUri();
132     static std::shared_ptr<OHOS::Media::PixelMap> Raw2PixelMap(const RawMem &rawMem);
133     static RawMem PixelMap2Raw(const std::shared_ptr<OHOS::Media::PixelMap> &pixelMap);
134 
135     std::string mimeType_;
136     std::shared_ptr<std::string> htmlText_;
137     std::shared_ptr<OHOS::AAFwk::Want> want_;
138     std::shared_ptr<std::string> plainText_;
139     std::shared_ptr<OHOS::Uri> uri_;
140     std::string convertUri_;
141     std::shared_ptr<OHOS::Media::PixelMap> pixelMap_;
142     std::shared_ptr<MineCustomData> customData_;
143     std::shared_ptr<FileDescriptor> fd_;
144 };
145 } // namespace MiscServices
146 } // namespace OHOS
147 #endif // PASTE_BOARD_RECORD_H
148