Bug 209676 - [Cocoa] Sleep disabling should be performed in the UI process
Summary: [Cocoa] Sleep disabling should be performed in the UI process
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: Per Arne Vollan
URL:
Keywords: InRadar
Depends on:
Blocks: 209981
  Show dependency treegraph
 
Reported: 2020-03-27 12:21 PDT by Per Arne Vollan
Modified: 2020-04-03 12:33 PDT (History)
14 users (show)

See Also:


Attachments
Patch (39.52 KB, patch)
2020-03-27 14:03 PDT, Per Arne Vollan
no flags Details | Formatted Diff | Diff
Patch (39.22 KB, patch)
2020-03-27 14:37 PDT, Per Arne Vollan
no flags Details | Formatted Diff | Diff
Patch (39.17 KB, patch)
2020-03-27 14:56 PDT, Per Arne Vollan
no flags Details | Formatted Diff | Diff
Patch (45.41 KB, patch)
2020-03-28 08:30 PDT, Per Arne Vollan
no flags Details | Formatted Diff | Diff
Patch (46.69 KB, patch)
2020-03-28 09:00 PDT, Per Arne Vollan
no flags Details | Formatted Diff | Diff
Patch (47.80 KB, patch)
2020-03-28 09:29 PDT, Per Arne Vollan
no flags Details | Formatted Diff | Diff
Patch (47.88 KB, patch)
2020-03-28 09:38 PDT, Per Arne Vollan
no flags Details | Formatted Diff | Diff
Patch (47.88 KB, patch)
2020-03-28 09:48 PDT, Per Arne Vollan
darin: review+
Details | Formatted Diff | Diff
Patch (48.00 KB, patch)
2020-03-30 07:15 PDT, Per Arne Vollan
no flags Details | Formatted Diff | Diff
Patch (1.02 KB, patch)
2020-03-30 11:35 PDT, Per Arne Vollan
no flags Details | Formatted Diff | Diff
Patch (1.04 KB, patch)
2020-03-30 11:40 PDT, Per Arne Vollan
no flags Details | Formatted Diff | Diff
Patch (1.46 KB, patch)
2020-03-30 11:46 PDT, Per Arne Vollan
no flags Details | Formatted Diff | Diff
Patch (1.05 KB, patch)
2020-03-30 12:18 PDT, Per Arne Vollan
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Per Arne Vollan 2020-03-27 12:21:50 PDT
Since sleep disabling is causing communication with the power management service, it should be performed in the UI process.
Comment 1 Per Arne Vollan 2020-03-27 14:03:22 PDT
Created attachment 394756 [details]
Patch
Comment 2 Per Arne Vollan 2020-03-27 14:37:18 PDT
Created attachment 394758 [details]
Patch
Comment 3 Per Arne Vollan 2020-03-27 14:56:34 PDT
Created attachment 394762 [details]
Patch
Comment 4 Darin Adler 2020-03-27 15:09:39 PDT
Comment on attachment 394762 [details]
Patch

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

Started reviewing. Didn’t finish yet, but some initial comments.

> Source/WebCore/platform/SleepDisabler.cpp:39
> +void setSleepDisablerClient(std::unique_ptr<SleepDisablerClient>&& client)

It seems puzzling that the function body is in SleepDisabler.cpp but the function is declared in SleepDisablerClient.h. That’s not great long term for future code maintainability even though it might be handy for sharing a file-local sleepDisablerClient() function.

> Source/WebCore/platform/SleepDisabler.cpp:42
> +    auto& c = sleepDisablerClient();
> +    c = WTFMove(client);

Why the local variable? Why name it "c"?

    sleepDisablerClient() = WTFMove(client);

> Source/WebCore/platform/SleepDisabler.cpp:54
> +        sleepDisablerClient()->didCreateSleepDisabler(reinterpret_cast<uintptr_t>(this), reason, type == Type::Display);

This is not a good pattern. We do not want to expose pointers across process boundaries. Instead we should use the normal ObjectIdentifier pattern we use elsewhere.

> Source/WebCore/platform/SleepDisabler.h:32
> +class SleepDisabler : public PAL::SleepDisabler {

I don’t understand why this class both derives from PAL::SleepDisabler, and also stores a pointer to a PAL::SleepDisabler. Doesn’t make logical sense to me.

> Source/WebCore/platform/SleepDisabler.h:34
> +    WEBCORE_EXPORT static std::unique_ptr<SleepDisabler> create(const char*, PAL::SleepDisabler::Type);

This design pattern is not needed for unique_ptr. Instead we should provide a public constructor; the caller should use makeUnique<SleepDisabler>.

This pattern is for classes where the constructor can’t do all the necessary work, such as classes that need an adoptRef to safely make a Ref<> or have some work that has to be done with function calls after the constructor is called.

> Source/WebCore/platform/SleepDisablerClient.h:37
> +    virtual ~SleepDisablerClient() { };
> +    virtual void didCreateSleepDisabler(uintptr_t, const String&, bool) { };
> +    virtual void didDestroySleepDisabler(uintptr_t) { };

No semicolons needed.

Could didCreate/Destroy be pure virtual functions instead of function sixth empty bodies?

Need to document what the arguments are with argument names.

> Source/WebCore/testing/Internals.h:968
> +    uint64_t createSleepDisabler(const String& reason, bool display);
> +    bool destroySleepDisabler(uint64_t identifier);

Seems like a really peculiar pattern to expose pointers through the internals object. This seems to be the first time we have ever done this. Why not just generate incrementing identifiers instead of using the pointer? For one thing, they could then just be 32-bit integers instead of 64-bit ones.
Comment 5 Per Arne Vollan 2020-03-28 08:30:23 PDT
Created attachment 394817 [details]
Patch
Comment 6 Per Arne Vollan 2020-03-28 08:31:16 PDT
(In reply to Darin Adler from comment #4)
> Comment on attachment 394762 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=394762&action=review
> 
> Started reviewing. Didn’t finish yet, but some initial comments.
> 
> > Source/WebCore/platform/SleepDisabler.cpp:39
> > +void setSleepDisablerClient(std::unique_ptr<SleepDisablerClient>&& client)
> 
> It seems puzzling that the function body is in SleepDisabler.cpp but the
> function is declared in SleepDisablerClient.h. That’s not great long term
> for future code maintainability even though it might be handy for sharing a
> file-local sleepDisablerClient() function.
> 
> > Source/WebCore/platform/SleepDisabler.cpp:42
> > +    auto& c = sleepDisablerClient();
> > +    c = WTFMove(client);
> 
> Why the local variable? Why name it "c"?
> 
>     sleepDisablerClient() = WTFMove(client);
> 
> > Source/WebCore/platform/SleepDisabler.cpp:54
> > +        sleepDisablerClient()->didCreateSleepDisabler(reinterpret_cast<uintptr_t>(this), reason, type == Type::Display);
> 
> This is not a good pattern. We do not want to expose pointers across process
> boundaries. Instead we should use the normal ObjectIdentifier pattern we use
> elsewhere.
> 
> > Source/WebCore/platform/SleepDisabler.h:32
> > +class SleepDisabler : public PAL::SleepDisabler {
> 
> I don’t understand why this class both derives from PAL::SleepDisabler, and
> also stores a pointer to a PAL::SleepDisabler. Doesn’t make logical sense to
> me.
> 
> > Source/WebCore/platform/SleepDisabler.h:34
> > +    WEBCORE_EXPORT static std::unique_ptr<SleepDisabler> create(const char*, PAL::SleepDisabler::Type);
> 
> This design pattern is not needed for unique_ptr. Instead we should provide
> a public constructor; the caller should use makeUnique<SleepDisabler>.
> 
> This pattern is for classes where the constructor can’t do all the necessary
> work, such as classes that need an adoptRef to safely make a Ref<> or have
> some work that has to be done with function calls after the constructor is
> called.
> 
> > Source/WebCore/platform/SleepDisablerClient.h:37
> > +    virtual ~SleepDisablerClient() { };
> > +    virtual void didCreateSleepDisabler(uintptr_t, const String&, bool) { };
> > +    virtual void didDestroySleepDisabler(uintptr_t) { };
> 
> No semicolons needed.
> 
> Could didCreate/Destroy be pure virtual functions instead of function sixth
> empty bodies?
> 
> Need to document what the arguments are with argument names.
> 
> > Source/WebCore/testing/Internals.h:968
> > +    uint64_t createSleepDisabler(const String& reason, bool display);
> > +    bool destroySleepDisabler(uint64_t identifier);
> 
> Seems like a really peculiar pattern to expose pointers through the
> internals object. This seems to be the first time we have ever done this.
> Why not just generate incrementing identifiers instead of using the pointer?
> For one thing, they could then just be 32-bit integers instead of 64-bit
> ones.

Thanks for reviewing! The latest patch should address all of the issues above.
Comment 7 Per Arne Vollan 2020-03-28 09:00:11 PDT
Created attachment 394818 [details]
Patch
Comment 8 Per Arne Vollan 2020-03-28 09:29:13 PDT
Created attachment 394822 [details]
Patch
Comment 9 Per Arne Vollan 2020-03-28 09:38:40 PDT
Created attachment 394823 [details]
Patch
Comment 10 Per Arne Vollan 2020-03-28 09:48:53 PDT
Created attachment 394824 [details]
Patch
Comment 11 Darin Adler 2020-03-28 16:49:09 PDT
Comment on attachment 394824 [details]
Patch

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

Looks good

> Source/WebCore/platform/SleepDisablerClient.h:32
> +#include "SleepDisablerIdentifier.h"
> +
> +#include <pal/system/SleepDisabler.h>
> +#include <wtf/ObjectIdentifier.h>
> +#include <wtf/text/WTFString.h>

Seems like this is too many includes. <wtf/Forward.h> should be all we need for a const String& argument. I don’t see any reason we’d need the ObjectIdentifer.h or SleepDisabler.h include. Might need to include <memory> so we can use std::unique_ptr.

Also, headers should have one paragraph of includes, not two.

> Source/WebCore/testing/Internals.cpp:5570
> +    static unsigned identifier = 0;

This is a pretty good name. I also like the name "lastUsedIdentifier" for globals like this one.

> Source/WebKit/UIProcess/WebProcessProxy.cpp:1797
> +    return m_sleepDisablers.size() > 0;

This should use !isEmpty() instead of size() > 0.

> Source/WebKit/UIProcess/WebProcessProxy.h:382
> +    bool hasSleepDisabler();

This should be const.

> Source/WebKit/UIProcess/API/Cocoa/WKWebViewTesting.mm:225
> +    return _page ? _page->process().hasSleepDisabler() : false;

This can use &&, which I think is clearer than ? : false.

> Source/WebKit/WebProcess/GPU/media/RemoteLegacyCDM.cpp:33
> +#include "GPUProcessConnection.h"
> +
>  #include "RemoteLegacyCDMFactory.h"

New include should not be in a separate paragraph.
Comment 12 Darin Adler 2020-03-28 16:50:27 PDT
Comment on attachment 394824 [details]
Patch

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

> Source/WebKit/UIProcess/WebProcessProxy.cpp:1788
> +void WebProcessProxy::didCreateSleepDisabler(SleepDisablerIdentifier identifier, const String& reason, bool display)
> +{
> +    auto sleepDisabler = makeUnique<WebCore::SleepDisabler>(reason.utf8().data(), display ? PAL::SleepDisabler::Type::Display : PAL::SleepDisabler::Type::System);
> +    m_sleepDisablers.add(identifier, WTFMove(sleepDisabler));
> +}

Oh, this needs a MESSAGE_CHECK that the identifier is a good one, I think.

> Source/WebKit/UIProcess/WebProcessProxy.cpp:1793
> +void WebProcessProxy::didDestroySleepDisabler(SleepDisablerIdentifier identifier)
> +{
> +    m_sleepDisablers.remove(identifier);
> +}

Ditto.
Comment 13 Per Arne Vollan 2020-03-30 07:15:41 PDT
Created attachment 394914 [details]
Patch
Comment 14 Per Arne Vollan 2020-03-30 07:16:53 PDT
(In reply to Darin Adler from comment #12)
> Comment on attachment 394824 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=394824&action=review
> 
> > Source/WebKit/UIProcess/WebProcessProxy.cpp:1788
> > +void WebProcessProxy::didCreateSleepDisabler(SleepDisablerIdentifier identifier, const String& reason, bool display)
> > +{
> > +    auto sleepDisabler = makeUnique<WebCore::SleepDisabler>(reason.utf8().data(), display ? PAL::SleepDisabler::Type::Display : PAL::SleepDisabler::Type::System);
> > +    m_sleepDisablers.add(identifier, WTFMove(sleepDisabler));
> > +}
> 
> Oh, this needs a MESSAGE_CHECK that the identifier is a good one, I think.
> 
> > Source/WebKit/UIProcess/WebProcessProxy.cpp:1793
> > +void WebProcessProxy::didDestroySleepDisabler(SleepDisablerIdentifier identifier)
> > +{
> > +    m_sleepDisablers.remove(identifier);
> > +}
> 
> Ditto.

Fixed.
Comment 15 Per Arne Vollan 2020-03-30 07:18:12 PDT
(In reply to Darin Adler from comment #11)
> Comment on attachment 394824 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=394824&action=review
> 
> Looks good
> 
> > Source/WebCore/platform/SleepDisablerClient.h:32
> > +#include "SleepDisablerIdentifier.h"
> > +
> > +#include <pal/system/SleepDisabler.h>
> > +#include <wtf/ObjectIdentifier.h>
> > +#include <wtf/text/WTFString.h>
> 
> Seems like this is too many includes. <wtf/Forward.h> should be all we need
> for a const String& argument. I don’t see any reason we’d need the
> ObjectIdentifer.h or SleepDisabler.h include. Might need to include <memory>
> so we can use std::unique_ptr.
> 
> Also, headers should have one paragraph of includes, not two.
> 

Fixed.

> > Source/WebCore/testing/Internals.cpp:5570
> > +    static unsigned identifier = 0;
> 
> This is a pretty good name. I also like the name "lastUsedIdentifier" for
> globals like this one.
> 

Fixed.

> > Source/WebKit/UIProcess/WebProcessProxy.cpp:1797
> > +    return m_sleepDisablers.size() > 0;
> 
> This should use !isEmpty() instead of size() > 0.
> 

Done.

> > Source/WebKit/UIProcess/WebProcessProxy.h:382
> > +    bool hasSleepDisabler();
> 
> This should be const.
> 

Fixed.

> > Source/WebKit/UIProcess/API/Cocoa/WKWebViewTesting.mm:225
> > +    return _page ? _page->process().hasSleepDisabler() : false;
> 
> This can use &&, which I think is clearer than ? : false.
> 

Fixed.

> > Source/WebKit/WebProcess/GPU/media/RemoteLegacyCDM.cpp:33
> > +#include "GPUProcessConnection.h"
> > +
> >  #include "RemoteLegacyCDMFactory.h"
> 
> New include should not be in a separate paragraph.

Done.

Thanks for reviewing!
Comment 16 EWS 2020-03-30 07:48:44 PDT
Committed r259200: <https://trac.webkit.org/changeset/259200>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 394914 [details].
Comment 17 Radar WebKit Bug Importer 2020-03-30 07:49:11 PDT
<rdar://problem/61055348>
Comment 18 Darin Adler 2020-03-30 10:29:30 PDT
Comment on attachment 394914 [details]
Patch

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

> Source/WebCore/platform/SleepDisabler.h:31
> +#include <pal/system/SleepDisabler.h>
> +#include <wtf/ObjectIdentifier.h>

Still don’t think these includes are needed.
Comment 19 Per Arne Vollan 2020-03-30 11:35:20 PDT
Reopening to attach new patch.
Comment 20 Per Arne Vollan 2020-03-30 11:35:22 PDT
Created attachment 394938 [details]
Patch
Comment 21 Per Arne Vollan 2020-03-30 11:40:50 PDT
Reopening to attach new patch.
Comment 22 Per Arne Vollan 2020-03-30 11:40:52 PDT
Created attachment 394939 [details]
Patch
Comment 23 Per Arne Vollan 2020-03-30 11:46:29 PDT
Created attachment 394940 [details]
Patch
Comment 24 Per Arne Vollan 2020-03-30 12:18:19 PDT
Created attachment 394946 [details]
Patch
Comment 25 Per Arne Vollan 2020-03-30 12:39:35 PDT
(In reply to Darin Adler from comment #18)
> Comment on attachment 394914 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=394914&action=review
> 
> > Source/WebCore/platform/SleepDisabler.h:31
> > +#include <pal/system/SleepDisabler.h>
> > +#include <wtf/ObjectIdentifier.h>
> 
> Still don’t think these includes are needed.

Thanks! Removed <wtf/ObjectIdentifier.h>, but kept <pal/system/SleepDisabler.h>, since this was needed for the type argument (PAL::SleepDisabler::Type) in the WebCore::SleepDisabler constructor.
Comment 26 Per Arne Vollan 2020-03-30 12:58:23 PDT
Committed r259219: <https://trac.webkit.org/changeset/259219/webkit>