Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | // |
michael@0 | 2 | // SkTRefArray.h |
michael@0 | 3 | // core |
michael@0 | 4 | // |
michael@0 | 5 | // Created by Mike Reed on 7/17/12. |
michael@0 | 6 | // Copyright (c) 2012 __MyCompanyName__. All rights reserved. |
michael@0 | 7 | // |
michael@0 | 8 | |
michael@0 | 9 | #ifndef SkTRefArray_DEFINED |
michael@0 | 10 | #define SkTRefArray_DEFINED |
michael@0 | 11 | |
michael@0 | 12 | #include "SkRefCnt.h" |
michael@0 | 13 | #include <new> |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * Wrapper to manage thread-safe sharing of an array of T objects. The array |
michael@0 | 17 | * cannot be grown or shrunk. |
michael@0 | 18 | */ |
michael@0 | 19 | template <typename T> class SkTRefArray : public SkRefCnt { |
michael@0 | 20 | /* |
michael@0 | 21 | * Shared factory to allocate the space needed for our instance plus N |
michael@0 | 22 | * T entries at the end. We call our constructor, but not the constructors |
michael@0 | 23 | * for the elements. Those are called by the proper Create method. |
michael@0 | 24 | */ |
michael@0 | 25 | static SkTRefArray<T>* Alloc(int count) { |
michael@0 | 26 | // space for us, and our [count] elements |
michael@0 | 27 | size_t size = sizeof(SkTRefArray<T>) + count * sizeof(T); |
michael@0 | 28 | SkTRefArray<T>* obj = (SkTRefArray<T>*)sk_malloc_throw(size); |
michael@0 | 29 | |
michael@0 | 30 | SkNEW_PLACEMENT(obj, SkTRefArray<T>); |
michael@0 | 31 | obj->fCount = count; |
michael@0 | 32 | return obj; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | public: |
michael@0 | 36 | /** |
michael@0 | 37 | * Return a new array with 'count' elements, initialized to their default |
michael@0 | 38 | * value. To change them to some other value, use writableBegin/End or |
michael@0 | 39 | * writableAt(), but do that before this array is given to another thread. |
michael@0 | 40 | */ |
michael@0 | 41 | static SkTRefArray<T>* Create(int count) { |
michael@0 | 42 | SkTRefArray<T>* obj = Alloc(count); |
michael@0 | 43 | T* array = const_cast<T*>(obj->begin()); |
michael@0 | 44 | for (int i = 0; i < count; ++i) { |
michael@0 | 45 | SkNEW_PLACEMENT(&array[i], T); |
michael@0 | 46 | } |
michael@0 | 47 | return obj; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | /** |
michael@0 | 51 | * Return a new array with 'count' elements, initialized from the provided |
michael@0 | 52 | * src array. To change them to some other value, use writableBegin/End or |
michael@0 | 53 | * writableAt(), but do that before this array is given to another thread. |
michael@0 | 54 | */ |
michael@0 | 55 | static SkTRefArray<T>* Create(const T src[], int count) { |
michael@0 | 56 | SkTRefArray<T>* obj = Alloc(count); |
michael@0 | 57 | T* array = const_cast<T*>(obj->begin()); |
michael@0 | 58 | for (int i = 0; i < count; ++i) { |
michael@0 | 59 | SkNEW_PLACEMENT_ARGS(&array[i], T, (src[i])); |
michael@0 | 60 | } |
michael@0 | 61 | return obj; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | int count() const { return fCount; } |
michael@0 | 65 | const T* begin() const { return (const T*)(this + 1); } |
michael@0 | 66 | const T* end() const { return this->begin() + fCount; } |
michael@0 | 67 | const T& at(int index) const { |
michael@0 | 68 | SkASSERT((unsigned)index < (unsigned)fCount); |
michael@0 | 69 | return this->begin()[index]; |
michael@0 | 70 | } |
michael@0 | 71 | const T& operator[](int index) const { return this->at(index); } |
michael@0 | 72 | |
michael@0 | 73 | // For the writable methods, we assert that we are the only owner if we |
michael@0 | 74 | // call these, since other owners are not informed if we change an element. |
michael@0 | 75 | |
michael@0 | 76 | T* writableBegin() { |
michael@0 | 77 | SkASSERT(this->unique()); |
michael@0 | 78 | return (T*)(this + 1); |
michael@0 | 79 | } |
michael@0 | 80 | T* writableEnd() { |
michael@0 | 81 | return this->writableBegin() + fCount; |
michael@0 | 82 | } |
michael@0 | 83 | T& writableAt(int index) { |
michael@0 | 84 | SkASSERT((unsigned)index < (unsigned)fCount); |
michael@0 | 85 | return this->writableBegin()[index]; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | protected: |
michael@0 | 89 | virtual void internal_dispose() const SK_OVERRIDE { |
michael@0 | 90 | T* array = const_cast<T*>(this->begin()); |
michael@0 | 91 | int n = fCount; |
michael@0 | 92 | |
michael@0 | 93 | for (int i = 0; i < n; ++i) { |
michael@0 | 94 | array->~T(); |
michael@0 | 95 | array += 1; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | this->internal_dispose_restore_refcnt_to_1(); |
michael@0 | 99 | this->~SkTRefArray<T>(); |
michael@0 | 100 | sk_free((void*)this); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | private: |
michael@0 | 104 | int fCount; |
michael@0 | 105 | |
michael@0 | 106 | // hide this |
michael@0 | 107 | virtual ~SkTRefArray() {} |
michael@0 | 108 | |
michael@0 | 109 | typedef SkRefCnt INHERITED; |
michael@0 | 110 | }; |
michael@0 | 111 | |
michael@0 | 112 | #endif |