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 | /* |
michael@0 | 3 | * Copyright 2008 The Android Open Source Project |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | #ifndef SkWriter32_DEFINED |
michael@0 | 11 | #define SkWriter32_DEFINED |
michael@0 | 12 | |
michael@0 | 13 | #include "SkData.h" |
michael@0 | 14 | #include "SkMatrix.h" |
michael@0 | 15 | #include "SkPath.h" |
michael@0 | 16 | #include "SkPoint.h" |
michael@0 | 17 | #include "SkRRect.h" |
michael@0 | 18 | #include "SkRect.h" |
michael@0 | 19 | #include "SkRegion.h" |
michael@0 | 20 | #include "SkScalar.h" |
michael@0 | 21 | #include "SkStream.h" |
michael@0 | 22 | #include "SkTemplates.h" |
michael@0 | 23 | #include "SkTypes.h" |
michael@0 | 24 | |
michael@0 | 25 | class SkWriter32 : SkNoncopyable { |
michael@0 | 26 | public: |
michael@0 | 27 | /** |
michael@0 | 28 | * The caller can specify an initial block of storage, which the caller manages. |
michael@0 | 29 | * |
michael@0 | 30 | * SkWriter32 will try to back reserve and write calls with this external storage until the |
michael@0 | 31 | * first time an allocation doesn't fit. From then it will use dynamically allocated storage. |
michael@0 | 32 | * This used to be optional behavior, but pipe now relies on it. |
michael@0 | 33 | */ |
michael@0 | 34 | SkWriter32(void* external = NULL, size_t externalBytes = 0) { |
michael@0 | 35 | this->reset(external, externalBytes); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | // return the current offset (will always be a multiple of 4) |
michael@0 | 39 | size_t bytesWritten() const { return fUsed; } |
michael@0 | 40 | |
michael@0 | 41 | SK_ATTR_DEPRECATED("use bytesWritten") |
michael@0 | 42 | size_t size() const { return this->bytesWritten(); } |
michael@0 | 43 | |
michael@0 | 44 | void reset(void* external = NULL, size_t externalBytes = 0) { |
michael@0 | 45 | SkASSERT(SkIsAlign4((uintptr_t)external)); |
michael@0 | 46 | SkASSERT(SkIsAlign4(externalBytes)); |
michael@0 | 47 | |
michael@0 | 48 | fSnapshot.reset(NULL); |
michael@0 | 49 | fData = (uint8_t*)external; |
michael@0 | 50 | fCapacity = externalBytes; |
michael@0 | 51 | fUsed = 0; |
michael@0 | 52 | fExternal = external; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | // Returns the current buffer. |
michael@0 | 56 | // The pointer may be invalidated by any future write calls. |
michael@0 | 57 | const uint32_t* contiguousArray() const { |
michael@0 | 58 | return (uint32_t*)fData; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | // size MUST be multiple of 4 |
michael@0 | 62 | uint32_t* reserve(size_t size) { |
michael@0 | 63 | SkASSERT(SkAlign4(size) == size); |
michael@0 | 64 | size_t offset = fUsed; |
michael@0 | 65 | size_t totalRequired = fUsed + size; |
michael@0 | 66 | if (totalRequired > fCapacity) { |
michael@0 | 67 | this->growToAtLeast(totalRequired); |
michael@0 | 68 | } |
michael@0 | 69 | fUsed = totalRequired; |
michael@0 | 70 | return (uint32_t*)(fData + offset); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | /** |
michael@0 | 74 | * Read a T record at offset, which must be a multiple of 4. Only legal if the record |
michael@0 | 75 | * was written atomically using the write methods below. |
michael@0 | 76 | */ |
michael@0 | 77 | template<typename T> |
michael@0 | 78 | const T& readTAt(size_t offset) const { |
michael@0 | 79 | SkASSERT(SkAlign4(offset) == offset); |
michael@0 | 80 | SkASSERT(offset < fUsed); |
michael@0 | 81 | return *(T*)(fData + offset); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | /** |
michael@0 | 85 | * Overwrite a T record at offset, which must be a multiple of 4. Only legal if the record |
michael@0 | 86 | * was written atomically using the write methods below. |
michael@0 | 87 | */ |
michael@0 | 88 | template<typename T> |
michael@0 | 89 | void overwriteTAt(size_t offset, const T& value) { |
michael@0 | 90 | SkASSERT(SkAlign4(offset) == offset); |
michael@0 | 91 | SkASSERT(offset < fUsed); |
michael@0 | 92 | SkASSERT(fSnapshot.get() == NULL); |
michael@0 | 93 | *(T*)(fData + offset) = value; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | bool writeBool(bool value) { |
michael@0 | 97 | this->write32(value); |
michael@0 | 98 | return value; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | void writeInt(int32_t value) { |
michael@0 | 102 | this->write32(value); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | void write8(int32_t value) { |
michael@0 | 106 | *(int32_t*)this->reserve(sizeof(value)) = value & 0xFF; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | void write16(int32_t value) { |
michael@0 | 110 | *(int32_t*)this->reserve(sizeof(value)) = value & 0xFFFF; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | void write32(int32_t value) { |
michael@0 | 114 | *(int32_t*)this->reserve(sizeof(value)) = value; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | void writePtr(void* value) { |
michael@0 | 118 | *(void**)this->reserve(sizeof(value)) = value; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | void writeScalar(SkScalar value) { |
michael@0 | 122 | *(SkScalar*)this->reserve(sizeof(value)) = value; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | void writePoint(const SkPoint& pt) { |
michael@0 | 126 | *(SkPoint*)this->reserve(sizeof(pt)) = pt; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | void writeRect(const SkRect& rect) { |
michael@0 | 130 | *(SkRect*)this->reserve(sizeof(rect)) = rect; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | void writeIRect(const SkIRect& rect) { |
michael@0 | 134 | *(SkIRect*)this->reserve(sizeof(rect)) = rect; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | void writeRRect(const SkRRect& rrect) { |
michael@0 | 138 | rrect.writeToMemory(this->reserve(SkRRect::kSizeInMemory)); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | void writePath(const SkPath& path) { |
michael@0 | 142 | size_t size = path.writeToMemory(NULL); |
michael@0 | 143 | SkASSERT(SkAlign4(size) == size); |
michael@0 | 144 | path.writeToMemory(this->reserve(size)); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | void writeMatrix(const SkMatrix& matrix) { |
michael@0 | 148 | size_t size = matrix.writeToMemory(NULL); |
michael@0 | 149 | SkASSERT(SkAlign4(size) == size); |
michael@0 | 150 | matrix.writeToMemory(this->reserve(size)); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | void writeRegion(const SkRegion& rgn) { |
michael@0 | 154 | size_t size = rgn.writeToMemory(NULL); |
michael@0 | 155 | SkASSERT(SkAlign4(size) == size); |
michael@0 | 156 | rgn.writeToMemory(this->reserve(size)); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | // write count bytes (must be a multiple of 4) |
michael@0 | 160 | void writeMul4(const void* values, size_t size) { |
michael@0 | 161 | this->write(values, size); |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | /** |
michael@0 | 165 | * Write size bytes from values. size must be a multiple of 4, though |
michael@0 | 166 | * values need not be 4-byte aligned. |
michael@0 | 167 | */ |
michael@0 | 168 | void write(const void* values, size_t size) { |
michael@0 | 169 | SkASSERT(SkAlign4(size) == size); |
michael@0 | 170 | memcpy(this->reserve(size), values, size); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | /** |
michael@0 | 174 | * Reserve size bytes. Does not need to be 4 byte aligned. The remaining space (if any) will be |
michael@0 | 175 | * filled in with zeroes. |
michael@0 | 176 | */ |
michael@0 | 177 | uint32_t* reservePad(size_t size) { |
michael@0 | 178 | size_t alignedSize = SkAlign4(size); |
michael@0 | 179 | uint32_t* p = this->reserve(alignedSize); |
michael@0 | 180 | if (alignedSize != size) { |
michael@0 | 181 | SkASSERT(alignedSize >= 4); |
michael@0 | 182 | p[alignedSize / 4 - 1] = 0; |
michael@0 | 183 | } |
michael@0 | 184 | return p; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | /** |
michael@0 | 188 | * Write size bytes from src, and pad to 4 byte alignment with zeroes. |
michael@0 | 189 | */ |
michael@0 | 190 | void writePad(const void* src, size_t size) { |
michael@0 | 191 | memcpy(this->reservePad(size), src, size); |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | /** |
michael@0 | 195 | * Writes a string to the writer, which can be retrieved with |
michael@0 | 196 | * SkReader32::readString(). |
michael@0 | 197 | * The length can be specified, or if -1 is passed, it will be computed by |
michael@0 | 198 | * calling strlen(). The length must be < max size_t. |
michael@0 | 199 | * |
michael@0 | 200 | * If you write NULL, it will be read as "". |
michael@0 | 201 | */ |
michael@0 | 202 | void writeString(const char* str, size_t len = (size_t)-1); |
michael@0 | 203 | |
michael@0 | 204 | /** |
michael@0 | 205 | * Computes the size (aligned to multiple of 4) need to write the string |
michael@0 | 206 | * in a call to writeString(). If the length is not specified, it will be |
michael@0 | 207 | * computed by calling strlen(). |
michael@0 | 208 | */ |
michael@0 | 209 | static size_t WriteStringSize(const char* str, size_t len = (size_t)-1); |
michael@0 | 210 | |
michael@0 | 211 | /** |
michael@0 | 212 | * Move the cursor back to offset bytes from the beginning. |
michael@0 | 213 | * offset must be a multiple of 4 no greater than size(). |
michael@0 | 214 | */ |
michael@0 | 215 | void rewindToOffset(size_t offset) { |
michael@0 | 216 | SkASSERT(SkAlign4(offset) == offset); |
michael@0 | 217 | SkASSERT(offset <= bytesWritten()); |
michael@0 | 218 | fUsed = offset; |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | // copy into a single buffer (allocated by caller). Must be at least size() |
michael@0 | 222 | void flatten(void* dst) const { |
michael@0 | 223 | memcpy(dst, fData, fUsed); |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | bool writeToStream(SkWStream* stream) const { |
michael@0 | 227 | return stream->write(fData, fUsed); |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | // read from the stream, and write up to length bytes. Return the actual |
michael@0 | 231 | // number of bytes written. |
michael@0 | 232 | size_t readFromStream(SkStream* stream, size_t length) { |
michael@0 | 233 | return stream->read(this->reservePad(length), length); |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | /** |
michael@0 | 237 | * Captures a snapshot of the data as it is right now, and return it. |
michael@0 | 238 | * Multiple calls without intervening writes may return the same SkData, |
michael@0 | 239 | * but this is not guaranteed. |
michael@0 | 240 | * Future appends will not affect the returned buffer. |
michael@0 | 241 | * It is illegal to call overwriteTAt after this without an intervening |
michael@0 | 242 | * append. It may cause the snapshot buffer to be corrupted. |
michael@0 | 243 | * Callers must unref the returned SkData. |
michael@0 | 244 | * This is not thread safe, it should only be called on the writing thread, |
michael@0 | 245 | * the result however can be shared across threads. |
michael@0 | 246 | */ |
michael@0 | 247 | SkData* snapshotAsData() const; |
michael@0 | 248 | private: |
michael@0 | 249 | void growToAtLeast(size_t size); |
michael@0 | 250 | |
michael@0 | 251 | uint8_t* fData; // Points to either fInternal or fExternal. |
michael@0 | 252 | size_t fCapacity; // Number of bytes we can write to fData. |
michael@0 | 253 | size_t fUsed; // Number of bytes written. |
michael@0 | 254 | void* fExternal; // Unmanaged memory block. |
michael@0 | 255 | SkAutoTMalloc<uint8_t> fInternal; // Managed memory block. |
michael@0 | 256 | SkAutoTUnref<SkData> fSnapshot; // Holds the result of last asData. |
michael@0 | 257 | }; |
michael@0 | 258 | |
michael@0 | 259 | /** |
michael@0 | 260 | * Helper class to allocated SIZE bytes as part of the writer, and to provide |
michael@0 | 261 | * that storage to the constructor as its initial storage buffer. |
michael@0 | 262 | * |
michael@0 | 263 | * This wrapper ensures proper alignment rules are met for the storage. |
michael@0 | 264 | */ |
michael@0 | 265 | template <size_t SIZE> class SkSWriter32 : public SkWriter32 { |
michael@0 | 266 | public: |
michael@0 | 267 | SkSWriter32() { this->reset(); } |
michael@0 | 268 | |
michael@0 | 269 | void reset() {this->INHERITED::reset(fData.fStorage, SIZE); } |
michael@0 | 270 | |
michael@0 | 271 | private: |
michael@0 | 272 | union { |
michael@0 | 273 | void* fPtrAlignment; |
michael@0 | 274 | double fDoubleAlignment; |
michael@0 | 275 | char fStorage[SIZE]; |
michael@0 | 276 | } fData; |
michael@0 | 277 | |
michael@0 | 278 | typedef SkWriter32 INHERITED; |
michael@0 | 279 | }; |
michael@0 | 280 | |
michael@0 | 281 | #endif |