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 | * Copyright 2011 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "SkData.h" |
michael@0 | 9 | #include "SkReadBuffer.h" |
michael@0 | 10 | #include "SkWriteBuffer.h" |
michael@0 | 11 | #include "SkOSFile.h" |
michael@0 | 12 | #include "SkOnce.h" |
michael@0 | 13 | |
michael@0 | 14 | SkData::SkData(const void* ptr, size_t size, ReleaseProc proc, void* context) { |
michael@0 | 15 | fPtr = ptr; |
michael@0 | 16 | fSize = size; |
michael@0 | 17 | fReleaseProc = proc; |
michael@0 | 18 | fReleaseProcContext = context; |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | SkData::~SkData() { |
michael@0 | 22 | if (fReleaseProc) { |
michael@0 | 23 | fReleaseProc(fPtr, fSize, fReleaseProcContext); |
michael@0 | 24 | } |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | bool SkData::equals(const SkData* other) const { |
michael@0 | 28 | if (NULL == other) { |
michael@0 | 29 | return false; |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | return fSize == other->fSize && !memcmp(fPtr, other->fPtr, fSize); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | size_t SkData::copyRange(size_t offset, size_t length, void* buffer) const { |
michael@0 | 36 | size_t available = fSize; |
michael@0 | 37 | if (offset >= available || 0 == length) { |
michael@0 | 38 | return 0; |
michael@0 | 39 | } |
michael@0 | 40 | available -= offset; |
michael@0 | 41 | if (length > available) { |
michael@0 | 42 | length = available; |
michael@0 | 43 | } |
michael@0 | 44 | SkASSERT(length > 0); |
michael@0 | 45 | |
michael@0 | 46 | memcpy(buffer, this->bytes() + offset, length); |
michael@0 | 47 | return length; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 51 | |
michael@0 | 52 | static SkData* gEmptyDataRef = NULL; |
michael@0 | 53 | static void cleanup_gEmptyDataRef() { gEmptyDataRef->unref(); } |
michael@0 | 54 | |
michael@0 | 55 | void SkData::NewEmptyImpl(int) { |
michael@0 | 56 | gEmptyDataRef = new SkData(NULL, 0, NULL, NULL); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | SkData* SkData::NewEmpty() { |
michael@0 | 60 | SK_DECLARE_STATIC_ONCE(once); |
michael@0 | 61 | SkOnce(&once, SkData::NewEmptyImpl, 0, cleanup_gEmptyDataRef); |
michael@0 | 62 | gEmptyDataRef->ref(); |
michael@0 | 63 | return gEmptyDataRef; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | // assumes fPtr was allocated via sk_malloc |
michael@0 | 67 | static void sk_free_releaseproc(const void* ptr, size_t, void*) { |
michael@0 | 68 | sk_free((void*)ptr); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | SkData* SkData::NewFromMalloc(const void* data, size_t length) { |
michael@0 | 72 | return new SkData(data, length, sk_free_releaseproc, NULL); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | SkData* SkData::NewWithCopy(const void* data, size_t length) { |
michael@0 | 76 | if (0 == length) { |
michael@0 | 77 | return SkData::NewEmpty(); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | void* copy = sk_malloc_throw(length); // balanced in sk_free_releaseproc |
michael@0 | 81 | memcpy(copy, data, length); |
michael@0 | 82 | return new SkData(copy, length, sk_free_releaseproc, NULL); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | SkData* SkData::NewWithProc(const void* data, size_t length, |
michael@0 | 86 | ReleaseProc proc, void* context) { |
michael@0 | 87 | return new SkData(data, length, proc, context); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | // assumes fPtr was allocated with sk_fmmap |
michael@0 | 91 | static void sk_mmap_releaseproc(const void* addr, size_t length, void*) { |
michael@0 | 92 | sk_fmunmap(addr, length); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | SkData* SkData::NewFromFILE(SkFILE* f) { |
michael@0 | 96 | size_t size; |
michael@0 | 97 | void* addr = sk_fmmap(f, &size); |
michael@0 | 98 | if (NULL == addr) { |
michael@0 | 99 | return NULL; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | return SkData::NewWithProc(addr, size, sk_mmap_releaseproc, NULL); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | SkData* SkData::NewFromFileName(const char path[]) { |
michael@0 | 106 | SkFILE* f = path ? sk_fopen(path, kRead_SkFILE_Flag) : NULL; |
michael@0 | 107 | if (NULL == f) { |
michael@0 | 108 | return NULL; |
michael@0 | 109 | } |
michael@0 | 110 | SkData* data = NewFromFILE(f); |
michael@0 | 111 | sk_fclose(f); |
michael@0 | 112 | return data; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | SkData* SkData::NewFromFD(int fd) { |
michael@0 | 116 | size_t size; |
michael@0 | 117 | void* addr = sk_fdmmap(fd, &size); |
michael@0 | 118 | if (NULL == addr) { |
michael@0 | 119 | return NULL; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | return SkData::NewWithProc(addr, size, sk_mmap_releaseproc, NULL); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | // assumes context is a SkData |
michael@0 | 126 | static void sk_dataref_releaseproc(const void*, size_t, void* context) { |
michael@0 | 127 | SkData* src = reinterpret_cast<SkData*>(context); |
michael@0 | 128 | src->unref(); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | SkData* SkData::NewSubset(const SkData* src, size_t offset, size_t length) { |
michael@0 | 132 | /* |
michael@0 | 133 | We could, if we wanted/need to, just make a deep copy of src's data, |
michael@0 | 134 | rather than referencing it. This would duplicate the storage (of the |
michael@0 | 135 | subset amount) but would possibly allow src to go out of scope sooner. |
michael@0 | 136 | */ |
michael@0 | 137 | |
michael@0 | 138 | size_t available = src->size(); |
michael@0 | 139 | if (offset >= available || 0 == length) { |
michael@0 | 140 | return SkData::NewEmpty(); |
michael@0 | 141 | } |
michael@0 | 142 | available -= offset; |
michael@0 | 143 | if (length > available) { |
michael@0 | 144 | length = available; |
michael@0 | 145 | } |
michael@0 | 146 | SkASSERT(length > 0); |
michael@0 | 147 | |
michael@0 | 148 | src->ref(); // this will be balanced in sk_dataref_releaseproc |
michael@0 | 149 | return new SkData(src->bytes() + offset, length, sk_dataref_releaseproc, |
michael@0 | 150 | const_cast<SkData*>(src)); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | SkData* SkData::NewWithCString(const char cstr[]) { |
michael@0 | 154 | size_t size; |
michael@0 | 155 | if (NULL == cstr) { |
michael@0 | 156 | cstr = ""; |
michael@0 | 157 | size = 1; |
michael@0 | 158 | } else { |
michael@0 | 159 | size = strlen(cstr) + 1; |
michael@0 | 160 | } |
michael@0 | 161 | return NewWithCopy(cstr, size); |
michael@0 | 162 | } |