2012-12-11 00:06:37 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file defines two implementations of the nsIBackgroundFileSaver
|
|
|
|
* interface. See the "test_backgroundfilesaver.js" file for usage examples.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BackgroundFileSaver_h__
|
|
|
|
#define BackgroundFileSaver_h__
|
|
|
|
|
2016-09-08 15:46:26 +03:00
|
|
|
#include "ScopedNSSTypes.h"
|
2012-12-11 00:06:37 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2014-01-28 00:38:35 +04:00
|
|
|
#include "nsCOMArray.h"
|
2012-12-11 00:06:37 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIAsyncOutputStream.h"
|
|
|
|
#include "nsIBackgroundFileSaver.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsStreamUtils.h"
|
2016-09-08 15:46:26 +03:00
|
|
|
#include "nsString.h"
|
2012-12-11 00:06:37 +04:00
|
|
|
|
|
|
|
class nsIAsyncInputStream;
|
|
|
|
class nsIThread;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2013-02-20 00:47:59 +04:00
|
|
|
class DigestOutputStream;
|
|
|
|
|
2012-12-11 00:06:37 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// BackgroundFileSaver
|
|
|
|
|
2018-01-25 01:44:01 +03:00
|
|
|
class BackgroundFileSaver : public nsIBackgroundFileSaver {
|
2012-12-11 00:06:37 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_NSIBACKGROUNDFILESAVER
|
|
|
|
|
|
|
|
BackgroundFileSaver();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the pipe and the worker thread on XPCOM construction.
|
|
|
|
*
|
|
|
|
* This is called automatically by the XPCOM infrastructure, and if this
|
|
|
|
* fails, the factory will delete this object without returning a reference.
|
|
|
|
*/
|
|
|
|
nsresult Init();
|
|
|
|
|
2013-03-05 07:40:11 +04:00
|
|
|
/**
|
|
|
|
* Number of worker threads that are currently running.
|
|
|
|
*/
|
|
|
|
static uint32_t sThreadCount;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum number of worker threads reached during the current download
|
|
|
|
* session, used for telemetry.
|
|
|
|
*
|
|
|
|
* When there are no more worker threads running, we consider the download
|
|
|
|
* session finished, and this counter is reset.
|
|
|
|
*/
|
|
|
|
static uint32_t sTelemetryMaxThreadCount;
|
|
|
|
|
2012-12-11 00:06:37 +04:00
|
|
|
protected:
|
|
|
|
virtual ~BackgroundFileSaver();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Thread that constructed this object.
|
|
|
|
*/
|
2017-06-01 23:43:44 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> mControlEventTarget;
|
2012-12-11 00:06:37 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Thread to which the actual input/output is delegated.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIThread> mWorkerThread;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stream that receives data from derived classes. The received data will be
|
2013-02-20 00:47:59 +04:00
|
|
|
* available to the worker thread through mPipeInputStream. This is an
|
|
|
|
* instance of nsPipeOutputStream, not BackgroundFileSaverOutputStream.
|
2012-12-11 00:06:37 +04:00
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIAsyncOutputStream> mPipeOutputStream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used during initialization, determines if the pipe is created with an
|
|
|
|
* infinite buffer. An infinite buffer is required if the derived class
|
|
|
|
* implements nsIStreamListener, because this interface requires all the
|
|
|
|
* provided data to be consumed synchronously.
|
|
|
|
*/
|
|
|
|
virtual bool HasInfiniteBuffer() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by derived classes if they need to be called back while copying.
|
|
|
|
*/
|
|
|
|
virtual nsAsyncCopyProgressFun GetProgressCallback() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stream used by the worker thread to read the data to be saved.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> mPipeInputStream;
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class NotifyTargetChangeRunnable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Matches the nsIBackgroundFileSaver::observer property.
|
|
|
|
*
|
|
|
|
* @remarks This is a strong reference so that JavaScript callers don't need
|
|
|
|
* to worry about keeping another reference to the observer.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIBackgroundFileSaverObserver> mObserver;
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Shared state between control and worker threads
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Protects the shared state between control and worker threads. This mutex
|
|
|
|
* is always locked for a very short time, never during input/output.
|
|
|
|
*/
|
|
|
|
mozilla::Mutex mLock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the worker thread is already waiting to process a change in state.
|
|
|
|
*/
|
|
|
|
bool mWorkerThreadAttentionRequested;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the operation should finish as soon as possibile.
|
|
|
|
*/
|
|
|
|
bool mFinishRequested;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the operation completed, with either success or failure.
|
|
|
|
*/
|
|
|
|
bool mComplete;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the current file saver status. This is a success status while the
|
|
|
|
* object is working correctly, and remains such if the operation completes
|
|
|
|
* successfully. This becomes an error status when an error occurs on the
|
|
|
|
* worker thread, or when the operation is canceled.
|
|
|
|
*/
|
|
|
|
nsresult mStatus;
|
|
|
|
|
2013-07-09 15:59:59 +04:00
|
|
|
/**
|
|
|
|
* True if we should append data to the initial target file, instead of
|
|
|
|
* overwriting it.
|
|
|
|
*/
|
|
|
|
bool mAppend;
|
|
|
|
|
2012-12-11 00:06:37 +04:00
|
|
|
/**
|
2013-10-09 12:13:49 +04:00
|
|
|
* This is set by the first SetTarget call on the control thread, and contains
|
|
|
|
* the target file name that will be used by the worker thread, as soon as it
|
|
|
|
* is possible to update mActualTarget and open the file. This is null if no
|
|
|
|
* target was ever assigned to this object.
|
2012-12-11 00:06:37 +04:00
|
|
|
*/
|
2013-10-09 12:13:49 +04:00
|
|
|
nsCOMPtr<nsIFile> mInitialTarget;
|
2012-12-11 00:06:37 +04:00
|
|
|
|
|
|
|
/**
|
2013-10-09 12:13:49 +04:00
|
|
|
* This is set by the first SetTarget call on the control thread, and
|
|
|
|
* indicates whether mInitialTarget should be kept as partially completed,
|
2012-12-11 00:06:37 +04:00
|
|
|
* rather than deleted, if the operation fails or is canceled.
|
|
|
|
*/
|
2013-10-09 12:13:49 +04:00
|
|
|
bool mInitialTargetKeepPartial;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is set by subsequent SetTarget calls on the control thread, and
|
|
|
|
* contains the new target file name to which the worker thread will move the
|
|
|
|
* target file, as soon as it can be done. This is null if SetTarget was
|
|
|
|
* called only once, or no target was ever assigned to this object.
|
|
|
|
*
|
|
|
|
* The target file can be renamed multiple times, though only the most recent
|
|
|
|
* rename is guaranteed to be processed by the worker thread.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIFile> mRenamedTarget;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is set by subsequent SetTarget calls on the control thread, and
|
|
|
|
* indicates whether mRenamedTarget should be kept as partially completed,
|
|
|
|
* rather than deleted, if the operation fails or is canceled.
|
|
|
|
*/
|
|
|
|
bool mRenamedTargetKeepPartial;
|
2012-12-11 00:06:37 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* While NS_AsyncCopy is in progress, allows canceling it. Null otherwise.
|
|
|
|
* This is read by both threads but only written by the worker thread.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsISupports> mAsyncCopyContext;
|
|
|
|
|
2013-02-20 00:47:59 +04:00
|
|
|
/**
|
|
|
|
* The SHA 256 hash in raw bytes of the downloaded file. This is written
|
|
|
|
* by the worker thread but can be read on the main thread.
|
|
|
|
*/
|
2015-11-19 12:30:23 +03:00
|
|
|
nsCString mSha256;
|
2013-02-20 00:47:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not to compute the hash. Must be set on the main thread before
|
|
|
|
* setTarget is called.
|
|
|
|
*/
|
|
|
|
bool mSha256Enabled;
|
|
|
|
|
2014-01-28 00:38:35 +04:00
|
|
|
/**
|
|
|
|
* Store the signature info.
|
|
|
|
*/
|
2019-11-19 22:51:44 +03:00
|
|
|
nsTArray<nsTArray<nsTArray<uint8_t>>> mSignatureInfo;
|
2014-01-28 00:38:35 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not to extract the signature. Must be set on the main thread
|
|
|
|
* before setTarget is called.
|
|
|
|
*/
|
|
|
|
bool mSignatureInfoEnabled;
|
|
|
|
|
2012-12-11 00:06:37 +04:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// State handled exclusively by the worker thread
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current target file associated to the input and output streams.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIFile> mActualTarget;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether mActualTarget should be kept as partially completed,
|
|
|
|
* rather than deleted, if the operation fails or is canceled.
|
|
|
|
*/
|
|
|
|
bool mActualTargetKeepPartial;
|
|
|
|
|
2013-02-20 00:47:59 +04:00
|
|
|
/**
|
|
|
|
* Used to calculate the file hash. This keeps state across file renames and
|
|
|
|
* is lazily initialized in ProcessStateChange.
|
|
|
|
*/
|
2016-05-13 15:53:57 +03:00
|
|
|
UniquePK11Context mDigestContext;
|
2013-02-20 00:47:59 +04:00
|
|
|
|
2012-12-11 00:06:37 +04:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Private methods
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when NS_AsyncCopy completes.
|
|
|
|
*
|
|
|
|
* @param aClosure
|
|
|
|
* Populated with a raw pointer to the BackgroundFileSaver object.
|
|
|
|
* @param aStatus
|
|
|
|
* Success or failure status specified when the copy was interrupted.
|
|
|
|
*/
|
|
|
|
static void AsyncCopyCallback(void* aClosure, nsresult aStatus);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called on the control thread after state changes, to ensure that the worker
|
|
|
|
* thread will process the state change appropriately.
|
|
|
|
*
|
|
|
|
* @param aShouldInterruptCopy
|
|
|
|
* If true, the current NS_AsyncCopy, if any, is canceled.
|
|
|
|
*/
|
|
|
|
nsresult GetWorkerThreadAttention(bool aShouldInterruptCopy);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event called on the worker thread to begin processing a state change.
|
|
|
|
*/
|
|
|
|
nsresult ProcessAttention();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by ProcessAttention to execute the operations corresponding to the
|
|
|
|
* state change. If this results in an error, ProcessAttention will force the
|
|
|
|
* entire operation to be aborted.
|
|
|
|
*/
|
|
|
|
nsresult ProcessStateChange();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if completion conditions are met on the worker thread. The
|
|
|
|
* first time this happens, posts the completion event to the control thread.
|
|
|
|
*/
|
|
|
|
bool CheckCompletion();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event called on the control thread to indicate that file contents will now
|
|
|
|
* be saved to the specified file.
|
|
|
|
*/
|
|
|
|
nsresult NotifyTargetChange(nsIFile* aTarget);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event called on the control thread to send the final notification.
|
|
|
|
*/
|
|
|
|
nsresult NotifySaveComplete();
|
2014-01-28 00:38:35 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies the signature of the binary at the specified file path and stores
|
|
|
|
* the signature data in mSignatureInfo. We extract only X.509 certificates,
|
|
|
|
* since that is what Google's Safebrowsing protocol specifies.
|
|
|
|
*/
|
|
|
|
nsresult ExtractSignatureInfo(const nsAString& filePath);
|
2012-12-11 00:06:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// BackgroundFileSaverOutputStream
|
|
|
|
|
|
|
|
class BackgroundFileSaverOutputStream : public BackgroundFileSaver,
|
|
|
|
public nsIAsyncOutputStream,
|
|
|
|
public nsIOutputStreamCallback {
|
|
|
|
public:
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-12-11 00:06:37 +04:00
|
|
|
NS_DECL_NSIOUTPUTSTREAM
|
|
|
|
NS_DECL_NSIASYNCOUTPUTSTREAM
|
|
|
|
NS_DECL_NSIOUTPUTSTREAMCALLBACK
|
|
|
|
|
|
|
|
BackgroundFileSaverOutputStream();
|
|
|
|
|
|
|
|
protected:
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool HasInfiniteBuffer() override;
|
|
|
|
virtual nsAsyncCopyProgressFun GetProgressCallback() override;
|
2012-12-11 00:06:37 +04:00
|
|
|
|
|
|
|
private:
|
2018-04-30 19:46:04 +03:00
|
|
|
~BackgroundFileSaverOutputStream() = default;
|
2012-12-11 00:06:37 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Original callback provided to our AsyncWait wrapper.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIOutputStreamCallback> mAsyncWaitCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-02-20 00:47:59 +04:00
|
|
|
//// BackgroundFileSaverStreamListener. This class is instantiated by
|
|
|
|
// nsExternalHelperAppService, DownloadCore.jsm, and possibly others.
|
2012-12-11 00:06:37 +04:00
|
|
|
|
2015-03-21 21:35:18 +03:00
|
|
|
class BackgroundFileSaverStreamListener final : public BackgroundFileSaver,
|
|
|
|
public nsIStreamListener {
|
2012-12-11 00:06:37 +04:00
|
|
|
public:
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-12-11 00:06:37 +04:00
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
|
|
|
|
BackgroundFileSaverStreamListener();
|
|
|
|
|
|
|
|
protected:
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool HasInfiniteBuffer() override;
|
|
|
|
virtual nsAsyncCopyProgressFun GetProgressCallback() override;
|
2012-12-11 00:06:37 +04:00
|
|
|
|
|
|
|
private:
|
2018-04-30 19:46:04 +03:00
|
|
|
~BackgroundFileSaverStreamListener() = default;
|
2012-12-11 00:06:37 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Protects the state related to whether the request should be suspended.
|
|
|
|
*/
|
|
|
|
mozilla::Mutex mSuspensionLock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether we should suspend the request because we received too much data.
|
|
|
|
*/
|
|
|
|
bool mReceivedTooMuchData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request for which we received too much data. This is populated when
|
|
|
|
* mReceivedTooMuchData becomes true for the first time.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIRequest> mRequest;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether mRequest is currently suspended.
|
|
|
|
*/
|
|
|
|
bool mRequestSuspended;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called while NS_AsyncCopy is copying data.
|
|
|
|
*/
|
|
|
|
static void AsyncCopyProgressCallback(void* aClosure, uint32_t aCount);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called on the control thread to suspend or resume the request.
|
|
|
|
*/
|
|
|
|
nsresult NotifySuspendOrResume();
|
|
|
|
};
|
|
|
|
|
2013-02-20 00:47:59 +04:00
|
|
|
// A wrapper around nsIOutputStream, so that we can compute hashes on the
|
|
|
|
// stream without copying and without polluting pristine NSS code with XPCOM
|
|
|
|
// interfaces.
|
2018-01-25 01:44:01 +03:00
|
|
|
class DigestOutputStream : public nsIOutputStream {
|
2013-02-20 00:47:59 +04:00
|
|
|
public:
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-02-20 00:47:59 +04:00
|
|
|
NS_DECL_NSIOUTPUTSTREAM
|
|
|
|
// Constructor. Neither parameter may be null. The caller owns both.
|
|
|
|
DigestOutputStream(nsIOutputStream* outputStream, PK11Context* aContext);
|
|
|
|
|
|
|
|
private:
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~DigestOutputStream() = default;
|
2014-06-24 20:36:44 +04:00
|
|
|
|
2013-02-20 00:47:59 +04:00
|
|
|
// Calls to write are passed to this stream.
|
|
|
|
nsCOMPtr<nsIOutputStream> mOutputStream;
|
|
|
|
// Digest context used to compute the hash, owned by the caller.
|
|
|
|
PK11Context* mDigestContext;
|
|
|
|
|
|
|
|
// Don't accidentally copy construct.
|
2018-04-30 19:46:04 +03:00
|
|
|
DigestOutputStream(const DigestOutputStream& d) = delete;
|
2013-02-20 00:47:59 +04:00
|
|
|
};
|
2015-07-13 18:25:42 +03:00
|
|
|
|
2012-12-11 00:06:37 +04:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|