import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart'; /// Object specifying creation parameters for creating a [AndroidCookieManager]. /// /// When adding additional fields make sure they can be null or have a default /// value to avoid breaking changes. See [PlatformCookieManagerCreationParams] for /// more information. @immutable class AndroidCookieManagerCreationParams extends PlatformCookieManagerCreationParams { /// Creates a new [AndroidCookieManagerCreationParams] instance. const AndroidCookieManagerCreationParams( // This parameter prevents breaking changes later. // ignore: avoid_unused_constructor_parameters PlatformCookieManagerCreationParams params, ) : super(); /// Creates a [AndroidCookieManagerCreationParams] instance based on [PlatformCookieManagerCreationParams]. factory AndroidCookieManagerCreationParams.fromPlatformCookieManagerCreationParams( PlatformCookieManagerCreationParams params) { return AndroidCookieManagerCreationParams(params); } } ///{@macro flutter_inappwebview_platform_interface.PlatformCookieManager} class AndroidCookieManager extends PlatformCookieManager with ChannelController { /// Creates a new [AndroidCookieManager]. AndroidCookieManager(PlatformCookieManagerCreationParams params) : super.implementation( params is AndroidCookieManagerCreationParams ? params : AndroidCookieManagerCreationParams .fromPlatformCookieManagerCreationParams(params), ) { channel = const MethodChannel( 'com.pichillilorenzo/flutter_inappwebview_cookiemanager'); handler = handleMethod; initMethodCallHandler(); } static AndroidCookieManager? _instance; ///Gets the [AndroidCookieManager] shared instance. static AndroidCookieManager instance() { return (_instance != null) ? _instance! : _init(); } static AndroidCookieManager _init() { _instance = AndroidCookieManager(AndroidCookieManagerCreationParams( const PlatformCookieManagerCreationParams())); return _instance!; } Future _handleMethod(MethodCall call) async {} @override Future setCookie( {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") PlatformInAppWebViewController? iosBelow11WebViewController, PlatformInAppWebViewController? webViewController}) async { assert(url.toString().isNotEmpty); assert(name.isNotEmpty); assert(value.isNotEmpty); assert(path.isNotEmpty); Map args = {}; args.putIfAbsent('url', () => url.toString()); 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); args.putIfAbsent('isHttpOnly', () => isHttpOnly); args.putIfAbsent('sameSite', () => sameSite?.toNativeValue()); return await channel?.invokeMethod('setCookie', args) ?? false; } @override Future> getCookies( {required WebUri url, @Deprecated("Use webViewController instead") PlatformInAppWebViewController? iosBelow11WebViewController, PlatformInAppWebViewController? webViewController}) async { assert(url.toString().isNotEmpty); List cookies = []; Map args = {}; args.putIfAbsent('url', () => url.toString()); List cookieListMap = await channel?.invokeMethod('getCookies', args) ?? []; cookieListMap = cookieListMap.cast>(); cookieListMap.forEach((cookieMap) { cookies.add(Cookie( name: cookieMap["name"], value: cookieMap["value"], expiresDate: cookieMap["expiresDate"], isSessionOnly: cookieMap["isSessionOnly"], domain: cookieMap["domain"], sameSite: HTTPCookieSameSitePolicy.fromNativeValue(cookieMap["sameSite"]), isSecure: cookieMap["isSecure"], isHttpOnly: cookieMap["isHttpOnly"], path: cookieMap["path"])); }); return cookies; } @override Future getCookie( {required WebUri url, required String name, @Deprecated("Use webViewController instead") PlatformInAppWebViewController? iosBelow11WebViewController, PlatformInAppWebViewController? webViewController}) async { assert(url.toString().isNotEmpty); assert(name.isNotEmpty); Map args = {}; args.putIfAbsent('url', () => url.toString()); List cookies = await channel?.invokeMethod('getCookies', args) ?? []; cookies = cookies.cast>(); for (var i = 0; i < cookies.length; i++) { cookies[i] = cookies[i].cast(); if (cookies[i]["name"] == name) return Cookie( name: cookies[i]["name"], value: cookies[i]["value"], expiresDate: cookies[i]["expiresDate"], isSessionOnly: cookies[i]["isSessionOnly"], domain: cookies[i]["domain"], sameSite: HTTPCookieSameSitePolicy.fromNativeValue( cookies[i]["sameSite"]), isSecure: cookies[i]["isSecure"], isHttpOnly: cookies[i]["isHttpOnly"], path: cookies[i]["path"]); } return null; } @override Future deleteCookie( {required WebUri url, required String name, String path = "/", String? domain, @Deprecated("Use webViewController instead") PlatformInAppWebViewController? iosBelow11WebViewController, PlatformInAppWebViewController? webViewController}) async { assert(url.toString().isNotEmpty); assert(name.isNotEmpty); Map args = {}; args.putIfAbsent('url', () => url.toString()); args.putIfAbsent('name', () => name); args.putIfAbsent('domain', () => domain); args.putIfAbsent('path', () => path); return await channel?.invokeMethod('deleteCookie', args) ?? false; } @override Future deleteCookies( {required WebUri url, String path = "/", String? domain, @Deprecated("Use webViewController instead") PlatformInAppWebViewController? iosBelow11WebViewController, PlatformInAppWebViewController? webViewController}) async { assert(url.toString().isNotEmpty); Map args = {}; args.putIfAbsent('url', () => url.toString()); args.putIfAbsent('domain', () => domain); args.putIfAbsent('path', () => path); return await channel?.invokeMethod('deleteCookies', args) ?? false; } @override Future deleteAllCookies() async { Map args = {}; return await channel?.invokeMethod('deleteAllCookies', args) ?? false; } @override Future removeSessionCookies() async { Map args = {}; return await channel?.invokeMethod('removeSessionCookies', args) ?? false; } @override void dispose() { // empty } } extension InternalCookieManager on AndroidCookieManager { get handleMethod => _handleMethod; }