Skip to content

Commit b219b11

Browse files
authored
[webview_flutter_platform_interface] Add support for getting cookie (#11037)
*Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.* #10833 ## 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 f330175 commit b219b11

5 files changed

Lines changed: 141 additions & 2 deletions

File tree

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 2.15.0
22

3+
* Adds support to retrieve WebView cookies. See `PlatformWebViewCookieManager.getCookies`.
34
* Updates minimum supported SDK version to Flutter 3.35/Dart 3.9.
45

56
## 2.14.0

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@ abstract class PlatformWebViewCookieManager extends PlatformInterface {
6464
'setCookie is not implemented on the current platform',
6565
);
6666
}
67+
68+
/// Returns a list of existing cookies for the specified domain from all
69+
/// [WebView] instances of the application.
70+
Future<List<WebViewCookie>> getCookies(Uri url) {
71+
throw UnimplementedError(
72+
'getCookies is not implemented on the current platform',
73+
);
74+
}
6775
}

packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/webview_cookie.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ class WebViewCookie {
3838
/// Its value should match "path-value" in RFC6265bis:
3939
/// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-02#section-4.1.1
4040
final String path;
41+
42+
@override
43+
String toString() {
44+
return 'WebViewCookie{name: $name, value: $value, domain: $domain, path: $path}';
45+
}
4146
}

packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/webview_flutt
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.14.0
7+
version: 2.15.0
88

99
environment:
1010
sdk: ^3.9.0
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:mockito/mockito.dart';
7+
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
8+
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';
9+
10+
import 'webview_platform_test.mocks.dart';
11+
12+
void main() {
13+
setUp(() {
14+
WebViewPlatform.instance = MockWebViewPlatformWithMixin();
15+
});
16+
17+
test('Cannot be implemented with `implements`', () {
18+
when(
19+
(WebViewPlatform.instance! as MockWebViewPlatform)
20+
.createPlatformCookieManager(any),
21+
).thenReturn(ImplementsPlatformWebViewCookieManager());
22+
23+
expect(() {
24+
PlatformWebViewCookieManager(
25+
const PlatformWebViewCookieManagerCreationParams(),
26+
);
27+
// In versions of `package:plugin_platform_interface` prior to fixing
28+
// https://github.com/flutter/flutter/issues/109339, an attempt to
29+
// implement a platform interface using `implements` would sometimes throw
30+
// a `NoSuchMethodError` and other times throw an `AssertionError`. After
31+
// the issue is fixed, an `AssertionError` will always be thrown. For the
32+
// purpose of this test, we don't really care what exception is thrown, so
33+
// just allow any exception.
34+
}, throwsA(anything));
35+
});
36+
37+
test('Can be extended', () {
38+
const params = PlatformWebViewCookieManagerCreationParams();
39+
when(
40+
(WebViewPlatform.instance! as MockWebViewPlatform)
41+
.createPlatformCookieManager(any),
42+
).thenReturn(ExtendsPlatformWebViewCookieManager(params));
43+
44+
expect(PlatformWebViewCookieManager(params), isNotNull);
45+
});
46+
47+
test('Can be mocked with `implements`', () {
48+
when(
49+
(WebViewPlatform.instance! as MockWebViewPlatform)
50+
.createPlatformCookieManager(any),
51+
).thenReturn(MockWebViewCookieManagerDelegate());
52+
53+
expect(
54+
PlatformWebViewCookieManager(
55+
const PlatformWebViewCookieManagerCreationParams(),
56+
),
57+
isNotNull,
58+
);
59+
});
60+
61+
test(
62+
'Default implementation of clearCookies should throw unimplemented error',
63+
() {
64+
final PlatformWebViewCookieManager cookieManager =
65+
ExtendsPlatformWebViewCookieManager(
66+
const PlatformWebViewCookieManagerCreationParams(),
67+
);
68+
69+
expect(() => cookieManager.clearCookies(), throwsUnimplementedError);
70+
},
71+
);
72+
73+
test(
74+
'Default implementation of setCookie should throw unimplemented error',
75+
() {
76+
final PlatformWebViewCookieManager cookieManager =
77+
ExtendsPlatformWebViewCookieManager(
78+
const PlatformWebViewCookieManagerCreationParams(),
79+
);
80+
81+
expect(
82+
() => cookieManager.setCookie(
83+
const WebViewCookie(name: 'foo', value: 'bar', domain: 'flutter.dev'),
84+
),
85+
throwsUnimplementedError,
86+
);
87+
},
88+
);
89+
90+
test(
91+
'Default implementation of getCookies should throw unimplemented error',
92+
() {
93+
final PlatformWebViewCookieManager cookieManager =
94+
ExtendsPlatformWebViewCookieManager(
95+
const PlatformWebViewCookieManagerCreationParams(),
96+
);
97+
98+
expect(
99+
() => cookieManager.getCookies(Uri.parse('https://flutter.dev')),
100+
throwsUnimplementedError,
101+
);
102+
},
103+
);
104+
}
105+
106+
class MockWebViewPlatformWithMixin extends MockWebViewPlatform
107+
with
108+
// ignore: prefer_mixin
109+
MockPlatformInterfaceMixin {}
110+
111+
class ImplementsPlatformWebViewCookieManager
112+
implements PlatformWebViewCookieManager {
113+
@override
114+
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
115+
}
116+
117+
class MockWebViewCookieManagerDelegate extends Mock
118+
with
119+
// ignore: prefer_mixin
120+
MockPlatformInterfaceMixin
121+
implements PlatformWebViewCookieManager {}
122+
123+
class ExtendsPlatformWebViewCookieManager extends PlatformWebViewCookieManager {
124+
ExtendsPlatformWebViewCookieManager(super.params) : super.implementation();
125+
}

0 commit comments

Comments
 (0)