2019-11-02 03:16:47 +00:00
import ' dart:async ' ;
2019-11-10 13:11:30 +00:00
import ' package:flutter/foundation.dart ' ;
2019-10-26 02:42:50 +00:00
import ' package:flutter/services.dart ' ;
2019-11-25 00:42:27 +00:00
import ' types.dart ' ;
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
///
///**NOTE for iOS**: available from iOS 11.0+.
class CookieManager {
2019-10-31 02:20:07 +00:00
static CookieManager _instance ;
2019-12-01 11:55:06 +00:00
static const MethodChannel _channel = const MethodChannel (
' com.pichillilorenzo/flutter_inappwebview_cookiemanager ' ) ;
2019-10-26 02:42:50 +00:00
2019-11-25 22:04:17 +00:00
///Gets the cookie manager shared instance.
2019-10-31 02:20:07 +00:00
static CookieManager instance ( ) {
return ( _instance ! = null ) ? _instance : _init ( ) ;
2019-10-26 02:42:50 +00:00
}
2019-10-31 02:20:07 +00:00
static CookieManager _init ( ) {
_channel . setMethodCallHandler ( _handleMethod ) ;
_instance = new CookieManager ( ) ;
return _instance ;
}
2019-12-01 11:55:06 +00:00
static Future < dynamic > _handleMethod ( MethodCall call ) async { }
2019-10-26 02:42:50 +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.
///
///The default value of [path] is `"/"`.
///If [domain] is `null`, its default value will be the domain name of [url].
2019-12-01 11:55:06 +00:00
Future < void > setCookie (
{ @ required String url ,
@ required String name ,
@ required String value ,
String domain ,
String path = " / " ,
int expiresDate ,
int maxAge ,
bool isSecure } ) async {
if ( domain = = null ) domain = _getDomainName ( url ) ;
2019-10-26 02:42:50 +00:00
assert ( url ! = null & & url . isNotEmpty ) ;
assert ( name ! = null & & name . isNotEmpty ) ;
assert ( value ! = null & & value . isNotEmpty ) ;
assert ( domain ! = null & & domain . isNotEmpty ) ;
assert ( path ! = null & & path . isNotEmpty ) ;
Map < String , dynamic > args = < String , dynamic > { } ;
args . putIfAbsent ( ' url ' , ( ) = > url ) ;
args . putIfAbsent ( ' name ' , ( ) = > name ) ;
args . putIfAbsent ( ' value ' , ( ) = > value ) ;
args . putIfAbsent ( ' domain ' , ( ) = > domain ) ;
args . putIfAbsent ( ' path ' , ( ) = > path ) ;
args . putIfAbsent ( ' expiresDate ' , ( ) = > expiresDate ? . toString ( ) ) ;
args . putIfAbsent ( ' maxAge ' , ( ) = > maxAge ) ;
args . putIfAbsent ( ' isSecure ' , ( ) = > isSecure ) ;
await _channel . invokeMethod ( ' setCookie ' , args ) ;
}
///Gets all the cookies for the given [url].
2019-11-25 00:42:27 +00:00
Future < List < Cookie > > getCookies ( { @ required String url } ) async {
2019-10-26 02:42:50 +00:00
assert ( url ! = null & & url . isNotEmpty ) ;
Map < String , dynamic > args = < String , dynamic > { } ;
args . putIfAbsent ( ' url ' , ( ) = > url ) ;
2019-12-01 11:55:06 +00:00
List < dynamic > cookieListMap =
await _channel . invokeMethod ( ' getCookies ' , args ) ;
2019-11-25 00:42:27 +00:00
cookieListMap = cookieListMap . cast < Map < dynamic , dynamic > > ( ) ;
List < Cookie > cookies = [ ] ;
2019-12-01 11:55:06 +00:00
for ( var i = 0 ; i < cookieListMap . length ; i + + ) {
cookies . add ( Cookie (
name: cookieListMap [ i ] [ " name " ] , value: cookieListMap [ i ] [ " value " ] ) ) ;
2019-10-26 02:42:50 +00:00
}
return cookies ;
}
///Gets a cookie by its [name] for the given [url].
2019-12-01 11:55:06 +00:00
Future < Cookie > getCookie (
{ @ required String url , @ required String name } ) async {
2019-10-26 02:42:50 +00:00
assert ( url ! = null & & url . isNotEmpty ) ;
assert ( name ! = null & & name . isNotEmpty ) ;
Map < String , dynamic > args = < String , dynamic > { } ;
args . putIfAbsent ( ' url ' , ( ) = > url ) ;
List < dynamic > cookies = await _channel . invokeMethod ( ' getCookies ' , args ) ;
cookies = cookies . cast < Map < dynamic , dynamic > > ( ) ;
2019-12-01 11:55:06 +00:00
for ( var i = 0 ; i < cookies . length ; i + + ) {
2019-10-26 02:42:50 +00:00
cookies [ i ] = cookies [ i ] . cast < String , dynamic > ( ) ;
if ( cookies [ i ] [ " name " ] = = name )
2019-11-25 00:42:27 +00:00
return Cookie ( name: cookies [ i ] [ " name " ] , value: cookies [ i ] [ " value " ] ) ;
2019-10-26 02:42:50 +00:00
}
return null ;
}
///Removes a cookie by its [name] for the given [url], [domain] and [path].
///
///The default value of [path] is `"/"`.
///If [domain] is `null` or empty, its default value will be the domain name of [url].
2019-12-01 11:55:06 +00:00
Future < void > deleteCookie (
{ @ required String url ,
@ required String name ,
String domain = " " ,
String path = " / " } ) async {
if ( domain = = null | | domain . isEmpty ) domain = _getDomainName ( url ) ;
2019-10-26 02:42:50 +00:00
assert ( url ! = null & & url . isNotEmpty ) ;
assert ( name ! = null & & name . isNotEmpty ) ;
assert ( domain ! = null & & url . isNotEmpty ) ;
assert ( path ! = null & & url . isNotEmpty ) ;
Map < String , dynamic > args = < String , dynamic > { } ;
args . putIfAbsent ( ' url ' , ( ) = > url ) ;
args . putIfAbsent ( ' name ' , ( ) = > name ) ;
args . putIfAbsent ( ' domain ' , ( ) = > domain ) ;
args . putIfAbsent ( ' path ' , ( ) = > path ) ;
await _channel . invokeMethod ( ' deleteCookie ' , args ) ;
}
///Removes all cookies for the given [url], [domain] and [path].
///
///The default value of [path] is `"/"`.
///If [domain] is `null` or empty, its default value will be the domain name of [url].
2019-12-01 11:55:06 +00:00
Future < void > deleteCookies (
{ @ required String url , String domain = " " , String path = " / " } ) async {
if ( domain = = null | | domain . isEmpty ) domain = _getDomainName ( url ) ;
2019-10-26 02:42:50 +00:00
assert ( url ! = null & & url . isNotEmpty ) ;
assert ( domain ! = null & & url . isNotEmpty ) ;
assert ( path ! = null & & url . isNotEmpty ) ;
Map < String , dynamic > args = < String , dynamic > { } ;
args . putIfAbsent ( ' url ' , ( ) = > url ) ;
args . putIfAbsent ( ' domain ' , ( ) = > domain ) ;
args . putIfAbsent ( ' path ' , ( ) = > path ) ;
await _channel . invokeMethod ( ' deleteCookies ' , args ) ;
}
///Removes all cookies.
2019-10-31 02:20:07 +00:00
Future < void > deleteAllCookies ( ) async {
2019-10-26 02:42:50 +00:00
Map < String , dynamic > args = < String , dynamic > { } ;
await _channel . invokeMethod ( ' deleteAllCookies ' , args ) ;
}
2019-10-31 02:20:07 +00:00
String _getDomainName ( String url ) {
2019-10-26 02:42:50 +00:00
Uri uri = Uri . parse ( url ) ;
String domain = uri . host ;
2019-12-01 11:55:06 +00:00
if ( domain = = null ) return " " ;
2019-10-26 02:42:50 +00:00
return domain . startsWith ( " www. " ) ? domain . substring ( 4 ) : domain ;
}
2019-12-01 11:55:06 +00:00
}