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"
|
|
|
|
#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-03-22 13:06:08 +03:00
|
|
|
#include "mozilla/dom/FetchSignal.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;
|
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;
|
|
|
|
|
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-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
|
|
|
};
|
|
|
|
|
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,
|
|
|
|
public FetchSignal::Follower
|
2014-10-06 22:01:20 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-12-10 11:51:59 +03:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
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,
|
|
|
|
nsILoadGroup* aLoadGroup);
|
|
|
|
|
|
|
|
nsresult Fetch(FetchSignal* aSignal,
|
|
|
|
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
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
void
|
|
|
|
SetWorkerScript(const nsACString& aWorkerScirpt)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aWorkerScirpt.IsEmpty());
|
|
|
|
mWorkerScript = aWorkerScirpt;
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:06:08 +03:00
|
|
|
// FetchSignal::Follower
|
|
|
|
|
|
|
|
void
|
|
|
|
Aborted() override;
|
|
|
|
|
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;
|
2017-03-22 13:06:08 +03:00
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
2016-09-07 05:20:23 +03:00
|
|
|
nsAutoPtr<SRICheckDataVerifier> mSRIDataVerifier;
|
|
|
|
SRIMetadata mSRIMetadata;
|
|
|
|
nsCString mWorkerScript;
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2016-02-26 18:52:06 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool mResponseAvailableCalled;
|
|
|
|
bool mFetchCalled;
|
|
|
|
#endif
|
2014-12-10 11:51:59 +03:00
|
|
|
|
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();
|
|
|
|
|
2015-10-20 04:24:36 +03:00
|
|
|
nsresult HttpFetch();
|
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.
|
2016-02-01 05:56:00 +03:00
|
|
|
void FailWithNetworkError();
|
2016-01-20 00:54:15 +03:00
|
|
|
|
|
|
|
void SetRequestHeaders(nsIHttpChannel* aChannel) const;
|
2014-10-06 22:01:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_FetchDriver_h
|