[webview_flutter_platform_interface] Add support for getting cookie#11037
[webview_flutter_platform_interface] Add support for getting cookie#11037khaled-0 wants to merge 2 commits intoflutter:mainfrom
Conversation
|
It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging. If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group. |
There was a problem hiding this comment.
Code Review
This pull request introduces support for retrieving cookies by adding a getCookies method to the PlatformWebViewCookieManager interface. The changes are well-contained within the platform interface package, including necessary updates to pubspec.yaml and CHANGELOG.md. My review focuses on improving documentation clarity and ensuring test coverage for the new functionality, in line with repository guidelines.
| Future<List<WebViewCookie>> getCookies(Uri url) { | ||
| throw UnimplementedError( | ||
| 'getCookies is not implemented on the current platform', | ||
| ); | ||
| } |
There was a problem hiding this comment.
The repository's contribution guide requires that code changes are tested. Please add a unit test for the new getCookies method to verify that its default implementation throws an UnimplementedError.
References
- The style guide states that code should be tested. (link)
There was a problem hiding this comment.
I missed this in the original PR, but this is actually missing a test. Though it looks like test/platform_webview_cookie_manager_test.dart was never created, so you will need to create a new test file. You can follow the pattern of test/platform_webview_controller_test.dart.
...view_flutter/webview_flutter_platform_interface/lib/src/platform_webview_cookie_manager.dart
Outdated
Show resolved
Hide resolved
bparrishMines
left a comment
There was a problem hiding this comment.
Following up with the same comments from #10833 and a test request.
For the future, you should wait to get approval for the full PR of #10833 before creating the individual PRs. Often this can require getting approval from multiple contributors, so feel free to ask if reviewers are not clear about which part they are approving.
| ); | ||
| } | ||
|
|
||
| /// Gets a list of existing cookie for specified domain from all |
There was a problem hiding this comment.
nit:
| /// Gets a list of existing cookie for specified domain from all | |
| /// Returns a list of existing cookies for the specified domain from all |
| } | ||
|
|
||
| /// Gets a list of existing cookie for specified domain from all | ||
| /// [WebView] instances of the application. |
There was a problem hiding this comment.
Is this part true? iOS docs suggests that it only gets cookies of the associated webview: https://developer.apple.com/documentation/webkit/wkhttpcookiestore/getallcookies(_:)#Discussion
There was a problem hiding this comment.
Seems to be true
I ran a test (with Ai)
import SwiftUI
import UIKit
import WebKit
@main
struct testApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
// MARK: - WebView Representable
struct WebView: UIViewRepresentable {
let url: URL
let store: WKWebsiteDataStore
func makeUIView(context: Context) -> WKWebView {
let config = WKWebViewConfiguration()
config.websiteDataStore = store
let webView = WKWebView(frame: .zero, configuration: config)
webView.navigationDelegate = context.coordinator
webView.load(URLRequest(url: url))
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {}
func makeCoordinator() -> Coordinator {
Coordinator()
}
class Coordinator: NSObject, WKNavigationDelegate {}
}
// MARK: - ContentView
struct ContentView: View {
let store = WKWebsiteDataStore.default()
var body: some View {
VStack {
WebView(
url: URL(
string: "https://httpbin.org/cookies/set?webview1=value1"
)!,
store: store
)
.frame(height: 200)
.border(Color.blue)
WebView(
url: URL(
string: "https://httpbin.org/cookies/set?webview2=value2"
)!,
store: store
)
.frame(height: 200)
.border(Color.green)
Button("Print Cookies") {
store.httpCookieStore.getAllCookies { cookies in
print("🍪 Total cookies in shared store:", cookies.count)
for cookie in cookies {
print("Name:", cookie.name, "Value:", cookie.value)
}
}
}
.padding()
}
.padding()
}
}
class ViewController: UIViewController, WKNavigationDelegate {
var webView1: WKWebView!
var webView2: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Shared persistent store
let store = WKWebsiteDataStore.default()
// First WebView
let config1 = WKWebViewConfiguration()
config1.websiteDataStore = store
webView1 = WKWebView(
frame: CGRect(x: 0, y: 50, width: view.bounds.width, height: 200),
configuration: config1
)
webView1.navigationDelegate = self
view.addSubview(webView1)
// Second WebView
let config2 = WKWebViewConfiguration()
config2.websiteDataStore = store
webView2 = WKWebView(
frame: CGRect(x: 0, y: 300, width: view.bounds.width, height: 200),
configuration: config2
)
webView2.navigationDelegate = self
view.addSubview(webView2)
// Load pages that set cookies
if let url1 = URL(
string: "https://httpbin.org/cookies/set?webview1=value1"
) {
webView1.load(URLRequest(url: url1))
}
if let url2 = URL(
string: "https://httpbin.org/cookies/set?webview2=value2"
) {
webView2.load(URLRequest(url: url2))
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
let store = WKWebsiteDataStore.default().httpCookieStore
store.getAllCookies { cookies in
DispatchQueue.main.async {
print("🍪 Total cookies in shared store:", cookies.count)
for cookie in cookies {
print("Name:", cookie.name, "Value:", cookie.value)
}
}
}
}
}
🍪 Total cookies in shared store: 2
Name: webview1 Value: value1
Name: webview2 Value: value2
| Future<List<WebViewCookie>> getCookies(Uri url) { | ||
| throw UnimplementedError( | ||
| 'getCookies is not implemented on the current platform', | ||
| ); | ||
| } |
There was a problem hiding this comment.
I missed this in the original PR, but this is actually missing a test. Though it looks like test/platform_webview_cookie_manager_test.dart was never created, so you will need to create a new test file. You can follow the pattern of test/platform_webview_controller_test.dart.
Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.
#10833
Pre-Review Checklist
[shared_preferences]pubspec.yamlwith an appropriate new version according to the pub versioning philosophy, or I have commented below to indicate which version change exemption this PR falls under1.CHANGELOG.mdto add a description of the change, following repository CHANGELOG style, or I have commented below to indicate which CHANGELOG exemption this PR falls under1.///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2 ↩3