gfx/skia/trunk/src/core/SkDescriptor.h

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.

     2 /*
     3  * Copyright 2006 The Android Open Source Project
     4  *
     5  * Use of this source code is governed by a BSD-style license that can be
     6  * found in the LICENSE file.
     7  */
    10 #ifndef SkDescriptor_DEFINED
    11 #define SkDescriptor_DEFINED
    13 #include "SkChecksum.h"
    14 #include "SkTypes.h"
    16 class SkDescriptor : SkNoncopyable {
    17 public:
    18     static size_t ComputeOverhead(int entryCount) {
    19         SkASSERT(entryCount >= 0);
    20         return sizeof(SkDescriptor) + entryCount * sizeof(Entry);
    21     }
    23     static SkDescriptor* Alloc(size_t length) {
    24         SkASSERT(SkAlign4(length) == length);
    25         SkDescriptor* desc = (SkDescriptor*)sk_malloc_throw(length);
    26         return desc;
    27     }
    29     static void Free(SkDescriptor* desc) {
    30         sk_free(desc);
    31     }
    33     void init() {
    34         fLength = sizeof(SkDescriptor);
    35         fCount  = 0;
    36     }
    38     uint32_t getLength() const { return fLength; }
    40     void* addEntry(uint32_t tag, uint32_t length, const void* data = NULL) {
    41         SkASSERT(tag);
    42         SkASSERT(SkAlign4(length) == length);
    43         SkASSERT(this->findEntry(tag, NULL) == NULL);
    45         Entry*  entry = (Entry*)((char*)this + fLength);
    46         entry->fTag = tag;
    47         entry->fLen = length;
    48         if (data) {
    49             memcpy(entry + 1, data, length);
    50         }
    52         fCount += 1;
    53         fLength += sizeof(Entry) + length;
    54         return (entry + 1); // return its data
    55     }
    57     void computeChecksum() {
    58         fChecksum = SkDescriptor::ComputeChecksum(this);
    59     }
    61 #ifdef SK_DEBUG
    62     void assertChecksum() const {
    63         SkASSERT(SkDescriptor::ComputeChecksum(this) == fChecksum);
    64     }
    65 #endif
    67     const void* findEntry(uint32_t tag, uint32_t* length) const {
    68         const Entry* entry = (const Entry*)(this + 1);
    69         int          count = fCount;
    71         while (--count >= 0) {
    72             if (entry->fTag == tag) {
    73                 if (length) {
    74                     *length = entry->fLen;
    75                 }
    76                 return entry + 1;
    77             }
    78             entry = (const Entry*)((const char*)(entry + 1) + entry->fLen);
    79         }
    80         return NULL;
    81     }
    83     SkDescriptor* copy() const {
    84         SkDescriptor* desc = SkDescriptor::Alloc(fLength);
    85         memcpy(desc, this, fLength);
    86         return desc;
    87     }
    89     bool equals(const SkDescriptor& other) const {
    90         // probe to see if we have a good checksum algo
    91 //        SkASSERT(a.fChecksum != b.fChecksum || memcmp(&a, &b, a.fLength) == 0);
    93         // the first value we should look at is the checksum, so this loop
    94         // should terminate early if they descriptors are different.
    95         // NOTE: if we wrote a sentinel value at the end of each, we chould
    96         //       remove the aa < stop test in the loop...
    97         const uint32_t* aa = (const uint32_t*)this;
    98         const uint32_t* bb = (const uint32_t*)&other;
    99         const uint32_t* stop = (const uint32_t*)((const char*)aa + fLength);
   100         do {
   101             if (*aa++ != *bb++)
   102                 return false;
   103         } while (aa < stop);
   104         return true;
   105     }
   107     uint32_t getChecksum() const { return fChecksum; }
   109     struct Entry {
   110         uint32_t fTag;
   111         uint32_t fLen;
   112     };
   114 #ifdef SK_DEBUG
   115     uint32_t getCount() const { return fCount; }
   116 #endif
   118 private:
   119     uint32_t fChecksum;  // must be first
   120     uint32_t fLength;    // must be second
   121     uint32_t fCount;
   123     static uint32_t ComputeChecksum(const SkDescriptor* desc) {
   124         const uint32_t* ptr = (const uint32_t*)desc + 1; // skip the checksum field
   125         size_t len = desc->fLength - sizeof(uint32_t);
   126         return SkChecksum::Compute(ptr, len);
   127     }
   129     // private so no one can create one except our factories
   130     SkDescriptor() {}
   131 };
   133 #include "SkScalerContext.h"
   135 class SkAutoDescriptor : SkNoncopyable {
   136 public:
   137     SkAutoDescriptor(size_t size) {
   138         if (size <= sizeof(fStorage)) {
   139             fDesc = (SkDescriptor*)(void*)fStorage;
   140         } else {
   141             fDesc = SkDescriptor::Alloc(size);
   142         }
   143     }
   145     ~SkAutoDescriptor() {
   146         if (fDesc != (SkDescriptor*)(void*)fStorage) {
   147             SkDescriptor::Free(fDesc);
   148         }
   149     }
   151     SkDescriptor* getDesc() const { return fDesc; }
   152 private:
   153     enum {
   154         kStorageSize =  sizeof(SkDescriptor)
   155                         + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContext::Rec)    // for rec
   156                         + sizeof(SkDescriptor::Entry) + sizeof(void*)                   // for typeface
   157                         + 32   // slop for occational small extras
   158     };
   159     SkDescriptor*   fDesc;
   160     uint32_t        fStorage[(kStorageSize + 3) >> 2];
   161 };
   162 #define SkAutoDescriptor(...) SK_REQUIRE_LOCAL_VAR(SkAutoDescriptor)
   165 #endif

mercurial