• 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.reserve(typelen + 1);
39             type.resize(typelen);
40             NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type.data(), typelen + 1, &typelen));
41             object = new (std::nothrow) Uri(type);
42             if (object == nullptr) {
43                 HILOG_ERROR("UriConstructor:: memory allocation failed, object is nullptr");
44                 return nullptr;
45             }
46         }
47         NAPI_CALL(env, napi_wrap(env, thisVar, object,
48             [](napi_env environment, void *data, void *hint) {
49             auto obj = reinterpret_cast<Uri*>(data);
50             if (obj != nullptr) {
51                 delete obj;
52                 obj = nullptr;
53             }
54         }, nullptr, nullptr));
55         return thisVar;
56     }
57 
Normalize(napi_env env,napi_callback_info info)58     static napi_value Normalize(napi_env env, napi_callback_info info)
59     {
60         napi_value thisVar = nullptr;
61         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
62         Uri *muri = nullptr;
63         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
64         std::string normalizeUri = muri->Normalize();
65         napi_value result = nullptr;
66         size_t tempLen = normalizeUri.size();
67         NAPI_CALL(env, napi_create_string_utf8(env, normalizeUri.c_str(), tempLen, &result));
68         return result;
69     }
70 
Equals(napi_env env,napi_callback_info info)71     static napi_value Equals(napi_env env, napi_callback_info info)
72     {
73         napi_value thisVar = nullptr;
74         napi_value result = nullptr;
75         size_t argc = 1;
76         napi_value argv[1] = { 0 };
77         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
78 
79         Uri *muri = nullptr;
80         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
81         Uri *other = nullptr;
82         NAPI_CALL(env, napi_unwrap(env, argv[0], reinterpret_cast<void**>(&other)));
83 
84         bool flag = muri->Equals(*other);
85         NAPI_CALL(env, napi_get_boolean(env, flag, &result));
86         return result;
87     }
88 
IsAbsolute(napi_env env,napi_callback_info info)89     static napi_value IsAbsolute(napi_env env, napi_callback_info info)
90     {
91         napi_value thisVar = nullptr;
92         napi_value result = nullptr;
93         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
94         Uri *muri = nullptr;
95         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
96         bool flag = muri->IsAbsolute();
97         NAPI_CALL(env, napi_get_boolean(env, flag, &result));
98         return result;
99     }
100 
IsFailed(napi_env env,napi_callback_info info)101     static napi_value IsFailed(napi_env env, napi_callback_info info)
102     {
103         napi_value thisVar = nullptr;
104         napi_value result = nullptr;
105         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
106         Uri *muri = nullptr;
107         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
108         std::string temp = muri->IsFailed();
109         size_t templen = temp.size();
110         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
111         return result;
112     }
113 
UriToString(napi_env env,napi_callback_info info)114     static napi_value UriToString(napi_env env, napi_callback_info info)
115     {
116         napi_value thisVar = nullptr;
117         napi_value result = nullptr;
118         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
119         Uri *muri = nullptr;
120         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
121         std::string temp = muri->ToString();
122         size_t templen = temp.size();
123         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
124         return result;
125     }
126 
IsRelative(napi_env env,napi_callback_info info)127     static napi_value IsRelative(napi_env env, napi_callback_info info)
128     {
129         napi_value thisVar = nullptr;
130         napi_value result = nullptr;
131         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
132         Uri *muri = nullptr;
133         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
134         bool flag = muri->IsRelative();
135         NAPI_CALL(env, napi_get_boolean(env, flag, &result));
136         return result;
137     }
138 
IsOpaque(napi_env env,napi_callback_info info)139     static napi_value IsOpaque(napi_env env, napi_callback_info info)
140     {
141         napi_value thisVar = nullptr;
142         napi_value result = nullptr;
143         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
144         Uri *muri = nullptr;
145         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
146         bool flag = muri->IsOpaque();
147         NAPI_CALL(env, napi_get_boolean(env, flag, &result));
148         return result;
149     }
150 
IsHierarchical(napi_env env,napi_callback_info info)151     static napi_value IsHierarchical(napi_env env, napi_callback_info info)
152     {
153         napi_value thisVar = nullptr;
154         napi_value result = nullptr;
155         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
156         Uri *muri = nullptr;
157         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
158         bool flag = muri->IsHierarchical();
159         NAPI_CALL(env, napi_get_boolean(env, flag, &result));
160         return result;
161     }
162 
AddQueryValue(napi_env env,napi_callback_info info)163     static napi_value AddQueryValue(napi_env env, napi_callback_info info)
164     {
165         napi_value thisVar = nullptr;
166         napi_value result = nullptr;
167         size_t argc = 2;
168         napi_value argv[2] = { nullptr };
169         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
170         Uri *muri = nullptr;
171         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
172         std::string key = "";
173         size_t keyLen = 0;
174         NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &keyLen));
175         key.reserve(keyLen + 1);
176         key.resize(keyLen);
177         NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], key.data(), keyLen + 1, &keyLen));
178         std::string value = "";
179         size_t valueLen = 0;
180         NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], nullptr, 0, &valueLen));
181         value.reserve(valueLen + 1);
182         value.resize(valueLen);
183         NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], value.data(), valueLen + 1, &valueLen));
184         std::string temp = muri->AddQueryValue(key, value);
185         if (temp.empty()) {
186             napi_get_null(env, &result);
187             return result;
188         }
189         size_t templen = temp.size();
190         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
191         return result;
192     }
193 
AddSegment(napi_env env,napi_callback_info info)194     static napi_value AddSegment(napi_env env, napi_callback_info info)
195     {
196         napi_value thisVar = nullptr;
197         napi_value result = nullptr;
198         size_t argc = 1;
199         napi_value argv[1] = { nullptr };
200         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
201         Uri *muri = nullptr;
202         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
203         std::string segment = "";
204         size_t segmentLen = 0;
205         NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &segmentLen));
206         segment.reserve(segmentLen + 1);
207         segment.resize(segmentLen);
208         NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], segment.data(), segmentLen + 1, &segmentLen));
209         std::string temp = muri->AddSegment(segment);
210         if (temp.empty()) {
211             napi_get_null(env, &result);
212             return result;
213         }
214         size_t tempLen = temp.size();
215         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), tempLen, &result));
216         return result;
217     }
218 
SetScheme(napi_env env,napi_callback_info info)219     static napi_value SetScheme(napi_env env, napi_callback_info info)
220     {
221         napi_value thisVar = nullptr;
222         napi_value result = nullptr;
223         size_t argc = 1;
224         napi_value argv[1] = { nullptr };
225         if (napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr) != napi_ok) {
226             HILOG_ERROR("URI:: can not get thisVar");
227             return nullptr;
228         }
229         Uri *muri = nullptr;
230         std::string scheme = "";
231         size_t schemeLen = 0;
232         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &schemeLen) != napi_ok) {
233             HILOG_ERROR("URI:: can not get scheme size");
234             return nullptr;
235         }
236         scheme.reserve(schemeLen + 1);
237         scheme.resize(schemeLen);
238         if (napi_get_value_string_utf8(env, argv[0], scheme.data(), schemeLen + 1, &schemeLen) != napi_ok) {
239             HILOG_ERROR("URI:: can not get scheme value");
240             return nullptr;
241         }
242         if (napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)) != napi_ok) {
243             HILOG_ERROR("URI:: can not get uri");
244             return nullptr;
245         }
246         if (muri != nullptr) {
247             muri->SetScheme(scheme);
248         }
249         return result;
250     }
251 
SetUserInfo(napi_env env,napi_callback_info info)252     static napi_value SetUserInfo(napi_env env, napi_callback_info info)
253     {
254         napi_value thisVar = nullptr;
255         napi_value result = nullptr;
256         size_t argc = 1;
257         napi_value argv[1] = { nullptr };
258         if (napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr) != napi_ok) {
259             HILOG_ERROR("URI:: can not get thisVar");
260             return nullptr;
261         }
262         Uri *muri = nullptr;
263         std::string userInfo = "";
264         size_t userInfoLen = 0;
265         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &userInfoLen) != napi_ok) {
266             HILOG_ERROR("URI:: can not get userInfo size");
267             return nullptr;
268         }
269         userInfo.reserve(userInfoLen + 1);
270         userInfo.resize(userInfoLen);
271         if (napi_get_value_string_utf8(env, argv[0], userInfo.data(), userInfoLen + 1, &userInfoLen) != napi_ok) {
272             HILOG_ERROR("URI:: can not get userInfo value");
273             return nullptr;
274         }
275         if (napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)) != napi_ok) {
276             HILOG_ERROR("URI:: can not get uri");
277             return nullptr;
278         }
279         if (muri != nullptr) {
280             muri->SetUserInfo(userInfo);
281         }
282         return result;
283     }
284 
SetPath(napi_env env,napi_callback_info info)285     static napi_value SetPath(napi_env env, napi_callback_info info)
286     {
287         napi_value thisVar = nullptr;
288         napi_value result = nullptr;
289         size_t argc = 1;
290         napi_value argv[1] = { nullptr };
291         if (napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr) != napi_ok) {
292             HILOG_ERROR("URI:: can not get thisVar");
293             return nullptr;
294         }
295         Uri *muri = nullptr;
296         std::string pathStr = "";
297         size_t pathLen = 0;
298         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &pathLen) != napi_ok) {
299             HILOG_ERROR("URI:: can not get pathStr size");
300             return nullptr;
301         }
302         pathStr.reserve(pathLen + 1);
303         pathStr.resize(pathLen);
304         if (napi_get_value_string_utf8(env, argv[0], pathStr.data(), pathLen + 1, &pathLen) != napi_ok) {
305             HILOG_ERROR("URI:: can not get pathStr value");
306             return nullptr;
307         }
308         if (napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)) != napi_ok) {
309             HILOG_ERROR("URI:: can not get uri");
310             return nullptr;
311         }
312         if (muri != nullptr) {
313             muri->SetPath(pathStr);
314         }
315         return result;
316     }
317 
SetFragment(napi_env env,napi_callback_info info)318     static napi_value SetFragment(napi_env env, napi_callback_info info)
319     {
320         napi_value thisVar = nullptr;
321         napi_value result = nullptr;
322         size_t argc = 1;
323         napi_value argv[1] = { nullptr };
324         if (napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr) != napi_ok) {
325             HILOG_ERROR("URI:: can not get thisVar");
326             return nullptr;
327         }
328         Uri *muri = nullptr;
329         std::string fragmentStr = "";
330         size_t fragmentLen = 0;
331         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &fragmentLen) != napi_ok) {
332             HILOG_ERROR("URI:: can not get fragmentStr size");
333             return nullptr;
334         }
335         fragmentStr.reserve(fragmentLen + 1);
336         fragmentStr.resize(fragmentLen);
337         if (napi_get_value_string_utf8(env, argv[0], fragmentStr.data(), fragmentLen + 1, &fragmentLen) != napi_ok) {
338             HILOG_ERROR("URI:: can not get fragmentStr value");
339             return nullptr;
340         }
341         if (napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)) != napi_ok) {
342             HILOG_ERROR("URI:: can not get uri");
343             return nullptr;
344         }
345         if (muri != nullptr) {
346             muri->SetFragment(fragmentStr);
347         }
348         return result;
349     }
350 
SetQuery(napi_env env,napi_callback_info info)351     static napi_value SetQuery(napi_env env, napi_callback_info info)
352     {
353         napi_value thisVar = nullptr;
354         napi_value result = nullptr;
355         size_t argc = 1;
356         napi_value argv[1] = { nullptr };
357         if (napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr) != napi_ok) {
358             HILOG_ERROR("URI:: can not get thisVar");
359             return nullptr;
360         }
361         Uri *muri = nullptr;
362         std::string queryStr = "";
363         size_t queryLen = 0;
364         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &queryLen) != napi_ok) {
365             HILOG_ERROR("URI:: can not get queryStr size");
366             return nullptr;
367         }
368         queryStr.reserve(queryLen + 1);
369         queryStr.resize(queryLen);
370         if (napi_get_value_string_utf8(env, argv[0], queryStr.data(), queryLen + 1, &queryLen) != napi_ok) {
371             HILOG_ERROR("URI:: can not get queryStr value");
372             return nullptr;
373         }
374         if (napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)) != napi_ok) {
375             HILOG_ERROR("URI:: can not get uri");
376             return nullptr;
377         }
378         if (muri != nullptr) {
379             muri->SetQuery(queryStr);
380         }
381         return result;
382     }
383 
SetAuthority(napi_env env,napi_callback_info info)384     static napi_value SetAuthority(napi_env env, napi_callback_info info)
385     {
386         napi_value thisVar = nullptr;
387         napi_value result = nullptr;
388         size_t argc = 1;
389         napi_value argv[1] = { nullptr };
390         if (napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr) != napi_ok) {
391             HILOG_ERROR("URI:: can not get thisVar");
392             return nullptr;
393         }
394         Uri *muri = nullptr;
395         std::string authorityStr = "";
396         size_t authorityStrLen = 0;
397         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &authorityStrLen) != napi_ok) {
398             HILOG_ERROR("URI:: can not get authorityStr size");
399             return nullptr;
400         }
401         authorityStr.reserve(authorityStrLen + 1);
402         authorityStr.resize(authorityStrLen);
403         if (napi_get_value_string_utf8(env, argv[0], authorityStr.data(),
404             authorityStrLen + 1, &authorityStrLen) != napi_ok) {
405             HILOG_ERROR("URI:: can not get authorityStr value");
406             return nullptr;
407         }
408         if (napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)) != napi_ok) {
409             HILOG_ERROR("URI:: can not get uri");
410             return nullptr;
411         }
412         if (muri != nullptr) {
413             muri->SetAuthority(authorityStr);
414         }
415         return result;
416     }
417 
SetSsp(napi_env env,napi_callback_info info)418     static napi_value SetSsp(napi_env env, napi_callback_info info)
419     {
420         napi_value thisVar = nullptr;
421         napi_value result = nullptr;
422         size_t argc = 1;
423         napi_value argv[1] = { nullptr };
424         if (napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr) != napi_ok) {
425             HILOG_ERROR("URI:: can not get thisVar");
426             return nullptr;
427         }
428         Uri *muri = nullptr;
429         std::string sspStr = "";
430         size_t sspStrLen = 0;
431         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &sspStrLen) != napi_ok) {
432             HILOG_ERROR("URI:: can not get authorityStr size");
433             return nullptr;
434         }
435         sspStr.reserve(sspStrLen + 1);
436         sspStr.resize(sspStrLen);
437         if (napi_get_value_string_utf8(env, argv[0], sspStr.data(), sspStrLen + 1, &sspStrLen) != napi_ok) {
438             HILOG_ERROR("URI:: can not get authorityStr value");
439             return nullptr;
440         }
441         if (napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)) != napi_ok) {
442             HILOG_ERROR("URI:: can not get uri");
443             return nullptr;
444         }
445         if (muri != nullptr) {
446             muri->SetSsp(sspStr);
447         }
448         return result;
449     }
450 
GetLastSegment(napi_env env,napi_callback_info info)451     static napi_value GetLastSegment(napi_env env, napi_callback_info info)
452     {
453         napi_value thisVar = nullptr;
454         napi_value result = nullptr;
455         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
456         Uri *muri = nullptr;
457         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
458         std::string temp = muri->GetLastSegment();
459         if (temp.empty()) {
460             napi_get_null(env, &result);
461             return result;
462         }
463         size_t templen = temp.size();
464         if (napi_create_string_utf8(env, temp.c_str(), templen, &result) != napi_ok) {
465             HILOG_ERROR("GetLastSegment:: can not get temp value");
466             return nullptr;
467         }
468         return result;
469     }
470 
GetScheme(napi_env env,napi_callback_info info)471     static napi_value GetScheme(napi_env env, napi_callback_info info)
472     {
473         napi_value thisVar = nullptr;
474         napi_value result = nullptr;
475         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
476         Uri *muri = nullptr;
477         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
478         std::string temp = muri->GetScheme();
479         if (temp.empty()) {
480             napi_get_null(env, &result);
481             return result;
482         }
483         size_t templen = temp.size();
484         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
485         return result;
486     }
487 
GetAuthority(napi_env env,napi_callback_info info)488     static napi_value GetAuthority(napi_env env, napi_callback_info info)
489     {
490         napi_value thisVar = nullptr;
491         napi_value result = nullptr;
492         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
493         Uri *muri = nullptr;
494         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
495         std::string temp = muri->GetAuthority();
496         if (temp.empty()) {
497             napi_get_null(env, &result);
498             return result;
499         }
500         size_t templen = temp.size();
501         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
502         return result;
503     }
504 
GetSsp(napi_env env,napi_callback_info info)505     static napi_value GetSsp(napi_env env, napi_callback_info info)
506     {
507         napi_value thisVar = nullptr;
508         napi_value result = nullptr;
509         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
510         Uri *muri = nullptr;
511         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
512         std::string temp = muri->GetSsp();
513         size_t templen = temp.size();
514         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
515         return result;
516     }
517 
GetUserinfo(napi_env env,napi_callback_info info)518     static napi_value GetUserinfo(napi_env env, napi_callback_info info)
519     {
520         napi_value thisVar = nullptr;
521         napi_value result = nullptr;
522         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
523         Uri *muri = nullptr;
524         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
525         std::string temp = muri->GetUserinfo();
526         if (temp.empty()) {
527             napi_get_null(env, &result);
528             return result;
529         }
530         size_t templen = temp.size();
531         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
532         return result;
533     }
534 
GetHost(napi_env env,napi_callback_info info)535     static napi_value GetHost(napi_env env, napi_callback_info info)
536     {
537         napi_value thisVar = nullptr;
538         napi_value result = nullptr;
539         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
540         Uri *muri = nullptr;
541         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
542         std::string temp = muri->GetHost();
543         if (temp.empty()) {
544             napi_get_null(env, &result);
545             return result;
546         }
547         size_t templen = temp.size();
548         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
549         return result;
550     }
551 
GetPort(napi_env env,napi_callback_info info)552     static napi_value GetPort(napi_env env, napi_callback_info info)
553     {
554         napi_value thisVar = nullptr;
555         napi_value result = nullptr;
556         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
557         Uri *muri = nullptr;
558         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
559         std::string temp = muri->GetPort();
560         size_t templen = temp.size();
561         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
562         return result;
563     }
564 
GetPath(napi_env env,napi_callback_info info)565     static napi_value GetPath(napi_env env, napi_callback_info info)
566     {
567         napi_value thisVar = nullptr;
568         napi_value result = nullptr;
569         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
570         Uri *muri = nullptr;
571         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
572         std::string temp = muri->GetPath();
573         if (temp.empty()) {
574             napi_get_null(env, &result);
575             return result;
576         }
577         size_t templen = temp.size();
578         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
579         return result;
580     }
581 
GetQuery(napi_env env,napi_callback_info info)582     static napi_value GetQuery(napi_env env, napi_callback_info info)
583     {
584         napi_value thisVar = nullptr;
585         napi_value result = nullptr;
586         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
587         Uri *muri = nullptr;
588         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
589         std::string temp = muri->GetQuery();
590         if (temp.empty()) {
591             napi_get_null(env, &result);
592             return result;
593         }
594         size_t templen = temp.size();
595         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
596         return result;
597     }
598 
GetFragment(napi_env env,napi_callback_info info)599     static napi_value GetFragment(napi_env env, napi_callback_info info)
600     {
601         napi_value thisVar = nullptr;
602         napi_value result = nullptr;
603         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
604         Uri *muri = nullptr;
605         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
606         std::string temp = muri->GetFragment();
607         if (temp.empty()) {
608             napi_get_null(env, &result);
609             return result;
610         }
611         size_t templen = temp.size();
612         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
613         return result;
614     }
615 
ClearQuery(napi_env env,napi_callback_info info)616     static napi_value ClearQuery(napi_env env, napi_callback_info info)
617     {
618         napi_value thisVar = nullptr;
619         napi_value result = nullptr;
620         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
621         Uri *muri = nullptr;
622         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&muri)));
623         std::string temp = muri->ClearQuery();
624         if (temp.empty()) {
625             napi_get_null(env, &result);
626             return result;
627         }
628         size_t tempLen = temp.size();
629         NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), tempLen, &result));
630         return result;
631     }
632 
UriInit(napi_env env,napi_value exports)633     napi_value UriInit(napi_env env, napi_value exports)
634     {
635         const char *uriClassName = "uri";
636         napi_value uriClass = nullptr;
637         napi_property_descriptor uriDesc[] = {
638             DECLARE_NAPI_FUNCTION("normalize", Normalize),
639             DECLARE_NAPI_FUNCTION("equals", Equals),
640             DECLARE_NAPI_FUNCTION("checkIsAbsolute", IsAbsolute),
641             DECLARE_NAPI_FUNCTION("toString", UriToString),
642             DECLARE_NAPI_FUNCTION("checkIsRelative", IsRelative),
643             DECLARE_NAPI_FUNCTION("checkIsOpaque", IsOpaque),
644             DECLARE_NAPI_FUNCTION("checkIsHierarchical", IsHierarchical),
645             DECLARE_NAPI_FUNCTION("addQueryValue", AddQueryValue),
646             DECLARE_NAPI_FUNCTION("getLastSegment", GetLastSegment),
647             DECLARE_NAPI_FUNCTION("addSegment", AddSegment),
648             DECLARE_NAPI_FUNCTION("clearQuery", ClearQuery),
649             DECLARE_NAPI_GETTER_SETTER("scheme", GetScheme, SetScheme),
650             DECLARE_NAPI_GETTER_SETTER("authority", GetAuthority, SetAuthority),
651             DECLARE_NAPI_GETTER_SETTER("ssp", GetSsp, SetSsp),
652             DECLARE_NAPI_GETTER_SETTER("userInfo", GetUserinfo, SetUserInfo),
653             DECLARE_NAPI_GETTER("host", GetHost),
654             DECLARE_NAPI_GETTER("port", GetPort),
655             DECLARE_NAPI_GETTER_SETTER("path", GetPath, SetPath),
656             DECLARE_NAPI_GETTER_SETTER("query", GetQuery, SetQuery),
657             DECLARE_NAPI_GETTER_SETTER("fragment", GetFragment, SetFragment),
658             DECLARE_NAPI_GETTER("isFailed", IsFailed),
659         };
660         NAPI_CALL(env, napi_define_class(env, uriClassName, strlen(uriClassName), UriConstructor,
661                                          nullptr, sizeof(uriDesc) / sizeof(uriDesc[0]), uriDesc, &uriClass));
662         napi_property_descriptor desc[] = {
663             DECLARE_NAPI_PROPERTY("Uri", uriClass)
664         };
665         napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
666         return exports;
667     }
668 
669     extern "C"
NAPI_uri_GetJSCode(const char ** buf,int * bufLen)670     __attribute__((visibility("default"))) void NAPI_uri_GetJSCode(const char **buf, int *bufLen)
671     {
672         if (buf != nullptr) {
673             *buf = _binary_js_uri_js_start;
674         }
675         if (bufLen != nullptr) {
676             *bufLen = _binary_js_uri_js_end - _binary_js_uri_js_start;
677         }
678     }
679     extern "C"
NAPI_uri_GetABCCode(const char ** buf,int * buflen)680     __attribute__((visibility("default"))) void NAPI_uri_GetABCCode(const char** buf, int* buflen)
681     {
682         if (buf != nullptr) {
683             *buf = _binary_uri_abc_start;
684         }
685         if (buflen != nullptr) {
686             *buflen = _binary_uri_abc_end - _binary_uri_abc_start;
687         }
688     }
689 
690     static napi_module_with_js UriModule = {
691         .nm_version = 1,
692         .nm_flags = 0,
693         .nm_filename = nullptr,
694         .nm_register_func = UriInit,
695         .nm_modname = "uri",
696         .nm_priv = reinterpret_cast<void*>(0),
697         .nm_get_abc_code = NAPI_uri_GetABCCode,
698         .nm_get_js_code = NAPI_uri_GetJSCode,
699     };
UriRegisterModule()700     extern "C" __attribute__((constructor)) void UriRegisterModule()
701     {
702         napi_module_with_js_register(&UriModule);
703     }
704 } // namespace OHOS::Uri
705