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