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 [AndroidHttpAuthCredentialDatabase]. /// /// When adding additional fields make sure they can be null or have a default /// value to avoid breaking changes. See [PlatformHttpAuthCredentialDatabaseCreationParams] for /// more information. @immutable class AndroidHttpAuthCredentialDatabaseCreationParams extends PlatformHttpAuthCredentialDatabaseCreationParams { /// Creates a new [AndroidHttpAuthCredentialDatabaseCreationParams] instance. const AndroidHttpAuthCredentialDatabaseCreationParams( // This parameter prevents breaking changes later. // ignore: avoid_unused_constructor_parameters PlatformHttpAuthCredentialDatabaseCreationParams params, ) : super(); /// Creates a [AndroidHttpAuthCredentialDatabaseCreationParams] instance based on [PlatformHttpAuthCredentialDatabaseCreationParams]. factory AndroidHttpAuthCredentialDatabaseCreationParams.fromPlatformHttpAuthCredentialDatabaseCreationParams( PlatformHttpAuthCredentialDatabaseCreationParams params) { return AndroidHttpAuthCredentialDatabaseCreationParams(params); } } ///{@macro flutter_inappwebview_platform_interface.PlatformHttpAuthCredentialDatabase} class AndroidHttpAuthCredentialDatabase extends PlatformHttpAuthCredentialDatabase with ChannelController { /// Creates a new [AndroidHttpAuthCredentialDatabase]. AndroidHttpAuthCredentialDatabase( PlatformHttpAuthCredentialDatabaseCreationParams params) : super.implementation( params is AndroidHttpAuthCredentialDatabaseCreationParams ? params : AndroidHttpAuthCredentialDatabaseCreationParams .fromPlatformHttpAuthCredentialDatabaseCreationParams(params), ) { channel = const MethodChannel( 'com.pichillilorenzo/flutter_inappwebview_credential_database'); handler = handleMethod; initMethodCallHandler(); } static AndroidHttpAuthCredentialDatabase? _instance; ///Gets the database shared instance. static AndroidHttpAuthCredentialDatabase instance() { return (_instance != null) ? _instance! : _init(); } static AndroidHttpAuthCredentialDatabase _init() { _instance = AndroidHttpAuthCredentialDatabase( AndroidHttpAuthCredentialDatabaseCreationParams( const PlatformHttpAuthCredentialDatabaseCreationParams())); return _instance!; } Future _handleMethod(MethodCall call) async {} @override Future> getAllAuthCredentials() async { Map args = {}; List allCredentials = await channel?.invokeMethod('getAllAuthCredentials', args) ?? []; List result = []; for (Map map in allCredentials) { var element = URLProtectionSpaceHttpAuthCredentials.fromMap( map.cast()); if (element != null) { result.add(element); } } return result; } @override Future> getHttpAuthCredentials( {required URLProtectionSpace protectionSpace}) async { Map args = {}; args.putIfAbsent("host", () => protectionSpace.host); args.putIfAbsent("protocol", () => protectionSpace.protocol); args.putIfAbsent("realm", () => protectionSpace.realm); args.putIfAbsent("port", () => protectionSpace.port); List credentialList = await channel?.invokeMethod('getHttpAuthCredentials', args) ?? []; List credentials = []; for (Map map in credentialList) { var credential = URLCredential.fromMap(map.cast()); if (credential != null) { credentials.add(credential); } } return credentials; } @override Future setHttpAuthCredential( {required URLProtectionSpace protectionSpace, required URLCredential credential}) async { Map args = {}; args.putIfAbsent("host", () => protectionSpace.host); args.putIfAbsent("protocol", () => protectionSpace.protocol); args.putIfAbsent("realm", () => protectionSpace.realm); args.putIfAbsent("port", () => protectionSpace.port); args.putIfAbsent("username", () => credential.username); args.putIfAbsent("password", () => credential.password); await channel?.invokeMethod('setHttpAuthCredential', args); } @override Future removeHttpAuthCredential( {required URLProtectionSpace protectionSpace, required URLCredential credential}) async { Map args = {}; args.putIfAbsent("host", () => protectionSpace.host); args.putIfAbsent("protocol", () => protectionSpace.protocol); args.putIfAbsent("realm", () => protectionSpace.realm); args.putIfAbsent("port", () => protectionSpace.port); args.putIfAbsent("username", () => credential.username); args.putIfAbsent("password", () => credential.password); await channel?.invokeMethod('removeHttpAuthCredential', args); } @override Future removeHttpAuthCredentials( {required URLProtectionSpace protectionSpace}) async { Map args = {}; args.putIfAbsent("host", () => protectionSpace.host); args.putIfAbsent("protocol", () => protectionSpace.protocol); args.putIfAbsent("realm", () => protectionSpace.realm); args.putIfAbsent("port", () => protectionSpace.port); await channel?.invokeMethod('removeHttpAuthCredentials', args); } @override Future clearAllAuthCredentials() async { Map args = {}; await channel?.invokeMethod('clearAllAuthCredentials', args); } @override void dispose() { // empty } } extension InternalHttpAuthCredentialDatabase on AndroidHttpAuthCredentialDatabase { get handleMethod => _handleMethod; }