Skip to content

Commit ff0189e

Browse files
authored
[webview_flutter] Platform implementations for getCookies #11037 (#11386)
*List which issues are fixed by this PR. You must list at least one issue.* #10833 #11037 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent cde5b36 commit ff0189e

25 files changed

Lines changed: 1089 additions & 1400 deletions

packages/webview_flutter/webview_flutter_android/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.12.0
2+
3+
* Adds support for retrieving cookies with `PlatformWebViewCookieManager.getCookies`.
4+
15
## 4.11.0
26

37
* Adds support to opt out of Android inset changes. See

packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/AndroidWebkitLibrary.g.kt

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2013 The Flutter Authors
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
// Autogenerated from Pigeon (v26.2.0), do not edit directly.
4+
// Autogenerated from Pigeon (v26.3.4), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66
@file:Suppress("UNCHECKED_CAST", "ArrayInDataClass")
77

@@ -49,7 +49,7 @@ class AndroidWebKitError(
4949
val code: String,
5050
override val message: String? = null,
5151
val details: Any? = null
52-
) : Throwable()
52+
) : RuntimeException()
5353
/**
5454
* Maintains instances used to communicate with the corresponding objects in Dart.
5555
*
@@ -1568,6 +1568,18 @@ abstract class PigeonApiCookieManager(
15681568
accept: Boolean
15691569
)
15701570

1571+
/**
1572+
* Gets all the cookies for the given URL.
1573+
*
1574+
* This may return multiple key-value pairs if multiple cookies are associated with this URL, in
1575+
* which case each cookie will be delimited by "; " characters (semicolon followed by a space).
1576+
* Each key-value pair will be of the form "key=value".
1577+
*
1578+
* Note: Any cookies set with the "Partitioned" attribute will only be returned for the top-level
1579+
* partition of url.
1580+
*/
1581+
abstract fun getCookies(pigeon_instance: android.webkit.CookieManager, url: String): String?
1582+
15711583
companion object {
15721584
@Suppress("LocalVariableName")
15731585
fun setUpMessageHandlers(binaryMessenger: BinaryMessenger, api: PigeonApiCookieManager?) {
@@ -1670,6 +1682,29 @@ abstract class PigeonApiCookieManager(
16701682
channel.setMessageHandler(null)
16711683
}
16721684
}
1685+
run {
1686+
val channel =
1687+
BasicMessageChannel<Any?>(
1688+
binaryMessenger,
1689+
"dev.flutter.pigeon.webview_flutter_android.CookieManager.getCookies",
1690+
codec)
1691+
if (api != null) {
1692+
channel.setMessageHandler { message, reply ->
1693+
val args = message as List<Any?>
1694+
val pigeon_instanceArg = args[0] as android.webkit.CookieManager
1695+
val urlArg = args[1] as String
1696+
val wrapped: List<Any?> =
1697+
try {
1698+
listOf(api.getCookies(pigeon_instanceArg, urlArg))
1699+
} catch (exception: Throwable) {
1700+
AndroidWebkitLibraryPigeonUtils.wrapError(exception)
1701+
}
1702+
reply.reply(wrapped)
1703+
}
1704+
} else {
1705+
channel.setMessageHandler(null)
1706+
}
1707+
}
16731708
}
16741709
}
16751710

packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/CookieManagerProxyApi.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,9 @@ public void setAcceptThirdPartyCookies(
5252
@NonNull CookieManager pigeon_instance, @NonNull WebView webView, boolean accept) {
5353
pigeon_instance.setAcceptThirdPartyCookies(webView, accept);
5454
}
55+
56+
@Override
57+
public @NonNull String getCookies(@NonNull CookieManager pigeon_instance, @NonNull String url) {
58+
return pigeon_instance.getCookie(url);
59+
}
5560
}

packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/CookieManagerTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static org.junit.Assert.assertEquals;
88
import static org.mockito.Mockito.mock;
99
import static org.mockito.Mockito.verify;
10+
import static org.mockito.Mockito.when;
1011

1112
import android.webkit.CookieManager;
1213
import android.webkit.ValueCallback;
@@ -64,4 +65,20 @@ public void setAcceptThirdPartyCookies() {
6465

6566
verify(instance).setAcceptThirdPartyCookies(webView, accept);
6667
}
68+
69+
@Test
70+
public void getCookies_returnsCookieString() {
71+
final PigeonApiCookieManager api = new TestProxyApiRegistrar().getPigeonApiCookieManager();
72+
73+
final CookieManager instance = mock(CookieManager.class);
74+
final String domain = "https://flutter.dev";
75+
final String cookieValue = "session=12345";
76+
77+
// Mock the CookieManager to return the cookie string
78+
when(instance.getCookie(domain)).thenReturn(cookieValue);
79+
80+
final String result = api.getCookies(instance, domain);
81+
82+
assertEquals(cookieValue, result);
83+
}
6784
}

packages/webview_flutter/webview_flutter_android/example/lib/main.dart

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,17 @@ class SampleMenu extends StatelessWidget {
496496
}
497497

498498
Future<void> _onListCookies(BuildContext context) async {
499-
final cookies =
500-
await webViewController.runJavaScriptReturningResult('document.cookie')
501-
as String;
499+
final Uri? domain = Uri.tryParse(
500+
(await webViewController.currentUrl()) ?? '',
501+
);
502+
final List<WebViewCookie> cookies;
503+
504+
if (domain == null) {
505+
cookies = [];
506+
} else {
507+
cookies = await cookieManager.getCookies(domain);
508+
}
509+
502510
if (context.mounted) {
503511
ScaffoldMessenger.of(context).showSnackBar(
504512
SnackBar(
@@ -662,13 +670,12 @@ class SampleMenu extends StatelessWidget {
662670
return webViewController.loadHtmlString(kAlertTestPage);
663671
}
664672

665-
Widget _getCookieList(String cookies) {
666-
if (cookies == '""') {
673+
Widget _getCookieList(List<WebViewCookie> cookies) {
674+
if (cookies.isEmpty) {
667675
return Container();
668676
}
669-
final List<String> cookieList = cookies.split(';');
670-
final Iterable<Text> cookieWidgets = cookieList.map(
671-
(String cookie) => Text(cookie),
677+
final Iterable<Text> cookieWidgets = cookies.map(
678+
(WebViewCookie cookie) => Text(cookie.toString()),
672679
);
673680
return Column(
674681
mainAxisAlignment: MainAxisAlignment.end,

0 commit comments

Comments
 (0)