image/decoders/nsIconDecoder.cpp

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsIconDecoder.h"
michael@0 8 #include "nsIInputStream.h"
michael@0 9 #include "RasterImage.h"
michael@0 10 #include "nspr.h"
michael@0 11 #include "nsRect.h"
michael@0 12
michael@0 13 #include "nsError.h"
michael@0 14 #include <algorithm>
michael@0 15
michael@0 16 namespace mozilla {
michael@0 17 namespace image {
michael@0 18
michael@0 19 nsIconDecoder::nsIconDecoder(RasterImage &aImage)
michael@0 20 : Decoder(aImage),
michael@0 21 mWidth(-1),
michael@0 22 mHeight(-1),
michael@0 23 mPixBytesRead(0),
michael@0 24 mState(iconStateStart)
michael@0 25 {
michael@0 26 // Nothing to do
michael@0 27 }
michael@0 28
michael@0 29 nsIconDecoder::~nsIconDecoder()
michael@0 30 { }
michael@0 31
michael@0 32 void
michael@0 33 nsIconDecoder::WriteInternal(const char *aBuffer, uint32_t aCount, DecodeStrategy)
michael@0 34 {
michael@0 35 NS_ABORT_IF_FALSE(!HasError(), "Shouldn't call WriteInternal after error!");
michael@0 36
michael@0 37 // We put this here to avoid errors about crossing initialization with case
michael@0 38 // jumps on linux.
michael@0 39 uint32_t bytesToRead = 0;
michael@0 40
michael@0 41 // Loop until the input data is gone
michael@0 42 while (aCount > 0) {
michael@0 43 switch (mState) {
michael@0 44 case iconStateStart:
michael@0 45
michael@0 46 // Grab the width
michael@0 47 mWidth = (uint8_t)*aBuffer;
michael@0 48
michael@0 49 // Book Keeping
michael@0 50 aBuffer++;
michael@0 51 aCount--;
michael@0 52 mState = iconStateHaveHeight;
michael@0 53 break;
michael@0 54
michael@0 55 case iconStateHaveHeight:
michael@0 56
michael@0 57 // Grab the Height
michael@0 58 mHeight = (uint8_t)*aBuffer;
michael@0 59
michael@0 60 // Post our size to the superclass
michael@0 61 PostSize(mWidth, mHeight);
michael@0 62 if (HasError()) {
michael@0 63 // Setting the size led to an error.
michael@0 64 mState = iconStateFinished;
michael@0 65 return;
michael@0 66 }
michael@0 67
michael@0 68 // If We're doing a size decode, we're done
michael@0 69 if (IsSizeDecode()) {
michael@0 70 mState = iconStateFinished;
michael@0 71 break;
michael@0 72 }
michael@0 73
michael@0 74 if (!mImageData) {
michael@0 75 PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
michael@0 76 return;
michael@0 77 }
michael@0 78
michael@0 79 // Book Keeping
michael@0 80 aBuffer++;
michael@0 81 aCount--;
michael@0 82 mState = iconStateReadPixels;
michael@0 83 break;
michael@0 84
michael@0 85 case iconStateReadPixels:
michael@0 86 {
michael@0 87
michael@0 88 // How many bytes are we reading?
michael@0 89 bytesToRead = std::min(aCount, mImageDataLength - mPixBytesRead);
michael@0 90
michael@0 91 // Copy the bytes
michael@0 92 memcpy(mImageData + mPixBytesRead, aBuffer, bytesToRead);
michael@0 93
michael@0 94 // Performance isn't critical here, so our update rectangle is
michael@0 95 // always the full icon
michael@0 96 nsIntRect r(0, 0, mWidth, mHeight);
michael@0 97
michael@0 98 // Invalidate
michael@0 99 PostInvalidation(r);
michael@0 100
michael@0 101 // Book Keeping
michael@0 102 aBuffer += bytesToRead;
michael@0 103 aCount -= bytesToRead;
michael@0 104 mPixBytesRead += bytesToRead;
michael@0 105
michael@0 106 // If we've got all the pixel bytes, we're finished
michael@0 107 if (mPixBytesRead == mImageDataLength) {
michael@0 108 PostFrameStop();
michael@0 109 PostDecodeDone();
michael@0 110 mState = iconStateFinished;
michael@0 111 }
michael@0 112 break;
michael@0 113 }
michael@0 114
michael@0 115 case iconStateFinished:
michael@0 116
michael@0 117 // Consume all excess data silently
michael@0 118 aCount = 0;
michael@0 119
michael@0 120 break;
michael@0 121 }
michael@0 122 }
michael@0 123 }
michael@0 124
michael@0 125 } // namespace image
michael@0 126 } // namespace mozilla

mercurial