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 * CreateHtmlData 99 * @descrition Create Html Paste Data. 100 * @param std::string text . 101 * @return PasteData. 102 */ 103 std::shared_ptr<PasteData> CreateHtmlData(const std::string &htmlText); 104 105 /** 106 * CreatePlainTextData 107 * @descritionCreate Plain Text Paste Data. 108 * @param std::string text . 109 * @return PasteData. 110 */ 111 std::shared_ptr<PasteData> CreatePlainTextData(const std::string &text); 112 113 /** 114 * CreatePixelMapData 115 * @descrition Create PixelMap Paste Data. 116 * @param OHOS::Media::PixelMap pixelMap . 117 * @return PasteData. 118 */ 119 std::shared_ptr<PasteData> CreatePixelMapData(std::shared_ptr<OHOS::Media::PixelMap> pixelMap); 120 121 /** 122 * CreateUriData 123 * @descrition Create Uri Paste Data. 124 * @param OHOS::Uri uri . 125 * @return PasteData. 126 */ 127 std::shared_ptr<PasteData> CreateUriData(const OHOS::Uri &uri); 128 129 /** 130 * CreateWantData 131 * @descrition Create Want Paste Data. 132 * @param OHOS::AAFwk::Want want . 133 * @return PasteData. 134 */ 135 std::shared_ptr<PasteData> CreateWantData(std::shared_ptr<OHOS::AAFwk::Want> want); 136 137 /** 138 * CreateKvData 139 * @descrition Create Kv Paste Data. 140 * @param std::string mimeType 141 * @param std::vector<uint8_t> arrayBuffer 142 * @return PasteData. 143 */ 144 std::shared_ptr<PasteData> CreateKvData(const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer); 145 146 /** 147 * GetRecordValueByType 148 * @descrition get entry value from the pasteboard. 149 * @param dataId the dataId of the PasteData. 150 * @param recordId the recordId of the PasteRecord. 151 * @param value the value of the PasteDataEntry. 152 * @return int32_t. 153 */ 154 int32_t GetRecordValueByType(uint32_t dataId, uint32_t recordId, PasteDataEntry& value); 155 156 /** 157 * GetPasteData 158 * @descrition get paste data from the pasteboard. 159 * @param pasteData the object of the PasteDate. 160 * @return int32_t. 161 */ 162 int32_t GetPasteData(PasteData &pasteData); 163 164 /** 165 * HasPasteData 166 * @descrition check paste data exist in the pasteboard. 167 * @return bool. True exists, false does not exist 168 */ 169 bool HasPasteData(); 170 171 /** 172 * Clear 173 * @descrition Clear Current pasteboard data. 174 * @return void. 175 */ 176 void Clear(); 177 178 /** 179 * SetPasteData 180 * @descrition set paste data to the pasteboard. 181 * @param pasteData the object of the PasteData. 182 * @param pasteData the object of the PasteboardDelayGetter. 183 * @param pasteData the map of the EntryGetter. 184 * @return int32_t. 185 */ 186 int32_t SetPasteData(PasteData &pasteData, std::shared_ptr<PasteboardDelayGetter> delayGetter = nullptr, 187 std::map<uint32_t, std::shared_ptr<UDMF::EntryGetter>> entryGetters = {}); 188 189 /** 190 * SetPasteData 191 * @descrition set paste data to the pasteboard. 192 * @param unifiedData the object of the PasteDate. 193 * @return int32_t. 194 */ 195 int32_t SetUnifiedData(const UDMF::UnifiedData &unifiedData, 196 std::shared_ptr<PasteboardDelayGetter> delayGetter = nullptr); 197 198 /** 199 * SetPasteData 200 * @descrition set paste data to the pasteboard. 201 * @param unifiedData the object of the PasteDate. 202 * @return int32_t. 203 */ 204 int32_t GetUnifiedData(UDMF::UnifiedData &unifiedData); 205 206 /** 207 * SetUdsdData 208 * @descrition set unified data with uds entries to the pasteboard. 209 * @param unifiedData the object of the PasteDate. 210 * @return int32_t. 211 */ 212 int32_t SetUdsdData(const UDMF::UnifiedData& unifiedData); 213 214 /** 215 * GetUnifiedDataWithEntry 216 * @descrition get unified data with uds entries from the pasteboard. 217 * @param unifiedData the object of the PasteDate. 218 * @return int32_t. 219 */ 220 int32_t GetUdsdData(UDMF::UnifiedData& unifiedData); 221 222 /** 223 * IsRemoteData 224 * @descrition check if remote data. 225 * @return bool. True is remote data, else false. 226 */ 227 bool IsRemoteData(); 228 229 /** 230 * GetDataSource 231 * @descrition Obtain the package name of the data source application. 232 * @param std::string bundleName The package name of the application. 233 * @return int32_t. 234 */ 235 int32_t GetDataSource(std::string &bundleName); 236 237 /** 238 * HasDataType 239 * @descrition Check if there is data of the specified type in the pasteboard. 240 * @param std::string mimeType Specified mimetype. 241 * @return bool. True exists, false does not exist 242 */ 243 bool HasDataType(const std::string &mimeType); 244 245 /** 246 * DetectPatterns 247 * @description Checks the specified patterns contained in clipboard, and removes if not found. 248 * @param patternsToCheck A reference to an set of Pattern to check against the clipboard. 249 * @return Returns DetectPatterns. 250 */ 251 std::set<Pattern> DetectPatterns(const std::set<Pattern> &patternsToCheck); 252 253 /** 254 * Subscribe 255 * @descrition 256 * @param type observer type 257 * @param observer pasteboard change callback. 258 * @return void. 259 */ 260 void Subscribe(PasteboardObserverType type, sptr<PasteboardObserver> callback); 261 262 /** 263 * AddPasteboardChangedObserver 264 * @descrition 265 * @param observer pasteboard change callback. 266 * @return void. 267 */ 268 void AddPasteboardChangedObserver(sptr<PasteboardObserver> callback); 269 270 /** 271 * AddPasteboardEventObserver 272 * @descrition 273 * @param observer pasteboard event(read or change) callback. 274 * @return void. 275 */ 276 void AddPasteboardEventObserver(sptr<PasteboardObserver> callback); 277 278 /** 279 * Unsubscribe 280 * @descrition 281 * @param type observer type 282 * @param observer pasteboard change callback. 283 * @return void. 284 */ 285 void Unsubscribe(PasteboardObserverType type, sptr<PasteboardObserver> callback); 286 287 /** 288 * RemovePasteboardChangedObserver 289 * @descrition 290 * @param observer pasteboard change callback. 291 * @return void. 292 */ 293 void RemovePasteboardChangedObserver(sptr<PasteboardObserver> callback); 294 295 /** 296 * RemovePasteboardEventObserver 297 * @descrition 298 * @param observer pasteboard event callback. 299 * @return void. 300 */ 301 void RemovePasteboardEventObserver(sptr<PasteboardObserver> callback); 302 303 /** 304 * SetGlobalShareOption 305 * @descrition Set globalShareOptions. 306 * @param globalShareOption globalShareOptions 307 * @return int32_t 308 */ 309 int32_t SetGlobalShareOption(const std::map<uint32_t, ShareOption> &globalShareOptions); 310 311 /** 312 * RemoveGlobalShareOption 313 * @descrition Remove globalShareOptions. 314 * @param tokenId tokenIds 315 * @return int32_t 316 */ 317 int32_t RemoveGlobalShareOption(const std::vector<uint32_t> &tokenIds); 318 319 /** 320 * GetGlobalShareOption 321 * @descrition Get globalShareOptions. 322 * @param tokenId tokenIds 323 * @return globalShareOptions 324 */ 325 std::map<uint32_t, ShareOption> GetGlobalShareOption(const std::vector<uint32_t> &tokenIds); 326 327 /** 328 * SetAppShareOptions 329 * @description Sets a unified ShareOptions for the application. 330 * @param shareOptions shareOptions 331 * @return result 332 */ 333 int32_t SetAppShareOptions(const ShareOption &shareOptions); 334 335 /** 336 * RemoveAppShareOptions 337 * @description Removes the ShareOptions for the application. 338 * @return result 339 */ 340 int32_t RemoveAppShareOptions(); 341 342 /** 343 * OnRemoteSaDied 344 * @descrition 345 * @param object systemAbility proxy object 346 * @return void. 347 */ 348 void OnRemoteSaDied(const wptr<IRemoteObject> &object); 349 350 /** 351 * LoadSystemAbilitySuccess 352 * @descrition inherit SystemAbilityLoadCallbackStub override LoadSystemAbilitySuccess 353 * @param remoteObject systemAbility proxy object. 354 * @return void. 355 */ 356 void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject); 357 358 /** 359 * LoadSystemAbilityFail 360 * @descrition inherit SystemAbilityLoadCallbackStub override LoadSystemAbilityFail 361 * @return void. 362 */ 363 void LoadSystemAbilityFail(); 364 365 /** 366 * PasteStart 367 * @descrition Utilized to notify pasteboard service while reading PasteData, in this case, the service will help to 368 * preserve the context and resources 369 * @return void. 370 */ 371 void PasteStart(const std::string &pasteId); 372 373 /** 374 * PasteComplete 375 * @descrition Invoked to notify pasteboard service the utilization of PasteData has completed and occupied 376 * resources can be released for further usage 377 * @return void. 378 */ 379 void PasteComplete(const std::string &deviceId, const std::string &pasteId); 380 381 private: 382 sptr<IPasteboardService> GetPasteboardService(); 383 sptr<IPasteboardService> GetPasteboardServiceProxy(); 384 static void RetainUri(PasteData &pasteData); 385 static std::shared_ptr<PasteData> SplitWebviewPasteData(PasteData &pasteData); 386 static sptr<IPasteboardService> pasteboardServiceProxy_; 387 static std::mutex instanceLock_; 388 static std::condition_variable proxyConVar_; 389 sptr<IRemoteObject::DeathRecipient> deathRecipient_{ nullptr }; 390 std::atomic<uint32_t> getSequenceId_ = 0; 391 class StaticDestoryMonitor { 392 public: StaticDestoryMonitor()393 StaticDestoryMonitor() : destoryed_(false) {} ~StaticDestoryMonitor()394 ~StaticDestoryMonitor() 395 { 396 destoryed_ = true; 397 } 398 IsDestoryed()399 bool IsDestoryed() const 400 { 401 return destoryed_; 402 } 403 404 private: 405 bool destoryed_; 406 }; 407 static StaticDestoryMonitor staticDestoryMonitor_; 408 void RebuildWebviewPasteData(PasteData &pasteData); 409 void Init(); 410 }; 411 } // namespace MiscServices 412 } // namespace OHOS 413 #endif // PASTE_BOARD_CLIENT_H 414