• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     ~JSCallbackObject();
32     napi_ref callback_ = nullptr;
33     napi_env env_{};
34     std::thread::id threadId_;
35     std::shared_ptr<BlockData<bool>> isDone_;
36 };
37 
38 // Ensure this object abstract in constract thread.
39 class JSMsgHandlerCallbackObject {
40 public:
41     JSMsgHandlerCallbackObject(napi_env env, napi_value onTerminated, napi_value onMessage);
42     ~JSMsgHandlerCallbackObject();
43     napi_env env_{};
44     napi_ref onTerminatedCallback_ = nullptr;
45     napi_ref onMessageCallback_ = nullptr;
46     std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler();
47 
48 private:
49     std::mutex eventHandlerMutex_;
50     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
51     std::thread::id threadId_;
52 };
53 } // namespace MiscServices
54 } // namespace OHOS
55 #endif // JS_CALLBACK_OBJECT_H
56