From a46cb6ac4d81956ca657e003d98b9e0edb83a955 Mon Sep 17 00:00:00 2001 From: lixiang Date: Tue, 31 Oct 2023 18:20:25 +0800 Subject: [PATCH] add PostUrl API Signed-off-by: lixiang --- ohos_nweb/include/nweb.h | 11 +++++++++++ ohos_nweb/src/cef_delegate/nweb_delegate.cc | 19 +++++++++++++++++++ ohos_nweb/src/cef_delegate/nweb_delegate.h | 1 + ohos_nweb/src/nweb_delegate_interface.h | 1 + ohos_nweb/src/nweb_impl.cc | 8 ++++++++ ohos_nweb/src/nweb_impl.h | 1 + 6 files changed, 41 insertions(+) diff --git a/ohos_nweb/include/nweb.h b/ohos_nweb/include/nweb.h index 1dec8c978e..f661215e88 100644 --- a/ohos_nweb/include/nweb.h +++ b/ohos_nweb/include/nweb.h @@ -792,6 +792,17 @@ class OHOS_NWEB_EXPORT NWeb : public std::enable_shared_from_this { * Set the virtual keyboard to override the web status. */ virtual bool ShouldVirtualKeyboardOverlay() = 0; + + /** + * load the URL with postData using POST method. + * + * @param url String: the URL of the resource to load This value cannot be null. + * @param postData the data will be passed to "POST" request, + * whilch must be "application/x-www-form-urlencoded" encoded. + * + * @return title string for the current page. + */ + virtual int PostUrl(const std::string& url, std::vector& postData) = 0; }; } // namespace OHOS::NWeb diff --git a/ohos_nweb/src/cef_delegate/nweb_delegate.cc b/ohos_nweb/src/cef_delegate/nweb_delegate.cc index 4cc7c77698..5d572f38cf 100644 --- a/ohos_nweb/src/cef_delegate/nweb_delegate.cc +++ b/ohos_nweb/src/cef_delegate/nweb_delegate.cc @@ -678,6 +678,25 @@ int NWebDelegate::Load(const std::string& url) { return NWEB_OK; } +int NWebDelegate::PostUrl(const std::string& url, std::vector& postData) { + GURL gurl = GURL(url); + if (gurl.is_empty() || !gurl.is_valid()) { + GURL gurlWithHttp = GURL("https://" + url); + if (!gurlWithHttp.is_valid()) { + return NWEB_INVALID_URL; + } + } + LOG(DEBUG) << "NWebDelegate::PostUrl url=" << url; + auto browser = GetBrowser(); + if (browser == nullptr) { + LOG(ERROR) << "NWebDelegate::PostUrl browser is nullptr"; + return NWEB_ERR; + } + browser->GetMainFrame()->PostURL(CefString(url), postData); + RequestVisitedHistory(); + return NWEB_OK; +} + bool NWebDelegate::IsNavigatebackwardAllowed() const { LOG(DEBUG) << "NWebDelegate::IsNavigatebackwardAllowed"; if (GetBrowser().get()) { diff --git a/ohos_nweb/src/cef_delegate/nweb_delegate.h b/ohos_nweb/src/cef_delegate/nweb_delegate.h index 7c96b23dbd..d4392e5b67 100644 --- a/ohos_nweb/src/cef_delegate/nweb_delegate.h +++ b/ohos_nweb/src/cef_delegate/nweb_delegate.h @@ -238,6 +238,7 @@ class NWebDelegate : public NWebDelegateInterface, public virtual CefRefCount { void SetToken(void* token) override; void SetVirtualPixelRatio(float ratio) override; void SetNestedScrollMode(const NestedScrollMode& nestedScrollMode) override; + int PostUrl(const std::string& url, std::vector& postData) override; public: int argc_; diff --git a/ohos_nweb/src/nweb_delegate_interface.h b/ohos_nweb/src/nweb_delegate_interface.h index 5da4bfc134..63a67460e5 100644 --- a/ohos_nweb/src/nweb_delegate_interface.h +++ b/ohos_nweb/src/nweb_delegate_interface.h @@ -243,6 +243,7 @@ class NWebDelegateInterface virtual void SetNestedScrollMode(const NestedScrollMode& nestedScrollMode) = 0; virtual void SetVirtualKeyBoardArg(int32_t width, int32_t height, double keyboard) = 0; virtual bool ShouldVirtualKeyboardOverlay() = 0; + virtual int PostUrl(const std::string& url, std::vector& postData) = 0; }; } // namespace OHOS::NWeb diff --git a/ohos_nweb/src/nweb_impl.cc b/ohos_nweb/src/nweb_impl.cc index dc18ea2062..e14a89dc2d 100644 --- a/ohos_nweb/src/nweb_impl.cc +++ b/ohos_nweb/src/nweb_impl.cc @@ -842,6 +842,14 @@ int NWebImpl::Load(std::string& url, return nweb_delegate_->Load(url, additionalHttpHeaders); } +int NWebImpl::PostUrl(const std::string& url, + std::vector& postData) { + if (nweb_delegate_ == nullptr) { + return NWEB_ERR; + } + return nweb_delegate_->PostUrl(url, postData); +} + int NWebImpl::LoadWithDataAndBaseUrl(const std::string& baseUrl, const std::string& data, const std::string& mimeType, diff --git a/ohos_nweb/src/nweb_impl.h b/ohos_nweb/src/nweb_impl.h index e541ccf48b..c40dc44cac 100644 --- a/ohos_nweb/src/nweb_impl.h +++ b/ohos_nweb/src/nweb_impl.h @@ -180,6 +180,7 @@ class NWebImpl : public NWeb { return nweb_delegate_ ? nweb_delegate_->GetCefClient() : nullptr; } void AddNWebToMap(uint32_t id, std::shared_ptr& nweb); + int PostUrl(const std::string& url, std::vector& postData) override; #if defined (OHOS_NWEB_EX) static const std::vector& GetCommandLineArgsForNWebEx(); -- Gitee