| 1 | /* |
| 2 | * Copyright (C) 2010 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include <wtf/Optional.h> |
| 29 | |
| 30 | namespace WebCore { |
| 31 | class IntConstraint; |
| 32 | class DoubleConstraint; |
| 33 | class ResourceResponse; |
| 34 | struct ViewportArguments; |
| 35 | } |
| 36 | |
| 37 | namespace IPC { |
| 38 | |
| 39 | class Decoder; |
| 40 | class Encoder; |
| 41 | |
| 42 | template<typename> struct ArgumentCoder; |
| 43 | |
| 44 | template<typename> class UsesModernDecoder; |
| 45 | |
| 46 | template<typename U> |
| 47 | class UsesModernDecoder { |
| 48 | private: |
| 49 | template<typename T, T> struct Helper; |
| 50 | template<typename T> static uint8_t check(Helper<Optional<U> (*)(Decoder&), &T::decode>*); |
| 51 | template<typename T> static uint16_t check(...); |
| 52 | template<typename T> static uint8_t checkArgumentCoder(Helper<Optional<U> (*)(Decoder&), &ArgumentCoder<T>::decode>*); |
| 53 | template<typename T> static uint16_t checkArgumentCoder(...); |
| 54 | public: |
| 55 | static constexpr bool argumentCoderValue = sizeof(check<U>(nullptr)) == sizeof(uint8_t); |
| 56 | static constexpr bool value = argumentCoderValue || sizeof(checkArgumentCoder<U>(nullptr)) == sizeof(uint8_t); |
| 57 | }; |
| 58 | |
| 59 | template<typename... Types> |
| 60 | class UsesModernDecoder<std::tuple<Types...>> { |
| 61 | public: |
| 62 | static constexpr bool value = true; |
| 63 | static constexpr bool argumentCoderValue = true; |
| 64 | }; |
| 65 | |
| 66 | template<typename U> |
| 67 | class UsesLegacyDecoder { |
| 68 | private: |
| 69 | template<typename T, T> struct Helper; |
| 70 | template<typename T> static uint8_t check(Helper<bool (*)(Decoder&, U&), &T::decode>*); |
| 71 | template<typename T> static uint16_t check(...); |
| 72 | template<typename T> static uint8_t checkArgumentCoder(Helper<bool (*)(Decoder&, U&), &ArgumentCoder<T>::decode>*); |
| 73 | template<typename T> static uint16_t checkArgumentCoder(...); |
| 74 | public: |
| 75 | static constexpr bool argumentCoderValue = sizeof(check<U>(nullptr)) == sizeof(uint8_t); |
| 76 | static constexpr bool value = argumentCoderValue || sizeof(checkArgumentCoder<U>(nullptr)) == sizeof(uint8_t); |
| 77 | }; |
| 78 | |
| 79 | template<typename BoolType> |
| 80 | class DefaultDecoderValues { |
| 81 | public: |
| 82 | static constexpr bool argumentCoderValue = BoolType::value; |
| 83 | static constexpr bool value = BoolType::value; |
| 84 | }; |
| 85 | |
| 86 | // ResourceResponseBase has the legacy decode template, not ResourceResponse. |
| 87 | template<> class UsesModernDecoder<WebCore::ResourceResponse> : public DefaultDecoderValues<std::false_type> { }; |
| 88 | template<> class UsesLegacyDecoder<WebCore::ResourceResponse> : public DefaultDecoderValues<std::true_type> { }; |
| 89 | |
| 90 | // IntConstraint and DoubleConstraint have their legacy decoder templates in NumericConstraint. |
| 91 | template<> class UsesModernDecoder<WebCore::IntConstraint> : public DefaultDecoderValues<std::false_type> { }; |
| 92 | template<> class UsesLegacyDecoder<WebCore::IntConstraint> : public DefaultDecoderValues<std::true_type> { }; |
| 93 | template<> class UsesModernDecoder<WebCore::DoubleConstraint> : public DefaultDecoderValues<std::false_type> { }; |
| 94 | template<> class UsesLegacyDecoder<WebCore::DoubleConstraint> : public DefaultDecoderValues<std::true_type> { }; |
| 95 | |
| 96 | template<typename T> struct ArgumentCoder { |
| 97 | static void encode(Encoder& encoder, const T& t) |
| 98 | { |
| 99 | t.encode(encoder); |
| 100 | } |
| 101 | |
| 102 | template<typename U = T, std::enable_if_t<UsesLegacyDecoder<U>::argumentCoderValue>* = nullptr> |
| 103 | static bool decode(Decoder& decoder, U& u) |
| 104 | { |
| 105 | return U::decode(decoder, u); |
| 106 | } |
| 107 | |
| 108 | template<typename U = T, std::enable_if_t<UsesModernDecoder<U>::argumentCoderValue>* = nullptr> |
| 109 | static Optional<U> decode(Decoder& decoder) |
| 110 | { |
| 111 | return U::decode(decoder); |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | } |
| 116 | |