Skip to content

add GetCookies Method #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/src/webview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,15 @@ class WebviewController extends ValueNotifier<WebviewValue> {
return _methodChannel.invokeMethod('clearCookies');
}

/// get browser Cookies.
Future<String?> getCookies(String url) async {
if (_isDisposed) {
return '';
}
assert(value.isInitialized);
return _methodChannel.invokeMethod<String>('getCookies', url);
}

/// Clears browser cache.
Future<void> clearCache() async {
if (_isDisposed) {
Expand Down
38 changes: 38 additions & 0 deletions windows/webview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICoreWebView2CookieManager> cookieManager;
auto webview2 = webview_.try_query<ICoreWebView2_2>();
webview2->get_CookieManager(cookieManager.put());
if (SUCCEEDED(cookieManager->GetCookies(
util::Utf16FromUtf8(url).c_str(),
Callback<ICoreWebView2GetCookiesCompletedHandler>(
[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<ICoreWebView2Cookie> 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;
Expand Down
3 changes: 3 additions & 0 deletions windows/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Webview {
typedef std::function<void(bool, const std::string&)>
AddScriptToExecuteOnDocumentCreatedCallback;
typedef std::function<void(bool, const std::string&)> ScriptExecutedCallback;
typedef std::function<void(bool, const std::string&)> GetCookiesCallback;
typedef std::function<void(const std::string&)> WebMessageReceivedCallback;
typedef std::function<void(WebviewPermissionState state)>
WebviewPermissionRequestedCompleter;
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions windows/webview_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<std::string>(method_call.arguments())) {
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>>
shared_result = std::move(result);
webview_->GetCookies(
*url, [shared_result](bool success, const std::string& cookies) {
if (success) {
shared_result->Success(cookies);
} else {
shared_result->Error(kScriptFailed, "getCookies failed.");
}
});
return;
}
return result->Error(kErrorInvalidArgs);
}

// clearCache
if (method_name.compare(kMethodClearCache) == 0) {
if (webview_->ClearCache()) {
Expand Down