• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2  * Copyright (c) 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 "native_module_uri.h"
16 #include "tools/log.h"
17 
18 extern const char _binary_js_uri_js_start[];
19 extern const char _binary_js_uri_js_end[];
20 extern const char _binary_uri_abc_start[];
21 extern const char _binary_uri_abc_end[];
22 
23 namespace OHOS::Uri {
UriConstructor(napi_env env,napi_callback_info info)24     static napi_value UriConstructor(napi_env env, napi_callback_info info)
25     {
26         napi_value thisVar = nullptr;
27         void *data = nullptr;
28         size_t argc = 1;
29         napi_value argv[1] = { 0 };
30         Uri *object = nullptr;
31         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
32         napi_valuetype valuetype;
33         NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
34         if (valuetype == napi_string) {
35             std::string type = "";
36             size_t typelen = 0;
37             NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen));
38             type.resize(typelen);
39             NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type.data(), typelen + 1, &typelen));
40             object = new (std::nothrow) Uri(type);
41             if (object == nullptr) {
42                 HILOG_ERROR("UriConstructor:: memory allocation failed, object is nullptr");
43                 return nullptr;
44             }
45         }
46         NAPI_CALL(env, napi_wrap(env, thisVar, object,
47             [](napi_env environment, void *data, void *hint) {
48             auto obj = reinterpret_cast<Uri*>(data);
49             if (obj != nullptr) {
50                 delete obj;
51                 obj = nullptr;
52             }
53         }, nullptr, nullptr));
54         return thisVar;
55     }
56 
Normalize(napi_env env,napi_callback_info info)57     static napi_value Normalize(napi_env env, napi_callback_info info)
58     {
59         napi_value thisVar = nullptr;
60         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
61         Uri *muri = nullptr;
62         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
63         std::string normalizeUri = muri->Normalize();
64         napi_value result = nullptr;
65         size_t tempLen = normalizeUri.size();
66         NAPI_CALL(env, napi_create_string_utf8(env, normalizeUri.c_str(), tempLen, &result));
67         return result;
68     }
69 
Equals(napi_env env,napi_callback_info info)70     static napi_value Equals(napi_env env, napi_callback_info info)
71     {
72         napi_value thisVar = nullptr;
73         napi_value result = nullptr;
74         size_t argc = 1;
75         napi_value argv[1] = { 0 };
76         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
77 
78         Uri *muri = nullptr;
79         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
80         Uri *other = nullptr;
81         NAPI_CALL(env, napi_unwrap(env, argv[0], reinterpret_cast<void**>(&other)));
82 
83         bool flag = muri->Equals(*other);
84         NAPI_CALL(env, napi_get_boolean(env, flag, &result));
85         return result;
86     }
87 
IsAbsolute(napi_env env,napi_callback_info info)88     static napi_value IsAbsolute(napi_env env, napi_callback_info info)
89     {
90         napi_value thisVar = nullptr;
91         napi_value result = nullptr;
92         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
93         Uri *muri = nullptr;
94         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
95         bool flag = muri->IsAbsolute();
96         NAPI_CALL(env, napi_get_boolean(env, flag, &result));
97         return result;
98     }
99 
IsFailed(napi_env env,napi_callback_info info)100     static napi_value IsFailed(napi_env env, napi_callback_info info)
101     {
102         napi_value thisVar = nullptr;
103         napi_value result = nullptr;
104         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
105         Uri *muri = nullptr;
106         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
107         std::string temp = muri->IsFailed();
108         size_t templen = temp.size();
109         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
110         return result;
111     }
112 
UriToString(napi_env env,napi_callback_info info)113     static napi_value UriToString(napi_env env, napi_callback_info info)
114     {
115         napi_value thisVar = nullptr;
116         napi_value result = nullptr;
117         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
118         Uri *muri = nullptr;
119         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
120         std::string temp = muri->ToString();
121         size_t templen = temp.size();
122         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
123         return result;
124     }
125 
IsRelative(napi_env env,napi_callback_info info)126     static napi_value IsRelative(napi_env env, napi_callback_info info)
127     {
128         napi_value thisVar = nullptr;
129         napi_value result = nullptr;
130         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
131         Uri *muri = nullptr;
132         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
133         bool flag = muri->IsRelative();
134         NAPI_CALL(env, napi_get_boolean(env, flag, &result));
135         return result;
136     }
137 
IsOpaque(napi_env env,napi_callback_info info)138     static napi_value IsOpaque(napi_env env, napi_callback_info info)
139     {
140         napi_value thisVar = nullptr;
141         napi_value result = nullptr;
142         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
143         Uri *muri = nullptr;
144         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
145         bool flag = muri->IsOpaque();
146         NAPI_CALL(env, napi_get_boolean(env, flag, &result));
147         return result;
148     }
149 
IsHierarchical(napi_env env,napi_callback_info info)150     static napi_value IsHierarchical(napi_env env, napi_callback_info info)
151     {
152         napi_value thisVar = nullptr;
153         napi_value result = nullptr;
154         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
155         Uri *muri = nullptr;
156         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
157         bool flag = muri->IsHierarchical();
158         NAPI_CALL(env, napi_get_boolean(env, flag, &result));
159         return result;
160     }
161 
AddQueryValue(napi_env env,napi_callback_info info)162     static napi_value AddQueryValue(napi_env env, napi_callback_info info)
163     {
164         napi_value thisVar = nullptr;
165         napi_value result = nullptr;
166         size_t argc = 2;
167         napi_value argv[2] = { nullptr };
168         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
169         Uri *muri = nullptr;
170         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
171         std::string key = "";
172         size_t keyLen = 0;
173         NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &keyLen));
174         key.resize(keyLen);
175         NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], key.data(), keyLen + 1, &keyLen));
176         std::string value = "";
177         size_t valueLen = 0;
178         NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], nullptr, 0, &valueLen));
179         value.resize(valueLen);
180         NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], value.data(), valueLen + 1, &valueLen));
181         std::string temp = muri->AddQueryValue(key, value);
182         if (temp.empty()) {
183             napi_get_null(env, &result);
184             return result;
185         }
186         size_t templen = temp.size();
187         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
188         return result;
189     }
190 
AddSegment(napi_env env,napi_callback_info info)191     static napi_value AddSegment(napi_env env, napi_callback_info info)
192     {
193         napi_value thisVar = nullptr;
194         napi_value result = nullptr;
195         size_t argc = 1;
196         napi_value argv[1] = { nullptr };
197         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
198         Uri *muri = nullptr;
199         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
200         std::string segment = "";
201         size_t segmentLen = 0;
202         NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &segmentLen));
203         segment.resize(segmentLen);
204         NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], segment.data(), segmentLen + 1, &segmentLen));
205         std::string temp = muri->AddSegment(segment);
206         if (temp.empty()) {
207             napi_get_null(env, &result);
208             return result;
209         }
210         size_t tempLen = temp.size();
211         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), tempLen, &result));
212         return result;
213     }
214 
GetLastSegment(napi_env env,napi_callback_info info)215     static napi_value GetLastSegment(napi_env env, napi_callback_info info)
216     {
217         napi_value thisVar = nullptr;
218         napi_value result = nullptr;
219         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
220         Uri *muri = nullptr;
221         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
222         std::string temp = muri->GetLastSegment();
223         if (temp.empty()) {
224             napi_get_null(env, &result);
225             return result;
226         }
227         size_t templen = temp.size();
228         if (napi_create_string_utf8(env, temp.c_str(), templen, &result) != napi_ok) {
229             HILOG_ERROR("GetLastSegment:: can not get temp value");
230             return nullptr;
231         }
232         return result;
233     }
234 
GetScheme(napi_env env,napi_callback_info info)235     static napi_value GetScheme(napi_env env, napi_callback_info info)
236     {
237         napi_value thisVar = nullptr;
238         napi_value result = nullptr;
239         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
240         Uri *muri = nullptr;
241         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
242         std::string temp = muri->GetScheme();
243         if (temp.empty()) {
244             napi_get_null(env, &result);
245             return result;
246         }
247         size_t templen = temp.size();
248         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
249         return result;
250     }
251 
GetAuthority(napi_env env,napi_callback_info info)252     static napi_value GetAuthority(napi_env env, napi_callback_info info)
253     {
254         napi_value thisVar = nullptr;
255         napi_value result = nullptr;
256         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
257         Uri *muri = nullptr;
258         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
259         std::string temp = muri->GetAuthority();
260         if (temp.empty()) {
261             napi_get_null(env, &result);
262             return result;
263         }
264         size_t templen = temp.size();
265         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
266         return result;
267     }
268 
GetSsp(napi_env env,napi_callback_info info)269     static napi_value GetSsp(napi_env env, napi_callback_info info)
270     {
271         napi_value thisVar = nullptr;
272         napi_value result = nullptr;
273         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
274         Uri *muri = nullptr;
275         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
276         std::string temp = muri->GetSsp();
277         size_t templen = temp.size();
278         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
279         return result;
280     }
281 
GetUserinfo(napi_env env,napi_callback_info info)282     static napi_value GetUserinfo(napi_env env, napi_callback_info info)
283     {
284         napi_value thisVar = nullptr;
285         napi_value result = nullptr;
286         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
287         Uri *muri = nullptr;
288         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
289         std::string temp = muri->GetUserinfo();
290         if (temp.empty()) {
291             napi_get_null(env, &result);
292             return result;
293         }
294         size_t templen = temp.size();
295         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
296         return result;
297     }
298 
GetHost(napi_env env,napi_callback_info info)299     static napi_value GetHost(napi_env env, napi_callback_info info)
300     {
301         napi_value thisVar = nullptr;
302         napi_value result = nullptr;
303         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
304         Uri *muri = nullptr;
305         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
306         std::string temp = muri->GetHost();
307         if (temp.empty()) {
308             napi_get_null(env, &result);
309             return result;
310         }
311         size_t templen = temp.size();
312         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
313         return result;
314     }
315 
GetPort(napi_env env,napi_callback_info info)316     static napi_value GetPort(napi_env env, napi_callback_info info)
317     {
318         napi_value thisVar = nullptr;
319         napi_value result = nullptr;
320         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
321         Uri *muri = nullptr;
322         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
323         std::string temp = muri->GetPort();
324         size_t templen = temp.size();
325         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
326         return result;
327     }
328 
GetPath(napi_env env,napi_callback_info info)329     static napi_value GetPath(napi_env env, napi_callback_info info)
330     {
331         napi_value thisVar = nullptr;
332         napi_value result = nullptr;
333         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
334         Uri *muri = nullptr;
335         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
336         std::string temp = muri->GetPath();
337         if (temp.empty()) {
338             napi_get_null(env, &result);
339             return result;
340         }
341         size_t templen = temp.size();
342         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
343         return result;
344     }
345 
GetQuery(napi_env env,napi_callback_info info)346     static napi_value GetQuery(napi_env env, napi_callback_info info)
347     {
348         napi_value thisVar = nullptr;
349         napi_value result = nullptr;
350         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
351         Uri *muri = nullptr;
352         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
353         std::string temp = muri->GetQuery();
354         if (temp.empty()) {
355             napi_get_null(env, &result);
356             return result;
357         }
358         size_t templen = temp.size();
359         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
360         return result;
361     }
362 
GetFragment(napi_env env,napi_callback_info info)363     static napi_value GetFragment(napi_env env, napi_callback_info info)
364     {
365         napi_value thisVar = nullptr;
366         napi_value result = nullptr;
367         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
368         Uri *muri = nullptr;
369         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
370         std::string temp = muri->GetFragment();
371         if (temp.empty()) {
372             napi_get_null(env, &result);
373             return result;
374         }
375         size_t templen = temp.size();
376         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
377         return result;
378     }
379 
ClearQuery(napi_env env,napi_callback_info info)380     static napi_value ClearQuery(napi_env env, napi_callback_info info)
381     {
382         napi_value thisVar = nullptr;
383         napi_value result = nullptr;
384         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
385         Uri *muri = nullptr;
386         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
387         std::string temp = muri->ClearQuery();
388         if (temp.empty()) {
389             napi_get_null(env, &result);
390             return result;
391         }
392         size_t tempLen = temp.size();
393         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), tempLen, &result));
394         return result;
395     }
396 
UriInit(napi_env env,napi_value exports)397     napi_value UriInit(napi_env env, napi_value exports)
398     {
399         const char *uriClassName = "uri";
400         napi_value uriClass = nullptr;
401         napi_property_descriptor uriDesc[] = {
402             DECLARE_NAPI_FUNCTION("normalize", Normalize),
403             DECLARE_NAPI_FUNCTION("equals", Equals),
404             DECLARE_NAPI_FUNCTION("checkIsAbsolute", IsAbsolute),
405             DECLARE_NAPI_FUNCTION("toString", UriToString),
406             DECLARE_NAPI_FUNCTION("checkIsRelative", IsRelative),
407             DECLARE_NAPI_FUNCTION("checkIsOpaque", IsOpaque),
408             DECLARE_NAPI_FUNCTION("checkIsHierarchical", IsHierarchical),
409             DECLARE_NAPI_FUNCTION("addQueryValue", AddQueryValue),
410             DECLARE_NAPI_FUNCTION("getLastSegment", GetLastSegment),
411             DECLARE_NAPI_FUNCTION("addSegment", AddSegment),
412             DECLARE_NAPI_FUNCTION("clearQuery", ClearQuery),
413             DECLARE_NAPI_GETTER("scheme", GetScheme),
414             DECLARE_NAPI_GETTER("authority", GetAuthority),
415             DECLARE_NAPI_GETTER("ssp", GetSsp),
416             DECLARE_NAPI_GETTER("userInfo", GetUserinfo),
417             DECLARE_NAPI_GETTER("host", GetHost),
418             DECLARE_NAPI_GETTER("port", GetPort),
419             DECLARE_NAPI_GETTER("path", GetPath),
420             DECLARE_NAPI_GETTER("query", GetQuery),
421             DECLARE_NAPI_GETTER("fragment", GetFragment),
422             DECLARE_NAPI_GETTER("isFailed", IsFailed),
423         };
424         NAPI_CALL(env, napi_define_class(env, uriClassName, strlen(uriClassName), UriConstructor,
425                                          nullptr, sizeof(uriDesc) / sizeof(uriDesc[0]), uriDesc, &uriClass));
426         napi_property_descriptor desc[] = {
427             DECLARE_NAPI_PROPERTY("Uri", uriClass)
428         };
429         napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
430         return exports;
431     }
432 
433     extern "C"
NAPI_uri_GetJSCode(const char ** buf,int * bufLen)434     __attribute__((visibility("default"))) void NAPI_uri_GetJSCode(const char **buf, int *bufLen)
435     {
436         if (buf != nullptr) {
437             *buf = _binary_js_uri_js_start;
438         }
439         if (bufLen != nullptr) {
440             *bufLen = _binary_js_uri_js_end - _binary_js_uri_js_start;
441         }
442     }
443     extern "C"
NAPI_uri_GetABCCode(const char ** buf,int * buflen)444     __attribute__((visibility("default"))) void NAPI_uri_GetABCCode(const char** buf, int* buflen)
445     {
446         if (buf != nullptr) {
447             *buf = _binary_uri_abc_start;
448         }
449         if (buflen != nullptr) {
450             *buflen = _binary_uri_abc_end - _binary_uri_abc_start;
451         }
452     }
453 
454     static napi_module_with_js UriModule = {
455         .nm_version = 1,
456         .nm_flags = 0,
457         .nm_filename = nullptr,
458         .nm_register_func = UriInit,
459         .nm_modname = "uri",
460         .nm_priv = reinterpret_cast<void*>(0),
461         .nm_get_abc_code = NAPI_uri_GetABCCode,
462         .nm_get_js_code = NAPI_uri_GetJSCode,
463     };
UriRegisterModule()464     extern "C" __attribute__((constructor)) void UriRegisterModule()
465     {
466         napi_module_with_js_register(&UriModule);
467     }
468 } // namespace OHOS::Uri
469