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_CLIENT_H
17 #define PASTE_BOARD_CLIENT_H
18 
19 #include <functional>
20 #include <singleton.h>
21 #include <condition_variable>
22 #include "i_pasteboard_service.h"
23 #include "paste_data.h"
24 #include "paste_data_record.h"
25 #include "pasteboard_delay_getter.h"
26 #include "pasteboard_observer.h"
27 #include "unified_data.h"
28 #include "want.h"
29 
30 namespace OHOS {
31 namespace MiscServices {
32 class API_EXPORT PasteboardSaDeathRecipient : public IRemoteObject::DeathRecipient {
33 public:
34     explicit PasteboardSaDeathRecipient();
35     ~PasteboardSaDeathRecipient() = default;
36     void OnRemoteDied(const wptr<IRemoteObject> &object) override;
37 
38 private:
39     DISALLOW_COPY_AND_MOVE(PasteboardSaDeathRecipient);
40 };
41 class API_EXPORT PasteboardClient : public DelayedSingleton<PasteboardClient> {
42     DECLARE_DELAYED_SINGLETON(PasteboardClient);
43 
44 public:
45     DISALLOW_COPY_AND_MOVE(PasteboardClient);
46 
47     /**
48      * CreateHtmlTextRecord
49      * @descrition Create Html Text Record.
50      * @param std::string text.
51      * @return PasteDataRecord.
52      */
53     std::shared_ptr<PasteDataRecord> CreateHtmlTextRecord(const std::string &text);
54 
55     /**
56      * CreatePlainTextRecord
57      * @descrition Create Plaint Text Record.
58      * @param std::string text.
59      * @return PasteDataRecord.
60      */
61     std::shared_ptr<PasteDataRecord> CreatePlainTextRecord(const std::string &text);
62 
63     /**
64      * CreatePixelMapRecord
65      * @descrition Create PixelMap Record.
66      * @param OHOS::Media::PixelMap pixelMap.
67      * @return PasteDataRecord.
68      */
69     std::shared_ptr<PasteDataRecord> CreatePixelMapRecord(std::shared_ptr<OHOS::Media::PixelMap> pixelMap);
70 
71     /**
72      * CreateUriRecord
73      * @descrition Create Uri Text Record.
74      * @param OHOS::Uri uri.
75      * @return PasteDataRecord.
76      */
77     std::shared_ptr<PasteDataRecord> CreateUriRecord(const OHOS::Uri &uri);
78 
79     /**
80      * CreateWantRecord
81      * @descrition Create Plaint Want Record.
82      * @param OHOS::AAFwk::Want want.
83      * @return PasteDataRecord.
84      */
85     std::shared_ptr<PasteDataRecord> CreateWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want);
86 
87     /**
88      * CreateKvRecord
89      * @descrition Create Kv Record.
90      * @param std::string mimeType
91      * @param std::vector<uint8_t> arrayBuffer
92      * @return PasteDataRecord.
93      */
94     std::shared_ptr<PasteDataRecord> CreateKvRecord(
95         const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer);
96 
97     /**
98      * CreateMultiDelayRecord
99      * @descrition Create Multi DelayRecord.
100      * @param std::vector<std::string> mimeTypes
101      * @param std::shared_ptr<UDMF::EntryGetter> entryGetter
102      * @return PasteDataRecord.
103      */
104     std::shared_ptr<PasteDataRecord> CreateMultiDelayRecord(
105         std::vector<std::string> mimeTypes, const std::shared_ptr<UDMF::EntryGetter> entryGetter);
106 
107     /**
108      * CreateHtmlData
109      * @descrition Create Html Paste Data.
110      * @param std::string text  .
111      * @return PasteData.
112      */
113     std::shared_ptr<PasteData> CreateHtmlData(const std::string &htmlText);
114 
115     /**
116      * CreatePlainTextData
117      * @descritionCreate Plain Text Paste Data.
118      * @param std::string text .
119      * @return PasteData.
120      */
121     std::shared_ptr<PasteData> CreatePlainTextData(const std::string &text);
122 
123     /**
124      * CreatePixelMapData
125      * @descrition Create PixelMap Paste Data.
126      * @param OHOS::Media::PixelMap pixelMap .
127      * @return PasteData.
128      */
129     std::shared_ptr<PasteData> CreatePixelMapData(std::shared_ptr<OHOS::Media::PixelMap> pixelMap);
130 
131     /**
132      * CreateUriData
133      * @descrition Create Uri Paste Data.
134      * @param OHOS::Uri uri .
135      * @return PasteData.
136      */
137     std::shared_ptr<PasteData> CreateUriData(const OHOS::Uri &uri);
138 
139     /**
140      * CreateWantData
141      * @descrition Create Want Paste Data.
142      * @param OHOS::AAFwk::Want want .
143      * @return PasteData.
144      */
145     std::shared_ptr<PasteData> CreateWantData(std::shared_ptr<OHOS::AAFwk::Want> want);
146 
147     /**
148      * CreateKvData
149      * @descrition Create Kv Paste Data.
150      * @param std::string mimeType
151      * @param std::vector<uint8_t> arrayBuffer
152      * @return PasteData.
153      */
154     std::shared_ptr<PasteData> CreateKvData(const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer);
155 
156     /**
157      * CreateMultiTypeData
158      * @descrition Create multi-type Data.
159      * @param std::map<std::string, EntryValue> typeValueMap
160      * @param recordMimeType record's default mimeType
161      * @return PasteData.
162      */
163     std::shared_ptr<PasteData> CreateMultiTypeData(
164         std::shared_ptr<std::map<std::string, std::shared_ptr<EntryValue>>> typeValueMap,
165         const std::string &recordMimeType = "");
166 
167     /**
168      * CreateMultiTypeDelayData
169      * @descrition Create delayed multi-type Data.
170      * @param std::vector<std::string> utdTypes
171      * @param std::shared_ptr<UDMF::EntryGetter> entryGetter
172      * @return PasteData.
173      */
174     std::shared_ptr<PasteData> CreateMultiTypeDelayData(std::vector<std::string> mimeTypes,
175         std::shared_ptr<UDMF::EntryGetter> entryGetter);
176 
177     /**
178      * GetRecordValueByType
179      * @descrition get entry value from the pasteboard.
180      * @param dataId the dataId of the PasteData.
181      * @param recordId the recordId of the PasteRecord.
182      * @param value the value of the PasteDataEntry.
183      * @return int32_t.
184      */
185     int32_t GetRecordValueByType(uint32_t dataId, uint32_t recordId, PasteDataEntry& value);
186 
187     /**
188      * GetPasteData
189      * @descrition get paste data from the pasteboard.
190      * @param pasteData the object of the PasteDate.
191      * @return int32_t.
192      */
193     int32_t GetPasteData(PasteData &pasteData);
194 
195     /**
196      * GetMimeTypes
197      * @descrition get mime types from the pasteboard.
198      * @return Returns MimeTypes
199      */
200     std::vector<std::string> GetMimeTypes();
201 
202     /**
203      * HasPasteData
204      * @descrition check paste data exist in the pasteboard.
205      * @return bool. True exists, false does not exist
206      */
207     bool HasPasteData();
208 
209     /**
210      * Clear
211      * @descrition Clear Current pasteboard data.
212      * @return void.
213      */
214     void Clear();
215 
216     /**
217      * SetPasteData
218      * @descrition set paste data to the pasteboard.
219      * @param pasteData the object of the PasteData.
220      * @param pasteData the object of the PasteboardDelayGetter.
221      * @param pasteData the map of the EntryGetter.
222      * @return int32_t.
223      */
224     int32_t SetPasteData(PasteData &pasteData, std::shared_ptr<PasteboardDelayGetter> delayGetter = nullptr,
225         std::map<uint32_t, std::shared_ptr<UDMF::EntryGetter>> entryGetters = {});
226 
227     /**
228      * SetPasteData
229      * @descrition set paste data to the pasteboard.
230      * @param unifiedData the object of the PasteDate.
231      * @return int32_t.
232      */
233     int32_t SetUnifiedData(const UDMF::UnifiedData &unifiedData,
234         std::shared_ptr<PasteboardDelayGetter> delayGetter = nullptr);
235 
236     /**
237      * SetPasteData
238      * @descrition set paste data to the pasteboard.
239      * @param unifiedData the object of the PasteDate.
240      * @return int32_t.
241      */
242     int32_t GetUnifiedData(UDMF::UnifiedData &unifiedData);
243 
244     /**
245      * SetUdsdData
246      * @descrition set unified data with uds entries to the pasteboard.
247      * @param unifiedData the object of the PasteDate.
248      * @return int32_t.
249      */
250     int32_t SetUdsdData(const UDMF::UnifiedData& unifiedData);
251 
252     /**
253      * GetUnifiedDataWithEntry
254      * @descrition get unified data with uds entries from the pasteboard.
255      * @param unifiedData the object of the PasteDate.
256      * @return int32_t.
257      */
258     int32_t GetUdsdData(UDMF::UnifiedData& unifiedData);
259 
260     /**
261      * IsRemoteData
262      * @descrition check if remote data.
263      * @return bool. True is remote data, else false.
264      */
265     bool IsRemoteData();
266 
267     /**
268      * GetDataSource
269      * @descrition Obtain the package name of the data source application.
270      * @param std::string bundleName The package name of the application.
271      * @return int32_t.
272      */
273     int32_t GetDataSource(std::string &bundleName);
274 
275     /**
276      * HasDataType
277      * @descrition Check if there is data of the specified type in the pasteboard.
278      * @param std::string mimeType Specified mimetype.
279      * @return bool. True exists, false does not exist
280      */
281     bool HasDataType(const std::string &mimeType);
282 
283     /**
284      * DetectPatterns
285      * @description Checks the specified patterns contained in clipboard, and removes if not found.
286      * @param patternsToCheck A reference to an set of Pattern to check against the clipboard.
287      * @return Returns DetectPatterns.
288      */
289     std::set<Pattern> DetectPatterns(const std::set<Pattern> &patternsToCheck);
290 
291     /**
292      * Subscribe
293      * @descrition
294      * @param type observer type
295      * @param observer pasteboard change callback.
296      * @return void.
297      */
298     void Subscribe(PasteboardObserverType type, sptr<PasteboardObserver> callback);
299 
300     /**
301      * AddPasteboardChangedObserver
302      * @descrition
303      * @param observer pasteboard change callback.
304      * @return void.
305      */
306     void AddPasteboardChangedObserver(sptr<PasteboardObserver> callback);
307 
308     /**
309      * AddPasteboardEventObserver
310      * @descrition
311      * @param observer pasteboard event(read or change) callback.
312      * @return void.
313      */
314     void AddPasteboardEventObserver(sptr<PasteboardObserver> callback);
315 
316         /**
317      * Unsubscribe
318      * @descrition
319      * @param type observer type
320      * @param observer pasteboard change callback.
321      * @return void.
322      */
323     void Unsubscribe(PasteboardObserverType type, sptr<PasteboardObserver> callback);
324 
325     /**
326      * RemovePasteboardChangedObserver
327      * @descrition
328      * @param observer pasteboard change callback.
329      * @return void.
330      */
331     void RemovePasteboardChangedObserver(sptr<PasteboardObserver> callback);
332 
333     /**
334      * RemovePasteboardEventObserver
335      * @descrition
336      * @param observer pasteboard event callback.
337      * @return void.
338      */
339     void RemovePasteboardEventObserver(sptr<PasteboardObserver> callback);
340 
341     /**
342      * SetGlobalShareOption
343      * @descrition Set globalShareOptions.
344      * @param globalShareOption globalShareOptions
345      * @return int32_t
346      */
347     int32_t SetGlobalShareOption(const std::map<uint32_t, ShareOption> &globalShareOptions);
348 
349     /**
350      * RemoveGlobalShareOption
351      * @descrition Remove globalShareOptions.
352      * @param tokenId tokenIds
353      * @return int32_t
354      */
355     int32_t RemoveGlobalShareOption(const std::vector<uint32_t> &tokenIds);
356 
357     /**
358      * GetGlobalShareOption
359      * @descrition Get globalShareOptions.
360      * @param tokenId tokenIds
361      * @return globalShareOptions
362      */
363     std::map<uint32_t, ShareOption> GetGlobalShareOption(const std::vector<uint32_t> &tokenIds);
364 
365     /**
366      * SetAppShareOptions
367      * @description Sets a unified ShareOptions for the application.
368      * @param shareOptions shareOptions
369      * @return result
370      */
371     int32_t SetAppShareOptions(const ShareOption &shareOptions);
372 
373     /**
374      * RemoveAppShareOptions
375      * @description Removes the ShareOptions for the application.
376      * @return result
377      */
378     int32_t RemoveAppShareOptions();
379 
380     /**
381      * OnRemoteSaDied
382      * @descrition
383      * @param object systemAbility proxy object
384      * @return void.
385      */
386     void OnRemoteSaDied(const wptr<IRemoteObject> &object);
387 
388     /**
389      * LoadSystemAbilitySuccess
390      * @descrition inherit SystemAbilityLoadCallbackStub override LoadSystemAbilitySuccess
391      * @param remoteObject systemAbility proxy object.
392      * @return void.
393      */
394     void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
395 
396     /**
397      * LoadSystemAbilityFail
398      * @descrition inherit SystemAbilityLoadCallbackStub override LoadSystemAbilityFail
399      * @return void.
400      */
401     void LoadSystemAbilityFail();
402 
403     /**
404     * PasteStart
405     * @descrition Utilized to notify pasteboard service while reading PasteData, in this case, the service will help to
406     *     preserve the context and resources
407     * @return void.
408     */
409     void PasteStart(const std::string &pasteId);
410 
411     /**
412      * PasteComplete
413      * @descrition Invoked to notify pasteboard service the utilization of PasteData has completed and occupied
414      *     resources can be released for further usage
415      * @return void.
416      */
417     void PasteComplete(const std::string &deviceId, const std::string &pasteId);
418 
419 private:
420     sptr<IPasteboardService> GetPasteboardService();
421     sptr<IPasteboardService> GetPasteboardServiceProxy();
422     static void RetainUri(PasteData &pasteData);
423     static void SplitWebviewPasteData(PasteData &pasteData);
424     static void RefreshUri(std::shared_ptr<PasteDataRecord> &record);
425     static sptr<IPasteboardService> pasteboardServiceProxy_;
426     static std::mutex instanceLock_;
427     static std::condition_variable proxyConVar_;
428     sptr<IRemoteObject::DeathRecipient> deathRecipient_{ nullptr };
429     std::atomic<uint32_t> getSequenceId_ = 0;
430     class StaticDestoryMonitor {
431         public:
StaticDestoryMonitor()432             StaticDestoryMonitor() : destoryed_(false) {}
~StaticDestoryMonitor()433             ~StaticDestoryMonitor()
434             {
435                 destoryed_ = true;
436             }
437 
IsDestoryed()438             bool IsDestoryed() const
439             {
440                 return destoryed_;
441             }
442 
443         private:
444             bool destoryed_;
445     };
446     static StaticDestoryMonitor staticDestoryMonitor_;
447     void RebuildWebviewPasteData(PasteData &pasteData);
448     void Init();
449 };
450 } // namespace MiscServices
451 } // namespace OHOS
452 #endif // PASTE_BOARD_CLIENT_H
453