2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-10-06 22:01:20 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_FetchDriver_h
|
|
|
|
#define mozilla_dom_FetchDriver_h
|
|
|
|
|
2015-01-08 00:47:18 +03:00
|
|
|
#include "nsIChannelEventSink.h"
|
2017-12-04 09:39:15 +03:00
|
|
|
#include "nsICacheInfoChannel.h"
|
2015-01-08 00:47:18 +03:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2014-10-06 22:01:20 +04:00
|
|
|
#include "nsIStreamListener.h"
|
2015-08-28 23:49:07 +03:00
|
|
|
#include "nsIThreadRetargetableStreamListener.h"
|
2016-09-07 05:20:23 +03:00
|
|
|
#include "mozilla/ConsoleReportCollector.h"
|
2017-08-29 12:31:06 +03:00
|
|
|
#include "mozilla/dom/AbortSignal.h"
|
2016-09-26 15:03:25 +03:00
|
|
|
#include "mozilla/dom/SRIMetadata.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2014-12-23 18:56:19 +03:00
|
|
|
#include "mozilla/net/ReferrerPolicy.h"
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
class nsIConsoleReportCollector;
|
2014-10-15 03:36:58 +04:00
|
|
|
class nsIDocument;
|
2017-05-16 13:27:01 +03:00
|
|
|
class nsIEventTarget;
|
2014-12-10 11:51:59 +03:00
|
|
|
class nsIOutputStream;
|
2015-01-08 19:55:05 +03:00
|
|
|
class nsILoadGroup;
|
2014-12-10 11:51:59 +03:00
|
|
|
class nsIPrincipal;
|
2014-10-06 22:01:20 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class InternalRequest;
|
|
|
|
class InternalResponse;
|
2018-01-24 19:17:31 +03:00
|
|
|
class PerformanceStorage;
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2016-06-14 06:08:00 +03:00
|
|
|
/**
|
|
|
|
* Provides callbacks to be called when response is available or on error.
|
|
|
|
* Implemenations usually resolve or reject the promise returned from fetch().
|
|
|
|
* The callbacks can be called synchronously or asynchronously from FetchDriver::Fetch.
|
|
|
|
*/
|
2014-10-06 22:01:20 +04:00
|
|
|
class FetchDriverObserver
|
|
|
|
{
|
|
|
|
public:
|
2016-09-07 05:20:23 +03:00
|
|
|
FetchDriverObserver() : mReporter(new ConsoleReportCollector())
|
|
|
|
, mGotResponseAvailable(false)
|
2015-09-14 18:05:35 +03:00
|
|
|
{ }
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FetchDriverObserver);
|
2015-09-14 18:05:35 +03:00
|
|
|
void OnResponseAvailable(InternalResponse* aResponse)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mGotResponseAvailable);
|
|
|
|
mGotResponseAvailable = true;
|
|
|
|
OnResponseAvailableInternal(aResponse);
|
|
|
|
}
|
2017-03-22 13:06:08 +03:00
|
|
|
|
|
|
|
enum EndReason
|
|
|
|
{
|
|
|
|
eAborted,
|
|
|
|
eByNetworking,
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void OnResponseEnd(EndReason aReason)
|
2015-01-15 07:59:00 +03:00
|
|
|
{ };
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
nsIConsoleReportCollector* GetReporter() const
|
|
|
|
{
|
|
|
|
return mReporter;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void FlushConsoleReport() = 0;
|
2017-03-22 13:06:08 +03:00
|
|
|
|
2017-10-13 05:23:16 +03:00
|
|
|
// Called in OnStartRequest() to determine if the OnDataAvailable() method
|
|
|
|
// needs to be called. Invoking that method may generate additional main
|
|
|
|
// thread runnables.
|
|
|
|
virtual bool NeedOnDataAvailable() = 0;
|
|
|
|
|
|
|
|
// Called once when the first byte of data is received iff
|
|
|
|
// NeedOnDataAvailable() returned true when called in OnStartRequest().
|
2017-03-22 13:07:27 +03:00
|
|
|
virtual void OnDataAvailable() = 0;
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
protected:
|
|
|
|
virtual ~FetchDriverObserver()
|
|
|
|
{ };
|
2015-09-14 18:05:35 +03:00
|
|
|
|
|
|
|
virtual void OnResponseAvailableInternal(InternalResponse* aResponse) = 0;
|
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
nsCOMPtr<nsIConsoleReportCollector> mReporter;
|
2015-09-14 18:05:35 +03:00
|
|
|
private:
|
|
|
|
bool mGotResponseAvailable;
|
2014-10-06 22:01:20 +04:00
|
|
|
};
|
|
|
|
|
2017-12-04 09:39:15 +03:00
|
|
|
class AlternativeDataStreamListener;
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class FetchDriver final : public nsIStreamListener,
|
2016-01-20 00:54:15 +03:00
|
|
|
public nsIChannelEventSink,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsIInterfaceRequestor,
|
2017-03-22 13:06:08 +03:00
|
|
|
public nsIThreadRetargetableStreamListener,
|
2017-08-29 12:31:07 +03:00
|
|
|
public AbortFollower
|
2014-10-06 22:01:20 +04:00
|
|
|
{
|
|
|
|
public:
|
2017-09-26 13:53:00 +03:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2014-12-10 11:51:59 +03:00
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
2016-01-20 00:54:15 +03:00
|
|
|
NS_DECL_NSICHANNELEVENTSINK
|
2015-01-08 00:47:18 +03:00
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
2015-08-28 23:49:07 +03:00
|
|
|
NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
FetchDriver(InternalRequest* aRequest,
|
|
|
|
nsIPrincipal* aPrincipal,
|
2017-05-19 08:29:00 +03:00
|
|
|
nsILoadGroup* aLoadGroup,
|
2017-05-16 13:27:01 +03:00
|
|
|
nsIEventTarget* aMainThreadEventTarget,
|
2018-01-24 19:17:31 +03:00
|
|
|
PerformanceStorage* aPerformanceStorage,
|
2017-05-19 08:29:00 +03:00
|
|
|
bool aIsTrackingFetch);
|
2017-03-22 13:06:08 +03:00
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
nsresult Fetch(AbortSignal* aSignal,
|
2017-03-22 13:06:08 +03:00
|
|
|
FetchDriverObserver* aObserver);
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2014-12-23 18:56:19 +03:00
|
|
|
void
|
2015-03-05 23:16:18 +03:00
|
|
|
SetDocument(nsIDocument* aDocument);
|
2014-12-23 18:56:19 +03:00
|
|
|
|
2018-01-23 18:38:54 +03:00
|
|
|
void
|
|
|
|
SetClientInfo(const ClientInfo& aClientInfo);
|
|
|
|
|
|
|
|
void
|
|
|
|
SetController(const Maybe<ServiceWorkerDescriptor>& aController);
|
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
void
|
|
|
|
SetWorkerScript(const nsACString& aWorkerScirpt)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aWorkerScirpt.IsEmpty());
|
|
|
|
mWorkerScript = aWorkerScirpt;
|
|
|
|
}
|
|
|
|
|
2017-08-29 12:31:07 +03:00
|
|
|
// AbortFollower
|
2017-03-22 13:06:08 +03:00
|
|
|
void
|
2017-08-29 12:31:07 +03:00
|
|
|
Abort() override;
|
2017-03-22 13:06:08 +03:00
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
private:
|
2014-12-10 11:51:59 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2015-01-08 19:55:05 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> mLoadGroup;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalRequest> mRequest;
|
|
|
|
RefPtr<InternalResponse> mResponse;
|
2014-12-10 11:51:59 +03:00
|
|
|
nsCOMPtr<nsIOutputStream> mPipeOutputStream;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<FetchDriverObserver> mObserver;
|
2014-10-15 03:36:58 +04:00
|
|
|
nsCOMPtr<nsIDocument> mDocument;
|
2018-01-23 18:38:54 +03:00
|
|
|
Maybe<ClientInfo> mClientInfo;
|
|
|
|
Maybe<ServiceWorkerDescriptor> mController;
|
2017-03-22 13:06:08 +03:00
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
2016-09-07 05:20:23 +03:00
|
|
|
nsAutoPtr<SRICheckDataVerifier> mSRIDataVerifier;
|
2017-05-16 13:27:01 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
|
2018-01-24 19:17:31 +03:00
|
|
|
|
|
|
|
// This is set only when Fetch is used in workers.
|
|
|
|
RefPtr<PerformanceStorage> mPerformanceStorage;
|
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
SRIMetadata mSRIMetadata;
|
|
|
|
nsCString mWorkerScript;
|
2017-10-13 05:23:16 +03:00
|
|
|
|
|
|
|
// This is written once in OnStartRequest on the main thread and then
|
|
|
|
// written/read in OnDataAvailable() on any thread. Necko guarantees
|
|
|
|
// that these do not overlap.
|
|
|
|
bool mNeedToObserveOnDataAvailable;
|
|
|
|
|
2017-05-19 08:29:00 +03:00
|
|
|
bool mIsTrackingFetch;
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2017-12-04 09:39:15 +03:00
|
|
|
RefPtr<AlternativeDataStreamListener> mAltDataListener;
|
|
|
|
bool mOnStopRequestCalled;
|
|
|
|
|
2016-02-26 18:52:06 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool mResponseAvailableCalled;
|
|
|
|
bool mFetchCalled;
|
|
|
|
#endif
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2017-12-04 09:39:15 +03:00
|
|
|
friend class AlternativeDataStreamListener;
|
|
|
|
|
2015-01-07 02:35:02 +03:00
|
|
|
FetchDriver() = delete;
|
|
|
|
FetchDriver(const FetchDriver&) = delete;
|
|
|
|
FetchDriver& operator=(const FetchDriver&) = delete;
|
2014-10-06 22:01:20 +04:00
|
|
|
~FetchDriver();
|
|
|
|
|
2017-12-04 09:39:15 +03:00
|
|
|
|
|
|
|
nsresult HttpFetch(const nsACString& aPreferredAlternativeDataType = EmptyCString());
|
2014-12-10 11:51:59 +03:00
|
|
|
// Returns the filtered response sent to the observer.
|
|
|
|
already_AddRefed<InternalResponse>
|
2016-05-03 04:48:40 +03:00
|
|
|
BeginAndGetFilteredResponse(InternalResponse* aResponse,
|
2015-11-24 05:47:10 +03:00
|
|
|
bool aFoundOpaqueRedirect);
|
2014-12-10 11:51:59 +03:00
|
|
|
// Utility since not all cases need to do any post processing of the filtered
|
|
|
|
// response.
|
2017-11-15 22:53:42 +03:00
|
|
|
void FailWithNetworkError(nsresult rv);
|
2016-01-20 00:54:15 +03:00
|
|
|
|
|
|
|
void SetRequestHeaders(nsIHttpChannel* aChannel) const;
|
2017-12-04 09:39:15 +03:00
|
|
|
|
|
|
|
nsresult FinishOnStopRequest(AlternativeDataStreamListener* aAltDataListener);
|
2014-10-06 22:01:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_FetchDriver_h
|