Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | // StreamBinder.cpp |
michael@0 | 2 | |
michael@0 | 3 | #include "StdAfx.h" |
michael@0 | 4 | |
michael@0 | 5 | #include "StreamBinder.h" |
michael@0 | 6 | #include "../../Common/Defs.h" |
michael@0 | 7 | #include "../../Common/MyCom.h" |
michael@0 | 8 | |
michael@0 | 9 | using namespace NWindows; |
michael@0 | 10 | using namespace NSynchronization; |
michael@0 | 11 | |
michael@0 | 12 | class CSequentialInStreamForBinder: |
michael@0 | 13 | public ISequentialInStream, |
michael@0 | 14 | public CMyUnknownImp |
michael@0 | 15 | { |
michael@0 | 16 | public: |
michael@0 | 17 | MY_UNKNOWN_IMP |
michael@0 | 18 | |
michael@0 | 19 | STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); |
michael@0 | 20 | private: |
michael@0 | 21 | CStreamBinder *m_StreamBinder; |
michael@0 | 22 | public: |
michael@0 | 23 | ~CSequentialInStreamForBinder() { m_StreamBinder->CloseRead(); } |
michael@0 | 24 | void SetBinder(CStreamBinder *streamBinder) { m_StreamBinder = streamBinder; } |
michael@0 | 25 | }; |
michael@0 | 26 | |
michael@0 | 27 | STDMETHODIMP CSequentialInStreamForBinder::Read(void *data, UInt32 size, UInt32 *processedSize) |
michael@0 | 28 | { return m_StreamBinder->Read(data, size, processedSize); } |
michael@0 | 29 | |
michael@0 | 30 | class CSequentialOutStreamForBinder: |
michael@0 | 31 | public ISequentialOutStream, |
michael@0 | 32 | public CMyUnknownImp |
michael@0 | 33 | { |
michael@0 | 34 | public: |
michael@0 | 35 | MY_UNKNOWN_IMP |
michael@0 | 36 | |
michael@0 | 37 | STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); |
michael@0 | 38 | |
michael@0 | 39 | private: |
michael@0 | 40 | CStreamBinder *m_StreamBinder; |
michael@0 | 41 | public: |
michael@0 | 42 | ~CSequentialOutStreamForBinder() { m_StreamBinder->CloseWrite(); } |
michael@0 | 43 | void SetBinder(CStreamBinder *streamBinder) { m_StreamBinder = streamBinder; } |
michael@0 | 44 | }; |
michael@0 | 45 | |
michael@0 | 46 | STDMETHODIMP CSequentialOutStreamForBinder::Write(const void *data, UInt32 size, UInt32 *processedSize) |
michael@0 | 47 | { return m_StreamBinder->Write(data, size, processedSize); } |
michael@0 | 48 | |
michael@0 | 49 | |
michael@0 | 50 | ////////////////////////// |
michael@0 | 51 | // CStreamBinder |
michael@0 | 52 | // (_thereAreBytesToReadEvent && _bufferSize == 0) means that stream is finished. |
michael@0 | 53 | |
michael@0 | 54 | void CStreamBinder::CreateEvents() |
michael@0 | 55 | { |
michael@0 | 56 | _allBytesAreWritenEvent = new CManualResetEvent(true); |
michael@0 | 57 | _thereAreBytesToReadEvent = new CManualResetEvent(false); |
michael@0 | 58 | _readStreamIsClosedEvent = new CManualResetEvent(false); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | void CStreamBinder::ReInit() |
michael@0 | 62 | { |
michael@0 | 63 | _thereAreBytesToReadEvent->Reset(); |
michael@0 | 64 | _readStreamIsClosedEvent->Reset(); |
michael@0 | 65 | ProcessedSize = 0; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | CStreamBinder::~CStreamBinder() |
michael@0 | 69 | { |
michael@0 | 70 | if (_allBytesAreWritenEvent != NULL) |
michael@0 | 71 | delete _allBytesAreWritenEvent; |
michael@0 | 72 | if (_thereAreBytesToReadEvent != NULL) |
michael@0 | 73 | delete _thereAreBytesToReadEvent; |
michael@0 | 74 | if (_readStreamIsClosedEvent != NULL) |
michael@0 | 75 | delete _readStreamIsClosedEvent; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | |
michael@0 | 79 | |
michael@0 | 80 | |
michael@0 | 81 | void CStreamBinder::CreateStreams(ISequentialInStream **inStream, |
michael@0 | 82 | ISequentialOutStream **outStream) |
michael@0 | 83 | { |
michael@0 | 84 | CSequentialInStreamForBinder *inStreamSpec = new |
michael@0 | 85 | CSequentialInStreamForBinder; |
michael@0 | 86 | CMyComPtr<ISequentialInStream> inStreamLoc(inStreamSpec); |
michael@0 | 87 | inStreamSpec->SetBinder(this); |
michael@0 | 88 | *inStream = inStreamLoc.Detach(); |
michael@0 | 89 | |
michael@0 | 90 | CSequentialOutStreamForBinder *outStreamSpec = new |
michael@0 | 91 | CSequentialOutStreamForBinder; |
michael@0 | 92 | CMyComPtr<ISequentialOutStream> outStreamLoc(outStreamSpec); |
michael@0 | 93 | outStreamSpec->SetBinder(this); |
michael@0 | 94 | *outStream = outStreamLoc.Detach(); |
michael@0 | 95 | |
michael@0 | 96 | _buffer = NULL; |
michael@0 | 97 | _bufferSize= 0; |
michael@0 | 98 | ProcessedSize = 0; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | HRESULT CStreamBinder::Read(void *data, UInt32 size, UInt32 *processedSize) |
michael@0 | 102 | { |
michael@0 | 103 | UInt32 sizeToRead = size; |
michael@0 | 104 | if (size > 0) |
michael@0 | 105 | { |
michael@0 | 106 | if(!_thereAreBytesToReadEvent->Lock()) |
michael@0 | 107 | return E_FAIL; |
michael@0 | 108 | sizeToRead = MyMin(_bufferSize, size); |
michael@0 | 109 | if (_bufferSize > 0) |
michael@0 | 110 | { |
michael@0 | 111 | MoveMemory(data, _buffer, sizeToRead); |
michael@0 | 112 | _buffer = ((const Byte *)_buffer) + sizeToRead; |
michael@0 | 113 | _bufferSize -= sizeToRead; |
michael@0 | 114 | if (_bufferSize == 0) |
michael@0 | 115 | { |
michael@0 | 116 | _thereAreBytesToReadEvent->Reset(); |
michael@0 | 117 | _allBytesAreWritenEvent->Set(); |
michael@0 | 118 | } |
michael@0 | 119 | } |
michael@0 | 120 | } |
michael@0 | 121 | if (processedSize != NULL) |
michael@0 | 122 | *processedSize = sizeToRead; |
michael@0 | 123 | ProcessedSize += sizeToRead; |
michael@0 | 124 | return S_OK; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | void CStreamBinder::CloseRead() |
michael@0 | 128 | { |
michael@0 | 129 | _readStreamIsClosedEvent->Set(); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | HRESULT CStreamBinder::Write(const void *data, UInt32 size, UInt32 *processedSize) |
michael@0 | 133 | { |
michael@0 | 134 | if (size > 0) |
michael@0 | 135 | { |
michael@0 | 136 | _buffer = data; |
michael@0 | 137 | _bufferSize = size; |
michael@0 | 138 | _allBytesAreWritenEvent->Reset(); |
michael@0 | 139 | _thereAreBytesToReadEvent->Set(); |
michael@0 | 140 | |
michael@0 | 141 | HANDLE events[2]; |
michael@0 | 142 | events[0] = *_allBytesAreWritenEvent; |
michael@0 | 143 | events[1] = *_readStreamIsClosedEvent; |
michael@0 | 144 | DWORD waitResult = ::WaitForMultipleObjects(2, events, FALSE, INFINITE); |
michael@0 | 145 | if (waitResult != WAIT_OBJECT_0 + 0) |
michael@0 | 146 | { |
michael@0 | 147 | // ReadingWasClosed = true; |
michael@0 | 148 | return E_FAIL; |
michael@0 | 149 | } |
michael@0 | 150 | // if(!_allBytesAreWritenEvent.Lock()) |
michael@0 | 151 | // return E_FAIL; |
michael@0 | 152 | } |
michael@0 | 153 | if (processedSize != NULL) |
michael@0 | 154 | *processedSize = size; |
michael@0 | 155 | return S_OK; |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | void CStreamBinder::CloseWrite() |
michael@0 | 159 | { |
michael@0 | 160 | // _bufferSize must be = 0 |
michael@0 | 161 | _thereAreBytesToReadEvent->Set(); |
michael@0 | 162 | } |