|
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
5 | 5 | import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart'; |
| 6 | +import 'package:webview_flutter_wkwebview/src/foundation/foundation.dart'; |
| 7 | +import 'package:webview_flutter_wkwebview/src/web_kit/web_kit.dart'; |
6 | 8 |
|
7 | | -import 'web_kit_cookie_manager.dart'; |
8 | | - |
9 | | -/// Handles all cookie operations for the current platform. |
| 9 | +/// Handles all cookie operations for the WebView platform. |
10 | 10 | class WKWebViewCookieManager extends WebViewCookieManagerPlatform { |
11 | | - final WebKitCookieManager _webKitManager = WebKitCookieManager(); |
| 11 | + /// Constructs a [WKWebViewCookieManager]. |
| 12 | + WKWebViewCookieManager({WKWebsiteDataStore? websiteDataStore}) |
| 13 | + : websiteDataStore = |
| 14 | + websiteDataStore ?? WKWebsiteDataStore.defaultDataStore; |
| 15 | + |
| 16 | + /// Manages stored data for [WKWebView]s. |
| 17 | + final WKWebsiteDataStore websiteDataStore; |
12 | 18 |
|
13 | 19 | @override |
14 | | - Future<bool> clearCookies() => _webKitManager.clearCookies(); |
| 20 | + Future<bool> clearCookies() async { |
| 21 | + return websiteDataStore.removeDataOfTypes( |
| 22 | + <WKWebsiteDataType>{WKWebsiteDataType.cookies}, |
| 23 | + DateTime.fromMillisecondsSinceEpoch(0), |
| 24 | + ); |
| 25 | + } |
15 | 26 |
|
16 | 27 | @override |
17 | 28 | Future<void> setCookie(WebViewCookie cookie) { |
18 | 29 | if (!_isValidPath(cookie.path)) { |
19 | 30 | throw ArgumentError( |
20 | 31 | 'The path property for the provided cookie was not given a legal value.'); |
21 | 32 | } |
22 | | - return _webKitManager.setCookie(cookie); |
| 33 | + |
| 34 | + return websiteDataStore.httpCookieStore.setCookie( |
| 35 | + NSHttpCookie.withProperties( |
| 36 | + <NSHttpCookiePropertyKey, Object>{ |
| 37 | + NSHttpCookiePropertyKey.name: cookie.name, |
| 38 | + NSHttpCookiePropertyKey.value: cookie.value, |
| 39 | + NSHttpCookiePropertyKey.domain: cookie.domain, |
| 40 | + NSHttpCookiePropertyKey.path: cookie.path, |
| 41 | + }, |
| 42 | + ), |
| 43 | + ); |
23 | 44 | } |
24 | 45 |
|
25 | 46 | bool _isValidPath(String path) { |
26 | 47 | // Permitted ranges based on RFC6265bis: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-02#section-4.1.1 |
27 | | - for (final int char in path.codeUnits) { |
28 | | - if ((char < 0x20 || char > 0x3A) && (char < 0x3C || char > 0x7E)) { |
29 | | - return false; |
30 | | - } |
31 | | - } |
32 | | - return true; |
| 48 | + return !path.codeUnits.any( |
| 49 | + (int char) { |
| 50 | + return (char < 0x20 || char > 0x3A) && (char < 0x3C || char > 0x7E); |
| 51 | + }, |
| 52 | + ); |
33 | 53 | } |
34 | 54 | } |
0 commit comments