-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[webview_flutter_wkwebview] Add support for cookie manager #5203
Changes from 3 commits
9b6ca5e
a1ea194
a183681
54c05a0
1a7bb90
3a83c87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart'; | ||
| import 'package:webview_flutter_wkwebview/src/foundation/foundation.dart'; | ||
| import 'package:webview_flutter_wkwebview/src/web_kit/web_kit.dart'; | ||
|
|
||
| /// Handles all cookie operations for the WebView platform. | ||
| class WebKitCookieManager extends WebViewCookieManagerPlatform { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the WebKit library, the |
||
| /// Constructs a [WebKitCookieManager]. | ||
| WebKitCookieManager({WKWebsiteDataStore? websiteDataStore}) | ||
| : websiteDataStore = | ||
| websiteDataStore ?? WKWebsiteDataStore.defaultDataStore; | ||
|
|
||
| /// Manages stored data for [WKWebView]s. | ||
| final WKWebsiteDataStore websiteDataStore; | ||
|
|
||
| @override | ||
| Future<bool> clearCookies() async { | ||
| return websiteDataStore.removeDataOfTypes( | ||
| <WKWebsiteDataTypes>{WKWebsiteDataTypes.cookies}, | ||
| DateTime.fromMillisecondsSinceEpoch(0), | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| Future<void> setCookie(WebViewCookie cookie) { | ||
| if (!_isValidPath(cookie.path)) { | ||
| throw ArgumentError( | ||
| 'The path property for the provided cookie was not given a legal value.'); | ||
| } | ||
|
|
||
| return websiteDataStore.httpCookieStore.setCookie( | ||
| NSHttpCookie.withProperties( | ||
| <NSHttpCookiePropertyKey, Object>{ | ||
| NSHttpCookiePropertyKey.name: cookie.name, | ||
| NSHttpCookiePropertyKey.value: cookie.value, | ||
| NSHttpCookiePropertyKey.domain: cookie.domain, | ||
| NSHttpCookiePropertyKey.path: cookie.path, | ||
| }, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| bool _isValidPath(String path) { | ||
| // Permitted ranges based on RFC6265bis: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-02#section-4.1.1 | ||
| for (final int char in path.codeUnits) { | ||
| if ((char < 0x20 || char > 0x3A) && (char < 0x3C || char > 0x7E)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: This can just be written as: |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // Mocks generated by Mockito 5.1.0 from annotations | ||
| // in webview_flutter_wkwebview/test/src/web_kit/web_kit_test.dart. | ||
| // in webview_flutter_wkwebview/example/ios/.symlinks/plugins/webview_flutter_wkwebview/test/src/web_kit/web_kit_test.dart. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still don't know why it uses symlinks sometimes :( |
||
| // Do not manually edit this file. | ||
|
|
||
| import 'dart:async' as _i4; | ||
|
|
@@ -275,13 +275,12 @@ class MockTestWKWebsiteDataStoreHostApi extends _i1.Mock | |
| [instanceId, configurationInstanceId]), | ||
| returnValueForMissingStub: null); | ||
| @override | ||
| _i4.Future<void> removeDataOfTypes( | ||
| _i4.Future<bool> removeDataOfTypes( | ||
| int? instanceId, | ||
| List<_i3.WKWebsiteDataTypesEnumData?>? dataTypes, | ||
| double? secondsModifiedSinceEpoch) => | ||
| (super.noSuchMethod( | ||
| Invocation.method(#removeDataOfTypes, | ||
| [instanceId, dataTypes, secondsModifiedSinceEpoch]), | ||
| returnValue: Future<void>.value(), | ||
| returnValueForMissingStub: Future<void>.value()) as _i4.Future<void>); | ||
| returnValue: Future<bool>.value(false)) as _i4.Future<bool>); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This Objective-C method doesn't return a
bool, but I decided to just add this feature for simplicity of addingCookieManager.setCookie.