• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
16 #include "js_callback_handler.h"
17 
18 #include "global.h"
19 
20 namespace OHOS {
21 namespace MiscServices {
22 constexpr size_t MAX_ARGV_COUNT = 10;
Execute(const std::shared_ptr<JSCallbackObject> & object,const ArgContainer & argContainer,napi_value & output)23 void JsCallbackHandler::Execute(const std::shared_ptr<JSCallbackObject> &object, const ArgContainer &argContainer,
24     napi_value &output)
25 {
26     if (object == nullptr) {
27         IMSA_HILOGE("object is nullptr!");
28         return;
29     }
30     if (object->threadId_ != std::this_thread::get_id()) {
31         IMSA_HILOGW("threadId not same!");
32         return;
33     }
34     napi_value argv[MAX_ARGV_COUNT] = { nullptr };
35     if (argContainer.argvProvider != nullptr && !argContainer.argvProvider(object->env_, argv, MAX_ARGV_COUNT)) {
36         return;
37     }
38     napi_value callback = nullptr;
39     napi_value global = nullptr;
40     napi_get_reference_value(object->env_, object->callback_, &callback);
41     if (callback == nullptr) {
42         IMSA_HILOGE("callback is nullptr!");
43         return;
44     }
45     napi_get_global(object->env_, &global);
46     InputMethodSyncTrace tracer("Execute napi_call_function");
47     auto status = napi_call_function(object->env_, global, callback, argContainer.argc, argv, &output);
48     if (status != napi_ok) {
49         output = nullptr;
50     }
51 }
52 } // namespace MiscServices
53 } // namespace OHOS