Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Conversation

jeroen-meijer
Copy link

@jeroen-meijer jeroen-meijer commented Jul 19, 2019

Description

This PR adds better support for cookies by adding getCookies and setCookies methods to the CookieManager.

Instead of a List<String>, these methods will use a List<Cookie> where the Cookie class comes from dart:io. This provides the user with easier cookie handling because the cookies from the WebView instance no longer have to be manually deserialised from Strings. Also, this provides better interop with Dart's native HttpClient.

Related Issues

This PR may help solve the following issues:
flutter/flutter#27795
flutter/flutter#27624
flutter/flutter#27597

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (flutter analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate a breaking change in CHANGELOG.md and increment major revision).
  • No, this is not a breaking change.

@amirh
Copy link
Contributor

amirh commented Jul 19, 2019

Thanks for the contribution!

I'm following the initial PR review policy, this PR isn't trivial to review so I'm labeling it with "backlog" and we will prioritize according to the priority of the associated issues.

@amirh amirh added the backlog label Jul 19, 2019
@joelbrostrom
Copy link

Status on this PR?
Need it :)

@amirh
Copy link
Contributor

amirh commented Aug 7, 2019

@joelbrostrom see the comment above - the issues will be prioritized as described in the wiki.

@zhuimengHCZ
Copy link

How to use it

@zhuimengHCZ
Copy link

现在更新版本里面没setcookie 呀,什么时候发布新版本

@jeroen-meijer
Copy link
Author

@joelbrostrom @18316603765
Don't forget you can already use this PR by adding webview_flutter to your projects by adding this to your pubspec.yaml:

  webview_flutter:
    git: 
      url: git://github.com/devangels/plugins.git
      ref: cookies
      path: packages/webview_flutter

This does mean that any updates for webview_flutter done after this PR was submitted aren't there, so use it with caution.

@Pekno
Copy link

Pekno commented Aug 23, 2019

Any news on it being added soon ? I can't seem to use the git path.

@Pekno
Copy link

Pekno commented Aug 23, 2019

I managed to add the package to my app, I needed to replace the 'git://github.com/devangels/plugins.git' to 'https://github.com/devangels/plugins.git' for it to work.
But now I can't seem to build my app anymore.

What went wrong:Execution failed for task ':webview_flutter:compileDebugJavaWithJavac'. Unable to find source java class: 'C:\Users\1604742\Documents\flutter\.pub-cache\hosted\pub.dartlang.org\webview_flutter-0.3.13\android\src\main\java\io\flutter\plugins\webviewflutter\DisplayListenerProxy.java' because it does not belong to any of the source dirs: '[C:\Users\1604742\AppData\Roaming\Pub\Cache\git\plugins-c73e8b7fd115fcdeaf512239905337017f525971\packages\webview_flutter\android\src\main\java, C:\Users\1604742\AppData\Roaming\Pub\Cache\git\plugins-c73e8b7fd115fcdeaf512239905337017f525971\packages\webview_flutter\android\src\debug\java, C:\Users\1604742\Documents\Projet\flutter_app\build\webview_flutter\generated\not_namespaced_r_class_sources\debug\generateDebugRFile\out, C:\Users\1604742\Documents\Projet\flutter_app\build\webview_flutter\generated\source\buildConfig\debug, C:\Users\1604742\Documents\Projet\flutter_app\build\webview_flutter\generated\source\aidl\debug, C:\Users\1604742\Documents\Projet\flutter_app\build\webview_flutter\generated\source\rs\debug]'

@ashbin
Copy link

ashbin commented Sep 3, 2019

@jeroen-meijer can you merge this please.

@twopai
Copy link

twopai commented Sep 3, 2019

most of flutter webview, their all not method for the setcookies ,but communicate with js by it ,so can you add the api directly!
and today , i do the setCookies method on my program by your CookieManager, but not work ,help me thanks !

2 similar comments
@twopai
Copy link

twopai commented Sep 3, 2019

most of flutter webview, their all not method for the setcookies ,but communicate with js by it ,so can you add the api directly!
and today , i do the setCookies method on my program by your CookieManager, but not work ,help me thanks !

@twopai
Copy link

twopai commented Sep 3, 2019

most of flutter webview, their all not method for the setcookies ,but communicate with js by it ,so can you add the api directly!
and today , i do the setCookies method on my program by your CookieManager, but not work ,help me thanks !

@jeroen-meijer
Copy link
Author

@ashbin I can't, I'm don't have the permissions.
@twopai Wait for this PR to land before reporting error in specific workflows.

@kwongfung
Copy link

Need it

Copy link
Contributor

@wwwdata wwwdata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only tested for iOS. I quickly looked at the Android code and also found that you were only setting name and value as well. So I guess other properties must be supported there as well.

Thank you very much for this PR. I hope this get's merged soon. In the meantime I will make my custom Cookie Getter/Setter based on this :)

Comment on lines 50 to 39
- (void)setCookies:(FlutterMethodCall *)call result:(FlutterResult)result API_AVAILABLE(ios(11.0)) {
NSArray<CookieDto *> *cookieDtos = [CookieDto manyFromDictionaries:[call arguments]];
for (CookieDto *cookieDto in cookieDtos) {
[cookieStore setCookie:[cookieDto toNSHTTPCookie]
completionHandler:^(){
}];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this out and found several issues:

  • the result handler will never be called => your async dart code is stuck, same for clearCookies
  • the cookie data only considers name and value. So this is not going to work at all. In order for cookies to work we also need to consider other properties like the domain. But after adding the rest of the fields, and implementing some stupid counter code, I made it work.

Stupid counter code that calls result in the end example:

- (void)setCookies:(FlutterMethodCall *)call result:(FlutterResult)result API_AVAILABLE(ios(11.0)) {
    NSArray<CookieDto *> *cookieDtos = [CookieDto manyFromDictionaries:[call arguments]];
    __block int counter = 0;
    for (CookieDto *cookieDto in cookieDtos) {
        [cookieStore setCookie:[cookieDto toNSHTTPCookie]
             completionHandler:^(){
            counter++;
            if (counter == cookieDtos.count) {
                result(nil);
            }
        }];
    }
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'll use the code you provided until I or someone else comes up with a better solution.

Comment on lines 69 to 72
- (NSHTTPCookie *)toNSHTTPCookie {
return [NSHTTPCookie cookieWithProperties:@{NSHTTPCookieName : name, NSHTTPCookieValue : value}];
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other properties like

  • NSHTTPCookieDomain
  • NSHTTPCookieSecure
  • NSHTTPCookiePath

are missing. After adding them, it worked for me

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Do you propose those fields should be added to the CookieDto (in Dart, Java and ObjC) as well then?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I would support all the fields that the Cookie class in Dart has.

@jeroen-meijer
Copy link
Author

jeroen-meijer commented Oct 22, 2019

Thanks, @wwwdata! I did the best I could at the time, but I really appreciate you stepping in and taking a look.

I'll take a look at the changes you requested.


Edit: Just to confirm, you propose to

  1. Change the CookieDto models to include domain, secure and path
  2. Add the counter to the completionHandler in iOS

Is that correct?

@googlebot
Copy link

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@googlebot
Copy link

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@wwwdata
Copy link
Contributor

wwwdata commented Nov 29, 2019

Good changes, I think sending it as a string makes the most sense for Android. I now also found out that the toString method when you construct the Cookie object in Android is buggy and my open PR does not work.

I have to check out your iOS part as well. I noticed that using this process pool was necessary to cookies sync across different web views. If you would also use that and everything works, I think we should push this PR through and close the other one from me again. You can check the code on my PR for how to do the process pool in iOS.

@jeroen-meijer
Copy link
Author

@googlebot I consent.

@aboutmydreams
Copy link

Error output from CocoaPods:

WARNING: CocoaPods requires your terminal to be using UTF-8 encoding.
Consider adding the following to ~/.profile:
export LANG=en_US.UTF-8

[!] Automatically assigning platform `iOS` with version `8.0` on target `Runner` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

Error running pod install
Error launching application on iPhone 11 Pro Max.
Exited (sigterm)

@dotcink
Copy link

dotcink commented Jan 15, 2020

Is this still in progress?
If not, I might submit another PR.

@wwwdata
Copy link
Contributor

wwwdata commented Jan 15, 2020

Is this still in progress?
If not, I might submit another PR.

what would be different in your PR? Did you also have a look at my open PR? It is a bid sad that this PR is now open for so long since cookie syncing is, in my opinion, a very important topic.

@dotcink
Copy link

dotcink commented Jan 16, 2020

Is this still in progress?
If not, I might submit another PR.

what would be different in your PR? Did you also have a look at my open PR? It is a bid sad that this PR is now open for so long since cookie syncing is, in my opinion, a very important topic.

Nothing special.
I wrote setCookie feature before searched this PR, but its checks seem having failed for more than a month, so I asked the progress.

@teejayhh
Copy link

teejayhh commented Mar 6, 2020

This is something we could really use, and in fact its a requirement. How can we make google aware that this is something needed @jeroen-meijer and @wwwdata

@Justwen
Copy link

Justwen commented May 20, 2020

Could this PR be available during my rest of my life ?

@wwwdata
Copy link
Contributor

wwwdata commented May 28, 2020

@Justwen maybe, maybe not, who knows!

@cklee75
Copy link

cklee75 commented Jun 8, 2020

This is important feature for webview_flutter plugin as the existing solution which uses Javascript eval document.cookie cannot retrieve secured/HttpOnly cookies, which is important for certain authentication solution.

@joaoarmando
Copy link

Any news about it?

@fryette
Copy link

fryette commented Jul 12, 2020

If someone needed a solution
https://pub.dev/packages/webview_cookie_manager

@rodruiz
Copy link

rodruiz commented Jul 28, 2020

If someone needed a solution
https://pub.dev/packages/webview_cookie_manager

Confirming this package works.

@jeroen-meijer
Copy link
Author

Though I also believe that cookie handling is important in Flutter, I currently don't have more time to spend on this PR. 😔

The changes are still available on the source branch, though, so if anybody wants to fork it, pick this up and continue working on it, be my guest.

In the meantime, the package proposed by @fryette seems to work, so I recommend people to use that instead.

Closing this. Keeping the branch open.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.