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