RESOLVED FIXED209676
[Cocoa] Sleep disabling should be performed in the UI process
https://bugs.webkit.org/show_bug.cgi?id=209676
Summary [Cocoa] Sleep disabling should be performed in the UI process
Per Arne Vollan
Reported 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.
Attachments
Patch (39.52 KB, patch)
2020-03-27 14:03 PDT, Per Arne Vollan
no flags
Patch (39.22 KB, patch)
2020-03-27 14:37 PDT, Per Arne Vollan
no flags
Patch (39.17 KB, patch)
2020-03-27 14:56 PDT, Per Arne Vollan
no flags
Patch (45.41 KB, patch)
2020-03-28 08:30 PDT, Per Arne Vollan
no flags
Patch (46.69 KB, patch)
2020-03-28 09:00 PDT, Per Arne Vollan
no flags
Patch (47.80 KB, patch)
2020-03-28 09:29 PDT, Per Arne Vollan
no flags
Patch (47.88 KB, patch)
2020-03-28 09:38 PDT, Per Arne Vollan
no flags
Patch (47.88 KB, patch)
2020-03-28 09:48 PDT, Per Arne Vollan
darin: review+
Patch (48.00 KB, patch)
2020-03-30 07:15 PDT, Per Arne Vollan
no flags
Patch (1.02 KB, patch)
2020-03-30 11:35 PDT, Per Arne Vollan
no flags
Patch (1.04 KB, patch)
2020-03-30 11:40 PDT, Per Arne Vollan
no flags
Patch (1.46 KB, patch)
2020-03-30 11:46 PDT, Per Arne Vollan
no flags
Patch (1.05 KB, patch)
2020-03-30 12:18 PDT, Per Arne Vollan
no flags
Per Arne Vollan
Comment 1 2020-03-27 14:03:22 PDT
Per Arne Vollan
Comment 2 2020-03-27 14:37:18 PDT
Per Arne Vollan
Comment 3 2020-03-27 14:56:34 PDT
Darin Adler
Comment 4 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.
Per Arne Vollan
Comment 5 2020-03-28 08:30:23 PDT
Per Arne Vollan
Comment 6 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.
Per Arne Vollan
Comment 7 2020-03-28 09:00:11 PDT
Per Arne Vollan
Comment 8 2020-03-28 09:29:13 PDT
Per Arne Vollan
Comment 9 2020-03-28 09:38:40 PDT
Per Arne Vollan
Comment 10 2020-03-28 09:48:53 PDT
Darin Adler
Comment 11 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.
Darin Adler
Comment 12 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.
Per Arne Vollan
Comment 13 2020-03-30 07:15:41 PDT
Per Arne Vollan
Comment 14 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.
Per Arne Vollan
Comment 15 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!
EWS
Comment 16 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].
Radar WebKit Bug Importer
Comment 17 2020-03-30 07:49:11 PDT
Darin Adler
Comment 18 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.
Per Arne Vollan
Comment 19 2020-03-30 11:35:20 PDT
Reopening to attach new patch.
Per Arne Vollan
Comment 20 2020-03-30 11:35:22 PDT
Per Arne Vollan
Comment 21 2020-03-30 11:40:50 PDT
Reopening to attach new patch.
Per Arne Vollan
Comment 22 2020-03-30 11:40:52 PDT
Per Arne Vollan
Comment 23 2020-03-30 11:46:29 PDT
Per Arne Vollan
Comment 24 2020-03-30 12:18:19 PDT
Per Arne Vollan
Comment 25 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.
Per Arne Vollan
Comment 26 2020-03-30 12:58:23 PDT
Note You need to log in before you can comment on or make changes to this bug.