• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 <singleton.h>
20 
21 #include "entity_recognition_observer.h"
22 #include "pasteboard_observer.h"
23 #include "pasteboard_progress_signal.h"
24 
25 namespace OHOS {
26 namespace MiscServices {
27 class API_EXPORT PasteboardSaDeathRecipient : public IRemoteObject::DeathRecipient {
28 public:
29     explicit PasteboardSaDeathRecipient();
30     ~PasteboardSaDeathRecipient() = default;
31     void OnRemoteDied(const wptr<IRemoteObject> &object) override;
32 
33 private:
34     DISALLOW_COPY_AND_MOVE(PasteboardSaDeathRecipient);
35 };
36 
37 enum ProgressStatus {
38     NORMAL_PASTE = 0,
39     CANCEL_PASTE = 1,
40     PASTE_TIME_OUT = 2,
41 };
42 
43 enum FileConflictOption {
44     FILE_OVERWRITE = 0,
45     FILE_SKIP = 1,
46     FILE_RENAME = 2
47 };
48 
49 enum ProgressIndicator {
50     NONE_PROGRESS_INDICATOR = 0,
51     DEFAULT_PROGRESS_INDICATOR = 1
52 };
53 
54 struct PasteDataFromServiceInfo {
55     pid_t pid;
56     std::string currentPid;
57     std::string currentId;
58 };
59 
60 struct ProgressReportLintener {
61     void (*OnProgressFail)(int32_t result);
62 };
63 
64 struct ProgressInfo {
65     int percentage;
66 };
67 
68 struct GetDataParams;
69 struct ProgressListener {
70     void (*ProgressNotify)(std::shared_ptr<GetDataParams> params);
71 };
72 
73 struct GetDataParams {
74     std::string destUri;
75     enum FileConflictOption fileConflictOption;
76     enum ProgressIndicator progressIndicator;
77     struct ProgressListener listener;
78     std::shared_ptr<ProgressSignalClient> progressSignal;
79     ProgressInfo *info;
80 };
81 
82 class API_EXPORT PasteboardClient : public DelayedSingleton<PasteboardClient> {
83     DECLARE_DELAYED_SINGLETON(PasteboardClient);
84 
85 public:
86     DISALLOW_COPY_AND_MOVE(PasteboardClient);
87 
88     /**
89      * CreateHtmlTextRecord
90      * @descrition Create Html Text Record.
91      * @param std::string text.
92      * @return PasteDataRecord.
93      */
94     std::shared_ptr<PasteDataRecord> CreateHtmlTextRecord(const std::string &text);
95 
96     /**
97      * CreatePlainTextRecord
98      * @descrition Create Plaint Text Record.
99      * @param std::string text.
100      * @return PasteDataRecord.
101      */
102     std::shared_ptr<PasteDataRecord> CreatePlainTextRecord(const std::string &text);
103 
104     /**
105      * CreatePixelMapRecord
106      * @descrition Create PixelMap Record.
107      * @param OHOS::Media::PixelMap pixelMap.
108      * @return PasteDataRecord.
109      */
110     std::shared_ptr<PasteDataRecord> CreatePixelMapRecord(std::shared_ptr<OHOS::Media::PixelMap> pixelMap);
111 
112     /**
113      * CreateUriRecord
114      * @descrition Create Uri Text Record.
115      * @param OHOS::Uri uri.
116      * @return PasteDataRecord.
117      */
118     std::shared_ptr<PasteDataRecord> CreateUriRecord(const OHOS::Uri &uri);
119 
120     /**
121      * CreateWantRecord
122      * @descrition Create Plaint Want Record.
123      * @param OHOS::AAFwk::Want want.
124      * @return PasteDataRecord.
125      */
126     std::shared_ptr<PasteDataRecord> CreateWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want);
127 
128     /**
129      * CreateKvRecord
130      * @descrition Create Kv Record.
131      * @param std::string mimeType
132      * @param std::vector<uint8_t> arrayBuffer
133      * @return PasteDataRecord.
134      */
135     std::shared_ptr<PasteDataRecord> CreateKvRecord(
136         const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer);
137 
138     /**
139      * CreateMultiDelayRecord
140      * @descrition Create Multi DelayRecord.
141      * @param std::vector<std::string> mimeTypes
142      * @param std::shared_ptr<UDMF::EntryGetter> entryGetter
143      * @return PasteDataRecord.
144      */
145     std::shared_ptr<PasteDataRecord> CreateMultiDelayRecord(
146         std::vector<std::string> mimeTypes, const std::shared_ptr<UDMF::EntryGetter> entryGetter);
147 
148     /**
149      * CreateHtmlData
150      * @descrition Create Html Paste Data.
151      * @param std::string text  .
152      * @return PasteData.
153      */
154     std::shared_ptr<PasteData> CreateHtmlData(const std::string &htmlText);
155 
156     /**
157      * CreatePlainTextData
158      * @descritionCreate Plain Text Paste Data.
159      * @param std::string text .
160      * @return PasteData.
161      */
162     std::shared_ptr<PasteData> CreatePlainTextData(const std::string &text);
163 
164     /**
165      * CreatePixelMapData
166      * @descrition Create PixelMap Paste Data.
167      * @param OHOS::Media::PixelMap pixelMap .
168      * @return PasteData.
169      */
170     std::shared_ptr<PasteData> CreatePixelMapData(std::shared_ptr<OHOS::Media::PixelMap> pixelMap);
171 
172     /**
173      * CreateUriData
174      * @descrition Create Uri Paste Data.
175      * @param OHOS::Uri uri .
176      * @return PasteData.
177      */
178     std::shared_ptr<PasteData> CreateUriData(const OHOS::Uri &uri);
179 
180     /**
181      * CreateWantData
182      * @descrition Create Want Paste Data.
183      * @param OHOS::AAFwk::Want want .
184      * @return PasteData.
185      */
186     std::shared_ptr<PasteData> CreateWantData(std::shared_ptr<OHOS::AAFwk::Want> want);
187 
188     /**
189      * CreateKvData
190      * @descrition Create Kv Paste Data.
191      * @param std::string mimeType
192      * @param std::vector<uint8_t> arrayBuffer
193      * @return PasteData.
194      */
195     std::shared_ptr<PasteData> CreateKvData(const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer);
196 
197     /**
198      * CreateMultiTypeData
199      * @descrition Create multi-type Data.
200      * @param std::map<std::string, EntryValue> typeValueMap
201      * @param recordMimeType record's default mimeType
202      * @return PasteData.
203      */
204     std::shared_ptr<PasteData> CreateMultiTypeData(
205        std::shared_ptr<std::map<std::string, std::shared_ptr<EntryValue>>> typeValueMap,
206        const std::string &recordMimeType = "");
207 
208     /**
209      * CreateMultiTypeDelayData
210      * @descrition Create delayed multi-type Data.
211      * @param std::vector<std::string> utdTypes
212      * @param std::shared_ptr<UDMF::EntryGetter> entryGetter
213      * @return PasteData.
214      */
215     std::shared_ptr<PasteData> CreateMultiTypeDelayData(std::vector<std::string> mimeTypes,
216         std::shared_ptr<UDMF::EntryGetter> entryGetter);
217 
218     /**
219      * GetChangeCount
220      * @descrition get clip changed count from the pasteboard.
221      * @param changeCount the changeCount of the PasteData.
222      * @return int32_t.
223      */
224     int32_t GetChangeCount(uint32_t &changeCount);
225 
226     /**
227      * SubscribeEntityObserver
228      * @description Subscribe the EntityRecognitionObserver.
229      * @param entityType the type of recognized PasteData.
230      * @param expectedDataLength the length of PasteData expected to observer.
231      * @param observer callback observer when recognized PasteData.
232      * @return int32_t.
233      */
234     int32_t SubscribeEntityObserver(
235         EntityType entityType, uint32_t expectedDataLength, const sptr<EntityRecognitionObserver> &observer);
236 
237     /**
238      * SubscribeEntityObserver
239      * @description Subscribe the EntityRecognitionObserver.
240      * @param entityType the type of recognized PasteData.
241      * @param expectedDataLength the length of PasteData expected to observer.
242      * @param observer callback observer when recognized PasteData.
243      * @return int32_t.
244      */
245     int32_t UnsubscribeEntityObserver(
246         EntityType entityType, uint32_t expectedDataLength, const sptr<EntityRecognitionObserver> &observer);
247 
248     /**
249      * GetRecordValueByType
250      * @descrition get entry value from the pasteboard.
251      * @param dataId the dataId of the PasteData.
252      * @param recordId the recordId of the PasteRecord.
253      * @param value the value of the PasteDataEntry.
254      * @return int32_t.
255      */
256     int32_t GetRecordValueByType(uint32_t dataId, uint32_t recordId, PasteDataEntry& value);
257 
258     /**
259      * GetPasteData
260      * @descrition get paste data from the pasteboard.
261      * @param pasteData the object of the PasteDate.
262      * @return int32_t.
263      */
264     int32_t GetPasteData(PasteData &pasteData);
265 
266     /**
267      * GetMimeTypes
268      * @descrition get mime types from the pasteboard.
269      * @return Returns MimeTypes
270      */
271     std::vector<std::string> GetMimeTypes();
272 
273     /**
274      * HasPasteData
275      * @descrition check paste data exist in the pasteboard.
276      * @return bool. True exists, false does not exist
277      */
278     bool HasPasteData();
279 
280     /**
281      * Clear
282      * @descrition Clear Current pasteboard data.
283      * @return void.
284      */
285     void Clear();
286 
287     /**
288      * SetPasteData
289      * @descrition set paste data to the pasteboard.
290      * @param pasteData the object of the PasteData.
291      * @param pasteData the object of the PasteboardDelayGetter.
292      * @param pasteData the map of the EntryGetter.
293      * @return int32_t.
294      */
295     int32_t SetPasteData(PasteData &pasteData, std::shared_ptr<PasteboardDelayGetter> delayGetter = nullptr,
296         std::map<uint32_t, std::shared_ptr<UDMF::EntryGetter>> entryGetters = {});
297 
298     /**
299      * SetPasteData
300      * @descrition set paste data to the pasteboard.
301      * @param unifiedData the object of the PasteDate.
302      * @return int32_t.
303      */
304     int32_t SetUnifiedData(
305         const UDMF::UnifiedData &unifiedData, std::shared_ptr<PasteboardDelayGetter> delayGetter = nullptr);
306 
307     /**
308      * SetPasteData
309      * @descrition set paste data to the pasteboard.
310      * @param unifiedData the object of the PasteDate.
311      * @return int32_t.
312      */
313     int32_t GetUnifiedData(UDMF::UnifiedData &unifiedData);
314 
315     /**
316      * SetUdsdData
317      * @descrition set unified data with uds entries to the pasteboard.
318      * @param unifiedData the object of the PasteDate.
319      * @return int32_t.
320      */
321     int32_t SetUdsdData(const UDMF::UnifiedData &unifiedData);
322 
323     /**
324      * GetUnifiedDataWithEntry
325      * @descrition get unified data with uds entries from the pasteboard.
326      * @param unifiedData the object of the PasteDate.
327      * @return int32_t.
328      */
329     int32_t GetUdsdData(UDMF::UnifiedData &unifiedData);
330 
331     /**
332      * IsRemoteData
333      * @descrition check if remote data.
334      * @return bool. True is remote data, else false.
335      */
336     bool IsRemoteData();
337 
338     /**
339      * GetDataSource
340      * @descrition Obtain the package name of the data source application.
341      * @param std::string bundleName The package name of the application.
342      * @return int32_t.
343      */
344     int32_t GetDataSource(std::string &bundleName);
345 
346     /**
347      * HasDataType
348      * @descrition Check if there is data of the specified type in the pasteboard.
349      * @param std::string mimeType Specified mimetype.
350      * @return bool. True exists, false does not exist
351      */
352     bool HasDataType(const std::string &mimeType);
353 
354     /**
355      * DetectPatterns
356      * @description Checks the specified patterns contained in clipboard, and removes if not found.
357      * @param patternsToCheck A reference to an set of Pattern to check against the clipboard.
358      * @return Returns DetectPatterns.
359      */
360     std::set<Pattern> DetectPatterns(const std::set<Pattern> &patternsToCheck);
361 
362     /**
363      * Subscribe
364      * @descrition
365      * @param type observer type
366      * @param observer pasteboard change callback.
367      * @return void.
368      */
369     void Subscribe(PasteboardObserverType type, sptr<PasteboardObserver> callback);
370 
371     /**
372      * AddPasteboardChangedObserver
373      * @descrition
374      * @param observer pasteboard change callback.
375      * @return void.
376      */
377     void AddPasteboardChangedObserver(sptr<PasteboardObserver> callback);
378 
379     /**
380      * AddPasteboardEventObserver
381      * @descrition
382      * @param observer pasteboard event(read or change) callback.
383      * @return void.
384      */
385     void AddPasteboardEventObserver(sptr<PasteboardObserver> callback);
386 
387     /**
388      * Unsubscribe
389      * @descrition
390      * @param type observer type
391      * @param observer pasteboard change callback.
392      * @return void.
393      */
394     void Unsubscribe(PasteboardObserverType type, sptr<PasteboardObserver> callback);
395 
396     /**
397      * RemovePasteboardChangedObserver
398      * @descrition
399      * @param observer pasteboard change callback.
400      * @return void.
401      */
402     void RemovePasteboardChangedObserver(sptr<PasteboardObserver> callback);
403 
404     /**
405      * RemovePasteboardEventObserver
406      * @descrition
407      * @param observer pasteboard event callback.
408      * @return void.
409      */
410     void RemovePasteboardEventObserver(sptr<PasteboardObserver> callback);
411 
412     /**
413      * SetGlobalShareOption
414      * @descrition Set globalShareOptions.
415      * @param globalShareOption globalShareOptions
416      * @return int32_t
417      */
418     int32_t SetGlobalShareOption(const std::map<uint32_t, ShareOption> &globalShareOptions);
419 
420     /**
421      * RemoveGlobalShareOption
422      * @descrition Remove globalShareOptions.
423      * @param tokenId tokenIds
424      * @return int32_t
425      */
426     int32_t RemoveGlobalShareOption(const std::vector<uint32_t> &tokenIds);
427 
428     /**
429      * GetGlobalShareOption
430      * @descrition Get globalShareOptions.
431      * @param tokenId tokenIds
432      * @return globalShareOptions
433      */
434     std::map<uint32_t, ShareOption> GetGlobalShareOption(const std::vector<uint32_t> &tokenIds);
435 
436     /**
437      * SetAppShareOptions
438      * @description Sets a unified ShareOptions for the application.
439      * @param shareOptions shareOptions
440      * @return result
441      */
442     int32_t SetAppShareOptions(const ShareOption &shareOptions);
443 
444     /**
445      * RemoveAppShareOptions
446      * @description Removes the ShareOptions for the application.
447      * @return result
448      */
449     int32_t RemoveAppShareOptions();
450 
451     /**
452      * OnRemoteSaDied
453      * @descrition
454      * @param object systemAbility proxy object
455      * @return void.
456      */
457     void OnRemoteSaDied(const wptr<IRemoteObject> &object);
458 
459     /**
460      * LoadSystemAbilitySuccess
461      * @descrition inherit SystemAbilityLoadCallbackStub override LoadSystemAbilitySuccess
462      * @param remoteObject systemAbility proxy object.
463      * @return void.
464      */
465     void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
466 
467     /**
468      * LoadSystemAbilityFail
469      * @descrition inherit SystemAbilityLoadCallbackStub override LoadSystemAbilityFail
470      * @return void.
471      */
472     void LoadSystemAbilityFail();
473 
474     /**
475      * PasteStart
476      * @descrition Utilized to notify pasteboard service while reading PasteData, in this case, the service will help to
477      *     preserve the context and resources
478      * @return void.
479      */
480     void PasteStart(const std::string &pasteId);
481 
482     /**
483      * PasteComplete
484      * @descrition Invoked to notify pasteboard service the utilization of PasteData has completed and occupied
485      *     resources can be released for further usage
486      * @return void.
487      */
488     void PasteComplete(const std::string &deviceId, const std::string &pasteId);
489 
490     /**
491      * GetDataWithProgress
492      * @descrition Get pastedata from the system pasteboard with system progress indicator.
493      * @param pasteData the object of the PasteData.
494      * @param params - Indicates the {@link GetDataParams}.
495      * @returns int32_t
496      */
497     int32_t GetDataWithProgress(PasteData &pasteData, std::shared_ptr<GetDataParams> params);
498 
499     /**
500      * GetUnifiedDataWithProgress
501      * @descrition Get pastedata from the system pasteboard with system progress indicator.
502      * @param unifiedData - the object of the PasteData.
503      * @param params - Indicates the {@link GetDataParams}.
504      * @returns int32_t
505      */
506     int32_t GetUnifiedDataWithProgress(UDMF::UnifiedData &unifiedData, std::shared_ptr<GetDataParams> params);
507 
508     /**
509      * GetRemoteDeviceName
510      * @descrition Obtain the remote device name.
511      * @param std::string deviceName - the device name of the remote device.
512      * @returns int32_t
513      */
514     int32_t GetRemoteDeviceName(std::string &deviceName, bool &isRemote);
515 
516     /**
517      * HandleSignalValue
518      * @descrition Handle hap signal value.
519      * @param std::string signalValue - the value of hap ipc proxy.
520      * @returns int32_t
521      */
522     int32_t HandleSignalValue(const std::string &signalValue);
523 
524 private:
525     sptr<IPasteboardService> GetPasteboardService();
526     sptr<IPasteboardService> GetPasteboardServiceProxy();
527     static void GetProgressByProgressInfo(std::shared_ptr<GetDataParams> params);
528     static int32_t SetProgressWithoutFile(std::string &progressKey, std::shared_ptr<GetDataParams> params);
529     static void ProgressSmoothToTwentyPercent(PasteData &pasteData, std::string &progressKey,
530        std::shared_ptr<GetDataParams> params);
531     int32_t GetPasteDataFromService(PasteData &pasteData, PasteDataFromServiceInfo &pasteDataFromServiceInfo,
532        std::string progressKey, std::shared_ptr<GetDataParams> params);
533     static void UpdateProgress(std::shared_ptr<GetDataParams> params, int progressValue);
534     static std::atomic<uint64_t> progressStartTime_;
535     static void OnProgressAbnormal(int32_t result);
536     void ProgressRadarReport(PasteData &pasteData, PasteDataFromServiceInfo &pasteDataFromServiceInfo);
537     static int32_t ProgressAfterTwentyPercent(PasteData &pasteData, std::shared_ptr<GetDataParams> params,
538        std::string progressKey);
539     void SetPasteboardServiceProxy(const sptr<IRemoteObject> &remoteObject);
540     static int32_t CheckProgressParam(std::shared_ptr<GetDataParams> params);
541     void ShowProgress(const std::string &progressKey);
542     static sptr<IPasteboardService> pasteboardServiceProxy_;
543     static std::mutex instanceLock_;
544     bool constructing_ = false;
545     static std::condition_variable proxyConVar_;
546     sptr<IRemoteObject::DeathRecipient> deathRecipient_{ nullptr };
547     std::atomic<uint32_t> getSequenceId_ = 0;
548     static std::atomic<bool> remoteTask_;
549     static std::atomic<bool> isPasting_;
550     class StaticDestoryMonitor {
551     public:
StaticDestoryMonitor()552         StaticDestoryMonitor() : destoryed_(false) {}
~StaticDestoryMonitor()553         ~StaticDestoryMonitor()
554         {
555             destoryed_ = true;
556         }
557 
IsDestoryed()558         bool IsDestoryed() const
559         {
560             return destoryed_;
561         }
562 
563     private:
564         bool destoryed_;
565     };
566     static StaticDestoryMonitor staticDestoryMonitor_;
567 };
568 } // namespace MiscServices
569 } // namespace OHOS
570 #endif // PASTE_BOARD_CLIENT_H
571