Skip to content

[webview_flutter_android] Adds support to accept third party cookies #3834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.7.0

* Adds support to accept third party cookies. See
`AndroidWebViewCookieManager.setAcceptThirdPartyCookies`.

## 3.6.3

* Updates gradle, AGP and fixes some lint errors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,93 @@
import android.os.Build;
import android.webkit.CookieManager;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugins.webviewflutter.GeneratedAndroidWebView.CookieManagerHostApi;
import java.util.Objects;

/**
* Host API implementation for `CookieManager`.
*
* <p>This class may handle instantiating and adding native object instances that are attached to a
* Dart instance or handle method calls on the associated native class or an instance of the class.
*/
public class CookieManagerHostApiImpl implements CookieManagerHostApi {
// To ease adding additional methods, this value is added prematurely.
@SuppressWarnings({"unused", "FieldCanBeLocal"})
private final BinaryMessenger binaryMessenger;

private final InstanceManager instanceManager;
private final CookieManagerProxy proxy;

/** Proxy for constructors and static method of `CookieManager`. */
@VisibleForTesting
static class CookieManagerProxy {
/** Handles the Dart static method `MyClass.myStaticMethod`. */
@NonNull
public CookieManager getInstance() {
return CookieManager.getInstance();
}
}

/**
* Constructs a {@link CookieManagerHostApiImpl}.
*
* @param binaryMessenger used to communicate with Dart over asynchronous messages
* @param instanceManager maintains instances stored to communicate with attached Dart objects
*/
public CookieManagerHostApiImpl(
@NonNull BinaryMessenger binaryMessenger, @NonNull InstanceManager instanceManager) {
this(binaryMessenger, instanceManager, new CookieManagerProxy());
}

/**
* Constructs a {@link CookieManagerHostApiImpl}.
*
* @param binaryMessenger used to communicate with Dart over asynchronous messages
* @param instanceManager maintains instances stored to communicate with attached Dart objects
* @param proxy proxy for constructors and static methods of `CookieManager`
*/
public CookieManagerHostApiImpl(
@NonNull BinaryMessenger binaryMessenger,
@NonNull InstanceManager instanceManager,
@NonNull CookieManagerProxy proxy) {
this.binaryMessenger = binaryMessenger;
this.instanceManager = instanceManager;
this.proxy = proxy;
}

class CookieManagerHostApiImpl implements CookieManagerHostApi {
@Override
public void clearCookies(@NonNull GeneratedAndroidWebView.Result<Boolean> result) {
CookieManager cookieManager = CookieManager.getInstance();
public void attachInstance(@NonNull Long instanceIdentifier) {
instanceManager.addDartCreatedInstance(proxy.getInstance(), instanceIdentifier);
}

@Override
public void setCookie(@NonNull Long identifier, @NonNull String url, @NonNull String value) {
getCookieManagerInstance(identifier).setCookie(url, value);
}

@Override
public void removeAllCookies(
@NonNull Long identifier, @NonNull GeneratedAndroidWebView.Result<Boolean> result) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeAllCookies(result::success);
getCookieManagerInstance(identifier).removeAllCookies(result::success);
} else {
result.success(removeCookiesPreL(cookieManager));
result.success(removeCookiesPreL(getCookieManagerInstance(identifier)));
}
}

@Override
public void setCookie(@NonNull String url, @NonNull String value) {
CookieManager.getInstance().setCookie(url, value);
public void setAcceptThirdPartyCookies(
@NonNull Long identifier, @NonNull Long webViewIdentifier, @NonNull Boolean accept) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getCookieManagerInstance(identifier)
.setAcceptThirdPartyCookies(
Objects.requireNonNull(instanceManager.getInstance(webViewIdentifier)), accept);
} else {
throw new UnsupportedOperationException(
"`setAcceptThirdPartyCookies` is unsupported on versions below `Build.VERSION_CODES.LOLLIPOP`.");
}
}

/**
Expand All @@ -40,4 +111,9 @@ private boolean removeCookiesPreL(CookieManager cookieManager) {
}
return hasCookies;
}

@NonNull
private CookieManager getCookieManagerInstance(@NonNull Long identifier) {
return Objects.requireNonNull(instanceManager.getInstance(identifier));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Autogenerated from Pigeon (v9.2.4), do not edit directly.
// Autogenerated from Pigeon (v9.2.5), do not edit directly.
// See also: https://pub.dev/packages/pigeon

package io.flutter.plugins.webviewflutter;
Expand Down Expand Up @@ -589,12 +589,25 @@ public void dispose(@NonNull Long identifierArg, @NonNull Reply<Void> callback)
channelReply -> callback.reply(null));
}
}
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
/**
* Host API for `CookieManager`.
*
* <p>This class may handle instantiating and adding native object instances that are attached to
* a Dart instance or handle method calls on the associated native class or an instance of the
* class.
*
* <p>Generated interface from Pigeon that represents a handler of messages from Flutter.
*/
public interface CookieManagerHostApi {

void clearCookies(@NonNull Result<Boolean> result);

void setCookie(@NonNull String url, @NonNull String value);
/** Handles attaching `CookieManager.instance` to a native instance. */
void attachInstance(@NonNull Long instanceIdentifier);
/** Handles Dart method `CookieManager.setCookie`. */
void setCookie(@NonNull Long identifier, @NonNull String url, @NonNull String value);
/** Handles Dart method `CookieManager.removeAllCookies`. */
void removeAllCookies(@NonNull Long identifier, @NonNull Result<Boolean> result);
/** Handles Dart method `CookieManager.setAcceptThirdPartyCookies`. */
void setAcceptThirdPartyCookies(
@NonNull Long identifier, @NonNull Long webViewIdentifier, @NonNull Boolean accept);

/** The codec used by CookieManagerHostApi. */
static @NonNull MessageCodec<Object> getCodec() {
Expand All @@ -610,12 +623,66 @@ static void setup(
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger,
"dev.flutter.pigeon.CookieManagerHostApi.clearCookies",
"dev.flutter.pigeon.CookieManagerHostApi.attachInstance",
getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
Number instanceIdentifierArg = (Number) args.get(0);
try {
api.attachInstance(
(instanceIdentifierArg == null) ? null : instanceIdentifierArg.longValue());
wrapped.add(0, null);
} catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.CookieManagerHostApi.setCookie", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
Number identifierArg = (Number) args.get(0);
String urlArg = (String) args.get(1);
String valueArg = (String) args.get(2);
try {
api.setCookie(
(identifierArg == null) ? null : identifierArg.longValue(), urlArg, valueArg);
wrapped.add(0, null);
} catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger,
"dev.flutter.pigeon.CookieManagerHostApi.removeAllCookies",
getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
Number identifierArg = (Number) args.get(0);
Result<Boolean> resultCallback =
new Result<Boolean>() {
public void success(Boolean result) {
Expand All @@ -629,7 +696,8 @@ public void error(Throwable error) {
}
};

api.clearCookies(resultCallback);
api.removeAllCookies(
(identifierArg == null) ? null : identifierArg.longValue(), resultCallback);
});
} else {
channel.setMessageHandler(null);
Expand All @@ -638,16 +706,22 @@ public void error(Throwable error) {
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.CookieManagerHostApi.setCookie", getCodec());
binaryMessenger,
"dev.flutter.pigeon.CookieManagerHostApi.setAcceptThirdPartyCookies",
getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
String urlArg = (String) args.get(0);
String valueArg = (String) args.get(1);
Number identifierArg = (Number) args.get(0);
Number webViewIdentifierArg = (Number) args.get(1);
Boolean acceptArg = (Boolean) args.get(2);
try {
api.setCookie(urlArg, valueArg);
api.setAcceptThirdPartyCookies(
(identifierArg == null) ? null : identifierArg.longValue(),
(webViewIdentifierArg == null) ? null : webViewIdentifierArg.longValue(),
acceptArg);
wrapped.add(0, null);
} catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ private void setUp(
instanceManager, new WebSettingsHostApiImpl.WebSettingsCreator()));
FlutterAssetManagerHostApi.setup(
binaryMessenger, new FlutterAssetManagerHostApiImpl(flutterAssetManager));
CookieManagerHostApi.setup(binaryMessenger, new CookieManagerHostApiImpl());
CookieManagerHostApi.setup(
binaryMessenger, new CookieManagerHostApiImpl(binaryMessenger, instanceManager));
WebStorageHostApi.setup(
binaryMessenger,
new WebStorageHostApiImpl(instanceManager, new WebStorageHostApiImpl.WebStorageCreator()));
Expand Down

This file was deleted.

Loading