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