Skip to content

[Problem/Bug]: StateChanged handler never called #5638

Description

@jib-at-ucn

What happened?

When watching downloads (add_DownloadStarting) the state never changes and stay forever "in progress". My code was running perfectly well with v144, but doesn't run anymore with 147, 148 and 150.

Edit : it's still working perfectly when downloading while inside the page (i.e. no window.open or taget="_blank"_). I don't even need to check state, juste configuring the listener and waiting for completness is enough.

Importance

Important. My app's user experience is significantly compromised.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

150.0.4078.48

SDK Version

1.0.2792.45

Framework

Win32

Operating System

Windows 11

OS Version

26100.8655

Repro steps

Add a download listener and start downloading a file (only tried a small one).

Microsoft::WRL::ComPtr<ICoreWebView2_4> coreWebView2_4;
if (SUCCEEDED(m_mainWebView.As(&coreWebView2_4))) {
    hRes = coreWebView2_4->add_DownloadStarting(Callback<ICoreWebView2DownloadStartingEventHandler>(
        [this](ICoreWebView2* webview, ICoreWebView2DownloadStartingEventArgs* args) -> HRESULT
    {

        Microsoft::WRL::ComPtr<ICoreWebView2DownloadOperation> downloadOperation;
        if (SUCCEEDED(args->get_DownloadOperation(&downloadOperation))) {
                        
            HRESULT hRes = S_OK;
            EventRegistrationToken stateChangedToken;
            hRes = downloadOperation->add_StateChanged(Callback<ICoreWebView2StateChangedEventHandler>(
                [this, stateChangedToken, downloadOperation](ICoreWebView2DownloadOperation* downloadOperation, IUnknown* args) -> HRESULT {
                    COREWEBVIEW2_DOWNLOAD_STATE downloadState;
                    HRESULT hRes = (downloadOperation->get_State(&downloadState));
                    if (SUCCEEDED(hRes) && downloadState != COREWEBVIEW2_DOWNLOAD_STATE_IN_PROGRESS) {
                        downloadOperation->remove_StateChanged(stateChangedToken);
                        // do something ... NEVER GETS CALLED :'(
                    }
                    return S_OK;
                }
            ).Get(), &stateChangedToken);

            INT64 bytesReceived, totalBytes;
            downloadOperation->get_BytesReceived(&bytesReceived);
            downloadOperation->get_TotalBytesToReceive(&totalBytes);

            // HERE !!!
            // bytesReceived == totalBytes but downloadState is still "in progress"
            // and stateChanged listener never gets called afterwards
            // (even if I keep a reference to downloadOperation into a member variable)

            COREWEBVIEW2_DOWNLOAD_STATE downloadState;
            hRes = (downloadOperation->get_State(&downloadState));
            if ((SUCCEEDED(hRes) && downloadState != COREWEBVIEW2_DOWNLOAD_STATE_IN_PROGRESS)) {                            
                downloadOperation->remove_StateChanged(stateChangedToken);
                args->put_Handled(TRUE);
                // do something ...
            }
        }

        return S_OK;
    }).Get(), &m_downloadStartingToken);

    RETURN_IF_FAILED(hRes);
}

Http request :
Image

Repros in Edge Browser

No, issue does not reproduce in the corresponding Edge version

Regression

Regression in newer Runtime

Last working version (if regression)

144

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingregressionSomething used to work but doesn't anymore

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions