1 /*
2  * Copyright (c) 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 
16 #include "napi_hidebug_init.h"
17 
18 #include <map>
19 #include <utility>
20 #include <vector>
21 
22 #include "hitrace_meter.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace {
27 constexpr int32_t MAIN_THREAD = 1;
28 constexpr int32_t ALL_THREADS = 2;
29 const std::string TRACE_FLAG_CLASS_NAME = "TraceFlag";
30 const std::string TAGS_CLASS_NAME = "tags";
31 
ClassConstructor(napi_env env, napi_callback_info info)32 napi_value ClassConstructor(napi_env env, napi_callback_info info)
33 {
34     size_t argc = 0;
35     napi_value argv = nullptr;
36     napi_value thisArg = nullptr;
37     void* data = nullptr;
38     napi_get_cb_info(env, info, &argc, &argv, &thisArg, &data);
39 
40     napi_value global = 0;
41     napi_get_global(env, &global);
42 
43     return thisArg;
44 }
45 
CreateInt32(const napi_env env, int32_t num)46 napi_value CreateInt32(const napi_env env, int32_t num)
47 {
48     napi_value intValue = nullptr;
49     if (napi_create_int32(env, num, &intValue) != napi_ok) {
50         return nullptr;
51     }
52     return intValue;
53 }
54 
CreateBigintUint64(const napi_env env, uint64_t num)55 napi_value CreateBigintUint64(const napi_env env, uint64_t num)
56 {
57     napi_value intValue = nullptr;
58     if (napi_create_bigint_uint64(env, num, &intValue) != napi_ok) {
59         return nullptr;
60     }
61     return intValue;
62 }
63 
InitTraceFlagVector(napi_env env, std::vector<std::pair<const char*, napi_value>>& traceFlagVector)64 void InitTraceFlagVector(napi_env env, std::vector<std::pair<const char*, napi_value>>& traceFlagVector)
65 {
66     traceFlagVector.emplace_back("MAIN_THREAD", CreateInt32(env, MAIN_THREAD));
67     traceFlagVector.emplace_back("ALL_THREADS", CreateInt32(env, ALL_THREADS));
68 }
69 
InitTagsVector(napi_env env, std::vector<std::pair<const char*, napi_value>>& tagsVector)70 void InitTagsVector(napi_env env, std::vector<std::pair<const char*, napi_value>>& tagsVector)
71 {
72     tagsVector.emplace_back("ABILITY_MANAGER", CreateBigintUint64(env, HITRACE_TAG_ABILITY_MANAGER));
73     tagsVector.emplace_back("ARKUI", CreateBigintUint64(env, HITRACE_TAG_ACE));
74     tagsVector.emplace_back("ARK", CreateBigintUint64(env, HITRACE_TAG_ARK));
75     tagsVector.emplace_back("BLUETOOTH", CreateBigintUint64(env, HITRACE_TAG_BLUETOOTH));
76     tagsVector.emplace_back("COMMON_LIBRARY", CreateBigintUint64(env, HITRACE_TAG_COMMONLIBRARY));
77     tagsVector.emplace_back("DISTRIBUTED_HARDWARE_DEVICE_MANAGER", CreateBigintUint64(env, HITRACE_TAG_DEVICE_MANAGER));
78     tagsVector.emplace_back("DISTRIBUTED_AUDIO", CreateBigintUint64(env, HITRACE_TAG_DISTRIBUTED_AUDIO));
79     tagsVector.emplace_back("DISTRIBUTED_CAMERA", CreateBigintUint64(env, HITRACE_TAG_DISTRIBUTED_CAMERA));
80     tagsVector.emplace_back("DISTRIBUTED_DATA", CreateBigintUint64(env, HITRACE_TAG_DISTRIBUTEDDATA));
81     tagsVector.emplace_back("DISTRIBUTED_HARDWARE_FRAMEWORK",
82         CreateBigintUint64(env, HITRACE_TAG_DISTRIBUTED_HARDWARE_FWK));
83     tagsVector.emplace_back("DISTRIBUTED_INPUT", CreateBigintUint64(env, HITRACE_TAG_DISTRIBUTED_INPUT));
84     tagsVector.emplace_back("DISTRIBUTED_SCREEN", CreateBigintUint64(env, HITRACE_TAG_DISTRIBUTED_SCREEN));
85     tagsVector.emplace_back("DISTRIBUTED_SCHEDULER", CreateBigintUint64(env, HITRACE_TAG_DISTRIBUTED_SCHEDULE));
86     tagsVector.emplace_back("FFRT", CreateBigintUint64(env, HITRACE_TAG_FFRT));
87     tagsVector.emplace_back("FILE_MANAGEMENT", CreateBigintUint64(env, HITRACE_TAG_FILEMANAGEMENT));
88     tagsVector.emplace_back("GLOBAL_RESOURCE_MANAGER", CreateBigintUint64(env, HITRACE_TAG_GLOBAL_RESMGR));
89     tagsVector.emplace_back("GRAPHICS", CreateBigintUint64(env, HITRACE_TAG_GRAPHIC_AGP));
90     tagsVector.emplace_back("HDF", CreateBigintUint64(env, HITRACE_TAG_HDF));
91     tagsVector.emplace_back("MISC", CreateBigintUint64(env, HITRACE_TAG_MISC));
92     tagsVector.emplace_back("MULTIMODAL_INPUT", CreateBigintUint64(env, HITRACE_TAG_MULTIMODALINPUT));
93     tagsVector.emplace_back("NET", CreateBigintUint64(env, HITRACE_TAG_NET));
94     tagsVector.emplace_back("NOTIFICATION", CreateBigintUint64(env, HITRACE_TAG_NOTIFICATION));
95     tagsVector.emplace_back("NWEB", CreateBigintUint64(env, HITRACE_TAG_NWEB));
96     tagsVector.emplace_back("OHOS", CreateBigintUint64(env, HITRACE_TAG_OHOS));
97     tagsVector.emplace_back("POWER_MANAGER", CreateBigintUint64(env, HITRACE_TAG_POWER));
98     tagsVector.emplace_back("RPC", CreateBigintUint64(env, HITRACE_TAG_RPC));
99     tagsVector.emplace_back("SAMGR", CreateBigintUint64(env, HITRACE_TAG_SAMGR));
100     tagsVector.emplace_back("WINDOW_MANAGER", CreateBigintUint64(env, HITRACE_TAG_WINDOW_MANAGER));
101     tagsVector.emplace_back("AUDIO", CreateBigintUint64(env, HITRACE_TAG_ZAUDIO));
102     tagsVector.emplace_back("CAMERA", CreateBigintUint64(env, HITRACE_TAG_ZCAMERA));
103     tagsVector.emplace_back("IMAGE", CreateBigintUint64(env, HITRACE_TAG_ZIMAGE));
104     tagsVector.emplace_back("MEDIA", CreateBigintUint64(env, HITRACE_TAG_ZMEDIA));
105 }
106 
InitConstClassByName(napi_env env, napi_value exports, const std::string& name)107 void InitConstClassByName(napi_env env, napi_value exports, const std::string& name)
108 {
109     std::vector<std::pair<const char*, napi_value>> propertyVector;
110     if (name == TRACE_FLAG_CLASS_NAME) {
111         InitTraceFlagVector(env, propertyVector);
112     } else if (name == TAGS_CLASS_NAME) {
113         InitTagsVector(env, propertyVector);
114     } else {
115         return;
116     }
117 
118     int i = 0;
119     napi_property_descriptor descriptors[propertyVector.size()];
120     for (auto& it : propertyVector) {
121         descriptors[i++] = DECLARE_NAPI_STATIC_PROPERTY(it.first, it.second);
122     }
123 
124     napi_value result = nullptr;
125     napi_define_class(env, name.c_str(), NAPI_AUTO_LENGTH, ClassConstructor, nullptr,
126         sizeof(descriptors) / sizeof(*descriptors), descriptors, &result);
127     napi_set_named_property(env, exports, name.c_str(), result);
128 }
129 }
130 
InitNapiClass(napi_env env, napi_value exports)131 napi_value InitNapiClass(napi_env env, napi_value exports)
132 {
133     InitConstClassByName(env, exports, TRACE_FLAG_CLASS_NAME);
134     InitConstClassByName(env, exports, TAGS_CLASS_NAME);
135     return exports;
136 }
137 
138 } // namespace HiviewDFX
139 } // namespace OHOS