1 /* 2 * Copyright (C) 2021 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 <singleton.h> 20 21 #include <functional> 22 23 #include "i_pasteboard_service.h" 24 #include "paste_data.h" 25 #include "paste_data_record.h" 26 #include "pasteboard_observer.h" 27 #include "want.h" 28 29 namespace OHOS { 30 namespace MiscServices { 31 class PasteboardSaDeathRecipient : public IRemoteObject::DeathRecipient { 32 public: 33 explicit PasteboardSaDeathRecipient(); 34 ~PasteboardSaDeathRecipient() = default; 35 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 36 private: 37 DISALLOW_COPY_AND_MOVE(PasteboardSaDeathRecipient); 38 }; 39 class PasteboardClient : public DelayedSingleton<PasteboardClient> { 40 DECLARE_DELAYED_SINGLETON(PasteboardClient); 41 public: 42 DISALLOW_COPY_AND_MOVE(PasteboardClient); 43 44 /** 45 * CreateHtmlTextRecord 46 * @descrition Create Html Text Record. 47 * @param std::string text. 48 * @return PasteDataRecord. 49 */ 50 std::shared_ptr<PasteDataRecord> CreateHtmlTextRecord(const std::string &text); 51 52 /** 53 * CreatePlainTextRecord 54 * @descrition Create Plaint Text Record. 55 * @param std::string text. 56 * @return PasteDataRecord. 57 */ 58 std::shared_ptr<PasteDataRecord> CreatePlainTextRecord(const std::string &text); 59 60 /** 61 * CreatePixelMapRecord 62 * @descrition Create PixelMap Record. 63 * @param OHOS::Media::PixelMap pixelMap. 64 * @return PasteDataRecord. 65 */ 66 std::shared_ptr<PasteDataRecord> CreatePixelMapRecord(std::shared_ptr<OHOS::Media::PixelMap> pixelMap); 67 68 /** 69 * CreateUriRecord 70 * @descrition Create Uri Text Record. 71 * @param OHOS::Uri uri. 72 * @return PasteDataRecord. 73 */ 74 std::shared_ptr<PasteDataRecord> CreateUriRecord(const OHOS::Uri &uri); 75 76 /** 77 * CreateWantRecord 78 * @descrition Create Plaint Want Record. 79 * @param OHOS::AAFwk::Want want. 80 * @return PasteDataRecord. 81 */ 82 std::shared_ptr<PasteDataRecord> CreateWantRecord(std::shared_ptr<OHOS::AAFwk::Want> want); 83 84 /** 85 * CreateKvRecord 86 * @descrition Create Kv Record. 87 * @param std::string mimeType 88 * @param std::vector<uint8_t> arrayBuffer 89 * @return PasteDataRecord. 90 */ 91 std::shared_ptr<PasteDataRecord> CreateKvRecord( 92 const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer); 93 94 /** 95 * CreateHtmlData 96 * @descrition Create Html Paste Data. 97 * @param std::string text . 98 * @return PasteData. 99 */ 100 std::shared_ptr<PasteData> CreateHtmlData(const std::string &htmlText); 101 102 /** 103 * CreatePlainTextData 104 * @descritionCreate Plain Text Paste Data. 105 * @param std::string text . 106 * @return PasteData. 107 */ 108 std::shared_ptr<PasteData> CreatePlainTextData(const std::string &text); 109 110 /** 111 * CreatePixelMapData 112 * @descrition Create PixelMap Paste Data. 113 * @param OHOS::Media::PixelMap pixelMap . 114 * @return PasteData. 115 */ 116 std::shared_ptr<PasteData> CreatePixelMapData(std::shared_ptr<OHOS::Media::PixelMap> pixelMap); 117 118 /** 119 * CreateUriData 120 * @descrition Create Uri Paste Data. 121 * @param OHOS::Uri uri . 122 * @return PasteData. 123 */ 124 std::shared_ptr<PasteData> CreateUriData(const OHOS::Uri &uri); 125 126 /** 127 * CreateWantData 128 * @descrition Create Want Paste Data. 129 * @param OHOS::AAFwk::Want want . 130 * @return PasteData. 131 */ 132 std::shared_ptr<PasteData> CreateWantData(std::shared_ptr<OHOS::AAFwk::Want> want); 133 134 /** 135 * CreateKvData 136 * @descrition Create Kv Paste Data. 137 * @param std::string mimeType 138 * @param std::vector<uint8_t> arrayBuffer 139 * @return PasteData. 140 */ 141 std::shared_ptr<PasteData> CreateKvData(const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer); 142 143 /** 144 * GetPasteData 145 * @descrition 146 * @return bool. 147 */ 148 int32_t GetPasteData(PasteData& pasteData); 149 150 /** 151 * HasPasteData 152 * @descrition 153 * @return bool true on success, false on failure. 154 */ 155 bool HasPasteData(); 156 157 /** 158 * Clear 159 * @descrition Clear Current pasteboard data 160 * @return bool true on success, false on failure. 161 */ 162 void Clear(); 163 164 /** 165 * SetPasteData 166 * @descrition 167 * @param pasteData . 168 * @return void. 169 */ 170 int32_t SetPasteData(PasteData& pasteData); 171 172 /** 173 * AddPasteboardChangedObserver 174 * @descrition 175 * @param observer pasteboard change callback. 176 * @return void. 177 */ 178 void AddPasteboardChangedObserver(sptr<PasteboardObserver> callback); 179 180 /** 181 * AddPasteboardEventObserver 182 * @descrition 183 * @param observer pasteboard event(read or change) callback. 184 * @return void. 185 */ 186 void AddPasteboardEventObserver(sptr<PasteboardObserver> callback); 187 188 /** 189 * RemovePasteboardChangedObserver 190 * @descrition 191 * @param observer pasteboard change callback. 192 * @return void. 193 */ 194 void RemovePasteboardChangedObserver(sptr<PasteboardObserver> callback); 195 196 /** 197 * RemovePasteboardEventObserver 198 * @descrition 199 * @param observer pasteboard event callback. 200 * @return void. 201 */ 202 void RemovePasteboardEventObserver(sptr<PasteboardObserver> callback); 203 204 void OnRemoteSaDied(const wptr<IRemoteObject> &object); 205 206 private: 207 inline bool IsServiceAvailable(); 208 void ConnectService(); 209 static void RetainUri(PasteData &pasteData); 210 211 static sptr<IPasteboardService> pasteboardServiceProxy_; 212 static std::mutex instanceLock_; 213 214 sptr<IRemoteObject::DeathRecipient> deathRecipient_{ nullptr }; 215 216 class StaticDestoryMonitor { 217 public: StaticDestoryMonitor()218 StaticDestoryMonitor() : destoryed_(false) {} ~StaticDestoryMonitor()219 ~StaticDestoryMonitor() 220 { 221 destoryed_ = true; 222 } 223 IsDestoryed()224 bool IsDestoryed() const 225 { 226 return destoryed_; 227 } 228 229 private: 230 bool destoryed_; 231 }; 232 static StaticDestoryMonitor staticDestoryMonitor_; 233 }; 234 } // namespace MiscServices 235 } // namespace OHOS 236 #endif // PASTE_BOARD_CLIENT_H