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