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.
1 /*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
8 #include "SkMallocPixelRef.h"
9 #include "SkBitmap.h"
10 #include "SkReadBuffer.h"
11 #include "SkWriteBuffer.h"
13 // assumes ptr was allocated via sk_malloc
14 static void sk_free_releaseproc(void* ptr, void*) {
15 sk_free(ptr);
16 }
18 static bool is_valid(const SkImageInfo& info, SkColorTable* ctable) {
19 if (info.fWidth < 0 ||
20 info.fHeight < 0 ||
21 (unsigned)info.fColorType > (unsigned)kLastEnum_SkColorType ||
22 (unsigned)info.fAlphaType > (unsigned)kLastEnum_SkAlphaType)
23 {
24 return false;
25 }
27 // these seem like good checks, but currently we have (at least) tests
28 // that expect the pixelref to succeed even when there is a mismatch
29 // with colortables. fix?
30 #if 0
31 if (kIndex8_SkColorType == info.fColorType && NULL == ctable) {
32 return false;
33 }
34 if (kIndex8_SkColorType != info.fColorType && NULL != ctable) {
35 return false;
36 }
37 #endif
38 return true;
39 }
41 SkMallocPixelRef* SkMallocPixelRef::NewDirect(const SkImageInfo& info,
42 void* addr,
43 size_t rowBytes,
44 SkColorTable* ctable) {
45 if (!is_valid(info, ctable)) {
46 return NULL;
47 }
48 return SkNEW_ARGS(SkMallocPixelRef,
49 (info, addr, rowBytes, ctable, NULL, NULL));
50 }
52 SkMallocPixelRef* SkMallocPixelRef::NewAllocate(const SkImageInfo& info,
53 size_t requestedRowBytes,
54 SkColorTable* ctable) {
55 if (!is_valid(info, ctable)) {
56 return NULL;
57 }
59 int32_t minRB = SkToS32(info.minRowBytes());
60 if (minRB < 0) {
61 return NULL; // allocation will be too large
62 }
63 if (requestedRowBytes > 0 && (int32_t)requestedRowBytes < minRB) {
64 return NULL; // cannot meet requested rowbytes
65 }
67 int32_t rowBytes;
68 if (requestedRowBytes) {
69 rowBytes = SkToS32(requestedRowBytes);
70 } else {
71 rowBytes = minRB;
72 }
74 int64_t bigSize = (int64_t)info.fHeight * rowBytes;
75 if (!sk_64_isS32(bigSize)) {
76 return NULL;
77 }
79 size_t size = sk_64_asS32(bigSize);
80 SkASSERT(size >= info.getSafeSize(rowBytes));
81 void* addr = sk_malloc_flags(size, 0);
82 if (NULL == addr) {
83 return NULL;
84 }
86 return SkNEW_ARGS(SkMallocPixelRef,
87 (info, addr, rowBytes, ctable,
88 sk_free_releaseproc, NULL));
89 }
91 SkMallocPixelRef* SkMallocPixelRef::NewWithProc(const SkImageInfo& info,
92 size_t rowBytes,
93 SkColorTable* ctable,
94 void* addr,
95 SkMallocPixelRef::ReleaseProc proc,
96 void* context) {
97 if (!is_valid(info, ctable)) {
98 return NULL;
99 }
100 return SkNEW_ARGS(SkMallocPixelRef,
101 (info, addr, rowBytes, ctable, proc, context));
102 }
104 static void sk_data_releaseproc(void*, void* dataPtr) {
105 (static_cast<SkData*>(dataPtr))->unref();
106 }
108 SkMallocPixelRef* SkMallocPixelRef::NewWithData(const SkImageInfo& info,
109 size_t rowBytes,
110 SkColorTable* ctable,
111 SkData* data,
112 size_t offset) {
113 SkASSERT(data != NULL);
114 SkASSERT(offset <= data->size());
115 if (!is_valid(info, ctable)) {
116 return NULL;
117 }
118 if ((rowBytes < info.minRowBytes())
119 || ((data->size() - offset) < info.getSafeSize(rowBytes))) {
120 return NULL;
121 }
122 data->ref();
123 const void* ptr = static_cast<const void*>(data->bytes() + offset);
124 SkMallocPixelRef* pr
125 = SkNEW_ARGS(SkMallocPixelRef,
126 (info, const_cast<void*>(ptr), rowBytes, ctable,
127 sk_data_releaseproc, static_cast<void*>(data)));
128 SkASSERT(pr != NULL);
129 // We rely on the immutability of the pixels to make the
130 // const_cast okay.
131 pr->setImmutable();
132 return pr;
133 }
135 ///////////////////////////////////////////////////////////////////////////////
137 SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage,
138 size_t rowBytes, SkColorTable* ctable,
139 bool ownsPixels)
140 : INHERITED(info)
141 , fReleaseProc(ownsPixels ? sk_free_releaseproc : NULL)
142 , fReleaseProcContext(NULL) {
143 // This constructor is now DEPRICATED.
144 SkASSERT(is_valid(info, ctable));
145 SkASSERT(rowBytes >= info.minRowBytes());
147 if (kIndex_8_SkColorType != info.fColorType) {
148 ctable = NULL;
149 }
151 fStorage = storage;
152 fCTable = ctable;
153 fRB = rowBytes;
154 SkSafeRef(ctable);
156 this->setPreLocked(fStorage, rowBytes, fCTable);
157 }
159 SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage,
160 size_t rowBytes, SkColorTable* ctable,
161 SkMallocPixelRef::ReleaseProc proc,
162 void* context)
163 : INHERITED(info)
164 , fReleaseProc(proc)
165 , fReleaseProcContext(context)
166 {
167 SkASSERT(is_valid(info, ctable));
168 SkASSERT(rowBytes >= info.minRowBytes());
170 if (kIndex_8_SkColorType != info.fColorType) {
171 ctable = NULL;
172 }
174 fStorage = storage;
175 fCTable = ctable;
176 fRB = rowBytes;
177 SkSafeRef(ctable);
179 this->setPreLocked(fStorage, rowBytes, fCTable);
180 }
183 SkMallocPixelRef::~SkMallocPixelRef() {
184 SkSafeUnref(fCTable);
185 if (fReleaseProc != NULL) {
186 fReleaseProc(fStorage, fReleaseProcContext);
187 }
188 }
190 bool SkMallocPixelRef::onNewLockPixels(LockRec* rec) {
191 rec->fPixels = fStorage;
192 rec->fRowBytes = fRB;
193 rec->fColorTable = fCTable;
194 return true;
195 }
197 void SkMallocPixelRef::onUnlockPixels() {
198 // nothing to do
199 }
201 size_t SkMallocPixelRef::getAllocatedSizeInBytes() const {
202 return this->info().getSafeSize(fRB);
203 }
205 void SkMallocPixelRef::flatten(SkWriteBuffer& buffer) const {
206 this->INHERITED::flatten(buffer);
208 buffer.write32(SkToU32(fRB));
210 // TODO: replace this bulk write with a chunky one that can trim off any
211 // trailing bytes on each scanline (in case rowbytes > width*size)
212 size_t size = this->info().getSafeSize(fRB);
213 buffer.writeByteArray(fStorage, size);
214 buffer.writeBool(fCTable != NULL);
215 if (fCTable) {
216 fCTable->writeToBuffer(buffer);
217 }
218 }
220 SkMallocPixelRef::SkMallocPixelRef(SkReadBuffer& buffer)
221 : INHERITED(buffer, NULL)
222 , fReleaseProc(sk_free_releaseproc)
223 , fReleaseProcContext(NULL)
224 {
225 fRB = buffer.read32();
226 size_t size = buffer.isValid() ? this->info().getSafeSize(fRB) : 0;
227 if (buffer.validateAvailable(size)) {
228 fStorage = sk_malloc_throw(size);
229 buffer.readByteArray(fStorage, size);
230 } else {
231 fStorage = NULL;
232 }
234 if (buffer.readBool()) {
235 fCTable = SkNEW_ARGS(SkColorTable, (buffer));
236 } else {
237 fCTable = NULL;
238 }
240 this->setPreLocked(fStorage, fRB, fCTable);
241 }
243 ///////////////////////////////////////////////////////////////////////////////
245 SkPixelRef* SkMallocPixelRef::PRFactory::create(const SkImageInfo& info,
246 SkColorTable* ctable) {
247 return SkMallocPixelRef::NewAllocate(info, info.minRowBytes(), ctable);
248 }