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 "napi/native_api.h" 20 #include "napi/native_node_api.h" 21 #include "pasteboard_observer.h" 22 #include "pastedata_napi.h" 23 #include "pastedata_record_napi.h" 24 #include "pixel_map_napi.h" 25 #include "uri.h" 26 27 namespace OHOS { 28 namespace MiscServicesNapi { 29 class PasteboardObserverInstance : public std::enable_shared_from_this<PasteboardObserverInstance> { 30 public: 31 class PasteboardObserverImpl : public MiscServices::PasteboardObserver { 32 public: 33 explicit PasteboardObserverImpl() = default; 34 void OnPasteboardChanged() override; 35 void SetObserverWrapper(const std::shared_ptr<PasteboardObserverInstance> &observerInstance); 36 37 private: 38 std::weak_ptr<PasteboardObserverInstance> wrapper_; 39 }; 40 41 explicit PasteboardObserverInstance(const napi_env &env, const napi_ref &ref); 42 ~PasteboardObserverInstance(); 43 44 void OnPasteboardChanged(); GetEnv()45 napi_env GetEnv() 46 { 47 return env_; 48 } GetRef()49 napi_ref GetRef() 50 { 51 return ref_; 52 } 53 sptr<PasteboardObserverImpl> GetStub(); 54 55 private: 56 napi_env env_ = nullptr; 57 napi_ref ref_ = nullptr; 58 sptr<PasteboardObserverImpl> stub_ = nullptr; 59 }; 60 61 struct PasteboardDataWorker { 62 std::shared_ptr<PasteboardObserverInstance> observer = nullptr; 63 }; 64 65 struct HasContextInfo : public AsyncCall::Context { 66 bool hasPasteData; 67 napi_status status = napi_generic_failure; HasContextInfoHasContextInfo68 HasContextInfo() : Context(nullptr, nullptr){}; 69 operatorHasContextInfo70 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 71 { 72 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 73 return Context::operator()(env, argc, argv, self); 74 } operatorHasContextInfo75 napi_status operator()(napi_env env, napi_value *result) override 76 { 77 if (status != napi_ok) { 78 return status; 79 } 80 return Context::operator()(env, result); 81 } 82 }; 83 84 struct SetContextInfo : public AsyncCall::Context { 85 std::shared_ptr<MiscServices::PasteData> obj; 86 napi_status status = napi_generic_failure; SetContextInfoSetContextInfo87 SetContextInfo() : Context(nullptr, nullptr){}; 88 operatorSetContextInfo89 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 90 { 91 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 92 return Context::operator()(env, argc, argv, self); 93 } operatorSetContextInfo94 napi_status operator()(napi_env env, napi_value *result) override 95 { 96 if (status != napi_ok) { 97 return status; 98 } 99 return Context::operator()(env, result); 100 } 101 }; 102 103 struct GetContextInfo : public AsyncCall::Context { 104 std::shared_ptr<MiscServices::PasteData> pasteData; 105 napi_status status = napi_generic_failure; GetContextInfoGetContextInfo106 GetContextInfo() : Context(nullptr, nullptr){}; 107 operatorGetContextInfo108 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 109 { 110 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 111 return Context::operator()(env, argc, argv, self); 112 } operatorGetContextInfo113 napi_status operator()(napi_env env, napi_value *result) override 114 { 115 if (status != napi_ok) { 116 return status; 117 } 118 return Context::operator()(env, result); 119 } 120 }; 121 122 class SystemPasteboardNapi { 123 public: 124 static napi_value SystemPasteboardInit(napi_env env, napi_value exports); 125 static napi_value New(napi_env env, napi_callback_info info); 126 static napi_status NewInstance(napi_env env, napi_value &instance); 127 static void Destructor(napi_env env, void *nativeObject, void *finalize_hint); 128 static void DeleteObserver(const std::shared_ptr<PasteboardObserverInstance> &observer); 129 SystemPasteboardNapi(); 130 ~SystemPasteboardNapi(); 131 132 private: 133 static napi_value On(napi_env env, napi_callback_info info); 134 static napi_value Off(napi_env env, napi_callback_info info); 135 static napi_value Clear(napi_env env, napi_callback_info info); 136 static napi_value GetPasteData(napi_env env, napi_callback_info info); 137 static napi_value SetPasteData(napi_env env, napi_callback_info info); 138 static napi_value HasPasteData(napi_env env, napi_callback_info info); 139 static napi_value ClearData(napi_env env, napi_callback_info info); 140 static napi_value GetData(napi_env env, napi_callback_info info); 141 static napi_value SetData(napi_env env, napi_callback_info info); 142 static napi_value HasData(napi_env env, napi_callback_info info); 143 static napi_value IsRemoteData(napi_env env, napi_callback_info info); 144 static napi_value GetDataSource(napi_env env, napi_callback_info info); 145 static napi_value HasDataType(napi_env env, napi_callback_info info); 146 static napi_value ClearDataSync(napi_env env, napi_callback_info info); 147 static napi_value GetDataSync(napi_env env, napi_callback_info info); 148 static napi_value SetDataSync(napi_env env, napi_callback_info info); 149 static napi_value HasDataSync(napi_env env, napi_callback_info info); 150 static bool CheckAgrsOfOnAndOff(napi_env env, bool checkArgsCount, napi_value *argv, size_t argc); 151 static void SetObserver(napi_ref ref, std::shared_ptr<PasteboardObserverInstance> observer); 152 static std::shared_ptr<PasteboardObserverInstance> GetObserver(napi_env env, napi_value observer); 153 static void GetDataCommon(std::shared_ptr<GetContextInfo> &context); 154 static void SetDataCommon(std::shared_ptr<SetContextInfo> &context); 155 156 std::shared_ptr<PasteDataNapi> value_; 157 std::shared_ptr<MiscServices::PasteData> pasteData_; 158 napi_env env_; 159 static thread_local std::map<napi_ref, std::shared_ptr<PasteboardObserverInstance>> observers_; 160 }; 161 } // namespace MiscServicesNapi 162 } // namespace OHOS 163 #endif