diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 55989e27..201b1711 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -15,16 +15,12 @@ + - - - - + - - @@ -41,50 +37,25 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - + - - + + @@ -92,21 +63,26 @@ - - - - - + + + + + + + + + + - + - + @@ -126,8 +102,6 @@ - _throwIsAlreadyOpened - isHidden shouldOverrideUrlLoading toolbarTopFixedTitle useChromeSafariBrowser @@ -156,6 +130,8 @@ assert( sNotEmpty() loadData + _valide + initialized activity.getPreferences(0) @@ -197,11 +173,11 @@ @@ -217,18 +193,6 @@ - - - - - - - - - + + + @@ -408,16 +384,15 @@ - - + - + @@ -430,6 +405,7 @@ + @@ -685,10 +661,27 @@ + + + + + + + + + + + + + + + + + - - + + @@ -699,60 +692,42 @@ - - - - - - - - - - - - - - - - - - - + + - - + + + + + + + + + + + + + + + + - - + + - - - - - - - - - - - - - - - diff --git a/CHANGELOG.md b/CHANGELOG.md index 59f235d6..efc659aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.5.3 + +- added `CookieManager` class + ## 0.5.2 - fixed some missing `result.success()` on Android and iOS diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java index 7c08780c..a7e091db 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java @@ -39,6 +39,8 @@ import com.pichillilorenzo.flutter_inappbrowser.ChromeCustomTabs.CustomTabActivi import java.io.IOException; import java.io.Serializable; +import java.net.MalformedURLException; +import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -78,6 +80,8 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler { final MethodChannel channel = new MethodChannel(registrar.messenger(), "com.pichillilorenzo/flutter_inappbrowser"); channel.setMethodCallHandler(new InAppBrowserFlutterPlugin(registrar, activity)); + new MyCookieManager(registrar); + registrar .platformViewRegistry() .registerViewFactory( diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/MyCookieManager.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/MyCookieManager.java new file mode 100644 index 00000000..7717a56c --- /dev/null +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/MyCookieManager.java @@ -0,0 +1,96 @@ +package com.pichillilorenzo.flutter_inappbrowser; + +import android.os.Build; +import android.text.TextUtils; +import android.util.Log; +import android.webkit.CookieManager; +import android.webkit.ValueCallback; + +import java.net.HttpCookie; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; + +import io.flutter.plugin.common.MethodCall; +import io.flutter.plugin.common.MethodChannel; +import io.flutter.plugin.common.PluginRegistry; + +public class MyCookieManager implements MethodChannel.MethodCallHandler { + + static final String LOG_TAG = "MyCookieManager"; + + public static PluginRegistry.Registrar registrar; + public static MethodChannel channel; + + public MyCookieManager(PluginRegistry.Registrar r) { + registrar = r; + channel = new MethodChannel(registrar.messenger(), "com.pichillilorenzo/flutter_inappbrowser_cookiemanager"); + channel.setMethodCallHandler(this); + } + + @Override + public void onMethodCall(MethodCall call, MethodChannel.Result result) { + switch (call.method) { + case "setCookie": + { + String url = (String) call.argument("url"); + String name = (String) call.argument("name"); + String value = (String) call.argument("value"); + String domain = (String) call.argument("domain"); + String path = (String) call.argument("path"); + Long expiresDate = new Long((Integer) call.argument("expiresDate")); + Boolean isHTTPOnly = (Boolean) call.argument("isHTTPOnly"); + Boolean isSecure = (Boolean) call.argument("isSecure"); + MyCookieManager.setCookie(url, name, value, domain, path, expiresDate, isHTTPOnly, isSecure, result); + } + break; + default: + result.notImplemented(); + } + } + + public static void setCookie(String url, + String name, + String value, + String domain, + String path, + Long expiresDate, + Boolean isHTTPOnly, + Boolean isSecure, + final MethodChannel.Result result) { + + String cookieValue = name + "=" + value; + + if (domain != null && !domain.isEmpty()) + cookieValue += "; Domain=" + domain; + + if (path != null && !path.isEmpty()) + cookieValue += "; Path=" + path; + + if (expiresDate != null) + cookieValue += "; Max-Age=" + expiresDate.toString(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) + if (isHTTPOnly != null && isHTTPOnly) + cookieValue += "; HttpOnly"; + + if (isSecure != null && isSecure) + cookieValue += "; Secure"; + + CookieManager cookieManager = CookieManager.getInstance(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + cookieManager.setCookie(url, cookieValue, new ValueCallback() { + @Override + public void onReceiveValue(Boolean aBoolean) { + result.success(true); + } + }); + } + else { + cookieManager.setCookie(url, cookieValue); + result.success(true); + } + } + +} diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/Util.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/Util.java index e5592261..9ddfcbab 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/Util.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/Util.java @@ -4,14 +4,14 @@ import android.content.res.AssetManager; import java.io.IOException; import java.io.InputStream; - import io.flutter.plugin.common.PluginRegistry; public class Util { + static final String LOG_TAG = "Util"; public static final String ANDROID_ASSET_URL = "file:///android_asset/"; - public static String getUrlAsset (PluginRegistry.Registrar registrar, String assetFilePath) throws IOException { + public static String getUrlAsset(PluginRegistry.Registrar registrar, String assetFilePath) throws IOException { String key = registrar.lookupKeyForAsset(assetFilePath); AssetManager mg = registrar.activeContext().getResources().getAssets(); InputStream is = null; @@ -36,4 +36,5 @@ public class Util { return ANDROID_ASSET_URL + key; } + } diff --git a/example/lib/main.dart b/example/lib/main.dart index c24d8d67..358afaca 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -65,7 +65,7 @@ class MyInAppBrowser extends InAppBrowser { // await this.webViewController.injectScriptCode("console.error('testError', false);"); // await this.webViewController.injectScriptCode("console.debug('testDebug', true);"); // -// print(await this.webViewController.injectScriptCode("document.body.innerHTML")); + print(await this.webViewController.injectScriptCode("document.cookie")); // // print(await this.webViewController.injectScriptCode("null")); // print(await this.webViewController.injectScriptCode("undefined")); @@ -271,6 +271,7 @@ class _MyAppState extends State { // //"toolbarBottom": false // }); // + await CookieManager.setCookie("https://flutter.io/", "my_cookie2", "cookieValue2", "flutter.io", expiresDate: 1000000, path: "/get-started/install"); await inAppBrowserFallback.open(url: "https://flutter.io/", options: { //"useOnLoadResource": true, //"hidden": true, diff --git a/lib/flutter_inappbrowser.dart b/lib/flutter_inappbrowser.dart index 4b410e60..ffb8e710 100644 --- a/lib/flutter_inappbrowser.dart +++ b/lib/flutter_inappbrowser.dart @@ -86,7 +86,7 @@ class ConsoleMessage { class _ChannelManager { static const MethodChannel channel = const MethodChannel('com.pichillilorenzo/flutter_inappbrowser'); - static final initialized = false; + static bool initialized = false; static final listeners = HashMap(); static Future _handleMethod(MethodCall call) async { @@ -102,6 +102,7 @@ class _ChannelManager { static void init () { channel.setMethodCallHandler(_handleMethod); + initialized = true; } } @@ -1116,4 +1117,44 @@ class InAppLocalhostServer { } } -} \ No newline at end of file +} + +class CookieManager { + static bool _initialized = false; + static const MethodChannel _channel = const MethodChannel('com.pichillilorenzo/flutter_inappbrowser_cookiemanager'); + + static void _init () { + _channel.setMethodCallHandler(_handleMethod); + _initialized = true; + } + + static Future _handleMethod(MethodCall call) async { + } + + static Future setCookie(String url, String name, String value, String domain, + { String path = "/", + int expiresDate, + bool isHTTPOnly, + bool isSecure }) async { + if (!_initialized) + _init(); + + 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 args = {}; + args.putIfAbsent('url', () => url); + args.putIfAbsent('name', () => name); + args.putIfAbsent('value', () => value); + args.putIfAbsent('domain', () => domain); + args.putIfAbsent('path', () => path); + args.putIfAbsent('expiresDate', () => expiresDate); + args.putIfAbsent('isHTTPOnly', () => isHTTPOnly); + args.putIfAbsent('isSecure', () => isSecure); + + await _channel.invokeMethod('setCookie', args); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 93d2a649..5dc82525 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_inappbrowser description: A Flutter plugin that allows you to add an inline webview or open an in-app browser window. (inspired by the popular cordova-plugin-inappbrowser). -version: 0.5.2 +version: 0.5.3 author: Lorenzo Pichilli homepage: https://github.com/pichillilorenzo/flutter_inappbrowser