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 #ifndef N_NAPI_PASTE_H 16 #define N_NAPI_PASTE_H 17 18 #include "async_call.h" 19 #include "common/block_object.h" 20 #include "pasteboard_error.h" 21 #include "pastedata_napi.h" 22 #include "pastedata_record_napi.h" 23 #include "unified_data_napi.h" 24 25 namespace OHOS { 26 namespace MiscServicesNapi { 27 class PasteboardObserverInstance : public std::enable_shared_from_this<PasteboardObserverInstance> { 28 public: 29 class PasteboardObserverImpl : public MiscServices::PasteboardObserver { 30 public: 31 explicit PasteboardObserverImpl() = default; 32 void OnPasteboardChanged() override; 33 void SetObserverWrapper(const std::shared_ptr<PasteboardObserverInstance> &observerInstance); 34 35 private: 36 std::weak_ptr<PasteboardObserverInstance> wrapper_; 37 }; 38 39 explicit PasteboardObserverInstance(const napi_env &env, const napi_ref &ref); 40 ~PasteboardObserverInstance(); 41 42 void OnPasteboardChanged(); GetEnv()43 napi_env GetEnv() 44 { 45 return env_; 46 } GetRef()47 napi_ref GetRef() 48 { 49 return ref_; 50 } 51 sptr<PasteboardObserverImpl> GetStub(); 52 53 private: 54 napi_env env_ = nullptr; 55 napi_ref ref_ = nullptr; 56 sptr<PasteboardObserverImpl> stub_ = nullptr; 57 }; 58 59 class PasteboardDelayGetterInstance : public std::enable_shared_from_this<PasteboardDelayGetterInstance> { 60 public: 61 class PasteboardDelayGetterImpl : public MiscServices::PasteboardDelayGetter { 62 public: 63 explicit PasteboardDelayGetterImpl() = default; 64 void GetPasteData(const std::string &type, MiscServices::PasteData &data) override; 65 void GetUnifiedData(const std::string &type, UDMF::UnifiedData &data) override; 66 void SetDelayGetterWrapper(const std::shared_ptr<PasteboardDelayGetterInstance> observerInstance); 67 68 private: 69 std::weak_ptr<PasteboardDelayGetterInstance> wrapper_; 70 }; 71 explicit PasteboardDelayGetterInstance(const napi_env &env, const napi_ref &ref); 72 ~PasteboardDelayGetterInstance(); 73 74 void GetUnifiedData(const std::string &type, UDMF::UnifiedData &data); 75 GetEnv()76 napi_env GetEnv() 77 { 78 return env_; 79 } 80 GetRef()81 napi_ref GetRef() 82 { 83 return ref_; 84 } 85 GetStub()86 std::shared_ptr<PasteboardDelayGetterImpl> GetStub() 87 { 88 return stub_; 89 } 90 91 private: 92 napi_env env_ = nullptr; 93 napi_ref ref_ = nullptr; 94 std::shared_ptr<PasteboardDelayGetterImpl> stub_ = nullptr; 95 }; 96 97 struct PasteboardDataWorker { 98 std::shared_ptr<PasteboardObserverInstance> observer = nullptr; 99 }; 100 101 struct PasteboardDelayWorker { 102 std::string dataType; 103 std::shared_ptr<PasteboardDelayGetterInstance> delayGetter = nullptr; 104 std::shared_ptr<UDMF::UnifiedData> unifiedData = nullptr; 105 bool complete; 106 bool clean; 107 std::condition_variable cv; 108 std::mutex mutex; 109 }; 110 111 struct HasContextInfo : public AsyncCall::Context { 112 bool hasPasteData; 113 napi_status status = napi_generic_failure; HasContextInfoHasContextInfo114 HasContextInfo() : Context(nullptr, nullptr){}; 115 operatorHasContextInfo116 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 117 { 118 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 119 return Context::operator()(env, argc, argv, self); 120 } operatorHasContextInfo121 napi_status operator()(napi_env env, napi_value *result) override 122 { 123 if (status != napi_ok) { 124 return status; 125 } 126 return Context::operator()(env, result); 127 } 128 }; 129 130 struct SetContextInfo : public AsyncCall::Context { 131 std::shared_ptr<MiscServices::PasteData> obj; 132 napi_status status = napi_generic_failure; SetContextInfoSetContextInfo133 SetContextInfo() : Context(nullptr, nullptr){}; 134 operatorSetContextInfo135 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 136 { 137 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 138 return Context::operator()(env, argc, argv, self); 139 } operatorSetContextInfo140 napi_status operator()(napi_env env, napi_value *result) override 141 { 142 if (status != napi_ok) { 143 return status; 144 } 145 return Context::operator()(env, result); 146 } 147 }; 148 149 struct SetUnifiedContextInfo : public AsyncCall::Context { 150 std::shared_ptr<PasteboardDelayGetterInstance> delayGetter; 151 std::shared_ptr<UDMF::UnifiedData> obj; 152 bool isDelay = false; 153 napi_status status = napi_generic_failure; SetUnifiedContextInfoSetUnifiedContextInfo154 SetUnifiedContextInfo() : Context(nullptr, nullptr){}; 155 operatorSetUnifiedContextInfo156 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 157 { 158 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 159 return Context::operator()(env, argc, argv, self); 160 } operatorSetUnifiedContextInfo161 napi_status operator()(napi_env env, napi_value *result) override 162 { 163 if (status != napi_ok) { 164 return status; 165 } 166 return Context::operator()(env, result); 167 } 168 }; 169 170 struct GetContextInfo : public AsyncCall::Context { 171 std::shared_ptr<MiscServices::PasteData> pasteData; 172 napi_status status = napi_generic_failure; GetContextInfoGetContextInfo173 GetContextInfo() : Context(nullptr, nullptr){}; 174 operatorGetContextInfo175 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 176 { 177 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 178 return Context::operator()(env, argc, argv, self); 179 } operatorGetContextInfo180 napi_status operator()(napi_env env, napi_value *result) override 181 { 182 if (status != napi_ok) { 183 return status; 184 } 185 return Context::operator()(env, result); 186 } 187 }; 188 189 struct GetUnifiedContextInfo : public AsyncCall::Context { 190 std::shared_ptr<UDMF::UnifiedData> unifiedData; 191 napi_status status = napi_generic_failure; GetUnifiedContextInfoGetUnifiedContextInfo192 GetUnifiedContextInfo() : Context(nullptr, nullptr){}; 193 operatorGetUnifiedContextInfo194 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 195 { 196 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 197 return Context::operator()(env, argc, argv, self); 198 } operatorGetUnifiedContextInfo199 napi_status operator()(napi_env env, napi_value *result) override 200 { 201 if (status != napi_ok) { 202 return status; 203 } 204 return Context::operator()(env, result); 205 } 206 }; 207 208 struct GetMimeTypesContextInfo : public AsyncCall::Context { 209 std::vector<std::string> mimeTypes; 210 napi_status status = napi_generic_failure; GetMimeTypesContextInfoGetMimeTypesContextInfo211 GetMimeTypesContextInfo() : Context(nullptr, nullptr){}; 212 operatorGetMimeTypesContextInfo213 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 214 { 215 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 216 return Context::operator()(env, argc, argv, self); 217 } operatorGetMimeTypesContextInfo218 napi_status operator()(napi_env env, napi_value *result) override 219 { 220 if (status != napi_ok) { 221 return status; 222 } 223 return Context::operator()(env, result); 224 } 225 }; 226 227 struct DetectPatternsContextInfo : public AsyncCall::Context { 228 std::set<MiscServices::Pattern> patternsDetect; 229 std::set<MiscServices::Pattern> patternsToCheck; 230 napi_status status = napi_generic_failure; DetectPatternsContextInfoDetectPatternsContextInfo231 DetectPatternsContextInfo() : Context(nullptr, nullptr){}; 232 operatorDetectPatternsContextInfo233 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 234 { 235 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 236 return Context::operator()(env, argc, argv, self); 237 } operatorDetectPatternsContextInfo238 napi_status operator()(napi_env env, napi_value *result) override 239 { 240 if (status != napi_ok) { 241 return status; 242 } 243 return Context::operator()(env, result); 244 } 245 }; 246 247 struct GetDataParamsContextInfo : public AsyncCall::Context { 248 std::shared_ptr<MiscServices::PasteData> pasteData; 249 std::shared_ptr<MiscServices::GetDataParams> getDataParams; 250 napi_status status = napi_generic_failure; GetDataParamsContextInfoGetDataParamsContextInfo251 GetDataParamsContextInfo() : Context(nullptr, nullptr){}; 252 operatorGetDataParamsContextInfo253 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 254 { 255 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 256 return Context::operator()(env, argc, argv, self); 257 } operatorGetDataParamsContextInfo258 napi_status operator()(napi_env env, napi_value *result) override 259 { 260 if (status != napi_ok) { 261 return status; 262 } 263 return Context::operator()(env, result); 264 } 265 }; 266 267 typedef struct { 268 napi_threadsafe_function tsFunction; 269 napi_value jsCallback; 270 } ProgressListenerFn; 271 272 const std::map<MiscServices::PasteboardError, MiscServices::JSErrorCode> ErrorCodeMap = { 273 {MiscServices::PasteboardError::TASK_PROCESSING, MiscServices::JSErrorCode::OTHER_COPY_OR_PASTE_IN_PROCESSING}, 274 {MiscServices::PasteboardError::COPY_FILE_ERROR, MiscServices::JSErrorCode::ERR_COPY_FILE_ERROR}, 275 {MiscServices::PasteboardError::PROGRESS_START_ERROR, MiscServices::JSErrorCode::ERR_PROGRESS_START_ERROR}, 276 {MiscServices::PasteboardError::PROGRESS_ABNORMAL, MiscServices::JSErrorCode::ERR_PROGRESS_ABNORMAL}, 277 {MiscServices::PasteboardError::PERMISSION_VERIFICATION_ERROR, MiscServices::JSErrorCode::NO_PERMISSION}, 278 {MiscServices::PasteboardError::INVALID_PARAM_ERROR, MiscServices::JSErrorCode::INVALID_PARAMETERS}, 279 }; 280 281 class SystemPasteboardNapi { 282 public: 283 static napi_value SystemPasteboardInit(napi_env env, napi_value exports); 284 static napi_value New(napi_env env, napi_callback_info info); 285 static napi_status NewInstance(napi_env env, napi_value &instance); 286 static void Destructor(napi_env env, void *nativeObject, void *finalize_hint); 287 static void DeleteObserver(const std::shared_ptr<PasteboardObserverInstance> &observer); 288 SystemPasteboardNapi(); 289 ~SystemPasteboardNapi(); 290 291 private: 292 static napi_value On(napi_env env, napi_callback_info info); 293 static napi_value Off(napi_env env, napi_callback_info info); 294 static napi_value Clear(napi_env env, napi_callback_info info); 295 static napi_value GetPasteData(napi_env env, napi_callback_info info); 296 static napi_value SetPasteData(napi_env env, napi_callback_info info); 297 static napi_value HasPasteData(napi_env env, napi_callback_info info); 298 static napi_value ClearData(napi_env env, napi_callback_info info); 299 static napi_value GetData(napi_env env, napi_callback_info info); 300 static napi_value SetData(napi_env env, napi_callback_info info); 301 static napi_value HasData(napi_env env, napi_callback_info info); 302 static napi_value IsRemoteData(napi_env env, napi_callback_info info); 303 static napi_value GetDataSource(napi_env env, napi_callback_info info); 304 static napi_value GetChangeCount(napi_env env, napi_callback_info info); 305 static napi_value GetMimeTypes(napi_env env, napi_callback_info info); 306 static napi_value HasDataType(napi_env env, napi_callback_info info); 307 static napi_value DetectPatterns(napi_env env, napi_callback_info info); 308 static napi_value ClearDataSync(napi_env env, napi_callback_info info); 309 static napi_value GetDataSync(napi_env env, napi_callback_info info); 310 static napi_value SetDataSync(napi_env env, napi_callback_info info); 311 static napi_value HasDataSync(napi_env env, napi_callback_info info); 312 static bool CheckArgsOfOnAndOff(napi_env env, bool checkArgsCount, napi_value *argv, size_t argc); 313 static void SetObserver(napi_ref ref, std::shared_ptr<PasteboardObserverInstance> observer); 314 static std::shared_ptr<PasteboardObserverInstance> GetObserver(napi_env env, napi_value observer); 315 static void GetDataCommon(std::shared_ptr<GetContextInfo> &context); 316 static void SetDataCommon(std::shared_ptr<SetContextInfo> &context); 317 318 static void GetDataCommon(std::shared_ptr<GetUnifiedContextInfo> &context); 319 static void SetDataCommon(std::shared_ptr<SetUnifiedContextInfo> &context); 320 321 static napi_value GetUnifiedData(napi_env env, napi_callback_info info); 322 static napi_value GetUnifiedDataSync(napi_env env, napi_callback_info info); 323 static napi_value SetUnifiedData(napi_env env, napi_callback_info info); 324 static napi_value SetUnifiedDataSync(napi_env env, napi_callback_info info); 325 326 static napi_value SetAppShareOptions(napi_env env, napi_callback_info info); 327 static napi_value RemoveAppShareOptions(napi_env env, napi_callback_info info); 328 329 static void ProgressNotify(std::shared_ptr<MiscServices::GetDataParams> params); 330 static void CallJsProgressNotify(napi_env env, napi_value jsFunction, void *context, void *data); 331 static bool ParseJsGetDataWithProgress(napi_env env, napi_value in, 332 std::shared_ptr<MiscServices::GetDataParams> &getDataParam); 333 static bool AddProgressListener(napi_env env, std::shared_ptr<MiscServices::GetDataParams> getDataParam, 334 const std::shared_ptr<ProgressListenerFn> listenerFn); 335 static bool CreateThreadSafeFunc(napi_env env, const std::shared_ptr<ProgressListenerFn> listenerFn); 336 static void GetDataWithProgressParam(std::shared_ptr<GetDataParamsContextInfo> &context); 337 static napi_value GetDataWithProgress(napi_env env, napi_callback_info info); 338 static bool CheckParamsType(napi_env env, napi_value in, napi_valuetype expectedType); 339 340 static std::mutex delayMutex_; 341 std::shared_ptr<PasteDataNapi> value_; 342 std::shared_ptr<MiscServices::PasteData> pasteData_; 343 napi_env env_; 344 static thread_local std::map<napi_ref, std::shared_ptr<PasteboardObserverInstance>> observers_; 345 static std::shared_ptr<PasteboardDelayGetterInstance> delayGetter_; 346 347 static std::recursive_mutex listenerMutex_; 348 static std::map<std::string, std::shared_ptr<ProgressListenerFn>> listenerMap_; 349 }; 350 } // namespace MiscServicesNapi 351 } // namespace OHOS 352 #endif