diff --git a/OAT.xml b/OAT.xml new file mode 100644 index 0000000000000000000000000000000000000000..eac93366c50c624c6c5edbaf40f91ffb0192cdc3 --- /dev/null +++ b/OAT.xml @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/frameworks/js/builtin/.clang-format b/frameworks/js/builtin/.clang-format index d329bf450c7a2a08b85b287e25c6a39395888d40..a7eb9003346bededc3c50151a4089a0c58392523 100644 --- a/frameworks/js/builtin/.clang-format +++ b/frameworks/js/builtin/.clang-format @@ -1,3 +1,16 @@ +# 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. + Language: Cpp # BasedOnStyle: LLVM # 访问说明符(public、private等)的偏移 diff --git a/frameworks/js/builtin/fetch_module.cpp b/frameworks/js/builtin/fetch_module.cpp index b40eb10b0f8f5af8f8e539a1957db0aa25abc527..bc76798899c62761645faf7599e090118b13089d 100644 --- a/frameworks/js/builtin/fetch_module.cpp +++ b/frameworks/js/builtin/fetch_module.cpp @@ -41,6 +41,9 @@ const char *const FetchModule::HTTP_API_KEY_STRING_TO_ARRAY_BUFFER = "stringToAr JSIValue FetchModule::Fetch(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) { + if (!args) { + return JSI::CreateUndefined(); + } if (argsNum < 1) { return JSI::CreateUndefined(); } @@ -64,13 +67,13 @@ JSIValue FetchModule::Fetch(const JSIValue thisVal, const JSIValue *args, uint8_ bool FetchModule::JsObjectToRequestData(JSIValue options, RequestData *req) { - if (options == nullptr || JSI::ValueIsUndefined(options) || !JSI::ValueIsObject(options)) { + if (!req || !options || JSI::ValueIsUndefined(options) || !JSI::ValueIsObject(options)) { return false; } std::unique_ptr urlString( JSI::GetStringProperty(options, HttpConstant::KEY_HTTP_REQUEST_URL), FreeString); - if (urlString == nullptr) { + if (!urlString) { return false; } HTTP_REQUEST_INFO("request url is ..."); @@ -94,8 +97,11 @@ bool FetchModule::JsObjectToRequestData(JSIValue options, RequestData *req) void FetchModule::GetNameValue(JSIValue nv, std::map &map) { + if (!nv || map.empty()) { + return; + } std::unique_ptr keys(JSI::GetObjectKeys(nv), JSI::ReleaseValue); - if (keys == nullptr || JSI::ValueIsUndefined(keys.get()) || !JSI::ValueIsArray(keys.get())) { + if (!keys || JSI::ValueIsUndefined(keys.get()) || !JSI::ValueIsArray(keys.get())) { HTTP_REQUEST_ERROR("get name value failed"); return; } @@ -104,18 +110,18 @@ void FetchModule::GetNameValue(JSIValue nv, std::map & for (uint32_t index = 0; index < length; ++index) { std::unique_ptr key(JSI::GetPropertyByIndex(keys.get(), index), JSI::ReleaseValue); - if (key == nullptr || JSI::ValueIsUndefined(key.get()) || !JSI::ValueIsString(key.get())) { + if (!key || JSI::ValueIsUndefined(key.get()) || !JSI::ValueIsString(key.get())) { continue; } std::unique_ptr keyStr(JSI::ValueToString(key.get()), FreeString); - if (keyStr == nullptr) { + if (!keyStr) { HTTP_REQUEST_ERROR("key to string failed"); continue; } std::unique_ptr valStr(JSI::GetStringProperty(nv, keyStr.get()), FreeString); - if (valStr == nullptr) { + if (!valStr) { HTTP_REQUEST_ERROR("get val failed"); continue; } @@ -126,9 +132,12 @@ void FetchModule::GetNameValue(JSIValue nv, std::map & void FetchModule::GetRequestBody(JSIValue options, RequestData *requestData) { + if (!requestData) { + return; + } std::unique_ptr body( JSI::GetNamedProperty(options, HttpConstant::KEY_HTTP_REQUEST_DATA), JSI::ReleaseValue); - if (body == nullptr || JSI::ValueIsUndefined(body.get())) { + if (!body || JSI::ValueIsUndefined(body.get())) { HTTP_REQUEST_ERROR("get body failed"); return; } @@ -137,7 +146,7 @@ void FetchModule::GetRequestBody(JSIValue options, RequestData *requestData) size_t size = 0; std::unique_ptr bodyStr(JSI::ValueToStringWithBufferSize(body.get(), size), FreeString); - if (bodyStr == nullptr) { + if (!bodyStr) { HTTP_REQUEST_ERROR("get body str failed"); return; } @@ -149,7 +158,7 @@ void FetchModule::GetRequestBody(JSIValue options, RequestData *requestData) if (JSI::ValueIsObject(body.get())) { std::unique_ptr jsonString(JSI::JsonStringify(body.get()), FreeString); - if (jsonString == nullptr) { + if (!jsonString) { return; } requestData->SetBody(jsonString.get()); @@ -163,10 +172,13 @@ void FetchModule::GetRequestBody(JSIValue options, RequestData *requestData) void FetchModule::ParseExtraData(JSIValue options, RequestData *requestData) { + if (!requestData) { + return; + } if (MethodForGet(requestData->GetMethod())) { std::unique_ptr extraData( JSI::GetNamedProperty(options, HttpConstant::KEY_HTTP_REQUEST_DATA), JSI::ReleaseValue); - if (extraData == nullptr || JSI::ValueIsUndefined(extraData.get())) { + if (!extraData || JSI::ValueIsUndefined(extraData.get())) { return; } @@ -180,12 +192,12 @@ void FetchModule::ParseExtraData(JSIValue options, RequestData *requestData) if (JSI::ValueIsString(extraData.get())) { std::unique_ptr dataStr(JSI::ValueToString(extraData.get()), FreeString); - if (dataStr == nullptr) { + if (!dataStr) { return; } - std::string extraParam(dataStr.get()); + std::string extraParam2(dataStr.get()); - requestData->SetUrl(MakeUrl(url, param, extraParam)); + requestData->SetUrl(MakeUrl(url, param, extraParam2)); return; } @@ -216,31 +228,34 @@ std::string FetchModule::GetMethodFromOptions(JSIValue options) { std::unique_ptr value( JSI::GetNamedProperty(options, HttpConstant::KEY_HTTP_REQUEST_METHOD), JSI::ReleaseValue); - if (value == nullptr || JSI::ValueIsUndefined(value.get()) || !JSI::ValueIsString(value.get())) { + if (!value || JSI::ValueIsUndefined(value.get()) || !JSI::ValueIsString(value.get())) { return HttpConstant::HTTP_METHOD_GET; } std::unique_ptr methodStr(JSI::ValueToString(value.get()), FreeString); - return methodStr == nullptr ? HttpConstant::HTTP_METHOD_GET : methodStr.get(); + return !methodStr ? HttpConstant::HTTP_METHOD_GET : methodStr.get(); } JSIValue FetchModule::StringToArrayBuffer(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) { (void)thisVal; + if (!args) { + return JSI::CreateUndefined(); + } - if (argsNum < 1 || args[0] == nullptr || JSI::ValueIsUndefined(args[0]) || !JSI::ValueIsString(args[0])) { + if (argsNum < 1 || !args[0] || JSI::ValueIsUndefined(args[0]) || !JSI::ValueIsString(args[0])) { return JSI::CreateUndefined(); } size_t size = 0; std::unique_ptr str(JSI::ValueToStringWithBufferSize(args[0], size), FreeString); - if (str == nullptr || size == 0) { + if (!str || !size) { return JSI::CreateUndefined(); } uint8_t *buffer = nullptr; JSIValue arrayBuffer = JSI::CreateArrayBuffer(size, buffer); - if (buffer == nullptr || arrayBuffer == nullptr) { + if (!buffer || !arrayBuffer) { return JSI::CreateUndefined(); } @@ -252,13 +267,13 @@ std::string FetchModule::GetResponseTypeFromOptions(JSIValue options) { std::unique_ptr value( JSI::GetNamedProperty(options, HttpConstant::KEY_HTTP_REQUEST_RESPONSE_TYPE), JSI::ReleaseValue); - if (value == nullptr || JSI::ValueIsUndefined(value.get()) || !JSI::ValueIsString(value.get())) { + if (!value || JSI::ValueIsUndefined(value.get()) || !JSI::ValueIsString(value.get())) { return ""; } std::unique_ptr responseType(JSI::ValueToString(value.get()), FreeString); - return responseType == nullptr ? "" : responseType.get(); + return !responseType ? "" : responseType.get(); } } // namespace ACELite -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/frameworks/js/builtin/fetch_module.h b/frameworks/js/builtin/fetch_module.h index 98aba9f999143ce9da6b4e130527a281122812d3..5915330dba93e4ab4cca3dc739cac218c6b037bb 100644 --- a/frameworks/js/builtin/fetch_module.h +++ b/frameworks/js/builtin/fetch_module.h @@ -31,8 +31,26 @@ public: ~FetchModule() = default; + /** + * @brief string to array buffer + * + * @param thisVal thisVal + * @param args *args + * @param argsNum argsNum + * + * @return array buffer + */ static JSIValue StringToArrayBuffer(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + /** + * @brief Asynchronous callback response callback + * + * @param thisVal thisVal + * @param args *args + * @param argsNum argsNum + * + * @return + */ static JSIValue Fetch(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); public: @@ -50,4 +68,4 @@ private: } // namespace ACELite } // namespace OHOS -#endif /* OHOS_ACELITE_FETCH_MODULE_H */ +#endif // OHOS_ACELITE_FETCH_MODULE_H diff --git a/frameworks/js/builtin/http_request/http_async_callback.cpp b/frameworks/js/builtin/http_request/http_async_callback.cpp index bb3fc5aa96d684001cf58cdac573eb115dd17bbd..d81641e5e642f770bb25c791154c07683abf6400 100644 --- a/frameworks/js/builtin/http_request/http_async_callback.cpp +++ b/frameworks/js/builtin/http_request/http_async_callback.cpp @@ -31,7 +31,7 @@ void HttpAsyncCallback::AsyncExecHttpRequest(void *data) { std::unique_ptr asyncCallback( static_cast(data), HttpAsyncCallback::AsyncCallbackDeleter); - if (asyncCallback == nullptr) { + if (!asyncCallback) { return; } @@ -48,6 +48,9 @@ void HttpAsyncCallback::AsyncExecHttpRequest(void *data) void HttpAsyncCallback::AsyncCallbackDeleter(HttpAsyncCallback *asyncCallback) { + if (!asyncCallback) { + return; + } if (asyncCallback->responseCallback[CB_SUCCESS]) { JSI::ReleaseValue(asyncCallback->responseCallback[CB_SUCCESS]); } @@ -63,7 +66,7 @@ void HttpAsyncCallback::AsyncCallbackDeleter(HttpAsyncCallback *asyncCallback) JSIValue HttpAsyncCallback::ResponseDataToJsValue(const ResponseData &responseData) { JSIValue object = JSI::CreateObject(); - if (object == nullptr) { + if (!object) { return nullptr; } @@ -85,7 +88,7 @@ JSIValue HttpAsyncCallback::ResponseDataToJsValue(const ResponseData &responseDa } std::unique_ptr headers(JSI::CreateObject(), JSI::ReleaseValue); - if (headers == nullptr) { + if (!headers) { JSI::ReleaseValue(object); return JSI::CreateUndefined(); } @@ -100,39 +103,45 @@ JSIValue HttpAsyncCallback::ResponseDataToJsValue(const ResponseData &responseDa void HttpAsyncCallback::OnSuccess(const ResponseData &responseData) { JSIValue success = responseCallback[CB_SUCCESS]; - if (success == nullptr || JSI::ValueIsUndefined(success) || !JSI::ValueIsFunction(success)) { + if (!success || JSI::ValueIsUndefined(success) || !JSI::ValueIsFunction(success)) { return; } std::unique_ptr obj(ResponseDataToJsValue(responseData), JSI::ReleaseValue); - if (obj == nullptr || JSI::ValueIsUndefined(obj.get()) || !JSI::ValueIsObject(obj.get())) { + if (!obj || JSI::ValueIsUndefined(obj.get()) || !JSI::ValueIsObject(obj.get())) { return; } JSIValue arg[ARGC_ONE] = {obj.get()}; - JSI::CallFunction(success, thisVal, arg, ARGC_ONE); + if (thisVal) { + JSI::CallFunction(success, thisVal, arg, ARGC_ONE); + } } void HttpAsyncCallback::OnFail(const char *errData, int32_t errCode) { JSIValue fail = responseCallback[CB_FAIL]; - if (fail == nullptr || JSI::ValueIsUndefined(fail) || !JSI::ValueIsFunction(fail)) { + if (!fail || JSI::ValueIsUndefined(fail) || !JSI::ValueIsFunction(fail)) { return; } std::unique_ptr errInfo(JSI::CreateString(errData), JSI::ReleaseValue); std::unique_ptr retCode(JSI::CreateNumber(errCode), JSI::ReleaseValue); JSIValue argv[ARGC_TWO] = {errInfo.get(), retCode.get()}; - JSI::CallFunction(fail, thisVal, argv, ARGC_TWO); + if (thisVal) { + JSI::CallFunction(fail, thisVal, argv, ARGC_TWO); + } } void HttpAsyncCallback::OnComplete() { JSIValue complete = responseCallback[CB_COMPLETE]; - if (complete == nullptr || JSI::ValueIsUndefined(complete) || !JSI::ValueIsFunction(complete)) { + if (!complete || JSI::ValueIsUndefined(complete) || !JSI::ValueIsFunction(complete)) { return; } - JSI::CallFunction(complete, thisVal, nullptr, 0); + if (thisVal) { + JSI::CallFunction(complete, thisVal, nullptr, 0); + } } } // namespace ACELite diff --git a/frameworks/js/builtin/http_request/http_async_callback.h b/frameworks/js/builtin/http_request/http_async_callback.h index 944c68e9c4bd189652519cb2a0ad19fec0749677..e291b0c1116fa6b7836827f37402ea723c7af2a1 100644 --- a/frameworks/js/builtin/http_request/http_async_callback.h +++ b/frameworks/js/builtin/http_request/http_async_callback.h @@ -31,7 +31,24 @@ public: explicit HttpAsyncCallback(JSIValue thisValue); public: + /** + * @brief Execute HTTP requests asynchronously + * + * @param data data + * + * @return null + */ static void AsyncExecHttpRequest(void *data); + + /** + * @brief Asynchronous callback response callback + * + * @param thisVal thisVal + * @param args *args + * @param argsNum argsNum + * + * @return + */ friend JSIValue FetchModule::Fetch(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); private: @@ -50,4 +67,4 @@ private: } // namespace ACELite } // namespace OHOS -#endif /* OHOS_ACELITE_HTTP_ASYNC_CALLBACK_H */ +#endif // OHOS_ACELITE_HTTP_ASYNC_CALLBACK_H diff --git a/frameworks/js/builtin/http_request/http_constant.h b/frameworks/js/builtin/http_request/http_constant.h index 75be041d6392a1fd222f0918cd9571314e2ad2f6..dbff6fddb0555574838631ad3321a32fd7149c92 100644 --- a/frameworks/js/builtin/http_request/http_constant.h +++ b/frameworks/js/builtin/http_request/http_constant.h @@ -61,4 +61,4 @@ public: } // namespace ACELite } // namespace OHOS -#endif /* OHOS_ACELITE_HTTP_CONSTANT_H */ +#endif // OHOS_ACELITE_HTTP_CONSTANT_H diff --git a/frameworks/js/builtin/http_request/http_request.cpp b/frameworks/js/builtin/http_request/http_request.cpp index b1673c39047665d006ba7c7431756bbe110b0a0b..fa86cb98717d07c0c16044429bc2d088ab8c7291 100644 --- a/frameworks/js/builtin/http_request/http_request.cpp +++ b/frameworks/js/builtin/http_request/http_request.cpp @@ -79,6 +79,9 @@ bool HttpRequest::Initialize() bool HttpRequest::Request(RequestData *requestData, ResponseData *responseData) { + if (!requestData || !responseData) { + return false; + } if (!Initialize()) { return false; } @@ -130,6 +133,9 @@ bool HttpRequest::Request(RequestData *requestData, ResponseData *responseData) bool HttpRequest::SetOption(RequestData *requestData, CURL *curl, ResponseData *responseData) { + if (!requestData || !curl || !responseData) { + return false; + } const std::string &method = requestData->GetMethod(); if (!MethodForGet(method) && !MethodForPost(method)) { HTTP_REQUEST_ERROR("method %s not supported", method.c_str()); @@ -157,14 +163,22 @@ bool HttpRequest::SetOption(RequestData *requestData, CURL *curl, ResponseData * size_t HttpRequest::OnWritingMemoryBody(const void *data, size_t size, size_t memBytes, void *userData) { - static_cast(userData)->AppendData(static_cast(data), size * memBytes); - return size * memBytes; + if (!data || !userData) { + return 0; + } + static_cast(userData)->AppendData( + static_cast(data), + static_cast(size * memBytes)); + return static_cast(size * memBytes); } size_t HttpRequest::OnWritingMemoryHeader(const void *data, size_t size, size_t memBytes, void *userData) { - static_cast(userData)->append(static_cast(data), size * memBytes); - return size * memBytes; + if (!data || !userData) { + return 0; + } + static_cast(userData)->append(static_cast(data), static_cast(size * memBytes)); + return static_cast(size * memBytes); } struct curl_slist *HttpRequest::MakeHeaders(const std::vector &vec) diff --git a/frameworks/js/builtin/http_request/http_request.h b/frameworks/js/builtin/http_request/http_request.h index 4831942868eaaf4ff26201c3f9f1bed7d299ffc2..7d427c98669de4ae28c5a90cb542fdef98447bac 100644 --- a/frameworks/js/builtin/http_request/http_request.h +++ b/frameworks/js/builtin/http_request/http_request.h @@ -35,6 +35,14 @@ public: ~HttpRequest() = default; + /** + * @brief Request + * + * @param requestData requestData + * @param responseData responseData + * + * @return whether the request was successful + */ static bool Request(RequestData *requestData, ResponseData *responseData); private: @@ -57,4 +65,4 @@ private: } // namespace ACELite } // namespace OHOS -#endif /* OHOS_ACELITE_HTTP_REQUEST_H */ +#endif // OHOS_ACELITE_HTTP_REQUEST_H diff --git a/frameworks/js/builtin/http_request/http_request_utils.cpp b/frameworks/js/builtin/http_request/http_request_utils.cpp index 7e029f1a47b9cfabbde7a6637d249e65db823958..e815e588879a17982690646d60b975f475139dc0 100644 --- a/frameworks/js/builtin/http_request/http_request_utils.cpp +++ b/frameworks/js/builtin/http_request/http_request_utils.cpp @@ -23,6 +23,9 @@ namespace OHOS { namespace ACELite { std::vector Split(const std::string &str, const std::string &sep) { + if (sep.empty()) { + return {str}; + } std::string s = str; std::vector res; while (!s.empty()) { @@ -39,6 +42,9 @@ std::vector Split(const std::string &str, const std::string &sep) std::string Strip(const std::string &str, char ch) { + if (str.empty()) { + return ""; + } int64_t i = 0; while (i < str.size() && str[i] == ch) { ++i; @@ -95,11 +101,11 @@ bool EncodeUrlParam(CURL *curl, std::string &url) std::unique_ptr encodeOut( curl_easy_escape(curl, param.c_str(), static_cast(strlen(param.c_str()))), curl_free); - if (encodeOut == nullptr || strlen(encodeOut.get()) == 0) { + if (!encodeOut || !strlen(encodeOut.get())) { return false; } url = url.substr(0, index) + HttpConstant::HTTP_URL_PARAM_SEPARATOR + encodeOut.get(); return true; } } // namespace ACELite -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/frameworks/js/builtin/http_request/http_request_utils.h b/frameworks/js/builtin/http_request/http_request_utils.h index a626511249d7d9fefeec6a1480e5c1d0ae0c23df..f8f6a08d5867744efcef02e8e23f94dbace2d746 100644 --- a/frameworks/js/builtin/http_request/http_request_utils.h +++ b/frameworks/js/builtin/http_request/http_request_utils.h @@ -49,4 +49,4 @@ bool EncodeUrlParam(CURL *curl, std::string &url); } // namespace ACELite } // namespace OHOS -#endif /* OHOS_ACELITE_HTTP_REQUEST_UTILS_H */ +#endif // OHOS_ACELITE_HTTP_REQUEST_UTILS_H diff --git a/frameworks/js/builtin/http_request/request_data.h b/frameworks/js/builtin/http_request/request_data.h index 35d742612071959860376f21280f69d0b4a8fadb..87488879a6d346e5f33fedd03293ccb1830584af 100644 --- a/frameworks/js/builtin/http_request/request_data.h +++ b/frameworks/js/builtin/http_request/request_data.h @@ -25,24 +25,95 @@ class RequestData { public: RequestData(); + /** + * @brief Set member variable url + * + * @param urlPara urlPara + * + * @return + */ void SetUrl(const std::string &urlPara); + /** + * @brief Set member variable method + * + * @param methodPara methodPara + * + * @return + */ void SetMethod(const std::string &methodPara); + /** + * @brief Set member variable header + * + * @param key key + * @param val val + * + * @return + */ void SetHeader(const std::string &key, const std::string &val); + /** + * @brief Set member variable body + * + * @param bodyPara bodyPara + * + * @return + */ void SetBody(const std::string &bodyPara); + /** + * @brief Set member variable responseType + * + * @param respType respType + * + * @return + */ void SetResponseType(const std::string &respType); + /** + * @brief Get member variable url + * + * @param + * + * @return url + */ [[nodiscard]] const std::string &GetUrl() const; + /** + * @brief Get member variable header + * + * @param + * + * @return header + */ [[nodiscard]] const std::map &GetHeader() const; + /** + * @brief Get member variable method + * + * @param + * + * @return method + */ [[nodiscard]] const std::string &GetMethod() const; + /** + * @brief Get member variable body + * + * @param + * + * @return body + */ [[nodiscard]] const std::string &GetBody() const; + /** + * @brief Get member variable responseType + * + * @param + * + * @return responseType + */ [[nodiscard]] const std::string &GetResponseType() const; private: @@ -55,4 +126,4 @@ private: } // namespace ACELite } // namespace OHOS -#endif /* OHOS_ACELITE_REQUEST_DATA_H */ +#endif // OHOS_ACELITE_REQUEST_DATA_H diff --git a/frameworks/js/builtin/http_request/response_data.cpp b/frameworks/js/builtin/http_request/response_data.cpp index ff49d44c01df44788ceb156429606640d9e988ac..e05f1f8968b35332b79d534281952f91fef1b555 100644 --- a/frameworks/js/builtin/http_request/response_data.cpp +++ b/frameworks/js/builtin/http_request/response_data.cpp @@ -39,6 +39,9 @@ void ResponseData::SetErrString(const std::string &err) void ResponseData::ParseHeaders(const std::string &headersStr) { std::vector vec = Split(headersStr, "\n"); + if (vec.empty()) { + return; + } for (auto header : vec) { header = Strip(Strip(header), '\r'); if (header.empty()) { diff --git a/frameworks/js/builtin/http_request/response_data.h b/frameworks/js/builtin/http_request/response_data.h index ecea2075a251adf98df18a3204948755d770be61..71874f93aa962644a30786feb4e47c8e876d1a5d 100644 --- a/frameworks/js/builtin/http_request/response_data.h +++ b/frameworks/js/builtin/http_request/response_data.h @@ -25,22 +25,88 @@ class ResponseData { public: ResponseData(); + /** + * @brief Set member variable code + * + * @param thisVal thisVal + * @param codePara codePara + * + * @return + */ void SetCode(int32_t codePara); + /** + * @brief Set member variable errString + * + * @param thisVal thisVal + * @param err err + * + * @return + */ void SetErrString(const std::string &err); + /** + * @brief Set member variable headers + * + * @param headersStr headersStr + * + * @return + */ void ParseHeaders(const std::string &headersStr); + /** + * @brief Set member variable data + * + * @param dataPara dataPara + * @param size size + * + * @return + */ void AppendData(const char *dataPara, size_t size); + /** + * @brief Get member variable code + * + * @param + * + * @return code + */ [[nodiscard]] int32_t GetCode() const; + /** + * @brief Get member variable errString + * + * @param + * + * @return errString + */ [[nodiscard]] const std::string &GetErrString() const; + /** + * @brief Get member variable data + * + * @param + * + * @return data + */ [[nodiscard]] const std::string &GetData() const; + /** + * @brief Get member variable statusLine + * + * @param + * + * @return statusLine + */ [[nodiscard]] const std::string &GetStatusLine() const; + /** + * @brief Get member variable headers + * + * @param + * + * @return headers + */ [[nodiscard]] const std::map &GetHeaders() const; private: @@ -53,4 +119,4 @@ private: } // namespace ACELite } // namespace OHOS -#endif /* OHOS_ACELITE_RESPONSE_DATA_H */ +#endif // OHOS_ACELITE_RESPONSE_DATA_H diff --git a/frameworks/js/builtin/test/CMakeLists.txt b/frameworks/js/builtin/test/CMakeLists.txt index dacebb3a302a4538e83345a798f7637a8f6ffe9a..5bfed1fefd7445d9c9c8a8c8991d1ee70f9096a7 100644 --- a/frameworks/js/builtin/test/CMakeLists.txt +++ b/frameworks/js/builtin/test/CMakeLists.txt @@ -1,3 +1,18 @@ +/* + * 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. + */ + add_executable(test_fetch_module_exec test_fetch_module.cpp) -target_link_libraries(test_fetch_module_exec test_fetch_module) \ No newline at end of file +target_link_libraries(test_fetch_module_exec test_fetch_module) diff --git a/frameworks/js/builtin/test/include/ace_log.h b/frameworks/js/builtin/test/include/ace_log.h index c2c3d7ac7ffffd17ebaac0c904900452d12183eb..9c2c8cc0f0222416261477d6f826a54c5cc61883 100644 --- a/frameworks/js/builtin/test/include/ace_log.h +++ b/frameworks/js/builtin/test/include/ace_log.h @@ -38,7 +38,7 @@ typedef enum { HILOG_MODULE_ACE = 1 } HiLogModuleType; #define HILOG_WARN(mod, format, ...) \ do { \ - if (strcmp(format, "todo call linux putmsg interface here!") == 0) { \ + if (!(strcmp(format, "call linux putmsg interface here!"))) { \ OHOS::ACELite::TestPutMessage(const_cast(msgPtr)); \ return MSGQ_OK; \ } \ diff --git a/frameworks/js/builtin/test/test_fetch_module.cpp b/frameworks/js/builtin/test/test_fetch_module.cpp index 623de739ee5c04201b26fa16e72124659ff55954..31db5e44c46160040e67224072c30ac015b3394c 100644 --- a/frameworks/js/builtin/test/test_fetch_module.cpp +++ b/frameworks/js/builtin/test/test_fetch_module.cpp @@ -64,6 +64,10 @@ public: void TestPutMessage(void *data) { + if (!data) { + return; + } + auto msg = static_cast(data); auto asyncWork = static_cast(msg->data); asyncWork->workHandler(asyncWork->data); @@ -74,6 +78,9 @@ JSIValue TestCallbackOnSuccess(const JSIValue thisVal, const JSIValue *args, uin FUNC_BEGIN(); (void)thisVal; (void)argsNum; + if (!args) { + return nullptr; + } JSIValue para = args[0]; HTTP_REQUEST_INFO("code = %d", @@ -81,9 +88,12 @@ JSIValue TestCallbackOnSuccess(const JSIValue thisVal, const JSIValue *args, uin size_t size = 0; char *data = JSI::GetStringPropertyWithBufferSize(para, HttpConstant::KEY_HTTP_RESPONSE_DATA, size); + if (!data) { + return nullptr; + } std::string body; for (uint32_t index = 0; index < size; ++index) { - if (data[index] != 0) { + if (data[index]) { body += data[index]; } else { body += "0"; @@ -111,6 +121,9 @@ JSIValue TestCallbackOnFail(const JSIValue thisVal, const JSIValue *args, uint8_ FUNC_BEGIN(); (void)thisVal; (void)argsNum; + if (!args) { + return nullptr; + } HTTP_REQUEST_INFO("err = %s", JSI::ValueToString(args[0])); HTTP_REQUEST_INFO("code = %d", static_cast(JSI::ValueToNumber(args[1]))); @@ -137,7 +150,7 @@ void TestHttpModuleMethodAndHeaderByDefault() FUNC_BEGIN(); JSIValue object = JSI::CreateObject(); - if (object == nullptr) { + if (!object) { return; } 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 e8bf9d066fae4f54e4c90e6c5c96c4d1c6491d69..e18e1bedad4b57e332b0cbfb52cc617870b5b462 100644 --- a/frameworks/js/napi/fetch/async_context/include/fetch_context.h +++ b/frameworks/js/napi/fetch/async_context/include/fetch_context.h @@ -71,4 +71,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_FETCH_CONTEXT_H */ +#endif // COMMUNICATIONNETSTACK_FETCH_CONTEXT_H diff --git a/frameworks/js/napi/fetch/async_work/include/fetch_async_work.h b/frameworks/js/napi/fetch/async_work/include/fetch_async_work.h index 5fb3e11c4d4b08a549bcef1ad1672e6d82df37f2..c73c7246b8b033a37d34a3c88f2fb7df95e3f7a2 100644 --- a/frameworks/js/napi/fetch/async_work/include/fetch_async_work.h +++ b/frameworks/js/napi/fetch/async_work/include/fetch_async_work.h @@ -32,4 +32,4 @@ public: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_FETCH_ASYNC_WORK_H */ +#endif // COMMUNICATIONNETSTACK_FETCH_ASYNC_WORK_H diff --git a/frameworks/js/napi/fetch/constant/include/constant.h b/frameworks/js/napi/fetch/constant/include/constant.h index fbfbdd36faf2b8388d0c76a4034500cc1e45d26d..9289fb2b4c9942e01e48f96aab19170adbcad661 100644 --- a/frameworks/js/napi/fetch/constant/include/constant.h +++ b/frameworks/js/napi/fetch/constant/include/constant.h @@ -67,4 +67,4 @@ public: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_CONSTANT_H */ +#endif // COMMUNICATIONNETSTACK_CONSTANT_H diff --git a/frameworks/js/napi/fetch/fetch_exec/include/fetch_exec.h b/frameworks/js/napi/fetch/fetch_exec/include/fetch_exec.h index 7014539cc0780bd39ee187a8dd661d88c6a281c2..82f5e16dac23cf28a1f20b555b0dba3b2ad96b2b 100644 --- a/frameworks/js/napi/fetch/fetch_exec/include/fetch_exec.h +++ b/frameworks/js/napi/fetch/fetch_exec/include/fetch_exec.h @@ -69,4 +69,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_FETCH_EXEC_H */ +#endif // COMMUNICATIONNETSTACK_FETCH_EXEC_H 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 c647e1eb753f77ac1b80dff85d3b13a4a6bd74de..d1cfba0e07786e550843e187c1b2bdde7d8bf396 100644 --- a/frameworks/js/napi/fetch/fetch_module/include/fetch_module.h +++ b/frameworks/js/napi/fetch/fetch_module/include/fetch_module.h @@ -30,4 +30,4 @@ private: static napi_value Fetch(napi_env env, napi_callback_info info); }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_FETCH_MODULE_H */ +#endif // COMMUNICATIONNETSTACK_FETCH_MODULE_H diff --git a/frameworks/js/napi/fetch/options/include/fetch_request.h b/frameworks/js/napi/fetch/options/include/fetch_request.h index b4f3b1185b80f471e5ab0e98a51e279ac32b2570..2a749a3a816e9f8c8db5c456a1144352369ba944 100644 --- a/frameworks/js/napi/fetch/options/include/fetch_request.h +++ b/frameworks/js/napi/fetch/options/include/fetch_request.h @@ -51,4 +51,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_FETCH_REQUEST_H */ +#endif // COMMUNICATIONNETSTACK_FETCH_REQUEST_H diff --git a/frameworks/js/napi/fetch/options/include/fetch_response.h b/frameworks/js/napi/fetch/options/include/fetch_response.h index f0647b89e83efdbed11e3ef53b3756212264429a..425024266103c7fb77843897964f630ec43da656 100644 --- a/frameworks/js/napi/fetch/options/include/fetch_response.h +++ b/frameworks/js/napi/fetch/options/include/fetch_response.h @@ -51,4 +51,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_FETCH_RESPONSE_H */ +#endif // COMMUNICATIONNETSTACK_FETCH_RESPONSE_H 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 e7e4355165edd69851b2296015922bccdd6b95d9..573ac8783a5a1aee64f6d9986ea80ee94bbfeb44 100644 --- a/frameworks/js/napi/http/async_context/include/request_context.h +++ b/frameworks/js/napi/http/async_context/include/request_context.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_REQUEST_CONTEXT_H -#define COMMUNICATIONNETSTACK_REQUEST_CONTEXT_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_ASYNC_CONTEXT_INCLUDE_UDP_SEND_CONTEXT_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_ASYNC_CONTEXT_INCLUDE_UDP_SEND_CONTEXT_H_ #include "http_request_options.h" #include "http_response.h" @@ -30,6 +30,14 @@ public: explicit RequestContext(napi_env env, EventManager *manager); + /** + * @brief Parse parameters + * + * @param params params + * @param paramsCount paramsCount + * + * @return + */ void ParseParams(napi_value *params, size_t paramsCount); HttpRequestOptions options; @@ -51,4 +59,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_REQUEST_CONTEXT_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_ASYNC_CONTEXT_INCLUDE_UDP_SEND_CONTEXT_H_ 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 7ff2a53d70b16538ae9de1a2aa3d0eb23d44b507..49f15c040ab149e1092eba70d712ae072bc5fb2e 100644 --- a/frameworks/js/napi/http/async_context/src/request_context.cpp +++ b/frameworks/js/napi/http/async_context/src/request_context.cpp @@ -34,6 +34,10 @@ RequestContext::RequestContext(napi_env env, EventManager *manager) : BaseContex void RequestContext::ParseParams(napi_value *params, size_t paramsCount) { + if (!params) { + return; + } + bool valid = CheckParamsType(params, paramsCount); if (!valid) { return; @@ -69,6 +73,10 @@ void RequestContext::ParseParams(napi_value *params, size_t paramsCount) bool RequestContext::CheckParamsType(napi_value *params, size_t paramsCount) { + if (!params) { + return false; + } + if (paramsCount == PARAM_JUST_URL) { // just url return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_string; @@ -91,25 +99,25 @@ bool RequestContext::CheckParamsType(napi_value *params, size_t paramsCount) void RequestContext::ParseNumberOptions(napi_value optionsValue) { options.SetReadTimeout(NapiUtils::GetUint32Property(GetEnv(), optionsValue, HttpConstant::PARAM_KEY_READ_TIMEOUT)); - if (options.GetReadTimeout() == 0) { + if (!(options.GetReadTimeout())) { options.SetReadTimeout(HttpConstant::DEFAULT_READ_TIMEOUT); } options.SetConnectTimeout( NapiUtils::GetUint32Property(GetEnv(), optionsValue, HttpConstant::PARAM_KEY_CONNECT_TIMEOUT)); - if (options.GetConnectTimeout() == 0) { + if (!(options.GetConnectTimeout())) { options.SetConnectTimeout(HttpConstant::DEFAULT_CONNECT_TIMEOUT); } options.SetIfModifiedSince( NapiUtils::GetUint32Property(GetEnv(), optionsValue, HttpConstant::PARAM_KEY_IF_MODIFIED_SINCE)); - if (options.GetIfModifiedSince() == 0) { + if (!(options.GetIfModifiedSince())) { options.SetIfModifiedSince(HttpConstant::DEFAULT_IF_MODIFIED_SINCE); } options.SetFixedLengthStreamingMode( NapiUtils::GetInt32Property(GetEnv(), optionsValue, HttpConstant::PARAM_KEY_FIXED_LENGTH_STREAMING_MODE)); - if (options.GetFixedLengthStreamingMode() == 0) { + if (!(options.GetFixedLengthStreamingMode())) { options.SetFixedLengthStreamingMode(HttpConstant::DEFAULT_FIXED_LENGTH_STREAMING_MODE); } } @@ -205,7 +213,7 @@ bool RequestContext::GetRequestBody(napi_value extraData) if (NapiUtils::ValueIsArrayBuffer(GetEnv(), extraData)) { size_t length = 0; void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), extraData, &length); - if (data == nullptr) { + if (!data) { return false; } options.SetBody(data, length); @@ -242,4 +250,4 @@ void RequestContext::UrlAndOptions(napi_value urlValue, napi_value optionsValue) SetParseOK(ParseExtraData(optionsValue)); } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/http/async_work/include/http_async_work.h b/frameworks/js/napi/http/async_work/include/http_async_work.h index 5bfec668e58b8c62bf229017300070dffec7a011..8890ca88d1034c34924b5ec0818e7591e4681680 100644 --- a/frameworks/js/napi/http/async_work/include/http_async_work.h +++ b/frameworks/js/napi/http/async_work/include/http_async_work.h @@ -26,10 +26,27 @@ class HttpAsyncWork final { public: ACE_DISALLOW_COPY_AND_MOVE(HttpAsyncWork); + /** + * @brief execute request + * + * @param env env + * @param data *data + * + * @return + */ static void ExecRequest(napi_env env, void *data); + /** + * @brief Request Callback + * + * @param env env + * @param status status + * @param data *data + * + * @return + */ static void RequestCallback(napi_env env, napi_status status, void *data); }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_ASYNC_WORK_H */ +#endif // COMMUNICATIONNETSTACK_ASYNC_WORK_H diff --git a/frameworks/js/napi/http/constant/include/constant.h b/frameworks/js/napi/http/constant/include/constant.h index a007695f12d2b15457fe1c7c0f8f77d3d168a6bb..279984120eecbf1a1a497446b2e180dc51f3e6b3 100644 --- a/frameworks/js/napi/http/constant/include/constant.h +++ b/frameworks/js/napi/http/constant/include/constant.h @@ -71,4 +71,4 @@ public: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_CONSTANT_H */ +#endif // COMMUNICATIONNETSTACK_CONSTANT_H diff --git a/frameworks/js/napi/http/constant/include/event_list.h b/frameworks/js/napi/http/constant/include/event_list.h index cf4150cd43a53b0642ec34a26c3d9a6be03b13b2..3fbb89b87a616f8f596eafab7c7ae01aa62e44a0 100644 --- a/frameworks/js/napi/http/constant/include/event_list.h +++ b/frameworks/js/napi/http/constant/include/event_list.h @@ -20,4 +20,4 @@ static constexpr const char *ON_HEADER_RECEIVE = "headerReceive"; static constexpr const char *ON_HEADERS_RECEIVE = "headersReceive"; -#endif /* COMMUNICATIONNETSTACK_EVENT_LIST_H */ +#endif // COMMUNICATIONNETSTACK_EVENT_LIST_H diff --git a/frameworks/js/napi/http/http_exec/include/http_exec.h b/frameworks/js/napi/http/http_exec/include/http_exec.h index d3288892ddba994cbc5ffff547a01bb9913266e4..04d1f83aee01de4032e03a7106371b4760e887d6 100644 --- a/frameworks/js/napi/http/http_exec/include/http_exec.h +++ b/frameworks/js/napi/http/http_exec/include/http_exec.h @@ -33,18 +33,69 @@ public: ~HttpExec() = default; + /** + * @brief execute request + * + * @param context *context + * + * @return Whether the execution is successful + */ static bool ExecRequest(RequestContext *context); + /** + * @brief Request Callback + * + * @param context *context + * + * @return an object + */ static napi_value RequestCallback(RequestContext *context); + /** + * @brief make url + * + * @param url url + * @param param param + * @param extraParam extraParam + * + * @return url + HttpConstant::HTTP_URL_PARAM_START + param + */ static std::string MakeUrl(const std::string &url, std::string param, const std::string &extraParam); + /** + * @brief Method For Get + * + * @param method method + * + * @return Whether Method For Get is successful + */ static bool MethodForGet(const std::string &method); + /** + * @brief Method For Post + * + * @param method method + * + * @return Whether Method For Post is successful + */ static bool MethodForPost(const std::string &method); + /** + * @brief Encode Url Param + * + * @param str str + * + * @return Whether Encode Url Param is successful + */ static bool EncodeUrlParam(std::string &str); + /** + * @brief Initialize + * + * @param + * + * @return Whether Initialize is successful + */ static bool Initialize(); private: @@ -69,4 +120,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_HTTP_REQUEST_EXEC_H */ +#endif // COMMUNICATIONNETSTACK_HTTP_REQUEST_EXEC_H 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 ef021a5338841883140c6b7eb025de9b7784a78d..57d3a53aa31d6a5d883ebf82c543789763cf02db 100644 --- a/frameworks/js/napi/http/http_exec/src/http_exec.cpp +++ b/frameworks/js/napi/http/http_exec/src/http_exec.cpp @@ -66,6 +66,10 @@ bool HttpExec::initialized_ = false; bool HttpExec::ExecRequest(RequestContext *context) { + if (!context) { + return false; + } + if (!initialized_) { NETSTACK_LOGE("curl not init"); return false; @@ -117,6 +121,9 @@ bool HttpExec::ExecRequest(RequestContext *context) napi_value HttpExec::RequestCallback(RequestContext *context) { + if (!context) { + return nullptr; + } napi_value object = NapiUtils::CreateObject(context->GetEnv()); if (NapiUtils::GetValueType(context->GetEnv(), object) != napi_object) { return nullptr; @@ -221,6 +228,9 @@ bool HttpExec::Initialize() bool HttpExec::SetOption(CURL *curl, RequestContext *context, struct curl_slist *requestHeader) { + if (!curl || !context || !requestHeader) { + return false; + } const std::string &method = context->options.GetMethod(); if (!MethodForGet(method) && !MethodForPost(method)) { NETSTACK_LOGE("method %{public}s not supported", method.c_str()); @@ -284,6 +294,9 @@ bool HttpExec::SetOption(CURL *curl, RequestContext *context, struct curl_slist size_t HttpExec::OnWritingMemoryBody(const void *data, size_t size, size_t memBytes, void *userData) { + if (!data || !userData) { + return 0; + } auto context = static_cast(userData); context->response.AppendResult(data, size * memBytes); return size * memBytes; @@ -291,6 +304,9 @@ size_t HttpExec::OnWritingMemoryBody(const void *data, size_t size, size_t memBy size_t HttpExec::OnWritingMemoryHeader(const void *data, size_t size, size_t memBytes, void *userData) { + if (!data || !userData) { + return 0; + } auto context = static_cast(userData); context->response.AppendRawHeader(data, size * memBytes); return size * memBytes; @@ -309,6 +325,9 @@ struct curl_slist *HttpExec::MakeHeaders(const std::vector &vec) napi_value HttpExec::MakeResponseHeader(RequestContext *context) { + if (!context) { + return nullptr; + } napi_value header = NapiUtils::CreateObject(context->GetEnv()); if (NapiUtils::GetValueType(context->GetEnv(), header) == napi_object) { std::for_each(context->response.GetHeader().begin(), context->response.GetHeader().end(), @@ -323,6 +342,10 @@ napi_value HttpExec::MakeResponseHeader(RequestContext *context) void HttpExec::OnHeaderReceive(RequestContext *context, napi_value header) { + if (!context) { + return; + } + napi_value undefined = NapiUtils::GetUndefined(context->GetEnv()); context->Emit(ON_HEADER_RECEIVE, std::make_pair(undefined, header)); context->Emit(ON_HEADERS_RECEIVE, std::make_pair(undefined, header)); diff --git a/frameworks/js/napi/http/http_module/include/http_module.h b/frameworks/js/napi/http/http_module/include/http_module.h index 354c2ad9795db08834da5510ea92a21f084fd630..66e4fdef37b8fab293fb50a16f83fb136f3fbb57 100644 --- a/frameworks/js/napi/http/http_module/include/http_module.h +++ b/frameworks/js/napi/http/http_module/include/http_module.h @@ -79,6 +79,14 @@ public: static constexpr const char *INTERFACE_RESPONSE_CODE = "ResponseCode"; static constexpr const char *INTERFACE_HTTP_REQUEST = "HttpRequest"; + /** + * @brief Initialize the Http module + * + * @param env env + * @param exports exports + * + * @return exports value + */ static napi_value InitHttpModule(napi_env env, napi_value exports); private: @@ -93,4 +101,4 @@ private: static void InitResponseCode(napi_env env, napi_value exports); }; } // namespace OHOS::NetStack -#endif // COMMUNICATIONNETSTACK_HTTP_MODULE_H \ No newline at end of file +#endif // COMMUNICATIONNETSTACK_HTTP_MODULE_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 5c503905866facca1f9e9c2523963c7e9f1d382b..2f70d0dc059d6481ee531620efbcefbaa447e0a6 100644 --- a/frameworks/js/napi/http/http_module/src/http_module.cpp +++ b/frameworks/js/napi/http/http_module/src/http_module.cpp @@ -143,7 +143,6 @@ napi_value HttpModuleExports::HttpRequest::Request(napi_env env, napi_callback_i napi_value HttpModuleExports::HttpRequest::Destroy(napi_env env, napi_callback_info info) { - (void)env; (void)info; return NapiUtils::GetUndefined(env); diff --git a/frameworks/js/napi/http/options/include/http_request_options.h b/frameworks/js/napi/http/options/include/http_request_options.h index f9cfa3a0e2d1bb9182effb89853a3e9ee2421d30..01e6a4a7d360ffbb74008fc3b3e8838ebd92f8fe 100644 --- a/frameworks/js/napi/http/options/include/http_request_options.h +++ b/frameworks/js/napi/http/options/include/http_request_options.h @@ -24,36 +24,150 @@ class HttpRequestOptions final { public: HttpRequestOptions(); + /** + * @brief Set member variable url_ + * + * @param url url + * + * @return + */ void SetUrl(const std::string &url); + /** + * @brief Set member variable method_ + * + * @param method method + * + * @return + */ void SetMethod(const std::string &method); + /** + * @brief Set member variable body_ + * + * @param data *data + * @param length length + * + * @return + */ void SetBody(const void *data, size_t length); + /** + * @brief Set member variable header_ + * + * @param key key + * @param val val + * + * @return + */ void SetHeader(const std::string &key, const std::string &val); + /** + * @brief Set member variable readTimeout_ + * + * @param readTimeout readTimeout + * + * @return + */ void SetReadTimeout(uint32_t readTimeout); + /** + * @brief Set member variable connectTimeout_ + * + * @param connectTimeout connectTimeout + * + * @return + */ void SetConnectTimeout(uint32_t connectTimeout); + /** + * @brief Set member variable ifModifiedSince_ + * + * @param ifModifiedSince ifModifiedSince + * + * @return + */ void SetIfModifiedSince(uint32_t ifModifiedSince); + /** + * @brief Set member variable fixedLengthStreamingMode_ + * + * @param fixedLengthStreamingMode fixedLengthStreamingMode + * + * @return + */ void SetFixedLengthStreamingMode(int32_t fixedLengthStreamingMode); + /** + * @brief Get member variable result_ + * + * @param + * + * @return result_ value + */ [[nodiscard]] const std::string &GetUrl() const; + /** + * @brief Get member variable method_ + * + * @param + * + * @return method_ value + */ [[nodiscard]] const std::string &GetMethod() const; + /** + * @brief Get member variable body_ + * + * @param + * + * @return body_ value + */ [[nodiscard]] const std::string &GetBody() const; + /** + * @brief Get member variable header_ + * + * @param + * + * @return header_ value + */ [[nodiscard]] const std::map &GetHeader() const; + /** + * @brief Get member variable readTimeout_ + * + * @param + * + * @return readTimeout_ value + */ [[nodiscard]] uint32_t GetReadTimeout() const; + /** + * @brief Get member variable connectTimeout_ + * + * @param + * + * @return connectTimeout_ value + */ [[nodiscard]] uint32_t GetConnectTimeout() const; + /** + * @brief Get member variable ifModifiedSince_ + * + * @param + * + * @return ifModifiedSince_ value + */ [[nodiscard]] uint32_t GetIfModifiedSince() const; + /** + * @brief Get member variable fixedLengthStreamingMode_ + * + * @param + * + * @return fixedLengthStreamingMode_ value + */ [[nodiscard]] int32_t GetFixedLengthStreamingMode() const; private: @@ -75,4 +189,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_HTTP_REQUEST_OPTIONS_H */ +#endif // COMMUNICATIONNETSTACK_HTTP_REQUEST_OPTIONS_H diff --git a/frameworks/js/napi/http/options/include/http_response.h b/frameworks/js/napi/http/options/include/http_response.h index 359467464a5c9a60cb4ba597ccb9ff5f023a5334..a7030e62718dc9cbd07282cfce9cc4ce0271bfdc 100644 --- a/frameworks/js/napi/http/options/include/http_response.h +++ b/frameworks/js/napi/http/options/include/http_response.h @@ -26,22 +26,88 @@ class HttpResponse final { public: HttpResponse(); + /** + * @brief Append after member variable result_ + * + * @param length length + * @param data *data + * + * @return + */ void AppendResult(const void *data, size_t length); + /** + * @brief Append after member variable rawHeader_ + * + * @param length length + * @param data *data + * + * @return + */ void AppendRawHeader(const void *data, size_t length); + /** + * @brief Set Response Code + * + * @param + * + * @return + */ void SetResponseCode(uint32_t responseCode); + /** + * @brief Parse Headers + * + * @param + * + * @return + */ void ParseHeaders(); + /** + * @brief Append after member variable cookies_ + * + * @param length length + * @param data *data + * + * @return + */ void AppendCookies(const void *data, size_t length); + /** + * @brief Get member variable result_ + * + * @param + * + * @return result_ value + */ [[nodiscard]] const std::string &GetResult() const; + /** + * @brief Get member variable responseCode_ + * + * @param + * + * @return responseCode_ value + */ [[nodiscard]] uint32_t GetResponseCode() const; + /** + * @brief Get member variable header_ + * + * @param + * + * @return header_ + */ [[nodiscard]] const std::map &GetHeader() const; + /** + * @brief Get member variable cookies_ + * + * @param + * + * @return cookies_ value + */ [[nodiscard]] const std::string &GetCookies() const; private: @@ -57,4 +123,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_HTTP_RESPONSE_H */ +#endif // COMMUNICATIONNETSTACK_HTTP_RESPONSE_H diff --git a/frameworks/js/napi/http/options/src/http_response.cpp b/frameworks/js/napi/http/options/src/http_response.cpp index 4a2fb17285b72a26f75f2618964683901bfae13a..c0f10466a93ce29fc97a68b8bd30a37b3d8c34a1 100644 --- a/frameworks/js/napi/http/options/src/http_response.cpp +++ b/frameworks/js/napi/http/options/src/http_response.cpp @@ -40,6 +40,9 @@ void HttpResponse::SetResponseCode(uint32_t responseCode) void HttpResponse::ParseHeaders() { std::vector vec = CommonUtils::Split(rawHeader_, HttpConstant::HTTP_LINE_SEPARATOR); + if (vec.empty()) { + return; + } for (const auto &header : vec) { if (CommonUtils::Strip(header).empty()) { continue; @@ -79,4 +82,4 @@ const std::string &HttpResponse::GetCookies() const { return cookies_; } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack 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 c5a0109c0473a97f75c730e3b13f1a0028738c06..1e209da96c359192c4dea0a9b4c6268a7cad25fc 100644 --- a/frameworks/js/napi/socket/async_context/include/bind_context.h +++ b/frameworks/js/napi/socket/async_context/include/bind_context.h @@ -30,8 +30,23 @@ public: explicit BindContext(napi_env env, EventManager *manager); + /** + * @brief Parse parameters + * + * @param params *params + * @param paramsCount paramsCount + * + * @return + */ void ParseParams(napi_value *params, size_t paramsCount); + /** + * @brief get socket Fd + * + * @param + * + * @return EventManager *manager_; manager_->GetData() + */ [[nodiscard]] int GetSocketFd() const; NetAddress address; @@ -41,4 +56,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_BIND_CONTEXT_H */ +#endif // COMMUNICATIONNETSTACK_BIND_CONTEXT_H 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 2983ca6eb3cee0ba2c13d476f9bfdc0162eccad0..ae559f5be4b181b598cd3e932c78b2fdd7f97af1 100644 --- a/frameworks/js/napi/socket/async_context/include/common_context.h +++ b/frameworks/js/napi/socket/async_context/include/common_context.h @@ -31,8 +31,23 @@ public: explicit CommonContext(napi_env env, EventManager *manager); + /** + * @brief Parse parameters + * + * @param params *params + * @param paramsCount paramsCount + * + * @return + */ void ParseParams(napi_value *params, size_t paramsCount); + /** + * @brief get socket Fd + * + * @param + * + * @return EventManager *manager_; manager_->GetData() + */ [[nodiscard]] int GetSocketFd() const; SocketStateBase state; @@ -59,4 +74,4 @@ public: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_COMMON_CONTEXT_H */ +#endif // COMMUNICATIONNETSTACK_COMMON_CONTEXT_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 dc405b9d2718531344bf5b6298e2b10f3c100aa7..fe740bbcfae293278d39013c85ce8d23fd6ed560 100644 --- a/frameworks/js/napi/socket/async_context/include/connect_context.h +++ b/frameworks/js/napi/socket/async_context/include/connect_context.h @@ -29,8 +29,23 @@ public: explicit ConnectContext(napi_env env, EventManager *manager); + /** + * @brief Parse parameters + * + * @param params *params + * @param paramsCount paramsCount + * + * @return + */ void ParseParams(napi_value *params, size_t paramsCount); + /** + * @brief get socket Fd + * + * @param + * + * @return EventManager *manager_; manager_->GetData() + */ [[nodiscard]] int GetSocketFd() const; TcpConnectOptions options; @@ -40,4 +55,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_CONNECT_CONTEXT_H */ +#endif // COMMUNICATIONNETSTACK_CONNECT_CONTEXT_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 61b530425cd764d7cb823bfc7bf305890abdf0cf..1eb515061e908348b04fb7637672dcf3cc726a42 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 @@ -29,8 +29,23 @@ public: explicit TcpSetExtraOptionsContext(napi_env env, EventManager *manager); + /** + * @brief Parse parameters + * + * @param params *params + * @param paramsCount paramsCount + * + * @return + */ void ParseParams(napi_value *params, size_t paramsCount); + /** + * @brief get socket Fd + * + * @param + * + * @return EventManager *manager_; manager_->GetData() + */ [[nodiscard]] int GetSocketFd() const; TCPExtraOptions options; @@ -40,4 +55,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_TCP_EXTRA_CONTEXT_H */ +#endif // COMMUNICATIONNETSTACK_TCP_EXTRA_CONTEXT_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 e13e79b27f057a564bcdcb9caeb91ce41e78f3b2..bd79afe1186ccfb105fd22a2a4547ec27f666df5 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 @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_TCP_SEND_CONTEXT_H -#define COMMUNICATIONNETSTACK_TCP_SEND_CONTEXT_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_ASYNC_CONTEXT_INCLUDE_TCP_SEND_CONTEXT_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_ASYNC_CONTEXT_INCLUDE_TCP_SEND_CONTEXT_H_ #include "netstack_base_context.h" #include "noncopyable.h" @@ -29,8 +29,23 @@ public: explicit TcpSendContext(napi_env env, EventManager *manager); + /** + * @brief Parse parameters + * + * @param params *params + * @param paramsCount paramsCount + * + * @return + */ void ParseParams(napi_value *params, size_t paramsCount); + /** + * @brief get socket Fd + * + * @param + * + * @return EventManager *manager_; manager_->GetData() + */ [[nodiscard]] int GetSocketFd() const; TCPSendOptions options; @@ -42,4 +57,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_TCP_SEND_CONTEXT_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_ASYNC_CONTEXT_INCLUDE_TCP_SEND_CONTEXT_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 35f0bab34774cf48da7cb4699183e383f1f66d83..9ce0022d4aa66eed11702e8a660e90c9836737b0 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 @@ -29,8 +29,23 @@ public: explicit UdpSetExtraOptionsContext(napi_env env, EventManager *manager); + /** + * @brief Parse parameters + * + * @param params *params + * @param paramsCount paramsCount + * + * @return + */ void ParseParams(napi_value *params, size_t paramsCount); + /** + * @brief get socket Fd + * + * @param + * + * @return EventManager *manager_; manager_->GetData() + */ [[nodiscard]] int GetSocketFd() const; UDPExtraOptions options; @@ -40,4 +55,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_UDP_EXTRA_CONTEXT_H */ +#endif // COMMUNICATIONNETSTACK_UDP_EXTRA_CONTEXT_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 bd16bd8903722c2497e7a9ef1c90fd24a6573dba..7f49532758e1cdc75197781edf00a69b57733a51 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 @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_UDP_SEND_CONTEXT_H -#define COMMUNICATIONNETSTACK_UDP_SEND_CONTEXT_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_ASYNC_CONTEXT_INCLUDE_UDP_SEND_CONTEXT_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_ASYNC_CONTEXT_INCLUDE_UDP_SEND_CONTEXT_H_ #include "netstack_base_context.h" #include "noncopyable.h" @@ -29,8 +29,23 @@ public: explicit UdpSendContext(napi_env env, EventManager *manager); + /** + * @brief Parse parameters + * + * @param params *params + * @param paramsCount paramsCount + * + * @return + */ void ParseParams(napi_value *params, size_t paramsCount); + /** + * @brief get socket Fd + * + * @param + * + * @return EventManager *manager_; manager_->GetData() + */ [[nodiscard]] int GetSocketFd() const; UDPSendOptions options; @@ -42,4 +57,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_UDP_SEND_CONTEXT_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_ASYNC_CONTEXT_INCLUDE_UDP_SEND_CONTEXT_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 6d9b747492cbe672e742acbef67e5a6dad387a90..1e153629c41b37ab13ab603a035175be5b06f40f 100644 --- a/frameworks/js/napi/socket/async_context/src/bind_context.cpp +++ b/frameworks/js/napi/socket/async_context/src/bind_context.cpp @@ -24,6 +24,10 @@ BindContext::BindContext(napi_env env, EventManager *manager) : BaseContext(env, void BindContext::ParseParams(napi_value *params, size_t paramsCount) { + if (!params) { + return; + } + bool valid = CheckParamsType(params, paramsCount); if (!valid) { return; @@ -52,11 +56,18 @@ void BindContext::ParseParams(napi_value *params, size_t paramsCount) int BindContext::GetSocketFd() const { + if (!manager_) { + return -1; + } return (int)(uint64_t)manager_->GetData(); } bool BindContext::CheckParamsType(napi_value *params, size_t paramsCount) { + if (!params) { + return false; + } + if (paramsCount == PARAM_JUST_OPTIONS) { return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object; } 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 034f5478766e56acaee342840313c28d04beec80..9eef40c7f747c78eeb1e8106d574fb8e7436837d 100644 --- a/frameworks/js/napi/socket/async_context/src/common_context.cpp +++ b/frameworks/js/napi/socket/async_context/src/common_context.cpp @@ -23,6 +23,10 @@ CommonContext::CommonContext(napi_env env, EventManager *manager) : BaseContext( void CommonContext::ParseParams(napi_value *params, size_t paramsCount) { + if (!params) { + return; + } + bool valid = CheckParamsType(params, paramsCount); if (!valid) { return; @@ -37,11 +41,18 @@ void CommonContext::ParseParams(napi_value *params, size_t paramsCount) int CommonContext::GetSocketFd() const { + if (!manager_) { + return -1; + } return (int)(uint64_t)manager_->GetData(); } bool CommonContext::CheckParamsType(napi_value *params, size_t paramsCount) { + if (!params) { + return false; + } + if (paramsCount == PARAM_NONE) { return true; } @@ -56,6 +67,9 @@ CloseContext::CloseContext(napi_env env, EventManager *manager) : CommonContext( void CloseContext::SetSocketFd(int sock) { + if (!manager_) { + return; + } 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 83c4beb7cc54033c812b8d1437adda7c38575cdc..912f506f3ce38a91143cd77d62f76ea7b9d9c5cb 100644 --- a/frameworks/js/napi/socket/async_context/src/connect_context.cpp +++ b/frameworks/js/napi/socket/async_context/src/connect_context.cpp @@ -24,6 +24,10 @@ ConnectContext::ConnectContext(napi_env env, EventManager *manager) : BaseContex void ConnectContext::ParseParams(napi_value *params, size_t paramsCount) { + if (!params) { + return; + } + bool valid = CheckParamsType(params, paramsCount); if (!valid) { return; @@ -46,7 +50,7 @@ void ConnectContext::ParseParams(napi_value *params, size_t paramsCount) } uint32_t timeout = NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_TIMEOUT); - if (timeout != 0) { + if (timeout) { options.SetTimeout(timeout); } @@ -64,6 +68,9 @@ int ConnectContext::GetSocketFd() const bool ConnectContext::CheckParamsType(napi_value *params, size_t paramsCount) { + if (!params) { + return false; + } if (paramsCount == PARAM_JUST_OPTIONS) { return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object && NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_ADDRESS)) == @@ -78,4 +85,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 1c81969faf3573133a62ae6b3bd03f2796bc97bd..5738ab7f79ad9cdf446edaa8503051fe055b6d81 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 @@ -23,6 +23,10 @@ TcpSetExtraOptionsContext::TcpSetExtraOptionsContext(napi_env env, EventManager void TcpSetExtraOptionsContext::ParseParams(napi_value *params, size_t paramsCount) { + if (!params) { + return; + } + bool valid = CheckParamsType(params, paramsCount); if (!valid) { return; @@ -78,11 +82,19 @@ void TcpSetExtraOptionsContext::ParseParams(napi_value *params, size_t paramsCou int TcpSetExtraOptionsContext::GetSocketFd() const { + if (!manager_) { + return -1; + } + return (int)(uint64_t)manager_->GetData(); } bool TcpSetExtraOptionsContext::CheckParamsType(napi_value *params, size_t paramsCount) { + if (!params) { + return false; + } + if (paramsCount == PARAM_JUST_OPTIONS) { return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object; } @@ -93,4 +105,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 832a4eb468dc6944c1be576855030f6a775f9f30..8b36fa362eb0c1f05b1f1feb056377213b53b7d6 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 @@ -24,6 +24,10 @@ TcpSendContext::TcpSendContext(napi_env env, EventManager *manager) : BaseContex void TcpSendContext::ParseParams(napi_value *params, size_t paramsCount) { + if (!params) { + return; + } + bool valid = CheckParamsType(params, paramsCount); if (!valid) { return; @@ -46,11 +50,19 @@ void TcpSendContext::ParseParams(napi_value *params, size_t paramsCount) int TcpSendContext::GetSocketFd() const { + if (!manager_) { + return -1; + } + return (int)(uint64_t)manager_->GetData(); } bool TcpSendContext::CheckParamsType(napi_value *params, size_t paramsCount) { + if (!params) { + return false; + } + if (paramsCount == PARAM_JUST_OPTIONS) { return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object; } @@ -77,14 +89,14 @@ bool TcpSendContext::GetData(napi_value udpSendOptions) if (NapiUtils::ValueIsArrayBuffer(GetEnv(), jsData)) { size_t length = 0; - void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), jsData, &length); - if (data == nullptr) { + void *data1 = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), jsData, &length); + if (!data1) { NETSTACK_LOGE("arraybuffer data is empty"); return false; } - options.SetData(data, length); + options.SetData(data1, length); return true; } 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 20cac9d5cfed94e8e5f70cad168f49244dd072c2..3163b531f6dbc7f6d2ae467419cb96f5384d8045 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 @@ -23,6 +23,9 @@ UdpSetExtraOptionsContext::UdpSetExtraOptionsContext(napi_env env, EventManager void UdpSetExtraOptionsContext::ParseParams(napi_value *params, size_t paramsCount) { + if (!params) { + return; + } bool valid = CheckParamsType(params, paramsCount); if (!valid) { return; @@ -57,11 +60,17 @@ void UdpSetExtraOptionsContext::ParseParams(napi_value *params, size_t paramsCou int UdpSetExtraOptionsContext::GetSocketFd() const { + if (!manager_) { + return -1; + } return (int)(uint64_t)manager_->GetData(); } bool UdpSetExtraOptionsContext::CheckParamsType(napi_value *params, size_t paramsCount) { + if (!params) { + return false; + } if (paramsCount == PARAM_JUST_OPTIONS) { return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object; } @@ -72,4 +81,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 e7d828e120aae4dc4093101ac4ad8fe6f7af4a98..408840e5d42874a39d009b124856849159c64d54 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 @@ -24,6 +24,9 @@ UdpSendContext::UdpSendContext(napi_env env, EventManager *manager) : BaseContex void UdpSendContext::ParseParams(napi_value *params, size_t paramsCount) { + if (!params) { + return; + } bool valid = CheckParamsType(params, paramsCount); if (!valid) { return; @@ -57,11 +60,17 @@ void UdpSendContext::ParseParams(napi_value *params, size_t paramsCount) int UdpSendContext::GetSocketFd() const { + if (!manager_) { + return -1; + } return (int)(uint64_t)manager_->GetData(); } bool UdpSendContext::CheckParamsType(napi_value *params, size_t paramsCount) { + if (!params) { + return false; + } if (paramsCount == PARAM_JUST_OPTIONS) { return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object && NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_ADDRESS)) == @@ -92,12 +101,12 @@ bool UdpSendContext::GetData(napi_value udpSendOptions) if (NapiUtils::ValueIsArrayBuffer(GetEnv(), jsData)) { size_t length = 0; - void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), jsData, &length); - if (data == nullptr) { + void *data1 = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), jsData, &length); + if (!data1) { NETSTACK_LOGE("arraybuffer data is empty"); return false; } - options.SetData(data, length); + options.SetData(data1, length); return true; } return false; diff --git a/frameworks/js/napi/socket/async_work/include/socket_async_work.h b/frameworks/js/napi/socket/async_work/include/socket_async_work.h index 7c037a6c166e5ed9f557feab148f60c28bb5489c..98ab8374e1ca1e7bad2a77a6aee81d05384d0e89 100644 --- a/frameworks/js/napi/socket/async_work/include/socket_async_work.h +++ b/frameworks/js/napi/socket/async_work/include/socket_async_work.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_SOCKET_ASYNC_WORK_H -#define COMMUNICATIONNETSTACK_SOCKET_ASYNC_WORK_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_ASYNC_WORK_INCLUDE_SOCKET_ASYNC_WORK_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_ASYNC_WORK_INCLUDE_SOCKET_ASYNC_WORK_H_ #include "napi/native_api.h" #include "noncopyable.h" @@ -25,43 +25,196 @@ public: ACE_DISALLOW_COPY_AND_MOVE(SocketAsyncWork); /* executor */ + /** + * @brief perform binding + * + * @param env env + * @param data *data + * + * @return + */ static void ExecBind(napi_env env, void *data); + /** + * @brief Perform Udp send + * + * @param thisVal env + * @param data *data + * + * @return + */ static void ExecUdpSend(napi_env env, void *data); + /** + * @brief Perform connect + * + * @param env env + * @param data *data + * + * @return + */ static void ExecConnect(napi_env env, void *data); + /** + * @brief Perform Tcp send + * + * @param env env + * @param data *data + * + * @return + */ static void ExecTcpSend(napi_env env, void *data); + /** + * @brief Perform Close + * + * @param env env + * @param data *data + * + * @return + */ static void ExecClose(napi_env env, void *data); + /** + * @brief execute get status + * + * @param env env + * @param data *data + * + * @return + */ static void ExecGetState(napi_env env, void *data); + /** + * @brief execute get remote address + * + * @param env env + * @param data *data + * + * @return + */ static void ExecGetRemoteAddress(napi_env env, void *data); + /** + * @brief Execute Tcp to set extra options + * + * @param env env + * @param data *data + * + * @return + */ static void ExecTcpSetExtraOptions(napi_env env, void *data); + /** + * @brief Execute Udp to set extra options + * + * @param env env + * @param data *data + * + * @return + */ static void ExecUdpSetExtraOptions(napi_env env, void *data); /* callback */ + /** + * @brief bind callback + * + * @param env env + * @param status status + * @param data *data + * + * @return + */ static void BindCallback(napi_env env, napi_status status, void *data); + /** + * @brief udp send callback + * + * @param env env + * @param status status + * @param data *data + * + * @return + */ static void UdpSendCallback(napi_env env, napi_status status, void *data); + /** + * @brief connection callback + * + * @param env env + * @param status status + * @param data *data + * + * @return + */ static void ConnectCallback(napi_env env, napi_status status, void *data); + /** + * @brief Tcp send callback + * + * @param env env + * @param status status + * @param data *data + * + * @return + */ static void TcpSendCallback(napi_env env, napi_status status, void *data); + /** + * @brief Close callback + * + * @param env env + * @param status status + * @param data *data + * + * @return + */ static void CloseCallback(napi_env env, napi_status status, void *data); + /** + * @brief Get State callback + * + * @param env env + * @param status status + * @param data *data + * + * @return + */ static void GetStateCallback(napi_env env, napi_status status, void *data); + /** + * @brief Get Remote Address callback + * + * @param env env + * @param status status + * @param data *data + * + * @return + */ static void GetRemoteAddressCallback(napi_env env, napi_status status, void *data); + /** + * @brief Set Tcp Extra Options callback + * + * @param env env + * @param status status + * @param data *data + * + * @return + */ static void TcpSetExtraOptionsCallback(napi_env env, napi_status status, void *data); + /** + * @brief Set Udp Extra Options callback + * + * @param env env + * @param status status + * @param data *data + * + * @return + */ static void UdpSetExtraOptionsCallback(napi_env env, napi_status status, void *data); }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_SOCKET_ASYNC_WORK_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_ASYNC_WORK_INCLUDE_SOCKET_ASYNC_WORK_H_ diff --git a/frameworks/js/napi/socket/constant/include/context_key.h b/frameworks/js/napi/socket/constant/include/context_key.h index 4044948299814b2852b5ee015f6d0649476f44e1..08c01836cb271d962db57762f46930750411413d 100644 --- a/frameworks/js/napi/socket/constant/include/context_key.h +++ b/frameworks/js/napi/socket/constant/include/context_key.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_CONTEXT_KEY_H -#define COMMUNICATIONNETSTACK_CONTEXT_KEY_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_CONSTANT_INCLUDE_CONTEXT_KEY_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_CONSTANT_INCLUDE_CONTEXT_KEY_H_ #define KEY_ADDRESS "address" @@ -64,12 +64,12 @@ #define KEY_REMOTE_INFO "remoteInfo" -#define PARAM_NONE 0 +constexpr int32_t PARAM_NONE = 0; -#define PARAM_JUST_OPTIONS 1 +constexpr int32_t PARAM_JUST_CALLBACK = 1; -#define PARAM_JUST_CALLBACK 1 +constexpr int32_t PARAM_JUST_OPTIONS = 1; -#define PARAM_OPTIONS_AND_CALLBACK 2 +constexpr int32_t PARAM_OPTIONS_AND_CALLBACK = 2; -#endif /* COMMUNICATIONNETSTACK_CONTEXT_KEY_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_CONSTANT_INCLUDE_CONTEXT_KEY_H_ diff --git a/frameworks/js/napi/socket/constant/include/event_list.h b/frameworks/js/napi/socket/constant/include/event_list.h index ea1f295bed659547bf7b316293b904339c0ba4fe..462b723049d80a34e787fe87243b36ec2dbdefa2 100644 --- a/frameworks/js/napi/socket/constant/include/event_list.h +++ b/frameworks/js/napi/socket/constant/include/event_list.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_EVENT_LIST_H -#define COMMUNICATIONNETSTACK_EVENT_LIST_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_CONSTANT_INCLUDE_EVENT_LIST_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_CONSTANT_INCLUDE_EVENT_LIST_H_ constexpr const char *EVENT_MESSAGE = "message"; @@ -26,4 +26,4 @@ constexpr const char *EVENT_CONNECT = "connect"; constexpr const char *EVENT_CLOSE = "close"; -#endif /* COMMUNICATIONNETSTACK_EVENT_LIST_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_CONSTANT_INCLUDE_EVENT_LIST_H_ diff --git a/frameworks/js/napi/socket/options/include/extra_options_base.h b/frameworks/js/napi/socket/options/include/extra_options_base.h index 8433701962b6f53b6ff1d7265465da5e58d9f83d..64fea5864992b2218d1dca1170263e0624882dc0 100644 --- a/frameworks/js/napi/socket/options/include/extra_options_base.h +++ b/frameworks/js/napi/socket/options/include/extra_options_base.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_EXTRA_OPTIONS_BASE_H -#define COMMUNICATIONNETSTACK_EXTRA_OPTIONS_BASE_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_EXTRA_OPTIONS_BASE_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_EXTRA_OPTIONS_BASE_H_ #include @@ -25,20 +25,76 @@ public: ~ExtraOptionsBase() = default; + /** + * @brief Set member variables receiveBufferSize_ + * + * @param receiveBufferSize receiveBufferSize + * + * @return + */ void SetReceiveBufferSize(uint32_t receiveBufferSize); + /** + * @brief Set member variables sendBufferSize_ + * + * @param sendBufferSize sendBufferSize + * + * @return + */ void SetSendBufferSize(uint32_t sendBufferSize); + /** + * @brief Set member variables reuseAddress_ + * + * @param reuseAddress_ reuseAddress_ + * + * @return + */ void SetReuseAddress(bool reuseAddress); + /** + * @brief Set member variables socketTimeout_ + * + * @param socketTimeout socketTimeout + * + * @return + */ void SetSocketTimeout(uint32_t socketTimeout); + /** + * @brief Get member variables size_ + * + * @param + * + * @return receiveBufferSize_ value + */ [[nodiscard]] uint32_t GetReceiveBufferSize() const; + /** + * @brief Get member variables size_ + * + * @param + * + * @return sendBufferSize_ value + */ [[nodiscard]] uint32_t GetSendBufferSize() const; + /** + * @brief Get member variables reuseAddress_ + * + * @param + * + * @return reuseAddress_ value + */ [[nodiscard]] bool IsReuseAddress() const; + /** + * @brief Get member variables socketTimeout_ + * + * @param + * + * @return socketTimeout_ value + */ [[nodiscard]] uint32_t GetSocketTimeout() const; private: @@ -52,4 +108,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_EXTRA_OPTIONS_BASE_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_EXTRA_OPTIONS_BASE_H_ diff --git a/frameworks/js/napi/socket/options/include/net_address.h b/frameworks/js/napi/socket/options/include/net_address.h index 98e686ef20be2351efe4e18613a012662d1b342f..6c900fe4e6c4f347677da71a78c3e71ebf00aa72 100644 --- a/frameworks/js/napi/socket/options/include/net_address.h +++ b/frameworks/js/napi/socket/options/include/net_address.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_NET_ADDRESS_H -#define COMMUNICATIONNETSTACK_NET_ADDRESS_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_NET_ADDRESS_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_NET_ADDRESS_H_ #include #include @@ -31,20 +31,76 @@ public: ~NetAddress() = default; + /** + * @brief Set member variables address_ + * + * @param address address + * + * @return + */ void SetAddress(const std::string &address); + /** + * @brief Set member variables family_ By JsValue + * + * @param family family + * + * @return + */ void SetFamilyByJsValue(uint32_t family); + /** + * @brief Set member variables family_ By SaFamily + * + * @param family family + * + * @return + */ void SetFamilyBySaFamily(sa_family_t family); + /** + * @brief Set member variables port_ + * + * @param port port + * + * @return + */ void SetPort(uint16_t port); + /** + * @brief Get member variables address_ + * + * @param + * + * @return address_ + */ [[nodiscard]] const std::string &GetAddress() const; + /** + * @brief Get member variables family_ by JsValue + * + * @param + * + * @return family_ + */ [[nodiscard]] uint32_t GetJsValueFamily() const; + /** + * @brief Get member variables family_ by SaFamily + * + * @param + * + * @return family_ + */ [[nodiscard]] sa_family_t GetSaFamily() const; + /** + * @brief Get member variables port_ + * + * @param + * + * @return port_ + */ [[nodiscard]] uint16_t GetPort() const; private: @@ -56,4 +112,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_NET_ADDRESS_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_NET_ADDRESS_H_ 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 7b6c805dc08c8df4b6829f62902fa6bd09370e3e..f47ec1569514648e9150a8ad2f05de4ac3995f1e 100644 --- a/frameworks/js/napi/socket/options/include/socket_remote_info.h +++ b/frameworks/js/napi/socket/options/include/socket_remote_info.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_SOCKET_REMOTE_INFO_H -#define COMMUNICATIONNETSTACK_SOCKET_REMOTE_INFO_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_SOCKET_REMOTE_INFO_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_SOCKET_REMOTE_INFO_H_ #include @@ -27,20 +27,76 @@ public: ~SocketRemoteInfo() = default; + /** + * @brief Set member variables address_ + * + * @param address address + * + * @return + */ void SetAddress(const std::string &address); + /** + * @brief Set member variables family_ + * + * @param family family + * + * @return + */ void SetFamily(sa_family_t family); + /** + * @brief Set member variables port_ + * + * @param port port + * + * @return + */ void SetPort(uint16_t port); + /** + * @brief Set member variables size_ + * + * @param size size + * + * @return + */ void SetSize(uint32_t size); + /** + * @brief Get member variables address_ + * + * @param + * + * @return address_ value + */ [[nodiscard]] const std::string &GetAddress() const; + /** + * @brief Get member variables family_ + * + * @param + * + * @return family_ value + */ [[nodiscard]] const std::string &GetFamily() const; + /** + * @brief Get member variables port_ + * + * @param + * + * @return port_ value + */ [[nodiscard]] uint16_t GetPort() const; + /** + * @brief Get member variables size_ + * + * @param + * + * @return size_ value + */ [[nodiscard]] uint32_t GetSize() const; private: @@ -54,4 +110,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_SOCKET_REMOTE_INFO_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_SOCKET_REMOTE_INFO_H_ diff --git a/frameworks/js/napi/socket/options/include/socket_state_base.h b/frameworks/js/napi/socket/options/include/socket_state_base.h index 2b9fa59453faa15d20bdb2c154d01f479351a450..0f657c92268e9845c76cacc449afb0902d462143 100644 --- a/frameworks/js/napi/socket/options/include/socket_state_base.h +++ b/frameworks/js/napi/socket/options/include/socket_state_base.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_SOCKET_STATE_BASE_H -#define COMMUNICATIONNETSTACK_SOCKET_STATE_BASE_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_SOCKET_STATE_BASE_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_SOCKET_STATE_BASE_H_ namespace OHOS::NetStack { class SocketStateBase final { @@ -23,16 +23,58 @@ public: ~SocketStateBase() = default; + /** + * @brief Set member variables isBound_ + * + * @param isBound isBound + * + * @return + */ void SetIsBound(bool isBound); + /** + * @brief Set member variables isClose_ + * + * @param isClose isClose + * + * @return + */ void SetIsClose(bool isClose); + /** + * @brief Set member variables isConnected_ + * + * @param isConnected isConnected + * + * @return + */ void SetIsConnected(bool isConnected); + /** + * @brief Get member variables isBound_ + * + * @param + * + * @return isBound_ value + */ [[nodiscard]] bool IsBound() const; + /** + * @brief Get member variables isClose_ + * + * @param + * + * @return isClose_ value + */ [[nodiscard]] bool IsClose() const; + /** + * @brief Get member variables isConnected_ + * + * @param + * + * @return isConnected_ value + */ [[nodiscard]] bool IsConnected() const; private: @@ -44,4 +86,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_SOCKET_STATE_BASE_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_SOCKET_STATE_BASE_H_ diff --git a/frameworks/js/napi/socket/options/include/tcp_connect_options.h b/frameworks/js/napi/socket/options/include/tcp_connect_options.h index a3613ac3a9de4155c02cd15eb38fefdd18647b27..97b829628cb6eed26c3bfdf35e012968277a93c1 100644 --- a/frameworks/js/napi/socket/options/include/tcp_connect_options.h +++ b/frameworks/js/napi/socket/options/include/tcp_connect_options.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_TCP_CONNECT_OPTIONS_H -#define COMMUNICATIONNETSTACK_TCP_CONNECT_OPTIONS_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_TCP_CONNECT_OPTIONS_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_TCP_CONNECT_OPTIONS_H_ #include "net_address.h" @@ -27,8 +27,22 @@ public: ~TcpConnectOptions() = default; + /** + * @brief Set member variables timeout_ + * + * @param timeout timeout + * + * @return + */ void SetTimeout(uint32_t timeout); + /** + * @brief Get member variables timeout_ + * + * @param + * + * @return timeout_ value + */ [[nodiscard]] uint32_t GetTimeout() const; NetAddress address; @@ -38,4 +52,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_TCP_CONNECT_OPTIONS_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_TCP_CONNECT_OPTIONS_H_ diff --git a/frameworks/js/napi/socket/options/include/tcp_extra_options.h b/frameworks/js/napi/socket/options/include/tcp_extra_options.h index d3485eb761560a16e3a599e02f9fc92293218fbb..c708b5543268e115a60e9600aa0e3b4e244525cc 100644 --- a/frameworks/js/napi/socket/options/include/tcp_extra_options.h +++ b/frameworks/js/napi/socket/options/include/tcp_extra_options.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_TCP_EXTRA_OPTIONS_H -#define COMMUNICATIONNETSTACK_TCP_EXTRA_OPTIONS_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_TCP_EXTRA_OPTIONS_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_TCP_EXTRA_OPTIONS_H_ #include "extra_options_base.h" @@ -68,4 +68,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_TCP_EXTRA_OPTIONS_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_TCP_EXTRA_OPTIONS_H_ diff --git a/frameworks/js/napi/socket/options/include/tcp_send_options.h b/frameworks/js/napi/socket/options/include/tcp_send_options.h index 247a8d9397e637399de1d2955818e93ee0a022eb..a008f7d1f147582e6e13aad7da4e3ea5c18dd952 100644 --- a/frameworks/js/napi/socket/options/include/tcp_send_options.h +++ b/frameworks/js/napi/socket/options/include/tcp_send_options.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_TCP_SEND_OPTIONS_H -#define COMMUNICATIONNETSTACK_TCP_SEND_OPTIONS_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_TCP_SEND_OPTIONS_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_TCP_SEND_OPTIONS_H_ #include @@ -25,14 +25,50 @@ public: ~TCPSendOptions() = default; + /** + * @brief Set member variables data_ + * + * @param data data + * + * @return + */ void SetData(const std::string &data); + /** + * @brief Append after member variable data_ + * + * @param data data + * @param size size + * + * @return + */ void SetData(void *data, size_t size); + /** + * @brief Set member variables encoding_ + * + * @param encoding encoding + * + * @return + */ void SetEncoding(const std::string &encoding); + /** + * @brief Get member variables data_ + * + * @param + * + * @return data_ value + */ [[nodiscard]] const std::string &GetData() const; + /** + * @brief Get member variables encoding_ + * + * @param + * + * @return encoding_ value + */ [[nodiscard]] const std::string &GetEncoding() const; private: @@ -42,4 +78,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_TCP_SEND_OPTIONS_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_TCP_SEND_OPTIONS_H_ diff --git a/frameworks/js/napi/socket/options/include/udp_extra_options.h b/frameworks/js/napi/socket/options/include/udp_extra_options.h index 4b3e1f3cc7e0c616881d5c76902dbdd0b64e4dc4..ce3f881e91b7c19a3d91403de95ad271d25c3bbb 100644 --- a/frameworks/js/napi/socket/options/include/udp_extra_options.h +++ b/frameworks/js/napi/socket/options/include/udp_extra_options.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_UDP_EXTRA_OPTIONS_H -#define COMMUNICATIONNETSTACK_UDP_EXTRA_OPTIONS_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_UDP_EXTRA_OPTIONS_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_UDP_EXTRA_OPTIONS_H_ #include "extra_options_base.h" @@ -25,8 +25,22 @@ public: ~UDPExtraOptions() = default; + /** + * @brief Set member variables broadcast_ + * + * @param broadcast broadcast + * + * @return + */ void SetBroadcast(bool broadcast); + /** + * @brief Get member variables broadcast_ + * + * @param + * + * @return broadcast_ + */ [[nodiscard]] bool IsBroadcast() const; private: @@ -34,4 +48,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_UDP_EXTRA_OPTIONS_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_UDP_EXTRA_OPTIONS_H_ diff --git a/frameworks/js/napi/socket/options/include/udp_send_options.h b/frameworks/js/napi/socket/options/include/udp_send_options.h index 001f1587372c8cc43e1bcf7de4f7c03fe347a274..bc6f45435e55b15d8af762e529314ab7ea508e08 100644 --- a/frameworks/js/napi/socket/options/include/udp_send_options.h +++ b/frameworks/js/napi/socket/options/include/udp_send_options.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_UDP_SEND_OPTIONS_H -#define COMMUNICATIONNETSTACK_UDP_SEND_OPTIONS_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_UDP_SEND_OPTIONS_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_UDP_SEND_OPTIONS_H_ #include "net_address.h" @@ -25,10 +25,32 @@ public: ~UDPSendOptions() = default; + /** + * @brief Set member variables data_ + * + * @param data data + * + * @return + */ void SetData(const std::string &data); + /** + * @brief Append after member variable data_ + * + * @param data *data + * @param size size + * + * @return + */ void SetData(void *data, size_t size); + /** + * @brief Get member variables data_ + * + * @param + * + * @return data_ + */ [[nodiscard]] const std::string &GetData() const; NetAddress address; @@ -38,4 +60,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_UDP_SEND_OPTIONS_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_OPTIONS_INCLUDE_UDP_SEND_OPTIONS_H_ diff --git a/frameworks/js/napi/socket/socket_exec/include/socket_exec.h b/frameworks/js/napi/socket/socket_exec/include/socket_exec.h index 377dfa7f28420b38c47b01bd68d106dab02280a7..fc1712d59a7d8641b3fe3a4128d74e0391867cbc 100644 --- a/frameworks/js/napi/socket/socket_exec/include/socket_exec.h +++ b/frameworks/js/napi/socket/socket_exec/include/socket_exec.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_SOCKET_EXEC_H -#define COMMUNICATIONNETSTACK_SOCKET_EXEC_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_SOCKET_EXEC_INCLUDE_SOCKET_EXEC_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_SOCKET_EXEC_INCLUDE_SOCKET_EXEC_H_ #include "bind_context.h" #include "common_context.h" @@ -68,4 +68,4 @@ napi_value TcpSetExtraOptionsCallback(TcpSetExtraOptionsContext *context); napi_value UdpSetExtraOptionsCallback(UdpSetExtraOptionsContext *context); } // namespace OHOS::NetStack::SocketExec -#endif /* COMMUNICATIONNETSTACK_SOCKET_EXEC_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_SOCKET_EXEC_INCLUDE_SOCKET_EXEC_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 694575cbe2e83eb8967e6a8a5d4a3178ae8e4ef5..25b55ad8291b3209cf193ef45d0009551042d924 100644 --- a/frameworks/js/napi/socket/socket_exec/src/socket_exec.cpp +++ b/frameworks/js/napi/socket/socket_exec/src/socket_exec.cpp @@ -40,25 +40,34 @@ namespace OHOS::NetStack::SocketExec { static void SetIsBound(sa_family_t family, GetStateContext *context, const sockaddr_in *addr4, const sockaddr_in6 *addr6) { + if (!context || addr4 == NULL || addr6 == NULL) { + return; + } if (family == AF_INET) { - context->state.SetIsBound(ntohs(addr4->sin_port) != 0); + context->state.SetIsBound(ntohs(addr4->sin_port)); } else if (family == AF_INET6) { - context->state.SetIsBound(ntohs(addr6->sin6_port) != 0); + context->state.SetIsBound(ntohs(addr6->sin6_port)); } } static void SetIsConnected(sa_family_t family, GetStateContext *context, const sockaddr_in *addr4, const sockaddr_in6 *addr6) { + if (!context || !addr4 || !addr6) { + return; + } if (family == AF_INET) { - context->state.SetIsConnected(ntohs(addr4->sin_port) != 0); + context->state.SetIsConnected(ntohs(addr4->sin_port)); } else if (family == AF_INET6) { - context->state.SetIsConnected(ntohs(addr6->sin6_port) != 0); + context->state.SetIsConnected(ntohs(addr6->sin6_port)); } } static void EmitError(BaseContext *context, int32_t errorNum) { + if (!context) { + return; + } napi_value error = NapiUtils::CreateObject(context->GetEnv()); NapiUtils::SetInt32Property(context->GetEnv(), error, KEY_ERROR_CODE, errorNum); context->Emit(EVENT_ERROR, std::make_pair(NapiUtils::GetUndefined(context->GetEnv()), error)); @@ -66,26 +75,32 @@ static void EmitError(BaseContext *context, int32_t errorNum) static std::string MakeAddressString(sockaddr *addr) { + if (!addr) { + return {}; + } if (addr->sa_family == AF_INET) { auto *addr4 = reinterpret_cast(addr); - const char *str = inet_ntoa(addr4->sin_addr); - if (str == nullptr || strlen(str) == 0) { + const char *str1 = inet_ntoa(addr4->sin_addr); + if (!str1 || !strlen(str1)) { return {}; } - return str; + return str1; } else if (addr->sa_family == AF_INET6) { auto *addr6 = reinterpret_cast(addr); - char str[INET6_ADDRSTRLEN] = {0}; - if (inet_ntop(AF_INET6, &addr6->sin6_addr, str, INET6_ADDRSTRLEN) == nullptr || strlen(str) == 0) { + char str2[INET6_ADDRSTRLEN] = {0}; + if (!(inet_ntop(AF_INET6, &addr6->sin6_addr, str2, INET6_ADDRSTRLEN)) || !strlen(str2)) { return {}; } - return str; + return str2; } return {}; } static napi_value MakeJsMessageParam(napi_env env, napi_value msgBuffer, SocketRemoteInfo *remoteInfo) { + if (!env || !msgBuffer || !remoteInfo) { + return nullptr; + } napi_value obj = NapiUtils::CreateObject(env); if (NapiUtils::GetValueType(env, obj) != napi_object) { return nullptr; @@ -106,7 +121,7 @@ static napi_value MakeJsMessageParam(napi_env env, napi_value msgBuffer, SocketR static void OnRecvMessage(BaseContext *context, void *data, size_t len, sockaddr *addr) { - if (data == nullptr || len <= 0) { + if (!data || len <= 0 || !context || !addr) { return; } @@ -180,7 +195,7 @@ public: socklen_t len4 = sizeof(sockaddr_in); ret = getpeername(sock, reinterpret_cast(&addr4), &len4); - if (ret < 0) { + if (ret < 0 || !context_) { return; } OnRecvMessage(context_, data, dataLen, reinterpret_cast(&addr4)); @@ -190,7 +205,7 @@ public: socklen_t len6 = sizeof(sockaddr_in6); ret = getpeername(sock, reinterpret_cast(&addr6), &len6); - if (ret < 0) { + if (ret < 0 || !context_) { return; } OnRecvMessage(context_, data, dataLen, reinterpret_cast(&addr6)); @@ -236,6 +251,9 @@ static bool MakeNonBlock(int sock) static bool PollSendData(int sock, const char *data, size_t size, sockaddr *addr, socklen_t addrLen) { + if (!data || !addr || addrLen <= 0) { + return false; + } int bufferSize = DEFAULT_BUFFER_SIZE; int opt = 0; socklen_t optLen = sizeof(opt); @@ -263,7 +281,7 @@ static bool PollSendData(int sock, const char *data, size_t size, sockaddr *addr NETSTACK_LOGE("poll to send failed %{public}s", strerror(errno)); return false; } - if (ret == 0) { + if (!ret) { NETSTACK_LOGE("poll to send timeout"); return false; } @@ -277,14 +295,14 @@ static bool PollSendData(int sock, const char *data, size_t size, sockaddr *addr NETSTACK_LOGE("send failed %{public}s", strerror(errno)); return false; } - if (sendLen == 0) { + if (!sendLen) { break; } curPos += sendLen; leftSize -= sendLen; } - if (leftSize != 0) { + if (leftSize) { NETSTACK_LOGE("send not complete"); return false; } @@ -293,6 +311,9 @@ static bool PollSendData(int sock, const char *data, size_t size, sockaddr *addr static bool PollRecvData(int sock, sockaddr *addr, socklen_t addrLen, const MessageCallback &callback) { + if (!addr || addrLen <= 0) { + return false; + } int bufferSize = DEFAULT_BUFFER_SIZE; int opt = 0; socklen_t optLen = sizeof(opt); @@ -315,7 +336,7 @@ static bool PollRecvData(int sock, sockaddr *addr, socklen_t addrLen, const Mess NETSTACK_LOGE("poll to recv failed %{public}s", strerror(errno)); return false; } - if (ret == 0) { + if (!ret) { break; } (void)memset_s(buf.get(), bufferSize, 0, bufferSize); @@ -328,7 +349,7 @@ static bool PollRecvData(int sock, sockaddr *addr, socklen_t addrLen, const Mess NETSTACK_LOGE("recv failed %{public}s", strerror(errno)); return false; } - if (recvLen == 0) { + if (!recvLen) { break; } callback(sock, buf.get(), recvLen, addr); @@ -350,7 +371,7 @@ static bool NonBlockConnect(int sock, sockaddr *addr, socklen_t addrLen, uint32_ fd_set set = {0}; FD_ZERO(&set); FD_SET(sock, &set); - if (timeoutSec == 0) { + if (!timeoutSec) { timeoutSec = DEFAULT_CONNECT_TIMEOUT; } timeval timeout = { @@ -362,7 +383,7 @@ static bool NonBlockConnect(int sock, sockaddr *addr, socklen_t addrLen, uint32_ if (ret < 0) { NETSTACK_LOGE("select error: %{public}s\n", strerror(errno)); return false; - } else if (ret == 0) { + } else if (!ret) { NETSTACK_LOGE("timeout!"); return false; } @@ -373,7 +394,7 @@ static bool NonBlockConnect(int sock, sockaddr *addr, socklen_t addrLen, uint32_ if (ret < 0) { return false; } - if (err != 0) { + if (err) { return false; } return true; @@ -381,6 +402,9 @@ static bool NonBlockConnect(int sock, sockaddr *addr, socklen_t addrLen, uint32_ static void GetAddr(NetAddress *address, sockaddr_in *addr4, sockaddr_in6 *addr6, sockaddr **addr, socklen_t *len) { + if (!address || !addr4 || !addr6 || !len || !addr) { + return; + } sa_family_t family = address->GetSaFamily(); if (family == AF_INET) { addr4->sin_family = AF_INET; @@ -399,16 +423,19 @@ static void GetAddr(NetAddress *address, sockaddr_in *addr4, sockaddr_in6 *addr6 static bool SetBaseOptions(int sock, ExtraOptionsBase *option) { - if (option->GetReceiveBufferSize() != 0) { - int size = (int)option->GetReceiveBufferSize(); - if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, reinterpret_cast(&size), sizeof(size)) < 0) { + if (!option) { + return false; + } + if (option->GetReceiveBufferSize()) { + int size1 = (int)option->GetReceiveBufferSize(); + if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, static_cast(&size1), sizeof(size1)) < 0) { return false; } } - if (option->GetSendBufferSize() != 0) { - int size = (int)option->GetSendBufferSize(); - if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, reinterpret_cast(&size), sizeof(size)) < 0) { + if (option->GetSendBufferSize()) { + int size2 = (int)option->GetSendBufferSize(); + if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, static_cast(&size2), sizeof(size2)) < 0) { return false; } } @@ -420,7 +447,7 @@ static bool SetBaseOptions(int sock, ExtraOptionsBase *option) } } - if (option->GetSocketTimeout() != 0) { + if (option->GetSocketTimeout()) { timeval timeout = {(int)option->GetSocketTimeout(), 0}; if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast(&timeout), sizeof(timeout)) < 0) { return false; @@ -469,12 +496,15 @@ int MakeUdpSocket(sa_family_t family) bool ExecBind(BindContext *context) { + if (!context) { + return false; + } sockaddr_in addr4 = {0}; sockaddr_in6 addr6 = {0}; sockaddr *addr = nullptr; socklen_t len; GetAddr(&context->address, &addr4, &addr6, &addr, &len); - if (addr == nullptr) { + if (!addr) { NETSTACK_LOGE("addr family error"); context->SetErrorCode(ADDRESS_INVALID); return false; @@ -507,12 +537,15 @@ bool ExecBind(BindContext *context) bool ExecUdpSend(UdpSendContext *context) { + if (!context) { + return false; + } sockaddr_in addr4 = {0}; sockaddr_in6 addr6 = {0}; sockaddr *addr = nullptr; socklen_t len; GetAddr(&context->options.address, &addr4, &addr6, &addr, &len); - if (addr == nullptr) { + if (!addr) { NETSTACK_LOGE("addr family error"); context->SetErrorCode(ADDRESS_INVALID); return false; @@ -532,8 +565,11 @@ bool ExecConnect(ConnectContext *context) sockaddr_in6 addr6 = {0}; sockaddr *addr = nullptr; socklen_t len; + if (!context) { + return false; + } GetAddr(&context->options.address, &addr4, &addr6, &addr, &len); - if (addr == nullptr) { + if (!addr) { NETSTACK_LOGE("addr family error"); context->SetErrorCode(ADDRESS_INVALID); return false; @@ -551,6 +587,9 @@ bool ExecConnect(ConnectContext *context) bool ExecTcpSend(TcpSendContext *context) { + if (!context) { + return false; + } std::string encoding = context->options.GetEncoding(); (void)encoding; /* no use for now */ @@ -566,15 +605,15 @@ bool ExecTcpSend(TcpSendContext *context) if (family == AF_INET) { sockaddr_in addr4 = {0}; socklen_t len4 = sizeof(addr4); - int ret = getpeername(context->GetSocketFd(), reinterpret_cast(&addr4), &len4); - if (ret >= 0 && addr4.sin_port != 0) { + int ret1 = getpeername(context->GetSocketFd(), reinterpret_cast(&addr4), &len4); + if (ret1 >= 0 && addr4.sin_port) { connected = true; } } else if (family == AF_INET6) { sockaddr_in6 addr6 = {0}; socklen_t len6 = sizeof(addr6); - int ret = getpeername(context->GetSocketFd(), reinterpret_cast(&addr6), &len6); - if (ret >= 0 && addr6.sin6_port != 0) { + int ret2 = getpeername(context->GetSocketFd(), reinterpret_cast(&addr6), &len6); + if (ret2 >= 0 && addr6.sin6_port) { connected = true; } } @@ -603,6 +642,9 @@ bool ExecClose(CloseContext *context) bool ExecGetState(GetStateContext *context) { + if (!context) { + return false; + } int opt; socklen_t optLen = sizeof(int); int r = getsockopt(context->GetSocketFd(), SOL_SOCKET, SO_TYPE, &opt, &optLen); @@ -631,7 +673,7 @@ bool ExecGetState(GetStateContext *context) addrLen = sizeof(addr6); } - if (addr == nullptr) { + if (!addr) { context->SetErrorCode(ADDRESS_INVALID); return false; } @@ -665,6 +707,9 @@ bool ExecGetState(GetStateContext *context) bool ExecGetRemoteAddress(GetRemoteAddressContext *context) { + if (!context) { + return false; + } sa_family_t family; socklen_t len = sizeof(family); int ret = getsockname(context->GetSocketFd(), reinterpret_cast(&family), &len); @@ -683,12 +728,12 @@ bool ExecGetRemoteAddress(GetRemoteAddressContext *context) return false; } - std::string address = MakeAddressString(reinterpret_cast(&addr4)); - if (address.empty()) { + std::string address1 = MakeAddressString(reinterpret_cast(&addr4)); + if (address1.empty()) { context->SetErrorCode(ADDRESS_INVALID); return false; } - context->address.SetAddress(address); + context->address.SetAddress(address1); context->address.SetFamilyBySaFamily(family); context->address.SetPort(ntohs(addr4.sin_port)); return true; @@ -702,12 +747,12 @@ bool ExecGetRemoteAddress(GetRemoteAddressContext *context) return false; } - std::string address = MakeAddressString(reinterpret_cast(&addr6)); - if (address.empty()) { + std::string address2 = MakeAddressString(reinterpret_cast(&addr6)); + if (address2.empty()) { context->SetErrorCode(ADDRESS_INVALID); return false; } - context->address.SetAddress(address); + context->address.SetAddress(address2); context->address.SetFamilyBySaFamily(family); context->address.SetPort(ntohs(addr6.sin6_port)); return true; @@ -718,6 +763,9 @@ bool ExecGetRemoteAddress(GetRemoteAddressContext *context) bool ExecTcpSetExtraOptions(TcpSetExtraOptionsContext *context) { + if (!context) { + return false; + } if (!SetBaseOptions(context->GetSocketFd(), &context->options)) { context->SetErrorCode(errno); return false; @@ -760,6 +808,9 @@ bool ExecTcpSetExtraOptions(TcpSetExtraOptionsContext *context) bool ExecUdpSetExtraOptions(UdpSetExtraOptionsContext *context) { + if (!context) { + return false; + } if (!SetBaseOptions(context->GetSocketFd(), &context->options)) { context->SetErrorCode(errno); return false; @@ -778,6 +829,9 @@ bool ExecUdpSetExtraOptions(UdpSetExtraOptionsContext *context) napi_value BindCallback(BindContext *context) { + if (!context) { + return nullptr; + } context->Emit(EVENT_LISTENING, std::make_pair(NapiUtils::GetUndefined(context->GetEnv()), NapiUtils::GetUndefined(context->GetEnv()))); return NapiUtils::GetUndefined(context->GetEnv()); @@ -785,6 +839,9 @@ napi_value BindCallback(BindContext *context) napi_value UdpSendCallback(UdpSendContext *context) { + if (!context) { + return nullptr; + } sa_family_t family; socklen_t len = sizeof(family); int ret = getsockname(context->GetSocketFd(), reinterpret_cast(&family), &len); @@ -812,6 +869,9 @@ napi_value UdpSendCallback(UdpSendContext *context) napi_value ConnectCallback(ConnectContext *context) { + if (!context) { + return nullptr; + } context->Emit(EVENT_CONNECT, std::make_pair(NapiUtils::GetUndefined(context->GetEnv()), NapiUtils::GetUndefined(context->GetEnv()))); if (!PollRecvData(context->GetSocketFd(), nullptr, 0, TcpMessageCallback(context))) { @@ -822,6 +882,9 @@ napi_value ConnectCallback(ConnectContext *context) napi_value TcpSendCallback(TcpSendContext *context) { + if (!context) { + return nullptr; + } if (!PollRecvData(context->GetSocketFd(), nullptr, 0, TcpMessageCallback(context))) { EmitError(context, errno); } @@ -830,6 +893,9 @@ napi_value TcpSendCallback(TcpSendContext *context) napi_value CloseCallback(CloseContext *context) { + if (!context) { + return nullptr; + } int ret = close(context->GetSocketFd()); if (ret < 0) { NETSTACK_LOGE("sock closed error %{public}s sock = %{public}d, ret = %{public}d", strerror(errno), @@ -845,6 +911,9 @@ napi_value CloseCallback(CloseContext *context) napi_value GetStateCallback(GetStateContext *context) { + if (!context) { + return nullptr; + } napi_value obj = NapiUtils::CreateObject(context->GetEnv()); if (NapiUtils::GetValueType(context->GetEnv(), obj) != napi_object) { return NapiUtils::GetUndefined(context->GetEnv()); @@ -859,6 +928,9 @@ napi_value GetStateCallback(GetStateContext *context) napi_value GetRemoteAddressCallback(GetRemoteAddressContext *context) { + if (!context) { + return nullptr; + } napi_value obj = NapiUtils::CreateObject(context->GetEnv()); if (NapiUtils::GetValueType(context->GetEnv(), obj) != napi_object) { return NapiUtils::GetUndefined(context->GetEnv()); @@ -878,6 +950,9 @@ napi_value TcpSetExtraOptionsCallback(TcpSetExtraOptionsContext *context) napi_value UdpSetExtraOptionsCallback(UdpSetExtraOptionsContext *context) { + if (!context) { + return nullptr; + } return NapiUtils::GetUndefined(context->GetEnv()); } } // namespace OHOS::NetStack::SocketExec diff --git a/frameworks/js/napi/socket/socket_module/include/socket_module.h b/frameworks/js/napi/socket/socket_module/include/socket_module.h index 7b6c8c9eb4aeefe7c0a9dd6d7270f0f98c0bc073..0885fc76ae6e85ce34f319dbf87709092875e7ce 100644 --- a/frameworks/js/napi/socket/socket_module/include/socket_module.h +++ b/frameworks/js/napi/socket/socket_module/include/socket_module.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_SOCKET_MODULE_H -#define COMMUNICATIONNETSTACK_SOCKET_MODULE_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_SOCKET_MODULE_INCLUDE_SOCKET_MODULE_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_SOCKET_MODULE_INCLUDE_SOCKET_MODULE_H_ #include "napi/native_api.h" @@ -68,6 +68,14 @@ public: static constexpr const char *INTERFACE_UDP_SOCKET = "UDPSocket"; static constexpr const char *INTERFACE_TCP_SOCKET = "TCPSocket"; + /** + * @brief Initialize the socket module + * + * @param env env + * @param exports exports + * + * @return exports value + */ static napi_value InitSocketModule(napi_env env, napi_value exports); private: @@ -82,4 +90,4 @@ private: static void InitSocketProperties(napi_env env, napi_value exports); }; } // namespace OHOS::NetStack -#endif // COMMUNICATIONNETSTACK_SOCKET_MODULE_H \ No newline at end of file +#endif // FRAMEWORKS_JS_NAPI_SOCKET_SOCKET_MODULE_INCLUDE_SOCKET_MODULE_H_ 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 f2f761325d2cd1aa47c4e0b1c9025bb7c3638aa5..85067fcab85867a0a66365a45fa071304fa92816 100644 --- a/frameworks/js/napi/socket/socket_module/src/socket_module.cpp +++ b/frameworks/js/napi/socket/socket_module/src/socket_module.cpp @@ -51,7 +51,7 @@ void Finalize(napi_env, void *data, void *) { NETSTACK_LOGI("socket handle is finalized"); auto manager = static_cast(data); - if (manager != nullptr) { + if (manager) { int sock = (int)(uint64_t)manager->GetData(); if (sock != 0) { close(sock); @@ -80,6 +80,9 @@ bool PushTask(napi_env env, napi_value thisVal, Context *context) static bool SetSocket(napi_env env, napi_value thisVal, BindContext *context, int sock) { + if (!context || !env || !thisVal || !sock) { + return false; + } if (sock < 0) { napi_value error = NapiUtils::CreateObject(env); if (NapiUtils::GetValueType(env, error) != napi_object) { @@ -91,7 +94,7 @@ static bool SetSocket(napi_env env, napi_value thisVal, BindContext *context, in } EventManager *manager = nullptr; - if (napi_unwrap(env, thisVal, reinterpret_cast(&manager)) != napi_ok || manager == nullptr) { + if (napi_unwrap(env, thisVal, reinterpret_cast(&manager)) != napi_ok || !manager) { return false; } @@ -102,6 +105,9 @@ static bool SetSocket(napi_env env, napi_value thisVal, BindContext *context, in static bool MakeTcpSocket(napi_env env, napi_value thisVal, BindContext *context) { + if (!context || !env || !thisVal) { + return false; + } int sock = SocketExec::MakeTcpSocket(context->address.GetSaFamily()); if (!SetSocket(env, thisVal, context, sock)) { return false; @@ -112,6 +118,9 @@ static bool MakeTcpSocket(napi_env env, napi_value thisVal, BindContext *context static bool MakeUdpSocket(napi_env env, napi_value thisVal, BindContext *context) { + if (!context) { + return false; + } int sock = SocketExec::MakeUdpSocket(context->address.GetSaFamily()); if (!SetSocket(env, thisVal, context, sock)) { return false; @@ -282,4 +291,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 57a56c79abeaaca71856521b6bc25e2d81111d3c..f6e9d370abc78c2119c15d50d2618c806c6e3831 100644 --- a/frameworks/js/napi/socket/task_queue/include/task_queue.h +++ b/frameworks/js/napi/socket/task_queue/include/task_queue.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_TASK_QUEUE_H -#define COMMUNICATIONNETSTACK_TASK_QUEUE_H +#ifndef FRAMEWORKS_JS_NAPI_SOCKET_TASK_QUEUE_INCLUDE_TASK_QUEUE_H_ +#define FRAMEWORKS_JS_NAPI_SOCKET_TASK_QUEUE_INCLUDE_TASK_QUEUE_H_ #include "netstack_base_context.h" @@ -34,4 +34,4 @@ void Callback(napi_env env, napi_status status, void *data); void PushTask(TaskPriority priority, AsyncWorkExecutor executor, AsyncWorkCallback callback, void *data); } // namespace OHOS::NetStack::Task -#endif /* COMMUNICATIONNETSTACK_TASK_QUEUE_H */ +#endif // FRAMEWORKS_JS_NAPI_SOCKET_TASK_QUEUE_INCLUDE_TASK_QUEUE_H_ diff --git a/frameworks/js/napi/socket/task_queue/src/task_queue.cpp b/frameworks/js/napi/socket/task_queue/src/task_queue.cpp index 5ed466dc22ae36e593752f637ebac38dc1daed94..9e24742a4cdec1a201d069d58803356e591b3458 100644 --- a/frameworks/js/napi/socket/task_queue/src/task_queue.cpp +++ b/frameworks/js/napi/socket/task_queue/src/task_queue.cpp @@ -62,8 +62,9 @@ void Executor(napi_env env, void *data) } auto context = static_cast(data); - context->SetExecOK(true); - + if (context) { + context->SetExecOK(true); + } Task task = g_taskExecutorQueue.top(); g_taskExecutorQueue.pop(); task.executor(env, task.data); @@ -76,6 +77,10 @@ void Callback(napi_env env, napi_status status, void *data) (void)status; + if (!data) { + return; + } + auto deleter = [](BaseContext *context) { delete context; }; std::unique_ptr context(static_cast(data), deleter); diff --git a/frameworks/js/napi/websocket/include/websocket_napi.h b/frameworks/js/napi/websocket/include/websocket_napi.h index 6894c85b9944db2f2eee3e520bfba8fbd3107ebb..ac9957b1b9e22bd4aa14f01ea5ffacf0355ec0b0 100644 --- a/frameworks/js/napi/websocket/include/websocket_napi.h +++ b/frameworks/js/napi/websocket/include/websocket_napi.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef WEBSOCKET_NAPI_H -#define WEBSOCKET_NAPI_H +#ifndef FRAMEWORKS_JS_NAPI_WEBSOCKET_INCLUDE_WEBSOCKET_NAPI_H_ +#define FRAMEWORKS_JS_NAPI_WEBSOCKET_INCLUDE_WEBSOCKET_NAPI_H_ #include "napi/native_api.h" #include "napi/native_common.h" @@ -23,4 +23,4 @@ namespace OHOS { namespace NetStack { } // namespace NetStack } // namespace OHOS -#endif // WEBSOCKET_NAPI_H +#endif // FRAMEWORKS_JS_NAPI_WEBSOCKET_INCLUDE_WEBSOCKET_NAPI_H_ diff --git a/test/napi/common/test_common.h b/test/napi/common/test_common.h index f149add385a80bb3562dbdcdbaa25ce6ca3fdd6e..94211d282d4b57f42acd42dc0e55e6e0cc1b4388 100644 --- a/test/napi/common/test_common.h +++ b/test/napi/common/test_common.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_TEST_COMMON_H -#define COMMUNICATIONNETSTACK_TEST_COMMON_H +#ifndef TEST_NAPI_COMMON_TEST_COMMON_H_ +#define TEST_NAPI_COMMON_TEST_COMMON_H_ #include @@ -54,7 +54,23 @@ class NativeEngineTest : public testing::Test { public: NativeEngineTest(); ~NativeEngineTest() override; + + /** + * @brief how to set up the environment. + * + * @param + * + * @return + */ void SetUp() override {} + + /** + * @brief how to tear down the environment + * + * @param + * + * @return + */ void TearDown() override {} protected: @@ -67,4 +83,4 @@ NativeEngineTest::NativeEngineTest() : engine_(g_nativeEngine) {} NativeEngineTest::~NativeEngineTest() = default; -#endif /* COMMUNICATIONNETSTACK_TEST_COMMON_H */ +#endif // TEST_NAPI_COMMON_TEST_COMMON_H_ diff --git a/test/napi/http/test_napi_exec.cpp b/test/napi/http/test_napi_exec.cpp index 97f7cfbe036613d55de37163f049d79555b41a65..71ca0613dc55ffd774b10df3e6fcaeedb7dd50de 100644 --- a/test/napi/http/test_napi_exec.cpp +++ b/test/napi/http/test_napi_exec.cpp @@ -238,6 +238,9 @@ void CallOff(napi_env env, napi_value thisVal, napi_value callback) void CallPromiseThenCatch(napi_env env, napi_value promise, napi_value callback) { + if (!promise || !callback) { + return ; + } bool isPromise = false; napi_is_promise(env, promise, &isPromise); if (!isPromise) { @@ -246,7 +249,7 @@ void CallPromiseThenCatch(napi_env env, napi_value promise, napi_value callback) JSValue quickJsPromise = reinterpret_cast(promise)->GetJsValue(); - JSContext *ctx = ((QuickJSNativeEngine *)g_nativeEngine)->GetContext(); + JSContext *ctx = reinterpret_cast(g_nativeEngine)->GetContext(); JSValue then = JS_GetPropertyStr(ctx, quickJsPromise, "then"); JSValue callbackValue = reinterpret_cast(callback)->GetJsValue(); @@ -512,15 +515,15 @@ DEFINE_TEST_BEGIN(TestHttpModuleMethodPostExtraDataArrayBuffer, true) FILE *fp = fopen("for-post.png", "rb"); fseek(fp, 0L, SEEK_END); - long size = ftell(fp); + int64 size = ftell(fp); void *buffer = malloc(size); fseek(fp, 0, SEEK_SET); - size_t readSize = fread((void *)buffer, 1, size, fp); + size_t readSize = fread(reinterpret_castbuffer, 1, size, fp); NETSTACK_LOGI("file size = %ld read size = %zu", size, readSize); void *data = nullptr; napi_value extraData = NapiUtils::CreateArrayBuffer(env, readSize, &data); - memcpy_s(data, readSize, buffer, readSize); + (void)memcpy_s(data, readSize, buffer, readSize); NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_HEADER), header); NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_EXTRA_DATA), extraData); @@ -622,12 +625,12 @@ int main(int argc, char **argv) JSRuntime *rt = JS_NewRuntime(); - if (rt == nullptr) { + if (!rt) { return 0; } JSContext *ctx = JS_NewContext(rt); - if (ctx == nullptr) { + if (!ctx) { return 0; } @@ -641,4 +644,4 @@ int main(int argc, char **argv) g_nativeEngine->Loop(LOOP_DEFAULT); return 0; -} \ No newline at end of file +} diff --git a/test/napi/socket/test_napi_exec.cpp b/test/napi/socket/test_napi_exec.cpp index 4d07f073db02615eee0a4d6cf39054f40f3a8690..9392f5fd9e4faae20c5ba75d9bc4d373c3163a57 100644 --- a/test/napi/socket/test_napi_exec.cpp +++ b/test/napi/socket/test_napi_exec.cpp @@ -122,7 +122,7 @@ size_t len = 0; \ void *data = NapiUtils::GetInfoFromArrayBufferValue(env, msgBuffer, &len); \ std::string s; \ - s.append((char *)data, len); \ + s.append(reinterpret_castdata, len); \ NETSTACK_LOGI("data:\n%s", s.c_str()); \ \ NETSTACK_LOGI("\n\n\n"); \ @@ -155,10 +155,10 @@ } else { \ FILE *fp = fopen("for-post.png", "rb"); \ fseek(fp, 0L, SEEK_END); \ - long size = ftell(fp); \ + int64 size = ftell(fp); \ void *buffer = malloc(size); \ fseek(fp, 0, SEEK_SET); \ - size_t readSize = fread((void *)buffer, 1, size, fp); \ + size_t readSize = fread(reinterpret_castbuffer, 1, size, fp); \ NETSTACK_LOGI("file size = %ld read size = %zu", size, readSize); \ \ sendStr = (const char *)buffer; \ @@ -167,7 +167,7 @@ \ void *data = nullptr; \ napi_value msgBuffer = NapiUtils::CreateArrayBuffer(env, len, &data); \ - memcpy_s(data, (size_t)len, sendStr, (size_t)len); \ + (void)memcpy_s(data, (size_t)len, sendStr, (size_t)len); \ napi_value tcpSendOptions = NapiUtils::CreateObject(env); \ NapiUtils::SetNamedProperty(env, (napi_value)tcpSendOptions, KEY_DATA, msgBuffer); \ \ @@ -208,10 +208,10 @@ } else { \ FILE *fp = fopen("for-post.png", "rb"); \ fseek(fp, 0L, SEEK_END); \ - long size = ftell(fp); \ + int64 size = ftell(fp); \ void *buffer = malloc(size); \ fseek(fp, 0, SEEK_SET); \ - size_t readSize = fread((void *)buffer, 1, size, fp); \ + size_t readSize = fread(reinterpret_castbuffer, 1, size, fp); \ NETSTACK_LOGI("file size = %ld read size = %zu", size, readSize); \ \ sendStr = (const char *)buffer; \ @@ -220,7 +220,7 @@ \ void *data = nullptr; \ napi_value msgBuffer = NapiUtils::CreateArrayBuffer(env, len, &data); \ - memcpy_s(data, len, sendStr, len); \ + (void)memcpy_s(data, len, sendStr, len); \ \ napi_value udpSendOptions = NapiUtils::CreateObject(env); \ NapiUtils::SetNamedProperty(env, (napi_value)udpSendOptions, KEY_ADDRESS, udpSendAddress); \ @@ -319,12 +319,12 @@ int main(int argc, char **argv) JSRuntime *rt = JS_NewRuntime(); - if (rt == nullptr) { + if (!rt) { return 0; } JSContext *ctx = JS_NewContext(rt); - if (ctx == nullptr) { + if (!ctx) { return 0; } @@ -338,4 +338,4 @@ int main(int argc, char **argv) g_nativeEngine->Loop(LOOP_DEFAULT); return 0; -} \ No newline at end of file +} diff --git a/test/utils/napi_utils/unittest/test_napi_utils.cpp b/test/utils/napi_utils/unittest/test_napi_utils.cpp index b5d44662461b9060daa4aa386ba6ca7b85721424..050ecb202ef71cc9b25e2e502d94db02a4249dfb 100644 --- a/test/utils/napi_utils/unittest/test_napi_utils.cpp +++ b/test/utils/napi_utils/unittest/test_napi_utils.cpp @@ -421,12 +421,12 @@ int main(int argc, char **argv) JSRuntime *rt = JS_NewRuntime(); - if (rt == nullptr) { + if (!rt) { return 0; } JSContext *ctx = JS_NewContext(rt); - if (ctx == nullptr) { + if (!ctx) { return 0; } diff --git a/utils/base_async_work/include/netstack_base_async_work.h b/utils/base_async_work/include/netstack_base_async_work.h index 6a63f702f6e1be7f6bd123c57378f6fcc7d14921..19cef912ad9ff3af090ca3b85899d84e500498f5 100644 --- a/utils/base_async_work/include/netstack_base_async_work.h +++ b/utils/base_async_work/include/netstack_base_async_work.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_NETSTACK_BASE_ASYNC_WORK_H -#define COMMUNICATIONNETSTACK_NETSTACK_BASE_ASYNC_WORK_H +#ifndef UTILS_BASE_ASYNC_WORK_INCLUDE_NETSTACK_BASE_ASYNC_WORK_H_ +#define UTILS_BASE_ASYNC_WORK_INCLUDE_NETSTACK_BASE_ASYNC_WORK_H_ #include @@ -73,12 +73,12 @@ public: } else { argv[1] = NapiUtils::GetUndefined(env); } - if (argv[1] == nullptr) { + if (!argv[1]) { return; } } else { argv[0] = NapiUtils::CreateObject(env); - if (argv[0] == nullptr) { + if (!argv[0]) { return; } NapiUtils::SetInt32Property(env, argv[0], BUSINESS_ERROR_KEY, context->GetErrorCode()); @@ -119,4 +119,4 @@ public: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_NETSTACK_BASE_ASYNC_WORK_H */ +#endif // UTILS_BASE_ASYNC_WORK_INCLUDE_NETSTACK_BASE_ASYNC_WORK_H_ diff --git a/utils/base_context/include/netstack_base_context.h b/utils/base_context/include/netstack_base_context.h index cc80e5b27ffcfada8a9543eb0d55017a4934eed2..ad1d32ab5ce0e04585721d1230d1e74a16797765 100644 --- a/utils/base_context/include/netstack_base_context.h +++ b/utils/base_context/include/netstack_base_context.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_BASE_CONTEXT_H -#define COMMUNICATIONNETSTACK_BASE_CONTEXT_H +#ifndef UTILS_BASE_CONTEXT_INCLUDE_NETSTACK_BASE_CONTEXT_H_ +#define UTILS_BASE_CONTEXT_INCLUDE_NETSTACK_BASE_CONTEXT_H_ #include @@ -38,42 +38,178 @@ public: virtual ~BaseContext(); + /** + * @brief Set member variable parseOK_ + * + * @param parseOK parseOK + * + * @return + */ void SetParseOK(bool parseOK); + /** + * @brief Set member variable requestOK_ + * + * @param requestOK requestOK + * + * @return + */ void SetExecOK(bool requestOK); + /** + * @brief Set member variable errorCode_ + * + * @param errorCode errorCode + * + * @return + */ void SetErrorCode(int32_t errorCode); + /** + * @brief Set member variable callback_ + * + * @param callback callback + * + * @return + */ napi_status SetCallback(napi_value callback); + /** + * @brief Delete DeleteCallback + * + * @param + * + * @return + */ void DeleteCallback(); + /** + * @brief Create Async variablesWork + * + * @param name name + * @param executor executor + * @param callback callback + * + * @return + */ void CreateAsyncWork(const std::string &name, AsyncWorkExecutor executor, AsyncWorkCallback callback); + /** + * @brief Delete Async Work + * + * @param + * + * @return + */ void DeleteAsyncWork(); + /** + * @brief Create Promise + * + * @param + * + * @return napi_create_promise result + */ napi_value CreatePromise(); + /** + * @brief Get member variable parseOK_ + * + * @param + * + * @return parseOK_ value + */ [[nodiscard]] bool IsParseOK() const; + /** + * @brief Get member variable requestOK_ + * + * @param + * + * @return requestOK_ value + */ [[nodiscard]] bool IsExecOK() const; + /** + * @brief Get member variable env_ + * + * @param + * + * @return env_ value + */ [[nodiscard]] napi_env GetEnv() const; + /** + * @brief Get member variable errorCode_ + * + * @param + * + * @return errorCode_ value + */ [[nodiscard]] int32_t GetErrorCode() const; + /** + * @brief Get member variable callback_ + * + * @param + * + * @return callback_ + */ [[nodiscard]] napi_value GetCallback() const; + /** + * @brief Get member variable deferred_ + * + * @param + * + * @return deferred_ value + */ [[nodiscard]] napi_deferred GetDeferred() const; + /** + * @brief Get member variable asyncWorkName_ + * + * @param + * + * @return asyncWorkName_ value + */ [[nodiscard]] const std::string &GetAsyncWorkName() const; + /** + * @brief Emit + * + * @param type type + * @param argv argv + * + * @return + */ void Emit(const std::string &type, const std::pair &argv); + /** + * @brief Set member variable needPromise_ + * + * @param needPromise needPromise + * + * @return + */ void SetNeedPromise(bool needPromise); + /** + * @brief Get member variable needPromise_ + * + * @param + * + * @return needPromise_ value + */ [[nodiscard]] bool IsNeedPromise() const; + /** + * @brief Get member variable manager_ + * + * @param + * + * @return manager_ value + */ [[nodiscard]] EventManager *GetManager() const; protected: @@ -100,4 +236,4 @@ private: }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_BASE_CONTEXT_H */ +#endif // UTILS_BASE_CONTEXT_INCLUDE_NETSTACK_BASE_CONTEXT_H_ diff --git a/utils/base_context/src/netstack_base_context.cpp b/utils/base_context/src/netstack_base_context.cpp index 20162d60320dc2419e93117e22a23c4082758f6e..ef58ab29a4d283a038a9d111ee9bb70ae6336875 100644 --- a/utils/base_context/src/netstack_base_context.cpp +++ b/utils/base_context/src/netstack_base_context.cpp @@ -62,7 +62,7 @@ napi_status BaseContext::SetCallback(napi_value callback) void BaseContext::DeleteCallback() { - if (callback_ == nullptr) { + if (!callback_) { return; } (void)napi_delete_reference(env_, callback_); @@ -82,7 +82,7 @@ void BaseContext::CreateAsyncWork(const std::string &name, AsyncWorkExecutor exe void BaseContext::DeleteAsyncWork() { - if (asyncWork_ == nullptr) { + if (!asyncWork_) { return; } (void)napi_delete_async_work(env_, asyncWork_); @@ -117,7 +117,7 @@ int32_t BaseContext::GetErrorCode() const napi_value BaseContext::GetCallback() const { - if (callback_ == nullptr) { + if (!callback_) { return nullptr; } napi_value callback = nullptr; @@ -156,4 +156,4 @@ EventManager *BaseContext::GetManager() const { return manager_; } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/utils/common_utils/include/netstack_common_utils.h b/utils/common_utils/include/netstack_common_utils.h index 7fe078f73d06fba847e83581814509b062f4ee10..e9284b1a4614267c561c275b9aee141f3fd1aea5 100644 --- a/utils/common_utils/include/netstack_common_utils.h +++ b/utils/common_utils/include/netstack_common_utils.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_COMMON_UTILS_H -#define COMMUNICATIONNETSTACK_COMMON_UTILS_H +#ifndef UTILS_COMMON_UTILS_INCLUDE_NETSTACK_COMMON_UTILS_H_ +#define UTILS_COMMON_UTILS_INCLUDE_NETSTACK_COMMON_UTILS_H_ #include #include @@ -29,4 +29,4 @@ std::string Strip(const std::string &str, char ch = ' '); std::string ToLower(const std::string &s); } // namespace OHOS::NetStack::CommonUtils -#endif /* COMMUNICATIONNETSTACK_COMMON_UTILS_H */ +#endif // UTILS_COMMON_UTILS_INCLUDE_NETSTACK_COMMON_UTILS_H_ diff --git a/utils/common_utils/src/netstack_common_utils.cpp b/utils/common_utils/src/netstack_common_utils.cpp index 5e589c41d584b479a600dc8da80a3582b6f1e4a1..4c426d5d615a44571b84471a391cab9aacd55208 100644 --- a/utils/common_utils/src/netstack_common_utils.cpp +++ b/utils/common_utils/src/netstack_common_utils.cpp @@ -58,6 +58,9 @@ std::vector Split(const std::string &str, const std::string &sep, s std::string Strip(const std::string &str, char ch) { int64_t i = 0; + if (str.empty()) { + return ""; + } while (i < str.size() && str[i] == ch) { ++i; } @@ -77,4 +80,4 @@ std::string ToLower(const std::string &s) std::transform(res.begin(), res.end(), res.begin(), tolower); return res; } -} // namespace OHOS::NetStack::CommonUtils \ No newline at end of file +} // namespace OHOS::NetStack::CommonUtils diff --git a/utils/event_manager/include/netstack_event_listener.h b/utils/event_manager/include/netstack_event_listener.h index b15663611bf284e3b9f3721f12bfa86eed6f5c2b..f42b551ddd387a17f4d740b500348a89aee4e789 100644 --- a/utils/event_manager/include/netstack_event_listener.h +++ b/utils/event_manager/include/netstack_event_listener.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_EVENT_LISTENER_H -#define COMMUNICATIONNETSTACK_EVENT_LISTENER_H +#ifndef UTILS_EVENT_MANAGER_INCLUDE_NETSTACK_EVENT_LISTENER_H_ +#define UTILS_EVENT_MANAGER_INCLUDE_NETSTACK_EVENT_LISTENER_H_ #include @@ -33,22 +33,88 @@ public: ~EventListener(); + /** + * @brief operator + * + * @param listener listener + * + * @return + */ EventListener &operator=(const EventListener &listener); + /** + * @brief Emit + * + * @param eventType eventType + * @param argc argc + * @param argv argv + * + * @return + */ void Emit(const std::string &eventType, size_t argc, napi_value *argv) const; + /** + * @brief Match + * + * @param type type + * @param callback callback + * + * @return napi_strict_equals return + */ [[nodiscard]] bool Match(const std::string &type, napi_value callback) const; + /** + * @brief Match Once, Get member variable once_ + * + * @param type type + * + * @return once_ value + */ [[nodiscard]] bool MatchOnce(const std::string &type) const; + /** + * @brief Match Type + * + * @param + * + * @return true or false + */ [[nodiscard]] bool MatchType(const std::string &type) const; + /** + * @brief Get member variable asyncCallback_ + * + * @param + * + * @return asyncCallback_ value + */ [[nodiscard]] bool IsAsyncCallback() const; + /** + * @brief Emit By Uv + * + * @param + * + * @return + */ void EmitByUv(const std::string &type, void *data, void(Handler)(uv_work_t *, int status)) const; + /** + * @brief get env + * + * @param + * + * @return napi env + */ [[nodiscard]] napi_env GetEnv() const; + /** + * @brief Get Callback Ref + * + * @param + * + * @return napi ref + */ [[nodiscard]] napi_ref GetCallbackRef() const; private: @@ -74,4 +140,4 @@ struct UvWorkWrapper { }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_EVENT_LISTENER_H */ +#endif // UTILS_EVENT_MANAGER_INCLUDE_NETSTACK_EVENT_LISTENER_H_ diff --git a/utils/event_manager/include/netstack_event_manager.h b/utils/event_manager/include/netstack_event_manager.h index c421f5f4433254b232b96f32cd487b4378c9bbd4..ff1c8f6783b0e34b4b44678834fd2a73fc66032c 100644 --- a/utils/event_manager/include/netstack_event_manager.h +++ b/utils/event_manager/include/netstack_event_manager.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_EVENT_MANAGER_H -#define COMMUNICATIONNETSTACK_EVENT_MANAGER_H +#ifndef UTILS_EVENT_MANAGER_INCLUDE_NETSTACK_EVENT_MANAGER_H_ +#define UTILS_EVENT_MANAGER_INCLUDE_NETSTACK_EVENT_MANAGER_H_ #include #include @@ -26,18 +26,73 @@ class EventManager { public: EventManager(); + /** + * @brief Add Listener + * + * @param env env + * @param type type + * @param callback callback + * @param once once + * @param asyncCallback asyncCallback + * + * @return + */ void AddListener(napi_env env, const std::string &type, napi_value callback, bool once, bool asyncCallback); + /** + * @brief Delete Listener + * + * @param type type + * @param callback callback + * + * @return + */ void DeleteListener(const std::string &type, napi_value callback); + /** + * @brief Emit + * + * @param type type + * @param argv argv + * + * @return + */ void Emit(const std::string &type, const std::pair &argv); + /** + * @brief Set member variable data_ + * + * @param data *data + * + * @return + */ void SetData(void *data); + /** + * @brief Get member variable data_ + * + * @param + * + * @return data_ value + */ [[nodiscard]] void *GetData(); + /** + * @brief Emit By Uv + * + * @param + * + * @return + */ void EmitByUv(const std::string &type, void *data, void(Handler)(uv_work_t *, int status)); + /** + * @brief Has Event Listener + * + * @param + * + * @return true or false + */ bool HasEventListener(const std::string &type); private: @@ -48,4 +103,4 @@ private: void *data_; }; } // namespace OHOS::NetStack -#endif /* COMMUNICATIONNETSTACK_EVENT_MANAGER_H */ +#endif // UTILS_EVENT_MANAGER_INCLUDE_NETSTACK_EVENT_MANAGER_H_ diff --git a/utils/event_manager/src/netstack_event_listener.cpp b/utils/event_manager/src/netstack_event_listener.cpp index 29455f52ad1da244c920febd208001712ac43587..eeb3adb727a3b6653e4a545e1fc26fa8c752930e 100644 --- a/utils/event_manager/src/netstack_event_listener.cpp +++ b/utils/event_manager/src/netstack_event_listener.cpp @@ -34,7 +34,7 @@ EventListener::EventListener(const EventListener &listener) once_ = listener.once_; asyncCallback_ = listener.asyncCallback_; - if (listener.callbackRef_ == nullptr) { + if (!listener.callbackRef_) { callbackRef_ = nullptr; return; } @@ -57,7 +57,7 @@ EventListener &EventListener::operator=(const EventListener &listener) once_ = listener.once_; asyncCallback_ = listener.asyncCallback_; - if (listener.callbackRef_ == nullptr) { + if (!listener.callbackRef_) { callbackRef_ = nullptr; return *this; } @@ -72,7 +72,7 @@ void EventListener::Emit(const std::string &eventType, size_t argc, napi_value * return; } - if (callbackRef_ == nullptr) { + if (!callbackRef_) { return; } napi_value callback = NapiUtils::GetReference(env_, callbackRef_); diff --git a/utils/event_manager/src/netstack_event_manager.cpp b/utils/event_manager/src/netstack_event_manager.cpp index 59bc0fe02dd7c75d28807e669f9fcdff8364db9a..56040f98bd953d9fac5ac459c8d5ceb7ee8db7aa 100644 --- a/utils/event_manager/src/netstack_event_manager.cpp +++ b/utils/event_manager/src/netstack_event_manager.cpp @@ -52,12 +52,12 @@ void EventManager::Emit(const std::string &type, const std::pair bool { return listener.MatchType(type); }); } -} // namespace OHOS::NetStack \ No newline at end of file +} // namespace OHOS::NetStack diff --git a/utils/log/include/netstack_log.h b/utils/log/include/netstack_log.h index 4353ff3bcdcdb6c324ae556439f2f074c4f5d71f..e7def62ea6f57adeb204b28250c82e44e8614285 100644 --- a/utils/log/include/netstack_log.h +++ b/utils/log/include/netstack_log.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_NETSTACK_LOG -#define COMMUNICATIONNETSTACK_NETSTACK_LOG +#ifndef UTILS_LOG_INCLUDE_NETSTACK_LOG_H_ +#define UTILS_LOG_INCLUDE_NETSTACK_LOG_H_ #include #include @@ -86,4 +86,4 @@ static void NetStackPrintLog(const char *fmt, ...) #define NETSTACK_LOGI(fmt, ...) NETSTACK_HILOG_PRINT(Info, fmt, ##__VA_ARGS__) -#endif /* COMMUNICATIONNETSTACK_NETSTACK_LOG */ \ No newline at end of file +#endif // UTILS_LOG_INCLUDE_NETSTACK_LOG_H_ diff --git a/utils/module_template/include/netstack_module_template.h b/utils/module_template/include/netstack_module_template.h index 3a3626f01a8966698c6d6733ce740575c6b7d264..77dcc5cb9058d23908ea65fe9088a31f38888064 100644 --- a/utils/module_template/include/netstack_module_template.h +++ b/utils/module_template/include/netstack_module_template.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_NETSTACK_MODULE_TEMPLATE_H -#define COMMUNICATIONNETSTACK_NETSTACK_MODULE_TEMPLATE_H +#ifndef UTILS_MODULE_TEMPLATE_INCLUDE_NETSTACK_MODULE_TEMPLATE_H_ +#define UTILS_MODULE_TEMPLATE_INCLUDE_NETSTACK_MODULE_TEMPLATE_H_ #include @@ -27,6 +27,18 @@ namespace OHOS::NetStack::ModuleTemplate { typedef void (*Finalizer)(napi_env, void *data, void *); +/** + * @brief Interface + * + * @param env env + * @param info info + * @param asyncWorkName asyncWorkName + * @param Work Work + * @param executor executor + * @param callback callback + * + * @return + */ template napi_value Interface(napi_env env, napi_callback_info info, @@ -62,6 +74,16 @@ napi_value Interface(napi_env env, return NapiUtils::GetUndefined(env); } +/** + * @brief Interface With Out AsyncWork + * + * @param env env + * @param info info + * @param asyncWorkName asyncWorkName + * @param Work Work + * + * @return + */ template napi_value InterfaceWithOutAsyncWork(napi_env env, napi_callback_info info, bool (*Work)(napi_env, napi_value, Context *)) @@ -90,19 +112,68 @@ napi_value return NapiUtils::GetUndefined(env); } +/** + * @brief On + * + * @param env env + * @param info info + * @param events events + * @param asyncCallback asyncCallback + * + * @return + */ napi_value On(napi_env env, napi_callback_info info, const std::initializer_list &events, bool asyncCallback); +/** + * @brief Once + * + * @param env env + * @param info info + * @param events events + * @param asyncCallback asyncCallback + * + * @return + */ napi_value Once(napi_env env, napi_callback_info info, const std::initializer_list &events, bool asyncCallback); +/** + * @brief Off + * + * @param env env + * @param info info + * @param events events + * + * @return + */ napi_value Off(napi_env env, napi_callback_info info, const std::initializer_list &events); +/** + * @brief Define Class + * + * @param env env + * @param exports exports + * @param properties properties + * @param className className + * + * @return + */ void DefineClass(napi_env env, napi_value exports, const std::initializer_list &properties, const std::string &className); +/** + * @brief New Instance + * + * @param env env + * @param info info + * @param finalizer finalizer + * @param className className + * + * @return + */ napi_value NewInstance(napi_env env, napi_callback_info info, const std::string &className, Finalizer finalizer); } // namespace OHOS::NetStack::ModuleTemplate -#endif /* COMMUNICATIONNETSTACK_NETSTACK_MODULE_TEMPLATE_H */ +#endif // UTILS_MODULE_TEMPLATE_INCLUDE_NETSTACK_MODULE_TEMPLATE_H_ diff --git a/utils/module_template/src/netstack_module_template.cpp b/utils/module_template/src/netstack_module_template.cpp index d7e1284acfd5d7b2dc81a896e90b61f5d6c2078e..1d6b8e785f24a550c013ab3ae38e3791fd0abf8c 100644 --- a/utils/module_template/src/netstack_module_template.cpp +++ b/utils/module_template/src/netstack_module_template.cpp @@ -141,8 +141,10 @@ napi_value NewInstance(napi_env env, napi_callback_info info, const std::string NAPI_CALL(env, napi_new_instance(env, jsConstructor, 0, nullptr, &result)); auto manager = new EventManager(); - napi_wrap(env, result, reinterpret_cast(manager), finalizer, nullptr, nullptr); + if (manager) { + napi_wrap(env, result, reinterpret_cast(manager), finalizer, nullptr, nullptr); + } return result; } -} // namespace OHOS::NetStack::ModuleTemplate \ No newline at end of file +} // namespace OHOS::NetStack::ModuleTemplate diff --git a/utils/napi_utils/include/netstack_napi_utils.h b/utils/napi_utils/include/netstack_napi_utils.h index 720eae3067e6ef80d2fa035d30971871012b8394..f4096539ac1cdfa8b201a3d07219eb8259b8bd98 100644 --- a/utils/napi_utils/include/netstack_napi_utils.h +++ b/utils/napi_utils/include/netstack_napi_utils.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMMUNICATIONNETSTACK_NETSTACK_NAPI_UTILS_H -#define COMMUNICATIONNETSTACK_NETSTACK_NAPI_UTILS_H +#ifndef UTILS_NAPI_UTILS_INCLUDE_NETSTACK_NAPI_UTILS_H_ +#define UTILS_NAPI_UTILS_INCLUDE_NETSTACK_NAPI_UTILS_H_ #include #include @@ -113,4 +113,4 @@ napi_handle_scope OpenScope(napi_env env); void CloseScope(napi_env env, napi_handle_scope scope); } // namespace OHOS::NetStack::NapiUtils -#endif /* COMMUNICATIONNETSTACK_NETSTACK_NAPI_UTILS_H */ +#endif // UTILS_NAPI_UTILS_INCLUDE_NETSTACK_NAPI_UTILS_H_ diff --git a/utils/napi_utils/src/netstack_napi_utils.cpp b/utils/napi_utils/src/netstack_napi_utils.cpp index 171c66c533259ad17a4c5f9b9151bae277dee286..daec0a04e36a59671dbd592b05d3bff2db81b782 100644 --- a/utils/napi_utils/src/netstack_napi_utils.cpp +++ b/utils/napi_utils/src/netstack_napi_utils.cpp @@ -32,7 +32,7 @@ static constexpr const char *GLOBAL_JSON_PARSE = "parse"; napi_valuetype GetValueType(napi_env env, napi_value value) { - if (value == nullptr) { + if (!value) { return napi_undefined; } @@ -205,7 +205,7 @@ bool ValueIsArrayBuffer(napi_env env, napi_value value) void *GetInfoFromArrayBufferValue(napi_env env, napi_value value, size_t *length) { - if (length == nullptr) { + if (!length) { return nullptr; } @@ -216,7 +216,7 @@ void *GetInfoFromArrayBufferValue(napi_env env, napi_value value, size_t *length napi_value CreateArrayBuffer(napi_env env, size_t length, void **data) { - if (length == 0) { + if (!length) { return nullptr; } napi_value result = nullptr; @@ -390,4 +390,4 @@ void CloseScope(napi_env env, napi_handle_scope scope) { (void)napi_close_handle_scope(env, scope); } -} // namespace OHOS::NetStack::NapiUtils \ No newline at end of file +} // namespace OHOS::NetStack::NapiUtils