Changeset 244450 in webkit
- Timestamp:
- Apr 18, 2019 9:26:49 PM (5 years ago)
- Location:
- trunk/Source/WebCore
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebCore/ChangeLog
r244448 r244450 1 2019-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 1 21 2019-04-18 Ross Kirsling <ross.kirsling@sony.com> 2 22 -
trunk/Source/WebCore/PlatformWin.cmake
r244443 r244450 166 166 loader/archive/cf/LegacyWebArchive.cpp 167 167 168 platform/cf/KeyedDecoderCF.cpp169 platform/cf/KeyedEncoderCF.cpp170 168 platform/cf/SharedBufferCF.cpp 171 169 … … 185 183 else () 186 184 list(APPEND WebCore_SOURCES 185 platform/text/Hyphenation.cpp 186 ) 187 endif () 188 189 if (USE_CF AND NOT WTF_PLATFORM_WIN_CAIRO) 190 list(APPEND WebCore_SOURCES 191 platform/cf/KeyedDecoderCF.cpp 192 platform/cf/KeyedEncoderCF.cpp 193 ) 194 else () 195 list(APPEND WebCore_SOURCES 187 196 platform/generic/KeyedDecoderGeneric.cpp 188 197 platform/generic/KeyedEncoderGeneric.cpp 189 190 platform/text/Hyphenation.cpp191 198 ) 192 199 endif () -
trunk/Source/WebCore/platform/generic/KeyedDecoderGeneric.cpp
r238913 r244450 27 27 #include "KeyedDecoderGeneric.h" 28 28 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 29 36 namespace WebCore { 30 37 31 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=186410 38 class KeyedDecoderGeneric::Dictionary { 39 WTF_MAKE_FAST_ALLOCATED; 40 public: 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 47 private: 48 HashMap<String, std::unique_ptr<Node>> m_map; 49 }; 50 51 static 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 63 template<typename T> 64 static 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 32 78 std::unique_ptr<KeyedDecoder> KeyedDecoder::decoder(const uint8_t* data, size_t size) 33 79 { … … 35 81 } 36 82 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; 83 KeyedDecoderGeneric::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 184 bool 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 194 bool 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 203 bool 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 212 bool 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 221 bool 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 230 bool 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 239 bool 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 248 bool 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 257 bool 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 266 bool 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; 93 273 } 94 274 95 275 void KeyedDecoderGeneric::endObject() 96 276 { 97 } 98 99 bool KeyedDecoderGeneric::beginArray(const String&) 100 { 101 return false; 277 m_dictionaryStack.removeLast(); 278 } 279 280 bool 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; 102 288 } 103 289 104 290 bool KeyedDecoderGeneric::beginArrayElement() 105 291 { 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; 107 298 } 108 299 109 300 void KeyedDecoderGeneric::endArrayElement() 110 301 { 302 m_dictionaryStack.removeLast(); 111 303 } 112 304 113 305 void KeyedDecoderGeneric::endArray() 114 306 { 307 m_arrayStack.removeLast(); 308 m_arrayIndexStack.removeLast(); 115 309 } 116 310 -
trunk/Source/WebCore/platform/generic/KeyedDecoderGeneric.h
r238913 r244450 33 33 public: 34 34 KeyedDecoderGeneric(const uint8_t* data, size_t); 35 ~KeyedDecoderGeneric() override; 35 36 class Dictionary; 37 using Array = Vector<std::unique_ptr<Dictionary>>; 36 38 37 39 private: … … 53 55 void endArrayElement() override; 54 56 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; 55 62 }; 56 63 -
trunk/Source/WebCore/platform/generic/KeyedEncoderGeneric.cpp
r238913 r244450 28 28 29 29 #include "SharedBuffer.h" 30 #include <wtf/persistence/PersistentEncoder.h> 30 31 31 32 namespace WebCore { 32 33 33 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=18641034 34 std::unique_ptr<KeyedEncoder> KeyedEncoder::encoder() 35 35 { … … 37 37 } 38 38 39 KeyedEncoderGeneric::KeyedEncoderGeneric()39 void KeyedEncoderGeneric::encodeString(const String& key) 40 40 { 41 auto utf8 = key.utf8(); 42 m_encoder << utf8.length(); 43 m_encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length()); 41 44 } 42 45 43 KeyedEncoderGeneric::~KeyedEncoderGeneric()46 void KeyedEncoderGeneric::encodeBytes(const String& key, const uint8_t* bytes, size_t size) 44 47 { 48 m_encoder << Type::Bytes; 49 encodeString(key); 50 m_encoder << size; 51 m_encoder.encodeFixedLengthData(bytes, size); 45 52 } 46 53 47 void KeyedEncoderGeneric::encodeB ytes(const String&, const uint8_t*, size_t)54 void KeyedEncoderGeneric::encodeBool(const String& key, bool value) 48 55 { 56 m_encoder << Type::Bool; 57 encodeString(key); 58 m_encoder << value; 49 59 } 50 60 51 void KeyedEncoderGeneric::encode Bool(const String&, bool)61 void KeyedEncoderGeneric::encodeUInt32(const String& key, uint32_t value) 52 62 { 63 m_encoder << Type::UInt32; 64 encodeString(key); 65 m_encoder << value; 53 66 } 54 67 55 void KeyedEncoderGeneric::encodeUInt 32(const String&, uint32_t)68 void KeyedEncoderGeneric::encodeUInt64(const String& key, uint64_t value) 56 69 { 70 m_encoder << Type::UInt64; 71 encodeString(key); 72 m_encoder << value; 57 73 } 58 74 59 void KeyedEncoderGeneric::encode UInt64(const String&, uint64_t)75 void KeyedEncoderGeneric::encodeInt32(const String& key, int32_t value) 60 76 { 77 m_encoder << Type::Int32; 78 encodeString(key); 79 m_encoder << value; 61 80 } 62 81 63 void KeyedEncoderGeneric::encodeInt 32(const String&, int32_t)82 void KeyedEncoderGeneric::encodeInt64(const String& key, int64_t value) 64 83 { 84 m_encoder << Type::Int64; 85 encodeString(key); 86 m_encoder << value; 65 87 } 66 88 67 void KeyedEncoderGeneric::encode Int64(const String&, int64_t)89 void KeyedEncoderGeneric::encodeFloat(const String& key, float value) 68 90 { 91 m_encoder << Type::Float; 92 encodeString(key); 93 m_encoder << value; 69 94 } 70 95 71 void KeyedEncoderGeneric::encode Float(const String&, float)96 void KeyedEncoderGeneric::encodeDouble(const String& key, double value) 72 97 { 98 m_encoder << Type::Double; 99 encodeString(key); 100 m_encoder << value; 73 101 } 74 102 75 void KeyedEncoderGeneric::encode Double(const String&, double)103 void KeyedEncoderGeneric::encodeString(const String& key, const String& value) 76 104 { 105 m_encoder << Type::String; 106 encodeString(key); 107 encodeString(value); 77 108 } 78 109 79 void KeyedEncoderGeneric:: encodeString(const String&, const String&)110 void KeyedEncoderGeneric::beginObject(const String& key) 80 111 { 81 } 82 83 void KeyedEncoderGeneric::beginObject(const String&) 84 { 112 m_encoder << Type::BeginObject; 113 encodeString(key); 85 114 } 86 115 87 116 void KeyedEncoderGeneric::endObject() 88 117 { 118 m_encoder << Type::EndObject; 89 119 } 90 120 91 void KeyedEncoderGeneric::beginArray(const String& )121 void KeyedEncoderGeneric::beginArray(const String& key) 92 122 { 123 m_encoder << Type::BeginArray; 124 encodeString(key); 93 125 } 94 126 95 127 void KeyedEncoderGeneric::beginArrayElement() 96 128 { 129 m_encoder << Type::BeginArrayElement; 97 130 } 98 131 99 132 void KeyedEncoderGeneric::endArrayElement() 100 133 { 134 m_encoder << Type::EndArrayElement; 101 135 } 102 136 103 137 void KeyedEncoderGeneric::endArray() 104 138 { 139 m_encoder << Type::EndArray; 105 140 } 106 141 107 142 RefPtr<SharedBuffer> KeyedEncoderGeneric::finishEncoding() 108 143 { 109 return nullptr;144 return SharedBuffer::create(m_encoder.buffer(), m_encoder.bufferSize()); 110 145 } 111 146 -
trunk/Source/WebCore/platform/generic/KeyedEncoderGeneric.h
r238913 r244450 27 27 28 28 #include "KeyedCoding.h" 29 #include <wtf/Forward.h> 29 30 #include <wtf/Vector.h> 31 #include <wtf/persistence/PersistentEncoder.h> 30 32 #include <wtf/text/WTFString.h> 31 33 … … 36 38 class KeyedEncoderGeneric final : public KeyedEncoder { 37 39 public: 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 }; 40 58 41 59 private: … … 59 77 void endArrayElement() override; 60 78 void endArray() override; 79 80 void encodeString(const String&); 81 82 WTF::Persistence::Encoder m_encoder; 61 83 }; 62 84 63 85 } // namespace WebCore 86 87 namespace WTF { 88 template<> 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.