netwerk/base/src/BackgroundFileSaver.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /**
michael@0 8 * This file defines two implementations of the nsIBackgroundFileSaver
michael@0 9 * interface. See the "test_backgroundfilesaver.js" file for usage examples.
michael@0 10 */
michael@0 11
michael@0 12 #ifndef BackgroundFileSaver_h__
michael@0 13 #define BackgroundFileSaver_h__
michael@0 14
michael@0 15 #include "mozilla/Mutex.h"
michael@0 16 #include "nsCOMArray.h"
michael@0 17 #include "nsCOMPtr.h"
michael@0 18 #include "nsNSSShutDown.h"
michael@0 19 #include "nsIAsyncOutputStream.h"
michael@0 20 #include "nsIBackgroundFileSaver.h"
michael@0 21 #include "nsIStreamListener.h"
michael@0 22 #include "nsStreamUtils.h"
michael@0 23 #include "ScopedNSSTypes.h"
michael@0 24
michael@0 25 class nsIAsyncInputStream;
michael@0 26 class nsIThread;
michael@0 27 class nsIX509CertList;
michael@0 28 class PRLogModuleInfo;
michael@0 29
michael@0 30 namespace mozilla {
michael@0 31 namespace net {
michael@0 32
michael@0 33 class DigestOutputStream;
michael@0 34
michael@0 35 ////////////////////////////////////////////////////////////////////////////////
michael@0 36 //// BackgroundFileSaver
michael@0 37
michael@0 38 class BackgroundFileSaver : public nsIBackgroundFileSaver,
michael@0 39 public nsNSSShutDownObject
michael@0 40 {
michael@0 41 public:
michael@0 42 NS_DECL_NSIBACKGROUNDFILESAVER
michael@0 43
michael@0 44 BackgroundFileSaver();
michael@0 45
michael@0 46 /**
michael@0 47 * Initializes the pipe and the worker thread on XPCOM construction.
michael@0 48 *
michael@0 49 * This is called automatically by the XPCOM infrastructure, and if this
michael@0 50 * fails, the factory will delete this object without returning a reference.
michael@0 51 */
michael@0 52 nsresult Init();
michael@0 53
michael@0 54 /**
michael@0 55 * Used by nsNSSShutDownList to manage nsNSSShutDownObjects.
michael@0 56 */
michael@0 57 void virtualDestroyNSSReference();
michael@0 58
michael@0 59 /**
michael@0 60 * Number of worker threads that are currently running.
michael@0 61 */
michael@0 62 static uint32_t sThreadCount;
michael@0 63
michael@0 64 /**
michael@0 65 * Maximum number of worker threads reached during the current download session,
michael@0 66 * used for telemetry.
michael@0 67 *
michael@0 68 * When there are no more worker threads running, we consider the download
michael@0 69 * session finished, and this counter is reset.
michael@0 70 */
michael@0 71 static uint32_t sTelemetryMaxThreadCount;
michael@0 72
michael@0 73
michael@0 74 protected:
michael@0 75 virtual ~BackgroundFileSaver();
michael@0 76
michael@0 77 static PRLogModuleInfo *prlog;
michael@0 78
michael@0 79 /**
michael@0 80 * Helper function for managing NSS objects (mDigestContext).
michael@0 81 */
michael@0 82 void destructorSafeDestroyNSSReference();
michael@0 83
michael@0 84 /**
michael@0 85 * Thread that constructed this object.
michael@0 86 */
michael@0 87 nsCOMPtr<nsIThread> mControlThread;
michael@0 88
michael@0 89 /**
michael@0 90 * Thread to which the actual input/output is delegated.
michael@0 91 */
michael@0 92 nsCOMPtr<nsIThread> mWorkerThread;
michael@0 93
michael@0 94 /**
michael@0 95 * Stream that receives data from derived classes. The received data will be
michael@0 96 * available to the worker thread through mPipeInputStream. This is an
michael@0 97 * instance of nsPipeOutputStream, not BackgroundFileSaverOutputStream.
michael@0 98 */
michael@0 99 nsCOMPtr<nsIAsyncOutputStream> mPipeOutputStream;
michael@0 100
michael@0 101 /**
michael@0 102 * Used during initialization, determines if the pipe is created with an
michael@0 103 * infinite buffer. An infinite buffer is required if the derived class
michael@0 104 * implements nsIStreamListener, because this interface requires all the
michael@0 105 * provided data to be consumed synchronously.
michael@0 106 */
michael@0 107 virtual bool HasInfiniteBuffer() = 0;
michael@0 108
michael@0 109 /**
michael@0 110 * Used by derived classes if they need to be called back while copying.
michael@0 111 */
michael@0 112 virtual nsAsyncCopyProgressFun GetProgressCallback() = 0;
michael@0 113
michael@0 114 /**
michael@0 115 * Stream used by the worker thread to read the data to be saved.
michael@0 116 */
michael@0 117 nsCOMPtr<nsIAsyncInputStream> mPipeInputStream;
michael@0 118
michael@0 119 private:
michael@0 120 friend class NotifyTargetChangeRunnable;
michael@0 121
michael@0 122 /**
michael@0 123 * Matches the nsIBackgroundFileSaver::observer property.
michael@0 124 *
michael@0 125 * @remarks This is a strong reference so that JavaScript callers don't need
michael@0 126 * to worry about keeping another reference to the observer.
michael@0 127 */
michael@0 128 nsCOMPtr<nsIBackgroundFileSaverObserver> mObserver;
michael@0 129
michael@0 130 //////////////////////////////////////////////////////////////////////////////
michael@0 131 //// Shared state between control and worker threads
michael@0 132
michael@0 133 /**
michael@0 134 * Protects the shared state between control and worker threads. This mutex
michael@0 135 * is always locked for a very short time, never during input/output.
michael@0 136 */
michael@0 137 mozilla::Mutex mLock;
michael@0 138
michael@0 139 /**
michael@0 140 * True if the worker thread is already waiting to process a change in state.
michael@0 141 */
michael@0 142 bool mWorkerThreadAttentionRequested;
michael@0 143
michael@0 144 /**
michael@0 145 * True if the operation should finish as soon as possibile.
michael@0 146 */
michael@0 147 bool mFinishRequested;
michael@0 148
michael@0 149 /**
michael@0 150 * True if the operation completed, with either success or failure.
michael@0 151 */
michael@0 152 bool mComplete;
michael@0 153
michael@0 154 /**
michael@0 155 * Holds the current file saver status. This is a success status while the
michael@0 156 * object is working correctly, and remains such if the operation completes
michael@0 157 * successfully. This becomes an error status when an error occurs on the
michael@0 158 * worker thread, or when the operation is canceled.
michael@0 159 */
michael@0 160 nsresult mStatus;
michael@0 161
michael@0 162 /**
michael@0 163 * True if we should append data to the initial target file, instead of
michael@0 164 * overwriting it.
michael@0 165 */
michael@0 166 bool mAppend;
michael@0 167
michael@0 168 /**
michael@0 169 * This is set by the first SetTarget call on the control thread, and contains
michael@0 170 * the target file name that will be used by the worker thread, as soon as it
michael@0 171 * is possible to update mActualTarget and open the file. This is null if no
michael@0 172 * target was ever assigned to this object.
michael@0 173 */
michael@0 174 nsCOMPtr<nsIFile> mInitialTarget;
michael@0 175
michael@0 176 /**
michael@0 177 * This is set by the first SetTarget call on the control thread, and
michael@0 178 * indicates whether mInitialTarget should be kept as partially completed,
michael@0 179 * rather than deleted, if the operation fails or is canceled.
michael@0 180 */
michael@0 181 bool mInitialTargetKeepPartial;
michael@0 182
michael@0 183 /**
michael@0 184 * This is set by subsequent SetTarget calls on the control thread, and
michael@0 185 * contains the new target file name to which the worker thread will move the
michael@0 186 * target file, as soon as it can be done. This is null if SetTarget was
michael@0 187 * called only once, or no target was ever assigned to this object.
michael@0 188 *
michael@0 189 * The target file can be renamed multiple times, though only the most recent
michael@0 190 * rename is guaranteed to be processed by the worker thread.
michael@0 191 */
michael@0 192 nsCOMPtr<nsIFile> mRenamedTarget;
michael@0 193
michael@0 194 /**
michael@0 195 * This is set by subsequent SetTarget calls on the control thread, and
michael@0 196 * indicates whether mRenamedTarget should be kept as partially completed,
michael@0 197 * rather than deleted, if the operation fails or is canceled.
michael@0 198 */
michael@0 199 bool mRenamedTargetKeepPartial;
michael@0 200
michael@0 201 /**
michael@0 202 * While NS_AsyncCopy is in progress, allows canceling it. Null otherwise.
michael@0 203 * This is read by both threads but only written by the worker thread.
michael@0 204 */
michael@0 205 nsCOMPtr<nsISupports> mAsyncCopyContext;
michael@0 206
michael@0 207 /**
michael@0 208 * The SHA 256 hash in raw bytes of the downloaded file. This is written
michael@0 209 * by the worker thread but can be read on the main thread.
michael@0 210 */
michael@0 211 nsAutoCString mSha256;
michael@0 212
michael@0 213 /**
michael@0 214 * Whether or not to compute the hash. Must be set on the main thread before
michael@0 215 * setTarget is called.
michael@0 216 */
michael@0 217 bool mSha256Enabled;
michael@0 218
michael@0 219 /**
michael@0 220 * Store the signature info.
michael@0 221 */
michael@0 222 nsCOMArray<nsIX509CertList> mSignatureInfo;
michael@0 223
michael@0 224 /**
michael@0 225 * Whether or not to extract the signature. Must be set on the main thread
michael@0 226 * before setTarget is called.
michael@0 227 */
michael@0 228 bool mSignatureInfoEnabled;
michael@0 229
michael@0 230 //////////////////////////////////////////////////////////////////////////////
michael@0 231 //// State handled exclusively by the worker thread
michael@0 232
michael@0 233 /**
michael@0 234 * Current target file associated to the input and output streams.
michael@0 235 */
michael@0 236 nsCOMPtr<nsIFile> mActualTarget;
michael@0 237
michael@0 238 /**
michael@0 239 * Indicates whether mActualTarget should be kept as partially completed,
michael@0 240 * rather than deleted, if the operation fails or is canceled.
michael@0 241 */
michael@0 242 bool mActualTargetKeepPartial;
michael@0 243
michael@0 244 /**
michael@0 245 * Used to calculate the file hash. This keeps state across file renames and
michael@0 246 * is lazily initialized in ProcessStateChange.
michael@0 247 */
michael@0 248 ScopedPK11Context mDigestContext;
michael@0 249
michael@0 250 //////////////////////////////////////////////////////////////////////////////
michael@0 251 //// Private methods
michael@0 252
michael@0 253 /**
michael@0 254 * Called when NS_AsyncCopy completes.
michael@0 255 *
michael@0 256 * @param aClosure
michael@0 257 * Populated with a raw pointer to the BackgroundFileSaver object.
michael@0 258 * @param aStatus
michael@0 259 * Success or failure status specified when the copy was interrupted.
michael@0 260 */
michael@0 261 static void AsyncCopyCallback(void *aClosure, nsresult aStatus);
michael@0 262
michael@0 263 /**
michael@0 264 * Called on the control thread after state changes, to ensure that the worker
michael@0 265 * thread will process the state change appropriately.
michael@0 266 *
michael@0 267 * @param aShouldInterruptCopy
michael@0 268 * If true, the current NS_AsyncCopy, if any, is canceled.
michael@0 269 */
michael@0 270 nsresult GetWorkerThreadAttention(bool aShouldInterruptCopy);
michael@0 271
michael@0 272 /**
michael@0 273 * Event called on the worker thread to begin processing a state change.
michael@0 274 */
michael@0 275 nsresult ProcessAttention();
michael@0 276
michael@0 277 /**
michael@0 278 * Called by ProcessAttention to execute the operations corresponding to the
michael@0 279 * state change. If this results in an error, ProcessAttention will force the
michael@0 280 * entire operation to be aborted.
michael@0 281 */
michael@0 282 nsresult ProcessStateChange();
michael@0 283
michael@0 284 /**
michael@0 285 * Returns true if completion conditions are met on the worker thread. The
michael@0 286 * first time this happens, posts the completion event to the control thread.
michael@0 287 */
michael@0 288 bool CheckCompletion();
michael@0 289
michael@0 290 /**
michael@0 291 * Event called on the control thread to indicate that file contents will now
michael@0 292 * be saved to the specified file.
michael@0 293 */
michael@0 294 nsresult NotifyTargetChange(nsIFile *aTarget);
michael@0 295
michael@0 296 /**
michael@0 297 * Event called on the control thread to send the final notification.
michael@0 298 */
michael@0 299 nsresult NotifySaveComplete();
michael@0 300
michael@0 301 /**
michael@0 302 * Verifies the signature of the binary at the specified file path and stores
michael@0 303 * the signature data in mSignatureInfo. We extract only X.509 certificates,
michael@0 304 * since that is what Google's Safebrowsing protocol specifies.
michael@0 305 */
michael@0 306 nsresult ExtractSignatureInfo(const nsAString& filePath);
michael@0 307 };
michael@0 308
michael@0 309 ////////////////////////////////////////////////////////////////////////////////
michael@0 310 //// BackgroundFileSaverOutputStream
michael@0 311
michael@0 312 class BackgroundFileSaverOutputStream : public BackgroundFileSaver
michael@0 313 , public nsIAsyncOutputStream
michael@0 314 , public nsIOutputStreamCallback
michael@0 315 {
michael@0 316 public:
michael@0 317 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 318 NS_DECL_NSIOUTPUTSTREAM
michael@0 319 NS_DECL_NSIASYNCOUTPUTSTREAM
michael@0 320 NS_DECL_NSIOUTPUTSTREAMCALLBACK
michael@0 321
michael@0 322 BackgroundFileSaverOutputStream();
michael@0 323
michael@0 324 protected:
michael@0 325 virtual bool HasInfiniteBuffer() MOZ_OVERRIDE;
michael@0 326 virtual nsAsyncCopyProgressFun GetProgressCallback() MOZ_OVERRIDE;
michael@0 327
michael@0 328 private:
michael@0 329 ~BackgroundFileSaverOutputStream();
michael@0 330
michael@0 331 /**
michael@0 332 * Original callback provided to our AsyncWait wrapper.
michael@0 333 */
michael@0 334 nsCOMPtr<nsIOutputStreamCallback> mAsyncWaitCallback;
michael@0 335 };
michael@0 336
michael@0 337 ////////////////////////////////////////////////////////////////////////////////
michael@0 338 //// BackgroundFileSaverStreamListener. This class is instantiated by
michael@0 339 // nsExternalHelperAppService, DownloadCore.jsm, and possibly others.
michael@0 340
michael@0 341 class BackgroundFileSaverStreamListener : public BackgroundFileSaver
michael@0 342 , public nsIStreamListener
michael@0 343 {
michael@0 344 public:
michael@0 345 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 346 NS_DECL_NSIREQUESTOBSERVER
michael@0 347 NS_DECL_NSISTREAMLISTENER
michael@0 348
michael@0 349 BackgroundFileSaverStreamListener();
michael@0 350
michael@0 351 protected:
michael@0 352 virtual bool HasInfiniteBuffer() MOZ_OVERRIDE;
michael@0 353 virtual nsAsyncCopyProgressFun GetProgressCallback() MOZ_OVERRIDE;
michael@0 354
michael@0 355 private:
michael@0 356 ~BackgroundFileSaverStreamListener();
michael@0 357
michael@0 358 /**
michael@0 359 * Protects the state related to whether the request should be suspended.
michael@0 360 */
michael@0 361 mozilla::Mutex mSuspensionLock;
michael@0 362
michael@0 363 /**
michael@0 364 * Whether we should suspend the request because we received too much data.
michael@0 365 */
michael@0 366 bool mReceivedTooMuchData;
michael@0 367
michael@0 368 /**
michael@0 369 * Request for which we received too much data. This is populated when
michael@0 370 * mReceivedTooMuchData becomes true for the first time.
michael@0 371 */
michael@0 372 nsCOMPtr<nsIRequest> mRequest;
michael@0 373
michael@0 374 /**
michael@0 375 * Whether mRequest is currently suspended.
michael@0 376 */
michael@0 377 bool mRequestSuspended;
michael@0 378
michael@0 379 /**
michael@0 380 * Called while NS_AsyncCopy is copying data.
michael@0 381 */
michael@0 382 static void AsyncCopyProgressCallback(void *aClosure, uint32_t aCount);
michael@0 383
michael@0 384 /**
michael@0 385 * Called on the control thread to suspend or resume the request.
michael@0 386 */
michael@0 387 nsresult NotifySuspendOrResume();
michael@0 388 };
michael@0 389
michael@0 390 // A wrapper around nsIOutputStream, so that we can compute hashes on the
michael@0 391 // stream without copying and without polluting pristine NSS code with XPCOM
michael@0 392 // interfaces.
michael@0 393 class DigestOutputStream : public nsNSSShutDownObject,
michael@0 394 public nsIOutputStream
michael@0 395 {
michael@0 396 public:
michael@0 397 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 398 NS_DECL_NSIOUTPUTSTREAM
michael@0 399 // Constructor. Neither parameter may be null. The caller owns both.
michael@0 400 DigestOutputStream(nsIOutputStream* outputStream, PK11Context* aContext);
michael@0 401 ~DigestOutputStream();
michael@0 402
michael@0 403 // We don't own any NSS objects here, so no need to clean up
michael@0 404 void virtualDestroyNSSReference() { }
michael@0 405
michael@0 406 private:
michael@0 407 // Calls to write are passed to this stream.
michael@0 408 nsCOMPtr<nsIOutputStream> mOutputStream;
michael@0 409 // Digest context used to compute the hash, owned by the caller.
michael@0 410 PK11Context* mDigestContext;
michael@0 411
michael@0 412 // Don't accidentally copy construct.
michael@0 413 DigestOutputStream(const DigestOutputStream& d);
michael@0 414 };
michael@0 415 } // namespace net
michael@0 416 } // namespace mozilla
michael@0 417
michael@0 418 #endif

mercurial