1 /* 2 * Copyright (C) 2025 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 PASTEBOARD_OBSERVER_NAPI_H 17 #define PASTEBOARD_OBSERVER_NAPI_H 18 19 #include "event_handler.h" 20 #include "napi_init.h" 21 #include "pasteboard_client.h" 22 23 namespace OHOS { 24 namespace MiscServicesNapi { 25 class PasteboardObserverInstance : public MiscServices::PasteboardObserver { 26 public: 27 explicit PasteboardObserverInstance(napi_threadsafe_function callback, napi_env env); 28 ~PasteboardObserverInstance(); 29 void OnPasteboardChanged() override; 30 31 private: 32 napi_threadsafe_function callback_ = nullptr; 33 napi_env env_; 34 }; 35 36 class PasteboardNapiScope { 37 public: PasteboardNapiScope(napi_env env)38 PasteboardNapiScope(napi_env env) 39 { 40 env_ = env; 41 napi_open_handle_scope(env_, &scope_); 42 } 43 ~PasteboardNapiScope()44 ~PasteboardNapiScope() 45 { 46 if (scope_ != nullptr && env_ != nullptr) { 47 napi_close_handle_scope(env_, scope_); 48 } 49 scope_ = nullptr; 50 env_ = nullptr; 51 } 52 53 private: 54 napi_handle_scope scope_ = nullptr; 55 napi_env env_ = nullptr; 56 }; 57 } // namespace MiscServicesNapi 58 } // namespace OHOS 59 #endif // PASTEBOARD_OBSERVER_NAPI_H