This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[webview_flutter] Add Android implementations for new cookie manager, to allow setting cookies directly and on webview creation. #4557
Merged
fluttergithubbot
merged 34 commits into
flutter:master
from
Baseflow:webview_flutter/set_cookies_android
Dec 7, 2021
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
9950ee5
Added cookie manager to interface.
BeMacized 4782a68
Added Android implementation for new CookieManager.
BeMacized 0577815
Added iOS implementation for new CookieManager.
BeMacized 2e3dba3
Update pubspec
BeMacized 4816d71
Revert accidental push of ios changes
BeMacized 34c680e
Update deprecation notice
BeMacized 44eca64
Merge branch 'master' into webview_flutter/set_cookies_platform
BeMacized e1f45b9
Merge branch 'webview_flutter/set_cookies_platform' into webview_flut…
BeMacized 0d0f281
Fix analysis issues
BeMacized 176b55c
Merge branch 'webview_flutter/set_cookies_platform' into webview_flut…
BeMacized 490da19
Enforce extending class for cookie manager platform interface
BeMacized 60b5945
Merge branch 'webview_flutter/set_cookies_platform' into webview_flut…
BeMacized 8eb7bf5
Update to match platform interface changes
BeMacized 6f9742a
Process PR feedback
BeMacized c7067c0
Process PR feedback
BeMacized e557cbc
Merge branch 'webview_flutter/set_cookies_platform' into webview_flut…
BeMacized 8f5bb77
Fix issue with WebViewCookieManager
BeMacized cdc302e
Add fix for platform implementation registration.
BeMacized 9f98faf
Fix build issue
BeMacized e75383f
Merge remote-tracking branch 'upstream/master' into webview_flutter/s…
BeMacized a0c5c76
Fix analysis errors (excl. pubspec)
BeMacized a4e663f
Format
BeMacized a6c93e6
Merge branch 'master' into webview_flutter/set_cookies_android
BeMacized d80dc54
Updated platform interface dependency
BeMacized b75ceda
Add missing license headers
BeMacized 5a9075d
Updated changelog and pubspec
BeMacized 3d5c13f
Revert automatic gradle updates
BeMacized 5d0408f
Implemented PR feedback
BeMacized 5bfcc7d
Merge remote-tracking branch 'upstream/master' into webview_flutter/s…
BeMacized b95d8cc
Fix mocks
BeMacized 6d79274
Merge branch 'master' into webview_flutter/set_cookies_android
BeMacized f201bc1
Revert "Merge branch 'master' into webview_flutter/set_cookies_android"
BeMacized 7b367f0
Merge branch 'master' into webview_flutter/set_cookies_android
BeMacized 3bfbe9e
Update changelog, pubspec version and mocks.
BeMacized File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
packages/webview_flutter/webview_flutter_android/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...oid/android/src/main/java/io/flutter/plugins/webviewflutter/CookieManagerHostApiImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // 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. | ||
|
|
||
| package io.flutter.plugins.webviewflutter; | ||
|
|
||
| import android.os.Build; | ||
| import android.webkit.CookieManager; | ||
|
|
||
| class CookieManagerHostApiImpl implements GeneratedAndroidWebView.CookieManagerHostApi { | ||
| @Override | ||
| public void clearCookies(GeneratedAndroidWebView.Result<Boolean> result) { | ||
| CookieManager cookieManager = CookieManager.getInstance(); | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | ||
| cookieManager.removeAllCookies(result::success); | ||
| } else { | ||
| final boolean hasCookies = cookieManager.hasCookies(); | ||
| if (hasCookies) { | ||
| cookieManager.removeAllCookie(); | ||
| } | ||
| result.success(hasCookies); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void setCookie(String url, String value) { | ||
| CookieManager.getInstance().setCookie(url, value); | ||
| } | ||
| } | ||
56 changes: 0 additions & 56 deletions
56
...android/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterCookieManager.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...android/src/test/java/io/flutter/plugins/webviewflutter/CookieManagerHostApiImplTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // 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. | ||
|
|
||
| package io.flutter.plugins.webviewflutter; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.doAnswer; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import android.os.Build; | ||
| import android.webkit.CookieManager; | ||
| import android.webkit.ValueCallback; | ||
| import io.flutter.plugins.webviewflutter.utils.TestUtils; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.mockito.MockedStatic; | ||
|
|
||
| public class CookieManagerHostApiImplTest { | ||
|
|
||
| private CookieManager cookieManager; | ||
| private MockedStatic<CookieManager> staticMockCookieManager; | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| staticMockCookieManager = mockStatic(CookieManager.class); | ||
| cookieManager = mock(CookieManager.class); | ||
| when(CookieManager.getInstance()).thenReturn(cookieManager); | ||
| when(cookieManager.hasCookies()).thenReturn(true); | ||
| doAnswer( | ||
| answer -> { | ||
| ((ValueCallback<Boolean>) answer.getArgument(0)).onReceiveValue(true); | ||
| return null; | ||
| }) | ||
| .when(cookieManager) | ||
| .removeAllCookies(any()); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| staticMockCookieManager.close(); | ||
| } | ||
|
|
||
| @Test | ||
| public void setCookieShouldCallSetCookie() { | ||
| // Setup | ||
| CookieManagerHostApiImpl impl = new CookieManagerHostApiImpl(); | ||
| // Run | ||
| impl.setCookie("flutter.dev", "foo=bar; path=/"); | ||
| // Verify | ||
| verify(cookieManager).setCookie("flutter.dev", "foo=bar; path=/"); | ||
| } | ||
|
|
||
| @Test | ||
| public void clearCookiesShouldCallRemoveAllCookiesOnAndroidLAbove() { | ||
| // Setup | ||
| TestUtils.setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.LOLLIPOP); | ||
| GeneratedAndroidWebView.Result<Boolean> result = mock(GeneratedAndroidWebView.Result.class); | ||
| CookieManagerHostApiImpl impl = new CookieManagerHostApiImpl(); | ||
| // Run | ||
| impl.clearCookies(result); | ||
| // Verify | ||
| verify(cookieManager).removeAllCookies(any()); | ||
| verify(result).success(true); | ||
| } | ||
|
|
||
| @Test | ||
| public void clearCookiesShouldCallRemoveAllCookieBelowAndroidL() { | ||
| // Setup | ||
| TestUtils.setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.KITKAT_WATCH); | ||
| GeneratedAndroidWebView.Result<Boolean> result = mock(GeneratedAndroidWebView.Result.class); | ||
| CookieManagerHostApiImpl impl = new CookieManagerHostApiImpl(); | ||
| // Run | ||
| impl.clearCookies(result); | ||
| // Verify | ||
| verify(cookieManager).removeAllCookie(); | ||
| verify(result).success(true); | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
...tter_android/android/src/test/java/io/flutter/plugins/webviewflutter/utils/TestUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // 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. | ||
|
|
||
| package io.flutter.plugins.webviewflutter.utils; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.Modifier; | ||
| import org.junit.Assert; | ||
|
|
||
| public class TestUtils { | ||
| public static <T> void setFinalStatic(Class<T> classToModify, String fieldName, Object newValue) { | ||
| try { | ||
| Field field = classToModify.getField(fieldName); | ||
| field.setAccessible(true); | ||
|
|
||
| Field modifiersField = Field.class.getDeclaredField("modifiers"); | ||
| modifiersField.setAccessible(true); | ||
| modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); | ||
|
|
||
| field.set(null, newValue); | ||
| } catch (Exception e) { | ||
| Assert.fail("Unable to mock static field: " + fieldName); | ||
| } | ||
| } | ||
|
|
||
| public static <T> void setPrivateField(T instance, String fieldName, Object newValue) { | ||
| try { | ||
| Field field = instance.getClass().getDeclaredField(fieldName); | ||
| field.setAccessible(true); | ||
| field.set(instance, newValue); | ||
| } catch (Exception e) { | ||
| Assert.fail("Unable to mock private field: " + fieldName); | ||
| } | ||
| } | ||
|
|
||
| public static <T> Object getPrivateField(T instance, String fieldName) { | ||
| try { | ||
| Field field = instance.getClass().getDeclaredField(fieldName); | ||
| field.setAccessible(true); | ||
| return field.get(instance); | ||
| } catch (Exception e) { | ||
| Assert.fail("Unable to mock private field: " + fieldName); | ||
| return null; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.