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"
|
2019-06-15 19:46:22 +03:00
|
|
|
#include "mozilla/dom/SerializedStackHolder.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;
|
2019-03-08 12:04:11 +03:00
|
|
|
class nsICookieSettings;
|
2018-10-23 09:17:13 +03:00
|
|
|
class nsICSPEventListener;
|
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 {
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
class Document;
|
2014-10-06 22:01:20 +04:00
|
|
|
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){};
|
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;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
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-16 13:27:01 +03:00
|
|
|
nsILoadGroup* aLoadGroup, nsIEventTarget* aMainThreadEventTarget,
|
2019-03-08 12:04:11 +03:00
|
|
|
nsICookieSettings* aCookieSettings,
|
2018-01-24 19:17:31 +03:00
|
|
|
PerformanceStorage* aPerformanceStorage, bool aIsTrackingFetch);
|
2017-03-22 13:06:08 +03:00
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
nsresult Fetch(AbortSignalImpl* aSignalImpl, FetchDriverObserver* aObserver);
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
void SetDocument(Document* aDocument);
|
2014-12-23 18:56:19 +03:00
|
|
|
|
2018-10-23 09:17:13 +03:00
|
|
|
void SetCSPEventListener(nsICSPEventListener* aCSPEventListener);
|
|
|
|
|
2018-01-23 18:38:54 +03:00
|
|
|
void SetClientInfo(const ClientInfo& aClientInfo);
|
|
|
|
|
|
|
|
void SetController(const Maybe<ServiceWorkerDescriptor>& aController);
|
|
|
|
|
2019-06-15 19:46:22 +03:00
|
|
|
void SetWorkerScript(const nsACString& aWorkerScript) {
|
|
|
|
MOZ_ASSERT(!aWorkerScript.IsEmpty());
|
|
|
|
mWorkerScript = aWorkerScript;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetOriginStack(UniquePtr<SerializedStackHolder>&& aOriginStack) {
|
|
|
|
mOriginStack = std::move(aOriginStack);
|
2016-09-07 05:20:23 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 12:31:07 +03:00
|
|
|
// AbortFollower
|
|
|
|
void 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;
|
2019-01-02 16:05:23 +03:00
|
|
|
RefPtr<Document> mDocument;
|
2018-10-23 09:17:13 +03:00
|
|
|
nsCOMPtr<nsICSPEventListener> mCSPEventListener;
|
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
|
|
|
|
2019-03-08 12:04:11 +03:00
|
|
|
nsCOMPtr<nsICookieSettings> mCookieSettings;
|
|
|
|
|
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;
|
2019-06-15 19:46:22 +03:00
|
|
|
UniquePtr<SerializedStackHolder> mOriginStack;
|
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.
|
2016-05-03 04:48:40 +03:00
|
|
|
already_AddRefed<InternalResponse> BeginAndGetFilteredResponse(
|
2015-11-24 05:47:10 +03:00
|
|
|
InternalResponse* aResponse, 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
|