Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: third_party/WebKit/Source/core/css/serializer/CSSDeserializeStream.cpp

Issue 1481383002: [Experimental] CSSSerializer Proof-of-concept Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: snapshot: top_25 sites ser/dser now works Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/CSSDeserializeStream.h"
6
7 #include "core/HTMLNames.h"
8 #include "core/css/CSSProperty.h"
9 #include "core/css/serializer/CSSSerializeStream.h" // FIXME: Remove
10 #include "platform/weborigin/Referrer.h"
11
12 namespace blink {
13
14 inline void CSSDeserializeStream::padIfNeeded()
15 {
16 uintptr_t misalign = reinterpret_cast<uintptr_t>(m_ptr) & 0x3;
17 if (!misalign)
18 return;
19 unsigned padding = 4 - static_cast<unsigned>(misalign);
20 CSDEBUG("padding %d\n", padding);
21 m_ptr += padding;
22 }
23
24 CSSDeserializeStream::CSSDeserializeStream(const char* data, size_t size)
25 : m_ptr(data)
26 , m_ptrEnd(data + size)
27 {
28 }
29
30 inline size_t CSSDeserializeStream::unconsumedSize() const
31 {
32 return m_ptrEnd - m_ptr;
33 }
34
35 CSSValue::ClassType CSSDeserializeStream::readClassType()
36 {
37 CSDEBUG("read classType ");
38 return static_cast<CSSValue::ClassType>(readUnsigned());
39 }
40
41 Color CSSDeserializeStream::readColor()
42 {
43 CSDEBUG("read color ");
44 return Color(static_cast<RGBA32>(readUnsigned()));
45 }
46
47 bool CSSDeserializeStream::readBool()
48 {
49 RELEASE_ASSERT(unconsumedSize() >= 1);
50
51 char c = *reinterpret_cast<const char*>(m_ptr);
52 m_ptr += 1;
53 CSDEBUG("read bool %d\n", c);
54
55 return c;
56 }
57
58 int CSSDeserializeStream::readInt()
59 {
60 padIfNeeded();
61 RELEASE_ASSERT(unconsumedSize() >= sizeof(int));
62
63 int n = *reinterpret_cast<const int*>(m_ptr);
64 m_ptr += sizeof(int);
65 CSDEBUG("read int %d %x\n", n, n);
66
67 return n;
68 }
69
70 unsigned CSSDeserializeStream::readUnsigned()
71 {
72 padIfNeeded();
73 RELEASE_ASSERT(unconsumedSize() >= sizeof(unsigned));
74
75 unsigned u = *reinterpret_cast<const unsigned*>(m_ptr);
76 m_ptr += sizeof(unsigned);
77 CSDEBUG("read unsigned %u %x\n", u, u);
78
79 return u;
80 }
81
82 double CSSDeserializeStream::readDouble()
83 {
84 padIfNeeded();
85 RELEASE_ASSERT(unconsumedSize() >= sizeof(double));
86
87 double d = *reinterpret_cast<const double*>(m_ptr);
88 m_ptr += sizeof(double);
89 CSDEBUG("read double %f\n", d);
90
91 return d;
92 }
93
94 UChar32 CSSDeserializeStream::readUChar32()
95 {
96 padIfNeeded();
97 RELEASE_ASSERT(unconsumedSize() >= sizeof(UChar32));
98
99 UChar32 u = *reinterpret_cast<const UChar32*>(m_ptr);
100 m_ptr += sizeof(UChar32);
101 CSDEBUG("read UChar32 %u\n", u);
102
103 return u;
104 }
105
106 String CSSDeserializeStream::readString()
107 {
108 unsigned length = readUnsigned();
109 if (!length) {
110 CSDEBUG("read String <empty>\n");
111 return emptyString();
112 } else if (length == 0xffffffff) {
113 CSDEBUG("read String <null>\n");
114 return String();
115 }
116 bool is8Bit = readUnsigned();
117
118 if (is8Bit) {
119 RELEASE_ASSERT(unconsumedSize() >= length);
120 String ret(reinterpret_cast<const LChar*>(m_ptr), length);
121 m_ptr += length;
122
123 CSDEBUG("read String "); CSSHOW(ret);
124 return ret;
125 } else {
126 RELEASE_ASSERT(unconsumedSize() >= length);
127 String ret(reinterpret_cast<const UChar*>(m_ptr), length);
128 m_ptr += length * 2;
129
130 CSDEBUG("read String "); CSSHOW(ret);
131 return ret;
132 }
133 }
134
135 AtomicString CSSDeserializeStream::readAtomicString()
136 {
137 CSDEBUG("read AtomicString ");
138 String str = readString();
139 unsigned hash = readUnsigned();
140 if (hash)
141 str.impl()->setHash(hash);
142 return AtomicString(str);
143 }
144
145 static const HTMLQualifiedName** htmlTagsArray()
146 {
147 DEFINE_STATIC_LOCAL(OwnPtr<const HTMLQualifiedName*[]>, array, ());
148 if (!array) {
149 array = HTMLNames::getHTMLTags();
150 }
151 return array.get();
152 }
153
154 QualifiedName CSSDeserializeStream::readQualifiedName()
155 {
156 CSDEBUG("read QualifiedName ");
157
158 int id = readInt();
159 if (id >= 0) {
160 return *htmlTagsArray()[id];
161 }
162
163 AtomicString namespaceURI = starAtom;
164 AtomicString localName(readAtomicString());
165
166 return QualifiedName(nullAtom, localName, namespaceURI);
167 }
168
169 StylePropertyMetadata CSSDeserializeStream::readStylePropertyMetadata()
170 {
171 CSDEBUG("read StylePropertyMetadata ");
172 unsigned u = readUnsigned();
173 return *reinterpret_cast<StylePropertyMetadata*>(&u);
174 }
175
176 Referrer CSSDeserializeStream::readReferrer()
177 {
178 Referrer referrer;
179 referrer.referrer = readAtomicString();
180 referrer.referrerPolicy = readIntAsEnum<ReferrerPolicy>();
181 return referrer;
182 }
183
184 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698