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 explicit PasteDataRecordAdapterImpl( 38 std::shared_ptr<MiscServices::PasteDataRecord> record); 39 PasteDataRecordAdapterImpl(const std::string& mimeType, 40 std::shared_ptr<std::string> htmlText, 41 std::shared_ptr<std::string> plainText); 42 explicit PasteDataRecordAdapterImpl(const std::string& mimeType); 43 bool SetHtmlText(std::shared_ptr<std::string> htmlText) override; 44 bool SetPlainText(std::shared_ptr<std::string> plainText) override; 45 bool SetImgData(std::shared_ptr<ClipBoardImageData> imageData) override; 46 bool SetUri(const std::string& uriString) override; 47 bool SetCustomData(PasteCustomData& data) override; 48 std::string GetMimeType() override; 49 std::shared_ptr<std::string> GetHtmlText() override; 50 std::shared_ptr<std::string> GetPlainText() override; 51 std::shared_ptr<MiscServices::PasteDataRecord> GetRecord(); 52 bool GetImgData(ClipBoardImageData &imageData) override; 53 std::shared_ptr<std::string> GetUri() override; 54 std::shared_ptr<PasteCustomData> GetCustomData() override; 55 void Clear(); 56 private: 57 std::shared_ptr<MiscServices::PasteDataRecord> record_; 58 std::shared_ptr<MiscServices::PasteDataRecord::Builder> builder_; 59 uint8_t *imgBuffer_ = nullptr; 60 uint32_t bufferSize_ = 0; 61 ClipBoardImageAlphaType ImageToClipboardAlphaType(const Media::ImageInfo &imgInfo); 62 ClipBoardImageColorType ImageToClipboardColorType(const Media::ImageInfo &imgInfo); 63 Media::AlphaType ClipboardToImageAlphaType(ClipBoardImageAlphaType alphaType); 64 Media::PixelFormat ClipboardToImageColorType(ClipBoardImageColorType colorType); 65 void ClearImgBuffer(); 66 }; 67 68 class PasteDataAdapterImpl : public PasteDataAdapter { 69 public: 70 PasteDataAdapterImpl(); 71 explicit PasteDataAdapterImpl(std::shared_ptr<MiscServices::PasteData> data); 72 void AddHtmlRecord(const std::string &html) override; 73 void AddTextRecord(const std::string &text) override; 74 std::vector<std::string> GetMimeTypes() override; 75 std::shared_ptr<std::string> GetPrimaryHtml() override; 76 std::shared_ptr<std::string> GetPrimaryText() override; 77 std::shared_ptr<std::string> GetPrimaryMimeType() override; 78 std::shared_ptr<PasteDataRecordAdapter> GetRecordAt(std::size_t index) override; 79 std::size_t GetRecordCount() override; 80 PasteRecordList AllRecords() const override; 81 private: 82 std::shared_ptr<MiscServices::PasteData> data_; 83 }; 84 85 using ObserverMap = 86 std::map<PasteboardObserverAdapter*, sptr<MiscServices::PasteboardObserver>>; 87 class PasteBoardClientAdapterImpl : public PasteBoardClientAdapter { 88 public: 89 static PasteBoardClientAdapterImpl& GetInstance(); 90 bool GetPasteData(PasteRecordList& data) override; 91 void SetPasteData(const PasteRecordList& data) override; 92 bool HasPasteData() override; 93 void Clear() override; 94 int32_t OpenRemoteUri(const std::string& path) override; 95 bool IsLocalPaste() const override; 96 uint32_t GetTokenId() const override; 97 void AddPasteboardChangedObserver(std::shared_ptr<PasteboardObserverAdapter> callback) override; 98 void RemovePasteboardChangedObserver(std::shared_ptr<PasteboardObserverAdapter> callback) override; 99 private: 100 PasteBoardClientAdapterImpl() = default; 101 PasteBoardClientAdapterImpl(const PasteBoardClientAdapterImpl&) = delete; 102 PasteBoardClientAdapterImpl& operator=(const PasteBoardClientAdapterImpl&) = delete; 103 uint32_t tokenId_ = 0; 104 bool isLocalPaste_ = false; 105 ObserverMap reg_; 106 std::mutex mutex_; 107 }; 108 } // namespace OHOS::NWeb 109 110 #endif // PASTEBOARD_CLIENT_ADAPTER_IMPL_H 111