• 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 
16 #include "js_callback_object.h"
17 #include "global.h"
18 
19 #include <uv.h>
20 
21 namespace OHOS {
22 namespace MiscServices {
23 constexpr int32_t MAX_TIMEOUT = 2000;
JSCallbackObject(napi_env env,napi_value callback,std::thread::id threadId,std::shared_ptr<AppExecFwk::EventHandler> jsHandler)24 JSCallbackObject::JSCallbackObject(napi_env env, napi_value callback, std::thread::id threadId,
25     std::shared_ptr<AppExecFwk::EventHandler> jsHandler)
26     : env_(env), threadId_(threadId), jsHandler_(jsHandler)
27 {
28     napi_create_reference(env, callback, 1, &callback_);
29 }
30 
~JSCallbackObject()31 JSCallbackObject::~JSCallbackObject()
32 {
33     if (callback_ != nullptr) {
34         if (threadId_ == std::this_thread::get_id()) {
35             napi_delete_reference(env_, callback_);
36             env_ = nullptr;
37             return;
38         }
39         isDone_ = std::make_shared<BlockData<bool>>(MAX_TIMEOUT, false);
40         std::string type = "~JSCallbackObject";
41         auto eventHandler = jsHandler_;
42         if (eventHandler == nullptr) {
43             IMSA_HILOGE("eventHandler is nullptr!");
44             return;
45         }
46         auto task = [env = env_, callback = callback_, isDone = isDone_]() {
47             napi_delete_reference(env, callback);
48             bool isFinish = true;
49             isDone->SetValue(isFinish);
50         };
51         eventHandler->PostTask(task, type);
52         isDone_->GetValue();
53     }
54     env_ = nullptr;
55 }
56 
57 
JSMsgHandlerCallbackObject(napi_env env,napi_value onTerminated,napi_value onMessage)58 JSMsgHandlerCallbackObject::JSMsgHandlerCallbackObject(napi_env env, napi_value onTerminated, napi_value onMessage)
59     : env_(env), handler_(AppExecFwk::EventHandler::Current()), threadId_(std::this_thread::get_id())
60 {
61     napi_create_reference(env, onTerminated, 1, &onTerminatedCallback_);
62     napi_create_reference(env, onMessage, 1, &onMessageCallback_);
63 }
64 
~JSMsgHandlerCallbackObject()65 JSMsgHandlerCallbackObject::~JSMsgHandlerCallbackObject()
66 {
67     if (threadId_ == std::this_thread::get_id()) {
68         if (onTerminatedCallback_ != nullptr) {
69             napi_delete_reference(env_, onTerminatedCallback_);
70         }
71         if (onMessageCallback_ != nullptr) {
72             napi_delete_reference(env_, onMessageCallback_);
73         }
74         env_ = nullptr;
75         return;
76     }
77     IMSA_HILOGW("Thread id is not same, abstract destructor is run in muti-thread!");
78     env_ = nullptr;
79 }
80 
GetEventHandler()81 std::shared_ptr<AppExecFwk::EventHandler> JSMsgHandlerCallbackObject::GetEventHandler()
82 {
83     std::lock_guard<std::mutex> lock(eventHandlerMutex_);
84     return handler_;
85 }
86 } // namespace MiscServices
87 } // namespace OHOS