• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #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 is 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     };
52     return errMap.find(result) == errMap.end() ? NapiUtil::CreateNull(env) :
53         NapiUtil::CreateError(env, errMap.at(result).first, errMap.at(result).second);
54 }
55 }
56 
Write(const napi_env env,HiAppEventAsyncContext * asyncContext)57 void Write(const napi_env env, HiAppEventAsyncContext* asyncContext)
58 {
59     napi_value resource = NapiUtil::CreateString(env, "NapiHiAppEventWriter");
60     napi_create_async_work(env, nullptr, resource,
61         [](napi_env env, void* data) {
62             HiAppEventAsyncContext* asyncContext = (HiAppEventAsyncContext*)data;
63             if (asyncContext->appEventPack != nullptr && asyncContext->result >= 0) {
64                 WriteEvent(asyncContext->appEventPack);
65             }
66         },
67         [](napi_env env, napi_status status, void* data) {
68             HiAppEventAsyncContext* asyncContext = (HiAppEventAsyncContext*)data;
69             napi_value results[RESULT_SIZE] = { 0 };
70             if (asyncContext->result == 0) {
71                 results[ERR_INDEX] = NapiUtil::CreateNull(env);
72                 results[VALUE_INDEX] = NapiUtil::CreateInt32(env, asyncContext->result);
73             } else {
74                 if (asyncContext->isV9) {
75                     results[ERR_INDEX] = BuildErrorByResult(env, asyncContext->result);
76                 } else {
77                     results[ERR_INDEX] = NapiUtil::CreateObject(env, "code",
78                         NapiUtil::CreateInt32(env, asyncContext->result));
79                 }
80                 results[VALUE_INDEX] = NapiUtil::CreateNull(env);
81             }
82 
83             if (asyncContext->deferred != nullptr) { // promise
84                 if (asyncContext->result == 0) {
85                     napi_resolve_deferred(env, asyncContext->deferred, results[VALUE_INDEX]);
86                 } else {
87                     napi_reject_deferred(env, asyncContext->deferred, results[ERR_INDEX]);
88                 }
89             } else { // callback
90                 napi_value callback = nullptr;
91                 napi_get_reference_value(env, asyncContext->callback, &callback);
92                 napi_value retValue = nullptr;
93                 napi_call_function(env, nullptr, callback, RESULT_SIZE, results, &retValue);
94                 napi_delete_reference(env, asyncContext->callback);
95             }
96             napi_delete_async_work(env, asyncContext->asyncWork);
97             delete asyncContext;
98         },
99         (void*)asyncContext, &asyncContext->asyncWork);
100     napi_queue_async_work(env, asyncContext->asyncWork);
101 }
102 } // namespace NapiHiAppEventWrite
103 } // namespace HiviewDFX
104 } // namespace OHOS
105