| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/css/serializer/CSSSerializeStream.h" |
| 6 |
| 7 #include "core/HTMLNames.h" |
| 8 #include "core/css/CSSProperty.h" |
| 9 #include "platform/weborigin/Referrer.h" |
| 10 |
| 11 namespace blink { |
| 12 |
| 13 inline void CSSSerializeStream::padIfNeeded() |
| 14 { |
| 15 uintptr_t misalign = m_buffer.size() & 0x3; |
| 16 if (misalign) { |
| 17 unsigned padding = 4 - static_cast<unsigned>(misalign); |
| 18 CSDEBUG("padding %d\n", padding); |
| 19 m_buffer.grow(m_buffer.size() + padding); |
| 20 } |
| 21 } |
| 22 |
| 23 void CSSSerializeStream::writeClassType(CSSValue::ClassType classType) |
| 24 { |
| 25 CSDEBUG("write classType "); |
| 26 writeUnsigned(classType); |
| 27 } |
| 28 |
| 29 void CSSSerializeStream::writeColor(Color color) |
| 30 { |
| 31 CSDEBUG("write color "); |
| 32 writeUnsigned(color.rgb()); |
| 33 } |
| 34 |
| 35 void CSSSerializeStream::writeBool(bool b) |
| 36 { |
| 37 CSDEBUG("write bool %d\n", b); |
| 38 m_buffer.append(reinterpret_cast<char*>(&b), 1); |
| 39 } |
| 40 |
| 41 void CSSSerializeStream::writeInt(int n) |
| 42 { |
| 43 CSDEBUG("write signed %d %x\n", n, n); |
| 44 padIfNeeded(); |
| 45 m_buffer.append(reinterpret_cast<char*>(&n), sizeof(int)); |
| 46 } |
| 47 |
| 48 void CSSSerializeStream::writeUnsigned(unsigned u) |
| 49 { |
| 50 CSDEBUG("write unsigned %u %x\n", u, u); |
| 51 padIfNeeded(); |
| 52 m_buffer.append(reinterpret_cast<char*>(&u), sizeof(unsigned)); |
| 53 } |
| 54 |
| 55 void CSSSerializeStream::writeDouble(double d) |
| 56 { |
| 57 CSDEBUG("write double %f\n", d); |
| 58 padIfNeeded(); |
| 59 m_buffer.append(reinterpret_cast<char*>(&d), sizeof(double)); |
| 60 } |
| 61 |
| 62 void CSSSerializeStream::writeUChar32(UChar32 u) |
| 63 { |
| 64 CSDEBUG("write UChar32 %u\n", u); |
| 65 padIfNeeded(); |
| 66 m_buffer.append(reinterpret_cast<char*>(&u), sizeof(UChar32)); |
| 67 } |
| 68 |
| 69 void CSSSerializeStream::writeString(const String& str) |
| 70 { |
| 71 // FIXME: emptyString vs. emptyString16??? |
| 72 CSDEBUG("write String "); |
| 73 CSSHOW(str); |
| 74 if (str.isNull()) { |
| 75 writeUnsigned(0xffffffff); |
| 76 return; |
| 77 } |
| 78 |
| 79 writeUnsigned(str.length()); |
| 80 if (str.length()) { |
| 81 writeUnsigned(str.is8Bit()); |
| 82 if (str.is8Bit()) { |
| 83 m_buffer.append(str.characters8(), str.length()); |
| 84 } else { |
| 85 m_buffer.append(str.characters16(), str.length() * 2); |
| 86 } |
| 87 } |
| 88 } |
| 89 |
| 90 void CSSSerializeStream::writeAtomicString(const AtomicString& str) |
| 91 { |
| 92 CSDEBUG("write AtomicString "); |
| 93 writeString(str.string()); |
| 94 writeUnsigned(str.impl() ? str.impl()->hash() : 0); |
| 95 } |
| 96 |
| 97 using QualifiedNameIntMap = HashMap<QualifiedName, int>; |
| 98 static const QualifiedNameIntMap& htmlTagNameToInt() |
| 99 { |
| 100 DEFINE_STATIC_LOCAL(QualifiedNameIntMap, map, ()); |
| 101 if (map.isEmpty()) { |
| 102 OwnPtr<const HTMLQualifiedName*[]> qualifiedNames = HTMLNames::getHTMLTa
gs(); |
| 103 for (size_t i = 0; i < HTMLNames::HTMLTagsCount; ++i) { |
| 104 map.set(*qualifiedNames[i], i); |
| 105 } |
| 106 } |
| 107 |
| 108 return map; |
| 109 } |
| 110 |
| 111 void CSSSerializeStream::writeQualifiedName(const QualifiedName& name) |
| 112 { |
| 113 // FIXME |
| 114 CSDEBUG("write QualifiedName "); |
| 115 |
| 116 const QualifiedNameIntMap& map = htmlTagNameToInt(); |
| 117 auto it = map.find(name); |
| 118 if (it != map.end()) { |
| 119 int id = it->value; |
| 120 CSDEBUG("Tag %d", id); |
| 121 writeInt(id); |
| 122 return; |
| 123 } |
| 124 |
| 125 writeInt(-1); |
| 126 RELEASE_ASSERT(!name.hasPrefix()); |
| 127 // name.namespaceURI().show(); |
| 128 // RELEASE_ASSERT(name.namespaceURI().isEmpty()); |
| 129 writeAtomicString(name.localName()); |
| 130 } |
| 131 |
| 132 void CSSSerializeStream::writeStylePropertyMetadata(StylePropertyMetadata metada
ta) |
| 133 { |
| 134 CSDEBUG("write StylePropertyMetadata "); |
| 135 static_assert(sizeof(StylePropertyMetadata) == sizeof(uint16_t), "uint16 hac
k is unapplicable"); |
| 136 writeUnsigned(*reinterpret_cast<uint16_t*>(&metadata)); |
| 137 } |
| 138 |
| 139 void CSSSerializeStream::writeReferrer(const Referrer& referrer) |
| 140 { |
| 141 writeAtomicString(referrer.referrer); |
| 142 writeInt(referrer.referrerPolicy); |
| 143 } |
| 144 |
| 145 } // namespace blink |
| OLD | NEW |