-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Blazor Hybrid CSS Hot Reload Fixes #9645
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#if WEBVIEW2_WINFORMS || WEBVIEW2_WPF | ||
|
||
using System.IO; | ||
|
||
namespace Microsoft.AspNetCore.Components.WebView.WebView2 | ||
{ | ||
internal class AutoCloseOnReadCompleteStream : Stream | ||
{ | ||
private readonly Stream _baseStream; | ||
|
||
public AutoCloseOnReadCompleteStream(Stream baseStream) | ||
{ | ||
_baseStream = baseStream; | ||
} | ||
|
||
public override bool CanRead => _baseStream.CanRead; | ||
|
||
public override bool CanSeek => _baseStream.CanSeek; | ||
|
||
public override bool CanWrite => _baseStream.CanWrite; | ||
|
||
public override long Length => _baseStream.Length; | ||
|
||
public override long Position { get => _baseStream.Position; set => _baseStream.Position = value; } | ||
|
||
public override void Flush() => _baseStream.Flush(); | ||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
var bytesRead = _baseStream.Read(buffer, offset, count); | ||
|
||
// Stream.Read only returns 0 when it has reached the end of stream | ||
// and no further bytes are expected. Otherwise it blocks until | ||
// one or more (and at most count) bytes can be read. | ||
if (bytesRead == 0) | ||
{ | ||
_baseStream.Close(); | ||
} | ||
Comment on lines
+35
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not think this check is correct. You might receive a Would it work if instead we compare the current position against the length of the stream? We should be able to leverage here the fact that we are dealing with files on disk. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm that's what I initially had, but it necessitated a bit more complexity to avoid accessing the properties of a closed out stream. I did this based on:
Based on this, I believe this should be fine as-is? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the empty buffer is still potentially problematic here, but that almost seems like an issue in the caller. Maybe we just throw an exception in this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you not keep track of whether you closed the underlying stream already? Would it also not throw before you get to this check in the call to Read if the internal stream was already closed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am somewhat fine with the additional info provided; those are the types of things that are great comments to add in the code to give context on why that might be Ok to do. Are there other APIs on the stream that a caller can call instead of just Read that might bypass the check? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
As far as I can tell this should be sufficient as it'll serve as the marker for when the end-of-stream is reached on read. I've done some basic testing locally and things are working as expected. cc/ @yildirimcagri this is a workaround we're implementing for MicrosoftEdge/WebView2Feedback#2513. Essentially wrapping the stream and closing it out when the read is completed. Wanted to get your thoughts on this approach and see if you had any concerns with how the CoreWebView2 interacts with this content stream. |
||
|
||
return bytesRead; | ||
} | ||
|
||
public override long Seek(long offset, SeekOrigin origin) => _baseStream.Seek(offset, origin); | ||
|
||
public override void SetLength(long value) => _baseStream.SetLength(value); | ||
|
||
public override void Write(byte[] buffer, int offset, int count) => _baseStream.Write(buffer, offset, count); | ||
} | ||
} | ||
|
||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is already in
main
: https://github.com/dotnet/maui/blob/main/src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs#L83-L88Not sure why GitHub is explicitly displaying it here given the effective 0 diff.