Skip to content

Commit

Permalink
working on CookieManager class (Manage cookies #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
pichillilorenzo committed Oct 23, 2018
1 parent aa88e7d commit 35233f0
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 107 deletions.
177 changes: 76 additions & 101 deletions .idea/workspace.xml

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.3

- added `CookieManager` class

## 0.5.2

- fixed some missing `result.success()` on Android and iOS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

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;
Expand Down Expand Up @@ -78,6 +80,8 @@ public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "com.pichillilorenzo/flutter_inappbrowser");
channel.setMethodCallHandler(new InAppBrowserFlutterPlugin(registrar, activity));

new MyCookieManager(registrar);

registrar
.platformViewRegistry()
.registerViewFactory(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Boolean>() {
@Override
public void onReceiveValue(Boolean aBoolean) {
result.success(true);
}
});
}
else {
cookieManager.setCookie(url, cookieValue);
result.success(true);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

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;
Expand All @@ -36,4 +36,5 @@ public static String getUrlAsset (PluginRegistry.Registrar registrar, String ass

return ANDROID_ASSET_URL + key;
}

}
3 changes: 2 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -271,6 +271,7 @@ class _MyAppState extends State<MyApp> {
// //"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,
Expand Down
45 changes: 43 additions & 2 deletions lib/flutter_inappbrowser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ListenerCallback>();

static Future<dynamic> _handleMethod(MethodCall call) async {
Expand All @@ -102,6 +102,7 @@ class _ChannelManager {

static void init () {
channel.setMethodCallHandler(_handleMethod);
initialized = true;
}
}

Expand Down Expand Up @@ -1116,4 +1117,44 @@ class InAppLocalhostServer {
}
}

}
}

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<dynamic> _handleMethod(MethodCall call) async {
}

static Future<void> 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<String, dynamic> args = <String, dynamic>{};
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);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
homepage: https://github.com/pichillilorenzo/flutter_inappbrowser

Expand Down

0 comments on commit 35233f0

Please sign in to comment.