Changeset 244450 in webkit


Ignore:
Timestamp:
Apr 18, 2019 9:26:49 PM (5 years ago)
Author:
Fujii Hironori
Message:

Implement KeyedDecoderGeneric and KeyedEncoderGeneric
https://bugs.webkit.org/show_bug.cgi?id=186410

Reviewed by Don Olmstead.

Implemented KeyedDecoderGeneric and KeyedEncoderGeneric by using
WTF::Persistence::Decoder and WTF::Persistence::Encoder.

No new tests. Covered by existing tests.

  • PlatformWin.cmake: Added KeyedDecoderGeneric.cpp and

KeyedEncoderGeneric.cpp, and removed KeyedDecoderCF.cpp and
KeyedEncoderCF.cpp for WinCairo port.

  • platform/generic/KeyedDecoderGeneric.cpp:
  • platform/generic/KeyedDecoderGeneric.h:
  • platform/generic/KeyedEncoderGeneric.cpp:
  • platform/generic/KeyedEncoderGeneric.h:
Location:
trunk/Source/WebCore
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r244448 r244450  
     12019-04-18  Fujii Hironori  <Hironori.Fujii@sony.com>
     2
     3        Implement KeyedDecoderGeneric and KeyedEncoderGeneric
     4        https://bugs.webkit.org/show_bug.cgi?id=186410
     5
     6        Reviewed by Don Olmstead.
     7
     8        Implemented KeyedDecoderGeneric and KeyedEncoderGeneric by using
     9        WTF::Persistence::Decoder and WTF::Persistence::Encoder.
     10
     11        No new tests. Covered by existing tests.
     12
     13        * PlatformWin.cmake: Added KeyedDecoderGeneric.cpp and
     14        KeyedEncoderGeneric.cpp, and removed KeyedDecoderCF.cpp and
     15        KeyedEncoderCF.cpp for WinCairo port.
     16        * platform/generic/KeyedDecoderGeneric.cpp:
     17        * platform/generic/KeyedDecoderGeneric.h:
     18        * platform/generic/KeyedEncoderGeneric.cpp:
     19        * platform/generic/KeyedEncoderGeneric.h:
     20
    1212019-04-18  Ross Kirsling  <ross.kirsling@sony.com>
    222
  • trunk/Source/WebCore/PlatformWin.cmake

    r244443 r244450  
    166166        loader/archive/cf/LegacyWebArchive.cpp
    167167
    168         platform/cf/KeyedDecoderCF.cpp
    169         platform/cf/KeyedEncoderCF.cpp
    170168        platform/cf/SharedBufferCF.cpp
    171169
     
    185183else ()
    186184    list(APPEND WebCore_SOURCES
     185        platform/text/Hyphenation.cpp
     186    )
     187endif ()
     188
     189if (USE_CF AND NOT WTF_PLATFORM_WIN_CAIRO)
     190    list(APPEND WebCore_SOURCES
     191        platform/cf/KeyedDecoderCF.cpp
     192        platform/cf/KeyedEncoderCF.cpp
     193    )
     194else ()
     195    list(APPEND WebCore_SOURCES
    187196        platform/generic/KeyedDecoderGeneric.cpp
    188197        platform/generic/KeyedEncoderGeneric.cpp
    189 
    190         platform/text/Hyphenation.cpp
    191198    )
    192199endif ()
  • trunk/Source/WebCore/platform/generic/KeyedDecoderGeneric.cpp

    r238913 r244450  
    2727#include "KeyedDecoderGeneric.h"
    2828
     29#include "KeyedEncoderGeneric.h"
     30#include <wtf/HashMap.h>
     31#include <wtf/Variant.h>
     32#include <wtf/Vector.h>
     33#include <wtf/persistence/PersistentDecoder.h>
     34#include <wtf/text/StringHash.h>
     35
    2936namespace WebCore {
    3037
    31 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=186410
     38class KeyedDecoderGeneric::Dictionary {
     39    WTF_MAKE_FAST_ALLOCATED;
     40public:
     41    using Node = Variant<Vector<uint8_t>, bool, uint32_t, uint64_t, int32_t, int64_t, float, double, String, std::unique_ptr<Dictionary>, std::unique_ptr<Array>>;
     42
     43    template <typename T>
     44    void add(const String& key, T&& value) { m_map.add(key, std::make_unique<Node>(std::forward<T>(value))); }
     45    Node& get(const String& key) { return *m_map.get(key); }
     46
     47private:
     48    HashMap<String, std::unique_ptr<Node>> m_map;
     49};
     50
     51static bool readString(WTF::Persistence::Decoder& decoder, String& result)
     52{
     53    size_t size;
     54    if (!decoder.decode(size))
     55        return false;
     56    Vector<uint8_t> buffer(size);
     57    if (!decoder.decodeFixedLengthData(buffer.data(), size))
     58        return false;
     59    result = String::fromUTF8(buffer.data(), size);
     60    return true;
     61}
     62
     63template<typename T>
     64static bool readSimpleValue(WTF::Persistence::Decoder& decoder, KeyedDecoderGeneric::Dictionary& dictionary)
     65{
     66    String key;
     67    bool ok = readString(decoder, key);
     68    if (!ok)
     69        return false;
     70    T value;
     71    ok = decoder.decode(value);
     72    if (!ok)
     73        return false;
     74    dictionary.add(key, WTFMove(value));
     75    return true;
     76}
     77
    3278std::unique_ptr<KeyedDecoder> KeyedDecoder::decoder(const uint8_t* data, size_t size)
    3379{
     
    3581}
    3682
    37 KeyedDecoderGeneric::KeyedDecoderGeneric(const uint8_t*, size_t)
    38 {
    39 }
    40 
    41 KeyedDecoderGeneric::~KeyedDecoderGeneric()
    42 {
    43 }
    44 
    45 bool KeyedDecoderGeneric::decodeBytes(const String&, const uint8_t*&, size_t&)
    46 {
    47     return false;
    48 }
    49 
    50 bool KeyedDecoderGeneric::decodeBool(const String&, bool&)
    51 {
    52     return false;
    53 }
    54 
    55 bool KeyedDecoderGeneric::decodeUInt32(const String&, uint32_t&)
    56 {
    57     return false;
    58 }
    59 
    60 bool KeyedDecoderGeneric::decodeUInt64(const String&, uint64_t&)
    61 {
    62     return false;
    63 }
    64 
    65 bool KeyedDecoderGeneric::decodeInt32(const String&, int32_t&)
    66 {
    67     return false;
    68 }
    69 
    70 bool KeyedDecoderGeneric::decodeInt64(const String&, int64_t&)
    71 {
    72     return false;
    73 }
    74 
    75 bool KeyedDecoderGeneric::decodeFloat(const String&, float&)
    76 {
    77     return false;
    78 }
    79 
    80 bool KeyedDecoderGeneric::decodeDouble(const String&, double&)
    81 {
    82     return false;
    83 }
    84 
    85 bool KeyedDecoderGeneric::decodeString(const String&, String&)
    86 {
    87     return false;
    88 }
    89 
    90 bool KeyedDecoderGeneric::beginObject(const String&)
    91 {
    92     return false;
     83KeyedDecoderGeneric::KeyedDecoderGeneric(const uint8_t* data, size_t size)
     84{
     85    WTF::Persistence::Decoder decoder(data, size);
     86    KeyedEncoderGeneric::Type type;
     87    String key;
     88
     89    m_rootDictionary = std::make_unique<Dictionary>();
     90    m_dictionaryStack.append(m_rootDictionary.get());
     91
     92    bool ok = true;
     93    while (ok && decoder.decodeEnum(type)) {
     94        switch (type) {
     95        case KeyedEncoderGeneric::Type::Bytes: {
     96            ok = readString(decoder, key);
     97            if (!ok)
     98                break;
     99            size_t size;
     100            ok = decoder.decode(size);
     101            if (!ok)
     102                break;
     103            Vector<uint8_t> buffer(size);
     104            ok = decoder.decodeFixedLengthData(buffer.data(), size);
     105            if (!ok)
     106                break;
     107            m_dictionaryStack.last()->add(key, WTFMove(buffer));
     108            break;
     109        }
     110        case KeyedEncoderGeneric::Type::Bool:
     111            ok = readSimpleValue<bool>(decoder, *m_dictionaryStack.last());
     112            break;
     113        case KeyedEncoderGeneric::Type::UInt32:
     114            ok = readSimpleValue<uint32_t>(decoder, *m_dictionaryStack.last());
     115            break;
     116        case KeyedEncoderGeneric::Type::UInt64:
     117            ok = readSimpleValue<uint64_t>(decoder, *m_dictionaryStack.last());
     118            break;
     119        case KeyedEncoderGeneric::Type::Int32:
     120            ok = readSimpleValue<int32_t>(decoder, *m_dictionaryStack.last());
     121            break;
     122        case KeyedEncoderGeneric::Type::Int64:
     123            ok = readSimpleValue<int64_t>(decoder, *m_dictionaryStack.last());
     124            break;
     125        case KeyedEncoderGeneric::Type::Float:
     126            ok = readSimpleValue<float>(decoder, *m_dictionaryStack.last());
     127            break;
     128        case KeyedEncoderGeneric::Type::Double:
     129            ok = readSimpleValue<double>(decoder, *m_dictionaryStack.last());
     130            break;
     131        case KeyedEncoderGeneric::Type::String: {
     132            ok = readString(decoder, key);
     133            if (!ok)
     134                break;
     135            String value;
     136            ok = readString(decoder, value);
     137            if (!ok)
     138                break;
     139            m_dictionaryStack.last()->add(key, WTFMove(value));
     140            break;
     141        }
     142        case KeyedEncoderGeneric::Type::BeginObject: {
     143            ok = readString(decoder, key);
     144            if (!ok)
     145                break;
     146            auto* currentDictinary = m_dictionaryStack.last();
     147            auto newDictionary = std::make_unique<Dictionary>();
     148            m_dictionaryStack.append(newDictionary.get());
     149            currentDictinary->add(key, WTFMove(newDictionary));
     150            break;
     151        }
     152        case KeyedEncoderGeneric::Type::EndObject:
     153            m_dictionaryStack.removeLast();
     154            break;
     155        case KeyedEncoderGeneric::Type::BeginArray: {
     156            ok = readString(decoder, key);
     157            if (!ok)
     158                break;
     159            auto newArray = std::make_unique<Array>();
     160            m_arrayStack.append(newArray.get());
     161            m_dictionaryStack.last()->add(key, WTFMove(newArray));
     162            break;
     163        }
     164        case KeyedEncoderGeneric::Type::BeginArrayElement: {
     165            auto newDictionary = std::make_unique<Dictionary>();
     166            m_dictionaryStack.append(newDictionary.get());
     167            m_arrayStack.last()->append(WTFMove(newDictionary));
     168            break;
     169        }
     170        case KeyedEncoderGeneric::Type::EndArrayElement:
     171            m_dictionaryStack.removeLast();
     172            break;
     173        case KeyedEncoderGeneric::Type::EndArray:
     174            m_arrayStack.removeLast();
     175            break;
     176        }
     177    }
     178    while (m_dictionaryStack.size() > 1)
     179        m_dictionaryStack.removeLast();
     180    while (!m_arrayStack.isEmpty())
     181        m_arrayStack.removeLast();
     182}
     183
     184bool KeyedDecoderGeneric::decodeBytes(const String& key, const uint8_t*& data, size_t& size)
     185{
     186    auto* value = WTF::get_if<Vector<uint8_t>>(m_dictionaryStack.last()->get(key));
     187    if (!value)
     188        return false;
     189    data = value->data();
     190    size = value->size();
     191    return true;
     192}
     193
     194bool KeyedDecoderGeneric::decodeBool(const String& key, bool& result)
     195{
     196    auto* value = WTF::get_if<bool>(m_dictionaryStack.last()->get(key));
     197    if (!value)
     198        return false;
     199    result = *value;
     200    return true;
     201}
     202
     203bool KeyedDecoderGeneric::decodeUInt32(const String& key, uint32_t& result)
     204{
     205    auto* value = WTF::get_if<uint32_t>(m_dictionaryStack.last()->get(key));
     206    if (!value)
     207        return false;
     208    result = *value;
     209    return true;
     210}
     211
     212bool KeyedDecoderGeneric::decodeUInt64(const String& key, uint64_t& result)
     213{
     214    auto* value = WTF::get_if<uint64_t>(m_dictionaryStack.last()->get(key));
     215    if (!value)
     216        return false;
     217    result = *value;
     218    return true;
     219}
     220
     221bool KeyedDecoderGeneric::decodeInt32(const String& key, int32_t& result)
     222{
     223    auto* value = WTF::get_if<int32_t>(m_dictionaryStack.last()->get(key));
     224    if (!value)
     225        return false;
     226    result = *value;
     227    return true;
     228}
     229
     230bool KeyedDecoderGeneric::decodeInt64(const String& key, int64_t& result)
     231{
     232    auto* value = WTF::get_if<int64_t>(m_dictionaryStack.last()->get(key));
     233    if (!value)
     234        return false;
     235    result = *value;
     236    return true;
     237}
     238
     239bool KeyedDecoderGeneric::decodeFloat(const String& key, float& result)
     240{
     241    auto* value = WTF::get_if<float>(m_dictionaryStack.last()->get(key));
     242    if (!value)
     243        return false;
     244    result = *value;
     245    return true;
     246}
     247
     248bool KeyedDecoderGeneric::decodeDouble(const String& key, double& result)
     249{
     250    auto* value = WTF::get_if<double>(m_dictionaryStack.last()->get(key));
     251    if (!value)
     252        return false;
     253    result = *value;
     254    return true;
     255}
     256
     257bool KeyedDecoderGeneric::decodeString(const String& key, String& result)
     258{
     259    auto* value = WTF::get_if<String>(m_dictionaryStack.last()->get(key));
     260    if (!value)
     261        return false;
     262    result = *value;
     263    return true;
     264}
     265
     266bool KeyedDecoderGeneric::beginObject(const String& key)
     267{
     268    auto* value = WTF::get_if<std::unique_ptr<Dictionary>>(m_dictionaryStack.last()->get(key));
     269    if (!value)
     270        return false;
     271    m_dictionaryStack.append(value->get());
     272    return true;
    93273}
    94274
    95275void KeyedDecoderGeneric::endObject()
    96276{
    97 }
    98 
    99 bool KeyedDecoderGeneric::beginArray(const String&)
    100 {
    101     return false;
     277    m_dictionaryStack.removeLast();
     278}
     279
     280bool KeyedDecoderGeneric::beginArray(const String& key)
     281{
     282    auto* value = WTF::get_if<std::unique_ptr<Array>>(m_dictionaryStack.last()->get(key));
     283    if (!value)
     284        return false;
     285    m_arrayStack.append(value->get());
     286    m_arrayIndexStack.append(0);
     287    return true;
    102288}
    103289
    104290bool KeyedDecoderGeneric::beginArrayElement()
    105291{
    106     return false;
     292    if (m_arrayIndexStack.last() >= m_arrayStack.last()->size())
     293        return false;
     294
     295    auto dictionary = m_arrayStack.last()->at(m_arrayIndexStack.last()++).get();
     296    m_dictionaryStack.append(dictionary);
     297    return true;
    107298}
    108299
    109300void KeyedDecoderGeneric::endArrayElement()
    110301{
     302    m_dictionaryStack.removeLast();
    111303}
    112304
    113305void KeyedDecoderGeneric::endArray()
    114306{
     307    m_arrayStack.removeLast();
     308    m_arrayIndexStack.removeLast();
    115309}
    116310
  • trunk/Source/WebCore/platform/generic/KeyedDecoderGeneric.h

    r238913 r244450  
    3333public:
    3434    KeyedDecoderGeneric(const uint8_t* data, size_t);
    35     ~KeyedDecoderGeneric() override;
     35
     36    class Dictionary;
     37    using Array = Vector<std::unique_ptr<Dictionary>>;
    3638
    3739private:
     
    5355    void endArrayElement() override;
    5456    void endArray() override;
     57
     58    std::unique_ptr<Dictionary> m_rootDictionary;
     59    Vector<Dictionary*, 16> m_dictionaryStack;
     60    Vector<Array*, 16> m_arrayStack;
     61    Vector<size_t, 16> m_arrayIndexStack;
    5562};
    5663
  • trunk/Source/WebCore/platform/generic/KeyedEncoderGeneric.cpp

    r238913 r244450  
    2828
    2929#include "SharedBuffer.h"
     30#include <wtf/persistence/PersistentEncoder.h>
    3031
    3132namespace WebCore {
    3233
    33 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=186410
    3434std::unique_ptr<KeyedEncoder> KeyedEncoder::encoder()
    3535{
     
    3737}
    3838
    39 KeyedEncoderGeneric::KeyedEncoderGeneric()
     39void KeyedEncoderGeneric::encodeString(const String& key)
    4040{
     41    auto utf8 = key.utf8();
     42    m_encoder << utf8.length();
     43    m_encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
    4144}
    4245
    43 KeyedEncoderGeneric::~KeyedEncoderGeneric()
     46void KeyedEncoderGeneric::encodeBytes(const String& key, const uint8_t* bytes, size_t size)
    4447{
     48    m_encoder << Type::Bytes;
     49    encodeString(key);
     50    m_encoder << size;
     51    m_encoder.encodeFixedLengthData(bytes, size);
    4552}
    4653
    47 void KeyedEncoderGeneric::encodeBytes(const String&, const uint8_t*, size_t)
     54void KeyedEncoderGeneric::encodeBool(const String& key, bool value)
    4855{
     56    m_encoder << Type::Bool;
     57    encodeString(key);
     58    m_encoder << value;
    4959}
    5060
    51 void KeyedEncoderGeneric::encodeBool(const String&, bool)
     61void KeyedEncoderGeneric::encodeUInt32(const String& key, uint32_t value)
    5262{
     63    m_encoder << Type::UInt32;
     64    encodeString(key);
     65    m_encoder << value;
    5366}
    5467
    55 void KeyedEncoderGeneric::encodeUInt32(const String&, uint32_t)
     68void KeyedEncoderGeneric::encodeUInt64(const String& key, uint64_t value)
    5669{
     70    m_encoder << Type::UInt64;
     71    encodeString(key);
     72    m_encoder << value;
    5773}
    5874
    59 void KeyedEncoderGeneric::encodeUInt64(const String&, uint64_t)
     75void KeyedEncoderGeneric::encodeInt32(const String& key, int32_t value)
    6076{
     77    m_encoder << Type::Int32;
     78    encodeString(key);
     79    m_encoder << value;
    6180}
    6281
    63 void KeyedEncoderGeneric::encodeInt32(const String&, int32_t)
     82void KeyedEncoderGeneric::encodeInt64(const String& key, int64_t value)
    6483{
     84    m_encoder << Type::Int64;
     85    encodeString(key);
     86    m_encoder << value;
    6587}
    6688
    67 void KeyedEncoderGeneric::encodeInt64(const String&, int64_t)
     89void KeyedEncoderGeneric::encodeFloat(const String& key, float value)
    6890{
     91    m_encoder << Type::Float;
     92    encodeString(key);
     93    m_encoder << value;
    6994}
    7095
    71 void KeyedEncoderGeneric::encodeFloat(const String&, float)
     96void KeyedEncoderGeneric::encodeDouble(const String& key, double value)
    7297{
     98    m_encoder << Type::Double;
     99    encodeString(key);
     100    m_encoder << value;
    73101}
    74102
    75 void KeyedEncoderGeneric::encodeDouble(const String&, double)
     103void KeyedEncoderGeneric::encodeString(const String& key, const String& value)
    76104{
     105    m_encoder << Type::String;
     106    encodeString(key);
     107    encodeString(value);
    77108}
    78109
    79 void KeyedEncoderGeneric::encodeString(const String&, const String&)
     110void KeyedEncoderGeneric::beginObject(const String& key)
    80111{
    81 }
    82 
    83 void KeyedEncoderGeneric::beginObject(const String&)
    84 {
     112    m_encoder << Type::BeginObject;
     113    encodeString(key);
    85114}
    86115
    87116void KeyedEncoderGeneric::endObject()
    88117{
     118    m_encoder << Type::EndObject;
    89119}
    90120
    91 void KeyedEncoderGeneric::beginArray(const String&)
     121void KeyedEncoderGeneric::beginArray(const String& key)
    92122{
     123    m_encoder << Type::BeginArray;
     124    encodeString(key);
    93125}
    94126
    95127void KeyedEncoderGeneric::beginArrayElement()
    96128{
     129    m_encoder << Type::BeginArrayElement;
    97130}
    98131
    99132void KeyedEncoderGeneric::endArrayElement()
    100133{
     134    m_encoder << Type::EndArrayElement;
    101135}
    102136
    103137void KeyedEncoderGeneric::endArray()
    104138{
     139    m_encoder << Type::EndArray;
    105140}
    106141
    107142RefPtr<SharedBuffer> KeyedEncoderGeneric::finishEncoding()
    108143{
    109     return nullptr;
     144    return SharedBuffer::create(m_encoder.buffer(), m_encoder.bufferSize());
    110145}
    111146
  • trunk/Source/WebCore/platform/generic/KeyedEncoderGeneric.h

    r238913 r244450  
    2727
    2828#include "KeyedCoding.h"
     29#include <wtf/Forward.h>
    2930#include <wtf/Vector.h>
     31#include <wtf/persistence/PersistentEncoder.h>
    3032#include <wtf/text/WTFString.h>
    3133
     
    3638class KeyedEncoderGeneric final : public KeyedEncoder {
    3739public:
    38     KeyedEncoderGeneric();
    39     ~KeyedEncoderGeneric();
     40
     41    enum class Type : uint8_t {
     42        Bytes,
     43        Bool,
     44        UInt32,
     45        UInt64,
     46        Int32,
     47        Int64,
     48        Float,
     49        Double,
     50        String,
     51        BeginObject,
     52        EndObject,
     53        BeginArray,
     54        BeginArrayElement,
     55        EndArrayElement,
     56        EndArray,
     57    };
    4058
    4159private:
     
    5977    void endArrayElement() override;
    6078    void endArray() override;
     79
     80    void encodeString(const String&);
     81
     82    WTF::Persistence::Encoder m_encoder;
    6183};
    6284
    6385} // namespace WebCore
     86
     87namespace WTF {
     88template<> struct EnumTraits<WebCore::KeyedEncoderGeneric::Type> {
     89    using values = EnumValues<
     90        WebCore::KeyedEncoderGeneric::Type,
     91        WebCore::KeyedEncoderGeneric::Type::Bytes,
     92        WebCore::KeyedEncoderGeneric::Type::Bool,
     93        WebCore::KeyedEncoderGeneric::Type::UInt32,
     94        WebCore::KeyedEncoderGeneric::Type::UInt64,
     95        WebCore::KeyedEncoderGeneric::Type::Int32,
     96        WebCore::KeyedEncoderGeneric::Type::Int64,
     97        WebCore::KeyedEncoderGeneric::Type::Float,
     98        WebCore::KeyedEncoderGeneric::Type::Double,
     99        WebCore::KeyedEncoderGeneric::Type::String,
     100        WebCore::KeyedEncoderGeneric::Type::BeginObject,
     101        WebCore::KeyedEncoderGeneric::Type::EndObject,
     102        WebCore::KeyedEncoderGeneric::Type::BeginArray,
     103        WebCore::KeyedEncoderGeneric::Type::BeginArrayElement,
     104        WebCore::KeyedEncoderGeneric::Type::EndArrayElement,
     105        WebCore::KeyedEncoderGeneric::Type::EndArray
     106    >;
     107};
     108
     109} // namespace WTF
Note: See TracChangeset for help on using the changeset viewer.