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 "napi_hisysevent_querier.h"
17
18 #include "hilog/log.h"
19 #include "napi_hisysevent_util.h"
20
21 namespace OHOS {
22 namespace HiviewDFX {
23 namespace {
24 constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002D08, "NAPI_HISYSEVENT_QUERIER" };
25 constexpr char ON_QUERY_ATTR[] = "onQuery";
26 constexpr char ON_COMPLETE_ATTR[] = "onComplete";
27 constexpr size_t ON_QUERY_PARAM_COUNT = 1;
28 constexpr size_t ON_QUERY_COMPLTE_COUNT = 3;
29 }
30
NapiHiSysEventQuerier(CallbackContext * context,ON_COMPLETE_FUNC handler)31 NapiHiSysEventQuerier::NapiHiSysEventQuerier(CallbackContext* context, ON_COMPLETE_FUNC handler)
32 {
33 callbackContext = context;
34 onCompleteHandler = handler;
35 jsCallbackManager = std::make_shared<JsCallbackManager>();
36 }
37
~NapiHiSysEventQuerier()38 NapiHiSysEventQuerier::~NapiHiSysEventQuerier()
39 {
40 if (jsCallbackManager != nullptr) {
41 jsCallbackManager->Release();
42 }
43 if (callbackContext->threadId == syscall(SYS_gettid)) {
44 napi_delete_reference(callbackContext->env, callbackContext->ref);
45 }
46 delete callbackContext;
47 }
48
OnQuery(const std::vector<std::string> & sysEvents,const std::vector<int64_t> & seq)49 void NapiHiSysEventQuerier::OnQuery(const std::vector<std::string>& sysEvents,
50 const std::vector<int64_t>& seq)
51 {
52 jsCallbackManager->Add(callbackContext,
53 [this, sysEvents, seq] (const napi_env env, const napi_ref ref, pid_t threadId) {
54 if (threadId != syscall(SYS_gettid)) {
55 return;
56 }
57 napi_value sysEventInfoJsArray = nullptr;
58 napi_create_array_with_length(env, sysEvents.size(), &sysEventInfoJsArray);
59 NapiHiSysEventUtil::CreateJsSysEventInfoArray(env, sysEvents, sysEventInfoJsArray);
60 napi_value argv[ON_QUERY_PARAM_COUNT] = {sysEventInfoJsArray};
61 napi_value querier = nullptr;
62 napi_get_reference_value(env, ref, &querier);
63 napi_value onQuery = NapiHiSysEventUtil::GetPropertyByName(env, querier, ON_QUERY_ATTR);
64 napi_value ret = nullptr;
65 napi_status status = napi_call_function(env, querier, onQuery, ON_QUERY_PARAM_COUNT,
66 argv, &ret);
67 if (status != napi_ok) {
68 HiLog::Error(LABEL, "failed to call OnQuery JS function.");
69 }
70 });
71 }
72
OnComplete(int32_t reason,int32_t total,int64_t seq)73 void NapiHiSysEventQuerier::OnComplete(int32_t reason, int32_t total, int64_t seq)
74 {
75 jsCallbackManager->Add(callbackContext,
76 [this, reason, total, seq] (const napi_env env, const napi_ref ref, pid_t threadId) {
77 if (threadId != syscall(SYS_gettid)) {
78 return;
79 }
80 napi_value reasonJsParam = nullptr;
81 NapiHiSysEventUtil::CreateInt32Value(env, reason, reasonJsParam);
82 napi_value totalJsParam = nullptr;
83 NapiHiSysEventUtil::CreateInt32Value(env, total, totalJsParam);
84 napi_value seqJsParm = nullptr;
85 NapiHiSysEventUtil::CreateInt64Value(env, seq, seqJsParm);
86 napi_value argv[ON_QUERY_COMPLTE_COUNT] = {reasonJsParam, totalJsParam, seqJsParm};
87 napi_value querier = nullptr;
88 napi_get_reference_value(env, ref, &querier);
89 napi_value OnComplete = NapiHiSysEventUtil::GetPropertyByName(env, querier, ON_COMPLETE_ATTR);
90 napi_value ret = nullptr;
91 napi_status status = napi_call_function(env, querier, OnComplete, ON_QUERY_COMPLTE_COUNT,
92 argv, &ret);
93 if (status != napi_ok) {
94 HiLog::Error(LABEL, "failed to call OnComplete JS function.");
95 }
96 }, [this] (pid_t threadId) {
97 if (threadId != syscall(SYS_gettid)) {
98 return;
99 }
100 if (this->onCompleteHandler != nullptr && this->callbackContext != nullptr) {
101 this->onCompleteHandler(this->callbackContext->env, this->callbackContext->ref);
102 }
103 });
104 }
105 } // HiviewDFX
106 } // OHOS