106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci */ 1506f6ba60Sopenharmony_ci 1606f6ba60Sopenharmony_ci#include "napi_hidebug_init.h" 1706f6ba60Sopenharmony_ci 1806f6ba60Sopenharmony_ci#include <map> 1906f6ba60Sopenharmony_ci#include <utility> 2006f6ba60Sopenharmony_ci#include <vector> 2106f6ba60Sopenharmony_ci 2206f6ba60Sopenharmony_ci#include "hitrace_meter.h" 2306f6ba60Sopenharmony_ci 2406f6ba60Sopenharmony_cinamespace OHOS { 2506f6ba60Sopenharmony_cinamespace HiviewDFX { 2606f6ba60Sopenharmony_cinamespace { 2706f6ba60Sopenharmony_ciconstexpr int32_t MAIN_THREAD = 1; 2806f6ba60Sopenharmony_ciconstexpr int32_t ALL_THREADS = 2; 2906f6ba60Sopenharmony_ciconst std::string TRACE_FLAG_CLASS_NAME = "TraceFlag"; 3006f6ba60Sopenharmony_ciconst std::string TAGS_CLASS_NAME = "tags"; 3106f6ba60Sopenharmony_ci 3206f6ba60Sopenharmony_cinapi_value ClassConstructor(napi_env env, napi_callback_info info) 3306f6ba60Sopenharmony_ci{ 3406f6ba60Sopenharmony_ci size_t argc = 0; 3506f6ba60Sopenharmony_ci napi_value argv = nullptr; 3606f6ba60Sopenharmony_ci napi_value thisArg = nullptr; 3706f6ba60Sopenharmony_ci void* data = nullptr; 3806f6ba60Sopenharmony_ci napi_get_cb_info(env, info, &argc, &argv, &thisArg, &data); 3906f6ba60Sopenharmony_ci 4006f6ba60Sopenharmony_ci napi_value global = 0; 4106f6ba60Sopenharmony_ci napi_get_global(env, &global); 4206f6ba60Sopenharmony_ci 4306f6ba60Sopenharmony_ci return thisArg; 4406f6ba60Sopenharmony_ci} 4506f6ba60Sopenharmony_ci 4606f6ba60Sopenharmony_cinapi_value CreateInt32(const napi_env env, int32_t num) 4706f6ba60Sopenharmony_ci{ 4806f6ba60Sopenharmony_ci napi_value intValue = nullptr; 4906f6ba60Sopenharmony_ci if (napi_create_int32(env, num, &intValue) != napi_ok) { 5006f6ba60Sopenharmony_ci return nullptr; 5106f6ba60Sopenharmony_ci } 5206f6ba60Sopenharmony_ci return intValue; 5306f6ba60Sopenharmony_ci} 5406f6ba60Sopenharmony_ci 5506f6ba60Sopenharmony_cinapi_value CreateBigintUint64(const napi_env env, uint64_t num) 5606f6ba60Sopenharmony_ci{ 5706f6ba60Sopenharmony_ci napi_value intValue = nullptr; 5806f6ba60Sopenharmony_ci if (napi_create_bigint_uint64(env, num, &intValue) != napi_ok) { 5906f6ba60Sopenharmony_ci return nullptr; 6006f6ba60Sopenharmony_ci } 6106f6ba60Sopenharmony_ci return intValue; 6206f6ba60Sopenharmony_ci} 6306f6ba60Sopenharmony_ci 6406f6ba60Sopenharmony_civoid InitTraceFlagVector(napi_env env, std::vector<std::pair<const char*, napi_value>>& traceFlagVector) 6506f6ba60Sopenharmony_ci{ 6606f6ba60Sopenharmony_ci traceFlagVector.emplace_back("MAIN_THREAD", CreateInt32(env, MAIN_THREAD)); 6706f6ba60Sopenharmony_ci traceFlagVector.emplace_back("ALL_THREADS", CreateInt32(env, ALL_THREADS)); 6806f6ba60Sopenharmony_ci} 6906f6ba60Sopenharmony_ci 7006f6ba60Sopenharmony_civoid InitTagsVector(napi_env env, std::vector<std::pair<const char*, napi_value>>& tagsVector) 7106f6ba60Sopenharmony_ci{ 7206f6ba60Sopenharmony_ci tagsVector.emplace_back("ABILITY_MANAGER", CreateBigintUint64(env, HITRACE_TAG_ABILITY_MANAGER)); 7306f6ba60Sopenharmony_ci tagsVector.emplace_back("ARKUI", CreateBigintUint64(env, HITRACE_TAG_ACE)); 7406f6ba60Sopenharmony_ci tagsVector.emplace_back("ARK", CreateBigintUint64(env, HITRACE_TAG_ARK)); 7506f6ba60Sopenharmony_ci tagsVector.emplace_back("BLUETOOTH", CreateBigintUint64(env, HITRACE_TAG_BLUETOOTH)); 7606f6ba60Sopenharmony_ci tagsVector.emplace_back("COMMON_LIBRARY", CreateBigintUint64(env, HITRACE_TAG_COMMONLIBRARY)); 7706f6ba60Sopenharmony_ci tagsVector.emplace_back("DISTRIBUTED_HARDWARE_DEVICE_MANAGER", CreateBigintUint64(env, HITRACE_TAG_DEVICE_MANAGER)); 7806f6ba60Sopenharmony_ci tagsVector.emplace_back("DISTRIBUTED_AUDIO", CreateBigintUint64(env, HITRACE_TAG_DISTRIBUTED_AUDIO)); 7906f6ba60Sopenharmony_ci tagsVector.emplace_back("DISTRIBUTED_CAMERA", CreateBigintUint64(env, HITRACE_TAG_DISTRIBUTED_CAMERA)); 8006f6ba60Sopenharmony_ci tagsVector.emplace_back("DISTRIBUTED_DATA", CreateBigintUint64(env, HITRACE_TAG_DISTRIBUTEDDATA)); 8106f6ba60Sopenharmony_ci tagsVector.emplace_back("DISTRIBUTED_HARDWARE_FRAMEWORK", 8206f6ba60Sopenharmony_ci CreateBigintUint64(env, HITRACE_TAG_DISTRIBUTED_HARDWARE_FWK)); 8306f6ba60Sopenharmony_ci tagsVector.emplace_back("DISTRIBUTED_INPUT", CreateBigintUint64(env, HITRACE_TAG_DISTRIBUTED_INPUT)); 8406f6ba60Sopenharmony_ci tagsVector.emplace_back("DISTRIBUTED_SCREEN", CreateBigintUint64(env, HITRACE_TAG_DISTRIBUTED_SCREEN)); 8506f6ba60Sopenharmony_ci tagsVector.emplace_back("DISTRIBUTED_SCHEDULER", CreateBigintUint64(env, HITRACE_TAG_DISTRIBUTED_SCHEDULE)); 8606f6ba60Sopenharmony_ci tagsVector.emplace_back("FFRT", CreateBigintUint64(env, HITRACE_TAG_FFRT)); 8706f6ba60Sopenharmony_ci tagsVector.emplace_back("FILE_MANAGEMENT", CreateBigintUint64(env, HITRACE_TAG_FILEMANAGEMENT)); 8806f6ba60Sopenharmony_ci tagsVector.emplace_back("GLOBAL_RESOURCE_MANAGER", CreateBigintUint64(env, HITRACE_TAG_GLOBAL_RESMGR)); 8906f6ba60Sopenharmony_ci tagsVector.emplace_back("GRAPHICS", CreateBigintUint64(env, HITRACE_TAG_GRAPHIC_AGP)); 9006f6ba60Sopenharmony_ci tagsVector.emplace_back("HDF", CreateBigintUint64(env, HITRACE_TAG_HDF)); 9106f6ba60Sopenharmony_ci tagsVector.emplace_back("MISC", CreateBigintUint64(env, HITRACE_TAG_MISC)); 9206f6ba60Sopenharmony_ci tagsVector.emplace_back("MULTIMODAL_INPUT", CreateBigintUint64(env, HITRACE_TAG_MULTIMODALINPUT)); 9306f6ba60Sopenharmony_ci tagsVector.emplace_back("NET", CreateBigintUint64(env, HITRACE_TAG_NET)); 9406f6ba60Sopenharmony_ci tagsVector.emplace_back("NOTIFICATION", CreateBigintUint64(env, HITRACE_TAG_NOTIFICATION)); 9506f6ba60Sopenharmony_ci tagsVector.emplace_back("NWEB", CreateBigintUint64(env, HITRACE_TAG_NWEB)); 9606f6ba60Sopenharmony_ci tagsVector.emplace_back("OHOS", CreateBigintUint64(env, HITRACE_TAG_OHOS)); 9706f6ba60Sopenharmony_ci tagsVector.emplace_back("POWER_MANAGER", CreateBigintUint64(env, HITRACE_TAG_POWER)); 9806f6ba60Sopenharmony_ci tagsVector.emplace_back("RPC", CreateBigintUint64(env, HITRACE_TAG_RPC)); 9906f6ba60Sopenharmony_ci tagsVector.emplace_back("SAMGR", CreateBigintUint64(env, HITRACE_TAG_SAMGR)); 10006f6ba60Sopenharmony_ci tagsVector.emplace_back("WINDOW_MANAGER", CreateBigintUint64(env, HITRACE_TAG_WINDOW_MANAGER)); 10106f6ba60Sopenharmony_ci tagsVector.emplace_back("AUDIO", CreateBigintUint64(env, HITRACE_TAG_ZAUDIO)); 10206f6ba60Sopenharmony_ci tagsVector.emplace_back("CAMERA", CreateBigintUint64(env, HITRACE_TAG_ZCAMERA)); 10306f6ba60Sopenharmony_ci tagsVector.emplace_back("IMAGE", CreateBigintUint64(env, HITRACE_TAG_ZIMAGE)); 10406f6ba60Sopenharmony_ci tagsVector.emplace_back("MEDIA", CreateBigintUint64(env, HITRACE_TAG_ZMEDIA)); 10506f6ba60Sopenharmony_ci} 10606f6ba60Sopenharmony_ci 10706f6ba60Sopenharmony_civoid InitConstClassByName(napi_env env, napi_value exports, const std::string& name) 10806f6ba60Sopenharmony_ci{ 10906f6ba60Sopenharmony_ci std::vector<std::pair<const char*, napi_value>> propertyVector; 11006f6ba60Sopenharmony_ci if (name == TRACE_FLAG_CLASS_NAME) { 11106f6ba60Sopenharmony_ci InitTraceFlagVector(env, propertyVector); 11206f6ba60Sopenharmony_ci } else if (name == TAGS_CLASS_NAME) { 11306f6ba60Sopenharmony_ci InitTagsVector(env, propertyVector); 11406f6ba60Sopenharmony_ci } else { 11506f6ba60Sopenharmony_ci return; 11606f6ba60Sopenharmony_ci } 11706f6ba60Sopenharmony_ci 11806f6ba60Sopenharmony_ci int i = 0; 11906f6ba60Sopenharmony_ci napi_property_descriptor descriptors[propertyVector.size()]; 12006f6ba60Sopenharmony_ci for (auto& it : propertyVector) { 12106f6ba60Sopenharmony_ci descriptors[i++] = DECLARE_NAPI_STATIC_PROPERTY(it.first, it.second); 12206f6ba60Sopenharmony_ci } 12306f6ba60Sopenharmony_ci 12406f6ba60Sopenharmony_ci napi_value result = nullptr; 12506f6ba60Sopenharmony_ci napi_define_class(env, name.c_str(), NAPI_AUTO_LENGTH, ClassConstructor, nullptr, 12606f6ba60Sopenharmony_ci sizeof(descriptors) / sizeof(*descriptors), descriptors, &result); 12706f6ba60Sopenharmony_ci napi_set_named_property(env, exports, name.c_str(), result); 12806f6ba60Sopenharmony_ci} 12906f6ba60Sopenharmony_ci} 13006f6ba60Sopenharmony_ci 13106f6ba60Sopenharmony_cinapi_value InitNapiClass(napi_env env, napi_value exports) 13206f6ba60Sopenharmony_ci{ 13306f6ba60Sopenharmony_ci InitConstClassByName(env, exports, TRACE_FLAG_CLASS_NAME); 13406f6ba60Sopenharmony_ci InitConstClassByName(env, exports, TAGS_CLASS_NAME); 13506f6ba60Sopenharmony_ci return exports; 13606f6ba60Sopenharmony_ci} 13706f6ba60Sopenharmony_ci 13806f6ba60Sopenharmony_ci} // namespace HiviewDFX 13906f6ba60Sopenharmony_ci} // namespace OHOS