1 /* 2 * Copyright (c) 2022 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 JS_CALLBACK_OBJECT_H 16 #define JS_CALLBACK_OBJECT_H 17 18 #include <mutex> 19 #include <thread> 20 21 #include "block_data.h" 22 #include "event_handler.h" 23 #include "napi/native_api.h" 24 #include "napi/native_node_api.h" 25 26 namespace OHOS { 27 namespace MiscServices { 28 class JSCallbackObject { 29 public: 30 JSCallbackObject(napi_env env, napi_value callback, std::thread::id threadId, 31 std::shared_ptr<AppExecFwk::EventHandler> jsHandler); 32 ~JSCallbackObject(); 33 napi_ref callback_ = nullptr; 34 napi_env env_{}; 35 std::thread::id threadId_; 36 std::shared_ptr<BlockData<bool>> isDone_; 37 std::shared_ptr<AppExecFwk::EventHandler> jsHandler_; 38 }; 39 40 // Ensure this object abstract in constract thread. 41 class JSMsgHandlerCallbackObject { 42 public: 43 JSMsgHandlerCallbackObject(napi_env env, napi_value onTerminated, napi_value onMessage); 44 ~JSMsgHandlerCallbackObject(); 45 napi_env env_{}; 46 napi_ref onTerminatedCallback_ = nullptr; 47 napi_ref onMessageCallback_ = nullptr; 48 std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler(); 49 50 private: 51 std::mutex eventHandlerMutex_; 52 std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr; 53 std::thread::id threadId_; 54 }; 55 } // namespace MiscServices 56 } // namespace OHOS 57 #endif // JS_CALLBACK_OBJECT_H 58