image/src/FrameSequence.h

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 #ifndef mozilla_imagelib_FrameSequence_h_
michael@0 8 #define mozilla_imagelib_FrameSequence_h_
michael@0 9
michael@0 10 #include "nsTArray.h"
michael@0 11 #include "mozilla/MemoryReporting.h"
michael@0 12 #include "gfxTypes.h"
michael@0 13 #include "imgFrame.h"
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16 namespace image {
michael@0 17
michael@0 18 /**
michael@0 19 * FrameDataPair is a slightly-smart tuple of (frame, raw frame data) where the
michael@0 20 * raw frame data is allowed to be (and is, initially) null.
michael@0 21 *
michael@0 22 * If you call LockAndGetData, you will be able to call GetFrameData() on that
michael@0 23 * instance, and when the FrameDataPair is destructed, the imgFrame lock will
michael@0 24 * be unlocked.
michael@0 25 */
michael@0 26 class FrameDataPair
michael@0 27 {
michael@0 28 public:
michael@0 29 explicit FrameDataPair(imgFrame* frame)
michael@0 30 : mFrame(frame)
michael@0 31 , mFrameData(nullptr)
michael@0 32 {}
michael@0 33
michael@0 34 FrameDataPair()
michael@0 35 : mFrame(nullptr)
michael@0 36 , mFrameData(nullptr)
michael@0 37 {}
michael@0 38
michael@0 39 FrameDataPair(FrameDataPair& other)
michael@0 40 {
michael@0 41 mFrame = other.mFrame;
michael@0 42 mFrameData = other.mFrameData;
michael@0 43
michael@0 44 // since mFrame is an nsAutoPtr, the assignment operator above actually
michael@0 45 // nulls out other.mFrame. In order to fully assume ownership over the
michael@0 46 // frame, we also null out the other's mFrameData.
michael@0 47 other.mFrameData = nullptr;
michael@0 48 }
michael@0 49
michael@0 50 ~FrameDataPair()
michael@0 51 {
michael@0 52 if (mFrameData) {
michael@0 53 mFrame->UnlockImageData();
michael@0 54 }
michael@0 55 }
michael@0 56
michael@0 57 // Lock the frame and store its mFrameData. The frame will be unlocked (and
michael@0 58 // deleted) when this FrameDataPair is deleted.
michael@0 59 void LockAndGetData()
michael@0 60 {
michael@0 61 if (mFrame) {
michael@0 62 if (NS_SUCCEEDED(mFrame->LockImageData())) {
michael@0 63 if (mFrame->GetIsPaletted()) {
michael@0 64 mFrameData = reinterpret_cast<uint8_t*>(mFrame->GetPaletteData());
michael@0 65 } else {
michael@0 66 mFrameData = mFrame->GetImageData();
michael@0 67 }
michael@0 68 }
michael@0 69 }
michael@0 70 }
michael@0 71
michael@0 72 // Null out this FrameDataPair and return its frame. You must ensure the
michael@0 73 // frame will be deleted separately.
michael@0 74 imgFrame* Forget()
michael@0 75 {
michael@0 76 if (mFrameData) {
michael@0 77 mFrame->UnlockImageData();
michael@0 78 }
michael@0 79
michael@0 80 imgFrame* frame = mFrame.forget();
michael@0 81 mFrameData = nullptr;
michael@0 82 return frame;
michael@0 83 }
michael@0 84
michael@0 85 bool HasFrameData() const
michael@0 86 {
michael@0 87 if (mFrameData) {
michael@0 88 MOZ_ASSERT(!!mFrame);
michael@0 89 }
michael@0 90 return !!mFrameData;
michael@0 91 }
michael@0 92
michael@0 93 uint8_t* GetFrameData() const
michael@0 94 {
michael@0 95 return mFrameData;
michael@0 96 }
michael@0 97
michael@0 98 imgFrame* GetFrame() const
michael@0 99 {
michael@0 100 return mFrame;
michael@0 101 }
michael@0 102
michael@0 103 // Resets this FrameDataPair to work with a different frame. Takes ownership
michael@0 104 // of the frame, deleting the old frame (if any).
michael@0 105 void SetFrame(imgFrame* frame)
michael@0 106 {
michael@0 107 if (mFrameData) {
michael@0 108 mFrame->UnlockImageData();
michael@0 109 }
michael@0 110
michael@0 111 mFrame = frame;
michael@0 112 mFrameData = nullptr;
michael@0 113 }
michael@0 114
michael@0 115 operator imgFrame*() const
michael@0 116 {
michael@0 117 return GetFrame();
michael@0 118 }
michael@0 119
michael@0 120 imgFrame* operator->() const
michael@0 121 {
michael@0 122 return GetFrame();
michael@0 123 }
michael@0 124
michael@0 125 bool operator==(imgFrame* other) const
michael@0 126 {
michael@0 127 return mFrame == other;
michael@0 128 }
michael@0 129
michael@0 130 private:
michael@0 131 nsAutoPtr<imgFrame> mFrame;
michael@0 132 uint8_t* mFrameData;
michael@0 133 };
michael@0 134
michael@0 135 /**
michael@0 136 * FrameSequence stores image frames (and their associated raw data pointers).
michael@0 137 * It is little more than a smart array.
michael@0 138 */
michael@0 139 class FrameSequence
michael@0 140 {
michael@0 141 public:
michael@0 142
michael@0 143 ~FrameSequence();
michael@0 144
michael@0 145 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FrameSequence)
michael@0 146
michael@0 147 /**
michael@0 148 * Get the read-only (frame, data) pair at index aIndex.
michael@0 149 */
michael@0 150 const FrameDataPair& GetFrame(uint32_t aIndex) const;
michael@0 151
michael@0 152 /**
michael@0 153 * Insert a frame into the array. FrameSequence takes ownership of the frame.
michael@0 154 */
michael@0 155 void InsertFrame(uint32_t framenum, imgFrame* aFrame);
michael@0 156
michael@0 157 /**
michael@0 158 * Remove (and delete) the frame at index framenum.
michael@0 159 */
michael@0 160 void RemoveFrame(uint32_t framenum);
michael@0 161
michael@0 162 /**
michael@0 163 * Swap aFrame with the frame at sequence framenum, and return that frame.
michael@0 164 * You take ownership over the frame returned.
michael@0 165 */
michael@0 166 imgFrame* SwapFrame(uint32_t framenum, imgFrame* aFrame);
michael@0 167
michael@0 168 /**
michael@0 169 * Remove (and delete) all frames.
michael@0 170 */
michael@0 171 void ClearFrames();
michael@0 172
michael@0 173 /* The total number of frames in this image. */
michael@0 174 uint32_t GetNumFrames() const;
michael@0 175
michael@0 176 size_t SizeOfDecodedWithComputedFallbackIfHeap(gfxMemoryLocation aLocation,
michael@0 177 mozilla::MallocSizeOf aMallocSizeOf) const;
michael@0 178
michael@0 179 private: // data
michael@0 180 //! All the frames of the image
michael@0 181 nsTArray<FrameDataPair> mFrames;
michael@0 182 };
michael@0 183
michael@0 184 } // namespace image
michael@0 185 } // namespace mozilla
michael@0 186
michael@0 187 #endif /* mozilla_imagelib_FrameSequence_h_ */

mercurial