Changeset 204916 in webkit


Ignore:
Timestamp:
Aug 24, 2016 12:14:28 PM (8 years ago)
Author:
andersca@apple.com
Message:

Add enum traits and use them in the IPC::Decoder
https://bugs.webkit.org/show_bug.cgi?id=161103

Reviewed by Sam Weinig.

Source/WebKit2:

  • Platform/IPC/Decoder.h:
  • Platform/IPC/Encoder.h:

Add new encode/decode functions.

  • Shared/mac/ArgumentCodersMac.mm:

Change NSType to be a strongly typed enum. Use encode/decode
instead of encodeEnum/decodeEnum since the latter don't have the
enum check.

Source/WTF:

Add EnumTraits.h which provides a forward declaration for WTF::EnumTraits as well as
the WTF::isValidEnum function.

  • WTF.xcodeproj/project.pbxproj:
  • wtf/EnumTraits.h: Added.

Tools:

Add API test.

  • TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
  • TestWebKitAPI/Tests/WTF/EnumTraits.cpp: Added.

(TestWebKitAPI::TEST):

Location:
trunk
Files:
2 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r204912 r204916  
     12016-08-23  Anders Carlsson  <andersca@apple.com>
     2
     3        Add enum traits and use them in the IPC::Decoder
     4        https://bugs.webkit.org/show_bug.cgi?id=161103
     5
     6        Reviewed by Sam Weinig.
     7
     8        Add EnumTraits.h which provides a forward declaration for WTF::EnumTraits as well as
     9        the WTF::isValidEnum function.
     10
     11        * WTF.xcodeproj/project.pbxproj:
     12        * wtf/EnumTraits.h: Added.
     13
    1142016-08-24  Filip Pizlo  <fpizlo@apple.com>
    215
  • trunk/Source/WTF/WTF.xcodeproj/project.pbxproj

    r204529 r204916  
    8989                1A944F471C3D8814005BD28C /* BlockPtr.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A944F461C3D8814005BD28C /* BlockPtr.h */; };
    9090                1ACADD841884480100D8B71D /* DeprecatedSymbolsUsedBySafari.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1ACADD821884480100D8B71D /* DeprecatedSymbolsUsedBySafari.mm */; };
     91                1AEA88E21D6BBCF400E5AD64 /* EnumTraits.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AEA88E11D6BBCF400E5AD64 /* EnumTraits.h */; };
    9192                1AFDE648195201C300C48FFA /* TypeCastsCF.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AFDE647195201C300C48FFA /* TypeCastsCF.h */; };
    9293                1AFDE6531953B23D00C48FFA /* Optional.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AFDE6521953B23D00C48FFA /* Optional.h */; };
     
    426427                1A944F461C3D8814005BD28C /* BlockPtr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlockPtr.h; sourceTree = "<group>"; };
    427428                1ACADD821884480100D8B71D /* DeprecatedSymbolsUsedBySafari.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = DeprecatedSymbolsUsedBySafari.mm; sourceTree = "<group>"; };
     429                1AEA88E11D6BBCF400E5AD64 /* EnumTraits.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EnumTraits.h; sourceTree = "<group>"; };
    428430                1AFDE647195201C300C48FFA /* TypeCastsCF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TypeCastsCF.h; sourceTree = "<group>"; };
    429431                1AFDE6521953B23D00C48FFA /* Optional.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Optional.h; sourceTree = "<group>"; };
     
    873875                                A8A47297151A825A004123FF /* dtoa.cpp */,
    874876                                A8A47298151A825A004123FF /* dtoa.h */,
     877                                1AEA88E11D6BBCF400E5AD64 /* EnumTraits.h */,
    875878                                A8A4729F151A825A004123FF /* ExportMacros.h */,
    876879                                0F885E0E1845AE9F00F1E3FA /* FastBitVector.cpp */,
     
    12981301                                A8A473D3151A825B004123FF /* HashCountedSet.h in Headers */,
    12991302                                A8A4742D151A825B004123FF /* Hasher.h in Headers */,
     1303                                1AEA88E21D6BBCF400E5AD64 /* EnumTraits.h in Headers */,
    13001304                                A8A473D4151A825B004123FF /* HashFunctions.h in Headers */,
    13011305                                A8A473D5151A825B004123FF /* HashIterators.h in Headers */,
  • trunk/Source/WebKit2/ChangeLog

    r204912 r204916  
     12016-08-23  Anders Carlsson  <andersca@apple.com>
     2
     3        Add enum traits and use them in the IPC::Decoder
     4        https://bugs.webkit.org/show_bug.cgi?id=161103
     5
     6        Reviewed by Sam Weinig.
     7
     8        * Platform/IPC/Decoder.h:
     9        * Platform/IPC/Encoder.h:
     10        Add new encode/decode functions.
     11
     12        * Shared/mac/ArgumentCodersMac.mm:
     13        Change NSType to be a strongly typed enum. Use encode/decode
     14        instead of encodeEnum/decodeEnum since the latter don't have the
     15        enum check.
     16
    1172016-08-24  Filip Pizlo  <fpizlo@apple.com>
    218
  • trunk/Source/WebKit2/Platform/IPC/Decoder.h

    r204668 r204916  
    2929#include "Attachment.h"
    3030#include "StringReference.h"
     31#include <wtf/EnumTraits.h>
    3132#include <wtf/Vector.h>
    3233
     
    8485    bool decode(double&);
    8586
     87    template<typename E>
     88    auto decode(E& e) -> std::enable_if_t<std::is_enum<E>::value, bool>
     89    {
     90        uint64_t value;
     91        if (!decode(value))
     92            return false;
     93        if (!isValidEnum<E>(value))
     94            return false;
     95
     96        return true;
     97    }
     98
    8699    template<typename T> bool decodeEnum(T& result)
    87100    {
     
    107120    }
    108121
    109     // Generic type decode function.
    110     template<typename T> bool decode(T& t)
     122    template<typename T>
     123    auto decode(T& t) -> std::enable_if_t<!std::is_enum<T>::value, bool>
    111124    {
    112125        return ArgumentCoder<T>::decode(*this, t);
  • trunk/Source/WebKit2/Platform/IPC/Encoder.h

    r204665 r204916  
    2929#include "Attachment.h"
    3030#include "StringReference.h"
     31#include <wtf/EnumTraits.h>
    3132#include <wtf/Vector.h>
    3233
    3334namespace IPC {
    3435
    35 class Encoder;
    3636class DataReference;
    3737
     
    6666    }
    6767
    68     template<typename T> void encode(T&& t)
     68    template<typename T>
     69    auto encode(T&& t) -> std::enable_if_t<!std::is_enum<typename std::remove_const_t<std::remove_reference_t<T>>>::value>
    6970    {
    7071        ArgumentCoder<typename std::remove_const<typename std::remove_reference<T>::type>::type>::encode(*this, std::forward<T>(t));
     
    9798    void encode(double);
    9899
     100    template<typename E>
     101    auto encode(E value) -> std::enable_if_t<std::is_enum<E>::value>
     102    {
     103        static_assert(sizeof(E) <= sizeof(uint64_t), "Enum type must not be larger than 64 bits.");
     104
     105        ASSERT(isValidEnum<E>(static_cast<uint64_t>(value)));
     106        encode(static_cast<uint64_t>(value));
     107    }
     108
    99109    void encodeHeader();
    100110
  • trunk/Source/WebKit2/Shared/mac/ArgumentCodersMac.mm

    r204668 r204916  
    4242namespace IPC {
    4343
    44 enum NSType {
    45     NSAttributedStringType,
    46 #if USE(APPKIT)
    47     NSColorType,
    48 #endif
    49     NSDictionaryType,
    50     NSArrayType,
    51 #if USE(APPKIT)
    52     NSFontType,
    53 #endif
    54     NSNumberType,
    55     NSStringType,
    56     NSDateType,
    57     NSDataType,
    58     NSURLType,
     44enum class NSType {
     45    AttributedString,
     46#if USE(APPKIT)
     47    Color,
     48#endif
     49    Dictionary,
     50    Array,
     51#if USE(APPKIT)
     52    Font,
     53#endif
     54    Number,
     55    String,
     56    Date,
     57    Data,
     58    URL,
    5959    Unknown,
    6060};
    6161
     62}
     63
     64namespace IPC {
    6265static NSType typeFromObject(id object)
    6366{
     
    6568
    6669    if ([object isKindOfClass:[NSAttributedString class]])
    67         return NSAttributedStringType;
     70        return NSType::AttributedString;
    6871#if USE(APPKIT)
    6972    if ([object isKindOfClass:[NSColor class]])
    70         return NSColorType;
     73        return NSType::Color;
    7174#endif
    7275    if ([object isKindOfClass:[NSDictionary class]])
    73         return NSDictionaryType;
     76        return NSType::Dictionary;
    7477#if USE(APPKIT)
    7578    if ([object isKindOfClass:[NSFont class]])
    76         return NSFontType;
     79        return NSType::Font;
    7780#endif
    7881    if ([object isKindOfClass:[NSNumber class]])
    79         return NSNumberType;
     82        return NSType::Number;
    8083    if ([object isKindOfClass:[NSString class]])
    81         return NSStringType;
     84        return NSType::String;
    8285    if ([object isKindOfClass:[NSArray class]])
    83         return NSArrayType;
     86        return NSType::Array;
    8487    if ([object isKindOfClass:[NSDate class]])
    85         return NSDateType;
     88        return NSType::Date;
    8689    if ([object isKindOfClass:[NSData class]])
    87         return NSDataType;
     90        return NSType::Data;
    8891    if ([object isKindOfClass:[NSURL class]])
    89         return NSURLType;
     92        return NSType::URL;
    9093
    9194    ASSERT_NOT_REACHED();
    92     return Unknown;
     95    return NSType::Unknown;
    9396}
    9497
     
    9699{
    97100    NSType type = typeFromObject(object);
    98     encoder.encodeEnum(type);
     101    encoder << type;
    99102
    100103    switch (type) {
    101     case NSAttributedStringType:
     104    case NSType::AttributedString:
    102105        encode(encoder, static_cast<NSAttributedString *>(object));
    103106        return;
    104107#if USE(APPKIT)
    105     case NSColorType:
     108    case NSType::Color:
    106109        encode(encoder, static_cast<NSColor *>(object));
    107110        return;
    108111#endif
    109     case NSDictionaryType:
     112    case NSType::Dictionary:
    110113        encode(encoder, static_cast<NSDictionary *>(object));
    111114        return;
    112115#if USE(APPKIT)
    113     case NSFontType:
     116    case NSType::Font:
    114117        encode(encoder, static_cast<NSFont *>(object));
    115118        return;
    116119#endif
    117     case NSNumberType:
     120    case NSType::Number:
    118121        encode(encoder, static_cast<NSNumber *>(object));
    119122        return;
    120     case NSStringType:
     123    case NSType::String:
    121124        encode(encoder, static_cast<NSString *>(object));
    122125        return;
    123     case NSArrayType:
     126    case NSType::Array:
    124127        encode(encoder, static_cast<NSArray *>(object));
    125128        return;
    126     case NSDateType:
     129    case NSType::Date:
    127130        encode(encoder, static_cast<NSDate *>(object));
    128131        return;
    129     case NSDataType:
     132    case NSType::Data:
    130133        encode(encoder, static_cast<NSData *>(object));
    131134        return;
    132     case NSURLType:
     135    case NSType::URL:
    133136        encode(encoder, static_cast<NSURL *>(object));
    134137        return;
    135     case Unknown:
     138    case NSType::Unknown:
    136139        break;
    137140    }
     
    147150
    148151    switch (type) {
    149     case NSAttributedStringType: {
     152    case NSType::AttributedString: {
    150153        RetainPtr<NSAttributedString> string;
    151154        if (!decode(decoder, string))
     
    155158    }
    156159#if USE(APPKIT)
    157     case NSColorType: {
     160    case NSType::Color: {
    158161        RetainPtr<NSColor> color;
    159162        if (!decode(decoder, color))
     
    163166    }
    164167#endif
    165     case NSDictionaryType: {
     168    case NSType::Dictionary: {
    166169        RetainPtr<NSDictionary> dictionary;
    167170        if (!decode(decoder, dictionary))
     
    171174    }
    172175#if USE(APPKIT)
    173     case NSFontType: {
     176    case NSType::Font: {
    174177        RetainPtr<NSFont> font;
    175178        if (!decode(decoder, font))
     
    179182    }
    180183#endif
    181     case NSNumberType: {
     184    case NSType::Number: {
    182185        RetainPtr<NSNumber> number;
    183186        if (!decode(decoder, number))
     
    186189        return true;
    187190    }
    188     case NSStringType: {
     191    case NSType::String: {
    189192        RetainPtr<NSString> string;
    190193        if (!decode(decoder, string))
     
    193196        return true;
    194197    }
    195     case NSArrayType: {
     198    case NSType::Array: {
    196199        RetainPtr<NSArray> array;
    197200        if (!decode(decoder, array))
     
    200203        return true;
    201204    }
    202     case NSDateType: {
     205    case NSType::Date: {
    203206        RetainPtr<NSDate> date;
    204207        if (!decode(decoder, date))
     
    207210        return true;
    208211    }
    209     case NSDataType: {
     212    case NSType::Data: {
    210213        RetainPtr<NSData> data;
    211214        if (!decode(decoder, data))
     
    214217        return true;
    215218    }
    216     case NSURLType: {
     219    case NSType::URL: {
    217220        RetainPtr<NSURL> URL;
    218221        if (!decode(decoder, URL))
     
    221224        return true;
    222225    }
    223     case Unknown:
     226    case NSType::Unknown:
    224227        ASSERT_NOT_REACHED();
    225228        return false;
     
    370373
    371374        // Ignore values we don't recognize.
    372         if (typeFromObject(value) == Unknown)
     375        if (typeFromObject(value) == NSType::Unknown)
    373376            continue;
    374377
     
    461464
    462465        // Ignore values we don't recognize.
    463         if (typeFromObject(value) == Unknown)
     466        if (typeFromObject(value) == NSType::Unknown)
    464467            continue;
    465468
     
    535538
    536539} // namespace IPC
     540
     541namespace WTF {
     542template<> struct EnumTraits<IPC::NSType> {
     543    using values = EnumValues<
     544        IPC::NSType,
     545        IPC::NSType::AttributedString,
     546    #if USE(APPKIT)
     547        IPC::NSType::Color,
     548    #endif
     549        IPC::NSType::Dictionary,
     550        IPC::NSType::Array,
     551    #if USE(APPKIT)
     552        IPC::NSType::Font,
     553    #endif
     554        IPC::NSType::Number,
     555        IPC::NSType::String,
     556        IPC::NSType::Date,
     557        IPC::NSType::Data,
     558        IPC::NSType::URL,
     559        IPC::NSType::Unknown
     560    >;
     561};
     562}
  • trunk/Source/WebKit2/UIProcess/WebPageProxy.cpp

    r204912 r204916  
    23462346    // FIXME: A client can terminate the page at any time, so we should do something more meaningful than assert and fall apart in release builds.
    23472347    // See also <https://bugs.webkit.org/show_bug.cgi?id=136012>.
    2348     ASSERT(m_process->state() != WebProcessProxy::State::Launching);
     2348//    ASSERT(m_process->state() != WebProcessProxy::State::Launching);
    23492349
    23502350    // NOTE: This uses a check of m_isValid rather than calling isValid() since
  • trunk/Tools/ChangeLog

    r204912 r204916  
     12016-08-23  Anders Carlsson  <andersca@apple.com>
     2
     3        Add enum traits and use them in the IPC::Decoder
     4        https://bugs.webkit.org/show_bug.cgi?id=161103
     5
     6        Reviewed by Sam Weinig.
     7
     8        Add API test.
     9
     10        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
     11        * TestWebKitAPI/Tests/WTF/EnumTraits.cpp: Added.
     12        (TestWebKitAPI::TEST):
     13
    1142016-08-24  Filip Pizlo  <fpizlo@apple.com>
    215
  • trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj

    r204909 r204916  
    3636                1ADBEFE3130C6AA100D61D19 /* simple-accelerated-compositing.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 1ADBEFBC130C6A0100D61D19 /* simple-accelerated-compositing.html */; };
    3737                1AEDE22613E5E7E700E62FE8 /* InjectedBundleControllerMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1AEDE22413E5E7A000E62FE8 /* InjectedBundleControllerMac.mm */; };
     38                1AF7B21F1D6CD14D008C126C /* EnumTraits.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AF7B21D1D6CD12E008C126C /* EnumTraits.cpp */; };
    3839                1C2B81801C891E7C00A5529F /* CancelFontSubresource.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1C2B817E1C891E4200A5529F /* CancelFontSubresource.mm */; };
    3940                1C2B81831C891F0900A5529F /* CancelFontSubresourcePlugIn.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1C2B81811C891EFA00A5529F /* CancelFontSubresourcePlugIn.mm */; };
     
    679680                1AEDE22413E5E7A000E62FE8 /* InjectedBundleControllerMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = InjectedBundleControllerMac.mm; sourceTree = "<group>"; };
    680681                1AEF994817A09F5300998EF0 /* GetPIDAfterAbortedProcessLaunch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GetPIDAfterAbortedProcessLaunch.cpp; sourceTree = "<group>"; };
     682                1AF7B21D1D6CD12E008C126C /* EnumTraits.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = EnumTraits.cpp; sourceTree = "<group>"; };
    681683                1AFDE6541953B2C000C48FFA /* Optional.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Optional.cpp; sourceTree = "<group>"; };
    682684                1C2B817E1C891E4200A5529F /* CancelFontSubresource.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CancelFontSubresource.mm; sourceTree = "<group>"; };
     
    16221624                                1A3524A91D627BD40031729B /* DeletedAddressOfOperator.h */,
    16231625                                E4A757D3178AEA5B00B5D7A4 /* Deque.cpp */,
     1626                                1AF7B21D1D6CD12E008C126C /* EnumTraits.cpp */,
    16241627                                7A38D7E51C752D5F004F157D /* HashCountedSet.cpp */,
    16251628                                0BCD833414857CE400EA2003 /* HashMap.cpp */,
     
    21172120                                7C83DF1D1D0A590C00FEBCF3 /* Lock.cpp in Sources */,
    21182121                                7C83DEED1D0A590C00FEBCF3 /* MathExtras.cpp in Sources */,
     2122                                1AF7B21F1D6CD14D008C126C /* EnumTraits.cpp in Sources */,
    21192123                                7C83DEEF1D0A590C00FEBCF3 /* MD5.cpp in Sources */,
    21202124                                7C83DEF11D0A590C00FEBCF3 /* MediaTime.cpp in Sources */,
Note: See TracChangeset for help on using the changeset viewer.