Skip to content

Commit

Permalink
added getCookies() and getCookie() for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
pichillilorenzo committed Oct 24, 2018
1 parent 35233f0 commit 40dccfd
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 101 deletions.
131 changes: 37 additions & 94 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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;
Expand Down Expand Up @@ -44,6 +44,9 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
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();
}
Expand All @@ -70,9 +73,8 @@ public static void setCookie(String url,
if (expiresDate != null)
cookieValue += "; Max-Age=" + expiresDate.toString();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
if (isHTTPOnly != null && isHTTPOnly)
cookieValue += "; HttpOnly";
if (isHTTPOnly != null && isHTTPOnly)
cookieValue += "; HttpOnly";

if (isSecure != null && isSecure)
cookieValue += "; Secure";
Expand All @@ -93,4 +95,24 @@ public void onReceiveValue(Boolean aBoolean) {
}
}

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;

}

}
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'com.android.tools.build:gradle:3.2.1'
}
}

Expand Down
7 changes: 7 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -281,6 +287,7 @@ class _MyAppState extends State<MyApp> {
//"toolbarTop": false,
//"toolbarBottom": false
});

},
child: Text("Open InAppBrowser")
),
Expand Down
34 changes: 34 additions & 0 deletions lib/flutter_inappbrowser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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,
Expand All @@ -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;
}
}

0 comments on commit 40dccfd

Please sign in to comment.