Title: [221760] trunk/Source/WebKit
Revision
221760
Author
[email protected]
Date
2017-09-07 14:57:18 -0700 (Thu, 07 Sep 2017)

Log Message

Allow modern decoding of enums and OptionSets
https://bugs.webkit.org/show_bug.cgi?id=176480

Reviewed by Andy Estes.

* Platform/IPC/ArgumentCoders.h:
(IPC::ArgumentCoder<OptionSet<T>>::decode):
* Platform/IPC/Decoder.h:
(IPC::Decoder::operator>>):
* Platform/IPC/Encoder.h:
* Shared/WebsitePolicies.h:
(WebKit::WebsitePolicies::encode const):
(WebKit::WebsitePolicies::decode):

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (221759 => 221760)


--- trunk/Source/WebKit/ChangeLog	2017-09-07 21:52:01 UTC (rev 221759)
+++ trunk/Source/WebKit/ChangeLog	2017-09-07 21:57:18 UTC (rev 221760)
@@ -1,3 +1,19 @@
+2017-09-07  Alex Christensen  <[email protected]>
+
+        Allow modern decoding of enums and OptionSets
+        https://bugs.webkit.org/show_bug.cgi?id=176480
+
+        Reviewed by Andy Estes.
+
+        * Platform/IPC/ArgumentCoders.h:
+        (IPC::ArgumentCoder<OptionSet<T>>::decode):
+        * Platform/IPC/Decoder.h:
+        (IPC::Decoder::operator>>):
+        * Platform/IPC/Encoder.h:
+        * Shared/WebsitePolicies.h:
+        (WebKit::WebsitePolicies::encode const):
+        (WebKit::WebsitePolicies::decode):
+
 2017-09-07  Frederic Wang  <[email protected]>
 
          Move more code from ScrollingTreeScrollingNodeDelegateIOS to ScrollingTreeScrollingNodeDelegate

Modified: trunk/Source/WebKit/Platform/IPC/ArgumentCoders.h (221759 => 221760)


--- trunk/Source/WebKit/Platform/IPC/ArgumentCoders.h	2017-09-07 21:52:01 UTC (rev 221759)
+++ trunk/Source/WebKit/Platform/IPC/ArgumentCoders.h	2017-09-07 21:57:18 UTC (rev 221760)
@@ -69,6 +69,15 @@
         optionSet = OptionSet<T>::fromRaw(value);
         return true;
     }
+
+    static std::optional<OptionSet<T>> decode(Decoder& decoder)
+    {
+        std::optional<uint64_t> value;
+        decoder >> value;
+        if (!value)
+            return std::nullopt;
+        return OptionSet<T>::fromRaw(*value);
+    }
 };
 
 template<typename T> struct ArgumentCoder<std::optional<T>> {

Modified: trunk/Source/WebKit/Platform/IPC/Decoder.h (221759 => 221760)


--- trunk/Source/WebKit/Platform/IPC/Decoder.h	2017-09-07 21:52:01 UTC (rev 221759)
+++ trunk/Source/WebKit/Platform/IPC/Decoder.h	2017-09-07 21:57:18 UTC (rev 221760)
@@ -109,6 +109,16 @@
         return true;
     }
 
+    template<typename E, std::enable_if_t<std::is_enum<E>::value>* = nullptr>
+    Decoder& operator>>(std::optional<E>& optional)
+    {
+        std::optional<uint64_t> value;
+        *this >> value;
+        if (value && isValidEnum<E>(*value))
+            optional = static_cast<E>(*value);
+        return *this;
+    }
+
     template<typename T> bool decodeEnum(T& result)
     {
         static_assert(sizeof(T) <= 8, "Enum type T must not be larger than 64 bits!");

Modified: trunk/Source/WebKit/Platform/IPC/Encoder.h (221759 => 221760)


--- trunk/Source/WebKit/Platform/IPC/Encoder.h	2017-09-07 21:52:01 UTC (rev 221759)
+++ trunk/Source/WebKit/Platform/IPC/Encoder.h	2017-09-07 21:57:18 UTC (rev 221760)
@@ -65,14 +65,22 @@
         encode(static_cast<uint64_t>(t));
     }
 
-    template<typename T>
-    auto encode(T&& t) -> std::enable_if_t<!std::is_enum<typename std::remove_const_t<std::remove_reference_t<T>>>::value>
+    template<typename T, std::enable_if_t<!std::is_enum<typename std::remove_const_t<std::remove_reference_t<T>>>::value>* = nullptr>
+    void encode(T&& t)
     {
         ArgumentCoder<typename std::remove_const<typename std::remove_reference<T>::type>::type>::encode(*this, std::forward<T>(t));
     }
 
-    template<typename T> Encoder& operator<<(T&& t)
+    template<typename T, std::enable_if_t<std::is_enum<T>::value>* = nullptr>
+    Encoder& operator<<(T&& t)
     {
+        encode(static_cast<uint64_t>(t));
+        return *this;
+    }
+
+    template<typename T, std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
+    Encoder& operator<<(T&& t)
+    {
         encode(std::forward<T>(t));
         return *this;
     }

Modified: trunk/Source/WebKit/Shared/WebsitePolicies.h (221759 => 221760)


--- trunk/Source/WebKit/Shared/WebsitePolicies.h	2017-09-07 21:52:01 UTC (rev 221759)
+++ trunk/Source/WebKit/Shared/WebsitePolicies.h	2017-09-07 21:57:18 UTC (rev 221760)
@@ -25,6 +25,7 @@
 
 #pragma once
 
+#include <wtf/EnumTraits.h>
 #include <wtf/OptionSet.h>
 #include <wtf/Optional.h>
 
@@ -52,28 +53,53 @@
     template<class Decoder> static std::optional<WebsitePolicies> decode(Decoder&);
 };
 
+} // namespace WebKit
+
+namespace WTF {
+
+template<> struct EnumTraits<WebKit::WebsiteAutoplayPolicy> {
+    using values = EnumValues<
+        WebKit::WebsiteAutoplayPolicy,
+        WebKit::WebsiteAutoplayPolicy::Default,
+        WebKit::WebsiteAutoplayPolicy::Allow,
+        WebKit::WebsiteAutoplayPolicy::AllowWithoutSound,
+        WebKit::WebsiteAutoplayPolicy::Deny
+    >;
+};
+
+} // namespace WTF
+
+namespace WebKit {
+
 template<class Encoder> void WebsitePolicies::encode(Encoder& encoder) const
 {
     encoder << contentBlockersEnabled;
-    encoder.encodeEnum(autoplayPolicy);
+    encoder << autoplayPolicy;
     encoder << allowedAutoplayQuirks;
 }
 
 template<class Decoder> std::optional<WebsitePolicies> WebsitePolicies::decode(Decoder& decoder)
 {
-    bool contentBlockersEnabled;
-    if (!decoder.decode(contentBlockersEnabled))
+    std::optional<bool> contentBlockersEnabled;
+    decoder >> contentBlockersEnabled;
+    if (!contentBlockersEnabled)
         return std::nullopt;
     
-    WebsiteAutoplayPolicy autoplayPolicy;
-    if (!decoder.decodeEnum(autoplayPolicy))
+    std::optional<WebsiteAutoplayPolicy> autoplayPolicy;
+    decoder >> autoplayPolicy;
+    if (!autoplayPolicy)
         return std::nullopt;
 
-    OptionSet<WebsiteAutoplayQuirk> allowedAutoplayQuirks;
-    if (!decoder.decode(allowedAutoplayQuirks))
+    std::optional<OptionSet<WebsiteAutoplayQuirk>> allowedAutoplayQuirks;
+    decoder >> allowedAutoplayQuirks;
+    if (!allowedAutoplayQuirks)
         return std::nullopt;
 
-    return { { contentBlockersEnabled, allowedAutoplayQuirks, autoplayPolicy } };
+    return { {
+        WTFMove(*contentBlockersEnabled),
+        WTFMove(*allowedAutoplayQuirks),
+        WTFMove(*autoplayPolicy),
+    } };
 }
 
 } // namespace WebKit
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to