2019-11-02 03:16:47 +00:00
import ' dart:async ' ;
2023-11-17 22:28:11 +00:00
import ' package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart ' ;
2019-10-26 02:42:50 +00:00
2021-02-22 11:16:23 +00:00
import ' in_app_webview/in_app_webview_controller.dart ' ;
import ' in_app_webview/headless_in_app_webview.dart ' ;
2019-11-25 00:42:27 +00:00
2020-05-11 00:48:41 +00:00
///Class that implements a singleton object (shared instance) which manages the cookies used by WebView instances.
2020-05-29 12:51:26 +00:00
///On Android, it is implemented using [CookieManager](https://developer.android.com/reference/android/webkit/CookieManager).
///On iOS, it is implemented using [WKHTTPCookieStore](https://developer.apple.com/documentation/webkit/wkhttpcookiestore).
2019-10-26 02:42:50 +00:00
///
2023-11-17 22:28:11 +00:00
///**NOTE for iOS below 11.0 and Web platform (LIMITED SUPPORT!)**: in this case, almost all of the methods
///([CookieManager.deleteAllCookies] and [CookieManager.getAllCookies] are not supported!)
2021-01-30 13:53:32 +00:00
///has been implemented using JavaScript because there is no other way to work with them on iOS below 11.0.
///See https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies for JavaScript restrictions.
2022-04-25 20:36:21 +00:00
///
///**Supported Platforms/Implementations**:
///- Android native WebView
///- iOS
2022-10-18 16:12:33 +00:00
///- MacOS
2022-04-25 20:36:21 +00:00
///- Web
2019-10-26 02:42:50 +00:00
class CookieManager {
2023-11-17 22:28:11 +00:00
/// Constructs a [CookieManager].
///
/// See [CookieManager.fromPlatformCreationParams] for setting
/// parameters for a specific platform.
CookieManager ( )
: this . fromPlatformCreationParams (
const PlatformCookieManagerCreationParams ( ) ,
) ;
/// Constructs a [CookieManager] from creation params for a specific
/// platform.
CookieManager . fromPlatformCreationParams (
PlatformCookieManagerCreationParams params ,
) : this . fromPlatform ( PlatformCookieManager ( params ) ) ;
/// Constructs a [CookieManager] from a specific platform
/// implementation.
CookieManager . fromPlatform ( this . platform ) ;
2019-10-26 02:42:50 +00:00
2023-11-17 22:28:11 +00:00
/// Implementation of [PlatformWebViewCookieManager] for the current platform.
final PlatformCookieManager platform ;
2022-10-19 00:42:23 +00:00
2022-04-19 23:31:14 +00:00
///Use [CookieManager] instead.
@ Deprecated ( " Use CookieManager instead " )
2023-11-17 22:28:11 +00:00
IOSCookieManager ios = IOSCookieManager . instance ( ) ;
static CookieManager ? _instance ;
2021-01-31 21:08:20 +00:00
///Gets the [CookieManager] shared instance.
2019-10-31 02:20:07 +00:00
static CookieManager instance ( ) {
2023-11-17 22:28:11 +00:00
if ( _instance = = null ) {
_instance = CookieManager ( ) ;
}
2021-01-28 16:10:15 +00:00
return _instance ! ;
2019-10-31 02:20:07 +00:00
}
2021-01-30 13:53:32 +00:00
///Sets a cookie for the given [url]. Any existing cookie with the same [host], [path] and [name] will be replaced with the new cookie.
///The cookie being set will be ignored if it is expired.
2019-10-26 02:42:50 +00:00
///
///The default value of [path] is `"/"`.
2021-01-30 13:53:32 +00:00
///
2022-04-25 20:36:21 +00:00
///[webViewController] could be used if you need to set a session-only cookie using JavaScript (so [isHttpOnly] cannot be set, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies)
2022-10-18 16:12:33 +00:00
///on the current URL of the [WebView] managed by that controller when you need to target iOS below 11, MacOS below 10.13 and Web platform. In this case the [url] parameter is ignored.
2022-04-25 20:36:21 +00:00
///
2023-11-10 13:13:06 +00:00
///The return value indicates whether the cookie was set successfully.
///Note that it will return always `true` for Web platform, iOS below 11.0 and MacOS below 10.13.
///
2022-10-18 16:12:33 +00:00
///**NOTE for iOS below 11.0 and MacOS below 10.13**: If [webViewController] is `null` or JavaScript is disabled for it, it will try to use a [HeadlessInAppWebView]
2022-04-25 20:36:21 +00:00
///to set the cookie (session-only cookie won't work! In that case, you should set also [expiresDate] or [maxAge]).
2021-01-30 13:53:32 +00:00
///
2022-04-25 20:36:21 +00:00
///**NOTE for Web**: this method will have effect only if the iframe has the same origin.
///If [webViewController] is `null` or JavaScript is disabled for it, it will try to use a [HeadlessInAppWebView]
2021-01-30 13:53:32 +00:00
///to set the cookie (session-only cookie won't work! In that case, you should set also [expiresDate] or [maxAge]).
2022-04-19 23:31:14 +00:00
///
///**Supported Platforms/Implementations**:
///- Android native WebView ([Official API - CookieManager.setCookie](https://developer.android.com/reference/android/webkit/CookieManager#setCookie(java.lang.String,%20java.lang.String,%20android.webkit.ValueCallback%3Cjava.lang.Boolean%3E)))
///- iOS ([Official API - WKHTTPCookieStore.setCookie](https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882007-setcookie))
2022-10-18 16:12:33 +00:00
///- MacOS ([Official API - WKHTTPCookieStore.setCookie](https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882007-setcookie))
2022-04-25 20:36:21 +00:00
///- Web
2023-11-10 13:13:06 +00:00
Future < bool > setCookie (
2023-11-17 22:28:11 +00:00
{ required WebUri url ,
required String name ,
required String value ,
String path = " / " ,
String ? domain ,
int ? expiresDate ,
int ? maxAge ,
bool ? isSecure ,
bool ? isHttpOnly ,
HTTPCookieSameSitePolicy ? sameSite ,
@ Deprecated ( " Use webViewController instead " )
InAppWebViewController ? iosBelow11WebViewController ,
InAppWebViewController ? webViewController } ) = >
platform . setCookie (
2022-10-18 16:12:33 +00:00
url: url ,
name: name ,
value: value ,
path: path ,
2023-11-17 22:28:11 +00:00
domain: domain ,
2022-10-18 16:12:33 +00:00
expiresDate: expiresDate ,
maxAge: maxAge ,
isSecure: isSecure ,
2023-11-17 22:28:11 +00:00
isHttpOnly: isHttpOnly ,
2022-10-18 16:12:33 +00:00
sameSite: sameSite ,
2023-11-17 22:28:11 +00:00
iosBelow11WebViewController: iosBelow11WebViewController ? . platform ,
webViewController: webViewController ? . platform ) ;
2021-02-01 14:55:27 +00:00
2019-10-26 02:42:50 +00:00
///Gets all the cookies for the given [url].
2021-01-30 13:53:32 +00:00
///
2022-04-25 20:36:21 +00:00
///[webViewController] is used for getting the cookies (also session-only cookies) using JavaScript (cookies with `isHttpOnly` enabled cannot be found, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies)
2022-10-18 16:12:33 +00:00
///from the current context of the [WebView] managed by that controller when you need to target iOS below 11, MacOS below 10.13 and Web platform. JavaScript must be enabled in order to work.
2021-01-30 13:53:32 +00:00
///In this case the [url] parameter is ignored.
///
2022-10-18 16:12:33 +00:00
///**NOTE for iOS below 11.0 and MacOS below 10.13**: All the cookies returned this way will have all the properties to `null` except for [Cookie.name] and [Cookie.value].
2022-04-25 20:36:21 +00:00
///If [webViewController] is `null` or JavaScript is disabled for it, it will try to use a [HeadlessInAppWebView]
///to get the cookies (session-only cookies and cookies with `isHttpOnly` enabled won't be found!).
///
///**NOTE for Web**: this method will have effect only if the iframe has the same origin.
///If [webViewController] is `null` or JavaScript is disabled for it, it will try to use a [HeadlessInAppWebView]
2021-01-30 13:53:32 +00:00
///to get the cookies (session-only cookies and cookies with `isHttpOnly` enabled won't be found!).
2022-04-19 23:31:14 +00:00
///
///**Supported Platforms/Implementations**:
///- Android native WebView ([Official API - CookieManager.getCookie](https://developer.android.com/reference/android/webkit/CookieManager#getCookie(java.lang.String)))
///- iOS ([Official API - WKHTTPCookieStore.getAllCookies](https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882005-getallcookies))
2022-10-18 16:12:33 +00:00
///- MacOS ([Official API - WKHTTPCookieStore.getAllCookies](https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882005-getallcookies))
2022-04-25 20:36:21 +00:00
///- Web
2021-02-22 22:54:09 +00:00
Future < List < Cookie > > getCookies (
2023-11-17 22:28:11 +00:00
{ required WebUri url ,
@ Deprecated ( " Use webViewController instead " )
InAppWebViewController ? iosBelow11WebViewController ,
InAppWebViewController ? webViewController } ) = >
platform . getCookies (
url: url ,
iosBelow11WebViewController: iosBelow11WebViewController ? . platform ,
webViewController: webViewController ? . platform ) ;
2021-02-01 14:55:27 +00:00
2019-10-26 02:42:50 +00:00
///Gets a cookie by its [name] for the given [url].
2021-01-30 13:53:32 +00:00
///
2022-04-25 20:36:21 +00:00
///[webViewController] is used for getting the cookie (also session-only cookie) using JavaScript (cookie with `isHttpOnly` enabled cannot be found, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies)
2022-10-18 16:12:33 +00:00
///from the current context of the [WebView] managed by that controller when you need to target iOS below 11, MacOS below 10.13 and Web platform. JavaScript must be enabled in order to work.
2021-01-30 13:53:32 +00:00
///In this case the [url] parameter is ignored.
///
2022-10-18 16:12:33 +00:00
///**NOTE for iOS below 11.0 and MacOS below 10.13**: All the cookies returned this way will have all the properties to `null` except for [Cookie.name] and [Cookie.value].
2022-04-25 20:36:21 +00:00
///If [webViewController] is `null` or JavaScript is disabled for it, it will try to use a [HeadlessInAppWebView]
///to get the cookie (session-only cookie and cookie with `isHttpOnly` enabled won't be found!).
///
///**NOTE for Web**: this method will have effect only if the iframe has the same origin.
///If [webViewController] is `null` or JavaScript is disabled for it, it will try to use a [HeadlessInAppWebView]
2021-01-30 13:53:32 +00:00
///to get the cookie (session-only cookie and cookie with `isHttpOnly` enabled won't be found!).
2022-04-19 23:31:14 +00:00
///
///**Supported Platforms/Implementations**:
///- Android native WebView
///- iOS
2022-10-18 16:12:33 +00:00
///- MacOS
2022-04-25 20:36:21 +00:00
///- Web
2021-01-28 16:10:15 +00:00
Future < Cookie ? > getCookie (
2023-11-17 22:28:11 +00:00
{ required WebUri url ,
required String name ,
@ Deprecated ( " Use webViewController instead " )
InAppWebViewController ? iosBelow11WebViewController ,
InAppWebViewController ? webViewController } ) = >
platform . getCookie (
url: url ,
name: name ,
iosBelow11WebViewController: iosBelow11WebViewController ? . platform ,
webViewController: webViewController ? . platform ) ;
2019-10-26 02:42:50 +00:00
///Removes a cookie by its [name] for the given [url], [domain] and [path].
///
///The default value of [path] is `"/"`.
2021-01-30 13:53:32 +00:00
///
2022-04-25 20:36:21 +00:00
///[webViewController] is used for deleting the cookie (also session-only cookie) using JavaScript (cookie with `isHttpOnly` enabled cannot be deleted, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies)
2022-10-18 16:12:33 +00:00
///from the current context of the [WebView] managed by that controller when you need to target iOS below 11, MacOS below 10.13 and Web platform. JavaScript must be enabled in order to work.
2021-01-30 13:53:32 +00:00
///In this case the [url] parameter is ignored.
///
2022-10-18 16:12:33 +00:00
///**NOTE for iOS below 11.0 and MacOS below 10.13**: If [webViewController] is `null` or JavaScript is disabled for it, it will try to use a [HeadlessInAppWebView]
2022-04-25 20:36:21 +00:00
///to delete the cookie (session-only cookie and cookie with `isHttpOnly` enabled won't be deleted!).
///
///**NOTE for Web**: this method will have effect only if the iframe has the same origin.
///If [webViewController] is `null` or JavaScript is disabled for it, it will try to use a [HeadlessInAppWebView]
2021-01-30 13:53:32 +00:00
///to delete the cookie (session-only cookie and cookie with `isHttpOnly` enabled won't be deleted!).
2022-04-19 23:31:14 +00:00
///
///**Supported Platforms/Implementations**:
///- Android native WebView
///- iOS ([Official API - WKHTTPCookieStore.delete](https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882009-delete)
2022-10-18 16:12:33 +00:00
///- MacOS ([Official API - WKHTTPCookieStore.delete](https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882009-delete)
2022-04-25 20:36:21 +00:00
///- Web
2019-12-01 11:55:06 +00:00
Future < void > deleteCookie (
2023-11-17 22:28:11 +00:00
{ required WebUri url ,
required String name ,
String path = " / " ,
String ? domain ,
@ Deprecated ( " Use webViewController instead " )
InAppWebViewController ? iosBelow11WebViewController ,
InAppWebViewController ? webViewController } ) = >
platform . deleteCookie (
2022-10-18 16:12:33 +00:00
url: url ,
name: name ,
path: path ,
domain: domain ,
2023-11-17 22:28:11 +00:00
iosBelow11WebViewController: iosBelow11WebViewController ? . platform ,
webViewController: webViewController ? . platform ) ;
2019-10-26 02:42:50 +00:00
///Removes all cookies for the given [url], [domain] and [path].
///
///The default value of [path] is `"/"`.
2021-01-30 13:53:32 +00:00
///
2022-04-25 20:36:21 +00:00
///[webViewController] is used for deleting the cookies (also session-only cookies) using JavaScript (cookies with `isHttpOnly` enabled cannot be deleted, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies)
2022-10-18 16:12:33 +00:00
///from the current context of the [WebView] managed by that controller when you need to target iOS below 11, MacOS below 10.13 and Web platform. JavaScript must be enabled in order to work.
2021-01-30 13:53:32 +00:00
///In this case the [url] parameter is ignored.
///
2022-10-18 16:12:33 +00:00
///**NOTE for iOS below 11.0 and MacOS below 10.13**: If [webViewController] is `null` or JavaScript is disabled for it, it will try to use a [HeadlessInAppWebView]
2022-04-25 20:36:21 +00:00
///to delete the cookies (session-only cookies and cookies with `isHttpOnly` enabled won't be deleted!).
///
///**NOTE for Web**: this method will have effect only if the iframe has the same origin.
///If [webViewController] is `null` or JavaScript is disabled for it, it will try to use a [HeadlessInAppWebView]
2021-01-30 13:53:32 +00:00
///to delete the cookies (session-only cookies and cookies with `isHttpOnly` enabled won't be deleted!).
2022-04-19 23:31:14 +00:00
///
///**Supported Platforms/Implementations**:
///- Android native WebView
///- iOS
2022-10-18 16:12:33 +00:00
///- MacOS
2022-04-25 20:36:21 +00:00
///- Web
2019-12-01 11:55:06 +00:00
Future < void > deleteCookies (
2023-11-17 22:28:11 +00:00
{ required WebUri url ,
String path = " / " ,
String ? domain ,
@ Deprecated ( " Use webViewController instead " )
InAppWebViewController ? iosBelow11WebViewController ,
InAppWebViewController ? webViewController } ) = >
platform . deleteCookies (
url: url ,
path: path ,
domain: domain ,
iosBelow11WebViewController: iosBelow11WebViewController ? . platform ,
webViewController: webViewController ? . platform ) ;
2019-10-26 02:42:50 +00:00
///Removes all cookies.
2021-01-30 13:53:32 +00:00
///
///**NOTE for iOS**: available from iOS 11.0+.
2022-04-19 23:31:14 +00:00
///
2022-10-18 16:12:33 +00:00
///**NOTE for MacOS**: available from iOS 10.13+.
///
2022-04-19 23:31:14 +00:00
///**Supported Platforms/Implementations**:
///- Android native WebView ([Official API - CookieManager.removeAllCookies](https://developer.android.com/reference/android/webkit/CookieManager#removeAllCookies(android.webkit.ValueCallback%3Cjava.lang.Boolean%3E)))
///- iOS ([Official API - WKWebsiteDataStore.removeData](https://developer.apple.com/documentation/webkit/wkwebsitedatastore/1532938-removedata))
2022-10-18 16:12:33 +00:00
///- MacOS ([Official API - WKWebsiteDataStore.removeData](https://developer.apple.com/documentation/webkit/wkwebsitedatastore/1532938-removedata))
2023-11-17 22:28:11 +00:00
Future < void > deleteAllCookies ( ) = > platform . deleteAllCookies ( ) ;
2019-10-26 02:42:50 +00:00
2022-04-19 23:31:14 +00:00
///Fetches all stored cookies.
///
2022-10-18 16:12:33 +00:00
///**NOTE for iOS**: available on iOS 11.0+.
///
///**NOTE for MacOS**: available from iOS 10.13+.
2022-04-19 23:31:14 +00:00
///
///**Supported Platforms/Implementations**:
///- iOS ([Official API - WKHTTPCookieStore.getAllCookies](https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882005-getallcookies))
2022-10-18 16:12:33 +00:00
///- MacOS ([Official API - WKHTTPCookieStore.getAllCookies](https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882005-getallcookies))
2023-11-17 22:28:11 +00:00
Future < List < Cookie > > getAllCookies ( ) = > platform . getAllCookies ( ) ;
2019-12-01 11:55:06 +00:00
}
2021-01-31 21:08:20 +00:00
///Class that contains only iOS-specific methods of [CookieManager].
2022-04-19 23:31:14 +00:00
///Use [CookieManager] instead.
@ Deprecated ( " Use CookieManager instead " )
2021-01-31 21:08:20 +00:00
class IOSCookieManager {
static IOSCookieManager ? _instance ;
///Gets the [IOSCookieManager] shared instance.
static IOSCookieManager instance ( ) {
return ( _instance ! = null ) ? _instance ! : _init ( ) ;
}
2022-10-19 00:42:23 +00:00
IOSCookieManager . _ ( ) ;
2021-01-31 21:08:20 +00:00
static IOSCookieManager _init ( ) {
2022-10-19 00:42:23 +00:00
_instance = IOSCookieManager . _ ( ) ;
2021-01-31 21:08:20 +00:00
return _instance ! ;
}
///Fetches all stored cookies.
///
///**NOTE**: available on iOS 11.0+.
///
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882005-getallcookies
Future < List < Cookie > > getAllCookies ( ) async {
2023-11-17 22:28:11 +00:00
return CookieManager . instance ( ) . getAllCookies ( ) ;
2021-01-31 21:08:20 +00:00
}
2021-02-22 22:54:09 +00:00
}