Allow modern decoding of std::optional<T>
authorachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 27 Sep 2017 23:50:21 +0000 (23:50 +0000)
committerachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 27 Sep 2017 23:50:21 +0000 (23:50 +0000)
https://bugs.webkit.org/show_bug.cgi?id=177519

Reviewed by Tim Horton.

Source/WebCore:

* platform/DragItem.h:
(WebCore::DragItem::decode):

Source/WebKit:

* Platform/IPC/ArgumentCoders.h:
(IPC::ArgumentCoder<std::optional<T>>::decode):
* Shared/Cocoa/WebCoreArgumentCodersCocoa.mm:
(IPC::ArgumentCoder<WebCore::PaymentAuthorizationResult>::decode):
(IPC::ArgumentCoder<WebCore::PaymentError>::decode):
(IPC::ArgumentCoder<WebCore::PaymentMethodUpdate>::decode):
(IPC::ArgumentCoder<WebCore::ShippingContactUpdate>::decode):
(IPC::ArgumentCoder<WebCore::ShippingMethodUpdate>::decode):
* Shared/WebCoreArgumentCoders.cpp:
(IPC::ArgumentCoder<IntPoint>::decode):
(IPC::ArgumentCoder<IntSize>::decode):
* Shared/WebCoreArgumentCoders.h:
* Shared/WebPageCreationParameters.cpp:
(WebKit::WebPageCreationParameters::decode):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@222584 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/DragItem.h
Source/WebKit/ChangeLog
Source/WebKit/Platform/IPC/ArgumentCoders.h
Source/WebKit/Shared/Cocoa/WebCoreArgumentCodersCocoa.mm
Source/WebKit/Shared/WebCoreArgumentCoders.cpp
Source/WebKit/Shared/WebCoreArgumentCoders.h
Source/WebKit/Shared/WebHitTestResultData.cpp
Source/WebKit/Shared/WebPageCreationParameters.cpp
Source/WebKit/Shared/ios/InteractionInformationAtPosition.mm
Source/WebKit/Shared/mac/WebHitTestResultData.mm

index 0de130b..c2aad3e 100644 (file)
@@ -1,3 +1,13 @@
+2017-09-27  Alex Christensen  <achristensen@webkit.org>
+
+        Allow modern decoding of std::optional<T>
+        https://bugs.webkit.org/show_bug.cgi?id=177519
+
+        Reviewed by Tim Horton.
+
+        * platform/DragItem.h:
+        (WebCore::DragItem::decode):
+
 2017-09-27  Myles C. Maxfield  <mmaxfield@apple.com>
 
         "Tag" codepoints require the complex text codepath
index b0faa03..25d4fd1 100644 (file)
@@ -92,10 +92,11 @@ bool DragItem::decode(Decoder& decoder, DragItem& result)
     if (!decoder.decode(hasIndicatorData))
         return false;
     if (hasIndicatorData) {
-        TextIndicatorData indicatorData;
-        if (!decoder.decode(indicatorData))
+        std::optional<TextIndicatorData> indicatorData;
+        decoder >> indicatorData;
+        if (!indicatorData)
             return false;
-        result.image.setIndicatorData(indicatorData);
+        result.image.setIndicatorData(*indicatorData);
     }
     return true;
 }
index c95725a..d38fec5 100644 (file)
@@ -1,3 +1,25 @@
+2017-09-27  Alex Christensen  <achristensen@webkit.org>
+
+        Allow modern decoding of std::optional<T>
+        https://bugs.webkit.org/show_bug.cgi?id=177519
+
+        Reviewed by Tim Horton.
+
+        * Platform/IPC/ArgumentCoders.h:
+        (IPC::ArgumentCoder<std::optional<T>>::decode):
+        * Shared/Cocoa/WebCoreArgumentCodersCocoa.mm:
+        (IPC::ArgumentCoder<WebCore::PaymentAuthorizationResult>::decode):
+        (IPC::ArgumentCoder<WebCore::PaymentError>::decode):
+        (IPC::ArgumentCoder<WebCore::PaymentMethodUpdate>::decode):
+        (IPC::ArgumentCoder<WebCore::ShippingContactUpdate>::decode):
+        (IPC::ArgumentCoder<WebCore::ShippingMethodUpdate>::decode):
+        * Shared/WebCoreArgumentCoders.cpp:
+        (IPC::ArgumentCoder<IntPoint>::decode):
+        (IPC::ArgumentCoder<IntSize>::decode):
+        * Shared/WebCoreArgumentCoders.h:
+        * Shared/WebPageCreationParameters.cpp:
+        (WebKit::WebPageCreationParameters::decode):
+
 2017-09-27  Commit Queue  <commit-queue@webkit.org>
 
         Unreviewed, rolling out r222541.
index ad6dbac..4d12af4 100644 (file)
@@ -110,6 +110,22 @@ template<typename T> struct ArgumentCoder<std::optional<T>> {
         optional = WTFMove(value);
         return true;
     }
+    
+    static std::optional<std::optional<T>> decode(Decoder& decoder)
+    {
+        std::optional<bool> isEngaged;
+        decoder >> isEngaged;
+        if (!isEngaged)
+            return std::nullopt;
+        if (*isEngaged) {
+            std::optional<T> value;
+            decoder >> value;
+            if (!value)
+                return std::nullopt;
+            return std::optional<std::optional<T>>(WTFMove(*value));
+        }
+        return std::optional<std::optional<T>>(std::optional<T>(std::nullopt));
+    }
 };
 
 template<typename T, typename U> struct ArgumentCoder<std::pair<T, U>> {
index 1548703..6a0d8c3 100644 (file)
@@ -88,14 +88,19 @@ void ArgumentCoder<WebCore::PaymentAuthorizationResult>::encode(Encoder& encoder
     encoder << result.errors;
 }
 
-bool ArgumentCoder<WebCore::PaymentAuthorizationResult>::decode(Decoder& decoder, WebCore::PaymentAuthorizationResult& result)
+std::optional<WebCore::PaymentAuthorizationResult> ArgumentCoder<WebCore::PaymentAuthorizationResult>::decode(Decoder& decoder)
 {
-    if (!decoder.decode(result.status))
-        return false;
-    if (!decoder.decode(result.errors))
-        return false;
+    std::optional<PaymentAuthorizationStatus> status;
+    decoder >> status;
+    if (!status)
+        return std::nullopt;
 
-    return true;
+    std::optional<Vector<PaymentError>> errors;
+    decoder >> errors;
+    if (!errors)
+        return std::nullopt;
+    
+    return {{ WTFMove(*status), WTFMove(*errors) }};
 }
 
 void ArgumentCoder<WebCore::PaymentContact>::encode(Encoder& encoder, const WebCore::PaymentContact& paymentContact)
@@ -141,15 +146,22 @@ void ArgumentCoder<WebCore::PaymentError>::encode(Encoder& encoder, const WebCor
 
 std::optional<WebCore::PaymentError> ArgumentCoder<WebCore::PaymentError>::decode(Decoder& decoder)
 {
-    WebCore::PaymentError error;
-    if (!decoder.decode(error.code))
+    std::optional<WebCore::PaymentError::Code> code;
+    decoder >> code;
+    if (!code)
         return std::nullopt;
-    if (!decoder.decode(error.message))
+    
+    std::optional<String> message;
+    decoder >> message;
+    if (!message)
         return std::nullopt;
-    if (!decoder.decode(error.contactField))
+    
+    std::optional<std::optional<WebCore::PaymentError::ContactField>> contactField;
+    decoder >> contactField;
+    if (!contactField)
         return std::nullopt;
 
-    return WTFMove(error);
+    return {{ WTFMove(*code), WTFMove(*message), WTFMove(*contactField) }};
 }
 
 void ArgumentCoder<WebCore::PaymentMerchantSession>::encode(Encoder& encoder, const WebCore::PaymentMerchantSession& paymentMerchantSession)
@@ -226,14 +238,13 @@ void ArgumentCoder<WebCore::PaymentMethodUpdate>::encode(Encoder& encoder, const
     encoder << update.newTotalAndLineItems;
 }
 
-bool ArgumentCoder<WebCore::PaymentMethodUpdate>::decode(Decoder& decoder, WebCore::PaymentMethodUpdate& update)
+std::optional<WebCore::PaymentMethodUpdate> ArgumentCoder<WebCore::PaymentMethodUpdate>::decode(Decoder& decoder)
 {
     std::optional<ApplePaySessionPaymentRequest::TotalAndLineItems> newTotalAndLineItems;
     decoder >> newTotalAndLineItems;
     if (!newTotalAndLineItems)
-        return false;
-    update = { WTFMove(*newTotalAndLineItems) };
-    return true;
+        return std::nullopt;
+    return {{ WTFMove(*newTotalAndLineItems) }};
 }
 
 void ArgumentCoder<ApplePaySessionPaymentRequest>::encode(Encoder& encoder, const ApplePaySessionPaymentRequest& request)
@@ -447,20 +458,24 @@ void ArgumentCoder<WebCore::ShippingContactUpdate>::encode(Encoder& encoder, con
     encoder << update.newTotalAndLineItems;
 }
 
-bool ArgumentCoder<WebCore::ShippingContactUpdate>::decode(Decoder& decoder, WebCore::ShippingContactUpdate& update)
+std::optional<WebCore::ShippingContactUpdate> ArgumentCoder<WebCore::ShippingContactUpdate>::decode(Decoder& decoder)
 {
-    if (!decoder.decode(update.errors))
-        return false;
-    if (!decoder.decode(update.newShippingMethods))
-        return false;
+    std::optional<Vector<PaymentError>> errors;
+    decoder >> errors;
+    if (!errors)
+        return std::nullopt;
+    
+    std::optional<Vector<ApplePaySessionPaymentRequest::ShippingMethod>> newShippingMethods;
+    decoder >> newShippingMethods;
+    if (!newShippingMethods)
+        return std::nullopt;
     
     std::optional<ApplePaySessionPaymentRequest::TotalAndLineItems> newTotalAndLineItems;
     decoder >> newTotalAndLineItems;
     if (!newTotalAndLineItems)
-        return false;
-    update.newTotalAndLineItems = WTFMove(*newTotalAndLineItems);
-
-    return true;
+        return std::nullopt;
+    
+    return {{ WTFMove(*errors), WTFMove(*newShippingMethods), WTFMove(*newTotalAndLineItems) }};
 }
 
 void ArgumentCoder<WebCore::ShippingMethodUpdate>::encode(Encoder& encoder, const WebCore::ShippingMethodUpdate& update)
@@ -468,14 +483,13 @@ void ArgumentCoder<WebCore::ShippingMethodUpdate>::encode(Encoder& encoder, cons
     encoder << update.newTotalAndLineItems;
 }
 
-bool ArgumentCoder<WebCore::ShippingMethodUpdate>::decode(Decoder& decoder, WebCore::ShippingMethodUpdate& update)
+std::optional<WebCore::ShippingMethodUpdate> ArgumentCoder<WebCore::ShippingMethodUpdate>::decode(Decoder& decoder)
 {
     std::optional<ApplePaySessionPaymentRequest::TotalAndLineItems> newTotalAndLineItems;
     decoder >> newTotalAndLineItems;
     if (!newTotalAndLineItems)
-        return false;
-    update = { WTFMove(*newTotalAndLineItems) };
-    return true;
+        return std::nullopt;
+    return {{ WTFMove(*newTotalAndLineItems) }};
 }
 
 }
index aa9980c..fd26b88 100644 (file)
@@ -559,6 +559,13 @@ bool ArgumentCoder<FloatPoint>::decode(Decoder& decoder, FloatPoint& floatPoint)
     return SimpleArgumentCoder<FloatPoint>::decode(decoder, floatPoint);
 }
 
+std::optional<FloatPoint> ArgumentCoder<FloatPoint>::decode(Decoder& decoder)
+{
+    FloatPoint floatPoint;
+    if (!SimpleArgumentCoder<FloatPoint>::decode(decoder, floatPoint))
+        return std::nullopt;
+    return WTFMove(floatPoint);
+}
 
 void ArgumentCoder<FloatPoint3D>::encode(Encoder& encoder, const FloatPoint3D& floatPoint)
 {
@@ -658,6 +665,13 @@ bool ArgumentCoder<IntPoint>::decode(Decoder& decoder, IntPoint& intPoint)
     return SimpleArgumentCoder<IntPoint>::decode(decoder, intPoint);
 }
 
+std::optional<WebCore::IntPoint> ArgumentCoder<IntPoint>::decode(Decoder& decoder)
+{
+    IntPoint intPoint;
+    if (!SimpleArgumentCoder<IntPoint>::decode(decoder, intPoint))
+        return std::nullopt;
+    return WTFMove(intPoint);
+}
 
 void ArgumentCoder<IntRect>::encode(Encoder& encoder, const IntRect& intRect)
 {
@@ -677,7 +691,6 @@ std::optional<IntRect> ArgumentCoder<IntRect>::decode(Decoder& decoder)
     return WTFMove(rect);
 }
 
-
 void ArgumentCoder<IntSize>::encode(Encoder& encoder, const IntSize& intSize)
 {
     SimpleArgumentCoder<IntSize>::encode(encoder, intSize);
@@ -688,6 +701,13 @@ bool ArgumentCoder<IntSize>::decode(Decoder& decoder, IntSize& intSize)
     return SimpleArgumentCoder<IntSize>::decode(decoder, intSize);
 }
 
+std::optional<IntSize> ArgumentCoder<IntSize>::decode(Decoder& decoder)
+{
+    IntSize intSize;
+    if (!SimpleArgumentCoder<IntSize>::decode(decoder, intSize))
+        return std::nullopt;
+    return WTFMove(intSize);
+}
 
 void ArgumentCoder<LayoutSize>::encode(Encoder& encoder, const LayoutSize& layoutSize)
 {
@@ -2298,44 +2318,45 @@ void ArgumentCoder<TextIndicatorData>::encode(Encoder& encoder, const TextIndica
     encodeOptionalImage(encoder, textIndicatorData.contentImageWithoutSelection.get());
 }
 
-bool ArgumentCoder<TextIndicatorData>::decode(Decoder& decoder, TextIndicatorData& textIndicatorData)
+std::optional<TextIndicatorData> ArgumentCoder<TextIndicatorData>::decode(Decoder& decoder)
 {
+    TextIndicatorData textIndicatorData;
     if (!decoder.decode(textIndicatorData.selectionRectInRootViewCoordinates))
-        return false;
+        return std::nullopt;
 
     if (!decoder.decode(textIndicatorData.textBoundingRectInRootViewCoordinates))
-        return false;
+        return std::nullopt;
 
     if (!decoder.decode(textIndicatorData.textRectsInBoundingRectCoordinates))
-        return false;
+        return std::nullopt;
 
     if (!decoder.decode(textIndicatorData.contentImageWithoutSelectionRectInRootViewCoordinates))
-        return false;
+        return std::nullopt;
 
     if (!decoder.decode(textIndicatorData.contentImageScaleFactor))
-        return false;
+        return std::nullopt;
 
     if (!decoder.decode(textIndicatorData.estimatedBackgroundColor))
-        return false;
+        return std::nullopt;
 
     if (!decoder.decodeEnum(textIndicatorData.presentationTransition))
-        return false;
+        return std::nullopt;
 
     uint64_t options;
     if (!decoder.decode(options))
-        return false;
+        return std::nullopt;
     textIndicatorData.options = static_cast<TextIndicatorOptions>(options);
 
     if (!decodeOptionalImage(decoder, textIndicatorData.contentImage))
-        return false;
+        return std::nullopt;
 
     if (!decodeOptionalImage(decoder, textIndicatorData.contentImageWithHighlight))
-        return false;
+        return std::nullopt;
 
     if (!decodeOptionalImage(decoder, textIndicatorData.contentImageWithoutSelection))
-        return false;
+        return std::nullopt;
 
-    return true;
+    return WTFMove(textIndicatorData);
 }
 
 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
@@ -2408,8 +2429,11 @@ bool ArgumentCoder<DictionaryPopupInfo>::decode(IPC::Decoder& decoder, Dictionar
     if (!decoder.decode(result.origin))
         return false;
 
-    if (!decoder.decode(result.textIndicator))
+    std::optional<TextIndicatorData> textIndicator;
+    decoder >> textIndicator;
+    if (!textIndicator)
         return false;
+    result.textIndicator = WTFMove(*textIndicator);
 
 #if PLATFORM(COCOA)
     bool hadOptions;
index 08c3839..dd9d84e 100644 (file)
@@ -231,6 +231,7 @@ template<> struct ArgumentCoder<WebCore::CertificateInfo> {
 template<> struct ArgumentCoder<WebCore::FloatPoint> {
     static void encode(Encoder&, const WebCore::FloatPoint&);
     static bool decode(Decoder&, WebCore::FloatPoint&);
+    static std::optional<WebCore::FloatPoint> decode(Decoder&);
 };
 
 template<> struct ArgumentCoder<WebCore::FloatPoint3D> {
@@ -274,6 +275,7 @@ template<> struct ArgumentCoder<WebCore::ViewportArguments> {
 template<> struct ArgumentCoder<WebCore::IntPoint> {
     static void encode(Encoder&, const WebCore::IntPoint&);
     static bool decode(Decoder&, WebCore::IntPoint&);
+    static std::optional<WebCore::IntPoint> decode(Decoder&);
 };
 
 template<> struct ArgumentCoder<WebCore::IntRect> {
@@ -285,6 +287,7 @@ template<> struct ArgumentCoder<WebCore::IntRect> {
 template<> struct ArgumentCoder<WebCore::IntSize> {
     static void encode(Encoder&, const WebCore::IntSize&);
     static bool decode(Decoder&, WebCore::IntSize&);
+    static std::optional<WebCore::IntSize> decode(Decoder&);
 };
 
 template<> struct ArgumentCoder<WebCore::LayoutSize> {
@@ -524,7 +527,7 @@ template<> struct ArgumentCoder<WebCore::MediaSessionMetadata> {
 
 template<> struct ArgumentCoder<WebCore::TextIndicatorData> {
     static void encode(Encoder&, const WebCore::TextIndicatorData&);
-    static bool decode(Decoder&, WebCore::TextIndicatorData&);
+    static std::optional<WebCore::TextIndicatorData> decode(Decoder&);
 };
 
 template<> struct ArgumentCoder<WebCore::DictionaryPopupInfo> {
@@ -565,7 +568,7 @@ template<> struct ArgumentCoder<WebCore::Payment> {
 
 template<> struct ArgumentCoder<WebCore::PaymentAuthorizationResult> {
     static void encode(Encoder&, const WebCore::PaymentAuthorizationResult&);
-    static bool decode(Decoder&, WebCore::PaymentAuthorizationResult&);
+    static std::optional<WebCore::PaymentAuthorizationResult> decode(Decoder&);
 };
 
 template<> struct ArgumentCoder<WebCore::PaymentContact> {
@@ -590,7 +593,7 @@ template<> struct ArgumentCoder<WebCore::PaymentMethod> {
 
 template<> struct ArgumentCoder<WebCore::PaymentMethodUpdate> {
     static void encode(Encoder&, const WebCore::PaymentMethodUpdate&);
-    static bool decode(Decoder&, WebCore::PaymentMethodUpdate&);
+    static std::optional<WebCore::PaymentMethodUpdate> decode(Decoder&);
 };
 
 template<> struct ArgumentCoder<WebCore::ApplePaySessionPaymentRequest> {
@@ -625,12 +628,12 @@ template<> struct ArgumentCoder<WebCore::ApplePaySessionPaymentRequest::TotalAnd
 
 template<> struct ArgumentCoder<WebCore::ShippingContactUpdate> {
     static void encode(Encoder&, const WebCore::ShippingContactUpdate&);
-    static bool decode(Decoder&, WebCore::ShippingContactUpdate&);
+    static std::optional<WebCore::ShippingContactUpdate> decode(Decoder&);
 };
 
 template<> struct ArgumentCoder<WebCore::ShippingMethodUpdate> {
     static void encode(Encoder&, const WebCore::ShippingMethodUpdate&);
-    static bool decode(Decoder&, WebCore::ShippingMethodUpdate&);
+    static std::optional<WebCore::ShippingMethodUpdate> decode(Decoder&);
 };
 
 #endif
index f3e6a5e..bb131ba 100644 (file)
@@ -164,11 +164,12 @@ bool WebHitTestResultData::decode(IPC::Decoder& decoder, WebHitTestResultData& h
         return false;
 
     if (hasLinkTextIndicator) {
-        WebCore::TextIndicatorData indicatorData;
-        if (!decoder.decode(indicatorData))
+        std::optional<WebCore::TextIndicatorData> indicatorData;
+        decoder >> indicatorData;
+        if (!indicatorData)
             return false;
 
-        hitTestResultData.linkTextIndicator = WebCore::TextIndicator::create(indicatorData);
+        hitTestResultData.linkTextIndicator = WebCore::TextIndicator::create(*indicatorData);
     }
 
     return platformDecode(decoder, hitTestResultData);
index f651570..94e8813 100644 (file)
@@ -191,8 +191,13 @@ std::optional<WebPageCreationParameters> WebPageCreationParameters::decode(IPC::
         return std::nullopt;
     if (!decoder.decodeEnum(parameters.scrollPinningBehavior))
         return std::nullopt;
-    if (!decoder.decode(parameters.scrollbarOverlayStyle))
+
+    std::optional<std::optional<uint32_t>> scrollbarOverlayStyle;
+    decoder >> scrollbarOverlayStyle;
+    if (!scrollbarOverlayStyle)
         return std::nullopt;
+    parameters.scrollbarOverlayStyle = WTFMove(*scrollbarOverlayStyle);
+
     if (!decoder.decode(parameters.backgroundExtendsBeyondPage))
         return std::nullopt;
     if (!decoder.decodeEnum(parameters.layerHostingMode))
@@ -246,8 +251,11 @@ std::optional<WebPageCreationParameters> WebPageCreationParameters::decode(IPC::
     if (!decoder.decode(parameters.overrideContentSecurityPolicy))
         return std::nullopt;
 
-    if (!decoder.decode(parameters.cpuLimit))
+    std::optional<std::optional<double>> cpuLimit;
+    decoder >> cpuLimit;
+    if (!cpuLimit)
         return std::nullopt;
+    parameters.cpuLimit = WTFMove(*cpuLimit);
 
     if (!decoder.decode(parameters.urlSchemeHandlers))
         return std::nullopt;
index fe550fb..f515082 100644 (file)
@@ -144,8 +144,11 @@ bool InteractionInformationAtPosition::decode(IPC::Decoder& decoder, Interaction
     if (!decoder.decode(result.textAfter))
         return false;
     
-    if (!decoder.decode(result.linkIndicator))
+    std::optional<WebCore::TextIndicatorData> linkIndicator;
+    decoder >> linkIndicator;
+    if (!linkIndicator)
         return false;
+    result.linkIndicator = WTFMove(*linkIndicator);
 
     ShareableBitmap::Handle handle;
     if (!decoder.decode(handle))
index 28a3ba7..91fd9fe 100644 (file)
@@ -97,11 +97,12 @@ bool WebHitTestResultData::platformDecode(IPC::Decoder& decoder, WebHitTestResul
         return false;
 
     if (hasDetectedDataTextIndicator) {
-        WebCore::TextIndicatorData indicatorData;
-        if (!decoder.decode(indicatorData))
+        std::optional<WebCore::TextIndicatorData> indicatorData;
+        decoder >> indicatorData;
+        if (!indicatorData)
             return false;
 
-        hitTestResultData.detectedDataTextIndicator = WebCore::TextIndicator::create(indicatorData);
+        hitTestResultData.detectedDataTextIndicator = WebCore::TextIndicator::create(*indicatorData);
     }
 
     return true;