1 /*
2 * Copyright (c) 2023 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_proxy_rule.h"
17
18 #include <cstring>
19 #include <js_native_api.h>
20 #include <js_native_api_types.h>
21 #include <napi/native_api.h>
22 #include "napi_parse_utils.h"
23 #include <securec.h>
24
25 #include "nweb_log.h"
26 #include "proxy_rule.h"
27
28 namespace OHOS {
29 namespace NWeb {
JS_GetSchemeFilter(napi_env env,napi_callback_info cbinfo)30 napi_value NapiProxyRule::JS_GetSchemeFilter(napi_env env, napi_callback_info cbinfo)
31 {
32 WVLOG_D("[PROXYCONTROLLER] NapiProxyRule::JS_GetSchemeFilter");
33 size_t argc = 1;
34 napi_value argv[1] = {0};
35 napi_value thisVar = nullptr;
36 void *data = nullptr;
37 ProxyRule *proxyRule = nullptr;
38 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
39
40 napi_unwrap(env, thisVar, (void **)&proxyRule);
41 if (!proxyRule) {
42 WVLOG_E("[PROXYCONTROLLER] NapiProxyRule::JS_GetSchemeFilter proxyRule is null.");
43 return nullptr;
44 }
45
46 napi_value schemeFilter = nullptr;
47 napi_status status = napi_create_int32(env, proxyRule->GetSchemeFilter(), &schemeFilter);
48 if (status != napi_ok) {
49 WVLOG_E("[PROXYCONTROLLER] NapiProxyRule::JS_SchemeFilter create string failed.");
50 return nullptr;
51 }
52 return schemeFilter;
53 }
54
JS_GetUrl(napi_env env,napi_callback_info cbinfo)55 napi_value NapiProxyRule::JS_GetUrl(napi_env env, napi_callback_info cbinfo)
56 {
57 WVLOG_D("[PROXYCONTROLLER] NapiProxyRule::JS_GetUrl");
58 size_t argc = 1;
59 napi_value argv[1] = {0};
60 napi_value thisVar = nullptr;
61 void *data = nullptr;
62 ProxyRule *proxyRule = nullptr;
63 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
64
65 napi_unwrap(env, thisVar, (void **)&proxyRule);
66 if (!proxyRule) {
67 WVLOG_E("[PROXYCONTROLLER] NapiProxyRule::JS_GetUrl ProxyRule is null.");
68 return nullptr;
69 }
70
71 napi_value urlValue = nullptr;
72 napi_status status = napi_create_string_utf8(env, proxyRule->GetUrl().c_str(), NAPI_AUTO_LENGTH, &urlValue);
73 if (status != napi_ok) {
74 WVLOG_E("[PROXYCONTROLLER] NapiProxyRule::JS_GetUrl create string failed.");
75 return nullptr;
76 }
77 return urlValue;
78 }
79
JS_Constructor(napi_env env,napi_callback_info cbinfo)80 napi_value NapiProxyRule::JS_Constructor(napi_env env, napi_callback_info cbinfo)
81 {
82 WVLOG_D("[PROXYCONTROLLER] NapiProxyRule::JS_Constructor is called");
83 napi_value thisVar = nullptr;
84 void *data = nullptr;
85 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
86
87 ProxyRule *proxyRule = new ProxyRule();
88
89 napi_wrap(
90 env, thisVar, proxyRule,
91 [](napi_env /* env */, void *data, void * /* hint */) {
92 ProxyRule *proxyRule = (ProxyRule *)data;
93 delete proxyRule;
94 },
95 nullptr, nullptr);
96
97 return thisVar;
98 }
99
Init(napi_env env,napi_value exports)100 napi_value NapiProxyRule::Init(napi_env env, napi_value exports)
101 {
102 WVLOG_D("[PROXYCONTROLLER] NapiProxyRule::Init");
103 /* export ProxyRule class */
104
105 ExportProxyRuleClass(env, &exports);
106
107 /* export ProxySchemeFilter enum */
108
109 ExportProxySchemeFilterEnum(env, &exports);
110
111 return exports;
112 }
113
ExportProxyRuleClass(napi_env env,napi_value * exportsPointer)114 void NapiProxyRule::ExportProxyRuleClass(napi_env env, napi_value* exportsPointer)
115 {
116 napi_property_descriptor properties[] = {
117 DECLARE_NAPI_FUNCTION("getUrl", JS_GetUrl),
118 DECLARE_NAPI_FUNCTION("getSchemeFilter", JS_GetSchemeFilter),
119 };
120 napi_value proxyClass = nullptr;
121 napi_define_class(env, PROXY_RULE_CLASS_NAME.c_str(), PROXY_RULE_CLASS_NAME.length(), JS_Constructor, nullptr,
122 sizeof(properties) / sizeof(properties[0]), properties, &proxyClass);
123 napi_set_named_property(env, *exportsPointer, PROXY_RULE_CLASS_NAME.c_str(), proxyClass);
124 }
125
ExportProxySchemeFilterEnum(napi_env env,napi_value * exportsPointer)126 void NapiProxyRule::ExportProxySchemeFilterEnum(napi_env env, napi_value* exportsPointer)
127 {
128 napi_value proxySchemeFilterTypeEnum = nullptr;
129 napi_property_descriptor proxySchemeFilterProperties[] = {
130 DECLARE_NAPI_STATIC_PROPERTY(
131 "MATCH_ALL_SCHEMES",
132 NapiParseUtils::ToInt32Value(
133 env, static_cast<int32_t>(ProxySchemeFilter::MATCH_ALL_SCHEMES))),
134 DECLARE_NAPI_STATIC_PROPERTY(
135 "MATCH_HTTP",
136 NapiParseUtils::ToInt32Value(
137 env, static_cast<int32_t>(ProxySchemeFilter::MATCH_HTTP))),
138 DECLARE_NAPI_STATIC_PROPERTY(
139 "MATCH_HTTPS",
140 NapiParseUtils::ToInt32Value(
141 env, static_cast<int32_t>(ProxySchemeFilter::MATCH_HTTPS))),
142 };
143 napi_define_class(env, PROXY_SCHEME_FILTER_ENUM_NAME.c_str(), PROXY_SCHEME_FILTER_ENUM_NAME.length(),
144 NapiParseUtils::CreateEnumConstructor, nullptr,
145 sizeof(proxySchemeFilterProperties) / sizeof(proxySchemeFilterProperties[0]),
146 proxySchemeFilterProperties, &proxySchemeFilterTypeEnum);
147 napi_set_named_property(env, *exportsPointer, PROXY_SCHEME_FILTER_ENUM_NAME.c_str(), proxySchemeFilterTypeEnum);
148 }
149
DefineProperties(napi_env env,napi_value * object)150 napi_status NapiProxyRule::DefineProperties(napi_env env, napi_value *object)
151 {
152 napi_property_descriptor properties[] = {
153 DECLARE_NAPI_FUNCTION("getUrl", JS_GetUrl),
154 DECLARE_NAPI_FUNCTION("getSchemeFilter", JS_GetSchemeFilter),
155 };
156 return napi_define_properties(env, *object, sizeof(properties) / sizeof(properties[0]), properties);
157 }
158 } // namespace NWeb
159 } // namespace OHOS