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

Side by Side Diff: Source/core/css/CSSValue.h

Issue 1164573002: CSSValue Immediates: Change CSSValue to an object instead of a pointer (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 6 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
1 /* 1 /*
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org) 2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * 4 *
5 * This library is free software; you can redistribute it and/or 5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public 6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either 7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version. 8 * version 2 of the License, or (at your option) any later version.
9 * 9 *
10 * This library is distributed in the hope that it will be useful, 10 * This library is distributed in the hope that it will be useful,
(...skipping 13 matching lines...) Expand all
24 #include "core/CoreExport.h" 24 #include "core/CoreExport.h"
25 #include "platform/heap/Handle.h" 25 #include "platform/heap/Handle.h"
26 #include "platform/weborigin/KURL.h" 26 #include "platform/weborigin/KURL.h"
27 #include "wtf/HashMap.h" 27 #include "wtf/HashMap.h"
28 #include "wtf/ListHashSet.h" 28 #include "wtf/ListHashSet.h"
29 #include "wtf/RefCounted.h" 29 #include "wtf/RefCounted.h"
30 #include "wtf/RefPtr.h" 30 #include "wtf/RefPtr.h"
31 31
32 namespace blink { 32 namespace blink {
33 33
34 class CORE_EXPORT CSSValue : public RefCountedWillBeGarbageCollectedFinalized<CS SValue> { 34 class CORE_EXPORT CSSValueObject : public RefCountedWillBeGarbageCollectedFinali zed<CSSValueObject> {
35 public: 35 public:
36 #if ENABLE(OILPAN) 36 #if ENABLE(OILPAN)
37 // Override operator new to allocate CSSValue subtype objects onto 37 // Override operator new to allocate CSSValueObject subtype objects onto
38 // a dedicated heap. 38 // a dedicated heap.
39 GC_PLUGIN_IGNORE("crbug.com/443854") 39 GC_PLUGIN_IGNORE("crbug.com/443854")
40 void* operator new(size_t size) 40 void* operator new(size_t size)
41 { 41 {
42 return allocateObject(size); 42 return allocateObject(size);
43 } 43 }
44 static void* allocateObject(size_t size) 44 static void* allocateObject(size_t size)
45 { 45 {
46 ThreadState* state = ThreadStateFor<ThreadingTrait<CSSValue>::Affinity>: :state(); 46 ThreadState* state = ThreadStateFor<ThreadingTrait<CSSValueObject>::Affi nity>::state();
47 return Heap::allocateOnHeapIndex(state, size, CSSValueHeapIndex, GCInfoT rait<CSSValue>::index()); 47 return Heap::allocateOnHeapIndex(state, size, CSSValueHeapIndex, GCInfoT rait<CSSValueObject>::index());
48 } 48 }
49 #else 49 #else
50 // Override RefCounted's deref() to ensure operator delete is called on 50 // Override RefCounted's deref() to ensure operator delete is called on
51 // the appropriate subclass type. 51 // the appropriate subclass type.
52 void deref() 52 void deref()
53 { 53 {
54 if (derefBase()) 54 if (derefBase())
55 destroy(); 55 destroy();
56 } 56 }
57 #endif // !ENABLE(OILPAN) 57 #endif // !ENABLE(OILPAN)
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 bool isLineBoxContainValue() const { return m_classType == LineBoxContainCla ss; } 89 bool isLineBoxContainValue() const { return m_classType == LineBoxContainCla ss; }
90 bool isCalcValue() const {return m_classType == CalculationClass; } 90 bool isCalcValue() const {return m_classType == CalculationClass; }
91 bool isGridTemplateAreasValue() const { return m_classType == GridTemplateAr easClass; } 91 bool isGridTemplateAreasValue() const { return m_classType == GridTemplateAr easClass; }
92 bool isSVGDocumentValue() const { return m_classType == CSSSVGDocumentClass; } 92 bool isSVGDocumentValue() const { return m_classType == CSSSVGDocumentClass; }
93 bool isContentDistributionValue() const { return m_classType == CSSContentDi stributionClass; } 93 bool isContentDistributionValue() const { return m_classType == CSSContentDi stributionClass; }
94 bool isUnicodeRangeValue() const { return m_classType == UnicodeRangeClass; } 94 bool isUnicodeRangeValue() const { return m_classType == UnicodeRangeClass; }
95 bool isGridLineNamesValue() const { return m_classType == GridLineNamesClass ; } 95 bool isGridLineNamesValue() const { return m_classType == GridLineNamesClass ; }
96 96
97 bool hasFailedOrCanceledSubresources() const; 97 bool hasFailedOrCanceledSubresources() const;
98 98
99 bool equals(const CSSValue&) const; 99 bool equals(const CSSValueObject&) const;
100 100
101 void finalizeGarbageCollectedObject(); 101 void finalizeGarbageCollectedObject();
102 DEFINE_INLINE_TRACE_AFTER_DISPATCH() { } 102 DEFINE_INLINE_TRACE_AFTER_DISPATCH() { }
103 DECLARE_TRACE(); 103 DECLARE_TRACE();
104 104
105 // ~CSSValue should be public, because non-public ~CSSValue causes C2248 105 // ~CSSValueObject should be public, because non-public ~CSSValueObject caus es C2248
106 // error: 'blink::CSSValue::~CSSValue' : cannot access protected member 106 // error: 'blink::CSSValueObject::~CSSValueObject' : cannot access protected member
107 // declared in class 'blink::CSSValue' when compiling 107 // declared in class 'blink::CSSValueObject' when compiling
108 // 'source\wtf\refcounted.h' by using msvc. 108 // 'source\wtf\refcounted.h' by using msvc.
109 ~CSSValue() { } 109 ~CSSValueObject() { }
110 110
111 protected: 111 protected:
112 112
113 static const size_t ClassTypeBits = 6; 113 static const size_t ClassTypeBits = 6;
114 enum ClassType { 114 enum ClassType {
115 PrimitiveClass, 115 PrimitiveClass,
116 116
117 // Image classes. 117 // Image classes.
118 ImageClass, 118 ImageClass,
119 CursorImageClass, 119 CursorImageClass,
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 160
161 static const size_t ValueListSeparatorBits = 2; 161 static const size_t ValueListSeparatorBits = 2;
162 enum ValueListSeparator { 162 enum ValueListSeparator {
163 SpaceSeparator, 163 SpaceSeparator,
164 CommaSeparator, 164 CommaSeparator,
165 SlashSeparator 165 SlashSeparator
166 }; 166 };
167 167
168 ClassType classType() const { return static_cast<ClassType>(m_classType); } 168 ClassType classType() const { return static_cast<ClassType>(m_classType); }
169 169
170 explicit CSSValue(ClassType classType) 170 explicit CSSValueObject(ClassType classType)
171 : m_primitiveUnitType(0) 171 : m_primitiveUnitType(0)
172 , m_hasCachedCSSText(false) 172 , m_hasCachedCSSText(false)
173 , m_isQuirkValue(false) 173 , m_isQuirkValue(false)
174 , m_valueListSeparator(SpaceSeparator) 174 , m_valueListSeparator(SpaceSeparator)
175 , m_classType(classType) 175 , m_classType(classType)
176 { 176 {
177 } 177 }
178 178
179 // NOTE: This class is non-virtual for memory and performance reasons. 179 // NOTE: This class is non-virtual for memory and performance reasons.
180 // Don't go making it virtual again unless you know exactly what you're doin g! 180 // Don't go making it virtual again unless you know exactly what you're doin g!
181 181
182 private: 182 private:
183 void destroy(); 183 void destroy();
184 184
185 protected: 185 protected:
186 // The bits in this section are only used by specific subclasses but kept he re 186 // The bits in this section are only used by specific subclasses but kept he re
187 // to maximize struct packing. 187 // to maximize struct packing.
188 188
189 // CSSPrimitiveValue bits: 189 // CSSPrimitiveValue bits:
190 unsigned m_primitiveUnitType : 7; // CSSPrimitiveValue::UnitType 190 unsigned m_primitiveUnitType : 7; // CSSPrimitiveValue::UnitType
191 mutable unsigned m_hasCachedCSSText : 1; 191 mutable unsigned m_hasCachedCSSText : 1;
192 unsigned m_isQuirkValue : 1; 192 unsigned m_isQuirkValue : 1;
193 193
194 unsigned m_valueListSeparator : ValueListSeparatorBits; 194 unsigned m_valueListSeparator : ValueListSeparatorBits;
195 195
196 private: 196 private:
197 unsigned m_classType : ClassTypeBits; // ClassType 197 unsigned m_classType : ClassTypeBits; // ClassType
198 }; 198 };
199 199
200 template<typename CSSValueType, size_t inlineCapacity> 200 // A non-nullable container for CSSValueObject. Takes ownership of the passed
201 inline bool compareCSSValueVector(const WillBeHeapVector<RefPtrWillBeMember<CSSV alueType>, inlineCapacity>& firstVector, const WillBeHeapVector<RefPtrWillBeMemb er<CSSValueType>, inlineCapacity>& secondVector) 201 // CSSValueObject.
202 class CORE_EXPORT CSSValue : public NoBaseWillBeGarbageCollectedFinalized<CSSVal ue> {
203 friend class NullableCSSValue;
204 public:
205 // Not explicit to allow for casting.
206 CSSValue(const CSSValueObject* cssValueObject)
207 : m_data(const_cast<CSSValueObject*>(cssValueObject))
208 {
209 ASSERT(m_data);
210 m_data->ref();
211 }
212
213 template<class T> CSSValue(PassRefPtrWillBeRawPtr<T> cssValueObject)
214 : m_data(cssValueObject.leakRef())
215 {
216 ASSERT(m_data);
217 m_data->ref();
218 }
219
220 template <class T> CSSValue(RefPtrWillBeRawPtr<T> cssValueObject)
221 : m_data(cssValueObject.release().leakRef())
222 {
223 ASSERT(m_data);
224 m_data->ref();
225 }
226
227 CSSValue(CSSValue& cssValue)
228 : m_data(cssValue.m_data)
229 {
230 m_data->ref();
231 }
232
233 CSSValue(const CSSValue& cssValue)
234 : m_data(cssValue.m_data)
235 {
236 m_data->ref();
237 }
238
239 ~CSSValue()
240 {
241 m_data->deref();
242 }
243
244 CSSValue& operator=(const CSSValue& rhs)
245 {
246 m_data->deref();
247 rhs.m_data->ref();
248 m_data = rhs.m_data;
249 return *this;
250 }
251
252 bool operator==(const CSSValue& other) const
253 {
254 return m_data->equals(*other.m_data);
255 }
256
257 // TODO: Remove this and update callsites to use operator== instead.
258 bool equals(const CSSValue& other) const
259 {
260 return m_data->equals(*other.m_data);
261 }
262
263 bool ptrEquals(const CSSValue& other) const
264 {
265 return m_data == other.m_data;
266 }
267
268 // Methods that proxy to CSSValueObject.
269 String cssText() const { return m_data->cssText(); }
270 bool hasFailedOrCanceledSubresources() const { return m_data->hasFailedOrCan celedSubresources(); };
271 bool isPrimitiveValue() const { return m_data->isPrimitiveValue(); }
272 bool isValueList() const { return m_data->isValueList(); }
273 bool isBaseValueList() const { return m_data->isBaseValueList(); }
274 bool isBorderImageSliceValue() const { return m_data->isBorderImageSliceValu e(); }
275 bool isCanvasValue() const { return m_data->isCanvasValue(); }
276 bool isCursorImageValue() const { return m_data->isCursorImageValue(); }
277 bool isCrossfadeValue() const { return m_data->isCrossfadeValue(); }
278 bool isFontFeatureValue() const { return m_data->isFontFeatureValue(); }
279 bool isFontFaceSrcValue() const { return m_data->isFontFaceSrcValue(); }
280 bool isFunctionValue() const { return m_data->isFunctionValue(); }
281 bool isImageGeneratorValue() const { return m_data->isImageGeneratorValue(); }
282 bool isGradientValue() const { return m_data->isGradientValue(); }
283 bool isImageSetValue() const { return m_data->isImageSetValue(); }
284 bool isImageValue() const { return m_data->isImageValue(); }
285 bool isImplicitInitialValue() const { return m_data->isImplicitInitialValue( ); };
286 bool isInheritedValue() const { return m_data->isInheritedValue(); };
287 bool isInitialValue() const { return m_data->isInitialValue(); };
288 bool isUnsetValue() const { return m_data->isUnsetValue(); };
289 bool isCSSWideKeyword() const { return m_data->isCSSWideKeyword(); };
290 bool isLinearGradientValue() const { return m_data->isLinearGradientValue(); };
291 bool isPathValue() const { return m_data->isPathValue(); };
292 bool isRadialGradientValue() const { return m_data->isRadialGradientValue(); };
293 bool isReflectValue() const { return m_data->isReflectValue(); };
294 bool isShadowValue() const { return m_data->isShadowValue(); };
295 bool isCubicBezierTimingFunctionValue() const { return m_data->isCubicBezier TimingFunctionValue(); };
296 bool isStepsTimingFunctionValue() const { return m_data->isStepsTimingFuncti onValue(); };
297 bool isLineBoxContainValue() const { return m_data->isLineBoxContainValue(); };
298 bool isCalcValue() const { return m_data->isCalcValue(); };
299 bool isGridTemplateAreasValue() const { return m_data->isGridTemplateAreasVa lue(); };
300 bool isSVGDocumentValue() const { return m_data->isSVGDocumentValue(); };
301 bool isContentDistributionValue() const { return m_data->isContentDistributi onValue(); };
302 bool isUnicodeRangeValue() const { return m_data->isUnicodeRangeValue(); };
303 bool isGridLineNamesValue() const { return m_data->isGridLineNamesValue(); } ;
304
305 #if !ENABLE(OILPAN)
306 void ref()
307 {
308 m_data->ref();
309 }
310 void deref()
311 {
312 m_data->deref();
313 }
314 #endif
315
316 DECLARE_TRACE();
317
318 private:
319 // To ensure these are never called. Without this, the compiler may accident ally call
320 // CSSValue(nullptr) with the CSSValueObject* constructor to store an interm ediate value.
321 // e.g. NullableCSSValue n = x ? CSSValue(foo) : nullptr;
322 CSSValue() { ASSERT_NOT_REACHED(); }
Timothy Loh 2015/06/02 00:42:03 = delete?
323 CSSValue(std::nullptr_t null) { ASSERT_NOT_REACHED(); }
Timothy Loh 2015/06/02 00:42:03 = delete?
324
325 CSSValueObject* m_data;
326 };
327
328 // A nullable container for CSSValueObject. Contents are the same as CSSValue.
329 // Behavior is similar to a CSSValue*.
330 class CORE_EXPORT NullableCSSValue : public NoBaseWillBeGarbageCollectedFinalize d<NullableCSSValue> {
331 public:
332 // Not explicit to allow for casting.
333 NullableCSSValue()
334 : m_data(nullptr)
335 {
336 };
337
338 NullableCSSValue(std::nullptr_t null)
339 : m_data(nullptr)
340 {
341 };
342
343 NullableCSSValue(const CSSValueObject* cssValueObject)
344 : m_data(const_cast<CSSValueObject*>(cssValueObject))
345 {
346 if (m_data)
347 m_data->ref();
348 }
349
350 template <class T>
351 NullableCSSValue(PassRefPtrWillBeRawPtr<T> cssValueObject)
352 : m_data(cssValueObject.leakRef())
353 {
354 if (m_data)
355 m_data->ref();
356 }
357
358 template <class T>
359 NullableCSSValue(RefPtrWillBeRawPtr<T> cssValueObject)
360 : m_data(cssValueObject.release().leakRef())
361 {
362 if (m_data)
363 m_data->ref();
364 }
365
366 NullableCSSValue(NullableCSSValue& cssValue)
367 : m_data(cssValue.m_data)
368 {
369 if (m_data)
370 m_data->ref();
371 }
372
373 NullableCSSValue(const NullableCSSValue& cssValue)
374 : m_data(cssValue.m_data)
375 {
376 if (m_data)
377 m_data->ref();
378 }
379
380 // CSSValues can be 'auto-cast' to NullableCSSValues, but not the other way around.
381 NullableCSSValue(CSSValue& cssValue)
382 : m_data(cssValue.m_data)
383 {
384 m_data->ref();
385 }
386
387 NullableCSSValue(const CSSValue& cssValue)
388 : m_data(cssValue.m_data)
389 {
390 m_data->ref();
391 }
392
393 ~NullableCSSValue()
394 {
395 if (m_data)
396 m_data->deref();
397 };
398
399 operator bool() const
400 {
401 return m_data;
402 }
403
404 NullableCSSValue& operator=(const NullableCSSValue& rhs)
405 {
406 if (m_data)
407 m_data->deref();
408 if (rhs.m_data)
409 rhs.m_data->ref();
410 m_data = rhs.m_data;
411 return *this;
412 }
413
414 bool operator==(const NullableCSSValue& rhs)
415 {
416 return m_data ? rhs.m_data && m_data->equals(*rhs.m_data) : bool(rhs.m_d ata);
417 }
418
419 CSSValue& operator*()
420 {
421 return *reinterpret_cast<CSSValue*>(this);
422 }
423
424 const CSSValue& operator*() const
425 {
426 return *reinterpret_cast<const CSSValue*>(this);
427 }
428
429 CSSValue* operator->()
430 {
431 return reinterpret_cast<CSSValue*>(this);
432 }
433
434 const CSSValue* operator->() const
435 {
436 return reinterpret_cast<const CSSValue*>(this);
437 }
438
439 void swap(NullableCSSValue& rhs)
440 {
441 std::swap(this->m_data, rhs.m_data);
442 }
443
444 DECLARE_TRACE();
445
446 private:
447 CSSValueObject* m_data;
448 };
449
450 static_assert(sizeof(CSSValue) == sizeof(CSSValueObject*), "CSSValue must only c ontain a CSSValueObject*, and no vtable");
451 static_assert(sizeof(NullableCSSValue) == sizeof(CSSValueObject*), "NullableCSSV alue must only contain a CSSValueObject*, and no vtable");
452 static_assert(sizeof(CSSValue) == sizeof(NullableCSSValue), "Both CSSValue conta iners must contain the same data members");
453
454 template<size_t inlineCapacity>
455 inline bool compareCSSValueVector(const WillBeHeapVector<CSSValue, inlineCapacit y>& firstVector, const WillBeHeapVector<CSSValue, inlineCapacity>& secondVector)
202 { 456 {
203 size_t size = firstVector.size(); 457 size_t size = firstVector.size();
204 if (size != secondVector.size()) 458 if (size != secondVector.size())
205 return false; 459 return false;
206 460
207 for (size_t i = 0; i < size; i++) { 461 for (size_t i = 0; i < size; i++) {
208 const RefPtrWillBeMember<CSSValueType>& firstPtr = firstVector[i]; 462 const CSSValue& firstPtr = firstVector[i];
209 const RefPtrWillBeMember<CSSValueType>& secondPtr = secondVector[i]; 463 const CSSValue& secondPtr = secondVector[i];
464 if (firstPtr.ptrEquals(secondPtr) || firstPtr.equals(secondPtr))
465 continue;
466 return false;
467 }
468 return true;
469 }
470
471 // TODO: Rename these to CompareCSSValueObjectPtr or make them take a CSSValue
472 template<typename CSSValueObjectType, size_t inlineCapacity>
473 inline bool compareCSSValueObjectVector(const WillBeHeapVector<RefPtrWillBeMembe r<CSSValueObjectType>, inlineCapacity>& firstVector, const WillBeHeapVector<RefP trWillBeMember<CSSValueObjectType>, inlineCapacity>& secondVector)
474 {
475 size_t size = firstVector.size();
476 if (size != secondVector.size())
477 return false;
478
479 for (size_t i = 0; i < size; i++) {
480 const RefPtrWillBeMember<CSSValueObjectType>& firstPtr = firstVector[i];
481 const RefPtrWillBeMember<CSSValueObjectType>& secondPtr = secondVector[i ];
210 if (firstPtr == secondPtr || (firstPtr && secondPtr && firstPtr->equals( *secondPtr))) 482 if (firstPtr == secondPtr || (firstPtr && secondPtr && firstPtr->equals( *secondPtr)))
211 continue; 483 continue;
212 return false; 484 return false;
213 } 485 }
214 return true; 486 return true;
215 } 487 }
216 488
217 template<typename CSSValueType> 489 template<typename CSSValueType>
218 inline bool compareCSSValuePtr(const RefPtr<CSSValueType>& first, const RefPtr<C SSValueType>& second) 490 inline bool compareCSSValuePtr(const RefPtr<CSSValueType>& first, const RefPtr<C SSValueType>& second)
219 { 491 {
220 return first ? second && first->equals(*second) : !second; 492 return first ? second && first->equals(*second) : !second;
221 } 493 }
222 494
223 template<typename CSSValueType> 495 template<typename CSSValueType>
224 inline bool compareCSSValuePtr(const RawPtr<CSSValueType>& first, const RawPtr<C SSValueType>& second) 496 inline bool compareCSSValuePtr(const RawPtr<CSSValueType>& first, const RawPtr<C SSValueType>& second)
225 { 497 {
226 return first ? second && first->equals(*second) : !second; 498 return first ? second && first->equals(*second) : !second;
227 } 499 }
228 500
229 template<typename CSSValueType> 501 template<typename CSSValueType>
230 inline bool compareCSSValuePtr(const Member<CSSValueType>& first, const Member<C SSValueType>& second) 502 inline bool compareCSSValuePtr(const Member<CSSValueType>& first, const Member<C SSValueType>& second)
231 { 503 {
232 return first ? second && first->equals(*second) : !second; 504 return first ? second && first->equals(*second) : !second;
233 } 505 }
234 506
235 #define DEFINE_CSS_VALUE_TYPE_CASTS(thisType, predicate) \ 507 #define DEFINE_CSS_VALUE_TYPE_CASTS(thisType, predicate) \
236 DEFINE_TYPE_CASTS(thisType, CSSValue, value, value->predicate, value.predica te) 508 DEFINE_TYPE_CASTS(thisType, CSSValueObject, value, value->predicate, value.p redicate); \
509 inline thisType& to##thisType(CSSValue value) \
510 { \
511 ASSERT_WITH_SECURITY_IMPLICATION(value.predicate); \
512 return **reinterpret_cast<thisType**>(&value); \
513 } \
514 inline thisType* to##thisType(NullableCSSValue value) \
515 { \
516 ASSERT_WITH_SECURITY_IMPLICATION(value->predicate); \
517 return *reinterpret_cast<thisType**>(&value); \
518 }
237 519
238 } // namespace blink 520 } // namespace blink
239 521
240 #endif // CSSValue_h 522 #endif // CSSValue_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698