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_adapter.h"
17
18 #include "def.h"
19 #include "napi_hisysevent_util.h"
20
21 namespace OHOS {
22 namespace HiviewDFX {
23 namespace {
24 constexpr size_t ERR_INDEX = 0;
25 constexpr size_t VAL_INDEX = 1;
26 constexpr size_t RET_SIZE = 2;
27 constexpr char FUNC_SOURCE_NAME[] = "JSHiSysEventWrite";
28 }
29
Write(const napi_env env,HiSysEventAsyncContext * eventAsyncContext)30 void NapiHiSysEventAdapter::Write(const napi_env env, HiSysEventAsyncContext* eventAsyncContext)
31 {
32 napi_value resource = nullptr;
33 NapiHiSysEventUtil::CreateStringValue(env, FUNC_SOURCE_NAME, resource);
34 napi_create_async_work(
35 env, nullptr, resource,
36 [] (napi_env env, void* data) {
37 HiSysEventAsyncContext* eventAsyncContext = reinterpret_cast<HiSysEventAsyncContext*>(data);
38 if (eventAsyncContext->eventWroteResult == SUCCESS) {
39 eventAsyncContext->eventWroteResult = Write(eventAsyncContext->eventInfo);
40 }
41 },
42 [] (napi_env env, napi_status status, void* data) {
43 HiSysEventAsyncContext* eventAsyncContext = reinterpret_cast<HiSysEventAsyncContext*>(data);
44 napi_value results[RET_SIZE] = {0};
45 auto isNormalWrote = eventAsyncContext->eventWroteResult == SUCCESS &&
46 !NapiHiSysEventUtil::HasStrParamLenOverLimit(eventAsyncContext->eventInfo);
47 if (isNormalWrote) {
48 NapiHiSysEventUtil::CreateNull(env, results[ERR_INDEX]);
49 NapiHiSysEventUtil::CreateInt32Value(env, eventAsyncContext->eventWroteResult, results[VAL_INDEX]);
50 } else {
51 NapiHiSysEventUtil::CreateNull(env, results[VAL_INDEX]);
52 auto errorCode = eventAsyncContext->eventWroteResult == SUCCESS ? ERR_VALUE_LENGTH_TOO_LONG :
53 eventAsyncContext->eventWroteResult;
54 results[ERR_INDEX] = NapiHiSysEventUtil::CreateErrorByRet(env, errorCode);
55 }
56 if (eventAsyncContext->deferred != nullptr) { // promise
57 isNormalWrote ? napi_resolve_deferred(env, eventAsyncContext->deferred, results[VAL_INDEX]) :
58 napi_reject_deferred(env, eventAsyncContext->deferred, results[ERR_INDEX]);
59 } else {
60 napi_value callback = nullptr;
61 napi_get_reference_value(env, eventAsyncContext->callback, &callback);
62 napi_value retValue = nullptr;
63 napi_call_function(env, nullptr, callback, RET_SIZE, results, &retValue);
64 napi_delete_reference(env, eventAsyncContext->callback);
65 }
66 napi_delete_async_work(env, eventAsyncContext->asyncWork);
67 delete eventAsyncContext;
68 }, reinterpret_cast<void*>(eventAsyncContext), &eventAsyncContext->asyncWork);
69 napi_queue_async_work(env, eventAsyncContext->asyncWork);
70 }
71
InnerWrite(HiSysEvent::EventBase & eventBase,const HiSysEventInfo & eventInfo)72 void NapiHiSysEventAdapter::InnerWrite(HiSysEvent::EventBase& eventBase,
73 const HiSysEventInfo& eventInfo)
74 {
75 AppendData<bool>(eventBase, eventInfo.boolParams);
76 AppendArrayData<bool>(eventBase, eventInfo.boolArrayParams);
77 AppendData<double>(eventBase, eventInfo.doubleParams);
78 AppendArrayData<double>(eventBase, eventInfo.doubleArrayParams);
79 AppendData<std::string>(eventBase, eventInfo.stringParams);
80 AppendArrayData<std::string>(eventBase, eventInfo.stringArrayParams);
81 }
82
Write(const HiSysEventInfo & eventInfo)83 int NapiHiSysEventAdapter::Write(const HiSysEventInfo& eventInfo)
84 {
85 HiSysEvent::EventBase eventBase(eventInfo.domain, eventInfo.name, eventInfo.eventType);
86 eventBase.jsonStr_ << "{";
87 HiSysEvent::WritebaseInfo(eventBase);
88 if (HiSysEvent::IsError(eventBase)) {
89 HiSysEvent::ExplainRetCode(eventBase);
90 return eventBase.retCode_;
91 }
92
93 InnerWrite(eventBase, eventInfo);
94 HiSysEvent::InnerWrite(eventBase);
95 if (HiSysEvent::IsError(eventBase)) {
96 HiSysEvent::ExplainRetCode(eventBase);
97 return eventBase.retCode_;
98 }
99 eventBase.jsonStr_ << "}";
100
101 HiSysEvent::SendSysEvent(eventBase);
102 return eventBase.retCode_;
103 }
104 } // namespace HiviewDFX
105 } // namespace OHOS