1 /*
2 * Copyright (c) 2021-2024 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 #include "napi_hiappevent_write.h"
16
17 #include <map>
18
19 #include "hiappevent_base.h"
20 #include "hiappevent_write.h"
21 #include "napi_error.h"
22 #include "napi_util.h"
23
24 using namespace OHOS::HiviewDFX;
25
26 namespace OHOS {
27 namespace HiviewDFX {
28 namespace NapiHiAppEventWrite {
29 namespace {
30 constexpr size_t ERR_INDEX = 0;
31 constexpr size_t VALUE_INDEX = 1;
32 constexpr size_t RESULT_SIZE = 2;
33
BuildErrorByResult(const napi_env env,int result)34 napi_value BuildErrorByResult(const napi_env env, int result)
35 {
36 const std::map<int, std::pair<int, std::string>> errMap = {
37 { ErrorCode::ERROR_INVALID_EVENT_NAME,
38 { NapiError::ERR_INVALID_NAME, "Invalid event name." } },
39 { ErrorCode::ERROR_INVALID_EVENT_DOMAIN,
40 { NapiError::ERR_INVALID_DOMAIN, "Invalid event domain." } },
41 { ErrorCode::ERROR_HIAPPEVENT_DISABLE,
42 { NapiError::ERR_DISABLE, "Function disabled." } },
43 { ErrorCode::ERROR_INVALID_PARAM_NAME,
44 { NapiError::ERR_INVALID_KEY, "Invalid event parameter name." } },
45 { ErrorCode::ERROR_INVALID_PARAM_VALUE_LENGTH,
46 { NapiError::ERR_INVALID_STR_LEN, "Invalid string length of the event parameter." } },
47 { ErrorCode::ERROR_INVALID_PARAM_NUM,
48 { NapiError::ERR_INVALID_PARAM_NUM, "Invalid number of event parameters." } },
49 { ErrorCode::ERROR_INVALID_LIST_PARAM_SIZE,
50 { NapiError::ERR_INVALID_ARR_LEN, "Invalid array length of the event parameter." } },
51 { ErrorCode::ERROR_INVALID_CUSTOM_PARAM_NUM,
52 { NapiError::ERR_INVALID_CUSTOM_PARAM_NUM, "The number of parameter keys exceeds the limit." }},
53 };
54 return errMap.find(result) == errMap.end() ? NapiUtil::CreateNull(env) :
55 NapiUtil::CreateError(env, errMap.at(result).first, errMap.at(result).second);
56 }
57 }
58
Write(const napi_env env,HiAppEventAsyncContext * asyncContext)59 void Write(const napi_env env, HiAppEventAsyncContext* asyncContext)
60 {
61 napi_value resource = NapiUtil::CreateString(env, "NapiHiAppEventWriter");
62 napi_create_async_work(env, nullptr, resource,
63 [](napi_env env, void* data) {
64 HiAppEventAsyncContext* asyncContext = (HiAppEventAsyncContext*)data;
65 if (asyncContext->appEventPack != nullptr && asyncContext->result >= 0) {
66 WriteEvent(asyncContext->appEventPack);
67 }
68 },
69 [](napi_env env, napi_status status, void* data) {
70 HiAppEventAsyncContext* asyncContext = (HiAppEventAsyncContext*)data;
71 napi_value results[RESULT_SIZE] = { 0 };
72 if (asyncContext->result == 0) {
73 results[ERR_INDEX] = NapiUtil::CreateNull(env);
74 results[VALUE_INDEX] = NapiUtil::CreateInt32(env, asyncContext->result);
75 } else {
76 if (asyncContext->isV9) {
77 results[ERR_INDEX] = BuildErrorByResult(env, asyncContext->result);
78 } else {
79 results[ERR_INDEX] = NapiUtil::CreateObject(env, "code",
80 NapiUtil::CreateInt32(env, asyncContext->result));
81 }
82 results[VALUE_INDEX] = NapiUtil::CreateNull(env);
83 }
84
85 if (asyncContext->deferred != nullptr) { // promise
86 if (asyncContext->result == 0) {
87 napi_resolve_deferred(env, asyncContext->deferred, results[VALUE_INDEX]);
88 } else {
89 napi_reject_deferred(env, asyncContext->deferred, results[ERR_INDEX]);
90 }
91 } else { // callback
92 napi_value callback = nullptr;
93 napi_get_reference_value(env, asyncContext->callback, &callback);
94 napi_value retValue = nullptr;
95 napi_call_function(env, nullptr, callback, RESULT_SIZE, results, &retValue);
96 napi_delete_reference(env, asyncContext->callback);
97 }
98 napi_delete_async_work(env, asyncContext->asyncWork);
99 delete asyncContext;
100 },
101 (void*)asyncContext, &asyncContext->asyncWork);
102 napi_queue_async_work_with_qos(env, asyncContext->asyncWork, napi_qos_default);
103 }
104
SetEventParam(const napi_env env,HiAppEventAsyncContext * asyncContext)105 void SetEventParam(const napi_env env, HiAppEventAsyncContext* asyncContext)
106 {
107 napi_value resource = NapiUtil::CreateString(env, "NapiHiAppEventSetEventParam");
108 napi_create_async_work(env, nullptr, resource,
109 [](napi_env env, void* data) {
110 HiAppEventAsyncContext* asyncContext = (HiAppEventAsyncContext*)data;
111 if (asyncContext->appEventPack != nullptr && asyncContext->result == 0) {
112 if (auto ret = SetEventParam(asyncContext->appEventPack); ret > 0) {
113 asyncContext->result = ret;
114 }
115 }
116 },
117 [](napi_env env, napi_status status, void* data) {
118 HiAppEventAsyncContext* asyncContext = (HiAppEventAsyncContext*)data;
119 napi_value results[RESULT_SIZE] = { 0 };
120 if (asyncContext != nullptr && asyncContext->deferred != nullptr) { // promise
121 if (asyncContext->result == 0) {
122 results[ERR_INDEX] = NapiUtil::CreateNull(env);
123 results[VALUE_INDEX] = NapiUtil::CreateInt32(env, asyncContext->result);
124 napi_resolve_deferred(env, asyncContext->deferred, results[VALUE_INDEX]);
125 } else {
126 results[ERR_INDEX] = BuildErrorByResult(env, asyncContext->result);
127 results[VALUE_INDEX] = NapiUtil::CreateNull(env);
128 napi_reject_deferred(env, asyncContext->deferred, results[ERR_INDEX]);
129 }
130 }
131 napi_delete_async_work(env, asyncContext->asyncWork);
132 delete asyncContext;
133 },
134 (void*)asyncContext, &asyncContext->asyncWork);
135 napi_queue_async_work_with_qos(env, asyncContext->asyncWork, napi_qos_default);
136 }
137 } // namespace NapiHiAppEventWrite
138 } // namespace HiviewDFX
139 } // namespace OHOS
140