1 /*
2  * Copyright (c) 2022 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 PASTEBOARD_CLIENT_ADAPTER_H
17 #define PASTEBOARD_CLIENT_ADAPTER_H
18 
19 #include <map>
20 #include <memory>
21 #include <vector>
22 
23 namespace OHOS::NWeb {
24 
25 enum class ClipBoardImageColorType { COLOR_TYPE_RGBA_8888, COLOR_TYPE_BGRA_8888, COLOR_TYPE_UNKNOWN };
26 
27 enum class ClipBoardImageAlphaType {
28     ALPHA_TYPE_OPAQUE,
29     ALPHA_TYPE_PREMULTIPLIED,
30     ALPHA_TYPE_POSTMULTIPLIED,
31     ALPHA_TYPE_UNKNOWN
32 };
33 
34 enum class CopyOptionMode { NONE = 0, IN_APP = 1, LOCAL_DEVICE = 2, CROSS_DEVICE = 3 };
35 
36 class ClipBoardImageDataAdapter {
37 public:
38     ClipBoardImageDataAdapter() = default;
39 
40     virtual ~ClipBoardImageDataAdapter() = default;
41 
42     virtual ClipBoardImageColorType GetColorType() = 0;
43 
44     virtual ClipBoardImageAlphaType GetAlphaType() = 0;
45 
46     virtual uint32_t* GetData() = 0;
47 
48     virtual size_t GetDataSize() = 0;
49 
50     virtual size_t GetRowBytes() = 0;
51 
52     virtual int32_t GetWidth() = 0;
53 
54     virtual int32_t GetHeight() = 0;
55 
56     virtual void SetColorType(ClipBoardImageColorType color) = 0;
57 
58     virtual void SetAlphaType(ClipBoardImageAlphaType alpha) = 0;
59 
60     virtual void SetData(uint32_t* data) = 0;
61 
62     virtual void SetDataSize(size_t size) = 0;
63 
64     virtual void SetRowBytes(size_t rowBytes) = 0;
65 
66     virtual void SetWidth(int32_t width) = 0;
67 
68     virtual void SetHeight(int32_t height) = 0;
69 };
70 
71 class PasteDataRecordAdapter;
72 class PasteDataAdapter;
73 using PasteRecordVector = std::vector<std::shared_ptr<PasteDataRecordAdapter>>;
74 using PasteCustomData = std::map<std::string, std::vector<uint8_t>>;
75 
76 class PasteboardObserverAdapter {
77 public:
78     PasteboardObserverAdapter() = default;
79 
80     virtual ~PasteboardObserverAdapter() = default;
81 
82     virtual void OnPasteboardChanged() = 0;
83 };
84 
85 class PasteBoardClientAdapter {
86 public:
87     PasteBoardClientAdapter() = default;
88 
89     virtual ~PasteBoardClientAdapter() = default;
90 
91     virtual bool GetPasteData(PasteRecordVector& data) = 0;
92 
93     virtual void SetPasteData(
94         const PasteRecordVector& data, CopyOptionMode copyOption = CopyOptionMode::CROSS_DEVICE) = 0;
95 
96     virtual bool HasPasteData() = 0;
97 
98     virtual void Clear() = 0;
99 
100     virtual int32_t OpenRemoteUri(const std::string& path) = 0;
101 
102     virtual bool IsLocalPaste() = 0;
103 
104     virtual uint32_t GetTokenId() = 0;
105 
106     virtual int32_t AddPasteboardChangedObserver(std::shared_ptr<PasteboardObserverAdapter> callback) = 0;
107 
108     virtual void RemovePasteboardChangedObserver(int32_t callbackId) = 0;
109 };
110 
111 class PasteDataRecordAdapter {
112 public:
113     PasteDataRecordAdapter() = default;
114 
115     virtual ~PasteDataRecordAdapter() = default;
116 
117     static std::shared_ptr<PasteDataRecordAdapter> NewRecord(const std::string& mimeType);
118 
119     static std::shared_ptr<PasteDataRecordAdapter> NewRecord(
120         const std::string& mimeType, std::shared_ptr<std::string> htmlText, std::shared_ptr<std::string> plainText);
121 
122     virtual bool SetHtmlText(std::shared_ptr<std::string> htmlText) = 0;
123 
124     virtual bool SetPlainText(std::shared_ptr<std::string> plainText) = 0;
125 
126     virtual bool SetImgData(std::shared_ptr<ClipBoardImageDataAdapter> imageData) = 0;
127 
128     virtual std::string GetMimeType() = 0;
129 
130     virtual std::shared_ptr<std::string> GetHtmlText() = 0;
131 
132     virtual std::shared_ptr<std::string> GetPlainText() = 0;
133 
134     virtual bool GetImgData(std::shared_ptr<ClipBoardImageDataAdapter> imageData) = 0;
135 
136     virtual bool SetUri(const std::string& uriString) = 0;
137 
138     virtual bool SetCustomData(PasteCustomData& data) = 0;
139 
140     virtual std::shared_ptr<std::string> GetUri() = 0;
141 
142     virtual std::shared_ptr<PasteCustomData> GetCustomData() = 0;
143 };
144 
145 class PasteDataAdapter {
146 public:
147     PasteDataAdapter() = default;
148 
149     virtual ~PasteDataAdapter() = default;
150 
151     virtual void AddHtmlRecord(const std::string& html) = 0;
152 
153     virtual void AddTextRecord(const std::string& text) = 0;
154 
155     virtual std::vector<std::string> GetMimeTypes() = 0;
156 
157     virtual std::shared_ptr<std::string> GetPrimaryHtml() = 0;
158 
159     virtual std::shared_ptr<std::string> GetPrimaryText() = 0;
160 
161     virtual std::shared_ptr<std::string> GetPrimaryMimeType() = 0;
162 
163     virtual std::shared_ptr<PasteDataRecordAdapter> GetRecordAt(std::size_t index) = 0;
164 
165     virtual std::size_t GetRecordCount() = 0;
166 
167     virtual PasteRecordVector AllRecords() = 0;
168 };
169 
170 } // namespace OHOS::NWeb
171 
172 #endif
173