gfx/skia/trunk/include/core/SkStream.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.

michael@0 1 /*
michael@0 2 * Copyright 2006 The Android Open Source Project
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 #ifndef SkStream_DEFINED
michael@0 9 #define SkStream_DEFINED
michael@0 10
michael@0 11 #include "SkRefCnt.h"
michael@0 12 #include "SkScalar.h"
michael@0 13
michael@0 14 class SkData;
michael@0 15
michael@0 16 class SkStream;
michael@0 17 class SkStreamRewindable;
michael@0 18 class SkStreamSeekable;
michael@0 19 class SkStreamAsset;
michael@0 20 class SkStreamMemory;
michael@0 21
michael@0 22 /**
michael@0 23 * SkStream -- abstraction for a source of bytes. Subclasses can be backed by
michael@0 24 * memory, or a file, or something else.
michael@0 25 *
michael@0 26 * NOTE:
michael@0 27 *
michael@0 28 * Classic "streams" APIs are sort of async, in that on a request for N
michael@0 29 * bytes, they may return fewer than N bytes on a given call, in which case
michael@0 30 * the caller can "try again" to get more bytes, eventually (modulo an error)
michael@0 31 * receiving their total N bytes.
michael@0 32 *
michael@0 33 * Skia streams behave differently. They are effectively synchronous, and will
michael@0 34 * always return all N bytes of the request if possible. If they return fewer
michael@0 35 * (the read() call returns the number of bytes read) then that means there is
michael@0 36 * no more data (at EOF or hit an error). The caller should *not* call again
michael@0 37 * in hopes of fulfilling more of the request.
michael@0 38 */
michael@0 39 class SK_API SkStream : public SkRefCnt { //TODO: remove SkRefCnt
michael@0 40 public:
michael@0 41 /**
michael@0 42 * Attempts to open the specified file, and return a stream to it (using
michael@0 43 * mmap if available). On success, the caller must call unref() on the
michael@0 44 * returned object. On failure, returns NULL.
michael@0 45 */
michael@0 46 static SkStreamAsset* NewFromFile(const char path[]);
michael@0 47
michael@0 48 SK_DECLARE_INST_COUNT(SkStream)
michael@0 49
michael@0 50 /** Reads or skips size number of bytes.
michael@0 51 * If buffer == NULL, skip size bytes, return how many were skipped.
michael@0 52 * If buffer != NULL, copy size bytes into buffer, return how many were copied.
michael@0 53 * @param buffer when NULL skip size bytes, otherwise copy size bytes into buffer
michael@0 54 * @param size the number of bytes to skip or copy
michael@0 55 * @return the number of bytes actually read.
michael@0 56 */
michael@0 57 virtual size_t read(void* buffer, size_t size) = 0;
michael@0 58
michael@0 59 /** Skip size number of bytes.
michael@0 60 * @return the actual number bytes that could be skipped.
michael@0 61 */
michael@0 62 size_t skip(size_t size) {
michael@0 63 return this->read(NULL, size);
michael@0 64 }
michael@0 65
michael@0 66 /** Returns true when all the bytes in the stream have been read.
michael@0 67 * This may return true early (when there are no more bytes to be read)
michael@0 68 * or late (after the first unsuccessful read).
michael@0 69 */
michael@0 70 virtual bool isAtEnd() const = 0;
michael@0 71
michael@0 72 int8_t readS8();
michael@0 73 int16_t readS16();
michael@0 74 int32_t readS32();
michael@0 75
michael@0 76 uint8_t readU8() { return (uint8_t)this->readS8(); }
michael@0 77 uint16_t readU16() { return (uint16_t)this->readS16(); }
michael@0 78 uint32_t readU32() { return (uint32_t)this->readS32(); }
michael@0 79
michael@0 80 bool readBool() { return this->readU8() != 0; }
michael@0 81 SkScalar readScalar();
michael@0 82 size_t readPackedUInt();
michael@0 83
michael@0 84 /**
michael@0 85 * Reconstitute an SkData object that was written to the stream
michael@0 86 * using SkWStream::writeData().
michael@0 87 */
michael@0 88 SkData* readData();
michael@0 89
michael@0 90 //SkStreamRewindable
michael@0 91 /** Rewinds to the beginning of the stream. Returns true if the stream is known
michael@0 92 * to be at the beginning after this call returns.
michael@0 93 */
michael@0 94 virtual bool rewind() { return false; }
michael@0 95
michael@0 96 /** Duplicates this stream. If this cannot be done, returns NULL.
michael@0 97 * The returned stream will be positioned at the beginning of its data.
michael@0 98 */
michael@0 99 virtual SkStreamRewindable* duplicate() const { return NULL; }
michael@0 100
michael@0 101 //SkStreamSeekable
michael@0 102 /** Returns true if this stream can report it's current position. */
michael@0 103 virtual bool hasPosition() const { return false; }
michael@0 104 /** Returns the current position in the stream. If this cannot be done, returns 0. */
michael@0 105 virtual size_t getPosition() const { return 0; }
michael@0 106
michael@0 107 /** Seeks to an absolute position in the stream. If this cannot be done, returns false.
michael@0 108 * If an attempt is made to seek past the end of the stream, the position will be set
michael@0 109 * to the end of the stream.
michael@0 110 */
michael@0 111 virtual bool seek(size_t position) { return false; }
michael@0 112
michael@0 113 /** Seeks to an relative offset in the stream. If this cannot be done, returns false.
michael@0 114 * If an attempt is made to move to a position outside the stream, the position will be set
michael@0 115 * to the closest point within the stream (beginning or end).
michael@0 116 */
michael@0 117 virtual bool move(long offset) { return false; }
michael@0 118
michael@0 119 /** Duplicates this stream. If this cannot be done, returns NULL.
michael@0 120 * The returned stream will be positioned the same as this stream.
michael@0 121 */
michael@0 122 virtual SkStreamSeekable* fork() const { return NULL; }
michael@0 123
michael@0 124 //SkStreamAsset
michael@0 125 /** Returns true if this stream can report it's total length. */
michael@0 126 virtual bool hasLength() const { return false; }
michael@0 127 /** Returns the total length of the stream. If this cannot be done, returns 0. */
michael@0 128 virtual size_t getLength() const { return 0; }
michael@0 129
michael@0 130 //SkStreamMemory
michael@0 131 /** Returns the starting address for the data. If this cannot be done, returns NULL. */
michael@0 132 //TODO: replace with virtual const SkData* getData()
michael@0 133 virtual const void* getMemoryBase() { return NULL; }
michael@0 134
michael@0 135 private:
michael@0 136 typedef SkRefCnt INHERITED;
michael@0 137 };
michael@0 138
michael@0 139 /** SkStreamRewindable is a SkStream for which rewind and duplicate are required. */
michael@0 140 class SK_API SkStreamRewindable : public SkStream {
michael@0 141 public:
michael@0 142 virtual bool rewind() SK_OVERRIDE = 0;
michael@0 143 virtual SkStreamRewindable* duplicate() const SK_OVERRIDE = 0;
michael@0 144 };
michael@0 145
michael@0 146 /** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */
michael@0 147 class SK_API SkStreamSeekable : public SkStreamRewindable {
michael@0 148 public:
michael@0 149 virtual SkStreamSeekable* duplicate() const SK_OVERRIDE = 0;
michael@0 150
michael@0 151 virtual bool hasPosition() const SK_OVERRIDE { return true; }
michael@0 152 virtual size_t getPosition() const SK_OVERRIDE = 0;
michael@0 153 virtual bool seek(size_t position) SK_OVERRIDE = 0;
michael@0 154 virtual bool move(long offset) SK_OVERRIDE = 0;
michael@0 155 virtual SkStreamSeekable* fork() const SK_OVERRIDE = 0;
michael@0 156 };
michael@0 157
michael@0 158 /** SkStreamAsset is a SkStreamSeekable for which getLength is required. */
michael@0 159 class SK_API SkStreamAsset : public SkStreamSeekable {
michael@0 160 public:
michael@0 161 virtual SkStreamAsset* duplicate() const SK_OVERRIDE = 0;
michael@0 162 virtual SkStreamAsset* fork() const SK_OVERRIDE = 0;
michael@0 163
michael@0 164 virtual bool hasLength() const SK_OVERRIDE { return true; }
michael@0 165 virtual size_t getLength() const SK_OVERRIDE = 0;
michael@0 166 };
michael@0 167
michael@0 168 /** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */
michael@0 169 class SK_API SkStreamMemory : public SkStreamAsset {
michael@0 170 public:
michael@0 171 virtual SkStreamMemory* duplicate() const SK_OVERRIDE = 0;
michael@0 172 virtual SkStreamMemory* fork() const SK_OVERRIDE = 0;
michael@0 173
michael@0 174 virtual const void* getMemoryBase() SK_OVERRIDE = 0;
michael@0 175 };
michael@0 176
michael@0 177 class SK_API SkWStream : SkNoncopyable {
michael@0 178 public:
michael@0 179 SK_DECLARE_INST_COUNT_ROOT(SkWStream)
michael@0 180
michael@0 181 virtual ~SkWStream();
michael@0 182
michael@0 183 /** Called to write bytes to a SkWStream. Returns true on success
michael@0 184 @param buffer the address of at least size bytes to be written to the stream
michael@0 185 @param size The number of bytes in buffer to write to the stream
michael@0 186 @return true on success
michael@0 187 */
michael@0 188 virtual bool write(const void* buffer, size_t size) = 0;
michael@0 189 virtual void newline();
michael@0 190 virtual void flush();
michael@0 191
michael@0 192 virtual size_t bytesWritten() const = 0;
michael@0 193
michael@0 194 // helpers
michael@0 195
michael@0 196 bool write8(U8CPU);
michael@0 197 bool write16(U16CPU);
michael@0 198 bool write32(uint32_t);
michael@0 199
michael@0 200 bool writeText(const char text[]);
michael@0 201 bool writeDecAsText(int32_t);
michael@0 202 bool writeBigDecAsText(int64_t, int minDigits = 0);
michael@0 203 bool writeHexAsText(uint32_t, int minDigits = 0);
michael@0 204 bool writeScalarAsText(SkScalar);
michael@0 205
michael@0 206 bool writeBool(bool v) { return this->write8(v); }
michael@0 207 bool writeScalar(SkScalar);
michael@0 208 bool writePackedUInt(size_t);
michael@0 209
michael@0 210 bool writeStream(SkStream* input, size_t length);
michael@0 211
michael@0 212 /**
michael@0 213 * Append an SkData object to the stream, such that it can be read
michael@0 214 * out of the stream using SkStream::readData().
michael@0 215 *
michael@0 216 * Note that the encoding method used to write the SkData object
michael@0 217 * to the stream may change over time. This method DOES NOT
michael@0 218 * just write the raw content of the SkData object to the stream.
michael@0 219 */
michael@0 220 bool writeData(const SkData*);
michael@0 221
michael@0 222 /**
michael@0 223 * This returns the number of bytes in the stream required to store
michael@0 224 * 'value'.
michael@0 225 */
michael@0 226 static int SizeOfPackedUInt(size_t value);
michael@0 227 };
michael@0 228
michael@0 229 ////////////////////////////////////////////////////////////////////////////////////////
michael@0 230
michael@0 231 #include "SkString.h"
michael@0 232 #include <stdio.h>
michael@0 233
michael@0 234 struct SkFILE;
michael@0 235
michael@0 236 /** A stream that wraps a C FILE* file stream. */
michael@0 237 class SK_API SkFILEStream : public SkStreamAsset {
michael@0 238 public:
michael@0 239 SK_DECLARE_INST_COUNT(SkFILEStream)
michael@0 240
michael@0 241 /** Initialize the stream by calling sk_fopen on the specified path.
michael@0 242 * This internal stream will be closed in the destructor.
michael@0 243 */
michael@0 244 explicit SkFILEStream(const char path[] = NULL);
michael@0 245
michael@0 246 enum Ownership {
michael@0 247 kCallerPasses_Ownership,
michael@0 248 kCallerRetains_Ownership
michael@0 249 };
michael@0 250 /** Initialize the stream with an existing C file stream.
michael@0 251 * While this stream exists, it assumes exclusive access to the C file stream.
michael@0 252 * The C file stream will be closed in the destructor unless the caller specifies
michael@0 253 * kCallerRetains_Ownership.
michael@0 254 */
michael@0 255 explicit SkFILEStream(FILE* file, Ownership ownership = kCallerPasses_Ownership);
michael@0 256
michael@0 257 virtual ~SkFILEStream();
michael@0 258
michael@0 259 /** Returns true if the current path could be opened. */
michael@0 260 bool isValid() const { return fFILE != NULL; }
michael@0 261
michael@0 262 /** Close the current file, and open a new file with the specified path.
michael@0 263 * If path is NULL, just close the current file.
michael@0 264 */
michael@0 265 void setPath(const char path[]);
michael@0 266
michael@0 267 virtual size_t read(void* buffer, size_t size) SK_OVERRIDE;
michael@0 268 virtual bool isAtEnd() const SK_OVERRIDE;
michael@0 269
michael@0 270 virtual bool rewind() SK_OVERRIDE;
michael@0 271 virtual SkStreamAsset* duplicate() const SK_OVERRIDE;
michael@0 272
michael@0 273 virtual size_t getPosition() const SK_OVERRIDE;
michael@0 274 virtual bool seek(size_t position) SK_OVERRIDE;
michael@0 275 virtual bool move(long offset) SK_OVERRIDE;
michael@0 276 virtual SkStreamAsset* fork() const SK_OVERRIDE;
michael@0 277
michael@0 278 virtual size_t getLength() const SK_OVERRIDE;
michael@0 279
michael@0 280 virtual const void* getMemoryBase() SK_OVERRIDE;
michael@0 281
michael@0 282 private:
michael@0 283 SkFILE* fFILE;
michael@0 284 SkString fName;
michael@0 285 Ownership fOwnership;
michael@0 286 // fData is lazilly initialized when needed.
michael@0 287 mutable SkAutoTUnref<SkData> fData;
michael@0 288
michael@0 289 typedef SkStreamAsset INHERITED;
michael@0 290 };
michael@0 291
michael@0 292 class SK_API SkMemoryStream : public SkStreamMemory {
michael@0 293 public:
michael@0 294 SK_DECLARE_INST_COUNT(SkMemoryStream)
michael@0 295
michael@0 296 SkMemoryStream();
michael@0 297
michael@0 298 /** We allocate (and free) the memory. Write to it via getMemoryBase() */
michael@0 299 SkMemoryStream(size_t length);
michael@0 300
michael@0 301 /** If copyData is true, the stream makes a private copy of the data. */
michael@0 302 SkMemoryStream(const void* data, size_t length, bool copyData = false);
michael@0 303
michael@0 304 /** Use the specified data as the memory for this stream.
michael@0 305 * The stream will call ref() on the data (assuming it is not NULL).
michael@0 306 */
michael@0 307 SkMemoryStream(SkData*);
michael@0 308
michael@0 309 virtual ~SkMemoryStream();
michael@0 310
michael@0 311 /** Resets the stream to the specified data and length,
michael@0 312 just like the constructor.
michael@0 313 if copyData is true, the stream makes a private copy of the data
michael@0 314 */
michael@0 315 virtual void setMemory(const void* data, size_t length,
michael@0 316 bool copyData = false);
michael@0 317 /** Replace any memory buffer with the specified buffer. The caller
michael@0 318 must have allocated data with sk_malloc or sk_realloc, since it
michael@0 319 will be freed with sk_free.
michael@0 320 */
michael@0 321 void setMemoryOwned(const void* data, size_t length);
michael@0 322
michael@0 323 /** Return the stream's data in a SkData.
michael@0 324 * The caller must call unref() when it is finished using the data.
michael@0 325 */
michael@0 326 SkData* copyToData() const;
michael@0 327
michael@0 328 /**
michael@0 329 * Use the specified data as the memory for this stream.
michael@0 330 * The stream will call ref() on the data (assuming it is not NULL).
michael@0 331 * The function returns the data parameter as a convenience.
michael@0 332 */
michael@0 333 SkData* setData(SkData*);
michael@0 334
michael@0 335 void skipToAlign4();
michael@0 336 const void* getAtPos();
michael@0 337 size_t peek() const { return fOffset; }
michael@0 338
michael@0 339 virtual size_t read(void* buffer, size_t size) SK_OVERRIDE;
michael@0 340 virtual bool isAtEnd() const SK_OVERRIDE;
michael@0 341
michael@0 342 virtual bool rewind() SK_OVERRIDE;
michael@0 343 virtual SkMemoryStream* duplicate() const SK_OVERRIDE;
michael@0 344
michael@0 345 virtual size_t getPosition() const SK_OVERRIDE;
michael@0 346 virtual bool seek(size_t position) SK_OVERRIDE;
michael@0 347 virtual bool move(long offset) SK_OVERRIDE;
michael@0 348 virtual SkMemoryStream* fork() const SK_OVERRIDE;
michael@0 349
michael@0 350 virtual size_t getLength() const SK_OVERRIDE;
michael@0 351
michael@0 352 virtual const void* getMemoryBase() SK_OVERRIDE;
michael@0 353
michael@0 354 private:
michael@0 355 SkData* fData;
michael@0 356 size_t fOffset;
michael@0 357
michael@0 358 typedef SkStreamMemory INHERITED;
michael@0 359 };
michael@0 360
michael@0 361 /////////////////////////////////////////////////////////////////////////////////////////////
michael@0 362
michael@0 363 class SK_API SkFILEWStream : public SkWStream {
michael@0 364 public:
michael@0 365 SK_DECLARE_INST_COUNT(SkFILEWStream)
michael@0 366
michael@0 367 SkFILEWStream(const char path[]);
michael@0 368 virtual ~SkFILEWStream();
michael@0 369
michael@0 370 /** Returns true if the current path could be opened.
michael@0 371 */
michael@0 372 bool isValid() const { return fFILE != NULL; }
michael@0 373
michael@0 374 virtual bool write(const void* buffer, size_t size) SK_OVERRIDE;
michael@0 375 virtual void flush() SK_OVERRIDE;
michael@0 376 virtual size_t bytesWritten() const SK_OVERRIDE;
michael@0 377
michael@0 378 private:
michael@0 379 SkFILE* fFILE;
michael@0 380
michael@0 381 typedef SkWStream INHERITED;
michael@0 382 };
michael@0 383
michael@0 384 class SkMemoryWStream : public SkWStream {
michael@0 385 public:
michael@0 386 SK_DECLARE_INST_COUNT(SkMemoryWStream)
michael@0 387
michael@0 388 SkMemoryWStream(void* buffer, size_t size);
michael@0 389 virtual bool write(const void* buffer, size_t size) SK_OVERRIDE;
michael@0 390 virtual size_t bytesWritten() const SK_OVERRIDE { return fBytesWritten; }
michael@0 391
michael@0 392 private:
michael@0 393 char* fBuffer;
michael@0 394 size_t fMaxLength;
michael@0 395 size_t fBytesWritten;
michael@0 396
michael@0 397 typedef SkWStream INHERITED;
michael@0 398 };
michael@0 399
michael@0 400 class SK_API SkDynamicMemoryWStream : public SkWStream {
michael@0 401 public:
michael@0 402 SK_DECLARE_INST_COUNT(SkDynamicMemoryWStream)
michael@0 403
michael@0 404 SkDynamicMemoryWStream();
michael@0 405 virtual ~SkDynamicMemoryWStream();
michael@0 406
michael@0 407 virtual bool write(const void* buffer, size_t size) SK_OVERRIDE;
michael@0 408 virtual size_t bytesWritten() const SK_OVERRIDE { return fBytesWritten; }
michael@0 409 // random access write
michael@0 410 // modifies stream and returns true if offset + size is less than or equal to getOffset()
michael@0 411 bool write(const void* buffer, size_t offset, size_t size);
michael@0 412 bool read(void* buffer, size_t offset, size_t size);
michael@0 413 size_t getOffset() const { return fBytesWritten; }
michael@0 414
michael@0 415 // copy what has been written to the stream into dst
michael@0 416 void copyTo(void* dst) const;
michael@0 417
michael@0 418 /**
michael@0 419 * Return a copy of the data written so far. This call is responsible for
michael@0 420 * calling unref() when they are finished with the data.
michael@0 421 */
michael@0 422 SkData* copyToData() const;
michael@0 423
michael@0 424 /** Reset, returning a reader stream with the current content. */
michael@0 425 SkStreamAsset* detachAsStream();
michael@0 426
michael@0 427 /** Reset the stream to its original, empty, state. */
michael@0 428 void reset();
michael@0 429 void padToAlign4();
michael@0 430 private:
michael@0 431 struct Block;
michael@0 432 Block* fHead;
michael@0 433 Block* fTail;
michael@0 434 size_t fBytesWritten;
michael@0 435 mutable SkData* fCopy; // is invalidated if we write after it is created
michael@0 436
michael@0 437 void invalidateCopy();
michael@0 438
michael@0 439 // For access to the Block type.
michael@0 440 friend class SkBlockMemoryStream;
michael@0 441 friend class SkBlockMemoryRefCnt;
michael@0 442
michael@0 443 typedef SkWStream INHERITED;
michael@0 444 };
michael@0 445
michael@0 446
michael@0 447 class SK_API SkDebugWStream : public SkWStream {
michael@0 448 public:
michael@0 449 SkDebugWStream() : fBytesWritten(0) {}
michael@0 450 SK_DECLARE_INST_COUNT(SkDebugWStream)
michael@0 451
michael@0 452 // overrides
michael@0 453 virtual bool write(const void* buffer, size_t size) SK_OVERRIDE;
michael@0 454 virtual void newline() SK_OVERRIDE;
michael@0 455 virtual size_t bytesWritten() const SK_OVERRIDE { return fBytesWritten; }
michael@0 456
michael@0 457 private:
michael@0 458 size_t fBytesWritten;
michael@0 459 typedef SkWStream INHERITED;
michael@0 460 };
michael@0 461
michael@0 462 // for now
michael@0 463 typedef SkFILEStream SkURLStream;
michael@0 464
michael@0 465 #endif

mercurial