From 1b2c5d547348ecd920678f2d489fb8a1453a43fd Mon Sep 17 00:00:00 2001 From: jiawen Date: Mon, 5 Sep 2022 09:57:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86napi=5Futils=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E6=88=90=E5=8A=A8=E6=80=81=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jiawen Change-Id: Iadd5aaf33486124b4d807a4d01603e470db4e48a --- .gitignore | 3 +- bundle.json | 9 +- frameworks/js/napi/BUILD.gn | 235 --------- frameworks/js/napi/fetch/BUILD.gn | 52 ++ .../async_context/include/fetch_context.h | 2 +- .../fetch/async_context/src/fetch_context.cpp | 4 +- .../fetch/async_work/src/fetch_async_work.cpp | 4 +- .../napi/fetch/fetch_exec/src/fetch_exec.cpp | 4 +- .../fetch/fetch_module/include/fetch_module.h | 2 +- .../fetch/fetch_module/src/fetch_module.cpp | 2 +- frameworks/js/napi/http/BUILD.gn | 74 +++ .../async_context/include/request_context.h | 2 +- .../async_context/src/request_context.cpp | 4 +- .../http/async_work/src/http_async_work.cpp | 6 +- .../js/napi/http/http_exec/src/http_exec.cpp | 2 +- .../napi/http/http_module/src/http_module.cpp | 2 +- frameworks/js/napi/socket/BUILD.gn | 61 +++ .../async_context/include/bind_context.h | 2 +- .../async_context/include/common_context.h | 2 +- .../async_context/include/connect_context.h | 2 +- .../async_context/include/tcp_extra_context.h | 2 +- .../async_context/include/tcp_send_context.h | 2 +- .../async_context/include/udp_extra_context.h | 2 +- .../async_context/include/udp_send_context.h | 2 +- .../socket/async_context/src/bind_context.cpp | 4 +- .../async_context/src/common_context.cpp | 6 +- .../async_context/src/connect_context.cpp | 6 +- .../async_context/src/tcp_extra_context.cpp | 6 +- .../async_context/src/tcp_send_context.cpp | 6 +- .../async_context/src/udp_extra_context.cpp | 6 +- .../async_context/src/udp_send_context.cpp | 4 +- .../async_work/src/socket_async_work.cpp | 4 +- .../options/include/socket_remote_info.h | 1 + .../socket/socket_exec/src/socket_exec.cpp | 2 +- .../socket_module/src/socket_module.cpp | 8 +- .../socket/task_queue/include/task_queue.h | 2 +- frameworks/js/napi/websocket/BUILD.gn | 54 ++ .../async_context/include/close_context.h | 2 +- .../async_context/include/connect_context.h | 2 +- .../async_context/include/send_context.h | 2 +- .../async_context/src/close_context.cpp | 4 +- .../async_context/src/connect_context.cpp | 2 +- .../async_context/src/send_context.cpp | 4 +- .../async_work/src/websocket_async_work.cpp | 2 +- .../websocket_exec/src/websocket_exec.cpp | 2 +- .../websocket_module/src/websocket_module.cpp | 4 +- .../innerkits/tls_socket/include/tls_socket.h | 473 ++++++++++++++++++ netstack_config.gni | 17 + .../fuzztest/socketexec_fuzzer/BUILD.gn | 34 +- utils/BUILD.gn | 39 ++ utils/log/include/netstack_log.h | 2 + utils/napi_utils/BUILD.gn | 44 ++ .../include/base_async_work.h} | 4 +- .../include/base_context.h} | 2 +- .../include/event_listener.h} | 0 .../include/event_manager.h} | 2 +- .../include/module_template.h} | 4 +- .../{netstack_napi_utils.h => napi_utils.h} | 7 + .../src/base_context.cpp} | 8 +- .../src/event_listener.cpp} | 4 +- .../src/event_manager.cpp} | 4 +- .../src/module_template.cpp} | 6 +- ...netstack_napi_utils.cpp => napi_utils.cpp} | 44 +- 63 files changed, 956 insertions(+), 353 deletions(-) delete mode 100644 frameworks/js/napi/BUILD.gn create mode 100644 frameworks/js/napi/fetch/BUILD.gn create mode 100644 frameworks/js/napi/http/BUILD.gn create mode 100644 frameworks/js/napi/socket/BUILD.gn create mode 100644 frameworks/js/napi/websocket/BUILD.gn create mode 100644 interfaces/innerkits/tls_socket/include/tls_socket.h create mode 100644 netstack_config.gni create mode 100644 utils/BUILD.gn create mode 100644 utils/napi_utils/BUILD.gn rename utils/{base_async_work/include/netstack_base_async_work.h => napi_utils/include/base_async_work.h} (98%) rename utils/{base_context/include/netstack_base_context.h => napi_utils/include/base_context.h} (98%) rename utils/{event_manager/include/netstack_event_listener.h => napi_utils/include/event_listener.h} (100%) rename utils/{event_manager/include/netstack_event_manager.h => napi_utils/include/event_manager.h} (98%) rename utils/{module_template/include/netstack_module_template.h => napi_utils/include/module_template.h} (98%) rename utils/napi_utils/include/{netstack_napi_utils.h => napi_utils.h} (92%) rename utils/{base_context/src/netstack_base_context.cpp => napi_utils/src/base_context.cpp} (96%) rename utils/{event_manager/src/netstack_event_listener.cpp => napi_utils/src/event_listener.cpp} (98%) rename utils/{event_manager/src/netstack_event_manager.cpp => napi_utils/src/event_manager.cpp} (98%) rename utils/{module_template/src/netstack_module_template.cpp => napi_utils/src/module_template.cpp} (98%) rename utils/napi_utils/src/{netstack_napi_utils.cpp => napi_utils.cpp} (92%) diff --git a/.gitignore b/.gitignore index ef9c841ac..d1ee1b3c3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ cmake-build-debug *.patch @ohos.net.connection.d.ts -basic.d.ts \ No newline at end of file +basic.d.ts +.vscode diff --git a/bundle.json b/bundle.json index a7bb58493..ee927d7c5 100644 --- a/bundle.json +++ b/bundle.json @@ -43,10 +43,11 @@ "build": { "group_type": { "base_group": [ - "//foundation/communication/netstack/frameworks/js/napi:http", - "//foundation/communication/netstack/frameworks/js/napi:socket", - "//foundation/communication/netstack/frameworks/js/napi:websocket", - "//foundation/communication/netstack/frameworks/js/napi:fetch" + "//foundation/communication/netstack/frameworks/js/napi/http:http", + "//foundation/communication/netstack/frameworks/js/napi/socket:socket", + "//foundation/communication/netstack/frameworks/js/napi/websocket:websocket", + "//foundation/communication/netstack/frameworks/js/napi/fetch:fetch", + "//foundation/communication/netstack/utils:common_utils" ], "fwk_group": [ ], diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn deleted file mode 100644 index 068914295..000000000 --- a/frameworks/js/napi/BUILD.gn +++ /dev/null @@ -1,235 +0,0 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//build/ohos.gni") - -SUBSYSTEM_DIR = "//foundation/communication" -NETSTACK_NAPI_ROOT = "$SUBSYSTEM_DIR/netstack/frameworks/js/napi/" - -utils_source = [ - "$SUBSYSTEM_DIR/netstack/utils/base_context/src/netstack_base_context.cpp", - "$SUBSYSTEM_DIR/netstack/utils/common_utils/src/netstack_common_utils.cpp", - "$SUBSYSTEM_DIR/netstack/utils/event_manager/src/netstack_event_listener.cpp", - "$SUBSYSTEM_DIR/netstack/utils/event_manager/src/netstack_event_manager.cpp", - "$SUBSYSTEM_DIR/netstack/utils/module_template/src/netstack_module_template.cpp", - "$SUBSYSTEM_DIR/netstack/utils/napi_utils/src/netstack_napi_utils.cpp", -] - -utils_include = [ - "$SUBSYSTEM_DIR/netstack/utils/base_async_work/include", - "$SUBSYSTEM_DIR/netstack/utils/base_context/include", - "$SUBSYSTEM_DIR/netstack/utils/common_utils/include", - "$SUBSYSTEM_DIR/netstack/utils/event_manager/include", - "$SUBSYSTEM_DIR/netstack/utils/log/include", - "$SUBSYSTEM_DIR/netstack/utils/module_template/include", - "$SUBSYSTEM_DIR/netstack/utils/napi_utils/include", -] - -common_include = [ - "//foundation/arkui/ace_engine/frameworks/base/utils", - "//foundation/arkui/napi/interfaces/kits", - "//foundation/arkui/napi", - "//third_party/node/src", -] - -common_deps = [ "//foundation/arkui/napi/:ace_napi" ] - -common_external_deps = [ - "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", -] - -common_relative_install_dir = "module/net" -common_part_name = "netstack" -common_subsystem_name = "communication" - -ohos_shared_library("http") { - include_dirs = [ - "$NETSTACK_NAPI_ROOT/http/async_context/include", - "$NETSTACK_NAPI_ROOT/http/async_work/include", - "$NETSTACK_NAPI_ROOT/http/cache/base64/include", - "$NETSTACK_NAPI_ROOT/http/cache/cache_constant/include", - "$NETSTACK_NAPI_ROOT/http/cache/cache_proxy/include", - "$NETSTACK_NAPI_ROOT/http/cache/cache_strategy/include", - "$NETSTACK_NAPI_ROOT/http/cache/lru_cache/include", - "$NETSTACK_NAPI_ROOT/http/cache/md5/include", - "$NETSTACK_NAPI_ROOT/http/constant/include", - "$NETSTACK_NAPI_ROOT/http/http_exec/include", - "$NETSTACK_NAPI_ROOT/http/http_module/include", - "$NETSTACK_NAPI_ROOT/http/options/include", - ] - include_dirs += utils_include - include_dirs += common_include - include_dirs += [ - "//third_party/curl/include", - "//third_party/glib/glib", - "//third_party/glib", - "//third_party/jsoncpp/include", - "//third_party/openssl/include", - ] - - sources = [ - "http/async_context/src/request_context.cpp", - "http/async_work/src/http_async_work.cpp", - "http/cache/base64/src/base64_utils.cpp", - "http/cache/cache_proxy/src/cache_proxy.cpp", - "http/cache/cache_strategy/src/http_cache_request.cpp", - "http/cache/cache_strategy/src/http_cache_response.cpp", - "http/cache/cache_strategy/src/http_cache_strategy.cpp", - "http/cache/cache_strategy/src/http_time.cpp", - "http/cache/lru_cache/src/disk_handler.cpp", - "http/cache/lru_cache/src/lru_cache.cpp", - "http/cache/lru_cache/src/lru_cache_disk_handler.cpp", - "http/cache/md5/src/calculate_md5.cpp", - "http/constant/src/constant.cpp", - "http/http_exec/src/http_exec.cpp", - "http/http_module/src/http_module.cpp", - "http/options/src/http_request_options.cpp", - "http/options/src/http_response.cpp", - ] - sources += utils_source - - deps = common_deps - deps += [ - "//third_party/curl:curl", - "//third_party/glib:glib_packages", - "//third_party/jsoncpp:jsoncpp", - "//third_party/openssl:libcrypto_shared", - ] - - external_deps = common_external_deps - - relative_install_dir = common_relative_install_dir - part_name = common_part_name - subsystem_name = common_subsystem_name -} - -ohos_shared_library("socket") { - include_dirs = [ - "$NETSTACK_NAPI_ROOT/socket/async_context/include", - "$NETSTACK_NAPI_ROOT/socket/async_work/include", - "$NETSTACK_NAPI_ROOT/socket/constant/include", - "$NETSTACK_NAPI_ROOT/socket/socket_exec/include", - "$NETSTACK_NAPI_ROOT/socket/socket_module/include", - "$NETSTACK_NAPI_ROOT/socket/options/include", - "$NETSTACK_NAPI_ROOT/socket/task_queue/include", - ] - include_dirs += utils_include - include_dirs += common_include - - sources = [ - "socket/async_context/src/bind_context.cpp", - "socket/async_context/src/common_context.cpp", - "socket/async_context/src/connect_context.cpp", - "socket/async_context/src/tcp_extra_context.cpp", - "socket/async_context/src/tcp_send_context.cpp", - "socket/async_context/src/udp_extra_context.cpp", - "socket/async_context/src/udp_send_context.cpp", - "socket/async_work/src/socket_async_work.cpp", - "socket/options/src/extra_options_base.cpp", - "socket/options/src/net_address.cpp", - "socket/options/src/socket_remote_info.cpp", - "socket/options/src/socket_state_base.cpp", - "socket/options/src/tcp_connect_options.cpp", - "socket/options/src/tcp_extra_options.cpp", - "socket/options/src/tcp_send_options.cpp", - "socket/options/src/udp_extra_options.cpp", - "socket/options/src/udp_send_options.cpp", - "socket/socket_exec/src/socket_exec.cpp", - "socket/socket_module/src/socket_module.cpp", - ] - sources += utils_source - - deps = common_deps - - external_deps = common_external_deps - - relative_install_dir = common_relative_install_dir - part_name = common_part_name - subsystem_name = common_subsystem_name -} - -ohos_shared_library("websocket") { - include_dirs = [ - "websocket/async_context/include", - "websocket/async_work/include", - "websocket/constant/include", - "websocket/websocket_exec/include", - "websocket/websocket_module/include", - ] - include_dirs += utils_include - include_dirs += common_include - include_dirs += [ - "//third_party/libwebsockets/include", - "//third_party/openssl/include", - ] - - sources = [ - "websocket/async_context/src/close_context.cpp", - "websocket/async_context/src/connect_context.cpp", - "websocket/async_context/src/send_context.cpp", - "websocket/async_work/src/websocket_async_work.cpp", - "websocket/constant/src/constant.cpp", - "websocket/websocket_exec/src/websocket_exec.cpp", - "websocket/websocket_module/src/websocket_module.cpp", - ] - sources += utils_source - - deps = common_deps - deps += [ - "//third_party/libwebsockets:websockets", - "//third_party/openssl:libcrypto_static", - "//third_party/openssl:ssl_source", - "//third_party/zlib:libz", - ] - - external_deps = common_external_deps - - relative_install_dir = common_relative_install_dir - part_name = common_part_name - subsystem_name = common_subsystem_name -} - -ohos_shared_library("fetch") { - include_dirs = [ - "$NETSTACK_NAPI_ROOT/fetch/async_context/include", - "$NETSTACK_NAPI_ROOT/fetch/async_work/include", - "$NETSTACK_NAPI_ROOT/fetch/constant/include", - "$NETSTACK_NAPI_ROOT/fetch/fetch_exec/include", - "$NETSTACK_NAPI_ROOT/fetch/fetch_module/include", - "$NETSTACK_NAPI_ROOT/fetch/options/include", - ] - include_dirs += utils_include - include_dirs += common_include - include_dirs += [ "//third_party/curl/include" ] - - sources = [ - "fetch/async_context/src/fetch_context.cpp", - "fetch/async_work/src/fetch_async_work.cpp", - "fetch/constant/src/constant.cpp", - "fetch/fetch_exec/src/fetch_exec.cpp", - "fetch/fetch_module/src/fetch_module.cpp", - "fetch/options/src/fetch_request.cpp", - "fetch/options/src/fetch_response.cpp", - ] - sources += utils_source - - deps = common_deps - deps += [ "//third_party/curl:curl" ] - - external_deps = common_external_deps - - relative_install_dir = "module" - part_name = common_part_name - subsystem_name = common_subsystem_name -} diff --git a/frameworks/js/napi/fetch/BUILD.gn b/frameworks/js/napi/fetch/BUILD.gn new file mode 100644 index 000000000..e31fe70e0 --- /dev/null +++ b/frameworks/js/napi/fetch/BUILD.gn @@ -0,0 +1,52 @@ +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//foundation/communication/netstack/netstack_config.gni") + +ohos_shared_library("fetch") { + include_dirs = [ + "async_context/include", + "async_work/include", + "constant/include", + "fetch_exec/include", + "fetch_module/include", + "options/include", + "//third_party/curl/include", + ] + + sources = [ + "async_context/src/fetch_context.cpp", + "async_work/src/fetch_async_work.cpp", + "constant/src/constant.cpp", + "fetch_exec/src/fetch_exec.cpp", + "fetch_module/src/fetch_module.cpp", + "options/src/fetch_request.cpp", + "options/src/fetch_response.cpp", + ] + + deps = [ + "$NETSTACK_DIR/utils/napi_utils:napi_utils", + "//third_party/curl:curl", + ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "napi:ace_napi", + ] + + relative_install_dir = "module" + part_name = "netstack" + subsystem_name = "communication" +} diff --git a/frameworks/js/napi/fetch/async_context/include/fetch_context.h b/frameworks/js/napi/fetch/async_context/include/fetch_context.h index f087dc175..35daee0ec 100644 --- a/frameworks/js/napi/fetch/async_context/include/fetch_context.h +++ b/frameworks/js/napi/fetch/async_context/include/fetch_context.h @@ -18,7 +18,7 @@ #include "fetch_request.h" #include "fetch_response.h" -#include "netstack_base_context.h" +#include "base_context.h" #include "nocopyable.h" namespace OHOS::NetStack { diff --git a/frameworks/js/napi/fetch/async_context/src/fetch_context.cpp b/frameworks/js/napi/fetch/async_context/src/fetch_context.cpp index 08081c6a5..284d087ce 100644 --- a/frameworks/js/napi/fetch/async_context/src/fetch_context.cpp +++ b/frameworks/js/napi/fetch/async_context/src/fetch_context.cpp @@ -19,7 +19,7 @@ #include "fetch_exec.h" #include "netstack_common_utils.h" #include "netstack_log.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" namespace OHOS::NetStack { FetchContext::FetchContext(napi_env env, EventManager *manager) @@ -254,4 +254,4 @@ std::string FetchContext::GetResponseType() const { return responseType_; } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/fetch/async_work/src/fetch_async_work.cpp b/frameworks/js/napi/fetch/async_work/src/fetch_async_work.cpp index 93792f546..9217a20eb 100644 --- a/frameworks/js/napi/fetch/async_work/src/fetch_async_work.cpp +++ b/frameworks/js/napi/fetch/async_work/src/fetch_async_work.cpp @@ -16,7 +16,7 @@ #include "fetch_async_work.h" #include "fetch_exec.h" -#include "netstack_base_async_work.h" +#include "base_async_work.h" namespace OHOS::NetStack { void FetchAsyncWork::ExecFetch(napi_env env, void *data) @@ -28,4 +28,4 @@ void FetchAsyncWork::FetchCallback(napi_env env, napi_status status, void *data) { BaseAsyncWork::AsyncWorkCallbackForSystem(env, status, data); } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/fetch/fetch_exec/src/fetch_exec.cpp b/frameworks/js/napi/fetch/fetch_exec/src/fetch_exec.cpp index 4923fdb77..8259305c7 100644 --- a/frameworks/js/napi/fetch/fetch_exec/src/fetch_exec.cpp +++ b/frameworks/js/napi/fetch/fetch_exec/src/fetch_exec.cpp @@ -22,7 +22,7 @@ #include "constant.h" #include "netstack_common_utils.h" #include "netstack_log.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" #include "securec.h" #define NETSTACK_CURL_EASY_SET_OPTION(handle, opt, data, asyncContext) \ @@ -346,4 +346,4 @@ bool FetchExec::IsUnReserved(unsigned char in) } return false; } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/fetch/fetch_module/include/fetch_module.h b/frameworks/js/napi/fetch/fetch_module/include/fetch_module.h index c647e1eb7..ae91b7c59 100644 --- a/frameworks/js/napi/fetch/fetch_module/include/fetch_module.h +++ b/frameworks/js/napi/fetch/fetch_module/include/fetch_module.h @@ -16,7 +16,7 @@ #ifndef COMMUNICATIONNETSTACK_FETCH_MODULE_H #define COMMUNICATIONNETSTACK_FETCH_MODULE_H -#include "netstack_napi_utils.h" +#include "napi_utils.h" #include "nocopyable.h" namespace OHOS::NetStack { diff --git a/frameworks/js/napi/fetch/fetch_module/src/fetch_module.cpp b/frameworks/js/napi/fetch/fetch_module/src/fetch_module.cpp index 14691dbc2..db7a0386a 100644 --- a/frameworks/js/napi/fetch/fetch_module/src/fetch_module.cpp +++ b/frameworks/js/napi/fetch/fetch_module/src/fetch_module.cpp @@ -18,7 +18,7 @@ #include "fetch_async_work.h" #include "fetch_exec.h" #include "netstack_log.h" -#include "netstack_module_template.h" +#include "module_template.h" namespace OHOS::NetStack { napi_value FetchModule::InitFetchModule(napi_env env, napi_value exports) diff --git a/frameworks/js/napi/http/BUILD.gn b/frameworks/js/napi/http/BUILD.gn new file mode 100644 index 000000000..821c28882 --- /dev/null +++ b/frameworks/js/napi/http/BUILD.gn @@ -0,0 +1,74 @@ +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//foundation/communication/netstack/netstack_config.gni") + +ohos_shared_library("http") { + include_dirs = [ + "async_context/include", + "async_work/include", + "cache/base64/include", + "cache/cache_constant/include", + "cache/cache_proxy/include", + "cache/cache_strategy/include", + "cache/lru_cache/include", + "cache/md5/include", + "constant/include", + "http_exec/include", + "http_module/include", + "options/include", + "//third_party/curl/include", + "//third_party/glib/glib", + "//third_party/glib", + "$SUBSYSTEM_DIR/netstack/utils/common_utils/include", + ] + + sources = [ + "async_context/src/request_context.cpp", + "async_work/src/http_async_work.cpp", + "cache/base64/src/base64_utils.cpp", + "cache/cache_proxy/src/cache_proxy.cpp", + "cache/cache_strategy/src/http_cache_request.cpp", + "cache/cache_strategy/src/http_cache_response.cpp", + "cache/cache_strategy/src/http_cache_strategy.cpp", + "cache/cache_strategy/src/http_time.cpp", + "cache/lru_cache/src/disk_handler.cpp", + "cache/lru_cache/src/lru_cache.cpp", + "cache/lru_cache/src/lru_cache_disk_handler.cpp", + "cache/md5/src/calculate_md5.cpp", + "constant/src/constant.cpp", + "http_exec/src/http_exec.cpp", + "http_module/src/http_module.cpp", + "options/src/http_request_options.cpp", + "options/src/http_response.cpp", + ] + + deps = [ + "$NETSTACK_DIR/utils/napi_utils:napi_utils", + "//third_party/curl:curl", + "//third_party/glib:glib_packages", + "//third_party/jsoncpp:jsoncpp", + "//third_party/openssl:libcrypto_shared", + ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "napi:ace_napi", + ] + + relative_install_dir = "module/net" + part_name = "netstack" + subsystem_name = "communication" +} diff --git a/frameworks/js/napi/http/async_context/include/request_context.h b/frameworks/js/napi/http/async_context/include/request_context.h index ac9ccec4a..37316cd3b 100644 --- a/frameworks/js/napi/http/async_context/include/request_context.h +++ b/frameworks/js/napi/http/async_context/include/request_context.h @@ -18,7 +18,7 @@ #include "http_request_options.h" #include "http_response.h" -#include "netstack_base_context.h" +#include "base_context.h" #include "nocopyable.h" namespace OHOS::NetStack { diff --git a/frameworks/js/napi/http/async_context/src/request_context.cpp b/frameworks/js/napi/http/async_context/src/request_context.cpp index 42a050e29..a356c4fcd 100644 --- a/frameworks/js/napi/http/async_context/src/request_context.cpp +++ b/frameworks/js/napi/http/async_context/src/request_context.cpp @@ -21,7 +21,7 @@ #include "http_exec.h" #include "netstack_common_utils.h" #include "netstack_log.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" static constexpr const int PARAM_JUST_URL = 1; @@ -266,4 +266,4 @@ bool RequestContext::IsUsingCache() const { return usingCache_; } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/http/async_work/src/http_async_work.cpp b/frameworks/js/napi/http/async_work/src/http_async_work.cpp index e89df42c9..008ae9764 100644 --- a/frameworks/js/napi/http/async_work/src/http_async_work.cpp +++ b/frameworks/js/napi/http/async_work/src/http_async_work.cpp @@ -16,8 +16,8 @@ #include "http_async_work.h" #include "http_exec.h" -#include "netstack_base_async_work.h" -#include "netstack_napi_utils.h" +#include "base_async_work.h" +#include "napi_utils.h" namespace OHOS::NetStack { void HttpAsyncWork::ExecRequest(napi_env env, void *data) @@ -49,4 +49,4 @@ void HttpAsyncWork::DeleteCallback(napi_env env, napi_status status, void *data) { BaseAsyncWork::AsyncWorkCallback(env, status, data); } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/http/http_exec/src/http_exec.cpp b/frameworks/js/napi/http/http_exec/src/http_exec.cpp index d8a326602..d63a95e05 100644 --- a/frameworks/js/napi/http/http_exec/src/http_exec.cpp +++ b/frameworks/js/napi/http/http_exec/src/http_exec.cpp @@ -25,7 +25,7 @@ #include "http_time.h" #include "netstack_common_utils.h" #include "netstack_log.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" #include "securec.h" #include "http_exec.h" diff --git a/frameworks/js/napi/http/http_module/src/http_module.cpp b/frameworks/js/napi/http/http_module/src/http_module.cpp index 6b3dfd060..f5d8c71b3 100644 --- a/frameworks/js/napi/http/http_module/src/http_module.cpp +++ b/frameworks/js/napi/http/http_module/src/http_module.cpp @@ -21,7 +21,7 @@ #include "http_async_work.h" #include "http_exec.h" #include "netstack_log.h" -#include "netstack_module_template.h" +#include "module_template.h" #define DECLARE_RESPONSE_CODE(code) \ DECLARE_NAPI_STATIC_PROPERTY(#code, NapiUtils::CreateUint32(env, static_cast(ResponseCode::code))) diff --git a/frameworks/js/napi/socket/BUILD.gn b/frameworks/js/napi/socket/BUILD.gn new file mode 100644 index 000000000..32956cbbc --- /dev/null +++ b/frameworks/js/napi/socket/BUILD.gn @@ -0,0 +1,61 @@ +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//foundation/communication/netstack/netstack_config.gni") + +ohos_shared_library("socket") { + include_dirs = [ + "async_context/include", + "async_work/include", + "constant/include", + "socket_exec/include", + "socket_module/include", + "options/include", + "task_queue/include", + ] + + sources = [ + "async_context/src/bind_context.cpp", + "async_context/src/common_context.cpp", + "async_context/src/connect_context.cpp", + "async_context/src/tcp_extra_context.cpp", + "async_context/src/tcp_send_context.cpp", + "async_context/src/udp_extra_context.cpp", + "async_context/src/udp_send_context.cpp", + "async_work/src/socket_async_work.cpp", + "options/src/extra_options_base.cpp", + "options/src/net_address.cpp", + "options/src/socket_remote_info.cpp", + "options/src/socket_state_base.cpp", + "options/src/tcp_connect_options.cpp", + "options/src/tcp_extra_options.cpp", + "options/src/tcp_send_options.cpp", + "options/src/udp_extra_options.cpp", + "options/src/udp_send_options.cpp", + "socket_exec/src/socket_exec.cpp", + "socket_module/src/socket_module.cpp", + ] + + deps = [ "$NETSTACK_DIR/utils/napi_utils:napi_utils" ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "napi:ace_napi", + ] + + relative_install_dir = "module/net" + part_name = "netstack" + subsystem_name = "communication" +} diff --git a/frameworks/js/napi/socket/async_context/include/bind_context.h b/frameworks/js/napi/socket/async_context/include/bind_context.h index 4b9cdb63f..385d6e42e 100644 --- a/frameworks/js/napi/socket/async_context/include/bind_context.h +++ b/frameworks/js/napi/socket/async_context/include/bind_context.h @@ -20,7 +20,7 @@ #include "napi/native_api.h" #include "net_address.h" -#include "netstack_base_context.h" +#include "base_context.h" #include "nocopyable.h" namespace OHOS::NetStack { diff --git a/frameworks/js/napi/socket/async_context/include/common_context.h b/frameworks/js/napi/socket/async_context/include/common_context.h index 79cc3dfc9..9e79d6ecc 100644 --- a/frameworks/js/napi/socket/async_context/include/common_context.h +++ b/frameworks/js/napi/socket/async_context/include/common_context.h @@ -20,7 +20,7 @@ #include "napi/native_api.h" #include "net_address.h" -#include "netstack_base_context.h" +#include "base_context.h" #include "nocopyable.h" #include "socket_remote_info.h" #include "socket_state_base.h" diff --git a/frameworks/js/napi/socket/async_context/include/connect_context.h b/frameworks/js/napi/socket/async_context/include/connect_context.h index 55d28000e..c42406015 100644 --- a/frameworks/js/napi/socket/async_context/include/connect_context.h +++ b/frameworks/js/napi/socket/async_context/include/connect_context.h @@ -19,7 +19,7 @@ #include #include "napi/native_api.h" -#include "netstack_base_context.h" +#include "base_context.h" #include "nocopyable.h" #include "tcp_connect_options.h" diff --git a/frameworks/js/napi/socket/async_context/include/tcp_extra_context.h b/frameworks/js/napi/socket/async_context/include/tcp_extra_context.h index bad6257af..47ea0fb0d 100644 --- a/frameworks/js/napi/socket/async_context/include/tcp_extra_context.h +++ b/frameworks/js/napi/socket/async_context/include/tcp_extra_context.h @@ -19,7 +19,7 @@ #include #include "napi/native_api.h" -#include "netstack_base_context.h" +#include "base_context.h" #include "nocopyable.h" #include "tcp_extra_options.h" diff --git a/frameworks/js/napi/socket/async_context/include/tcp_send_context.h b/frameworks/js/napi/socket/async_context/include/tcp_send_context.h index 4d52fc442..e477b6942 100644 --- a/frameworks/js/napi/socket/async_context/include/tcp_send_context.h +++ b/frameworks/js/napi/socket/async_context/include/tcp_send_context.h @@ -19,7 +19,7 @@ #include #include "napi/native_api.h" -#include "netstack_base_context.h" +#include "base_context.h" #include "nocopyable.h" #include "tcp_send_options.h" diff --git a/frameworks/js/napi/socket/async_context/include/udp_extra_context.h b/frameworks/js/napi/socket/async_context/include/udp_extra_context.h index cc9021dc9..c6f8dee83 100644 --- a/frameworks/js/napi/socket/async_context/include/udp_extra_context.h +++ b/frameworks/js/napi/socket/async_context/include/udp_extra_context.h @@ -19,7 +19,7 @@ #include #include "napi/native_api.h" -#include "netstack_base_context.h" +#include "base_context.h" #include "nocopyable.h" #include "udp_extra_options.h" diff --git a/frameworks/js/napi/socket/async_context/include/udp_send_context.h b/frameworks/js/napi/socket/async_context/include/udp_send_context.h index 45177aaf9..f791532c8 100644 --- a/frameworks/js/napi/socket/async_context/include/udp_send_context.h +++ b/frameworks/js/napi/socket/async_context/include/udp_send_context.h @@ -19,7 +19,7 @@ #include #include "napi/native_api.h" -#include "netstack_base_context.h" +#include "base_context.h" #include "nocopyable.h" #include "udp_send_options.h" diff --git a/frameworks/js/napi/socket/async_context/src/bind_context.cpp b/frameworks/js/napi/socket/async_context/src/bind_context.cpp index eefba4018..976ebbda1 100644 --- a/frameworks/js/napi/socket/async_context/src/bind_context.cpp +++ b/frameworks/js/napi/socket/async_context/src/bind_context.cpp @@ -16,9 +16,9 @@ #include "bind_context.h" #include "context_key.h" -#include "netstack_event_manager.h" +#include "event_manager.h" #include "netstack_log.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" namespace OHOS::NetStack { BindContext::BindContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {} diff --git a/frameworks/js/napi/socket/async_context/src/common_context.cpp b/frameworks/js/napi/socket/async_context/src/common_context.cpp index 067364101..5d4677201 100644 --- a/frameworks/js/napi/socket/async_context/src/common_context.cpp +++ b/frameworks/js/napi/socket/async_context/src/common_context.cpp @@ -16,8 +16,8 @@ #include "common_context.h" #include "context_key.h" -#include "netstack_event_manager.h" -#include "netstack_napi_utils.h" +#include "event_manager.h" +#include "napi_utils.h" namespace OHOS::NetStack { CommonContext::CommonContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {} @@ -59,4 +59,4 @@ void CloseContext::SetSocketFd(int sock) { manager_->SetData(reinterpret_cast(sock)); } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/socket/async_context/src/connect_context.cpp b/frameworks/js/napi/socket/async_context/src/connect_context.cpp index 999e21648..c8ad5b9fb 100644 --- a/frameworks/js/napi/socket/async_context/src/connect_context.cpp +++ b/frameworks/js/napi/socket/async_context/src/connect_context.cpp @@ -17,9 +17,9 @@ #include "context_key.h" #include "net_address.h" -#include "netstack_event_manager.h" +#include "event_manager.h" #include "netstack_log.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" namespace OHOS::NetStack { ConnectContext::ConnectContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {} @@ -80,4 +80,4 @@ bool ConnectContext::CheckParamsType(napi_value *params, size_t paramsCount) } return false; } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/socket/async_context/src/tcp_extra_context.cpp b/frameworks/js/napi/socket/async_context/src/tcp_extra_context.cpp index b9e16805f..56cc07135 100644 --- a/frameworks/js/napi/socket/async_context/src/tcp_extra_context.cpp +++ b/frameworks/js/napi/socket/async_context/src/tcp_extra_context.cpp @@ -16,8 +16,8 @@ #include "tcp_extra_context.h" #include "context_key.h" -#include "netstack_event_manager.h" -#include "netstack_napi_utils.h" +#include "event_manager.h" +#include "napi_utils.h" namespace OHOS::NetStack { TcpSetExtraOptionsContext::TcpSetExtraOptionsContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {} @@ -94,4 +94,4 @@ bool TcpSetExtraOptionsContext::CheckParamsType(napi_value *params, size_t param } return false; } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/socket/async_context/src/tcp_send_context.cpp b/frameworks/js/napi/socket/async_context/src/tcp_send_context.cpp index 90def4ba6..07313fd9d 100644 --- a/frameworks/js/napi/socket/async_context/src/tcp_send_context.cpp +++ b/frameworks/js/napi/socket/async_context/src/tcp_send_context.cpp @@ -16,9 +16,9 @@ #include "tcp_send_context.h" #include "context_key.h" -#include "netstack_event_manager.h" +#include "event_manager.h" #include "netstack_log.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" namespace OHOS::NetStack { TcpSendContext::TcpSendContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {} @@ -88,4 +88,4 @@ bool TcpSendContext::GetData(napi_value udpSendOptions) } return false; } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/socket/async_context/src/udp_extra_context.cpp b/frameworks/js/napi/socket/async_context/src/udp_extra_context.cpp index cee7897bd..19f2ca47b 100644 --- a/frameworks/js/napi/socket/async_context/src/udp_extra_context.cpp +++ b/frameworks/js/napi/socket/async_context/src/udp_extra_context.cpp @@ -16,8 +16,8 @@ #include "udp_extra_context.h" #include "context_key.h" -#include "netstack_event_manager.h" -#include "netstack_napi_utils.h" +#include "event_manager.h" +#include "napi_utils.h" namespace OHOS::NetStack { UdpSetExtraOptionsContext::UdpSetExtraOptionsContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {} @@ -73,4 +73,4 @@ bool UdpSetExtraOptionsContext::CheckParamsType(napi_value *params, size_t param } return false; } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/socket/async_context/src/udp_send_context.cpp b/frameworks/js/napi/socket/async_context/src/udp_send_context.cpp index ae525330d..37a04ac28 100644 --- a/frameworks/js/napi/socket/async_context/src/udp_send_context.cpp +++ b/frameworks/js/napi/socket/async_context/src/udp_send_context.cpp @@ -17,9 +17,9 @@ #include "context_key.h" #include "net_address.h" -#include "netstack_event_manager.h" +#include "event_manager.h" #include "netstack_log.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" namespace OHOS::NetStack { UdpSendContext::UdpSendContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {} diff --git a/frameworks/js/napi/socket/async_work/src/socket_async_work.cpp b/frameworks/js/napi/socket/async_work/src/socket_async_work.cpp index f4a75cac4..eebf822ed 100644 --- a/frameworks/js/napi/socket/async_work/src/socket_async_work.cpp +++ b/frameworks/js/napi/socket/async_work/src/socket_async_work.cpp @@ -18,7 +18,7 @@ #include "bind_context.h" #include "common_context.h" #include "connect_context.h" -#include "netstack_base_async_work.h" +#include "base_async_work.h" #include "socket_exec.h" #include "tcp_extra_context.h" #include "tcp_send_context.h" @@ -122,4 +122,4 @@ void SocketAsyncWork::UdpSetExtraOptionsCallback(napi_env env, napi_status statu BaseAsyncWork::AsyncWorkCallback(env, status, data); } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/socket/options/include/socket_remote_info.h b/frameworks/js/napi/socket/options/include/socket_remote_info.h index 6b6f11234..d5a5c7596 100644 --- a/frameworks/js/napi/socket/options/include/socket_remote_info.h +++ b/frameworks/js/napi/socket/options/include/socket_remote_info.h @@ -19,6 +19,7 @@ #include #include #include +#include "bits/alltypes.h" #include "net_address.h" diff --git a/frameworks/js/napi/socket/socket_exec/src/socket_exec.cpp b/frameworks/js/napi/socket/socket_exec/src/socket_exec.cpp index 6758150ff..0293372cd 100644 --- a/frameworks/js/napi/socket/socket_exec/src/socket_exec.cpp +++ b/frameworks/js/napi/socket/socket_exec/src/socket_exec.cpp @@ -28,7 +28,7 @@ #include "context_key.h" #include "event_list.h" #include "netstack_log.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" #include "securec.h" static constexpr const int DEFAULT_BUFFER_SIZE = 8192; diff --git a/frameworks/js/napi/socket/socket_module/src/socket_module.cpp b/frameworks/js/napi/socket/socket_module/src/socket_module.cpp index 0b17376d2..84b9f381f 100644 --- a/frameworks/js/napi/socket/socket_module/src/socket_module.cpp +++ b/frameworks/js/napi/socket/socket_module/src/socket_module.cpp @@ -29,10 +29,10 @@ #include "napi/native_api.h" #include "napi/native_common.h" #include "net_address.h" -#include "netstack_event_manager.h" +#include "event_manager.h" #include "netstack_log.h" -#include "netstack_module_template.h" -#include "netstack_napi_utils.h" +#include "module_template.h" +#include "napi_utils.h" #include "node_api.h" #include "socket_async_work.h" #include "socket_exec.h" @@ -276,4 +276,4 @@ extern "C" __attribute__((constructor)) void RegisterSocketModule(void) { napi_module_register(&g_socketModule); } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/socket/task_queue/include/task_queue.h b/frameworks/js/napi/socket/task_queue/include/task_queue.h index 57a56c79a..efa3b2b9f 100644 --- a/frameworks/js/napi/socket/task_queue/include/task_queue.h +++ b/frameworks/js/napi/socket/task_queue/include/task_queue.h @@ -16,7 +16,7 @@ #ifndef COMMUNICATIONNETSTACK_TASK_QUEUE_H #define COMMUNICATIONNETSTACK_TASK_QUEUE_H -#include "netstack_base_context.h" +#include "base_context.h" namespace OHOS::NetStack::Task { enum class TaskPriority { diff --git a/frameworks/js/napi/websocket/BUILD.gn b/frameworks/js/napi/websocket/BUILD.gn new file mode 100644 index 000000000..b9c5107ef --- /dev/null +++ b/frameworks/js/napi/websocket/BUILD.gn @@ -0,0 +1,54 @@ +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//foundation/communication/netstack/netstack_config.gni") + +ohos_shared_library("websocket") { + include_dirs = [ + "async_context/include", + "async_work/include", + "constant/include", + "websocket_exec/include", + "websocket_module/include", + "//third_party/libwebsockets/include", + ] + + sources = [ + "async_context/src/close_context.cpp", + "async_context/src/connect_context.cpp", + "async_context/src/send_context.cpp", + "async_work/src/websocket_async_work.cpp", + "constant/src/constant.cpp", + "websocket_exec/src/websocket_exec.cpp", + "websocket_module/src/websocket_module.cpp", + ] + + deps = [ + "$NETSTACK_DIR/utils/napi_utils:napi_utils", + "//third_party/libwebsockets:websockets", + "//third_party/openssl:libcrypto_static", + "//third_party/openssl:ssl_source", + "//third_party/zlib:libz", + ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "napi:ace_napi", + ] + + relative_install_dir = "module/net" + part_name = "netstack" + subsystem_name = "communication" +} diff --git a/frameworks/js/napi/websocket/async_context/include/close_context.h b/frameworks/js/napi/websocket/async_context/include/close_context.h index 3bb8253d8..908fa9fde 100644 --- a/frameworks/js/napi/websocket/async_context/include/close_context.h +++ b/frameworks/js/napi/websocket/async_context/include/close_context.h @@ -18,7 +18,7 @@ #include -#include "netstack_base_context.h" +#include "base_context.h" #include "nocopyable.h" namespace OHOS::NetStack { diff --git a/frameworks/js/napi/websocket/async_context/include/connect_context.h b/frameworks/js/napi/websocket/async_context/include/connect_context.h index ddb0b08ee..2a450c092 100644 --- a/frameworks/js/napi/websocket/async_context/include/connect_context.h +++ b/frameworks/js/napi/websocket/async_context/include/connect_context.h @@ -19,7 +19,7 @@ #include #include -#include "netstack_base_context.h" +#include "base_context.h" #include "nocopyable.h" #include "libwebsockets.h" diff --git a/frameworks/js/napi/websocket/async_context/include/send_context.h b/frameworks/js/napi/websocket/async_context/include/send_context.h index f054d2828..3d5b82267 100644 --- a/frameworks/js/napi/websocket/async_context/include/send_context.h +++ b/frameworks/js/napi/websocket/async_context/include/send_context.h @@ -19,7 +19,7 @@ #include #include "libwebsockets.h" -#include "netstack_base_context.h" +#include "base_context.h" #include "nocopyable.h" namespace OHOS::NetStack { diff --git a/frameworks/js/napi/websocket/async_context/src/close_context.cpp b/frameworks/js/napi/websocket/async_context/src/close_context.cpp index e3e8c7c3f..52c86a6f5 100644 --- a/frameworks/js/napi/websocket/async_context/src/close_context.cpp +++ b/frameworks/js/napi/websocket/async_context/src/close_context.cpp @@ -18,7 +18,7 @@ #include "constant.h" #include "netstack_common_utils.h" #include "netstack_log.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" namespace OHOS::NetStack { CloseContext::CloseContext(napi_env env, EventManager *manager) @@ -75,4 +75,4 @@ bool CloseContext::CheckParamsType(napi_value *params, size_t paramsCount) return false; } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/websocket/async_context/src/connect_context.cpp b/frameworks/js/napi/websocket/async_context/src/connect_context.cpp index 4f4c758dd..883513d6f 100644 --- a/frameworks/js/napi/websocket/async_context/src/connect_context.cpp +++ b/frameworks/js/napi/websocket/async_context/src/connect_context.cpp @@ -18,7 +18,7 @@ #include "constant.h" #include "netstack_common_utils.h" #include "netstack_log.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" #include "securec.h" namespace OHOS::NetStack { diff --git a/frameworks/js/napi/websocket/async_context/src/send_context.cpp b/frameworks/js/napi/websocket/async_context/src/send_context.cpp index 59e04a604..a559c62b7 100644 --- a/frameworks/js/napi/websocket/async_context/src/send_context.cpp +++ b/frameworks/js/napi/websocket/async_context/src/send_context.cpp @@ -17,7 +17,7 @@ #include "constant.h" #include "netstack_log.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" #include "securec.h" namespace OHOS::NetStack { @@ -96,4 +96,4 @@ bool SendContext::CheckParamsType(napi_value *params, size_t paramsCount) return false; } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/websocket/async_work/src/websocket_async_work.cpp b/frameworks/js/napi/websocket/async_work/src/websocket_async_work.cpp index 7162eb49e..e3bd04ec7 100644 --- a/frameworks/js/napi/websocket/async_work/src/websocket_async_work.cpp +++ b/frameworks/js/napi/websocket/async_work/src/websocket_async_work.cpp @@ -15,7 +15,7 @@ #include "websocket_async_work.h" -#include "netstack_base_async_work.h" +#include "base_async_work.h" namespace OHOS::NetStack { void WebSocketAsyncWork::ExecConnect(napi_env env, void *data) diff --git a/frameworks/js/napi/websocket/websocket_exec/src/websocket_exec.cpp b/frameworks/js/napi/websocket/websocket_exec/src/websocket_exec.cpp index 92202ed40..a87084554 100644 --- a/frameworks/js/napi/websocket/websocket_exec/src/websocket_exec.cpp +++ b/frameworks/js/napi/websocket/websocket_exec/src/websocket_exec.cpp @@ -22,7 +22,7 @@ #include "constant.h" #include "netstack_common_utils.h" #include "netstack_log.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" #include "securec.h" static constexpr const char *PATH_START = "/"; diff --git a/frameworks/js/napi/websocket/websocket_module/src/websocket_module.cpp b/frameworks/js/napi/websocket/websocket_module/src/websocket_module.cpp index 44b153d98..5492a72f3 100644 --- a/frameworks/js/napi/websocket/websocket_module/src/websocket_module.cpp +++ b/frameworks/js/napi/websocket/websocket_module/src/websocket_module.cpp @@ -17,7 +17,7 @@ #include "constant.h" #include "netstack_log.h" -#include "netstack_module_template.h" +#include "module_template.h" #include "websocket_async_work.h" namespace OHOS::NetStack { @@ -90,4 +90,4 @@ napi_value WebSocketModule::WebSocket::Off(napi_env env, napi_callback_info info } NAPI_MODULE(webSocket, WebSocketModule::InitWebSocketModule) -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/interfaces/innerkits/tls_socket/include/tls_socket.h b/interfaces/innerkits/tls_socket/include/tls_socket.h new file mode 100644 index 000000000..11e021e01 --- /dev/null +++ b/interfaces/innerkits/tls_socket/include/tls_socket.h @@ -0,0 +1,473 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef COMMUNICATIONNETSTACK_TLS_SOCEKT_H +#define COMMUNICATIONNETSTACK_TLS_SOCEKT_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "extra_options_base.h" +#include "net_address.h" +#include "socket_remote_info.h" +#include "socket_state_base.h" +#include "tcp_connect_options.h" +#include "tcp_extra_options.h" +#include "tcp_send_options.h" + +#include "tls_key.h" +#include "tls_certificate.h" +#include "tls_configuration.h" +#include "tls_context.h" +#include "tls_socket_internal.h" + +namespace OHOS { +namespace NetStack { + +using BindCallback = std::function; +using ConnectCallback = std::function; +using SendCallback = std::function; +using CloseCallback = std::function; +using GetRemoteAddressCallback = std::function; +using GetStateCallback = std::function; +using SetExtraOptionsCallback = std::function; +using GetCertificateCallback = std::function; +using GetRemoteCertificateCallback = std::function; +using GetProtocolCallback = std::function; +using GetCipherSuiteCallback = std::function &suite)>; +using GetSignatureAlgorithmsCallback = std::function &algorithms)>; + +using OnMessageCallback = std::function; +using OnConnectCallback = std::function; +using OnCloseCallback = std::function; +using OnErrorCallback = std::function; + +using CheckServerIdentity = + std::function(const std::string &hostName, + const std::vector &x509Certificates)>; + +static constexpr const char *PROTOCOL_TLS_V12 = "TLSv1.2"; +static constexpr const char *PROTOCOL_TLS_V13 = "TLSv1.3"; + +static constexpr const char *ALPN_PROTOCOLS_HTTP_1_1 = "http1.1"; +static constexpr const char *ALPN_PROTOCOLS_HTTP_2 = "h2"; + +/** +* TODO: +*/ +class TLSSecureOptions { +public: + TLSSecureOptions() = default; + ~TLSSecureOptions() = default; + + TLSSecureOptions& operator=(const TLSSecureOptions &tlsSecureOptions); + /** + * set root CA Chain to verify the server cert + * @param caChain root certificate chain to verify the server + */ + void SetCaChain(const std::vector &caChain); + + /** + * set cert chain to send to server/client for check + * @param certChain cert chain to send to server/client for check + */ + void SetCert(const std::string &cert); + + /** + * set key chain to decrypt server data + * @param keyChain key chain to decrypt server data + */ + void SetKey(const std::string &key); + + /** + * set cert password + * @param passwd creating cert used password + */ + void SetPassWd(const std::string &passwd); + + /** + * set protocol Chain to encryption data + * @param protocolChain protocol Chain to encryption data + */ + void SetProtocolChain(const std::vector &protocolChain); + + /** + * set flag use Remote Cipher Prefer + * @param useRemoteCipherPrefer is use Remote Cipher Prefer + */ + void SetUseRemoteCipherPrefer(bool useRemoteCipherPrefer); + + /** + * set signature Algorithms for encryption/decrypt data + * @param signatureAlgorithms signature Algorithms e.g: rsa + */ + void SetSignatureAlgorithms(const std::string &signatureAlgorithms); + + /** + * set cipher Suite + * @param cipherSuite cipher Suite + */ + void SetCipherSuite(const std::string &cipherSuite); + + /** + * set crl chain for to creat cert chain + * @param crlChain crl chain for to creat cert chain TODO:: + */ + void SetCrlChain(const std::vector &crlChain); + + /** + * get root CA Chain to verify the server cert + * @return root CA Chain to verify the server cert + */ + [[nodiscard]] const std::vector& GetCaChain() const; + + /** + * get cert chain to send to server/client for check + * @return cert chain to send to server/client for check + */ + [[nodiscard]] const std::string& GetCert() const; + + /** + * get key chain to decrypt server data + * @return key chain to decrypt server data + */ + [[nodiscard]] const std::string& GetKey() const; + + /** + * get cert creat used password + * @return password + */ + [[nodiscard]] const std::string& GetPasswd() const; + + /** + * get protocol Chain to encryption data + * @return protocol Chain to encryption data + */ + [[nodiscard]] const std::vector& GetProtocolChain() const; + + /** + * get flag use Remote Cipher Prefer + * @return is use Remote Cipher Prefer + */ + [[nodiscard]] bool UseRemoteCipherPrefer() const; + + /** + * get signature Algorithms for encryption/decrypt data + * @return signature Algorithms for encryption/decrypt data + */ + [[nodiscard]] const std::string& GetSignatureAlgorithms() const; + + /** + * get cipher suite + * @return cipher suite + */ + [[nodiscard]] const std::string& GetCipherSuite() const; + + /** + * get crl chain for to creat cert chain + * @return crl chain for to creat cert chain + */ + [[nodiscard]] const std::vector& GetCrlChain() const; + +private: + std::vector caChain_; + std::string cert_; + std::string key_; + std::string passwd_; + std::vector protocolChain_; + bool useRemoteCipherPrefer_ = false; + std::string signatureAlgorithms_; + std::string cipherSuite_; + std::vector crlChain_; +}; + +/** + * tls connect options + */ +class TLSConnectOptions { +public: + /** + * Set Net Socket Address + * @param address socket address + */ + void SetNetAddress(const NetAddress &address); + + /** + * Set Tls Secure Options + * @param tlsSecureOptions class TLSSecureOptions + */ + void SetTlsSecureOptions(TLSSecureOptions &tlsSecureOptions); + + /** + * Check Server Identity + * @param checkServerIdentity TODO:: + */ + void SetCheckServerIdentity(const CheckServerIdentity &checkServerIdentity); + + /** + * Set Alpn Protocols + * @param alpnProtocols alpn Protocols + */ + void SetAlpnProtocols(const std::vector &alpnProtocols); + + /** + * Get Net Socket Address + * @return net socket address + */ + [[nodiscard]] NetAddress GetNetAddress() const; + + /** + * Get TLS Secure Options + * @return tls secure options + */ + [[nodiscard]] TLSSecureOptions GetTlsSecureOptions() const; + + /** + * Check Server Indentity + * @return Server Indentity + */ + [[nodiscard]] CheckServerIdentity GetCheckServerIdentity() const; + + /** + * Get Alpn Protocols + * @return alpn protocols + */ + [[nodiscard]] const std::vector& GetAlpnProtocols() const; + +private: + NetAddress address_; + TLSSecureOptions tlsSecureOptions_; + CheckServerIdentity checkServerIdentity_; + std::vector alpnProtocols_; +}; + +/** + * TLS socket interface class + */ +class TLSSocket { +public: + TLSSocket(const TLSSocket &) = delete; + TLSSocket(TLSSocket &&) = delete; + + TLSSocket &operator=(const TLSSocket &) = delete; + TLSSocket &operator=(TLSSocket &&) = delete; + + TLSSocket() = default; + ~TLSSocket(); + + /** + * Establish Bind Monitor + * @param address Ip address + * @param callback callback + */ + void Bind(const NetAddress &address, const BindCallback &callback); + + /** + * Establish connection + * @param tlsConnectOptions tls connect options + * @param callback callback + */ + void Connect(TLSConnectOptions &tlsConnectOptions, const ConnectCallback &callback); + + /** + * Send Data + * @param tcpSendOptions tcp send options + * @param callback callback + */ + void Send(const TCPSendOptions &tcpSendOptions, const SendCallback &callback); + + /** + * Close + * @param callback callback + */ + void Close(const CloseCallback &callback); + + /** + * Get Remote Address + * @param callback callback + */ + void GetRemoteAddress(const GetRemoteAddressCallback &callback); + + /** + * Get Tls State + * @param callback callback state data + */ + void GetState(const GetStateCallback &callback); + + /** + * Set Extra Options + * @param tcpExtraOptions tcp extra options + * @param callback callback extra options + */ + void SetExtraOptions(const TCPExtraOptions &tcpExtraOptions, const SetExtraOptionsCallback &callback); + + /** + * Get Certificate + * @param callback callback certificate + */ + void GetCertificate(const GetCertificateCallback &callback); + + /** + * Get Remote Certificate + * @param needChain need chain + * @param callback callback get remote certificate + */ + void GetRemoteCertificate(const GetRemoteCertificateCallback &callback); + + /** + * Get Protocol + * @param callback callback protocol + */ + void GetProtocol(const GetProtocolCallback &callback); + + /** + * Get Cipher Suite + * @param callback callback cipher suite + */ + void GetCipherSuite(const GetCipherSuiteCallback &callback); + + /** + * Get Signature Algorithms + * @param callback callback signature algorithms + */ + void GetSignatureAlgorithms(const GetSignatureAlgorithmsCallback &callback); + + /** + * On Message + * @param onMessageCallback callback on message + */ + void OnMessage(const OnMessageCallback &onMessageCallback); + + /** + * On Connect + * @param onConnectCallback callback on connect + */ + void OnConnect(const OnConnectCallback &onConnectCallback); + + /** + * On Close + * @param onCloseCallback callback on close + */ + void OnClose(const OnCloseCallback &onCloseCallback); + + /** + * On Error + * @param onErrorCallback callback on error + */ + void OnError(const OnErrorCallback &onErrorCallback); + + /** + * On Error + * @param onErrorCallback callback on error + */ + void OffMessage(); + + /** + * Off Connect + */ + void OffConnect(); + + /** + * Off Close + */ + void OffClose(); + + /** + * Off Error + */ + void OffError(); + +private: + class OpenSSLContext { + public: + TLSContext tlsContext_; + TLSConfiguration tlsConfiguration_; + TLSCertificate tlsCertificate_; + TLSKey tlsKey_; + TLSSocketInternal tlsSocketInternal_; + }; + OpenSSLContext openSslContext_; + static std::string MakeErrnoString(); + + static std::string MakeAddressString(sockaddr *addr); + + static void + GetAddr(const NetAddress &address, sockaddr_in *addr4, sockaddr_in6 *addr6, sockaddr **addr, socklen_t *len); + + void CallOnMessageCallback(const std::string &data, const SocketRemoteInfo &remoteInfo); + void CallOnConnectCallback(); + void CallOnCloseCallback(); + void CallOnErrorCallback(int32_t err, const std::string &errString); + + void CallBindCallback(bool ok, const BindCallback &callback); + void CallConnectCallback(bool ok, const ConnectCallback &callback); + void CallSendCallback(bool ok, const SendCallback &callback); + void CallCloseCallback(bool ok, const CloseCallback &callback); + void CallGetRemoteAddressCallback(bool ok, const NetAddress &address, const GetRemoteAddressCallback &callback); + void CallGetStateCallback(bool ok, const SocketStateBase &state, const GetStateCallback &callback); + void CallSetExtraOptionsCallback(bool ok, const SetExtraOptionsCallback &callback); + void CallGetCertificateCallback(bool ok, const std::string &cert); + void CallGetRemoteCertificateCallback(bool ok, const std::string &cert); + void CallGetProtocolCallback(bool ok, const std::string &protocol); + void CallGetCipherSuiteCallback(bool ok, const std::vector &suite); + void CallGetSignatureAlgorithmsCallback(bool ok, const std::vector& algorithms); + + void StartReadMessage(); + + void GetIp4RemoteAddress(const GetRemoteAddressCallback &callback); + void GetIp6RemoteAddress(const GetRemoteAddressCallback &callback); + + [[nodiscard]] bool SetBaseOptions(const ExtraOptionsBase &option) const; + [[nodiscard]] bool SetExtraOptions(const TCPExtraOptions &option) const; + + void MakeTcpSocket(sa_family_t family); + + static constexpr const size_t MAX_ERROR_LEN = 128; + static constexpr const size_t MAX_BUFFER_SIZE = 8192; + + OnMessageCallback onMessageCallback_; + OnConnectCallback onConnectCallback_; + OnCloseCallback onCloseCallback_; + OnErrorCallback onErrorCallback_; + + BindCallback bindCallback_; + ConnectCallback connectCallback_; + SendCallback sendCallback_; + CloseCallback closeCallback_; + GetRemoteAddressCallback getRemoteAddressCallback_; + GetStateCallback getStateCallback_; + SetExtraOptionsCallback setExtraOptionsCallback_; + GetCertificateCallback getCertificateCallback_; + GetRemoteCertificateCallback getRemoteCertificateCallback_; + GetProtocolCallback getProtocolCallback_; + GetCipherSuiteCallback getCipherSuiteCallback_; + GetSignatureAlgorithmsCallback getSignatureAlgorithmsCallback_; + + std::mutex mutex_; + bool isRunning_ = false; + bool isRunOver_ = true; + + int sockFd_ = -1; +}; +} } // namespace OHOS::NetStack + +#endif // COMMUNICATIONNETSTACK_TLS_SOCEKT_H diff --git a/netstack_config.gni b/netstack_config.gni new file mode 100644 index 000000000..a5c76b058 --- /dev/null +++ b/netstack_config.gni @@ -0,0 +1,17 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +SUBSYSTEM_DIR = "//foundation/communication" +NETSTACK_DIR = "$SUBSYSTEM_DIR/netstack" +NETSTACK_NATIVE_ROOT = "$NETSTACK_DIR/frameworks/native" +NETSTACK_INNERKITS_DIR = "$NETSTACK_DIR/interfaces/innerkits" diff --git a/test/napi/socket/fuzztest/socketexec_fuzzer/BUILD.gn b/test/napi/socket/fuzztest/socketexec_fuzzer/BUILD.gn index 4314b5972..df69284c7 100644 --- a/test/napi/socket/fuzztest/socketexec_fuzzer/BUILD.gn +++ b/test/napi/socket/fuzztest/socketexec_fuzzer/BUILD.gn @@ -15,38 +15,12 @@ import("//build/config/features.gni") import("//build/ohos.gni") import("//build/test.gni") +import("//foundation/communication/netstack/netstack_config.gni") ##############################fuzztest########################################## -SUBSYSTEM_DIR = "//foundation/communication" NETSTACK_NAPI_ROOT = "$SUBSYSTEM_DIR/netstack/frameworks/js/napi/" -utils_include = [ - "$SUBSYSTEM_DIR/netstack/utils/base_async_work/include", - "$SUBSYSTEM_DIR/netstack/utils/base_context/include", - "$SUBSYSTEM_DIR/netstack/utils/common_utils/include", - "$SUBSYSTEM_DIR/netstack/utils/event_manager/include", - "$SUBSYSTEM_DIR/netstack/utils/log/include", - "$SUBSYSTEM_DIR/netstack/utils/module_template/include", - "$SUBSYSTEM_DIR/netstack/utils/napi_utils/include", -] - -utils_source = [ - "$SUBSYSTEM_DIR/netstack/utils/base_context/src/netstack_base_context.cpp", - "$SUBSYSTEM_DIR/netstack/utils/common_utils/src/netstack_common_utils.cpp", - "$SUBSYSTEM_DIR/netstack/utils/event_manager/src/netstack_event_listener.cpp", - "$SUBSYSTEM_DIR/netstack/utils/event_manager/src/netstack_event_manager.cpp", - "$SUBSYSTEM_DIR/netstack/utils/module_template/src/netstack_module_template.cpp", - "$SUBSYSTEM_DIR/netstack/utils/napi_utils/src/netstack_napi_utils.cpp", -] - -common_include = [ - "//foundation/arkui/ace_engine/frameworks/base/utils", - "//foundation/arkui/napi/interfaces/kits", - "//foundation/arkui/napi", - "//third_party/node/src", -] - ohos_fuzztest("SocketExecFuzzTest") { module_out_path = "communication/netstack" fuzz_config_file = "//foundation/communication/netstack/test/napi/socket/fuzztest/socketexec_fuzzer" @@ -60,8 +34,6 @@ ohos_fuzztest("SocketExecFuzzTest") { "$NETSTACK_NAPI_ROOT/socket/options/include", "$NETSTACK_NAPI_ROOT/socket/task_queue/include", ] - include_dirs += utils_include - include_dirs += common_include cflags = [ "-g", @@ -91,13 +63,13 @@ ohos_fuzztest("SocketExecFuzzTest") { "$NETSTACK_NAPI_ROOT/socket/socket_exec/src/socket_exec.cpp", ] sources += [ "socket_exec_fuzzer.cpp" ] - sources += utils_source - deps = [ "//foundation/arkui/napi/:ace_napi" ] + deps = [ "$NETSTACK_DIR/utils/napi_utils:napi_utils" ] external_deps = [ "c_utils:utils", "hiviewdfx_hilog_native:libhilog", + "napi:ace_napi", ] defines = [ diff --git a/utils/BUILD.gn b/utils/BUILD.gn new file mode 100644 index 000000000..976ce8fe3 --- /dev/null +++ b/utils/BUILD.gn @@ -0,0 +1,39 @@ +# Copyright (c) 2021~2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +group("common_utils") { + deps = [ + ":stack_utils_common", + "napi_utils:napi_utils", + ] +} + +config("stack_utils_common_public_config") { + include_dirs = [ + "common_utils/include", + "log/include", + ] +} + +ohos_shared_library("stack_utils_common") { + sources = [ "common_utils/src/netstack_common_utils.cpp" ] + + public_configs = [ ":stack_utils_common_public_config" ] + + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + + part_name = "netstack" + subsystem_name = "communication" +} diff --git a/utils/log/include/netstack_log.h b/utils/log/include/netstack_log.h index 4353ff3bc..ba62833af 100644 --- a/utils/log/include/netstack_log.h +++ b/utils/log/include/netstack_log.h @@ -86,4 +86,6 @@ static void NetStackPrintLog(const char *fmt, ...) #define NETSTACK_LOGI(fmt, ...) NETSTACK_HILOG_PRINT(Info, fmt, ##__VA_ARGS__) +#define NETSTACK_LOGD(fmt, ...) NETSTACK_HILOG_PRINT(Debug, fmt, ##__VA_ARGS__) + #endif /* COMMUNICATIONNETSTACK_NETSTACK_LOG */ \ No newline at end of file diff --git a/utils/napi_utils/BUILD.gn b/utils/napi_utils/BUILD.gn new file mode 100644 index 000000000..270e94d16 --- /dev/null +++ b/utils/napi_utils/BUILD.gn @@ -0,0 +1,44 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//foundation/communication/netstack/netstack_config.gni") + +config("napi_utils_public_config") { + include_dirs = [ + "include", + "//third_party/node/src", + ] +} + +ohos_shared_library("napi_utils") { + sources = [ + "src/base_context.cpp", + "src/event_listener.cpp", + "src/event_manager.cpp", + "src/module_template.cpp", + "src/napi_utils.cpp", + ] + + public_configs = [ ":napi_utils_public_config" ] + + public_deps = [ "$NETSTACK_DIR/utils:stack_utils_common" ] + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "napi:ace_napi", + ] + + part_name = "netstack" + subsystem_name = "communication" +} diff --git a/utils/base_async_work/include/netstack_base_async_work.h b/utils/napi_utils/include/base_async_work.h similarity index 98% rename from utils/base_async_work/include/netstack_base_async_work.h rename to utils/napi_utils/include/base_async_work.h index 85319a2f2..b9bdf786d 100644 --- a/utils/base_async_work/include/netstack_base_async_work.h +++ b/utils/napi_utils/include/base_async_work.h @@ -20,8 +20,8 @@ #include "napi/native_api.h" #include "napi/native_common.h" -#include "netstack_base_context.h" -#include "netstack_napi_utils.h" +#include "base_context.h" +#include "napi_utils.h" #include "nocopyable.h" static constexpr const int PARSE_PARAM_FAILED = -1; diff --git a/utils/base_context/include/netstack_base_context.h b/utils/napi_utils/include/base_context.h similarity index 98% rename from utils/base_context/include/netstack_base_context.h rename to utils/napi_utils/include/base_context.h index 9a8cc5a17..854ed7019 100644 --- a/utils/base_context/include/netstack_base_context.h +++ b/utils/napi_utils/include/base_context.h @@ -24,7 +24,7 @@ #include "napi/native_api.h" #include "napi/native_common.h" -#include "netstack_event_manager.h" +#include "event_manager.h" #include "nocopyable.h" #include "node_api_types.h" diff --git a/utils/event_manager/include/netstack_event_listener.h b/utils/napi_utils/include/event_listener.h similarity index 100% rename from utils/event_manager/include/netstack_event_listener.h rename to utils/napi_utils/include/event_listener.h diff --git a/utils/event_manager/include/netstack_event_manager.h b/utils/napi_utils/include/event_manager.h similarity index 98% rename from utils/event_manager/include/netstack_event_manager.h rename to utils/napi_utils/include/event_manager.h index b06e830a7..7bd6c3889 100644 --- a/utils/event_manager/include/netstack_event_manager.h +++ b/utils/napi_utils/include/event_manager.h @@ -23,7 +23,7 @@ #include #include "napi/native_api.h" -#include "netstack_event_listener.h" +#include "event_listener.h" #include "uv.h" namespace OHOS::NetStack { diff --git a/utils/module_template/include/netstack_module_template.h b/utils/napi_utils/include/module_template.h similarity index 98% rename from utils/module_template/include/netstack_module_template.h rename to utils/napi_utils/include/module_template.h index a35f2ed6f..b5a3241a9 100644 --- a/utils/module_template/include/netstack_module_template.h +++ b/utils/napi_utils/include/module_template.h @@ -24,9 +24,9 @@ #include "napi/native_api.h" #include "napi/native_api.h" #include "napi/native_common.h" -#include "netstack_base_context.h" +#include "base_context.h" #include "netstack_log.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" namespace OHOS::NetStack { class EventManager; } diff --git a/utils/napi_utils/include/netstack_napi_utils.h b/utils/napi_utils/include/napi_utils.h similarity index 92% rename from utils/napi_utils/include/netstack_napi_utils.h rename to utils/napi_utils/include/napi_utils.h index 6c5872ee1..6907e521c 100644 --- a/utils/napi_utils/include/netstack_napi_utils.h +++ b/utils/napi_utils/include/napi_utils.h @@ -103,6 +103,13 @@ void DefineProperties(napi_env env, napi_value object, const std::initializer_list &properties); +/* array */ +napi_value CreateArray(napi_env env, size_t length); +void SetArrayElement(napi_env env, napi_value array, uint32_t index, napi_value value); +bool IsArray(napi_env env, napi_value value); +uint32_t GetArrayLength(napi_env env, napi_value arr); +napi_value GetArrayElement(napi_env env, napi_value arr, uint32_t index); + /* JSON */ napi_value JsonStringify(napi_env env, napi_value object); diff --git a/utils/base_context/src/netstack_base_context.cpp b/utils/napi_utils/src/base_context.cpp similarity index 96% rename from utils/base_context/src/netstack_base_context.cpp rename to utils/napi_utils/src/base_context.cpp index 5f163d6d0..0c81709a7 100644 --- a/utils/base_context/src/netstack_base_context.cpp +++ b/utils/napi_utils/src/base_context.cpp @@ -13,12 +13,12 @@ * limitations under the License. */ -#include "netstack_base_context.h" +#include "base_context.h" #include "napi/native_api.h" #include "napi/native_common.h" -#include "netstack_event_manager.h" -#include "netstack_napi_utils.h" +#include "event_manager.h" +#include "napi_utils.h" #include "node_api.h" namespace OHOS::NetStack { @@ -177,4 +177,4 @@ void BaseContext::ParseParams(napi_value *params, size_t paramsCount) } SetParseOK(true); } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/utils/event_manager/src/netstack_event_listener.cpp b/utils/napi_utils/src/event_listener.cpp similarity index 98% rename from utils/event_manager/src/netstack_event_listener.cpp rename to utils/napi_utils/src/event_listener.cpp index c9c70ab6f..c5c817eb6 100644 --- a/utils/event_manager/src/netstack_event_listener.cpp +++ b/utils/napi_utils/src/event_listener.cpp @@ -13,11 +13,11 @@ * limitations under the License. */ -#include "netstack_event_listener.h" +#include "event_listener.h" #include "napi/native_api.h" #include "napi/native_common.h" -#include "netstack_napi_utils.h" +#include "napi_utils.h" namespace OHOS::NetStack { EventListener::EventListener(napi_env env, std::string type, napi_value callback, bool once, bool asyncCallback) diff --git a/utils/event_manager/src/netstack_event_manager.cpp b/utils/napi_utils/src/event_manager.cpp similarity index 98% rename from utils/event_manager/src/netstack_event_manager.cpp rename to utils/napi_utils/src/event_manager.cpp index af2e1428d..005531032 100644 --- a/utils/event_manager/src/netstack_event_manager.cpp +++ b/utils/napi_utils/src/event_manager.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "netstack_event_manager.h" +#include "event_manager.h" #include @@ -111,4 +111,4 @@ UvWorkWrapper::UvWorkWrapper(void *theData, napi_env theEnv, std::string eventTy : data(theData), env(theEnv), type(std::move(eventType)), manager(eventManager) { } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/utils/module_template/src/netstack_module_template.cpp b/utils/napi_utils/src/module_template.cpp similarity index 98% rename from utils/module_template/src/netstack_module_template.cpp rename to utils/napi_utils/src/module_template.cpp index 42d170f52..9a5cc190e 100644 --- a/utils/module_template/src/netstack_module_template.cpp +++ b/utils/napi_utils/src/module_template.cpp @@ -13,14 +13,14 @@ * limitations under the License. */ -#include "netstack_module_template.h" +#include "module_template.h" #include #include #include #include -#include "netstack_event_manager.h" +#include "event_manager.h" #include "netstack_log.h" namespace OHOS::NetStack::ModuleTemplate { @@ -175,4 +175,4 @@ napi_value NewInstanceNoManager(napi_env env, napi_callback_info info, const std return result; } -} // namespace OHOS::NetStack::ModuleTemplate \ No newline at end of file +} // namespace OHOS::NetStack::ModuleTemplate diff --git a/utils/napi_utils/src/netstack_napi_utils.cpp b/utils/napi_utils/src/napi_utils.cpp similarity index 92% rename from utils/napi_utils/src/netstack_napi_utils.cpp rename to utils/napi_utils/src/napi_utils.cpp index c07b8740a..0f5345a06 100644 --- a/utils/napi_utils/src/netstack_napi_utils.cpp +++ b/utils/napi_utils/src/napi_utils.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "netstack_napi_utils.h" +#include "napi_utils.h" #include #include @@ -28,7 +28,7 @@ #include "napi/native_api.h" #include "napi/native_common.h" #include "node_api.h" -#include "netstack_base_context.h" +#include "base_context.h" namespace OHOS::NetStack::NapiUtils { static constexpr const int MAX_STRING_LENGTH = 65536; @@ -335,6 +335,46 @@ void DefineProperties(napi_env env, (void)napi_define_properties(env, object, properties.size(), descriptors); } +/* array */ +napi_value CreateArray(napi_env env, size_t length) +{ + if (length == 0) { + napi_value res = nullptr; + NAPI_CALL(env, napi_create_array(env, &res)); + return res; + } + napi_value res = nullptr; + NAPI_CALL(env, napi_create_array_with_length(env, length, &res)); + return res; +} + +void SetArrayElement(napi_env env, napi_value array, uint32_t index, napi_value value) +{ + (void)napi_set_element(env, array, index, value); +} + +bool IsArray(napi_env env, napi_value value) +{ + bool result = false; + NAPI_CALL_BASE(env, napi_is_array(env, value, &result), false); + return result; +} + +uint32_t GetArrayLength(napi_env env, napi_value arr) +{ + uint32_t arrayLength = 0; + NAPI_CALL_BASE(env, napi_get_array_length(env, arr, &arrayLength), 0); + return arrayLength; +} + +napi_value GetArrayElement(napi_env env, napi_value arr, uint32_t index) +{ + napi_value elementValue = nullptr; + NAPI_CALL(env, napi_get_element(env, arr, index, &elementValue)); + return elementValue; +} + + /* JSON */ napi_value JsonStringify(napi_env env, napi_value object) { -- Gitee