Bug 217228 - Multiple calls to suspend media playback for the same page may result in resuming media playback too soon
Summary: Multiple calls to suspend media playback for the same page may result in resu...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit Misc. (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Kate Cheney
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2020-10-02 08:59 PDT by Kate Cheney
Modified: 2020-10-02 16:16 PDT (History)
3 users (show)

See Also:


Attachments
Patch (9.10 KB, patch)
2020-10-02 09:20 PDT, Kate Cheney
no flags Details | Formatted Diff | Diff
Patch (9.50 KB, patch)
2020-10-02 13:58 PDT, Kate Cheney
no flags Details | Formatted Diff | Diff
Patch for landing (9.44 KB, patch)
2020-10-02 15:36 PDT, Kate Cheney
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Kate Cheney 2020-10-02 08:59:23 PDT
It’s possible that multiple calls to _suspendAllMediaPlayback() could occur in the same WKWebView with different expectations of when to resume. The current implementation will resume after one call to _resumeAllMediaPlayback(). Instead, we should make sure that all calls to _suspendAllMediaPlayback() have been resolved with _resumeAllMediaPlayback() before actually resuming media content. Adding a counter to track how many calls to suspend have been made and only resuming when the counter is at 0 is a potential solution.
Comment 1 Kate Cheney 2020-10-02 08:59:40 PDT
rdar://problem/69709346
Comment 2 Kate Cheney 2020-10-02 09:20:49 PDT
Created attachment 410330 [details]
Patch
Comment 3 Kate Cheney 2020-10-02 13:58:03 PDT
Created attachment 410366 [details]
Patch
Comment 4 Eric Carlson 2020-10-02 15:10:53 PDT
Comment on attachment 410366 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=410366&action=review

> Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:1897
> +    return (NSInteger *)(static_cast<long>(_page->suspendMediaPlaybackCounter()));

can you `return @(_page->suspendMediaPlaybackCounter())` ?

> Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h:345
> +- (NSInteger *)_suspendMediaPlaybackCounter WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));

This should probably be WKWebViewPrivateForTesting.h

> Source/WebKit/UIProcess/WebPageProxy.cpp:6045
> +    if (!m_mediaPlaybackIsSuspended || m_suspendMediaPlaybackCounter > 0)

if (!m_mediaPlaybackIsSuspended || m_suspendMediaPlaybackCounter)

> Tools/TestWebKitAPI/Tests/WebKitCocoa/StopSuspendResumeAllMedia.mm:115
> +        auto counter = [webView _suspendMediaPlaybackCounter];
> +        if (counter >= (NSInteger *)(static_cast<long>(2)))

if ([webView _suspendMediaPlaybackCounter] <= @(2))

> Tools/TestWebKitAPI/Tests/WebKitCocoa/StopSuspendResumeAllMedia.mm:123
> +        auto counter = [webView _suspendMediaPlaybackCounter];
> +        if (counter <= (NSInteger *)(static_cast<long>(1)))

if ([webView _suspendMediaPlaybackCounter] <= @(1))
Comment 5 Kate Cheney 2020-10-02 15:32:15 PDT
(In reply to Eric Carlson from comment #4)
> Comment on attachment 410366 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=410366&action=review
> 

Thanks for the review!

> > Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:1897
> > +    return (NSInteger *)(static_cast<long>(_page->suspendMediaPlaybackCounter()));
> 
> can you `return @(_page->suspendMediaPlaybackCounter())` ?
> 

Yep, will change.

> > Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h:345
> > +- (NSInteger *)_suspendMediaPlaybackCounter WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
> 
> This should probably be WKWebViewPrivateForTesting.h
> 

Cool, didn't know about this, I'll move it.

> > Source/WebKit/UIProcess/WebPageProxy.cpp:6045
> > +    if (!m_mediaPlaybackIsSuspended || m_suspendMediaPlaybackCounter > 0)
> 
> if (!m_mediaPlaybackIsSuspended || m_suspendMediaPlaybackCounter)
> 

Will fix.

> > Tools/TestWebKitAPI/Tests/WebKitCocoa/StopSuspendResumeAllMedia.mm:115
> > +        auto counter = [webView _suspendMediaPlaybackCounter];
> > +        if (counter >= (NSInteger *)(static_cast<long>(2)))
> 
> if ([webView _suspendMediaPlaybackCounter] <= @(2))
> 
> > Tools/TestWebKitAPI/Tests/WebKitCocoa/StopSuspendResumeAllMedia.mm:123
> > +        auto counter = [webView _suspendMediaPlaybackCounter];
> > +        if (counter <= (NSInteger *)(static_cast<long>(1)))
> 
> if ([webView _suspendMediaPlaybackCounter] <= @(1))

I got 'error: direct comparison of a numeric literal has undefined behavior' with this change, but I can change it to if ([webView _suspendMediaPlaybackCounter].intValue <= 1) to avoid the casting.
Comment 6 Kate Cheney 2020-10-02 15:36:19 PDT
Created attachment 410382 [details]
Patch for landing
Comment 7 EWS 2020-10-02 16:15:17 PDT
Committed r267899: <https://trac.webkit.org/changeset/267899>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 410382 [details].
Comment 8 Radar WebKit Bug Importer 2020-10-02 16:16:52 PDT
<rdar://problem/69901158>