Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
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 file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "FileStreamWrappers.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "nsIFileStorage.h" |
michael@0 | 10 | #include "nsISeekableStream.h" |
michael@0 | 11 | #include "mozilla/Attributes.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "FileHelper.h" |
michael@0 | 14 | |
michael@0 | 15 | USING_FILE_NAMESPACE |
michael@0 | 16 | |
michael@0 | 17 | namespace { |
michael@0 | 18 | |
michael@0 | 19 | class ProgressRunnable MOZ_FINAL : public nsIRunnable |
michael@0 | 20 | { |
michael@0 | 21 | public: |
michael@0 | 22 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 23 | NS_DECL_NSIRUNNABLE |
michael@0 | 24 | |
michael@0 | 25 | ProgressRunnable(FileHelper* aFileHelper, |
michael@0 | 26 | uint64_t aProgress, |
michael@0 | 27 | uint64_t aProgressMax) |
michael@0 | 28 | : mFileHelper(aFileHelper), |
michael@0 | 29 | mProgress(aProgress), |
michael@0 | 30 | mProgressMax(aProgressMax) |
michael@0 | 31 | { |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | private: |
michael@0 | 35 | nsRefPtr<FileHelper> mFileHelper; |
michael@0 | 36 | uint64_t mProgress; |
michael@0 | 37 | uint64_t mProgressMax; |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | class CloseRunnable MOZ_FINAL : public nsIRunnable |
michael@0 | 41 | { |
michael@0 | 42 | public: |
michael@0 | 43 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 44 | NS_DECL_NSIRUNNABLE |
michael@0 | 45 | |
michael@0 | 46 | CloseRunnable(FileHelper* aFileHelper) |
michael@0 | 47 | : mFileHelper(aFileHelper) |
michael@0 | 48 | { } |
michael@0 | 49 | |
michael@0 | 50 | private: |
michael@0 | 51 | nsRefPtr<FileHelper> mFileHelper; |
michael@0 | 52 | }; |
michael@0 | 53 | |
michael@0 | 54 | class DestroyRunnable MOZ_FINAL : public nsIRunnable |
michael@0 | 55 | { |
michael@0 | 56 | public: |
michael@0 | 57 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 58 | NS_DECL_NSIRUNNABLE |
michael@0 | 59 | |
michael@0 | 60 | DestroyRunnable(FileHelper* aFileHelper) |
michael@0 | 61 | : mFileHelper(aFileHelper) |
michael@0 | 62 | { } |
michael@0 | 63 | |
michael@0 | 64 | private: |
michael@0 | 65 | nsRefPtr<FileHelper> mFileHelper; |
michael@0 | 66 | }; |
michael@0 | 67 | |
michael@0 | 68 | } // anonymous namespace |
michael@0 | 69 | |
michael@0 | 70 | FileStreamWrapper::FileStreamWrapper(nsISupports* aFileStream, |
michael@0 | 71 | FileHelper* aFileHelper, |
michael@0 | 72 | uint64_t aOffset, |
michael@0 | 73 | uint64_t aLimit, |
michael@0 | 74 | uint32_t aFlags) |
michael@0 | 75 | : mFileStream(aFileStream), |
michael@0 | 76 | mFileHelper(aFileHelper), |
michael@0 | 77 | mOffset(aOffset), |
michael@0 | 78 | mLimit(aLimit), |
michael@0 | 79 | mFlags(aFlags), |
michael@0 | 80 | mFirstTime(true) |
michael@0 | 81 | { |
michael@0 | 82 | NS_ASSERTION(mFileStream, "Must have a file stream!"); |
michael@0 | 83 | NS_ASSERTION(mFileHelper, "Must have a file helper!"); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | FileStreamWrapper::~FileStreamWrapper() |
michael@0 | 87 | { |
michael@0 | 88 | if (mFlags & NOTIFY_DESTROY) { |
michael@0 | 89 | if (NS_IsMainThread()) { |
michael@0 | 90 | mFileHelper->OnStreamDestroy(); |
michael@0 | 91 | } |
michael@0 | 92 | else { |
michael@0 | 93 | nsCOMPtr<nsIRunnable> runnable = |
michael@0 | 94 | new DestroyRunnable(mFileHelper); |
michael@0 | 95 | |
michael@0 | 96 | nsresult rv = NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL); |
michael@0 | 97 | if (NS_FAILED(rv)) { |
michael@0 | 98 | NS_WARNING("Failed to dispatch to the main thread!"); |
michael@0 | 99 | } |
michael@0 | 100 | } |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | NS_IMPL_ISUPPORTS0(FileStreamWrapper) |
michael@0 | 105 | |
michael@0 | 106 | FileInputStreamWrapper::FileInputStreamWrapper(nsISupports* aFileStream, |
michael@0 | 107 | FileHelper* aFileHelper, |
michael@0 | 108 | uint64_t aOffset, |
michael@0 | 109 | uint64_t aLimit, |
michael@0 | 110 | uint32_t aFlags) |
michael@0 | 111 | : FileStreamWrapper(aFileStream, aFileHelper, aOffset, aLimit, aFlags) |
michael@0 | 112 | { |
michael@0 | 113 | mInputStream = do_QueryInterface(mFileStream); |
michael@0 | 114 | NS_ASSERTION(mInputStream, "This should always succeed!"); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | NS_IMPL_ISUPPORTS_INHERITED(FileInputStreamWrapper, |
michael@0 | 118 | FileStreamWrapper, |
michael@0 | 119 | nsIInputStream) |
michael@0 | 120 | |
michael@0 | 121 | NS_IMETHODIMP |
michael@0 | 122 | FileInputStreamWrapper::Close() |
michael@0 | 123 | { |
michael@0 | 124 | NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 125 | |
michael@0 | 126 | if (mFlags & NOTIFY_CLOSE) { |
michael@0 | 127 | nsCOMPtr<nsIRunnable> runnable = new CloseRunnable(mFileHelper); |
michael@0 | 128 | |
michael@0 | 129 | if (NS_FAILED(NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL))) { |
michael@0 | 130 | NS_WARNING("Failed to dispatch to the main thread!"); |
michael@0 | 131 | } |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | mOffset = 0; |
michael@0 | 135 | mLimit = 0; |
michael@0 | 136 | |
michael@0 | 137 | return NS_OK; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | NS_IMETHODIMP |
michael@0 | 141 | FileInputStreamWrapper::Available(uint64_t* _retval) |
michael@0 | 142 | { |
michael@0 | 143 | // Performing sync IO on the main thread is generally not allowed. |
michael@0 | 144 | // However, the input stream wrapper is also used to track reads performed by |
michael@0 | 145 | // other APIs like FileReader, XHR, etc. |
michael@0 | 146 | // In that case nsInputStreamChannel::OpenContentStream() calls Available() |
michael@0 | 147 | // before setting the content length property. This property is not important |
michael@0 | 148 | // to perform reads properly, so we can just return NS_BASE_STREAM_CLOSED |
michael@0 | 149 | // here. It causes OpenContentStream() to set the content length property to |
michael@0 | 150 | // zero. |
michael@0 | 151 | |
michael@0 | 152 | if (NS_IsMainThread()) { |
michael@0 | 153 | return NS_BASE_STREAM_CLOSED; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | return mInputStream->Available(_retval); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | NS_IMETHODIMP |
michael@0 | 160 | FileInputStreamWrapper::Read(char* aBuf, uint32_t aCount, uint32_t* _retval) |
michael@0 | 161 | { |
michael@0 | 162 | NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 163 | |
michael@0 | 164 | nsresult rv; |
michael@0 | 165 | |
michael@0 | 166 | if (mFirstTime) { |
michael@0 | 167 | mFirstTime = false; |
michael@0 | 168 | |
michael@0 | 169 | if (mOffset != UINT64_MAX) { |
michael@0 | 170 | nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mInputStream); |
michael@0 | 171 | if (seekable) { |
michael@0 | 172 | rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, mOffset); |
michael@0 | 173 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 174 | } |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | mOffset = 0; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | uint64_t max = mLimit - mOffset; |
michael@0 | 181 | if (max == 0) { |
michael@0 | 182 | *_retval = 0; |
michael@0 | 183 | return NS_OK; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | if (aCount > max) { |
michael@0 | 187 | aCount = max; |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | rv = mInputStream->Read(aBuf, aCount, _retval); |
michael@0 | 191 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 192 | |
michael@0 | 193 | mOffset += *_retval; |
michael@0 | 194 | |
michael@0 | 195 | if (mFlags & NOTIFY_PROGRESS) { |
michael@0 | 196 | nsCOMPtr<nsIRunnable> runnable = |
michael@0 | 197 | new ProgressRunnable(mFileHelper, mOffset, mLimit); |
michael@0 | 198 | |
michael@0 | 199 | rv = NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL); |
michael@0 | 200 | if (NS_FAILED(rv)) { |
michael@0 | 201 | NS_WARNING("Failed to dispatch to the main thread!"); |
michael@0 | 202 | } |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | return NS_OK; |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | NS_IMETHODIMP |
michael@0 | 209 | FileInputStreamWrapper::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure, |
michael@0 | 210 | uint32_t aCount, uint32_t* _retval) |
michael@0 | 211 | { |
michael@0 | 212 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | NS_IMETHODIMP |
michael@0 | 216 | FileInputStreamWrapper::IsNonBlocking(bool* _retval) |
michael@0 | 217 | { |
michael@0 | 218 | *_retval = false; |
michael@0 | 219 | return NS_OK; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | FileOutputStreamWrapper::FileOutputStreamWrapper(nsISupports* aFileStream, |
michael@0 | 223 | FileHelper* aFileHelper, |
michael@0 | 224 | uint64_t aOffset, |
michael@0 | 225 | uint64_t aLimit, |
michael@0 | 226 | uint32_t aFlags) |
michael@0 | 227 | : FileStreamWrapper(aFileStream, aFileHelper, aOffset, aLimit, aFlags) |
michael@0 | 228 | #ifdef DEBUG |
michael@0 | 229 | , mWriteThread(nullptr) |
michael@0 | 230 | #endif |
michael@0 | 231 | { |
michael@0 | 232 | mOutputStream = do_QueryInterface(mFileStream); |
michael@0 | 233 | NS_ASSERTION(mOutputStream, "This should always succeed!"); |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | NS_IMPL_ISUPPORTS_INHERITED(FileOutputStreamWrapper, |
michael@0 | 237 | FileStreamWrapper, |
michael@0 | 238 | nsIOutputStream) |
michael@0 | 239 | |
michael@0 | 240 | NS_IMETHODIMP |
michael@0 | 241 | FileOutputStreamWrapper::Close() |
michael@0 | 242 | { |
michael@0 | 243 | NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 244 | |
michael@0 | 245 | nsresult rv = NS_OK; |
michael@0 | 246 | |
michael@0 | 247 | if (!mFirstTime) { |
michael@0 | 248 | NS_ASSERTION(PR_GetCurrentThread() == mWriteThread, |
michael@0 | 249 | "Unsetting thread locals on wrong thread!"); |
michael@0 | 250 | mFileHelper->mFileStorage->UnsetThreadLocals(); |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | if (mFlags & NOTIFY_CLOSE) { |
michael@0 | 254 | nsCOMPtr<nsIRunnable> runnable = new CloseRunnable(mFileHelper); |
michael@0 | 255 | |
michael@0 | 256 | if (NS_FAILED(NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL))) { |
michael@0 | 257 | NS_WARNING("Failed to dispatch to the main thread!"); |
michael@0 | 258 | } |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | mOffset = 0; |
michael@0 | 262 | mLimit = 0; |
michael@0 | 263 | |
michael@0 | 264 | return rv; |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | NS_IMETHODIMP |
michael@0 | 268 | FileOutputStreamWrapper::Write(const char* aBuf, uint32_t aCount, |
michael@0 | 269 | uint32_t* _retval) |
michael@0 | 270 | { |
michael@0 | 271 | NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 272 | |
michael@0 | 273 | nsresult rv; |
michael@0 | 274 | |
michael@0 | 275 | if (mFirstTime) { |
michael@0 | 276 | mFirstTime = false; |
michael@0 | 277 | |
michael@0 | 278 | #ifdef DEBUG |
michael@0 | 279 | mWriteThread = PR_GetCurrentThread(); |
michael@0 | 280 | #endif |
michael@0 | 281 | mFileHelper->mFileStorage->SetThreadLocals(); |
michael@0 | 282 | |
michael@0 | 283 | nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(mOutputStream); |
michael@0 | 284 | if (seekable) { |
michael@0 | 285 | if (mOffset == UINT64_MAX) { |
michael@0 | 286 | rv = seekable->Seek(nsISeekableStream::NS_SEEK_END, 0); |
michael@0 | 287 | } |
michael@0 | 288 | else { |
michael@0 | 289 | rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, mOffset); |
michael@0 | 290 | } |
michael@0 | 291 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | mOffset = 0; |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | uint64_t max = mLimit - mOffset; |
michael@0 | 298 | if (max == 0) { |
michael@0 | 299 | *_retval = 0; |
michael@0 | 300 | return NS_OK; |
michael@0 | 301 | } |
michael@0 | 302 | |
michael@0 | 303 | if (aCount > max) { |
michael@0 | 304 | aCount = max; |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | rv = mOutputStream->Write(aBuf, aCount, _retval); |
michael@0 | 308 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 309 | |
michael@0 | 310 | mOffset += *_retval; |
michael@0 | 311 | |
michael@0 | 312 | if (mFlags & NOTIFY_PROGRESS) { |
michael@0 | 313 | nsCOMPtr<nsIRunnable> runnable = |
michael@0 | 314 | new ProgressRunnable(mFileHelper, mOffset, mLimit); |
michael@0 | 315 | |
michael@0 | 316 | NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL); |
michael@0 | 317 | } |
michael@0 | 318 | |
michael@0 | 319 | return NS_OK; |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | NS_IMETHODIMP |
michael@0 | 323 | FileOutputStreamWrapper::Flush() |
michael@0 | 324 | { |
michael@0 | 325 | return NS_OK; |
michael@0 | 326 | } |
michael@0 | 327 | |
michael@0 | 328 | NS_IMETHODIMP |
michael@0 | 329 | FileOutputStreamWrapper::WriteFrom(nsIInputStream* aFromStream, |
michael@0 | 330 | uint32_t aCount, uint32_t* _retval) |
michael@0 | 331 | { |
michael@0 | 332 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | NS_IMETHODIMP |
michael@0 | 336 | FileOutputStreamWrapper::WriteSegments(nsReadSegmentFun aReader, |
michael@0 | 337 | void* aClosure, uint32_t aCount, |
michael@0 | 338 | uint32_t* _retval) |
michael@0 | 339 | { |
michael@0 | 340 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 341 | } |
michael@0 | 342 | |
michael@0 | 343 | NS_IMETHODIMP |
michael@0 | 344 | FileOutputStreamWrapper::IsNonBlocking(bool* _retval) |
michael@0 | 345 | { |
michael@0 | 346 | *_retval = false; |
michael@0 | 347 | return NS_OK; |
michael@0 | 348 | } |
michael@0 | 349 | |
michael@0 | 350 | NS_IMPL_ISUPPORTS(ProgressRunnable, nsIRunnable) |
michael@0 | 351 | |
michael@0 | 352 | NS_IMETHODIMP |
michael@0 | 353 | ProgressRunnable::Run() |
michael@0 | 354 | { |
michael@0 | 355 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 356 | |
michael@0 | 357 | mFileHelper->OnStreamProgress(mProgress, mProgressMax); |
michael@0 | 358 | mFileHelper = nullptr; |
michael@0 | 359 | |
michael@0 | 360 | return NS_OK; |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | NS_IMPL_ISUPPORTS(CloseRunnable, nsIRunnable) |
michael@0 | 364 | |
michael@0 | 365 | NS_IMETHODIMP |
michael@0 | 366 | CloseRunnable::Run() |
michael@0 | 367 | { |
michael@0 | 368 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 369 | |
michael@0 | 370 | mFileHelper->OnStreamClose(); |
michael@0 | 371 | mFileHelper = nullptr; |
michael@0 | 372 | |
michael@0 | 373 | return NS_OK; |
michael@0 | 374 | } |
michael@0 | 375 | |
michael@0 | 376 | NS_IMPL_ISUPPORTS(DestroyRunnable, nsIRunnable) |
michael@0 | 377 | |
michael@0 | 378 | NS_IMETHODIMP |
michael@0 | 379 | DestroyRunnable::Run() |
michael@0 | 380 | { |
michael@0 | 381 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 382 | |
michael@0 | 383 | mFileHelper->OnStreamDestroy(); |
michael@0 | 384 | mFileHelper = nullptr; |
michael@0 | 385 | |
michael@0 | 386 | return NS_OK; |
michael@0 | 387 | } |