Commit 40dccfde authored by pichillilorenzo's avatar pichillilorenzo

added getCookies() and getCookie() for Android

parent 35233f09
This diff is collapsed.
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
......@@ -44,6 +44,9 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
MyCookieManager.setCookie(url, name, value, domain, path, expiresDate, isHTTPOnly, isSecure, result);
}
break;
case "getCookies":
result.success(MyCookieManager.getCookies((String) call.argument("url")));
break;
default:
result.notImplemented();
}
......@@ -70,7 +73,6 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
if (expiresDate != null)
cookieValue += "; Max-Age=" + expiresDate.toString();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
if (isHTTPOnly != null && isHTTPOnly)
cookieValue += "; HttpOnly";
......@@ -93,4 +95,24 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
}
}
public static List<Map<String, Object>> getCookies(final String url) {
final List<Map<String, Object>> cookieListMap = new ArrayList<>();
CookieManager cookieManager = CookieManager.getInstance();
String[] cookies = cookieManager.getCookie(url).split(";");
for (String cookie : cookies) {
String[] nameValue = cookie.split("=", 2);
String name = nameValue[0].trim();
String value = nameValue[1].trim();
Map<String, Object> cookieMap = new HashMap<>();
cookieMap.put("name", name);
cookieMap.put("value", value);
cookieListMap.add(cookieMap);
}
return cookieListMap;
}
}
......@@ -5,7 +5,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'com.android.tools.build:gradle:3.2.1'
}
}
......
......@@ -66,6 +66,12 @@ class MyInAppBrowser extends InAppBrowser {
// await this.webViewController.injectScriptCode("console.debug('testDebug', true);");
//
print(await this.webViewController.injectScriptCode("document.cookie"));
print("");
print(await CookieManager.getCookies("https://flutter.io/"));
print("");
print(await CookieManager.getCookie("https://flutter.io/", "_ga"));
print("");
//
// print(await this.webViewController.injectScriptCode("null"));
// print(await this.webViewController.injectScriptCode("undefined"));
......@@ -281,6 +287,7 @@ class _MyAppState extends State<MyApp> {
//"toolbarTop": false,
//"toolbarBottom": false
});
},
child: Text("Open InAppBrowser")
),
......
......@@ -1119,6 +1119,7 @@ class InAppLocalhostServer {
}
///Manages the cookies used by an application's [InAppWebView] instances.
class CookieManager {
static bool _initialized = false;
static const MethodChannel _channel = const MethodChannel('com.pichillilorenzo/flutter_inappbrowser_cookiemanager');
......@@ -1131,6 +1132,7 @@ class CookieManager {
static Future<dynamic> _handleMethod(MethodCall call) async {
}
///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.
static Future<void> setCookie(String url, String name, String value, String domain,
{ String path = "/",
int expiresDate,
......@@ -1157,4 +1159,36 @@ class CookieManager {
await _channel.invokeMethod('setCookie', args);
}
///Gets all the cookies for the given [url].
static Future<List<Map<String, dynamic>>> getCookies(String url) async {
assert(url != null && url.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>>();
for(var i = 0; i < cookies.length; i++) {
cookies[i] = cookies[i].cast<String, dynamic>();
}
cookies = cookies.cast<Map<String, dynamic>>();
return cookies;
}
///Gets a cookie by its [cookieName] for the given [url].
static Future<Map<String, dynamic>> getCookie(String url, String cookieName) async {
assert(url != null && url.isNotEmpty);
assert(cookieName != null && cookieName.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>>();
for(var i = 0; i < cookies.length; i++) {
cookies[i] = cookies[i].cast<String, dynamic>();
if (cookies[i]["name"] == cookieName)
return cookies[i];
}
return null;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment