• 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 "napi_hisysevent_listener.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_LISTENER" };
25 constexpr char ON_EVENT_ATTR[] = "onEvent";
26 constexpr char ON_SERVICE_DIED_ATTR[] = "onServiceDied";
27 constexpr size_t ON_EVENT_PARAM_COUNT = 1;
28 constexpr size_t ON_SERVICE_DIED_PARAM_COUNT = 0;
29 }
30 
NapiHiSysEventListener(CallbackContext * context)31 NapiHiSysEventListener::NapiHiSysEventListener(CallbackContext* context)
32 {
33     callbackContext = context;
34     jsCallbackManager = std::make_shared<JsCallbackManager>();
35 }
36 
~NapiHiSysEventListener()37 NapiHiSysEventListener::~NapiHiSysEventListener()
38 {
39     if (jsCallbackManager != nullptr) {
40         jsCallbackManager->Release();
41     }
42     if (callbackContext->threadId == syscall(SYS_gettid)) {
43         napi_delete_reference(callbackContext->env, callbackContext->ref);
44     }
45     delete callbackContext;
46 }
47 
OnEvent(const std::string & domain,const std::string & eventName,const int eventType,const std::string & eventDetail)48 void NapiHiSysEventListener::OnEvent(const std::string& domain, const std::string& eventName, const int eventType,
49     const std::string& eventDetail)
50 {
51     jsCallbackManager->Add(callbackContext,
52         [this, domain, eventName, eventType, eventDetail] (const napi_env env, const napi_ref ref, pid_t threadId) {
53             if (threadId != syscall(SYS_gettid)) {
54                 return;
55             }
56             napi_value sysEventInfo = nullptr;
57             NapiHiSysEventUtil::CreateHiSysEventInfoJsObject(env, eventDetail, sysEventInfo);
58             napi_value argv[ON_EVENT_PARAM_COUNT] = {sysEventInfo};
59             napi_value listener = nullptr;
60             napi_status status = napi_get_reference_value(env, ref, &listener);
61             if (status != napi_ok) {
62                 HiLog::Error(LABEL, "failed to get JS reference of event listener.");
63             }
64             napi_value onEvent = NapiHiSysEventUtil::GetPropertyByName(env, listener, ON_EVENT_ATTR);
65             napi_value ret = nullptr;
66             status = napi_call_function(env, listener, onEvent, ON_EVENT_PARAM_COUNT, argv, &ret);
67             if (status != napi_ok) {
68                 HiLog::Error(LABEL, "failed to call onEvent JS function.");
69             }
70         });
71 }
72 
OnServiceDied()73 void NapiHiSysEventListener::OnServiceDied()
74 {
75     jsCallbackManager->Add(callbackContext,
76         [this] (const napi_env env, const napi_ref ref, pid_t threadId) {
77             if (threadId != syscall(SYS_gettid)) {
78                 return;
79             }
80             napi_value listener = nullptr;
81             napi_status status = napi_get_reference_value(env, ref, &listener);
82             if (status != napi_ok) {
83                 HiLog::Error(LABEL, "failed to get JS reference of event listener.");
84             }
85             napi_value onServiceDied = NapiHiSysEventUtil::GetPropertyByName(env, listener, ON_SERVICE_DIED_ATTR);
86             napi_value ret = nullptr;
87             status = napi_call_function(env, listener, onServiceDied, ON_SERVICE_DIED_PARAM_COUNT,
88                 nullptr, &ret);
89             if (status != napi_ok) {
90                 HiLog::Error(LABEL, "failed to call onServiceDied JS function.");
91             }
92         });
93 }
94 } // HiviewDFX
95 } // OHOS