gfx/skia/trunk/src/core/SkMetaData.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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 2006 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 #include "SkMetaData.h"
michael@0 11 #include "SkRefCnt.h"
michael@0 12
michael@0 13 struct PtrPair {
michael@0 14 void* fPtr;
michael@0 15 SkMetaData::PtrProc fProc;
michael@0 16 };
michael@0 17
michael@0 18 void* SkMetaData::RefCntProc(void* ptr, bool doRef) {
michael@0 19 SkASSERT(ptr);
michael@0 20 SkRefCnt* refcnt = reinterpret_cast<SkRefCnt*>(ptr);
michael@0 21
michael@0 22 if (doRef) {
michael@0 23 refcnt->ref();
michael@0 24 } else {
michael@0 25 refcnt->unref();
michael@0 26 }
michael@0 27 return ptr;
michael@0 28 }
michael@0 29
michael@0 30 SkMetaData::SkMetaData() : fRec(NULL)
michael@0 31 {
michael@0 32 }
michael@0 33
michael@0 34 SkMetaData::SkMetaData(const SkMetaData& src) : fRec(NULL)
michael@0 35 {
michael@0 36 *this = src;
michael@0 37 }
michael@0 38
michael@0 39 SkMetaData::~SkMetaData()
michael@0 40 {
michael@0 41 this->reset();
michael@0 42 }
michael@0 43
michael@0 44 void SkMetaData::reset()
michael@0 45 {
michael@0 46 Rec* rec = fRec;
michael@0 47 while (rec) {
michael@0 48 if (kPtr_Type == rec->fType) {
michael@0 49 PtrPair* pair = (PtrPair*)rec->data();
michael@0 50 if (pair->fProc && pair->fPtr) {
michael@0 51 pair->fPtr = pair->fProc(pair->fPtr, false);
michael@0 52 }
michael@0 53 }
michael@0 54 Rec* next = rec->fNext;
michael@0 55 Rec::Free(rec);
michael@0 56 rec = next;
michael@0 57 }
michael@0 58 fRec = NULL;
michael@0 59 }
michael@0 60
michael@0 61 SkMetaData& SkMetaData::operator=(const SkMetaData& src)
michael@0 62 {
michael@0 63 this->reset();
michael@0 64
michael@0 65 const Rec* rec = src.fRec;
michael@0 66 while (rec)
michael@0 67 {
michael@0 68 this->set(rec->name(), rec->data(), rec->fDataLen, (Type)rec->fType, rec->fDataCount);
michael@0 69 rec = rec->fNext;
michael@0 70 }
michael@0 71 return *this;
michael@0 72 }
michael@0 73
michael@0 74 void SkMetaData::setS32(const char name[], int32_t value)
michael@0 75 {
michael@0 76 (void)this->set(name, &value, sizeof(int32_t), kS32_Type, 1);
michael@0 77 }
michael@0 78
michael@0 79 void SkMetaData::setScalar(const char name[], SkScalar value)
michael@0 80 {
michael@0 81 (void)this->set(name, &value, sizeof(SkScalar), kScalar_Type, 1);
michael@0 82 }
michael@0 83
michael@0 84 SkScalar* SkMetaData::setScalars(const char name[], int count, const SkScalar values[])
michael@0 85 {
michael@0 86 SkASSERT(count > 0);
michael@0 87 if (count > 0)
michael@0 88 return (SkScalar*)this->set(name, values, sizeof(SkScalar), kScalar_Type, count);
michael@0 89 return NULL;
michael@0 90 }
michael@0 91
michael@0 92 void SkMetaData::setString(const char name[], const char value[])
michael@0 93 {
michael@0 94 (void)this->set(name, value, sizeof(char), kString_Type, SkToInt(strlen(value) + 1));
michael@0 95 }
michael@0 96
michael@0 97 void SkMetaData::setPtr(const char name[], void* ptr, PtrProc proc) {
michael@0 98 PtrPair pair = { ptr, proc };
michael@0 99 (void)this->set(name, &pair, sizeof(PtrPair), kPtr_Type, 1);
michael@0 100 }
michael@0 101
michael@0 102 void SkMetaData::setBool(const char name[], bool value)
michael@0 103 {
michael@0 104 (void)this->set(name, &value, sizeof(bool), kBool_Type, 1);
michael@0 105 }
michael@0 106
michael@0 107 void SkMetaData::setData(const char name[], const void* data, size_t byteCount) {
michael@0 108 (void)this->set(name, data, sizeof(char), kData_Type, SkToInt(byteCount));
michael@0 109 }
michael@0 110
michael@0 111 void* SkMetaData::set(const char name[], const void* data, size_t dataSize, Type type, int count)
michael@0 112 {
michael@0 113 SkASSERT(name);
michael@0 114 SkASSERT(dataSize);
michael@0 115 SkASSERT(count > 0);
michael@0 116
michael@0 117 (void)this->remove(name, type);
michael@0 118
michael@0 119 size_t len = strlen(name);
michael@0 120 Rec* rec = Rec::Alloc(sizeof(Rec) + dataSize * count + len + 1);
michael@0 121
michael@0 122 #ifndef SK_DEBUG
michael@0 123 rec->fType = SkToU8(type);
michael@0 124 #else
michael@0 125 rec->fType = type;
michael@0 126 #endif
michael@0 127 rec->fDataLen = SkToU8(dataSize);
michael@0 128 rec->fDataCount = SkToU16(count);
michael@0 129 if (data)
michael@0 130 memcpy(rec->data(), data, dataSize * count);
michael@0 131 memcpy(rec->name(), name, len + 1);
michael@0 132
michael@0 133 if (kPtr_Type == type) {
michael@0 134 PtrPair* pair = (PtrPair*)rec->data();
michael@0 135 if (pair->fProc && pair->fPtr) {
michael@0 136 pair->fPtr = pair->fProc(pair->fPtr, true);
michael@0 137 }
michael@0 138 }
michael@0 139
michael@0 140 rec->fNext = fRec;
michael@0 141 fRec = rec;
michael@0 142 return rec->data();
michael@0 143 }
michael@0 144
michael@0 145 bool SkMetaData::findS32(const char name[], int32_t* value) const
michael@0 146 {
michael@0 147 const Rec* rec = this->find(name, kS32_Type);
michael@0 148 if (rec)
michael@0 149 {
michael@0 150 SkASSERT(rec->fDataCount == 1);
michael@0 151 if (value)
michael@0 152 *value = *(const int32_t*)rec->data();
michael@0 153 return true;
michael@0 154 }
michael@0 155 return false;
michael@0 156 }
michael@0 157
michael@0 158 bool SkMetaData::findScalar(const char name[], SkScalar* value) const
michael@0 159 {
michael@0 160 const Rec* rec = this->find(name, kScalar_Type);
michael@0 161 if (rec)
michael@0 162 {
michael@0 163 SkASSERT(rec->fDataCount == 1);
michael@0 164 if (value)
michael@0 165 *value = *(const SkScalar*)rec->data();
michael@0 166 return true;
michael@0 167 }
michael@0 168 return false;
michael@0 169 }
michael@0 170
michael@0 171 const SkScalar* SkMetaData::findScalars(const char name[], int* count, SkScalar values[]) const
michael@0 172 {
michael@0 173 const Rec* rec = this->find(name, kScalar_Type);
michael@0 174 if (rec)
michael@0 175 {
michael@0 176 if (count)
michael@0 177 *count = rec->fDataCount;
michael@0 178 if (values)
michael@0 179 memcpy(values, rec->data(), rec->fDataCount * rec->fDataLen);
michael@0 180 return (const SkScalar*)rec->data();
michael@0 181 }
michael@0 182 return NULL;
michael@0 183 }
michael@0 184
michael@0 185 bool SkMetaData::findPtr(const char name[], void** ptr, PtrProc* proc) const {
michael@0 186 const Rec* rec = this->find(name, kPtr_Type);
michael@0 187 if (rec) {
michael@0 188 SkASSERT(rec->fDataCount == 1);
michael@0 189 const PtrPair* pair = (const PtrPair*)rec->data();
michael@0 190 if (ptr) {
michael@0 191 *ptr = pair->fPtr;
michael@0 192 }
michael@0 193 if (proc) {
michael@0 194 *proc = pair->fProc;
michael@0 195 }
michael@0 196 return true;
michael@0 197 }
michael@0 198 return false;
michael@0 199 }
michael@0 200
michael@0 201 const char* SkMetaData::findString(const char name[]) const
michael@0 202 {
michael@0 203 const Rec* rec = this->find(name, kString_Type);
michael@0 204 SkASSERT(rec == NULL || rec->fDataLen == sizeof(char));
michael@0 205 return rec ? (const char*)rec->data() : NULL;
michael@0 206 }
michael@0 207
michael@0 208 bool SkMetaData::findBool(const char name[], bool* value) const
michael@0 209 {
michael@0 210 const Rec* rec = this->find(name, kBool_Type);
michael@0 211 if (rec)
michael@0 212 {
michael@0 213 SkASSERT(rec->fDataCount == 1);
michael@0 214 if (value)
michael@0 215 *value = *(const bool*)rec->data();
michael@0 216 return true;
michael@0 217 }
michael@0 218 return false;
michael@0 219 }
michael@0 220
michael@0 221 const void* SkMetaData::findData(const char name[], size_t* length) const {
michael@0 222 const Rec* rec = this->find(name, kData_Type);
michael@0 223 if (rec) {
michael@0 224 SkASSERT(rec->fDataLen == sizeof(char));
michael@0 225 if (length) {
michael@0 226 *length = rec->fDataCount;
michael@0 227 }
michael@0 228 return rec->data();
michael@0 229 }
michael@0 230 return NULL;
michael@0 231 }
michael@0 232
michael@0 233 const SkMetaData::Rec* SkMetaData::find(const char name[], Type type) const
michael@0 234 {
michael@0 235 const Rec* rec = fRec;
michael@0 236 while (rec)
michael@0 237 {
michael@0 238 if (rec->fType == type && !strcmp(rec->name(), name))
michael@0 239 return rec;
michael@0 240 rec = rec->fNext;
michael@0 241 }
michael@0 242 return NULL;
michael@0 243 }
michael@0 244
michael@0 245 bool SkMetaData::remove(const char name[], Type type) {
michael@0 246 Rec* rec = fRec;
michael@0 247 Rec* prev = NULL;
michael@0 248 while (rec) {
michael@0 249 Rec* next = rec->fNext;
michael@0 250 if (rec->fType == type && !strcmp(rec->name(), name)) {
michael@0 251 if (prev) {
michael@0 252 prev->fNext = next;
michael@0 253 } else {
michael@0 254 fRec = next;
michael@0 255 }
michael@0 256
michael@0 257 if (kPtr_Type == type) {
michael@0 258 PtrPair* pair = (PtrPair*)rec->data();
michael@0 259 if (pair->fProc && pair->fPtr) {
michael@0 260 (void)pair->fProc(pair->fPtr, false);
michael@0 261 }
michael@0 262 }
michael@0 263 Rec::Free(rec);
michael@0 264 return true;
michael@0 265 }
michael@0 266 prev = rec;
michael@0 267 rec = next;
michael@0 268 }
michael@0 269 return false;
michael@0 270 }
michael@0 271
michael@0 272 bool SkMetaData::removeS32(const char name[])
michael@0 273 {
michael@0 274 return this->remove(name, kS32_Type);
michael@0 275 }
michael@0 276
michael@0 277 bool SkMetaData::removeScalar(const char name[])
michael@0 278 {
michael@0 279 return this->remove(name, kScalar_Type);
michael@0 280 }
michael@0 281
michael@0 282 bool SkMetaData::removeString(const char name[])
michael@0 283 {
michael@0 284 return this->remove(name, kString_Type);
michael@0 285 }
michael@0 286
michael@0 287 bool SkMetaData::removePtr(const char name[])
michael@0 288 {
michael@0 289 return this->remove(name, kPtr_Type);
michael@0 290 }
michael@0 291
michael@0 292 bool SkMetaData::removeBool(const char name[])
michael@0 293 {
michael@0 294 return this->remove(name, kBool_Type);
michael@0 295 }
michael@0 296
michael@0 297 bool SkMetaData::removeData(const char name[]) {
michael@0 298 return this->remove(name, kData_Type);
michael@0 299 }
michael@0 300
michael@0 301 ///////////////////////////////////////////////////////////////////////////////
michael@0 302
michael@0 303 SkMetaData::Iter::Iter(const SkMetaData& metadata) {
michael@0 304 fRec = metadata.fRec;
michael@0 305 }
michael@0 306
michael@0 307 void SkMetaData::Iter::reset(const SkMetaData& metadata) {
michael@0 308 fRec = metadata.fRec;
michael@0 309 }
michael@0 310
michael@0 311 const char* SkMetaData::Iter::next(SkMetaData::Type* t, int* count) {
michael@0 312 const char* name = NULL;
michael@0 313
michael@0 314 if (fRec) {
michael@0 315 if (t) {
michael@0 316 *t = (SkMetaData::Type)fRec->fType;
michael@0 317 }
michael@0 318 if (count) {
michael@0 319 *count = fRec->fDataCount;
michael@0 320 }
michael@0 321 name = fRec->name();
michael@0 322
michael@0 323 fRec = fRec->fNext;
michael@0 324 }
michael@0 325 return name;
michael@0 326 }
michael@0 327
michael@0 328 ///////////////////////////////////////////////////////////////////////////////
michael@0 329
michael@0 330 SkMetaData::Rec* SkMetaData::Rec::Alloc(size_t size) {
michael@0 331 return (Rec*)sk_malloc_throw(size);
michael@0 332 }
michael@0 333
michael@0 334 void SkMetaData::Rec::Free(Rec* rec) {
michael@0 335 sk_free(rec);
michael@0 336 }

mercurial