Skip to content

Commit b38e3e7

Browse files
committed
[webview_flutter] make api more consistent across platforms
1 parent 4f0c549 commit b38e3e7

17 files changed

Lines changed: 93 additions & 122 deletions

File tree

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,14 @@ class SampleMenu extends StatelessWidget {
431431
final Uri? domain = Uri.tryParse(
432432
(await webViewController.currentUrl()) ?? '',
433433
);
434-
final List<WebViewCookie> cookies = await cookieManager.getCookies(
435-
domain: domain,
436-
);
434+
435+
late final List<WebViewCookie> cookies;
436+
if (domain == null) {
437+
cookies = [];
438+
} else {
439+
cookies = await cookieManager.getCookies(domain: domain);
440+
}
441+
437442
if (context.mounted) {
438443
ScaffoldMessenger.of(context).showSnackBar(
439444
SnackBar(

packages/webview_flutter/webview_flutter/lib/src/webview_cookie_manager.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,8 @@ class WebViewCookieManager {
9090
/// Gets a list of existing cookie for specified domain from all
9191
/// WebView instances of the application.
9292
///
93-
/// On iOS & macOS if domain is not provided then it returns all cookies.
94-
///
95-
/// Android: Entire domain must be provided alongside scheme.
96-
///
97-
/// iOS & macOS: ignores scheme and uses partial match based on host.
98-
Future<List<WebViewCookie>> getCookies({Uri? domain}) =>
93+
/// Each platform can have different url requirements. Please check individual
94+
/// platform implementation for details
95+
Future<List<WebViewCookie>> getCookies({required Uri domain}) =>
9996
platform.getCookies(domain);
10097
}

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,13 +1516,16 @@ abstract class PigeonApiCookieManager(
15161516
)
15171517

15181518
/**
1519-
* Gets all the cookies for the given URL. This may return multiple key-value pairs if multiple
1520-
* cookies are associated with this URL, in which case each cookie will be delimited by "; "
1521-
* characters (semicolon followed by a space). Each key-value pair will be of the form
1522-
* "key=value". Note: Any cookies set with the "Partitioned" attribute will only be returned for
1523-
* the top-level partition of url.
1519+
* Gets all the cookies for the given URL.
1520+
*
1521+
* This may return multiple key-value pairs if multiple cookies are associated with this URL, in
1522+
* which case each cookie will be delimited by "; " characters (semicolon followed by a space).
1523+
* Each key-value pair will be of the form "key=value".
1524+
*
1525+
* Note: Any cookies set with the "Partitioned" attribute will only be returned for the top-level
1526+
* partition of url.
15241527
*/
1525-
abstract fun getCookies(pigeon_instance: android.webkit.CookieManager, domain: String): String
1528+
abstract fun getCookies(pigeon_instance: android.webkit.CookieManager, url: String): String?
15261529

15271530
companion object {
15281531
@Suppress("LocalVariableName")
@@ -1636,10 +1639,10 @@ abstract class PigeonApiCookieManager(
16361639
channel.setMessageHandler { message, reply ->
16371640
val args = message as List<Any?>
16381641
val pigeon_instanceArg = args[0] as android.webkit.CookieManager
1639-
val domainArg = args[1] as String
1642+
val urlArg = args[1] as String
16401643
val wrapped: List<Any?> =
16411644
try {
1642-
listOf(api.getCookies(pigeon_instanceArg, domainArg))
1645+
listOf(api.getCookies(pigeon_instanceArg, urlArg))
16431646
} catch (exception: Throwable) {
16441647
AndroidWebkitLibraryPigeonUtils.wrapError(exception)
16451648
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import kotlin.Result;
1111
import kotlin.Unit;
1212
import kotlin.jvm.functions.Function1;
13-
import org.jetbrains.annotations.NotNull;
1413

1514
/**
1615
* Host API implementation for `CookieManager`.
@@ -55,10 +54,7 @@ public void setAcceptThirdPartyCookies(
5554
}
5655

5756
@Override
58-
public @NotNull String getCookies(
59-
@NotNull CookieManager pigeon_instance, @NotNull String domain) {
60-
final String cookie = pigeon_instance.getCookie(domain);
61-
if (cookie == null) return "";
62-
return cookie;
57+
public @NonNull String getCookies(@NonNull CookieManager pigeon_instance, @NonNull String url) {
58+
return pigeon_instance.getCookie(url);
6359
}
6460
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,14 @@ class SampleMenu extends StatelessWidget {
499499
final Uri? domain = Uri.tryParse(
500500
(await webViewController.currentUrl()) ?? '',
501501
);
502-
final List<WebViewCookie> cookies = await cookieManager.getCookies(domain);
502+
late final List<WebViewCookie> cookies;
503+
504+
if (domain == null) {
505+
cookies = [];
506+
} else {
507+
cookies = await cookieManager.getCookies(domain);
508+
}
509+
503510
if (context.mounted) {
504511
ScaffoldMessenger.of(context).showSnackBar(
505512
SnackBar(

packages/webview_flutter/webview_flutter_android/lib/src/android_webkit.g.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,9 +1791,14 @@ class CookieManager extends PigeonInternalProxyApiBaseClass {
17911791
}
17921792
}
17931793

1794-
/// Gets all the cookies for the given URL. This may return multiple key-value pairs if multiple cookies are associated with this URL, in which case each cookie will be delimited by "; " characters (semicolon followed by a space). Each key-value pair will be of the form "key=value".
1794+
/// Gets all the cookies for the given URL.
1795+
///
1796+
/// This may return multiple key-value pairs if multiple cookies are associated with this URL,
1797+
/// in which case each cookie will be delimited by "; " characters (semicolon followed by a space).
1798+
/// Each key-value pair will be of the form "key=value".
1799+
///
17951800
/// Note: Any cookies set with the "Partitioned" attribute will only be returned for the top-level partition of url.
1796-
Future<String> getCookies(String domain) async {
1801+
Future<String?> getCookies(String url) async {
17971802
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =
17981803
_pigeonVar_codecCookieManager;
17991804
final BinaryMessenger? pigeonVar_binaryMessenger = pigeon_binaryMessenger;
@@ -1805,7 +1810,7 @@ class CookieManager extends PigeonInternalProxyApiBaseClass {
18051810
binaryMessenger: pigeonVar_binaryMessenger,
18061811
);
18071812
final Future<Object?> pigeonVar_sendFuture = pigeonVar_channel.send(
1808-
<Object?>[this, domain],
1813+
<Object?>[this, url],
18091814
);
18101815
final pigeonVar_replyList = await pigeonVar_sendFuture as List<Object?>?;
18111816
if (pigeonVar_replyList == null) {
@@ -1816,13 +1821,8 @@ class CookieManager extends PigeonInternalProxyApiBaseClass {
18161821
message: pigeonVar_replyList[1] as String?,
18171822
details: pigeonVar_replyList[2],
18181823
);
1819-
} else if (pigeonVar_replyList[0] == null) {
1820-
throw PlatformException(
1821-
code: 'null-error',
1822-
message: 'Host platform returned null value for non-null return value.',
1823-
);
18241824
} else {
1825-
return (pigeonVar_replyList[0] as String?)!;
1825+
return (pigeonVar_replyList[0] as String?);
18261826
}
18271827
}
18281828

packages/webview_flutter/webview_flutter_android/lib/src/android_webview_cookie_manager.dart

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,20 @@ class AndroidWebViewCookieManager extends PlatformWebViewCookieManager {
8989
}
9090

9191
@override
92-
Future<List<WebViewCookie>> getCookies(Uri? domain) async {
93-
if (domain == null) {
94-
throw UnsupportedError('Domain can not be null in android');
95-
}
96-
97-
final String cookies = await _cookieManager.getCookies(domain.toString());
98-
if (cookies.isEmpty) {
92+
Future<List<WebViewCookie>> getCookies(Uri url) async {
93+
final String? cookies = await _cookieManager.getCookies(url.toString());
94+
if (cookies == null || cookies.isEmpty) {
9995
return [];
10096
}
10197

102-
final List<WebViewCookie> webViewCookies = List.of([]);
98+
final webViewCookies = <WebViewCookie>[];
10399
for (final String cookie in cookies.split('; ')) {
104100
final List<String> cookieValue = cookie.split('=');
105101
webViewCookies.add(
106102
WebViewCookie(
107103
name: cookieValue.first,
108104
value: cookieValue.last,
109-
domain: domain.toString(),
105+
domain: url.toString(),
110106
),
111107
);
112108
}

packages/webview_flutter/webview_flutter_android/pigeons/android_webkit.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,14 @@ abstract class CookieManager {
262262
/// Sets whether the `WebView` should allow third party cookies to be set.
263263
void setAcceptThirdPartyCookies(WebView webView, bool accept);
264264

265-
/// Gets all the cookies for the given URL. This may return multiple key-value pairs if multiple cookies are associated with this URL, in which case each cookie will be delimited by "; " characters (semicolon followed by a space). Each key-value pair will be of the form "key=value".
265+
/// Gets all the cookies for the given URL.
266+
///
267+
/// This may return multiple key-value pairs if multiple cookies are associated with this URL,
268+
/// in which case each cookie will be delimited by "; " characters (semicolon followed by a space).
269+
/// Each key-value pair will be of the form "key=value".
270+
///
266271
/// Note: Any cookies set with the "Partitioned" attribute will only be returned for the top-level partition of url.
267-
String getCookies(String domain);
272+
String? getCookies(String url);
268273
}
269274

270275
/// A View that displays web pages.

packages/webview_flutter/webview_flutter_android/test/android_webview_cookie_manager_test.dart

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,6 @@ void main() {
169169
verify(mockCookieManager.getCookies('https://flutter.dev')).called(1);
170170
});
171171

172-
test('getCookies should throw UnsupportedError if domain is null', () async {
173-
final mockCookieManager = MockCookieManager();
174-
175-
final params =
176-
AndroidWebViewCookieManagerCreationParams.fromPlatformWebViewCookieManagerCreationParams(
177-
const PlatformWebViewCookieManagerCreationParams(),
178-
);
179-
180-
final cookieManager = AndroidWebViewCookieManager(
181-
params,
182-
cookieManager: mockCookieManager,
183-
);
184-
185-
expect(
186-
() => cookieManager.getCookies(null),
187-
throwsA(isA<UnsupportedError>()),
188-
);
189-
});
190-
191172
test('getCookies should handle single cookie correctly', () async {
192173
final mockCookieManager = MockCookieManager();
193174

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_webview_cookie_manager.dart

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,10 @@ abstract class PlatformWebViewCookieManager extends PlatformInterface {
6666
}
6767

6868
/// Gets a list of existing cookie for specified domain from all
69-
/// WebView instances of the application.
70-
///
71-
/// On iOS & macOS if domain is not provided then it returns all cookies.
72-
///
73-
/// Android: Entire domain must be provided alongside scheme.
74-
///
75-
/// iOS & macOS: ignores scheme and uses partial match based on host.
76-
Future<List<WebViewCookie>> getCookies(Uri? domain) {
69+
/// [WebView] instances of the application.
70+
Future<List<WebViewCookie>> getCookies(Uri url) {
7771
throw UnimplementedError(
78-
'getCookie is not implemented on the current platform',
72+
'getCookies is not implemented on the current platform',
7973
);
8074
}
8175
}

0 commit comments

Comments
 (0)