Skip to content
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

NullReferenceException throws on Windows when setting Cookies on .NET MAUI WebView #24846

Open
wants to merge 33 commits into
base: main
Choose a base branch
from

Conversation

NanthiniMahalingam
Copy link
Contributor

@NanthiniMahalingam NanthiniMahalingam commented Sep 20, 2024

Root Cause

While setting a cookie in MAUI WebView, CoreWebView2 is null before the CoreWebView2Initialized event triggered.

Description of Change

Added the null condition for CoreWebView2 property while sync platform cookie method.

Issues Fixed

Fixes #18452

Validated the behaviour in the following platforms

  • Android
  • Windows
  • iOS
  • Mac

Output images
Before changes
image

After changes
image

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Sep 20, 2024
@jfversluis
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@rmarinho
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@rmarinho
Copy link
Member

/rebase

@rmarinho
Copy link
Member

rmarinho commented Sep 27, 2024

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@rmarinho rmarinho modified the milestones: .NET 9 Planning, .NET 8 SR9 Sep 27, 2024
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen PureWeen modified the milestones: .NET 8 SR9, .NET 9 SR1 Oct 1, 2024
@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@NanthiniMahalingam
Copy link
Contributor Author

I've reran the CI but I think this one might be causing some issues

UI tests are failing

image

and device tests are both crashing https://dev.azure.com/xamarin/public/_build/results?buildId=125725&view=results

Hi @PureWeen ,
We've checked and resolved the failed cases with the updated fix. The above test cases pass successfully. Could you please verify them once?

@jsuarezruiz
Copy link
Contributor

jsuarezruiz commented Oct 28, 2024

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

jfversluis
jfversluis previously approved these changes Nov 1, 2024
@jfversluis jfversluis self-assigned this Nov 1, 2024
@@ -366,6 +371,10 @@ void OnCoreWebView2Initialized(WebView2 sender, CoreWebView2InitializedEventArgs
if (Handler is WebViewHandler handler)
{
sender.UpdateUserAgent(handler.VirtualView);
if (handler.PlatformView.Source is not null)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could simplify the code using directly sender instead handler.PlatformView.
For example:

if(sender.Source is not null)

Copy link
Contributor Author

@NanthiniMahalingam NanthiniMahalingam Nov 5, 2024

Choose a reason for hiding this comment

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

Could simplify the code using directly sender instead handler.PlatformView. For example:

if(sender.Source is not null)

Hi @jsuarezruiz,
We have simplified the source null condition based on your suggestion.

Label label = new Label();
label.AutomationId = "Label";

const string url = "https://httpbin.org/#/Cookies/get_cookies";
Copy link
Member

Choose a reason for hiding this comment

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

Can we use a msft or dotnet url here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we use a msft or dotnet url here?
Hi @PureWeen ,
We have used the Microsoft Learn documentation URL: https://learn.microsoft.com/en-us/dotnet/.

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@@ -366,6 +371,10 @@ void OnCoreWebView2Initialized(WebView2 sender, CoreWebView2InitializedEventArgs
if (Handler is WebViewHandler handler)
{
sender.UpdateUserAgent(handler.VirtualView);
if (sender.Source is not null)
{
handler.SyncPlatformCookies(handler.PlatformView.Source.ToString()).FireAndForget();
Copy link
Contributor

Choose a reason for hiding this comment

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

I would expect here

Suggested change
handler.SyncPlatformCookies(handler.PlatformView.Source.ToString()).FireAndForget();
handler.SyncPlatformCookies(sender.Source.ToString()).FireAndForget();

or not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would expect here

or not?
Hi @MartyIX ,

We have also updated the method for retrieving the SyncPlatformCookies source url.

@PureWeen PureWeen modified the milestones: .NET 9 SR1, .NET 9 SR2 Nov 5, 2024
@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Changes Requested
Development

Successfully merging this pull request may close these issues.

[regression/8.0.0-rc.2.9373] Setting Cookies on .NET MAUI WebView on Windows throws NullReferenceException
6 participants