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_config.h"
16
17 #include <map>
18 #include <unordered_set>
19 #include <string>
20
21 #include "hiappevent_config.h"
22 #include "resource_overlimit_mgr.h"
23 #include "hilog/log.h"
24 #include "napi_config_builder.h"
25 #include "napi_error.h"
26 #include "napi_util.h"
27
28 #undef LOG_DOMAIN
29 #define LOG_DOMAIN 0xD002D07
30
31 #undef LOG_TAG
32 #define LOG_TAG "NapiHiAppEventConfig"
33
34 namespace OHOS {
35 namespace HiviewDFX {
36 namespace NapiHiAppEventConfig {
37 namespace {
38 constexpr int ERROR_SET_FAILED = -1;
39 constexpr const char* const APP_CRASH = "APP_CRASH";
40 constexpr const char* const MAIN_THREAD_JANK = "MAIN_THREAD_JANK";
41 const std::map<std::string, napi_valuetype> CONFIG_OPTION_MAP = {
42 { "disable", napi_boolean },
43 { "maxStorage", napi_string },
44 };
45
SetEventConfigSync(HiAppEventConfigAsyncContext * asyncContext)46 int SetEventConfigSync(HiAppEventConfigAsyncContext* asyncContext)
47 {
48 if (asyncContext->eventConfigPack->eventName == APP_CRASH) {
49 return HiAppEventConfig::GetInstance().SetCrashConfig(asyncContext->eventConfigPack->configUintMap);
50 }
51 std::unordered_set<std::string> whiteList = { MAIN_THREAD_JANK, RESOURCE_OVERLIMIT };
52 if (whiteList.count(asyncContext->eventConfigPack->eventName) != 0) {
53 return HiAppEventConfig::GetInstance().SetEventConfig(asyncContext->eventConfigPack->eventName,
54 asyncContext->eventConfigPack->configStringMap);
55 }
56 HILOG_ERROR(LOG_CORE, "Failed to set event config, name is invalid. name=%{public}s",
57 asyncContext->eventConfigPack->eventName.c_str());
58 return ERROR_SET_FAILED;
59 }
60 }
Configure(const napi_env env,const napi_value configObj,bool isThrow)61 bool Configure(const napi_env env, const napi_value configObj, bool isThrow)
62 {
63 if (!NapiUtil::IsObject(env, configObj)) {
64 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("config", "ConfigOption"), isThrow);
65 return false;
66 }
67
68 std::vector<std::string> keys;
69 NapiUtil::GetPropertyNames(env, configObj, keys);
70 for (const auto& key : keys) {
71 if (CONFIG_OPTION_MAP.find(key) == CONFIG_OPTION_MAP.end()) {
72 continue;
73 }
74 napi_value value = NapiUtil::GetProperty(env, configObj, key);
75 if (CONFIG_OPTION_MAP.at(key) != NapiUtil::GetType(env, value)) {
76 std::string errMsg = NapiUtil::CreateErrMsg(key, CONFIG_OPTION_MAP.at(key));
77 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, errMsg, isThrow);
78 return false;
79 }
80 if (!HiAppEventConfig::GetInstance().SetConfigurationItem(key, NapiUtil::ConvertToString(env, value))) {
81 NapiUtil::ThrowErrorMsg(env, NapiError::ERR_INVALID_MAX_STORAGE, isThrow);
82 return false;
83 }
84 }
85 return true;
86 }
87
IsDisable()88 bool IsDisable()
89 {
90 return HiAppEventConfig::GetInstance().GetDisable();
91 }
92
GetStorageDir()93 std::string GetStorageDir()
94 {
95 return HiAppEventConfig::GetInstance().GetStorageDir();
96 }
97
SetEventConfig(const napi_env env,HiAppEventConfigAsyncContext * asyncContext)98 void SetEventConfig(const napi_env env, HiAppEventConfigAsyncContext* asyncContext)
99 {
100 napi_value resource = NapiUtil::CreateString(env, "NapiHiAppEventSetEventConfig");
101 napi_create_async_work(env, nullptr, resource,
102 [](napi_env env, void* data) {
103 HiAppEventConfigAsyncContext* asyncContext = (HiAppEventConfigAsyncContext*)data;
104 asyncContext->result = asyncContext->eventConfigPack->isValid ? SetEventConfigSync(asyncContext) :
105 ERROR_SET_FAILED;
106 },
107 [](napi_env env, napi_status status, void* data) {
108 HiAppEventConfigAsyncContext* asyncContext = (HiAppEventConfigAsyncContext*)data;
109 napi_value result = nullptr;
110 if (asyncContext != nullptr && asyncContext->deferred != nullptr) { // promise
111 if (asyncContext->result == 0) {
112 result = NapiUtil::CreateInt32(env, asyncContext->result);
113 napi_resolve_deferred(env, asyncContext->deferred, result);
114 } else {
115 result = NapiUtil::CreateError(env, NapiError::ERR_PARAM, "Invalid param value for event config.");
116 napi_reject_deferred(env, asyncContext->deferred, result);
117 }
118 }
119 napi_delete_async_work(env, asyncContext->asyncWork);
120 delete asyncContext;
121 },
122 (void*)asyncContext, &asyncContext->asyncWork);
123 napi_queue_async_work_with_qos(env, asyncContext->asyncWork, napi_qos_default);
124 }
125 } // namespace NapiHiAppEventConfig
126 } // namespace HiviewDFX
127 } // namespace OHOS
128