1 /* 2 * Copyright (c) 2022 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 #ifndef PASTEBOARD_CLIENT_ADAPTER_IMPL_H 16 #define PASTEBOARD_CLIENT_ADAPTER_IMPL_H 17 18 #include "pasteboard_client_adapter.h" 19 20 #include "paste_data.h" 21 #include "paste_data_record.h" 22 #include "pasteboard_client.h" 23 #include "pasteboard_observer.h" 24 25 namespace OHOS::NWeb { 26 class PasteboardObserverAdapterImpl : public MiscServices::PasteboardObserver { 27 public: 28 explicit PasteboardObserverAdapterImpl( 29 std::shared_ptr<PasteboardObserverAdapter> observer); 30 void OnPasteboardChanged() override; 31 private: 32 std::shared_ptr<PasteboardObserverAdapter> observer_; 33 }; 34 35 class PasteDataRecordAdapterImpl : public PasteDataRecordAdapter { 36 public: 37 PasteDataRecordAdapterImpl(); 38 explicit PasteDataRecordAdapterImpl( 39 std::shared_ptr<MiscServices::PasteDataRecord> record); 40 PasteDataRecordAdapterImpl(const std::string& mimeType, 41 std::shared_ptr<std::string> htmlText, 42 std::shared_ptr<std::string> plainText); 43 explicit PasteDataRecordAdapterImpl(const std::string& mimeType); 44 bool SetHtmlText(std::shared_ptr<std::string> htmlText) override; 45 bool SetHtmlText(std::shared_ptr<std::string> htmlText, std::shared_ptr<std::string> plainText); 46 bool SetPlainText(std::shared_ptr<std::string> plainText) override; 47 bool SetImgData(std::shared_ptr<ClipBoardImageDataAdapter> imageData) override; 48 bool SetUri(const std::string& uriString) override; 49 bool SetCustomData(PasteCustomData& data) override; 50 std::string GetMimeType() override; 51 std::shared_ptr<std::string> GetHtmlText() override; 52 std::shared_ptr<std::string> GetPlainText() override; 53 std::shared_ptr<MiscServices::PasteDataRecord> GetRecord(); 54 std::shared_ptr<Media::PixelMap> GetPixelMap(); 55 bool GetImgData(std::shared_ptr<ClipBoardImageDataAdapter> imageData) override; 56 std::shared_ptr<std::string> GetUri() override; 57 std::shared_ptr<PasteCustomData> GetCustomData() override; 58 void Clear(); 59 private: 60 std::shared_ptr<MiscServices::PasteDataRecord> record_; 61 std::shared_ptr<MiscServices::PasteDataRecord::Builder> builder_; 62 uint8_t *imgBuffer_ = nullptr; 63 uint32_t bufferSize_ = 0; 64 ClipBoardImageAlphaType ImageToClipboardAlphaType(const Media::ImageInfo &imgInfo); 65 ClipBoardImageColorType ImageToClipboardColorType(const Media::ImageInfo &imgInfo); 66 Media::AlphaType ClipboardToImageAlphaType(ClipBoardImageAlphaType alphaType); 67 Media::PixelFormat ClipboardToImageColorType(ClipBoardImageColorType colorType); 68 void ClearImgBuffer(); 69 }; 70 71 class PasteDataAdapterImpl : public PasteDataAdapter { 72 public: 73 PasteDataAdapterImpl(); 74 explicit PasteDataAdapterImpl(std::shared_ptr<MiscServices::PasteData> data); 75 void AddHtmlRecord(const std::string &html) override; 76 void AddTextRecord(const std::string &text) override; 77 std::vector<std::string> GetMimeTypes() override; 78 std::shared_ptr<std::string> GetPrimaryHtml() override; 79 std::shared_ptr<std::string> GetPrimaryText() override; 80 std::shared_ptr<std::string> GetPrimaryMimeType() override; 81 std::shared_ptr<PasteDataRecordAdapter> GetRecordAt(std::size_t index) override; 82 std::size_t GetRecordCount() override; 83 PasteRecordVector AllRecords() override; 84 private: 85 std::shared_ptr<MiscServices::PasteData> data_; 86 }; 87 88 using ObserverMap = 89 std::map<int32_t, sptr<MiscServices::PasteboardObserver>>; 90 class PasteBoardClientAdapterImpl : public PasteBoardClientAdapter { 91 public: 92 static PasteBoardClientAdapterImpl& GetInstance(); 93 bool GetPasteData(PasteRecordVector& data) override; 94 void SetPasteData(const PasteRecordVector& data, 95 CopyOptionMode copyOption = CopyOptionMode::CROSS_DEVICE) override; 96 bool HasPasteData() override; 97 void Clear() override; 98 int32_t OpenRemoteUri(const std::string& path) override; 99 bool IsLocalPaste() override; 100 uint32_t GetTokenId() override; 101 int32_t AddPasteboardChangedObserver(std::shared_ptr<PasteboardObserverAdapter> callback) override; 102 void RemovePasteboardChangedObserver(int32_t callbackId) override; 103 private: 104 PasteBoardClientAdapterImpl() = default; 105 PasteBoardClientAdapterImpl(const PasteBoardClientAdapterImpl&) = delete; 106 PasteBoardClientAdapterImpl& operator=(const PasteBoardClientAdapterImpl&) = delete; 107 uint32_t tokenId_ = 0; 108 bool isLocalPaste_ = false; 109 ObserverMap reg_; 110 std::mutex mutex_; 111 std::string webviewPasteDataTag_ = "WebviewPasteDataTag"; 112 MiscServices::ShareOption TransitionCopyOption(CopyOptionMode copyOption); 113 }; 114 } // namespace OHOS::NWeb 115 116 #endif // PASTEBOARD_CLIENT_ADAPTER_IMPL_H 117