1 /*
2 * Copyright (c) 2021-2025 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, int> errMap = {
37 { ErrorCode::ERROR_INVALID_EVENT_NAME, NapiError::ERR_INVALID_NAME },
38 { ErrorCode::ERROR_INVALID_EVENT_DOMAIN, NapiError::ERR_INVALID_DOMAIN },
39 { ErrorCode::ERROR_HIAPPEVENT_DISABLE, NapiError::ERR_DISABLE },
40 { ErrorCode::ERROR_INVALID_PARAM_NAME, NapiError::ERR_INVALID_KEY },
41 { ErrorCode::ERROR_INVALID_PARAM_VALUE_LENGTH, NapiError::ERR_INVALID_STR_LEN },
42 { ErrorCode::ERROR_INVALID_PARAM_NUM, NapiError::ERR_INVALID_PARAM_NUM },
43 { ErrorCode::ERROR_INVALID_LIST_PARAM_SIZE, NapiError::ERR_INVALID_ARR_LEN },
44 { ErrorCode::ERROR_INVALID_CUSTOM_PARAM_NUM, NapiError::ERR_INVALID_CUSTOM_PARAM_NUM },
45 };
46 auto it = errMap.find(result);
47 return it == errMap.end() ? NapiUtil::CreateNull(env) :
48 NapiUtil::CreateError(env, it->second, NapiError::GetErrorMsg(it->second));
49 }
50 }
51
Write(const napi_env env,HiAppEventAsyncContext * asyncContext)52 void Write(const napi_env env, HiAppEventAsyncContext* asyncContext)
53 {
54 napi_value resource = NapiUtil::CreateString(env, "NapiHiAppEventWriter");
55 napi_create_async_work(env, nullptr, resource,
56 [](napi_env env, void* data) {
57 HiAppEventAsyncContext* asyncContext = (HiAppEventAsyncContext*)data;
58 if (asyncContext->appEventPack != nullptr && asyncContext->result >= 0) {
59 WriteEvent(asyncContext->appEventPack);
60 }
61 },
62 [](napi_env env, napi_status status, void* data) {
63 HiAppEventAsyncContext* asyncContext = (HiAppEventAsyncContext*)data;
64 napi_value results[RESULT_SIZE] = { 0 };
65 if (asyncContext->result == 0) {
66 results[ERR_INDEX] = NapiUtil::CreateNull(env);
67 results[VALUE_INDEX] = NapiUtil::CreateInt32(env, asyncContext->result);
68 } else {
69 if (asyncContext->isV9) {
70 results[ERR_INDEX] = BuildErrorByResult(env, asyncContext->result);
71 } else {
72 results[ERR_INDEX] = NapiUtil::CreateObject(env, "code",
73 NapiUtil::CreateInt32(env, asyncContext->result));
74 }
75 results[VALUE_INDEX] = NapiUtil::CreateNull(env);
76 }
77
78 if (asyncContext->deferred != nullptr) { // promise
79 if (asyncContext->result == 0) {
80 napi_resolve_deferred(env, asyncContext->deferred, results[VALUE_INDEX]);
81 } else {
82 napi_reject_deferred(env, asyncContext->deferred, results[ERR_INDEX]);
83 }
84 } else { // callback
85 napi_value callback = nullptr;
86 napi_get_reference_value(env, asyncContext->callback, &callback);
87 napi_value retValue = nullptr;
88 napi_call_function(env, nullptr, callback, RESULT_SIZE, results, &retValue);
89 napi_delete_reference(env, asyncContext->callback);
90 }
91 napi_delete_async_work(env, asyncContext->asyncWork);
92 delete asyncContext;
93 },
94 (void*)asyncContext, &asyncContext->asyncWork);
95 napi_queue_async_work_with_qos(env, asyncContext->asyncWork, napi_qos_default);
96 }
97
SetEventParam(const napi_env env,HiAppEventAsyncContext * asyncContext)98 void SetEventParam(const napi_env env, HiAppEventAsyncContext* asyncContext)
99 {
100 napi_value resource = NapiUtil::CreateString(env, "NapiHiAppEventSetEventParam");
101 napi_create_async_work(env, nullptr, resource,
102 [](napi_env env, void* data) {
103 HiAppEventAsyncContext* asyncContext = (HiAppEventAsyncContext*)data;
104 if (asyncContext->appEventPack != nullptr && asyncContext->result == 0) {
105 if (auto ret = SetEventParam(asyncContext->appEventPack); ret > 0) {
106 asyncContext->result = ret;
107 }
108 }
109 },
110 [](napi_env env, napi_status status, void* data) {
111 HiAppEventAsyncContext* asyncContext = (HiAppEventAsyncContext*)data;
112 napi_value results[RESULT_SIZE] = { 0 };
113 if (asyncContext != nullptr && asyncContext->deferred != nullptr) { // promise
114 if (asyncContext->result == 0) {
115 results[ERR_INDEX] = NapiUtil::CreateNull(env);
116 results[VALUE_INDEX] = NapiUtil::CreateInt32(env, asyncContext->result);
117 napi_resolve_deferred(env, asyncContext->deferred, results[VALUE_INDEX]);
118 } else {
119 results[ERR_INDEX] = BuildErrorByResult(env, asyncContext->result);
120 results[VALUE_INDEX] = NapiUtil::CreateNull(env);
121 napi_reject_deferred(env, asyncContext->deferred, results[ERR_INDEX]);
122 }
123 }
124 napi_delete_async_work(env, asyncContext->asyncWork);
125 delete asyncContext;
126 },
127 (void*)asyncContext, &asyncContext->asyncWork);
128 napi_queue_async_work_with_qos(env, asyncContext->asyncWork, napi_qos_default);
129 }
130 } // namespace NapiHiAppEventWrite
131 } // namespace HiviewDFX
132 } // namespace OHOS
133