From f0ea45557c5463897521f70713d80b18993c906f Mon Sep 17 00:00:00 2001 From: root Date: Wed, 17 Jul 2024 17:19:08 +0800 Subject: [PATCH] add webview webdatabase 4 APIs Change-Id: I67555310f95c7c57f257e4148f52ff7726f26317 --- interfaces/kits/cj/BUILD.gn | 1 + interfaces/kits/cj/include/web_data_base.h | 40 ++++++++ interfaces/kits/cj/include/web_errors.h | 2 + interfaces/kits/cj/include/webview_ffi.h | 6 ++ interfaces/kits/cj/src/web_data_base.cpp | 110 +++++++++++++++++++++ interfaces/kits/cj/src/web_errors.cpp | 2 + interfaces/kits/cj/src/webview_ffi.cpp | 41 ++++++++ 7 files changed, 202 insertions(+) create mode 100644 interfaces/kits/cj/include/web_data_base.h create mode 100644 interfaces/kits/cj/src/web_data_base.cpp diff --git a/interfaces/kits/cj/BUILD.gn b/interfaces/kits/cj/BUILD.gn index 3603933fb..e53b6f2b9 100644 --- a/interfaces/kits/cj/BUILD.gn +++ b/interfaces/kits/cj/BUILD.gn @@ -39,6 +39,7 @@ ohos_shared_library("cj_webview_ffi") { "src/webview_javascript_execute_callback.cpp", "src/webview_javascript_result_callback.cpp", "src/webview_utils.cpp", + "src/web_data_base.cpp", ] deps = [ diff --git a/interfaces/kits/cj/include/web_data_base.h b/interfaces/kits/cj/include/web_data_base.h new file mode 100644 index 000000000..dc7dcfb42 --- /dev/null +++ b/interfaces/kits/cj/include/web_data_base.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WEB_DATA_BASE_H +#define WEB_DATA_BASE_H + +#include +#include + +namespace OHOS { +namespace NWeb { + +const int MAX_STRING_LENGTH = 40960; +const int MAX_PWD_LENGTH = 256; +class WebDataBase { +public: + WebDataBase() {} + ~WebDataBase() = default; + + static CArrString CJGetHttpAuthCredentials(const std::string &host, const std::string &realm); + static void CJSaveHttpAuthCredentials(const std::string &host, const std::string &realm, const std::string &username, const std::string &password); + static bool CJExistHttpAuthCredentials(); + static void CJDeleteHttpAuthCredentials(); +}; +} +} + +#endif \ No newline at end of file diff --git a/interfaces/kits/cj/include/web_errors.h b/interfaces/kits/cj/include/web_errors.h index a71d3776e..77b789254 100644 --- a/interfaces/kits/cj/include/web_errors.h +++ b/interfaces/kits/cj/include/web_errors.h @@ -47,6 +47,8 @@ constexpr ErrCode REGISTER_CUSTOM_SCHEME_FAILED = 17100020; constexpr ErrCode RESOURCE_HANDLER_INVALID = 17100021; constexpr ErrCode HTTP_BODY_STREAN_INIT_FAILED = 17100022; +constexpr ErrCode HTTP_AUTH_MALLOC_FAILED = 17100023; + std::string GetErrMsgByErrCode(ErrCode code); } } diff --git a/interfaces/kits/cj/include/webview_ffi.h b/interfaces/kits/cj/include/webview_ffi.h index af456fc2c..8faf3fe65 100644 --- a/interfaces/kits/cj/include/webview_ffi.h +++ b/interfaces/kits/cj/include/webview_ffi.h @@ -85,6 +85,12 @@ extern "C" { FFI_EXPORT bool FfiOHOSCookieMgrExistCookie(bool incognitoMode); FFI_EXPORT void FfiOHOSCookieMgrClearAllCookiesSync(bool incognitoMode); FFI_EXPORT void FfiOHOSCookieMgrClearSessionCookieSync(); + + // data_base + FFI_EXPORT RetDataCArrString FfiOHOSDBGetHttpAuthCredentials(const char *host, const char *realm); + FFI_EXPORT void FfiOHOSDBSaveHttpAuthCredentials(const char *host, const char *realm, const char *username, const char *password); + FFI_EXPORT bool FfiOHOSDBExistHttpAuthCredentials(); + FFI_EXPORT void FfiOHOSDBDeleteHttpAuthCredentials(); } #endif // WEBVIEW_FFI_H \ No newline at end of file diff --git a/interfaces/kits/cj/src/web_data_base.cpp b/interfaces/kits/cj/src/web_data_base.cpp new file mode 100644 index 000000000..07b29a838 --- /dev/null +++ b/interfaces/kits/cj/src/web_data_base.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2024 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. + */ + +#include "web_data_base.h" +#include "nweb_data_base.h" +#include "nweb_helper.h" +#include "web_errors.h" +#include "webview_utils.h" +#include "webview_log.h" +#include "string.h" + +namespace OHOS { +namespace NWeb { +const int DEFAULT_AUTH_LENGTH = 2; +const int HTTP_AUTH_INIT_LENGTH = -1; + +CArrString WebDataBase::CJGetHttpAuthCredentials(const std::string &host, const std::string &realm) +{ + CArrString ret = {nullptr, HTTP_AUTH_INIT_LENGTH}; + std::string username_s; + char password[MAX_PWD_LENGTH + 1] = {0}; + + char** result = static_cast(malloc(sizeof(char*) * DEFAULT_AUTH_LENGTH)); + if (result == nullptr) { + WEBVIEWLOGI("Webdatabase getHttpAuthCredentials malloc result failed!"); + return ret; + } + + result[0] = static_cast(malloc(sizeof(char) * (MAX_STRING_LENGTH + 1))); + if (result[0] == nullptr) { + WEBVIEWLOGI("Webdatabase getHttpAuthCredentials malloc username failed!"); + free(result); + return ret; + } + + result[1] = static_cast(malloc(sizeof(char) * (MAX_PWD_LENGTH + 1))); + if (result[1] == nullptr) { + WEBVIEWLOGI("Webdatabase getHttpAuthCredentials malloc password failed!"); + free(result[0]); + free(result); + return ret; + } + + std::shared_ptr database = NWebHelper::Instance().GetDataBase(); + if (database != nullptr) { + database->GetHttpAuthCredentials(host, realm, username_s, password, MAX_PWD_LENGTH + 1); + } + + if (username_s.empty() || strlen(password) == 0) { + ret.size = 0; + return ret; + } + + result[0] = OHOS::Webview::MallocCString(username_s); + if (result[0] == nullptr) { + WEBVIEWLOGI("Webdatabase getHttpAuthCredentials transfer username_s failed!"); + for (int i = 0; i < DEFAULT_AUTH_LENGTH; i++) + free(result[i]); + free(result); + return ret; + } + + result[1] = std::char_traits::copy(result[1], password, MAX_PWD_LENGTH); + ret.head = result; + ret.size = DEFAULT_AUTH_LENGTH; + return ret; +} + +void WebDataBase::CJSaveHttpAuthCredentials(const std::string &host, const std::string &realm, const std::string &username, const std::string &password) +{ + // get web database instance; + std::shared_ptr database = NWebHelper::Instance().GetDataBase(); + if (database != nullptr) { + database->SaveHttpAuthCredentials(host, realm, username, password.c_str()); + } +} + +bool WebDataBase::CJExistHttpAuthCredentials() +{ + bool isExist = false; + // get web database instance; + std::shared_ptr database = NWebHelper::Instance().GetDataBase(); + if (database != nullptr) { + isExist = database->ExistHttpAuthCredentials(); + } + return isExist; +} + +void WebDataBase::CJDeleteHttpAuthCredentials() +{ + // get web database instance; + std::shared_ptr database = NWebHelper::Instance().GetDataBase(); + if (database != nullptr) { + database->DeleteHttpAuthCredentials(); + } +} +} +} \ No newline at end of file diff --git a/interfaces/kits/cj/src/web_errors.cpp b/interfaces/kits/cj/src/web_errors.cpp index 93295db9a..2799bfbf3 100644 --- a/interfaces/kits/cj/src/web_errors.cpp +++ b/interfaces/kits/cj/src/web_errors.cpp @@ -41,6 +41,7 @@ const std::string DOWNLOAD_NOT_START_MSG = "The download task is not started yet const std::string REGISTER_CUSTOM_SCHEME_FAILED_MSG = "Failed to register custom schemes."; const std::string HTTP_BODY_STREAN_INIT_FAILED_MSG = "Failed to initialize the HTTP body stream."; const std::string RESOURCE_HANDLER_INVALID_MSG = "The resource handler is invalid."; +const std::string HTTP_AUTH_MALLOC_FAILED_MSG = "Failed to malloc string memory to get HttpAuth."; } namespace OHOS { @@ -67,6 +68,7 @@ std::unordered_map g_errCodeMsgMap = { {REGISTER_CUSTOM_SCHEME_FAILED, REGISTER_CUSTOM_SCHEME_FAILED_MSG}, {HTTP_BODY_STREAN_INIT_FAILED, HTTP_BODY_STREAN_INIT_FAILED_MSG}, {RESOURCE_HANDLER_INVALID, RESOURCE_HANDLER_INVALID_MSG}, + {HTTP_AUTH_MALLOC_FAILED, HTTP_AUTH_MALLOC_FAILED_MSG}, }; std::string GetErrMsgByErrCode(ErrCode code) diff --git a/interfaces/kits/cj/src/webview_ffi.cpp b/interfaces/kits/cj/src/webview_ffi.cpp index 62028c2aa..79266fed4 100644 --- a/interfaces/kits/cj/src/webview_ffi.cpp +++ b/interfaces/kits/cj/src/webview_ffi.cpp @@ -22,6 +22,7 @@ #include "webview_log.h" #include "parameters.h" #include "web_cookie_manager.h" +#include "web_data_base.h" #include "pixel_map.h" #include "cj_lambda.h" #include "pixel_map_impl.h" @@ -762,6 +763,46 @@ extern "C" { *errCode = NWebError::NO_ERROR; return ret; } + + // web data base; + RetDataCArrString FfiOHOSDBGetHttpAuthCredentials(const char *host, const char *realm) + { + std::string host_s = std::string(host); + std::string realm_s = std::string(realm); + + CArrString result = OHOS::NWeb::WebDataBase::CJGetHttpAuthCredentials(host_s, realm_s); + RetDataCArrString ret; + + if (result.size == -1) { + ret.code = NWebError::HTTP_AUTH_MALLOC_FAILED; + } + else { + ret.code = NWebError::NO_ERROR; + } + + ret.data = result; + return ret; + } + + void FfiOHOSDBSaveHttpAuthCredentials(const char *host, const char *realm, const char *username, const char *password) + { + std::string host_s = std::string(host); + std::string realm_s = std::string(realm); + std::string username_s = std::string(username); + std::string password_s = std::string(password); + + OHOS::NWeb::WebDataBase::CJSaveHttpAuthCredentials(host_s, realm_s, username_s, password_s); + } + + bool FfiOHOSDBExistHttpAuthCredentials() + { + return OHOS::NWeb::WebDataBase::CJExistHttpAuthCredentials(); + } + + void FfiOHOSDBDeleteHttpAuthCredentials() + { + OHOS::NWeb::WebDataBase::CJDeleteHttpAuthCredentials(); + } } } } -- Gitee