From 081a21bb6a47a656cc6129d2088b12f0e1f78cf3 Mon Sep 17 00:00:00 2001 From: xiaohucode <1601633802@qq.com> Date: Sat, 2 Sep 2023 22:16:22 +0800 Subject: [PATCH 1/2] add GetCookies Method --- lib/src/webview.dart | 9 +++++++++ windows/webview.cc | 38 ++++++++++++++++++++++++++++++++++++++ windows/webview.h | 3 +++ windows/webview_bridge.cc | 19 +++++++++++++++++++ 4 files changed, 69 insertions(+) diff --git a/lib/src/webview.dart b/lib/src/webview.dart index 32d0770..bfef387 100644 --- a/lib/src/webview.dart +++ b/lib/src/webview.dart @@ -397,6 +397,15 @@ class WebviewController extends ValueNotifier { return _methodChannel.invokeMethod('clearCookies'); } + /// get browser Cookies. + Future getCookies(String url) async { + if (_isDisposed) { + return ''; + } + assert(value.isInitialized); + return _methodChannel.invokeMethod('getCookies', url); + } + /// Clears browser cache. Future clearCache() async { if (_isDisposed) { diff --git a/windows/webview.cc b/windows/webview.cc index 0de207a..d584bc3 100644 --- a/windows/webview.cc +++ b/windows/webview.cc @@ -417,6 +417,44 @@ bool Webview::ClearCookies() { L"{}", nullptr) == S_OK; } +void Webview::GetCookies(const std::string& url, GetCookiesCallback callback) { + if (IsValid()) { + wil::com_ptr cookieManager; + auto webview2 = webview_.try_query(); + webview2->get_CookieManager(cookieManager.put()); + if (SUCCEEDED(cookieManager->GetCookies( + util::Utf16FromUtf8(url).c_str(), + Callback( + [this, url, callback]( + HRESULT error_code, + ICoreWebView2CookieList* list) -> HRESULT { + std::wstring result; + + UINT cookie_list_size; + list->get_Count(&cookie_list_size); + for (UINT i = 0; i < cookie_list_size; i++) { + wil::com_ptr cookie; + list->GetValueAtIndex(i, &cookie); + LPWSTR name_ptr; + cookie->get_Name(&name_ptr); + LPWSTR value_ptr; + cookie->get_Value(&value_ptr); + result.append(name_ptr + std::wstring(L"=")); + result.append(value_ptr + std::wstring(L";")); + // std::wcout << L"cookie:" << name_ptr << L"=" << value_ptr + // << ";\n"; + } + // std::wcout << L"cookies:" << result; + callback(SUCCEEDED(error_code), util::Utf8FromUtf16(result)); + return S_OK; + }) + .Get()))) { + return; + } + } + callback(false, std::string()); +} + bool Webview::ClearCache() { if (!IsValid()) { return false; diff --git a/windows/webview.h b/windows/webview.h index 567e742..a63a408 100644 --- a/windows/webview.h +++ b/windows/webview.h @@ -107,6 +107,7 @@ class Webview { typedef std::function AddScriptToExecuteOnDocumentCreatedCallback; typedef std::function ScriptExecutedCallback; + typedef std::function GetCookiesCallback; typedef std::function WebMessageReceivedCallback; typedef std::function WebviewPermissionRequestedCompleter; @@ -145,6 +146,8 @@ class Webview { ScriptExecutedCallback callback); bool PostWebMessage(const std::string& json); bool ClearCookies(); + void GetCookies(const std::string& url, + GetCookiesCallback callback); bool ClearCache(); bool SetCacheDisabled(bool disabled); void SetPopupWindowPolicy(WebviewPopupWindowPolicy policy); diff --git a/windows/webview_bridge.cc b/windows/webview_bridge.cc index f417f9a..8650979 100644 --- a/windows/webview_bridge.cc +++ b/windows/webview_bridge.cc @@ -41,6 +41,7 @@ constexpr auto kMethodSetVirtualHostNameMapping = "setVirtualHostNameMapping"; constexpr auto kMethodClearVirtualHostNameMapping = "clearVirtualHostNameMapping"; constexpr auto kMethodClearCookies = "clearCookies"; +constexpr auto kMethodGetCookies = "getCookies"; constexpr auto kMethodClearCache = "clearCache"; constexpr auto kMethodSetCacheDisabled = "setCacheDisabled"; constexpr auto kMethodSetPopupWindowPolicy = "setPopupWindowPolicy"; @@ -642,6 +643,24 @@ void WebviewBridge::HandleMethodCall( return result->Error(kMethodFailed); } + // getCookies + if (method_name.compare(kMethodGetCookies) == 0) { + if (const auto url = std::get_if(method_call.arguments())) { + std::shared_ptr> + shared_result = std::move(result); + webview_->GetCookies( + *url, [shared_result](bool success, const std::string& cookies) { + if (!cookies.empty() || !success) { + shared_result->Success(std::move(cookies)); + } else { + shared_result->Error(kScriptFailed, "getCookies failed."); + } + }); + return; + } + return result->Error(kErrorInvalidArgs); + } + // clearCache if (method_name.compare(kMethodClearCache) == 0) { if (webview_->ClearCache()) { From ed1f176c968d7053c58eda9fda1cfd8b20f3f07c Mon Sep 17 00:00:00 2001 From: xiaohucode <1601633802@qq.com> Date: Sun, 3 Sep 2023 17:08:26 +0800 Subject: [PATCH 2/2] success check --- windows/webview_bridge.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/windows/webview_bridge.cc b/windows/webview_bridge.cc index 8650979..773e316 100644 --- a/windows/webview_bridge.cc +++ b/windows/webview_bridge.cc @@ -650,8 +650,8 @@ void WebviewBridge::HandleMethodCall( shared_result = std::move(result); webview_->GetCookies( *url, [shared_result](bool success, const std::string& cookies) { - if (!cookies.empty() || !success) { - shared_result->Success(std::move(cookies)); + if (success) { + shared_result->Success(cookies); } else { shared_result->Error(kScriptFailed, "getCookies failed."); }