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-05-14 01:49:36 +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/. */
|
|
|
|
|
|
|
|
#include "ServiceWorkerEvents.h"
|
|
|
|
|
2016-06-07 23:10:18 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2017-08-11 04:04:56 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-10-30 05:53:25 +03:00
|
|
|
#include "nsIConsoleReportCollector.h"
|
2015-03-17 18:47:03 +03:00
|
|
|
#include "nsIHttpChannelInternal.h"
|
2015-02-19 04:34:29 +03:00
|
|
|
#include "nsINetworkInterceptController.h"
|
|
|
|
#include "nsIOutputStream.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
#include "nsIScriptError.h"
|
2017-04-10 16:13:21 +03:00
|
|
|
#include "nsITimedChannel.h"
|
2017-04-27 13:27:03 +03:00
|
|
|
#include "mozilla/Encoding.h"
|
2015-09-18 04:23:14 +03:00
|
|
|
#include "nsContentPolicyUtils.h"
|
2014-05-14 01:49:36 +04:00
|
|
|
#include "nsContentUtils.h"
|
2015-02-19 04:34:29 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsNetCID.h"
|
2015-09-18 04:23:14 +03:00
|
|
|
#include "nsNetUtil.h"
|
2015-03-05 02:28:45 +03:00
|
|
|
#include "nsSerializationHelper.h"
|
2015-04-15 19:47:03 +03:00
|
|
|
#include "nsQueryObject.h"
|
2018-01-27 00:08:59 +03:00
|
|
|
#include "ServiceWorker.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
#include "ServiceWorkerManager.h"
|
2014-05-14 01:49:36 +04:00
|
|
|
|
2015-10-30 05:53:25 +03:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2017-06-07 18:34:51 +03:00
|
|
|
#include "mozilla/LoadInfo.h"
|
2018-01-27 00:08:58 +03:00
|
|
|
#include "mozilla/Move.h"
|
2015-04-24 02:18:41 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2016-09-01 21:17:03 +03:00
|
|
|
#include "mozilla/dom/BodyUtil.h"
|
2017-12-12 23:44:47 +03:00
|
|
|
#include "mozilla/dom/Client.h"
|
2015-02-19 04:34:29 +03:00
|
|
|
#include "mozilla/dom/FetchEventBinding.h"
|
2015-11-10 09:31:41 +03:00
|
|
|
#include "mozilla/dom/MessagePort.h"
|
2015-02-19 04:34:29 +03:00
|
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
2016-03-16 23:44:24 +03:00
|
|
|
#include "mozilla/dom/PushEventBinding.h"
|
|
|
|
#include "mozilla/dom/PushMessageDataBinding.h"
|
2016-04-23 06:54:22 +03:00
|
|
|
#include "mozilla/dom/PushUtil.h"
|
2016-09-01 21:17:03 +03:00
|
|
|
#include "mozilla/dom/Request.h"
|
2015-09-17 15:10:42 +03:00
|
|
|
#include "mozilla/dom/TypedArray.h"
|
2016-09-01 21:17:03 +03:00
|
|
|
#include "mozilla/dom/Response.h"
|
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
2015-09-17 15:10:42 +03:00
|
|
|
|
2015-10-30 05:53:25 +03:00
|
|
|
#include "js/Conversions.h"
|
2015-10-31 20:34:20 +03:00
|
|
|
#include "js/TypeDecls.h"
|
|
|
|
#include "xpcpublic.h"
|
2015-03-20 18:44:20 +03:00
|
|
|
|
2018-01-27 00:08:58 +03:00
|
|
|
using namespace mozilla;
|
2014-05-14 01:49:36 +04:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2016-03-19 00:15:46 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncLog(nsIInterceptedChannel *aInterceptedChannel,
|
|
|
|
const nsACString& aRespondWithScriptSpec,
|
|
|
|
uint32_t aRespondWithLineNumber, uint32_t aRespondWithColumnNumber,
|
|
|
|
const nsACString& aMessageName, const nsTArray<nsString>& aParams)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aInterceptedChannel);
|
|
|
|
nsCOMPtr<nsIConsoleReportCollector> reporter =
|
|
|
|
aInterceptedChannel->GetConsoleReportCollector();
|
|
|
|
if (reporter) {
|
|
|
|
reporter->AddConsoleReport(nsIScriptError::errorFlag,
|
|
|
|
NS_LITERAL_CSTRING("Service Worker Interception"),
|
|
|
|
nsContentUtils::eDOM_PROPERTIES,
|
|
|
|
aRespondWithScriptSpec,
|
|
|
|
aRespondWithLineNumber,
|
|
|
|
aRespondWithColumnNumber,
|
|
|
|
aMessageName, aParams);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Params>
|
|
|
|
void
|
|
|
|
AsyncLog(nsIInterceptedChannel* aInterceptedChannel,
|
|
|
|
const nsACString& aRespondWithScriptSpec,
|
|
|
|
uint32_t aRespondWithLineNumber, uint32_t aRespondWithColumnNumber,
|
|
|
|
// We have to list one explicit string so that calls with an
|
|
|
|
// nsTArray of params won't end up in here.
|
|
|
|
const nsACString& aMessageName, const nsAString& aFirstParam,
|
|
|
|
Params&&... aParams)
|
|
|
|
{
|
|
|
|
nsTArray<nsString> paramsList(sizeof...(Params) + 1);
|
|
|
|
StringArrayAppender::Append(paramsList, sizeof...(Params) + 1,
|
|
|
|
aFirstParam, Forward<Params>(aParams)...);
|
|
|
|
AsyncLog(aInterceptedChannel, aRespondWithScriptSpec, aRespondWithLineNumber,
|
|
|
|
aRespondWithColumnNumber, aMessageName, paramsList);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2018-01-27 00:08:59 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2014-05-14 01:49:36 +04:00
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
CancelChannelRunnable::CancelChannelRunnable(
|
|
|
|
nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel,
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration,
|
|
|
|
nsresult aStatus)
|
2018-01-27 00:08:59 +03:00
|
|
|
: Runnable("dom::CancelChannelRunnable")
|
2017-06-12 22:34:10 +03:00
|
|
|
, mChannel(aChannel)
|
2016-04-27 12:24:04 +03:00
|
|
|
, mRegistration(aRegistration)
|
2015-09-04 22:00:24 +03:00
|
|
|
, mStatus(aStatus)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CancelChannelRunnable::Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-04-10 16:13:21 +03:00
|
|
|
|
|
|
|
// TODO: When bug 1204254 is implemented, this time marker should be moved to
|
|
|
|
// the point where the body of the network request is complete.
|
|
|
|
mChannel->SetHandleFetchEventEnd(TimeStamp::Now());
|
2017-04-26 01:16:57 +03:00
|
|
|
mChannel->SaveTimeStamps();
|
2017-04-10 16:13:21 +03:00
|
|
|
|
2017-10-09 20:03:40 +03:00
|
|
|
mChannel->CancelInterception(mStatus);
|
2016-04-27 12:24:04 +03:00
|
|
|
mRegistration->MaybeScheduleUpdate();
|
2015-09-04 22:00:24 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-19 04:34:29 +03:00
|
|
|
FetchEvent::FetchEvent(EventTarget* aOwner)
|
2015-10-27 05:51:30 +03:00
|
|
|
: ExtendableEvent(aOwner)
|
2015-10-30 05:53:25 +03:00
|
|
|
, mPreventDefaultLineNumber(0)
|
|
|
|
, mPreventDefaultColumnNumber(0)
|
2015-10-27 05:51:30 +03:00
|
|
|
, mIsReload(false)
|
|
|
|
, mWaitToRespond(false)
|
2015-02-19 04:34:29 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FetchEvent::~FetchEvent()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FetchEvent::PostInit(nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel,
|
2016-04-27 12:24:04 +03:00
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration,
|
2015-10-25 04:42:05 +03:00
|
|
|
const nsACString& aScriptSpec)
|
2015-02-19 04:34:29 +03:00
|
|
|
{
|
|
|
|
mChannel = aChannel;
|
2016-04-27 12:24:04 +03:00
|
|
|
mRegistration = aRegistration;
|
2015-10-01 02:11:03 +03:00
|
|
|
mScriptSpec.Assign(aScriptSpec);
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ already_AddRefed<FetchEvent>
|
|
|
|
FetchEvent::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aType,
|
|
|
|
const FetchEventInit& aOptions,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<EventTarget> owner = do_QueryObject(aGlobal.GetAsSupports());
|
2015-02-19 04:34:29 +03:00
|
|
|
MOZ_ASSERT(owner);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<FetchEvent> e = new FetchEvent(owner);
|
2015-02-19 04:34:29 +03:00
|
|
|
bool trusted = e->Init(owner);
|
|
|
|
e->InitEvent(aType, aOptions.mBubbles, aOptions.mCancelable);
|
|
|
|
e->SetTrusted(trusted);
|
2016-08-31 06:16:11 +03:00
|
|
|
e->SetComposed(aOptions.mComposed);
|
2016-01-09 06:35:22 +03:00
|
|
|
e->mRequest = aOptions.mRequest;
|
2015-11-26 02:05:34 +03:00
|
|
|
e->mClientId = aOptions.mClientId;
|
2015-10-25 06:29:43 +03:00
|
|
|
e->mIsReload = aOptions.mIsReload;
|
2015-02-19 04:34:29 +03:00
|
|
|
return e.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-10-17 23:38:55 +03:00
|
|
|
struct RespondWithClosure
|
|
|
|
{
|
|
|
|
nsMainThreadPtrHandle<nsIInterceptedChannel> mInterceptedChannel;
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> mRegistration;
|
|
|
|
const nsString mRequestURL;
|
|
|
|
const nsCString mRespondWithScriptSpec;
|
|
|
|
const uint32_t mRespondWithLineNumber;
|
|
|
|
const uint32_t mRespondWithColumnNumber;
|
|
|
|
|
|
|
|
RespondWithClosure(nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel,
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration,
|
|
|
|
const nsAString& aRequestURL,
|
|
|
|
const nsACString& aRespondWithScriptSpec,
|
|
|
|
uint32_t aRespondWithLineNumber,
|
|
|
|
uint32_t aRespondWithColumnNumber)
|
|
|
|
: mInterceptedChannel(aChannel)
|
|
|
|
, mRegistration(aRegistration)
|
|
|
|
, mRequestURL(aRequestURL)
|
|
|
|
, mRespondWithScriptSpec(aRespondWithScriptSpec)
|
|
|
|
, mRespondWithLineNumber(aRespondWithLineNumber)
|
|
|
|
, mRespondWithColumnNumber(aRespondWithColumnNumber)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class FinishResponse final : public Runnable
|
|
|
|
{
|
|
|
|
nsMainThreadPtrHandle<nsIInterceptedChannel> mChannel;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit FinishResponse(nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel)
|
2018-01-27 00:08:59 +03:00
|
|
|
: Runnable("dom::FinishResponse")
|
2017-10-17 23:38:55 +03:00
|
|
|
, mChannel(aChannel)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-10-17 23:38:55 +03:00
|
|
|
|
|
|
|
nsresult rv = mChannel->FinishSynthesizedResponse();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mChannel->CancelInterception(NS_ERROR_INTERCEPTION_FAILED);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeStamp timeStamp = TimeStamp::Now();
|
|
|
|
mChannel->SetHandleFetchEventEnd(timeStamp);
|
|
|
|
mChannel->SetFinishSynthesizedResponseEnd(timeStamp);
|
|
|
|
mChannel->SaveTimeStamps();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class BodyCopyHandle final : public nsIInterceptedBodyCallback
|
|
|
|
{
|
|
|
|
UniquePtr<RespondWithClosure> mClosure;
|
|
|
|
|
|
|
|
~BodyCopyHandle()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
explicit BodyCopyHandle(UniquePtr<RespondWithClosure>&& aClosure)
|
|
|
|
: mClosure(Move(aClosure))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
BodyComplete(nsresult aRv) override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(aRv))) {
|
|
|
|
AsyncLog(mClosure->mInterceptedChannel, mClosure->mRespondWithScriptSpec,
|
|
|
|
mClosure->mRespondWithLineNumber,
|
|
|
|
mClosure->mRespondWithColumnNumber,
|
|
|
|
NS_LITERAL_CSTRING("InterceptionFailedWithURL"),
|
|
|
|
mClosure->mRequestURL);
|
|
|
|
event = new CancelChannelRunnable(mClosure->mInterceptedChannel,
|
|
|
|
mClosure->mRegistration,
|
|
|
|
NS_ERROR_INTERCEPTION_FAILED);
|
|
|
|
} else {
|
|
|
|
event = new FinishResponse(mClosure->mInterceptedChannel);
|
|
|
|
}
|
|
|
|
|
|
|
|
mClosure.reset();
|
|
|
|
|
|
|
|
event->Run();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(BodyCopyHandle, nsIInterceptedBodyCallback)
|
2017-10-17 23:38:55 +03:00
|
|
|
|
2017-10-17 23:38:54 +03:00
|
|
|
class StartResponse final : public Runnable
|
2015-02-19 04:34:29 +03:00
|
|
|
{
|
|
|
|
nsMainThreadPtrHandle<nsIInterceptedChannel> mChannel;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> mInternalResponse;
|
2015-05-25 21:21:05 +03:00
|
|
|
ChannelInfo mWorkerChannelInfo;
|
2015-10-01 02:11:03 +03:00
|
|
|
const nsCString mScriptSpec;
|
2015-10-22 16:23:39 +03:00
|
|
|
const nsCString mResponseURLSpec;
|
2017-10-17 23:38:55 +03:00
|
|
|
UniquePtr<RespondWithClosure> mClosure;
|
2015-09-18 04:23:14 +03:00
|
|
|
|
2015-02-19 04:34:29 +03:00
|
|
|
public:
|
2017-10-17 23:38:54 +03:00
|
|
|
StartResponse(nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel,
|
|
|
|
InternalResponse* aInternalResponse,
|
|
|
|
const ChannelInfo& aWorkerChannelInfo,
|
|
|
|
const nsACString& aScriptSpec,
|
2017-10-17 23:38:55 +03:00
|
|
|
const nsACString& aResponseURLSpec,
|
|
|
|
UniquePtr<RespondWithClosure>&& aClosure)
|
2018-01-27 00:08:59 +03:00
|
|
|
: Runnable("dom::StartResponse")
|
2017-06-12 22:34:10 +03:00
|
|
|
, mChannel(aChannel)
|
2015-03-05 02:28:45 +03:00
|
|
|
, mInternalResponse(aInternalResponse)
|
2015-05-25 21:21:05 +03:00
|
|
|
, mWorkerChannelInfo(aWorkerChannelInfo)
|
2015-10-01 02:11:03 +03:00
|
|
|
, mScriptSpec(aScriptSpec)
|
2015-10-22 16:23:39 +03:00
|
|
|
, mResponseURLSpec(aResponseURLSpec)
|
2017-10-17 23:38:55 +03:00
|
|
|
, mClosure(Move(aClosure))
|
2015-02-19 04:34:29 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2016-08-08 05:18:10 +03:00
|
|
|
Run() override
|
2015-02-19 04:34:29 +03:00
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-03-05 02:28:45 +03:00
|
|
|
|
2015-10-22 21:07:32 +03:00
|
|
|
nsCOMPtr<nsIChannel> underlyingChannel;
|
|
|
|
nsresult rv = mChannel->GetChannel(getter_AddRefs(underlyingChannel));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(underlyingChannel, NS_ERROR_UNEXPECTED);
|
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = underlyingChannel->GetLoadInfo();
|
|
|
|
|
2017-02-07 06:49:34 +03:00
|
|
|
if (!loadInfo || !CSPPermitsResponse(loadInfo)) {
|
2017-10-09 20:03:40 +03:00
|
|
|
mChannel->CancelInterception(NS_ERROR_CONTENT_BLOCKED);
|
2015-09-18 04:23:14 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-25 21:21:05 +03:00
|
|
|
ChannelInfo channelInfo;
|
|
|
|
if (mInternalResponse->GetChannelInfo().IsInitialized()) {
|
|
|
|
channelInfo = mInternalResponse->GetChannelInfo();
|
|
|
|
} else {
|
2015-04-24 02:18:41 +03:00
|
|
|
// We are dealing with a synthesized response here, so fall back to the
|
2015-05-25 21:21:05 +03:00
|
|
|
// channel info for the worker script.
|
|
|
|
channelInfo = mWorkerChannelInfo;
|
2015-04-24 02:18:41 +03:00
|
|
|
}
|
2015-10-22 21:07:32 +03:00
|
|
|
rv = mChannel->SetChannelInfo(&channelInfo);
|
2015-05-25 21:21:05 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-10-09 20:03:40 +03:00
|
|
|
mChannel->CancelInterception(NS_ERROR_INTERCEPTION_FAILED);
|
2016-02-12 01:37:45 +03:00
|
|
|
return NS_OK;
|
2015-03-05 02:28:45 +03:00
|
|
|
}
|
|
|
|
|
2016-02-12 01:37:45 +03:00
|
|
|
rv = mChannel->SynthesizeStatus(mInternalResponse->GetUnfilteredStatus(),
|
|
|
|
mInternalResponse->GetUnfilteredStatusText());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-10-09 20:03:40 +03:00
|
|
|
mChannel->CancelInterception(NS_ERROR_INTERCEPTION_FAILED);
|
2016-02-12 01:37:45 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-03-17 18:48:30 +03:00
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<InternalHeaders::Entry, 5> entries;
|
2015-03-17 18:47:02 +03:00
|
|
|
mInternalResponse->UnfilteredHeaders()->GetEntries(entries);
|
2015-03-17 18:48:30 +03:00
|
|
|
for (uint32_t i = 0; i < entries.Length(); ++i) {
|
|
|
|
mChannel->SynthesizeHeader(entries[i].mName, entries[i].mValue);
|
|
|
|
}
|
|
|
|
|
2017-06-07 18:34:51 +03:00
|
|
|
auto castLoadInfo = static_cast<LoadInfo*>(loadInfo.get());
|
|
|
|
castLoadInfo->SynthesizeServiceWorkerTainting(mInternalResponse->GetTainting());
|
2015-10-22 21:07:32 +03:00
|
|
|
|
2017-12-04 09:39:21 +03:00
|
|
|
// Get the preferred alternative data type of outter channel
|
|
|
|
nsAutoCString preferredAltDataType(EmptyCString());
|
|
|
|
nsCOMPtr<nsICacheInfoChannel> outerChannel = do_QueryInterface(underlyingChannel);
|
|
|
|
if (outerChannel) {
|
|
|
|
outerChannel->GetPreferredAlternativeDataType(preferredAltDataType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the alternative data type saved in the InternalResponse
|
|
|
|
nsAutoCString altDataType;
|
2017-12-05 20:13:54 +03:00
|
|
|
nsCOMPtr<nsICacheInfoChannel> cacheInfoChannel =
|
|
|
|
mInternalResponse->TakeCacheInfoChannel().get();
|
2017-12-04 09:39:21 +03:00
|
|
|
if (cacheInfoChannel) {
|
|
|
|
cacheInfoChannel->GetAlternativeDataType(altDataType);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> body;
|
|
|
|
if (preferredAltDataType.Equals(altDataType)) {
|
|
|
|
body = mInternalResponse->TakeAlternativeBody();
|
|
|
|
}
|
|
|
|
if (!body) {
|
|
|
|
mInternalResponse->GetUnfilteredBody(getter_AddRefs(body));
|
2017-12-15 09:35:44 +03:00
|
|
|
} else {
|
|
|
|
Telemetry::ScalarAdd(Telemetry::ScalarID::SW_ALTERNATIVE_BODY_USED_COUNT, 1);
|
2017-12-04 09:39:21 +03:00
|
|
|
}
|
|
|
|
|
2017-10-17 23:38:55 +03:00
|
|
|
RefPtr<BodyCopyHandle> copyHandle;
|
|
|
|
copyHandle = new BodyCopyHandle(Move(mClosure));
|
|
|
|
|
2017-12-04 09:39:21 +03:00
|
|
|
rv = mChannel->StartSynthesizedResponse(body, copyHandle, cacheInfoChannel,
|
|
|
|
mResponseURLSpec,
|
2017-11-01 10:45:35 +03:00
|
|
|
mInternalResponse->IsRedirected());
|
2017-10-17 23:38:54 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mChannel->CancelInterception(NS_ERROR_INTERCEPTION_FAILED);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:38:55 +03:00
|
|
|
nsCOMPtr<nsIObserverService> obsService = services::GetObserverService();
|
|
|
|
if (obsService) {
|
|
|
|
obsService->NotifyObservers(underlyingChannel, "service-worker-synthesized-response", nullptr);
|
|
|
|
}
|
|
|
|
|
2015-02-19 04:34:29 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2017-10-17 23:38:54 +03:00
|
|
|
|
2015-10-22 21:07:32 +03:00
|
|
|
bool CSPPermitsResponse(nsILoadInfo* aLoadInfo)
|
2015-09-18 04:23:14 +03:00
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-22 21:07:32 +03:00
|
|
|
MOZ_ASSERT(aLoadInfo);
|
2015-09-18 04:23:14 +03:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2016-11-07 09:59:00 +03:00
|
|
|
nsCString url = mInternalResponse->GetUnfilteredURL();
|
2015-09-18 04:23:14 +03:00
|
|
|
if (url.IsEmpty()) {
|
|
|
|
// Synthetic response. The buck stops at the worker script.
|
2015-10-01 02:11:03 +03:00
|
|
|
url = mScriptSpec;
|
2015-09-18 04:23:14 +03:00
|
|
|
}
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), url, nullptr, nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
int16_t decision = nsIContentPolicy::ACCEPT;
|
2018-03-29 13:16:23 +03:00
|
|
|
rv = NS_CheckContentLoadPolicy(uri, aLoadInfo, EmptyCString(), &decision);
|
2015-09-18 04:23:14 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
return decision == nsIContentPolicy::ACCEPT;
|
|
|
|
}
|
2015-02-19 04:34:29 +03:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class RespondWithHandler final : public PromiseNativeHandler
|
2015-02-19 04:34:29 +03:00
|
|
|
{
|
|
|
|
nsMainThreadPtrHandle<nsIInterceptedChannel> mInterceptedChannel;
|
2016-04-27 12:24:04 +03:00
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> mRegistration;
|
2015-09-01 00:26:29 +03:00
|
|
|
const RequestMode mRequestMode;
|
2015-12-17 03:06:54 +03:00
|
|
|
const RequestRedirect mRequestRedirectMode;
|
2016-02-26 18:52:06 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
const bool mIsClientRequest;
|
|
|
|
#endif
|
2015-10-01 02:11:03 +03:00
|
|
|
const nsCString mScriptSpec;
|
2015-10-30 05:53:25 +03:00
|
|
|
const nsString mRequestURL;
|
2018-02-21 18:28:32 +03:00
|
|
|
const nsCString mRequestFragment;
|
2015-10-30 05:53:25 +03:00
|
|
|
const nsCString mRespondWithScriptSpec;
|
|
|
|
const uint32_t mRespondWithLineNumber;
|
|
|
|
const uint32_t mRespondWithColumnNumber;
|
2015-10-01 02:11:03 +03:00
|
|
|
bool mRequestWasHandled;
|
2015-02-19 04:34:29 +03:00
|
|
|
public:
|
2015-07-09 09:56:00 +03:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2015-02-19 04:34:29 +03:00
|
|
|
RespondWithHandler(nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel,
|
2016-04-27 12:24:04 +03:00
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration,
|
2015-09-01 00:26:29 +03:00
|
|
|
RequestMode aRequestMode, bool aIsClientRequest,
|
2015-12-17 03:06:54 +03:00
|
|
|
RequestRedirect aRedirectMode,
|
2015-10-30 05:53:25 +03:00
|
|
|
const nsACString& aScriptSpec,
|
2015-10-30 05:53:25 +03:00
|
|
|
const nsAString& aRequestURL,
|
2018-02-21 18:28:32 +03:00
|
|
|
const nsACString& aRequestFragment,
|
2015-10-30 05:53:25 +03:00
|
|
|
const nsACString& aRespondWithScriptSpec,
|
|
|
|
uint32_t aRespondWithLineNumber,
|
|
|
|
uint32_t aRespondWithColumnNumber)
|
2015-02-19 04:34:29 +03:00
|
|
|
: mInterceptedChannel(aChannel)
|
2016-04-27 12:24:04 +03:00
|
|
|
, mRegistration(aRegistration)
|
2015-03-17 18:47:03 +03:00
|
|
|
, mRequestMode(aRequestMode)
|
2015-12-17 03:06:54 +03:00
|
|
|
, mRequestRedirectMode(aRedirectMode)
|
2016-02-26 18:52:06 +03:00
|
|
|
#ifdef DEBUG
|
2015-07-14 23:11:26 +03:00
|
|
|
, mIsClientRequest(aIsClientRequest)
|
2016-02-26 18:52:06 +03:00
|
|
|
#endif
|
2015-10-01 02:11:03 +03:00
|
|
|
, mScriptSpec(aScriptSpec)
|
2015-10-30 05:53:25 +03:00
|
|
|
, mRequestURL(aRequestURL)
|
2018-02-21 18:28:32 +03:00
|
|
|
, mRequestFragment(aRequestFragment)
|
2015-10-30 05:53:25 +03:00
|
|
|
, mRespondWithScriptSpec(aRespondWithScriptSpec)
|
|
|
|
, mRespondWithLineNumber(aRespondWithLineNumber)
|
|
|
|
, mRespondWithColumnNumber(aRespondWithColumnNumber)
|
2015-10-01 02:11:03 +03:00
|
|
|
, mRequestWasHandled(false)
|
2015-02-19 04:34:29 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
2015-02-19 04:34:29 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
2015-02-19 04:34:29 +03:00
|
|
|
|
2015-07-14 23:11:26 +03:00
|
|
|
void CancelRequest(nsresult aStatus);
|
2015-10-30 05:53:25 +03:00
|
|
|
|
2015-10-30 05:53:25 +03:00
|
|
|
void AsyncLog(const nsACString& aMessageName, const nsTArray<nsString>& aParams)
|
|
|
|
{
|
|
|
|
::AsyncLog(mInterceptedChannel, mRespondWithScriptSpec, mRespondWithLineNumber,
|
|
|
|
mRespondWithColumnNumber, aMessageName, aParams);
|
|
|
|
}
|
2015-10-30 05:53:25 +03:00
|
|
|
|
2015-10-31 20:34:20 +03:00
|
|
|
void AsyncLog(const nsACString& aSourceSpec, uint32_t aLine, uint32_t aColumn,
|
|
|
|
const nsACString& aMessageName, const nsTArray<nsString>& aParams)
|
|
|
|
{
|
|
|
|
::AsyncLog(mInterceptedChannel, aSourceSpec, aLine, aColumn, aMessageName,
|
|
|
|
aParams);
|
|
|
|
}
|
|
|
|
|
2015-07-09 09:56:00 +03:00
|
|
|
private:
|
2015-10-01 02:11:03 +03:00
|
|
|
~RespondWithHandler()
|
|
|
|
{
|
|
|
|
if (!mRequestWasHandled) {
|
2015-10-30 05:53:25 +03:00
|
|
|
::AsyncLog(mInterceptedChannel, mRespondWithScriptSpec,
|
|
|
|
mRespondWithLineNumber, mRespondWithColumnNumber,
|
2015-11-20 21:36:46 +03:00
|
|
|
NS_LITERAL_CSTRING("InterceptionFailedWithURL"), mRequestURL);
|
2015-10-01 02:11:03 +03:00
|
|
|
CancelRequest(NS_ERROR_INTERCEPTION_FAILED);
|
|
|
|
}
|
|
|
|
}
|
2015-02-19 04:34:29 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class MOZ_STACK_CLASS AutoCancel
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<RespondWithHandler> mOwner;
|
2015-10-31 20:34:20 +03:00
|
|
|
nsCString mSourceSpec;
|
|
|
|
uint32_t mLine;
|
|
|
|
uint32_t mColumn;
|
2015-10-30 05:53:25 +03:00
|
|
|
nsCString mMessageName;
|
2015-10-30 05:53:25 +03:00
|
|
|
nsTArray<nsString> mParams;
|
2015-02-19 04:34:29 +03:00
|
|
|
|
|
|
|
public:
|
2015-10-30 05:53:25 +03:00
|
|
|
AutoCancel(RespondWithHandler* aOwner, const nsString& aRequestURL)
|
2015-02-19 04:34:29 +03:00
|
|
|
: mOwner(aOwner)
|
2015-10-31 20:34:20 +03:00
|
|
|
, mLine(0)
|
|
|
|
, mColumn(0)
|
2015-10-30 05:53:25 +03:00
|
|
|
, mMessageName(NS_LITERAL_CSTRING("InterceptionFailedWithURL"))
|
2015-02-19 04:34:29 +03:00
|
|
|
{
|
2015-10-30 05:53:25 +03:00
|
|
|
mParams.AppendElement(aRequestURL);
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoCancel()
|
|
|
|
{
|
|
|
|
if (mOwner) {
|
2015-10-31 20:34:20 +03:00
|
|
|
if (mSourceSpec.IsEmpty()) {
|
|
|
|
mOwner->AsyncLog(mMessageName, mParams);
|
|
|
|
} else {
|
|
|
|
mOwner->AsyncLog(mSourceSpec, mLine, mColumn, mMessageName, mParams);
|
|
|
|
}
|
2015-10-30 05:53:25 +03:00
|
|
|
mOwner->CancelRequest(NS_ERROR_INTERCEPTION_FAILED);
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
// This function steals the error message from a ErrorResult.
|
|
|
|
void
|
|
|
|
SetCancelErrorResult(JSContext* aCx, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aRv.Failed());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!JS_IsExceptionPending(aCx));
|
|
|
|
|
|
|
|
// Storing the error as exception in the JSContext.
|
|
|
|
if (!aRv.MaybeSetPendingException(aCx)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!aRv.Failed());
|
|
|
|
|
|
|
|
// Let's take the pending exception.
|
|
|
|
JS::Rooted<JS::Value> exn(aCx);
|
|
|
|
if (!JS_GetPendingException(aCx, &exn)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
|
|
|
|
// Converting the exception in a js::ErrorReport.
|
|
|
|
js::ErrorReport report(aCx);
|
|
|
|
if (!report.init(aCx, exn, js::ErrorReport::WithSideEffects)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mOwner);
|
|
|
|
MOZ_ASSERT(mMessageName.EqualsLiteral("InterceptionFailedWithURL"));
|
|
|
|
MOZ_ASSERT(mParams.Length() == 1);
|
|
|
|
|
|
|
|
// Let's store the error message here.
|
|
|
|
mMessageName.Assign(report.toStringResult().c_str());
|
|
|
|
mParams.Clear();
|
|
|
|
}
|
|
|
|
|
2015-10-30 05:53:25 +03:00
|
|
|
template<typename... Params>
|
2015-11-20 21:36:46 +03:00
|
|
|
void SetCancelMessage(const nsACString& aMessageName, Params&&... aParams)
|
2015-07-14 23:11:26 +03:00
|
|
|
{
|
2015-10-30 05:53:25 +03:00
|
|
|
MOZ_ASSERT(mOwner);
|
2015-10-30 05:53:25 +03:00
|
|
|
MOZ_ASSERT(mMessageName.EqualsLiteral("InterceptionFailedWithURL"));
|
|
|
|
MOZ_ASSERT(mParams.Length() == 1);
|
2015-10-30 05:53:25 +03:00
|
|
|
mMessageName = aMessageName;
|
2015-10-30 05:53:25 +03:00
|
|
|
mParams.Clear();
|
2015-11-20 21:36:46 +03:00
|
|
|
StringArrayAppender::Append(mParams, sizeof...(Params),
|
|
|
|
Forward<Params>(aParams)...);
|
2015-07-14 23:11:26 +03:00
|
|
|
}
|
|
|
|
|
2015-10-31 20:34:20 +03:00
|
|
|
template<typename... Params>
|
|
|
|
void SetCancelMessageAndLocation(const nsACString& aSourceSpec,
|
|
|
|
uint32_t aLine, uint32_t aColumn,
|
|
|
|
const nsACString& aMessageName,
|
2015-11-20 21:36:46 +03:00
|
|
|
Params&&... aParams)
|
2015-10-31 20:34:20 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwner);
|
|
|
|
MOZ_ASSERT(mMessageName.EqualsLiteral("InterceptionFailedWithURL"));
|
|
|
|
MOZ_ASSERT(mParams.Length() == 1);
|
|
|
|
|
|
|
|
mSourceSpec = aSourceSpec;
|
|
|
|
mLine = aLine;
|
|
|
|
mColumn = aColumn;
|
|
|
|
|
|
|
|
mMessageName = aMessageName;
|
|
|
|
mParams.Clear();
|
2015-11-20 21:36:46 +03:00
|
|
|
StringArrayAppender::Append(mParams, sizeof...(Params),
|
|
|
|
Forward<Params>(aParams)...);
|
2015-10-31 20:34:20 +03:00
|
|
|
}
|
|
|
|
|
2015-02-19 04:34:29 +03:00
|
|
|
void Reset()
|
|
|
|
{
|
|
|
|
mOwner = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-09 09:56:00 +03:00
|
|
|
NS_IMPL_ISUPPORTS0(RespondWithHandler)
|
|
|
|
|
2015-02-19 04:34:29 +03:00
|
|
|
void
|
|
|
|
RespondWithHandler::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
2015-10-30 05:53:25 +03:00
|
|
|
AutoCancel autoCancel(this, mRequestURL);
|
2017-04-26 01:16:57 +03:00
|
|
|
mInterceptedChannel->SetFinishResponseStart(TimeStamp::Now());
|
2015-02-19 04:34:29 +03:00
|
|
|
|
|
|
|
if (!aValue.isObject()) {
|
2015-05-04 23:51:51 +03:00
|
|
|
NS_WARNING("FetchEvent::RespondWith was passed a promise resolved to a non-Object value");
|
2015-10-31 20:34:20 +03:00
|
|
|
|
|
|
|
nsCString sourceSpec;
|
|
|
|
uint32_t line = 0;
|
|
|
|
uint32_t column = 0;
|
|
|
|
nsString valueString;
|
2017-08-11 04:04:56 +03:00
|
|
|
nsContentUtils::ExtractErrorValues(aCx, aValue, sourceSpec, &line, &column,
|
|
|
|
valueString);
|
2015-10-31 20:34:20 +03:00
|
|
|
|
|
|
|
autoCancel.SetCancelMessageAndLocation(sourceSpec, line, column,
|
|
|
|
NS_LITERAL_CSTRING("InterceptedNonResponseWithURL"),
|
2015-11-20 21:36:46 +03:00
|
|
|
mRequestURL, valueString);
|
2015-02-19 04:34:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Response> response;
|
2015-02-19 04:34:29 +03:00
|
|
|
nsresult rv = UNWRAP_OBJECT(Response, &aValue.toObject(), response);
|
|
|
|
if (NS_FAILED(rv)) {
|
2015-10-31 20:34:20 +03:00
|
|
|
nsCString sourceSpec;
|
|
|
|
uint32_t line = 0;
|
|
|
|
uint32_t column = 0;
|
|
|
|
nsString valueString;
|
2017-08-11 04:04:56 +03:00
|
|
|
nsContentUtils::ExtractErrorValues(aCx, aValue, sourceSpec, &line, &column,
|
|
|
|
valueString);
|
2015-10-31 20:34:20 +03:00
|
|
|
|
|
|
|
autoCancel.SetCancelMessageAndLocation(sourceSpec, line, column,
|
|
|
|
NS_LITERAL_CSTRING("InterceptedNonResponseWithURL"),
|
2015-11-20 21:36:46 +03:00
|
|
|
mRequestURL, valueString);
|
2015-02-19 04:34:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-03 06:09:21 +03:00
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
|
2015-12-17 03:06:54 +03:00
|
|
|
// Section "HTTP Fetch", step 3.3:
|
2015-07-14 23:11:26 +03:00
|
|
|
// If one of the following conditions is true, return a network error:
|
|
|
|
// * response's type is "error".
|
|
|
|
// * request's mode is not "no-cors" and response's type is "opaque".
|
2015-12-17 03:06:54 +03:00
|
|
|
// * request's redirect mode is not "manual" and response's type is
|
2015-09-01 00:26:29 +03:00
|
|
|
// "opaqueredirect".
|
2016-05-03 04:48:40 +03:00
|
|
|
// * request's redirect mode is not "follow" and response's url list
|
|
|
|
// has more than one item.
|
2015-07-14 23:11:26 +03:00
|
|
|
|
|
|
|
if (response->Type() == ResponseType::Error) {
|
2015-10-30 05:53:25 +03:00
|
|
|
autoCancel.SetCancelMessage(
|
2015-11-20 21:36:46 +03:00
|
|
|
NS_LITERAL_CSTRING("InterceptedErrorResponseWithURL"), mRequestURL);
|
2015-07-14 23:11:26 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-14 02:15:37 +03:00
|
|
|
MOZ_ASSERT_IF(mIsClientRequest, mRequestMode == RequestMode::Same_origin ||
|
|
|
|
mRequestMode == RequestMode::Navigate);
|
2015-09-01 17:58:34 +03:00
|
|
|
|
2015-07-14 23:11:26 +03:00
|
|
|
if (response->Type() == ResponseType::Opaque && mRequestMode != RequestMode::No_cors) {
|
2015-10-30 05:53:25 +03:00
|
|
|
uint32_t mode = static_cast<uint32_t>(mRequestMode);
|
|
|
|
NS_ConvertASCIItoUTF16 modeString(RequestModeValues::strings[mode].value,
|
|
|
|
RequestModeValues::strings[mode].length);
|
|
|
|
|
|
|
|
autoCancel.SetCancelMessage(
|
|
|
|
NS_LITERAL_CSTRING("BadOpaqueInterceptionRequestModeWithURL"),
|
2015-11-20 21:36:46 +03:00
|
|
|
mRequestURL, modeString);
|
2015-07-14 23:11:26 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-17 03:06:54 +03:00
|
|
|
if (mRequestRedirectMode != RequestRedirect::Manual &&
|
|
|
|
response->Type() == ResponseType::Opaqueredirect) {
|
2015-10-30 05:53:25 +03:00
|
|
|
autoCancel.SetCancelMessage(
|
2015-11-20 21:36:46 +03:00
|
|
|
NS_LITERAL_CSTRING("BadOpaqueRedirectInterceptionWithURL"), mRequestURL);
|
2015-09-01 00:26:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:48:40 +03:00
|
|
|
if (mRequestRedirectMode != RequestRedirect::Follow && response->Redirected()) {
|
|
|
|
autoCancel.SetCancelMessage(
|
|
|
|
NS_LITERAL_CSTRING("BadRedirectModeInterceptionWithURL"), mRequestURL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:48:30 +03:00
|
|
|
if (NS_WARN_IF(response->BodyUsed())) {
|
2015-10-30 05:53:25 +03:00
|
|
|
autoCancel.SetCancelMessage(
|
2015-11-20 21:36:46 +03:00
|
|
|
NS_LITERAL_CSTRING("InterceptedUsedResponseWithURL"), mRequestURL);
|
2015-03-05 02:28:45 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalResponse> ir = response->GetInternalResponse();
|
2015-03-17 18:48:30 +03:00
|
|
|
if (NS_WARN_IF(!ir)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-01 10:45:35 +03:00
|
|
|
|
|
|
|
// An extra safety check to make sure our invariant that opaque and cors
|
|
|
|
// responses always have a URL does not break.
|
|
|
|
if (NS_WARN_IF((response->Type() == ResponseType::Opaque ||
|
|
|
|
response->Type() == ResponseType::Cors) &&
|
|
|
|
ir->GetUnfilteredURL().IsEmpty())) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(false, "Cors or opaque Response without a URL");
|
|
|
|
return;
|
2015-10-22 16:23:39 +03:00
|
|
|
}
|
2017-10-17 23:38:54 +03:00
|
|
|
|
2017-11-16 04:45:39 +03:00
|
|
|
Telemetry::ScalarAdd(Telemetry::ScalarID::SW_SYNTHESIZED_RES_COUNT, 1);
|
|
|
|
|
|
|
|
if (mRequestMode == RequestMode::Same_origin &&
|
|
|
|
response->Type() == ResponseType::Cors) {
|
|
|
|
Telemetry::ScalarAdd(Telemetry::ScalarID::SW_CORS_RES_FOR_SO_REQ_COUNT, 1);
|
2017-11-01 10:45:35 +03:00
|
|
|
|
2017-11-28 05:21:07 +03:00
|
|
|
// XXXtt: Will have a pref to enable the quirk response in bug 1419684.
|
|
|
|
// The variadic template provided by StringArrayAppender requires exactly
|
|
|
|
// an nsString.
|
|
|
|
NS_ConvertUTF8toUTF16 responseURL(ir->GetUnfilteredURL());
|
|
|
|
autoCancel.SetCancelMessage(
|
|
|
|
NS_LITERAL_CSTRING("CorsResponseForSameOriginRequest"), mRequestURL,
|
|
|
|
responseURL);
|
|
|
|
return;
|
2017-11-01 10:45:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Propagate the URL to the content if the request mode is not "navigate".
|
|
|
|
// Note that, we only reflect the final URL if the response.redirected is
|
|
|
|
// false. We propagate all the URLs if the response.redirected is true.
|
|
|
|
nsCString responseURL;
|
|
|
|
if (mRequestMode != RequestMode::Navigate) {
|
|
|
|
responseURL = ir->GetUnfilteredURL();
|
2018-02-21 18:28:32 +03:00
|
|
|
|
|
|
|
// Similar to how we apply the request fragment to redirects automatically
|
|
|
|
// we also want to apply it automatically when propagating the response
|
|
|
|
// URL from a service worker interception. Currently response.url strips
|
|
|
|
// the fragment, so this will never conflict with an existing fragment
|
|
|
|
// on the response. In the future we will have to check for a response
|
|
|
|
// fragment and avoid overriding in that case.
|
2018-03-19 14:29:00 +03:00
|
|
|
if (!mRequestFragment.IsEmpty() && !responseURL.IsEmpty()) {
|
2018-02-21 18:28:32 +03:00
|
|
|
MOZ_ASSERT(!responseURL.Contains('#'));
|
|
|
|
responseURL.Append(NS_LITERAL_CSTRING("#"));
|
|
|
|
responseURL.Append(mRequestFragment);
|
|
|
|
}
|
2017-11-16 04:45:39 +03:00
|
|
|
}
|
|
|
|
|
2017-10-17 23:38:55 +03:00
|
|
|
UniquePtr<RespondWithClosure> closure(new RespondWithClosure(mInterceptedChannel,
|
2017-10-17 23:38:54 +03:00
|
|
|
mRegistration,
|
2015-10-30 05:53:25 +03:00
|
|
|
mRequestURL,
|
|
|
|
mRespondWithScriptSpec,
|
|
|
|
mRespondWithLineNumber,
|
|
|
|
mRespondWithColumnNumber));
|
2017-10-17 23:38:55 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> startRunnable = new StartResponse(mInterceptedChannel,
|
|
|
|
ir,
|
|
|
|
worker->GetChannelInfo(),
|
|
|
|
mScriptSpec,
|
|
|
|
responseURL,
|
|
|
|
Move(closure));
|
|
|
|
|
2015-03-17 18:48:30 +03:00
|
|
|
nsCOMPtr<nsIInputStream> body;
|
2015-09-01 00:26:29 +03:00
|
|
|
ir->GetUnfilteredBody(getter_AddRefs(body));
|
2015-03-17 18:48:30 +03:00
|
|
|
// Errors and redirects may not have a body.
|
|
|
|
if (body) {
|
2018-02-01 22:21:14 +03:00
|
|
|
ErrorResult error;
|
2017-08-11 04:04:55 +03:00
|
|
|
response->SetBodyUsed(aCx, error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
autoCancel.SetCancelErrorResult(aCx, error);
|
|
|
|
return;
|
|
|
|
}
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
2017-10-17 23:38:55 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(startRunnable.forget()));
|
|
|
|
|
2015-03-17 18:48:30 +03:00
|
|
|
MOZ_ASSERT(!closure);
|
2015-02-19 04:34:29 +03:00
|
|
|
autoCancel.Reset();
|
2015-10-01 02:11:03 +03:00
|
|
|
mRequestWasHandled = true;
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RespondWithHandler::RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
2015-10-31 20:34:20 +03:00
|
|
|
nsCString sourceSpec = mRespondWithScriptSpec;
|
|
|
|
uint32_t line = mRespondWithLineNumber;
|
|
|
|
uint32_t column = mRespondWithColumnNumber;
|
|
|
|
nsString valueString;
|
|
|
|
|
2017-04-26 01:16:57 +03:00
|
|
|
mInterceptedChannel->SetFinishResponseStart(TimeStamp::Now());
|
|
|
|
|
2017-08-11 04:04:56 +03:00
|
|
|
nsContentUtils::ExtractErrorValues(aCx, aValue, sourceSpec, &line, &column,
|
|
|
|
valueString);
|
2015-10-31 20:34:20 +03:00
|
|
|
|
|
|
|
::AsyncLog(mInterceptedChannel, sourceSpec, line, column,
|
|
|
|
NS_LITERAL_CSTRING("InterceptionRejectedResponseWithURL"),
|
2015-11-20 21:36:46 +03:00
|
|
|
mRequestURL, valueString);
|
2015-10-31 20:34:20 +03:00
|
|
|
|
2015-10-30 05:53:25 +03:00
|
|
|
CancelRequest(NS_ERROR_INTERCEPTION_FAILED);
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-14 23:11:26 +03:00
|
|
|
RespondWithHandler::CancelRequest(nsresult aStatus)
|
2015-02-19 04:34:29 +03:00
|
|
|
{
|
2015-07-14 23:11:26 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2016-04-27 12:24:04 +03:00
|
|
|
new CancelChannelRunnable(mInterceptedChannel, mRegistration, aStatus);
|
2016-09-14 06:14:02 +03:00
|
|
|
// Note, this may run off the worker thread during worker termination.
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
if (worker) {
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(runnable.forget()));
|
|
|
|
} else {
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable.forget()));
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
mRequestWasHandled = true;
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-02-19 04:34:29 +03:00
|
|
|
|
|
|
|
void
|
2015-10-30 05:53:25 +03:00
|
|
|
FetchEvent::RespondWith(JSContext* aCx, Promise& aArg, ErrorResult& aRv)
|
2015-02-19 04:34:29 +03:00
|
|
|
{
|
2015-10-08 00:01:28 +03:00
|
|
|
if (EventPhase() == nsIDOMEvent::NONE || mWaitToRespond) {
|
2015-02-19 04:34:29 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-30 05:53:25 +03:00
|
|
|
|
|
|
|
// Record where respondWith() was called in the script so we can include the
|
|
|
|
// information in any error reporting. We should be guaranteed not to get
|
|
|
|
// a file:// string here because service workers require http/https.
|
|
|
|
nsCString spec;
|
|
|
|
uint32_t line = 0;
|
|
|
|
uint32_t column = 0;
|
|
|
|
nsJSUtils::GetCallingLocation(aCx, spec, &line, &column);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalRequest> ir = mRequest->GetInternalRequest();
|
2015-10-30 05:53:25 +03:00
|
|
|
|
|
|
|
nsAutoCString requestURL;
|
|
|
|
ir->GetURL(requestURL);
|
|
|
|
|
2015-09-30 05:53:53 +03:00
|
|
|
StopImmediatePropagation();
|
2015-02-19 04:34:29 +03:00
|
|
|
mWaitToRespond = true;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<RespondWithHandler> handler =
|
2016-04-27 12:24:04 +03:00
|
|
|
new RespondWithHandler(mChannel, mRegistration, mRequest->Mode(),
|
|
|
|
ir->IsClientRequest(), mRequest->Redirect(),
|
|
|
|
mScriptSpec, NS_ConvertUTF8toUTF16(requestURL),
|
2018-02-21 18:28:32 +03:00
|
|
|
ir->GetFragment(), spec, line, column);
|
2015-07-31 20:30:55 +03:00
|
|
|
aArg.AppendNativeHandler(handler);
|
2015-10-27 05:51:30 +03:00
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
if (!WaitOnPromise(aArg)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
}
|
2015-03-20 18:44:20 +03:00
|
|
|
}
|
|
|
|
|
2015-10-30 05:53:25 +03:00
|
|
|
void
|
2016-11-16 22:10:22 +03:00
|
|
|
FetchEvent::PreventDefault(JSContext* aCx, CallerType aCallerType)
|
2015-10-30 05:53:25 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
2016-11-16 22:10:22 +03:00
|
|
|
MOZ_ASSERT(aCallerType != CallerType::System,
|
|
|
|
"Since when do we support system-principal service workers?");
|
2015-10-30 05:53:25 +03:00
|
|
|
|
|
|
|
if (mPreventDefaultScriptSpec.IsEmpty()) {
|
|
|
|
// Note when the FetchEvent might have been canceled by script, but don't
|
|
|
|
// actually log the location until we are sure it matters. This is
|
|
|
|
// determined in ServiceWorkerPrivate.cpp. We only remember the first
|
|
|
|
// call to preventDefault() as its the most likely to have actually canceled
|
|
|
|
// the event.
|
|
|
|
nsJSUtils::GetCallingLocation(aCx, mPreventDefaultScriptSpec,
|
|
|
|
&mPreventDefaultLineNumber,
|
|
|
|
&mPreventDefaultColumnNumber);
|
|
|
|
}
|
|
|
|
|
2016-11-16 22:10:22 +03:00
|
|
|
Event::PreventDefault(aCx, aCallerType);
|
2015-10-30 05:53:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FetchEvent::ReportCanceled()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mPreventDefaultScriptSpec.IsEmpty());
|
|
|
|
|
|
|
|
RefPtr<InternalRequest> ir = mRequest->GetInternalRequest();
|
|
|
|
nsAutoCString url;
|
|
|
|
ir->GetURL(url);
|
|
|
|
|
|
|
|
// The variadic template provided by StringArrayAppender requires exactly
|
|
|
|
// an nsString.
|
|
|
|
NS_ConvertUTF8toUTF16 requestURL(url);
|
|
|
|
//nsString requestURL;
|
|
|
|
//CopyUTF8toUTF16(url, requestURL);
|
|
|
|
|
|
|
|
::AsyncLog(mChannel.get(), mPreventDefaultScriptSpec,
|
|
|
|
mPreventDefaultLineNumber, mPreventDefaultColumnNumber,
|
2015-11-20 21:36:46 +03:00
|
|
|
NS_LITERAL_CSTRING("InterceptionCanceledWithURL"), requestURL);
|
2015-10-30 05:53:25 +03:00
|
|
|
}
|
|
|
|
|
2015-11-20 00:15:17 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class WaitUntilHandler final : public PromiseNativeHandler
|
|
|
|
{
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
const nsCString mScope;
|
|
|
|
nsCString mSourceSpec;
|
|
|
|
uint32_t mLine;
|
|
|
|
uint32_t mColumn;
|
|
|
|
nsString mRejectValue;
|
|
|
|
|
|
|
|
~WaitUntilHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
WaitUntilHandler(WorkerPrivate* aWorkerPrivate, JSContext* aCx)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate)
|
2017-03-25 02:56:48 +03:00
|
|
|
, mScope(mWorkerPrivate->ServiceWorkerScope())
|
2015-11-20 00:15:17 +03:00
|
|
|
, mLine(0)
|
|
|
|
, mColumn(0)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
// Save the location of the waitUntil() call itself as a fallback
|
|
|
|
// in case the rejection value does not contain any location info.
|
|
|
|
nsJSUtils::GetCallingLocation(aCx, mSourceSpec, &mLine, &mColumn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override
|
|
|
|
{
|
|
|
|
// do nothing, we are only here to report errors
|
|
|
|
}
|
|
|
|
|
|
|
|
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
nsCString spec;
|
|
|
|
uint32_t line = 0;
|
|
|
|
uint32_t column = 0;
|
2017-08-11 04:04:56 +03:00
|
|
|
nsContentUtils::ExtractErrorValues(aCx, aValue, spec, &line, &column,
|
|
|
|
mRejectValue);
|
2015-11-20 00:15:17 +03:00
|
|
|
|
|
|
|
// only use the extracted location if we found one
|
|
|
|
if (!spec.IsEmpty()) {
|
|
|
|
mSourceSpec = spec;
|
|
|
|
mLine = line;
|
|
|
|
mColumn = column;
|
|
|
|
}
|
|
|
|
|
2016-09-14 06:14:02 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(
|
2017-06-12 22:34:10 +03:00
|
|
|
NewRunnableMethod("WaitUntilHandler::ReportOnMainThread",
|
|
|
|
this, &WaitUntilHandler::ReportOnMainThread)));
|
2015-11-20 00:15:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReportOnMainThread()
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-11-20 00:15:17 +03:00
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
2017-01-13 01:00:36 +03:00
|
|
|
if (!swm) {
|
|
|
|
// browser shutdown
|
|
|
|
return;
|
|
|
|
}
|
2015-11-20 00:15:17 +03:00
|
|
|
|
|
|
|
// TODO: Make the error message a localized string. (bug 1222720)
|
|
|
|
nsString message;
|
|
|
|
message.AppendLiteral("Service worker event waitUntil() was passed a "
|
|
|
|
"promise that rejected with '");
|
|
|
|
message.Append(mRejectValue);
|
|
|
|
message.AppendLiteral("'.");
|
|
|
|
|
|
|
|
// Note, there is a corner case where this won't report to the window
|
|
|
|
// that triggered the error. Consider a navigation fetch event that
|
|
|
|
// rejects waitUntil() without holding respondWith() open. In this case
|
|
|
|
// there is no controlling document yet, the window did call .register()
|
|
|
|
// because there is no documeny yet, and the navigation is no longer
|
|
|
|
// being intercepted.
|
|
|
|
|
|
|
|
swm->ReportToAllClients(mScope, message, NS_ConvertUTF8toUTF16(mSourceSpec),
|
|
|
|
EmptyString(), mLine, mColumn,
|
|
|
|
nsIScriptError::errorFlag);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS0(WaitUntilHandler)
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-10-25 03:24:45 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(FetchEvent, ExtendableEvent)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(FetchEvent, ExtendableEvent)
|
2015-02-19 04:34:29 +03:00
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FetchEvent)
|
2015-10-25 03:24:45 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(ExtendableEvent)
|
2015-02-19 04:34:29 +03:00
|
|
|
|
2015-10-27 05:51:30 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(FetchEvent, ExtendableEvent, mRequest)
|
2015-02-19 04:34:29 +03:00
|
|
|
|
2014-11-06 18:57:57 +03:00
|
|
|
ExtendableEvent::ExtendableEvent(EventTarget* aOwner)
|
2014-05-14 01:49:36 +04:00
|
|
|
: Event(aOwner, nullptr, nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
bool
|
|
|
|
ExtendableEvent::WaitOnPromise(Promise& aPromise)
|
|
|
|
{
|
2016-12-22 20:27:00 +03:00
|
|
|
if (!mExtensionsHandler) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-21 05:14:53 +03:00
|
|
|
return mExtensionsHandler->WaitOnPromise(aPromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExtendableEvent::SetKeepAliveHandler(ExtensionsHandler* aExtensionsHandler)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mExtensionsHandler);
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
mExtensionsHandler = aExtensionsHandler;
|
|
|
|
}
|
|
|
|
|
2014-05-14 01:49:36 +04:00
|
|
|
void
|
2015-11-20 00:15:17 +03:00
|
|
|
ExtendableEvent::WaitUntil(JSContext* aCx, Promise& aPromise, ErrorResult& aRv)
|
2014-05-14 01:49:36 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
if (!WaitOnPromise(aPromise)) {
|
2015-08-19 02:28:04 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-20 00:15:17 +03:00
|
|
|
// Append our handler to each waitUntil promise separately so we
|
|
|
|
// can record the location in script where waitUntil was called.
|
|
|
|
RefPtr<WaitUntilHandler> handler =
|
|
|
|
new WaitUntilHandler(GetCurrentThreadWorkerPrivate(), aCx);
|
|
|
|
aPromise.AppendNativeHandler(handler);
|
2014-05-14 01:49:36 +04:00
|
|
|
}
|
|
|
|
|
2014-11-06 18:57:57 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(ExtendableEvent, Event)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ExtendableEvent, Event)
|
2014-05-14 01:49:36 +04:00
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ExtendableEvent)
|
2014-05-14 01:49:36 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(Event)
|
|
|
|
|
2015-09-17 15:10:42 +03:00
|
|
|
namespace {
|
|
|
|
nsresult
|
|
|
|
ExtractBytesFromUSVString(const nsAString& aStr, nsTArray<uint8_t>& aBytes)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aBytes.IsEmpty());
|
2017-04-27 13:27:03 +03:00
|
|
|
auto encoder = UTF_8_ENCODING->NewEncoder();
|
|
|
|
CheckedInt<size_t> needed =
|
|
|
|
encoder->MaxBufferLengthFromUTF16WithoutReplacement(aStr.Length());
|
|
|
|
if (NS_WARN_IF(!needed.isValid() ||
|
|
|
|
!aBytes.SetLength(needed.value(), fallible))) {
|
2015-09-17 15:10:42 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
uint32_t result;
|
|
|
|
size_t read;
|
|
|
|
size_t written;
|
|
|
|
Tie(result, read, written) =
|
|
|
|
encoder->EncodeFromUTF16WithoutReplacement(aStr, aBytes, true);
|
|
|
|
MOZ_ASSERT(result == kInputEmpty);
|
|
|
|
MOZ_ASSERT(read == aStr.Length());
|
|
|
|
aBytes.TruncateLength(written);
|
2015-09-17 15:10:42 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ExtractBytesFromData(const OwningArrayBufferViewOrArrayBufferOrUSVString& aDataInit, nsTArray<uint8_t>& aBytes)
|
2015-04-11 06:19:28 +03:00
|
|
|
{
|
2015-09-17 15:10:42 +03:00
|
|
|
if (aDataInit.IsArrayBufferView()) {
|
|
|
|
const ArrayBufferView& view = aDataInit.GetAsArrayBufferView();
|
2016-04-23 06:54:22 +03:00
|
|
|
if (NS_WARN_IF(!PushUtil::CopyArrayBufferViewToArray(view, aBytes))) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aDataInit.IsArrayBuffer()) {
|
2015-09-17 15:10:42 +03:00
|
|
|
const ArrayBuffer& buffer = aDataInit.GetAsArrayBuffer();
|
2016-04-23 06:54:22 +03:00
|
|
|
if (NS_WARN_IF(!PushUtil::CopyArrayBufferToArray(buffer, aBytes))) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aDataInit.IsUSVString()) {
|
2015-09-17 15:10:42 +03:00
|
|
|
return ExtractBytesFromUSVString(aDataInit.GetAsUSVString(), aBytes);
|
|
|
|
}
|
|
|
|
NS_NOTREACHED("Unexpected push message data");
|
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-11 06:19:28 +03:00
|
|
|
}
|
2015-09-17 15:10:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PushMessageData::PushMessageData(nsISupports* aOwner,
|
2016-04-23 06:54:22 +03:00
|
|
|
nsTArray<uint8_t>&& aBytes)
|
|
|
|
: mOwner(aOwner), mBytes(Move(aBytes)) {}
|
2015-04-11 06:19:28 +03:00
|
|
|
|
|
|
|
PushMessageData::~PushMessageData()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-17 15:10:42 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(PushMessageData, mOwner)
|
2015-04-11 06:19:28 +03:00
|
|
|
|
2015-07-18 01:17:14 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PushMessageData)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PushMessageData)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PushMessageData)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
2015-04-11 06:19:28 +03:00
|
|
|
|
2016-03-16 23:44:24 +03:00
|
|
|
JSObject*
|
|
|
|
PushMessageData::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
return mozilla::dom::PushMessageDataBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
2015-04-11 06:19:28 +03:00
|
|
|
void
|
2015-09-17 15:10:42 +03:00
|
|
|
PushMessageData::Json(JSContext* cx, JS::MutableHandle<JS::Value> aRetval,
|
|
|
|
ErrorResult& aRv)
|
2015-04-11 06:19:28 +03:00
|
|
|
{
|
2015-09-17 15:10:42 +03:00
|
|
|
if (NS_FAILED(EnsureDecodedText())) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_UNKNOWN_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2016-02-28 00:54:11 +03:00
|
|
|
BodyUtil::ConsumeJson(cx, aRetval, mDecodedText, aRv);
|
2015-04-11 06:19:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PushMessageData::Text(nsAString& aData)
|
|
|
|
{
|
2015-09-17 15:10:42 +03:00
|
|
|
if (NS_SUCCEEDED(EnsureDecodedText())) {
|
|
|
|
aData = mDecodedText;
|
|
|
|
}
|
2015-04-11 06:19:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-17 15:10:42 +03:00
|
|
|
PushMessageData::ArrayBuffer(JSContext* cx,
|
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
|
|
|
ErrorResult& aRv)
|
2015-04-11 06:19:28 +03:00
|
|
|
{
|
2015-09-17 15:10:42 +03:00
|
|
|
uint8_t* data = GetContentsCopy();
|
|
|
|
if (data) {
|
2016-02-28 00:54:11 +03:00
|
|
|
BodyUtil::ConsumeArrayBuffer(cx, aRetval, mBytes.Length(), data, aRv);
|
2015-09-17 15:10:42 +03:00
|
|
|
}
|
2015-04-11 06:19:28 +03:00
|
|
|
}
|
|
|
|
|
2015-09-17 15:10:42 +03:00
|
|
|
already_AddRefed<mozilla::dom::Blob>
|
|
|
|
PushMessageData::Blob(ErrorResult& aRv)
|
2015-04-11 06:19:28 +03:00
|
|
|
{
|
2015-09-17 15:10:42 +03:00
|
|
|
uint8_t* data = GetContentsCopy();
|
|
|
|
if (data) {
|
2016-02-28 00:54:11 +03:00
|
|
|
RefPtr<mozilla::dom::Blob> blob = BodyUtil::ConsumeBlob(
|
2015-09-17 15:10:42 +03:00
|
|
|
mOwner, EmptyString(), mBytes.Length(), data, aRv);
|
|
|
|
if (blob) {
|
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
}
|
2015-04-11 06:19:28 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-08-16 05:12:30 +03:00
|
|
|
nsresult
|
2015-09-17 15:10:42 +03:00
|
|
|
PushMessageData::EnsureDecodedText()
|
|
|
|
{
|
|
|
|
if (mBytes.IsEmpty() || !mDecodedText.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-02-28 00:54:11 +03:00
|
|
|
nsresult rv = BodyUtil::ConsumeText(
|
2015-09-17 15:10:42 +03:00
|
|
|
mBytes.Length(),
|
|
|
|
reinterpret_cast<uint8_t*>(mBytes.Elements()),
|
|
|
|
mDecodedText
|
|
|
|
);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mDecodedText.Truncate();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t*
|
|
|
|
PushMessageData::GetContentsCopy()
|
|
|
|
{
|
|
|
|
uint32_t length = mBytes.Length();
|
|
|
|
void* data = malloc(length);
|
|
|
|
if (!data) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
memcpy(data, mBytes.Elements(), length);
|
|
|
|
return reinterpret_cast<uint8_t*>(data);
|
|
|
|
}
|
|
|
|
|
2015-04-11 06:19:28 +03:00
|
|
|
PushEvent::PushEvent(EventTarget* aOwner)
|
|
|
|
: ExtendableEvent(aOwner)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-17 15:10:42 +03:00
|
|
|
already_AddRefed<PushEvent>
|
|
|
|
PushEvent::Constructor(mozilla::dom::EventTarget* aOwner,
|
|
|
|
const nsAString& aType,
|
|
|
|
const PushEventInit& aOptions,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PushEvent> e = new PushEvent(aOwner);
|
2015-09-17 15:10:42 +03:00
|
|
|
bool trusted = e->Init(aOwner);
|
|
|
|
e->InitEvent(aType, aOptions.mBubbles, aOptions.mCancelable);
|
|
|
|
e->SetTrusted(trusted);
|
2016-08-31 06:16:11 +03:00
|
|
|
e->SetComposed(aOptions.mComposed);
|
2015-09-17 15:10:42 +03:00
|
|
|
if(aOptions.mData.WasPassed()){
|
|
|
|
nsTArray<uint8_t> bytes;
|
|
|
|
nsresult rv = ExtractBytesFromData(aOptions.mData.Value(), bytes);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-04-23 06:54:22 +03:00
|
|
|
e->mData = new PushMessageData(aOwner, Move(bytes));
|
2015-09-17 15:10:42 +03:00
|
|
|
}
|
|
|
|
return e.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(PushEvent, ExtendableEvent)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(PushEvent, ExtendableEvent)
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PushEvent)
|
2015-09-17 15:10:42 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(ExtendableEvent)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(PushEvent, ExtendableEvent, mData)
|
|
|
|
|
2016-03-16 23:44:24 +03:00
|
|
|
JSObject*
|
|
|
|
PushEvent::WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
return mozilla::dom::PushEventBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
2015-11-10 09:31:41 +03:00
|
|
|
ExtendableMessageEvent::ExtendableMessageEvent(EventTarget* aOwner)
|
|
|
|
: ExtendableEvent(aOwner)
|
|
|
|
, mData(JS::UndefinedValue())
|
|
|
|
{
|
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExtendableMessageEvent::~ExtendableMessageEvent()
|
|
|
|
{
|
|
|
|
mData.setUndefined();
|
|
|
|
DropJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExtendableMessageEvent::GetData(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aData,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
aData.set(mData);
|
|
|
|
if (!JS_WrapValue(aCx, aData)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExtendableMessageEvent::GetSource(Nullable<OwningClientOrServiceWorkerOrMessagePort>& aValue) const
|
|
|
|
{
|
|
|
|
if (mClient) {
|
|
|
|
aValue.SetValue().SetAsClient() = mClient;
|
|
|
|
} else if (mServiceWorker) {
|
|
|
|
aValue.SetValue().SetAsServiceWorker() = mServiceWorker;
|
|
|
|
} else if (mMessagePort) {
|
|
|
|
aValue.SetValue().SetAsMessagePort() = mMessagePort;
|
|
|
|
} else {
|
2017-03-17 04:25:42 +03:00
|
|
|
// nullptr source is possible for manually constructed event
|
|
|
|
aValue.SetNull();
|
2015-11-10 09:31:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<ExtendableMessageEvent>
|
|
|
|
ExtendableMessageEvent::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aType,
|
|
|
|
const ExtendableMessageEventInit& aOptions,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
return Constructor(t, aType, aOptions, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<ExtendableMessageEvent>
|
|
|
|
ExtendableMessageEvent::Constructor(mozilla::dom::EventTarget* aEventTarget,
|
|
|
|
const nsAString& aType,
|
|
|
|
const ExtendableMessageEventInit& aOptions,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
RefPtr<ExtendableMessageEvent> event = new ExtendableMessageEvent(aEventTarget);
|
|
|
|
|
2015-11-13 04:23:59 +03:00
|
|
|
event->InitEvent(aType, aOptions.mBubbles, aOptions.mCancelable);
|
2015-11-10 09:31:41 +03:00
|
|
|
bool trusted = event->Init(aEventTarget);
|
|
|
|
event->SetTrusted(trusted);
|
|
|
|
|
|
|
|
event->mData = aOptions.mData;
|
|
|
|
event->mOrigin = aOptions.mOrigin;
|
|
|
|
event->mLastEventId = aOptions.mLastEventId;
|
|
|
|
|
2016-10-25 19:23:06 +03:00
|
|
|
if (!aOptions.mSource.IsNull()) {
|
|
|
|
if (aOptions.mSource.Value().IsClient()) {
|
|
|
|
event->mClient = aOptions.mSource.Value().GetAsClient();
|
|
|
|
} else if (aOptions.mSource.Value().IsServiceWorker()){
|
|
|
|
event->mServiceWorker = aOptions.mSource.Value().GetAsServiceWorker();
|
|
|
|
} else if (aOptions.mSource.Value().IsMessagePort()){
|
|
|
|
event->mMessagePort = aOptions.mSource.Value().GetAsMessagePort();
|
2015-11-10 09:31:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 19:23:06 +03:00
|
|
|
event->mPorts.AppendElements(aOptions.mPorts);
|
2015-11-10 09:31:41 +03:00
|
|
|
return event.forget();
|
|
|
|
}
|
|
|
|
|
2016-10-13 16:19:24 +03:00
|
|
|
void
|
2016-10-25 19:23:06 +03:00
|
|
|
ExtendableMessageEvent::GetPorts(nsTArray<RefPtr<MessagePort>>& aPorts)
|
2015-11-10 09:31:41 +03:00
|
|
|
{
|
2016-10-25 19:23:06 +03:00
|
|
|
aPorts = mPorts;
|
2015-11-10 09:31:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(ExtendableMessageEvent)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(ExtendableMessageEvent, Event)
|
|
|
|
tmp->mData.setUndefined();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mClient)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mServiceWorker)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mMessagePort)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPorts)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ExtendableMessageEvent, Event)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mClient)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mServiceWorker)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMessagePort)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPorts)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(ExtendableMessageEvent, Event)
|
2016-02-22 21:11:02 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mData)
|
2015-11-10 09:31:41 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ExtendableMessageEvent)
|
2015-11-10 09:31:41 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(Event)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ExtendableMessageEvent, Event)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ExtendableMessageEvent, Event)
|
|
|
|
|
2018-01-27 00:08:59 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|