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: */
|
2012-05-21 15:12:37 +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/. */
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
|
|
#include "ScriptLoader.h"
|
|
|
|
|
|
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2016-03-17 13:03:05 +03:00
|
|
|
|
#include "nsIDocShell.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
#include "nsIHttpChannel.h"
|
2015-07-02 17:30:06 +03:00
|
|
|
|
#include "nsIHttpChannelInternal.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
|
#include "nsIInputStreamPump.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
#include "nsIIOService.h"
|
2018-05-03 18:42:58 +03:00
|
|
|
|
#include "nsIOService.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
#include "nsIProtocolHandler.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
|
#include "nsIScriptError.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
|
#include "nsIStreamLoader.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
|
#include "nsIStreamListenerTee.h"
|
|
|
|
|
#include "nsIThreadRetargetableRequest.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
#include "nsIURI.h"
|
|
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
2015-10-15 06:05:00 +03:00
|
|
|
|
#include "jsfriendapi.h"
|
2018-08-25 06:51:49 +03:00
|
|
|
|
#include "js/CompilationAndEvaluation.h"
|
2018-08-25 05:01:58 +03:00
|
|
|
|
#include "js/SourceBufferHolder.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
|
#include "nsError.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
#include "nsDocShellCID.h"
|
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
|
#include "nsIPipe.h"
|
|
|
|
|
#include "nsIOutputStream.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
|
#include "nsPrintfCString.h"
|
2013-09-23 21:25:00 +04:00
|
|
|
|
#include "nsString.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
|
#include "nsStreamUtils.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
#include "nsXPCOM.h"
|
2013-03-08 02:27:21 +04:00
|
|
|
|
#include "xpcpublic.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2014-12-12 19:06:00 +03:00
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
|
#include "mozilla/LoadContext.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
|
#include "mozilla/Maybe.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
2018-06-02 16:51:42 +03:00
|
|
|
|
#include "mozilla/dom/BlobURLProtocolHandler.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
|
#include "mozilla/dom/CacheBinding.h"
|
2015-06-05 07:39:34 +03:00
|
|
|
|
#include "mozilla/dom/cache/CacheTypes.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
|
#include "mozilla/dom/cache/Cache.h"
|
|
|
|
|
#include "mozilla/dom/cache/CacheStorage.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
|
#include "mozilla/dom/ChannelInfo.h"
|
2017-11-21 23:13:04 +03:00
|
|
|
|
#include "mozilla/dom/ClientChannelHelper.h"
|
|
|
|
|
#include "mozilla/dom/ClientInfo.h"
|
2013-09-09 07:29:21 +04:00
|
|
|
|
#include "mozilla/dom/Exceptions.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
|
#include "mozilla/dom/InternalResponse.h"
|
2016-06-30 07:31:59 +03:00
|
|
|
|
#include "mozilla/dom/nsCSPService.h"
|
|
|
|
|
#include "mozilla/dom/nsCSPUtils.h"
|
2018-01-24 19:17:31 +03:00
|
|
|
|
#include "mozilla/dom/PerformanceStorage.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
|
#include "mozilla/dom/Response.h"
|
2017-05-08 09:24:22 +03:00
|
|
|
|
#include "mozilla/dom/ScriptLoader.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2016-10-04 03:36:00 +03:00
|
|
|
|
#include "mozilla/dom/SRILogHelper.h"
|
2018-04-02 17:50:21 +03:00
|
|
|
|
#include "mozilla/dom/ServiceWorkerBinding.h"
|
2018-10-29 18:08:24 +03:00
|
|
|
|
#include "mozilla/dom/ServiceWorkerManager.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
|
#include "mozilla/UniquePtr.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
#include "Principal.h"
|
2016-06-23 11:53:14 +03:00
|
|
|
|
#include "WorkerHolder.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
#include "WorkerPrivate.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
|
#include "WorkerRunnable.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
|
#include "WorkerScope.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
|
|
#define MAX_CONCURRENT_SCRIPTS 1000
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
using mozilla::dom::cache::Cache;
|
|
|
|
|
using mozilla::dom::cache::CacheStorage;
|
2015-07-02 15:54:00 +03:00
|
|
|
|
using mozilla::ipc::PrincipalInfo;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
2018-01-31 10:22:03 +03:00
|
|
|
|
namespace mozilla {
|
|
|
|
|
namespace dom {
|
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
|
namespace {
|
2013-03-08 02:27:21 +04:00
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
nsIURI*
|
|
|
|
|
GetBaseURI(bool aIsMainScript, WorkerPrivate* aWorkerPrivate)
|
|
|
|
|
{
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
nsIURI* baseURI;
|
|
|
|
|
WorkerPrivate* parentWorker = aWorkerPrivate->GetParent();
|
|
|
|
|
if (aIsMainScript) {
|
|
|
|
|
if (parentWorker) {
|
|
|
|
|
baseURI = parentWorker->GetBaseURI();
|
|
|
|
|
NS_ASSERTION(baseURI, "Should have been set already!");
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// May be null.
|
|
|
|
|
baseURI = aWorkerPrivate->GetBaseURI();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
baseURI = aWorkerPrivate->GetBaseURI();
|
|
|
|
|
NS_ASSERTION(baseURI, "Should have been set already!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return baseURI;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-08 02:27:21 +04:00
|
|
|
|
nsresult
|
|
|
|
|
ChannelFromScriptURL(nsIPrincipal* principal,
|
|
|
|
|
nsIURI* baseURI,
|
|
|
|
|
nsIDocument* parentDoc,
|
2018-01-24 19:17:31 +03:00
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
2013-03-08 02:27:21 +04:00
|
|
|
|
nsILoadGroup* loadGroup,
|
|
|
|
|
nsIIOService* ios,
|
|
|
|
|
nsIScriptSecurityManager* secMan,
|
2013-06-05 18:04:23 +04:00
|
|
|
|
const nsAString& aScriptURL,
|
2018-01-23 18:38:54 +03:00
|
|
|
|
const Maybe<ClientInfo>& aClientInfo,
|
|
|
|
|
const Maybe<ServiceWorkerDescriptor>& aController,
|
2015-03-17 13:15:19 +03:00
|
|
|
|
bool aIsMainScript,
|
|
|
|
|
WorkerScriptType aWorkerScriptType,
|
2017-02-15 17:55:58 +03:00
|
|
|
|
nsContentPolicyType aMainScriptContentPolicyType,
|
2015-10-06 16:37:07 +03:00
|
|
|
|
nsLoadFlags aLoadFlags,
|
2016-04-28 10:44:08 +03:00
|
|
|
|
bool aDefaultURIEncoding,
|
2013-06-05 18:04:23 +04:00
|
|
|
|
nsIChannel** aChannel)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
2013-03-08 02:27:21 +04:00
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
|
nsresult rv;
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2016-04-28 10:44:08 +03:00
|
|
|
|
|
|
|
|
|
if (aDefaultURIEncoding) {
|
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), aScriptURL, nullptr, baseURI);
|
|
|
|
|
} else {
|
|
|
|
|
rv = nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(uri),
|
|
|
|
|
aScriptURL, parentDoc,
|
|
|
|
|
baseURI);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
|
}
|
2013-03-08 02:27:21 +04:00
|
|
|
|
|
2015-11-11 08:16:12 +03:00
|
|
|
|
// If we have the document, use it. Unfortunately, for dedicated workers
|
|
|
|
|
// 'parentDoc' ends up being the parent document, which is not the document
|
|
|
|
|
// that we want to use. So make sure to avoid using 'parentDoc' in that
|
|
|
|
|
// situation.
|
|
|
|
|
if (parentDoc && parentDoc->NodePrincipal() != principal) {
|
|
|
|
|
parentDoc = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-17 01:41:03 +03:00
|
|
|
|
aLoadFlags |= nsIChannel::LOAD_CLASSIFY_URI;
|
|
|
|
|
uint32_t secFlags = aIsMainScript ? nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED
|
|
|
|
|
: nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS;
|
2013-03-08 02:27:21 +04:00
|
|
|
|
|
2018-05-03 18:42:58 +03:00
|
|
|
|
bool inheritAttrs = nsContentUtils::ChannelShouldInheritPrincipal(
|
|
|
|
|
principal, uri, true /* aInheritForAboutBlank */, false /* aForceInherit */);
|
|
|
|
|
|
|
|
|
|
bool isData = false;
|
|
|
|
|
rv = uri->SchemeIs("data", &isData);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
2018-05-30 22:21:17 +03:00
|
|
|
|
bool isURIUniqueOrigin = net::nsIOService::IsDataURIUniqueOpaqueOrigin() && isData;
|
2018-05-03 18:42:58 +03:00
|
|
|
|
if (inheritAttrs && !isURIUniqueOrigin) {
|
|
|
|
|
secFlags |= nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
|
if (aWorkerScriptType == DebuggerScript) {
|
2015-11-17 01:41:03 +03:00
|
|
|
|
// A DebuggerScript needs to be a local resource like chrome: or resource:
|
|
|
|
|
bool isUIResource = false;
|
|
|
|
|
rv = NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_IS_UI_RESOURCE,
|
|
|
|
|
&isUIResource);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
2015-11-16 22:13:43 +03:00
|
|
|
|
|
2015-11-17 01:41:03 +03:00
|
|
|
|
if (!isUIResource) {
|
2015-03-17 13:15:19 +03:00
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
}
|
2015-11-17 01:41:03 +03:00
|
|
|
|
|
|
|
|
|
secFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
|
2015-11-16 19:57:29 +03:00
|
|
|
|
}
|
2015-11-15 17:56:45 +03:00
|
|
|
|
|
2015-11-17 01:41:03 +03:00
|
|
|
|
// Note: this is for backwards compatibility and goes against spec.
|
|
|
|
|
// We should find a better solution.
|
2018-05-03 18:42:58 +03:00
|
|
|
|
if (aIsMainScript && isData) {
|
2015-11-17 01:41:03 +03:00
|
|
|
|
secFlags = nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL;
|
|
|
|
|
}
|
2015-11-16 22:13:43 +03:00
|
|
|
|
|
2017-02-15 17:55:58 +03:00
|
|
|
|
nsContentPolicyType contentPolicyType =
|
|
|
|
|
aIsMainScript ? aMainScriptContentPolicyType
|
|
|
|
|
: nsIContentPolicy::TYPE_INTERNAL_WORKER_IMPORT_SCRIPTS;
|
|
|
|
|
|
2018-04-02 17:50:21 +03:00
|
|
|
|
// The main service worker script should never be loaded over the network
|
|
|
|
|
// in this path. It should always be offlined by ServiceWorkerScriptCache.
|
|
|
|
|
// We assert here since this error should also be caught by the runtime
|
|
|
|
|
// check in CacheScriptLoader.
|
|
|
|
|
//
|
|
|
|
|
// Note, if we ever allow service worker scripts to be loaded from network
|
|
|
|
|
// here we need to configure the channel properly. For example, it must
|
|
|
|
|
// not allow redirects.
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(contentPolicyType !=
|
|
|
|
|
nsIContentPolicy::TYPE_INTERNAL_SERVICE_WORKER);
|
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2015-11-11 08:16:12 +03:00
|
|
|
|
// If we have the document, use it. Unfortunately, for dedicated workers
|
|
|
|
|
// 'parentDoc' ends up being the parent document, which is not the document
|
|
|
|
|
// that we want to use. So make sure to avoid using 'parentDoc' in that
|
|
|
|
|
// situation.
|
|
|
|
|
if (parentDoc && parentDoc->NodePrincipal() == principal) {
|
2014-09-21 20:39:57 +04:00
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
|
uri,
|
|
|
|
|
parentDoc,
|
2015-11-17 01:41:03 +03:00
|
|
|
|
secFlags,
|
2017-02-15 17:55:58 +03:00
|
|
|
|
contentPolicyType,
|
2018-01-24 19:17:31 +03:00
|
|
|
|
nullptr, // aPerformanceStorage
|
2014-09-21 20:39:57 +04:00
|
|
|
|
loadGroup,
|
|
|
|
|
nullptr, // aCallbacks
|
2015-10-06 16:37:07 +03:00
|
|
|
|
aLoadFlags,
|
2014-09-21 20:39:57 +04:00
|
|
|
|
ios);
|
2018-11-06 15:36:13 +03:00
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SECURITY_ERR);
|
2014-09-21 20:39:57 +04:00
|
|
|
|
} else {
|
2014-12-12 19:06:00 +03:00
|
|
|
|
// We must have a loadGroup with a load context for the principal to
|
|
|
|
|
// traverse the channel correctly.
|
|
|
|
|
MOZ_ASSERT(loadGroup);
|
|
|
|
|
MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(loadGroup, principal));
|
|
|
|
|
|
2018-01-24 19:17:31 +03:00
|
|
|
|
RefPtr<PerformanceStorage> performanceStorage;
|
2018-10-23 09:17:13 +03:00
|
|
|
|
nsCOMPtr<nsICSPEventListener> cspEventListener;
|
2018-01-24 19:17:31 +03:00
|
|
|
|
if (aWorkerPrivate && !aIsMainScript) {
|
|
|
|
|
performanceStorage = aWorkerPrivate->GetPerformanceStorage();
|
2018-10-23 09:17:13 +03:00
|
|
|
|
cspEventListener = aWorkerPrivate->CSPEventListener();
|
2018-01-24 19:17:31 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-23 18:38:54 +03:00
|
|
|
|
if (aClientInfo.isSome()) {
|
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
|
uri,
|
|
|
|
|
principal,
|
|
|
|
|
aClientInfo.ref(),
|
|
|
|
|
aController,
|
|
|
|
|
secFlags,
|
|
|
|
|
contentPolicyType,
|
2018-01-24 19:17:31 +03:00
|
|
|
|
performanceStorage,
|
2018-01-23 18:38:54 +03:00
|
|
|
|
loadGroup,
|
|
|
|
|
nullptr, // aCallbacks
|
|
|
|
|
aLoadFlags,
|
|
|
|
|
ios);
|
|
|
|
|
} else {
|
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
|
uri,
|
|
|
|
|
principal,
|
|
|
|
|
secFlags,
|
|
|
|
|
contentPolicyType,
|
2018-01-24 19:17:31 +03:00
|
|
|
|
performanceStorage,
|
2018-01-23 18:38:54 +03:00
|
|
|
|
loadGroup,
|
|
|
|
|
nullptr, // aCallbacks
|
|
|
|
|
aLoadFlags,
|
|
|
|
|
ios);
|
|
|
|
|
}
|
2014-09-21 20:39:57 +04:00
|
|
|
|
|
2018-11-06 15:36:13 +03:00
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SECURITY_ERR);
|
2018-10-23 09:17:13 +03:00
|
|
|
|
|
|
|
|
|
if (cspEventListener) {
|
|
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = channel->GetLoadInfo();
|
|
|
|
|
if (NS_WARN_IF(!loadInfo)) {
|
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rv = loadInfo->SetCspEventListener(cspEventListener);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
2015-06-05 02:20:13 +03:00
|
|
|
|
if (nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(channel)) {
|
2016-11-14 10:15:32 +03:00
|
|
|
|
mozilla::net::ReferrerPolicy referrerPolicy = parentDoc ?
|
2017-01-05 06:30:07 +03:00
|
|
|
|
parentDoc->GetReferrerPolicy() : mozilla::net::RP_Unset;
|
2015-06-05 02:20:13 +03:00
|
|
|
|
rv = nsContentUtils::SetFetchReferrerURIWithPolicy(principal, parentDoc,
|
2016-11-14 10:15:32 +03:00
|
|
|
|
httpChannel, referrerPolicy);
|
2015-06-05 02:20:13 +03:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
|
channel.forget(aChannel);
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
|
|
struct ScriptLoadInfo
|
|
|
|
|
{
|
|
|
|
|
ScriptLoadInfo()
|
2014-04-25 18:11:57 +04:00
|
|
|
|
: mScriptTextBuf(nullptr)
|
|
|
|
|
, mScriptTextLength(0)
|
2015-03-19 21:41:42 +03:00
|
|
|
|
, mLoadResult(NS_ERROR_NOT_INITIALIZED)
|
|
|
|
|
, mLoadingFinished(false)
|
|
|
|
|
, mExecutionScheduled(false)
|
2014-04-25 18:11:57 +04:00
|
|
|
|
, mExecutionResult(false)
|
2015-03-19 21:41:42 +03:00
|
|
|
|
, mCacheStatus(Uncached)
|
2017-01-04 12:09:00 +03:00
|
|
|
|
, mLoadFlags(nsIRequest::LOAD_NORMAL)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
{ }
|
|
|
|
|
|
2014-04-25 18:11:57 +04:00
|
|
|
|
~ScriptLoadInfo()
|
|
|
|
|
{
|
|
|
|
|
if (mScriptTextBuf) {
|
|
|
|
|
js_free(mScriptTextBuf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
nsString mURL;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
|
|
|
|
// This full URL string is populated only if this object is used in a
|
|
|
|
|
// ServiceWorker.
|
|
|
|
|
nsString mFullURL;
|
|
|
|
|
|
|
|
|
|
// This promise is set only when the script is for a ServiceWorker but
|
|
|
|
|
// it's not in the cache yet. The promise is resolved when the full body is
|
|
|
|
|
// stored into the cache. mCachePromise will be set to nullptr after
|
|
|
|
|
// resolution.
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<Promise> mCachePromise;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
2016-02-23 01:13:38 +03:00
|
|
|
|
// The reader stream the cache entry should be filled from, for those cases
|
|
|
|
|
// when we're going to have an mCachePromise.
|
|
|
|
|
nsCOMPtr<nsIInputStream> mCacheReadStream;
|
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
2017-11-21 23:13:04 +03:00
|
|
|
|
Maybe<ClientInfo> mReservedClientInfo;
|
2014-07-22 08:43:21 +04:00
|
|
|
|
char16_t* mScriptTextBuf;
|
2014-04-25 18:11:57 +04:00
|
|
|
|
size_t mScriptTextLength;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
|
|
nsresult mLoadResult;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
bool mLoadingFinished;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
bool mExecutionScheduled;
|
|
|
|
|
bool mExecutionResult;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
|
|
|
|
enum CacheStatus {
|
|
|
|
|
// By default a normal script is just loaded from the network. But for
|
|
|
|
|
// ServiceWorkers, we have to check if the cache contains the script and
|
|
|
|
|
// load it from the cache.
|
|
|
|
|
Uncached,
|
|
|
|
|
|
|
|
|
|
WritingToCache,
|
|
|
|
|
|
|
|
|
|
ReadingFromCache,
|
|
|
|
|
|
|
|
|
|
// This script has been loaded from the ServiceWorker cache.
|
|
|
|
|
Cached,
|
|
|
|
|
|
|
|
|
|
// This script must be stored in the ServiceWorker cache.
|
|
|
|
|
ToBeCached,
|
|
|
|
|
|
|
|
|
|
// Something went wrong or the worker went away.
|
|
|
|
|
Cancel
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CacheStatus mCacheStatus;
|
|
|
|
|
|
2017-01-04 12:09:00 +03:00
|
|
|
|
nsLoadFlags mLoadFlags;
|
|
|
|
|
|
2015-07-14 18:08:38 +03:00
|
|
|
|
Maybe<bool> mMutedErrorFlag;
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
bool Finished() const
|
|
|
|
|
{
|
|
|
|
|
return mLoadingFinished && !mCachePromise && !mChannel;
|
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
};
|
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
|
class ScriptLoaderRunnable;
|
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
|
class ScriptExecutorRunnable final : public MainThreadWorkerSyncRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
|
{
|
|
|
|
|
ScriptLoaderRunnable& mScriptLoader;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
bool mIsWorkerScript;
|
2012-08-22 19:56:38 +04:00
|
|
|
|
uint32_t mFirstIndex;
|
|
|
|
|
uint32_t mLastIndex;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ScriptExecutorRunnable(ScriptLoaderRunnable& aScriptLoader,
|
2015-03-19 21:41:42 +03:00
|
|
|
|
nsIEventTarget* aSyncLoopTarget,
|
|
|
|
|
bool aIsWorkerScript,
|
|
|
|
|
uint32_t aFirstIndex,
|
2012-08-22 19:56:38 +04:00
|
|
|
|
uint32_t aLastIndex);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
|
private:
|
|
|
|
|
~ScriptExecutorRunnable()
|
|
|
|
|
{ }
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
|
IsDebuggerRunnable() const override;
|
2015-03-17 13:15:19 +03:00
|
|
|
|
|
2016-03-02 00:52:26 +03:00
|
|
|
|
virtual bool
|
|
|
|
|
PreRun(WorkerPrivate* aWorkerPrivate) override;
|
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
|
virtual void
|
|
|
|
|
PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, bool aRunResult)
|
2015-03-21 19:28:04 +03:00
|
|
|
|
override;
|
2014-01-11 04:37:47 +04:00
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
|
nsresult
|
|
|
|
|
Cancel() override;
|
2014-01-11 04:37:47 +04:00
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ShutdownScriptLoader(JSContext* aCx,
|
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
2015-10-15 15:06:55 +03:00
|
|
|
|
bool aResult,
|
|
|
|
|
bool aMutedError);
|
2015-10-15 06:05:00 +03:00
|
|
|
|
|
|
|
|
|
void LogExceptionToConsole(JSContext* aCx,
|
|
|
|
|
WorkerPrivate* WorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
};
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
class CacheScriptLoader;
|
|
|
|
|
|
|
|
|
|
class CacheCreator final : public PromiseNativeHandler
|
|
|
|
|
{
|
|
|
|
|
public:
|
2015-07-09 09:56:00 +03:00
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
explicit CacheCreator(WorkerPrivate* aWorkerPrivate)
|
|
|
|
|
: mCacheName(aWorkerPrivate->ServiceWorkerCacheName())
|
2016-09-20 04:13:00 +03:00
|
|
|
|
, mOriginAttributes(aWorkerPrivate->GetOriginAttributes())
|
2015-03-19 21:41:42 +03:00
|
|
|
|
{
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
AddLoader(CacheScriptLoader* aLoader)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(!mCacheStorage);
|
|
|
|
|
mLoaders.AppendElement(aLoader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
|
|
// Try to load from cache with aPrincipal used for cache access.
|
|
|
|
|
nsresult
|
|
|
|
|
Load(nsIPrincipal* aPrincipal);
|
|
|
|
|
|
|
|
|
|
Cache*
|
|
|
|
|
Cache_() const
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(mCache);
|
|
|
|
|
return mCache;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsIGlobalObject*
|
|
|
|
|
Global() const
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(mSandboxGlobalObject);
|
|
|
|
|
return mSandboxGlobalObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
DeleteCache();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
~CacheCreator()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
|
CreateCacheStorage(nsIPrincipal* aPrincipal);
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FailLoaders(nsresult aRv);
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<Cache> mCache;
|
|
|
|
|
RefPtr<CacheStorage> mCacheStorage;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
nsCOMPtr<nsIGlobalObject> mSandboxGlobalObject;
|
2015-10-18 08:24:48 +03:00
|
|
|
|
nsTArray<RefPtr<CacheScriptLoader>> mLoaders;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
|
|
|
|
nsString mCacheName;
|
2017-01-12 19:38:48 +03:00
|
|
|
|
OriginAttributes mOriginAttributes;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
};
|
|
|
|
|
|
2015-07-09 09:56:00 +03:00
|
|
|
|
NS_IMPL_ISUPPORTS0(CacheCreator)
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
class CacheScriptLoader final : public PromiseNativeHandler
|
2017-05-08 09:24:22 +03:00
|
|
|
|
, public nsIStreamLoaderObserver
|
2015-03-19 21:41:42 +03:00
|
|
|
|
{
|
|
|
|
|
public:
|
2015-07-09 09:56:00 +03:00
|
|
|
|
NS_DECL_ISUPPORTS
|
2015-03-19 21:41:42 +03:00
|
|
|
|
NS_DECL_NSISTREAMLOADEROBSERVER
|
|
|
|
|
|
|
|
|
|
CacheScriptLoader(WorkerPrivate* aWorkerPrivate, ScriptLoadInfo& aLoadInfo,
|
|
|
|
|
uint32_t aIndex, bool aIsWorkerScript,
|
|
|
|
|
ScriptLoaderRunnable* aRunnable)
|
|
|
|
|
: mLoadInfo(aLoadInfo)
|
|
|
|
|
, mIndex(aIndex)
|
|
|
|
|
, mRunnable(aRunnable)
|
|
|
|
|
, mIsWorkerScript(aIsWorkerScript)
|
|
|
|
|
, mFailed(false)
|
2018-04-02 17:50:21 +03:00
|
|
|
|
, mState(aWorkerPrivate->GetServiceWorkerDescriptor().State())
|
2015-03-19 21:41:42 +03:00
|
|
|
|
{
|
2017-06-30 20:52:06 +03:00
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
|
2017-06-30 20:52:06 +03:00
|
|
|
|
mMainThreadEventTarget = aWorkerPrivate->MainThreadEventTarget();
|
|
|
|
|
MOZ_ASSERT(mMainThreadEventTarget);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
mBaseURI = GetBaseURI(mIsWorkerScript, aWorkerPrivate);
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Fail(nsresult aRv);
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Load(Cache* aCache);
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
~CacheScriptLoader()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScriptLoadInfo& mLoadInfo;
|
|
|
|
|
uint32_t mIndex;
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<ScriptLoaderRunnable> mRunnable;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
bool mIsWorkerScript;
|
|
|
|
|
bool mFailed;
|
2018-04-02 17:50:21 +03:00
|
|
|
|
const ServiceWorkerState mState;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
nsCOMPtr<nsIInputStreamPump> mPump;
|
|
|
|
|
nsCOMPtr<nsIURI> mBaseURI;
|
2015-07-02 15:54:00 +03:00
|
|
|
|
mozilla::dom::ChannelInfo mChannelInfo;
|
2015-06-05 07:39:34 +03:00
|
|
|
|
UniquePtr<PrincipalInfo> mPrincipalInfo;
|
2017-02-14 18:06:38 +03:00
|
|
|
|
nsCString mCSPHeaderValue;
|
|
|
|
|
nsCString mCSPReportOnlyHeaderValue;
|
2017-02-23 18:54:42 +03:00
|
|
|
|
nsCString mReferrerPolicyHeaderValue;
|
2017-06-30 20:52:06 +03:00
|
|
|
|
nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(CacheScriptLoader, nsIStreamLoaderObserver)
|
|
|
|
|
|
|
|
|
|
class CachePromiseHandler final : public PromiseNativeHandler
|
|
|
|
|
{
|
|
|
|
|
public:
|
2015-07-09 09:56:00 +03:00
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
CachePromiseHandler(ScriptLoaderRunnable* aRunnable,
|
|
|
|
|
ScriptLoadInfo& aLoadInfo,
|
|
|
|
|
uint32_t aIndex)
|
|
|
|
|
: mRunnable(aRunnable)
|
|
|
|
|
, mLoadInfo(aLoadInfo)
|
|
|
|
|
, mIndex(aIndex)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(mRunnable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
~CachePromiseHandler()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<ScriptLoaderRunnable> mRunnable;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
ScriptLoadInfo& mLoadInfo;
|
|
|
|
|
uint32_t mIndex;
|
|
|
|
|
};
|
|
|
|
|
|
2015-07-09 09:56:00 +03:00
|
|
|
|
NS_IMPL_ISUPPORTS0(CachePromiseHandler)
|
|
|
|
|
|
2015-11-17 01:41:03 +03:00
|
|
|
|
class LoaderListener final : public nsIStreamLoaderObserver
|
|
|
|
|
, public nsIRequestObserver
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
|
|
LoaderListener(ScriptLoaderRunnable* aRunnable, uint32_t aIndex)
|
|
|
|
|
: mRunnable(aRunnable)
|
|
|
|
|
, mIndex(aIndex)
|
|
|
|
|
{
|
|
|
|
|
MOZ_ASSERT(mRunnable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
|
OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext,
|
|
|
|
|
nsresult aStatus, uint32_t aStringLen,
|
|
|
|
|
const uint8_t* aString) override;
|
|
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
|
OnStartRequest(nsIRequest* aRequest, nsISupports* aContext) override;
|
|
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
|
OnStopRequest(nsIRequest* aRequest, nsISupports* aContext,
|
|
|
|
|
nsresult aStatusCode) override
|
|
|
|
|
{
|
|
|
|
|
// Nothing to do here!
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
~LoaderListener() {}
|
|
|
|
|
|
|
|
|
|
RefPtr<ScriptLoaderRunnable> mRunnable;
|
|
|
|
|
uint32_t mIndex;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(LoaderListener, nsIStreamLoaderObserver, nsIRequestObserver)
|
|
|
|
|
|
2017-05-10 02:19:37 +03:00
|
|
|
|
class ScriptLoaderRunnable final : public nsIRunnable,
|
|
|
|
|
public nsINamed
|
2011-07-17 23:09:13 +04:00
|
|
|
|
{
|
|
|
|
|
friend class ScriptExecutorRunnable;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
friend class CachePromiseHandler;
|
|
|
|
|
friend class CacheScriptLoader;
|
2015-11-17 01:41:03 +03:00
|
|
|
|
friend class LoaderListener;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
nsTArray<ScriptLoadInfo> mLoadInfos;
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<CacheCreator> mCacheCreator;
|
2018-01-23 18:38:54 +03:00
|
|
|
|
Maybe<ClientInfo> mClientInfo;
|
2017-12-06 04:45:22 +03:00
|
|
|
|
Maybe<ServiceWorkerDescriptor> mController;
|
2015-03-17 13:15:19 +03:00
|
|
|
|
bool mIsMainScript;
|
|
|
|
|
WorkerScriptType mWorkerScriptType;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
bool mCanceledMainThread;
|
2015-10-15 15:06:55 +03:00
|
|
|
|
ErrorResult& mRv;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
|
|
public:
|
2013-07-19 06:21:20 +04:00
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
|
|
ScriptLoaderRunnable(WorkerPrivate* aWorkerPrivate,
|
2013-10-23 17:16:49 +04:00
|
|
|
|
nsIEventTarget* aSyncLoopTarget,
|
2011-07-17 23:09:13 +04:00
|
|
|
|
nsTArray<ScriptLoadInfo>& aLoadInfos,
|
2018-01-23 18:38:54 +03:00
|
|
|
|
const Maybe<ClientInfo>& aClientInfo,
|
|
|
|
|
const Maybe<ServiceWorkerDescriptor>& aController,
|
2015-03-17 13:15:19 +03:00
|
|
|
|
bool aIsMainScript,
|
2015-10-15 15:06:55 +03:00
|
|
|
|
WorkerScriptType aWorkerScriptType,
|
|
|
|
|
ErrorResult& aRv)
|
2013-10-23 17:16:49 +04:00
|
|
|
|
: mWorkerPrivate(aWorkerPrivate), mSyncLoopTarget(aSyncLoopTarget),
|
2018-01-23 18:38:54 +03:00
|
|
|
|
mClientInfo(aClientInfo), mController(aController),
|
2015-03-17 13:15:19 +03:00
|
|
|
|
mIsMainScript(aIsMainScript), mWorkerScriptType(aWorkerScriptType),
|
2018-06-19 20:08:12 +03:00
|
|
|
|
mCanceledMainThread(false), mRv(aRv)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
{
|
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
2013-10-23 17:16:49 +04:00
|
|
|
|
MOZ_ASSERT(aSyncLoopTarget);
|
2015-03-17 13:15:19 +03:00
|
|
|
|
MOZ_ASSERT_IF(aIsMainScript, aLoadInfos.Length() == 1);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2013-04-01 21:35:13 +04:00
|
|
|
|
mLoadInfos.SwapElements(aLoadInfos);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 20:08:12 +03:00
|
|
|
|
void
|
|
|
|
|
CancelMainThreadWithBindingAborted()
|
|
|
|
|
{
|
|
|
|
|
CancelMainThread(NS_BINDING_ABORTED);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
|
private:
|
|
|
|
|
~ScriptLoaderRunnable()
|
|
|
|
|
{ }
|
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
NS_IMETHOD
|
2015-03-21 19:28:04 +03:00
|
|
|
|
Run() override
|
2011-07-17 23:09:13 +04:00
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
2016-02-24 18:38:31 +03:00
|
|
|
|
nsresult rv = RunInternal();
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
CancelMainThread(rv);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-10 02:19:37 +03:00
|
|
|
|
NS_IMETHOD
|
|
|
|
|
GetName(nsACString& aName) override
|
|
|
|
|
{
|
2018-09-08 01:12:01 +03:00
|
|
|
|
aName.AssignLiteral("ScriptLoaderRunnable");
|
2017-05-10 02:19:37 +03:00
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
void
|
|
|
|
|
LoadingFinished(uint32_t aIndex, nsresult aRv)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
|
|
|
|
|
|
|
|
|
loadInfo.mLoadResult = aRv;
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(!loadInfo.mLoadingFinished);
|
|
|
|
|
loadInfo.mLoadingFinished = true;
|
|
|
|
|
|
2017-02-23 18:54:41 +03:00
|
|
|
|
if (IsMainWorkerScript() && NS_SUCCEEDED(aRv)) {
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mWorkerPrivate->PrincipalURIMatchesScriptURL());
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
MaybeExecuteFinishedScripts(aIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
MaybeExecuteFinishedScripts(uint32_t aIndex)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
|
|
|
|
|
|
|
|
|
// We execute the last step if we don't have a pending operation with the
|
|
|
|
|
// cache and the loading is completed.
|
2015-03-25 03:11:57 +03:00
|
|
|
|
if (loadInfo.Finished()) {
|
2015-03-19 21:41:42 +03:00
|
|
|
|
ExecuteFinishedScripts();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-17 01:41:03 +03:00
|
|
|
|
nsresult
|
|
|
|
|
OnStreamComplete(nsIStreamLoader* aLoader, uint32_t aIndex,
|
2012-08-22 19:56:38 +04:00
|
|
|
|
nsresult aStatus, uint32_t aStringLen,
|
2015-11-17 01:41:03 +03:00
|
|
|
|
const uint8_t* aString)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
2015-11-17 01:41:03 +03:00
|
|
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2015-11-17 01:41:03 +03:00
|
|
|
|
nsresult rv = OnStreamCompleteInternal(aLoader, aStatus, aStringLen,
|
|
|
|
|
aString, mLoadInfos[aIndex]);
|
|
|
|
|
LoadingFinished(aIndex, rv);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-17 01:41:03 +03:00
|
|
|
|
nsresult
|
|
|
|
|
OnStartRequest(nsIRequest* aRequest, uint32_t aIndex)
|
2015-04-24 02:10:57 +03:00
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
2015-11-17 01:41:03 +03:00
|
|
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
2015-04-24 02:10:57 +03:00
|
|
|
|
|
2015-12-10 02:21:00 +03:00
|
|
|
|
// If one load info cancels or hits an error, it can race with the start
|
|
|
|
|
// callback coming from another load info.
|
|
|
|
|
if (mCanceledMainThread || !mCacheCreator) {
|
2015-12-17 18:11:26 +03:00
|
|
|
|
aRequest->Cancel(NS_ERROR_FAILURE);
|
2015-12-10 02:21:00 +03:00
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-17 01:41:03 +03:00
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
2015-04-24 02:10:57 +03:00
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
|
2017-09-13 15:53:54 +03:00
|
|
|
|
|
2018-10-29 18:08:24 +03:00
|
|
|
|
// Checking the MIME type is only required for ServiceWorkers'
|
|
|
|
|
// importScripts, per step 10 of https://w3c.github.io/ServiceWorker/#importscripts
|
|
|
|
|
//
|
|
|
|
|
// "Extract a MIME type from the response’s header list. If this MIME type
|
|
|
|
|
// (ignoring parameters) is not a JavaScript MIME type, return a network error."
|
|
|
|
|
if (mWorkerPrivate->IsServiceWorker()) {
|
|
|
|
|
nsAutoCString mimeType;
|
|
|
|
|
channel->GetContentType(mimeType);
|
|
|
|
|
|
|
|
|
|
if (!nsContentUtils::IsJavascriptMIMEType(NS_ConvertUTF8toUTF16(mimeType))) {
|
|
|
|
|
const nsCString& scope =
|
|
|
|
|
mWorkerPrivate->GetServiceWorkerRegistrationDescriptor().Scope();
|
|
|
|
|
|
|
|
|
|
ServiceWorkerManager::LocalizeAndReportToAllClients(
|
|
|
|
|
scope, "ServiceWorkerRegisterMimeTypeError2",
|
|
|
|
|
nsTArray<nsString> {
|
|
|
|
|
NS_ConvertUTF8toUTF16(scope),
|
|
|
|
|
NS_ConvertUTF8toUTF16(mimeType),
|
|
|
|
|
loadInfo.mURL
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
channel->Cancel(NS_ERROR_DOM_NETWORK_ERR);
|
|
|
|
|
return NS_ERROR_DOM_NETWORK_ERR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-13 15:53:54 +03:00
|
|
|
|
// Note that importScripts() can redirect. In theory the main
|
|
|
|
|
// script could also encounter an internal redirect, but currently
|
|
|
|
|
// the assert does not allow that.
|
|
|
|
|
MOZ_ASSERT_IF(mIsMainScript, channel == loadInfo.mChannel);
|
|
|
|
|
loadInfo.mChannel = channel;
|
2015-04-24 02:10:57 +03:00
|
|
|
|
|
|
|
|
|
// We synthesize the result code, but its never exposed to content.
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<mozilla::dom::InternalResponse> ir =
|
2015-07-02 15:54:00 +03:00
|
|
|
|
new mozilla::dom::InternalResponse(200, NS_LITERAL_CSTRING("OK"));
|
2016-06-02 00:02:29 +03:00
|
|
|
|
ir->SetBody(loadInfo.mCacheReadStream, InternalResponse::UNKNOWN_BODY_SIZE);
|
2017-02-14 18:06:39 +03:00
|
|
|
|
|
2016-02-23 01:13:38 +03:00
|
|
|
|
// Drop our reference to the stream now that we've passed it along, so it
|
|
|
|
|
// doesn't hang around once the cache is done with it and keep data alive.
|
|
|
|
|
loadInfo.mCacheReadStream = nullptr;
|
2015-04-24 02:10:57 +03:00
|
|
|
|
|
2015-05-25 21:21:05 +03:00
|
|
|
|
// Set the channel info of the channel on the response so that it's
|
2015-04-24 02:10:57 +03:00
|
|
|
|
// saved in the cache.
|
2015-05-25 21:21:05 +03:00
|
|
|
|
ir->InitChannelInfo(channel);
|
2015-04-24 02:10:57 +03:00
|
|
|
|
|
2015-06-05 07:39:34 +03:00
|
|
|
|
// Save the principal of the channel since its URI encodes the script URI
|
|
|
|
|
// rather than the ServiceWorkerRegistrationInfo URI.
|
|
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
|
NS_ASSERTION(ssm, "Should never be null!");
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> channelPrincipal;
|
|
|
|
|
nsresult rv = ssm->GetChannelResultPrincipal(channel, getter_AddRefs(channelPrincipal));
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
channel->Cancel(rv);
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UniquePtr<PrincipalInfo> principalInfo(new PrincipalInfo());
|
|
|
|
|
rv = PrincipalToPrincipalInfo(channelPrincipal, principalInfo.get());
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
channel->Cancel(rv);
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
|
ir->SetPrincipalInfo(std::move(principalInfo));
|
2017-02-14 18:06:39 +03:00
|
|
|
|
ir->Headers()->FillResponseHeaders(loadInfo.mChannel);
|
2015-06-05 07:39:34 +03:00
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<mozilla::dom::Response> response =
|
2017-08-29 12:31:06 +03:00
|
|
|
|
new mozilla::dom::Response(mCacheCreator->Global(), ir, nullptr);
|
2015-04-24 02:10:57 +03:00
|
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
|
mozilla::dom::RequestOrUSVString request;
|
2015-04-24 02:10:57 +03:00
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(!loadInfo.mFullURL.IsEmpty());
|
|
|
|
|
request.SetAsUSVString().Rebind(loadInfo.mFullURL.Data(),
|
|
|
|
|
loadInfo.mFullURL.Length());
|
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
|
// This JSContext will not end up executing JS code because here there are
|
|
|
|
|
// no ReadableStreams involved.
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
|
jsapi.Init();
|
|
|
|
|
|
2015-04-24 02:10:57 +03:00
|
|
|
|
ErrorResult error;
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<Promise> cachePromise =
|
2017-08-11 04:04:55 +03:00
|
|
|
|
mCacheCreator->Cache_()->Put(jsapi.cx(), request, *response, error);
|
2018-10-31 20:30:18 +03:00
|
|
|
|
error.WouldReportJSException();
|
2015-04-24 02:10:57 +03:00
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
|
nsresult rv = error.StealNSResult();
|
|
|
|
|
channel->Cancel(rv);
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<CachePromiseHandler> promiseHandler =
|
2015-11-17 01:41:03 +03:00
|
|
|
|
new CachePromiseHandler(this, loadInfo, aIndex);
|
2015-04-24 02:10:57 +03:00
|
|
|
|
cachePromise->AppendNativeHandler(promiseHandler);
|
|
|
|
|
|
|
|
|
|
loadInfo.mCachePromise.swap(cachePromise);
|
|
|
|
|
loadInfo.mCacheStatus = ScriptLoadInfo::WritingToCache;
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
|
bool
|
|
|
|
|
IsMainWorkerScript() const
|
|
|
|
|
{
|
|
|
|
|
return mIsMainScript && mWorkerScriptType == WorkerScript;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-05 20:49:11 +03:00
|
|
|
|
bool
|
|
|
|
|
IsDebuggerScript() const
|
|
|
|
|
{
|
|
|
|
|
return mWorkerScriptType == DebuggerScript;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-24 18:38:31 +03:00
|
|
|
|
void
|
|
|
|
|
CancelMainThread(nsresult aCancelResult)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
if (mCanceledMainThread) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mCanceledMainThread = true;
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (mCacheCreator) {
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsServiceWorker());
|
|
|
|
|
DeleteCache();
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
// Cancel all the channels that were already opened.
|
2012-08-22 19:56:38 +04:00
|
|
|
|
for (uint32_t index = 0; index < mLoadInfos.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[index];
|
|
|
|
|
|
2015-03-27 01:56:02 +03:00
|
|
|
|
// If promise or channel is non-null, their failures will lead to
|
2015-03-25 03:11:57 +03:00
|
|
|
|
// LoadingFinished being called.
|
|
|
|
|
bool callLoadingFinished = true;
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (loadInfo.mCachePromise) {
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsServiceWorker());
|
2016-02-24 18:38:31 +03:00
|
|
|
|
loadInfo.mCachePromise->MaybeReject(aCancelResult);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
loadInfo.mCachePromise = nullptr;
|
2015-03-25 03:11:57 +03:00
|
|
|
|
callLoadingFinished = false;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 03:11:57 +03:00
|
|
|
|
if (loadInfo.mChannel) {
|
2016-02-24 18:38:31 +03:00
|
|
|
|
if (NS_SUCCEEDED(loadInfo.mChannel->Cancel(aCancelResult))) {
|
2015-03-25 03:11:57 +03:00
|
|
|
|
callLoadingFinished = false;
|
|
|
|
|
} else {
|
|
|
|
|
NS_WARNING("Failed to cancel channel!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (callLoadingFinished && !loadInfo.Finished()) {
|
2016-02-24 18:38:31 +03:00
|
|
|
|
LoadingFinished(index, aCancelResult);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExecuteFinishedScripts();
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
void
|
|
|
|
|
DeleteCache()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
if (!mCacheCreator) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mCacheCreator->DeleteCache();
|
|
|
|
|
mCacheCreator = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
nsresult
|
|
|
|
|
RunInternal()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
2018-06-05 20:49:13 +03:00
|
|
|
|
if (IsMainWorkerScript()) {
|
2015-03-19 21:41:42 +03:00
|
|
|
|
mWorkerPrivate->SetLoadingWorkerScript(true);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-05 20:49:11 +03:00
|
|
|
|
if (!mWorkerPrivate->IsServiceWorker() || IsDebuggerScript()) {
|
2015-03-19 21:41:42 +03:00
|
|
|
|
for (uint32_t index = 0, len = mLoadInfos.Length(); index < len;
|
|
|
|
|
++index) {
|
|
|
|
|
nsresult rv = LoadScript(index);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2015-11-17 01:41:03 +03:00
|
|
|
|
LoadingFinished(index, rv);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mCacheCreator);
|
|
|
|
|
mCacheCreator = new CacheCreator(mWorkerPrivate);
|
|
|
|
|
|
|
|
|
|
for (uint32_t index = 0, len = mLoadInfos.Length(); index < len; ++index) {
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<CacheScriptLoader> loader =
|
2015-03-19 21:41:42 +03:00
|
|
|
|
new CacheScriptLoader(mWorkerPrivate, mLoadInfos[index], index,
|
|
|
|
|
IsMainWorkerScript(), this);
|
|
|
|
|
mCacheCreator->AddLoader(loader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The worker may have a null principal on first load, but in that case its
|
|
|
|
|
// parent definitely will have one.
|
|
|
|
|
nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
|
|
|
|
|
if (!principal) {
|
|
|
|
|
WorkerPrivate* parentWorker = mWorkerPrivate->GetParent();
|
|
|
|
|
MOZ_ASSERT(parentWorker, "Must have a parent!");
|
|
|
|
|
principal = parentWorker->GetPrincipal();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult rv = mCacheCreator->Load(principal);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
|
LoadScript(uint32_t aIndex)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
2017-08-07 10:55:39 +03:00
|
|
|
|
MOZ_ASSERT_IF(IsMainWorkerScript(), mWorkerScriptType != DebuggerScript);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
WorkerPrivate* parentWorker = mWorkerPrivate->GetParent();
|
|
|
|
|
|
2017-08-07 10:55:39 +03:00
|
|
|
|
// For JavaScript debugging, the devtools server must run on the same
|
|
|
|
|
// thread as the debuggee, indicating the worker uses content principal.
|
|
|
|
|
// However, in Bug 863246, web content will no longer be able to load
|
|
|
|
|
// resource:// URIs by default, so we need system principal to load
|
|
|
|
|
// debugger scripts.
|
|
|
|
|
nsIPrincipal* principal = (mWorkerScriptType == DebuggerScript) ?
|
|
|
|
|
nsContentUtils::GetSystemPrincipal() :
|
|
|
|
|
mWorkerPrivate->GetPrincipal();
|
|
|
|
|
|
2014-12-12 19:06:00 +03:00
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = mWorkerPrivate->GetLoadGroup();
|
2017-02-07 18:28:39 +03:00
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(principal);
|
2017-05-15 16:52:00 +03:00
|
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(NS_LoadGroupMatchesPrincipal(loadGroup, principal),
|
|
|
|
|
NS_ERROR_FAILURE);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
|
|
// Figure out our base URI.
|
2015-03-19 21:41:42 +03:00
|
|
|
|
nsCOMPtr<nsIURI> baseURI = GetBaseURI(mIsMainScript, mWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
|
|
// May be null.
|
|
|
|
|
nsCOMPtr<nsIDocument> parentDoc = mWorkerPrivate->GetDocument();
|
|
|
|
|
|
2013-03-08 02:27:21 +04:00
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2015-03-17 13:15:19 +03:00
|
|
|
|
if (IsMainWorkerScript()) {
|
2013-03-08 02:27:21 +04:00
|
|
|
|
// May be null.
|
2013-06-05 18:04:23 +04:00
|
|
|
|
channel = mWorkerPrivate->ForgetWorkerChannel();
|
2013-03-08 02:27:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
nsCOMPtr<nsIIOService> ios(do_GetIOService());
|
|
|
|
|
|
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
|
NS_ASSERTION(secMan, "This should never be null!");
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
|
|
|
|
nsresult& rv = loadInfo.mLoadResult;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2017-01-04 12:09:00 +03:00
|
|
|
|
nsLoadFlags loadFlags = loadInfo.mLoadFlags;
|
2015-10-06 16:37:07 +03:00
|
|
|
|
|
2016-03-17 13:03:05 +03:00
|
|
|
|
// Get the top-level worker.
|
|
|
|
|
WorkerPrivate* topWorkerPrivate = mWorkerPrivate;
|
|
|
|
|
WorkerPrivate* parent = topWorkerPrivate->GetParent();
|
|
|
|
|
while (parent) {
|
|
|
|
|
topWorkerPrivate = parent;
|
|
|
|
|
parent = topWorkerPrivate->GetParent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the top-level worker is a dedicated worker and has a window, and the
|
|
|
|
|
// window has a docshell, the caching behavior of this worker should match
|
|
|
|
|
// that of that docshell.
|
|
|
|
|
if (topWorkerPrivate->IsDedicatedWorker()) {
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = topWorkerPrivate->GetWindow();
|
|
|
|
|
if (window) {
|
2016-04-12 17:58:21 +03:00
|
|
|
|
nsCOMPtr<nsIDocShell> docShell = window->GetDocShell();
|
2016-03-17 13:03:05 +03:00
|
|
|
|
if (docShell) {
|
|
|
|
|
nsresult rv = docShell->GetDefaultLoadFlags(&loadFlags);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (!channel) {
|
2016-04-28 10:44:08 +03:00
|
|
|
|
// Only top level workers' main script use the document charset for the
|
|
|
|
|
// script uri encoding. Otherwise, default encoding (UTF-8) is applied.
|
|
|
|
|
bool useDefaultEncoding = !(!parentWorker && IsMainWorkerScript());
|
2018-01-24 19:17:31 +03:00
|
|
|
|
rv = ChannelFromScriptURL(principal, baseURI, parentDoc, mWorkerPrivate,
|
|
|
|
|
loadGroup, ios,
|
2018-01-23 18:38:54 +03:00
|
|
|
|
secMan, loadInfo.mURL,
|
|
|
|
|
mClientInfo, mController,
|
|
|
|
|
IsMainWorkerScript(),
|
2015-06-17 04:21:08 +03:00
|
|
|
|
mWorkerScriptType,
|
2015-10-06 16:37:07 +03:00
|
|
|
|
mWorkerPrivate->ContentPolicyType(), loadFlags,
|
2016-04-28 10:44:08 +03:00
|
|
|
|
useDefaultEncoding,
|
2015-06-17 04:21:08 +03:00
|
|
|
|
getter_AddRefs(channel));
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
// We need to know which index we're on in OnStreamComplete so we know
|
|
|
|
|
// where to put the result.
|
2015-11-17 01:41:03 +03:00
|
|
|
|
RefPtr<LoaderListener> listener = new LoaderListener(this, aIndex);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
// We don't care about progress so just use the simple stream loader for
|
|
|
|
|
// OnStreamComplete notification only.
|
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
2015-11-17 01:41:03 +03:00
|
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), listener);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2017-11-21 23:13:04 +03:00
|
|
|
|
if (IsMainWorkerScript()) {
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(loadInfo.mReservedClientInfo.isSome());
|
|
|
|
|
rv = AddClientChannelHelper(channel,
|
2018-05-30 22:15:35 +03:00
|
|
|
|
std::move(loadInfo.mReservedClientInfo),
|
2017-11-21 23:13:04 +03:00
|
|
|
|
Maybe<ClientInfo>(),
|
|
|
|
|
mWorkerPrivate->HybridEventTarget());
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (loadInfo.mCacheStatus != ScriptLoadInfo::ToBeCached) {
|
2015-11-17 01:41:03 +03:00
|
|
|
|
rv = channel->AsyncOpen2(loader);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
nsCOMPtr<nsIOutputStream> writer;
|
|
|
|
|
|
|
|
|
|
// In case we return early.
|
|
|
|
|
loadInfo.mCacheStatus = ScriptLoadInfo::Cancel;
|
|
|
|
|
|
2016-02-23 01:13:38 +03:00
|
|
|
|
rv = NS_NewPipe(getter_AddRefs(loadInfo.mCacheReadStream),
|
|
|
|
|
getter_AddRefs(writer), 0,
|
2015-03-19 21:41:42 +03:00
|
|
|
|
UINT32_MAX, // unlimited size to avoid writer WOULD_BLOCK case
|
|
|
|
|
true, false); // non-blocking reader, blocking writer
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamListenerTee> tee =
|
|
|
|
|
do_CreateInstance(NS_STREAMLISTENERTEE_CONTRACTID);
|
2015-11-17 01:41:03 +03:00
|
|
|
|
rv = tee->Init(loader, writer, listener);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-17 01:41:03 +03:00
|
|
|
|
nsresult rv = channel->AsyncOpen2(tee);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
loadInfo.mChannel.swap(channel);
|
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult
|
2015-11-17 01:41:03 +03:00
|
|
|
|
OnStreamCompleteInternal(nsIStreamLoader* aLoader, nsresult aStatus,
|
|
|
|
|
uint32_t aStringLen, const uint8_t* aString,
|
|
|
|
|
ScriptLoadInfo& aLoadInfo)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
if (!aLoadInfo.mChannel) {
|
|
|
|
|
return NS_BINDING_ABORTED;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
|
aLoadInfo.mChannel = nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
|
return aStatus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NS_ASSERTION(aString, "This should never be null!");
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRequest> request;
|
|
|
|
|
nsresult rv = aLoader->GetRequest(getter_AddRefs(request));
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
2015-07-14 18:08:38 +03:00
|
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
|
|
|
|
|
MOZ_ASSERT(channel);
|
|
|
|
|
|
|
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
|
NS_ASSERTION(ssm, "Should never be null!");
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> channelPrincipal;
|
|
|
|
|
rv = ssm->GetChannelResultPrincipal(channel, getter_AddRefs(channelPrincipal));
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
|
|
|
|
|
if (!principal) {
|
|
|
|
|
WorkerPrivate* parentWorker = mWorkerPrivate->GetParent();
|
|
|
|
|
MOZ_ASSERT(parentWorker, "Must have a parent!");
|
|
|
|
|
principal = parentWorker->GetPrincipal();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-07 14:29:05 +03:00
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
if (IsMainWorkerScript()) {
|
|
|
|
|
nsCOMPtr<nsIPrincipal> loadingPrincipal =
|
|
|
|
|
mWorkerPrivate->GetLoadingPrincipal();
|
|
|
|
|
// if we are not in a ServiceWorker, and the principal is not null, then the
|
|
|
|
|
// loading principal must subsume the worker principal if it is not a
|
|
|
|
|
// nullPrincipal (sandbox).
|
|
|
|
|
MOZ_ASSERT(!loadingPrincipal ||
|
|
|
|
|
loadingPrincipal->GetIsNullPrincipal() ||
|
|
|
|
|
principal->GetIsNullPrincipal() ||
|
|
|
|
|
loadingPrincipal->Subsumes(principal));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-10-26 20:28:45 +03:00
|
|
|
|
// We don't mute the main worker script becase we've already done
|
|
|
|
|
// same-origin checks on them so we should be able to see their errors.
|
|
|
|
|
// Note that for data: url, where we allow it through the same-origin check
|
|
|
|
|
// but then give it a different origin.
|
|
|
|
|
aLoadInfo.mMutedErrorFlag.emplace(IsMainWorkerScript()
|
2016-07-18 10:12:40 +03:00
|
|
|
|
? false
|
2015-10-26 20:28:45 +03:00
|
|
|
|
: !principal->Subsumes(channelPrincipal));
|
2015-07-14 18:08:38 +03:00
|
|
|
|
|
|
|
|
|
// Make sure we're not seeing the result of a 404 or something by checking
|
|
|
|
|
// the 'requestSucceeded' attribute on the http channel.
|
2011-07-17 23:09:13 +04:00
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request);
|
2016-07-19 10:38:26 +03:00
|
|
|
|
nsAutoCString tCspHeaderValue, tCspROHeaderValue, tRPHeaderCValue;
|
2016-06-30 07:31:59 +03:00
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
if (httpChannel) {
|
2011-09-29 10:19:26 +04:00
|
|
|
|
bool requestSucceeded;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
rv = httpChannel->GetRequestSucceeded(&requestSucceeded);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
|
|
if (!requestSucceeded) {
|
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
}
|
2016-06-30 07:31:59 +03:00
|
|
|
|
|
2016-12-20 06:49:32 +03:00
|
|
|
|
Unused << httpChannel->GetResponseHeader(
|
2016-06-30 07:31:59 +03:00
|
|
|
|
NS_LITERAL_CSTRING("content-security-policy"),
|
|
|
|
|
tCspHeaderValue);
|
|
|
|
|
|
2016-12-20 06:49:32 +03:00
|
|
|
|
Unused << httpChannel->GetResponseHeader(
|
2016-06-30 07:31:59 +03:00
|
|
|
|
NS_LITERAL_CSTRING("content-security-policy-report-only"),
|
|
|
|
|
tCspROHeaderValue);
|
2016-07-19 10:38:26 +03:00
|
|
|
|
|
2016-12-20 06:49:32 +03:00
|
|
|
|
Unused << httpChannel->GetResponseHeader(
|
2016-07-19 10:38:26 +03:00
|
|
|
|
NS_LITERAL_CSTRING("referrer-policy"),
|
|
|
|
|
tRPHeaderCValue);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// May be null.
|
|
|
|
|
nsIDocument* parentDoc = mWorkerPrivate->GetDocument();
|
|
|
|
|
|
2017-05-08 09:24:22 +03:00
|
|
|
|
// Use the regular ScriptLoader for this grunt work! Should be just fine
|
2011-07-17 23:09:13 +04:00
|
|
|
|
// because we're running on the main thread.
|
2013-10-10 02:23:47 +04:00
|
|
|
|
// Unlike <script> tags, Worker scripts are always decoded as UTF-8,
|
|
|
|
|
// per spec. So we explicitly pass in the charset hint.
|
2017-05-08 09:24:22 +03:00
|
|
|
|
rv = ScriptLoader::ConvertToUTF16(aLoadInfo.mChannel, aString, aStringLen,
|
|
|
|
|
NS_LITERAL_STRING("UTF-8"), parentDoc,
|
|
|
|
|
aLoadInfo.mScriptTextBuf,
|
|
|
|
|
aLoadInfo.mScriptTextLength);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 21:53:48 +03:00
|
|
|
|
if (!aLoadInfo.mScriptTextLength && !aLoadInfo.mScriptTextBuf) {
|
|
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
|
|
|
|
|
NS_LITERAL_CSTRING("DOM"), parentDoc,
|
|
|
|
|
nsContentUtils::eDOM_PROPERTIES,
|
|
|
|
|
"EmptyWorkerSourceWarning");
|
|
|
|
|
} else if (!aLoadInfo.mScriptTextBuf) {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Figure out what we actually loaded.
|
|
|
|
|
nsCOMPtr<nsIURI> finalURI;
|
|
|
|
|
rv = NS_GetFinalChannelURI(channel, getter_AddRefs(finalURI));
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
|
|
nsCString filename;
|
|
|
|
|
rv = finalURI->GetSpec(filename);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
|
|
if (!filename.IsEmpty()) {
|
|
|
|
|
// This will help callers figure out what their script url resolved to in
|
|
|
|
|
// case of errors.
|
|
|
|
|
aLoadInfo.mURL.Assign(NS_ConvertUTF8toUTF16(filename));
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 03:36:00 +03:00
|
|
|
|
nsCOMPtr<nsILoadInfo> chanLoadInfo = channel->GetLoadInfo();
|
|
|
|
|
if (chanLoadInfo && chanLoadInfo->GetEnforceSRI()) {
|
|
|
|
|
// importScripts() and the Worker constructor do not support integrity metadata
|
|
|
|
|
// (or any fetch options). Until then, we can just block.
|
|
|
|
|
// If we ever have those data in the future, we'll have to the check to
|
|
|
|
|
// by using the SRICheck module
|
|
|
|
|
MOZ_LOG(SRILogHelper::GetSriLog(), mozilla::LogLevel::Debug,
|
|
|
|
|
("Scriptloader::Load, SRI required but not supported in workers"));
|
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> wcsp;
|
|
|
|
|
chanLoadInfo->LoadingPrincipal()->GetCsp(getter_AddRefs(wcsp));
|
2018-09-17 18:53:10 +03:00
|
|
|
|
MOZ_ASSERT(wcsp, "We should have a CSP for the worker here");
|
2016-10-04 03:36:00 +03:00
|
|
|
|
if (wcsp) {
|
|
|
|
|
wcsp->LogViolationDetails(
|
|
|
|
|
nsIContentSecurityPolicy::VIOLATION_TYPE_REQUIRE_SRI_FOR_SCRIPT,
|
2018-07-10 18:40:21 +03:00
|
|
|
|
nullptr, // triggering element
|
2018-10-23 09:17:13 +03:00
|
|
|
|
mWorkerPrivate->CSPEventListener(),
|
2018-07-05 09:21:04 +03:00
|
|
|
|
aLoadInfo.mURL, EmptyString(), 0, 0, EmptyString(), EmptyString());
|
2016-10-04 03:36:00 +03:00
|
|
|
|
}
|
|
|
|
|
return NS_ERROR_SRI_CORRUPT;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
// Update the principal of the worker and its base URI if we just loaded the
|
|
|
|
|
// worker's primary script.
|
2015-03-17 13:15:19 +03:00
|
|
|
|
if (IsMainWorkerScript()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
// Take care of the base URI first.
|
2011-11-04 20:32:17 +04:00
|
|
|
|
mWorkerPrivate->SetBaseURI(finalURI);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2015-05-25 21:21:05 +03:00
|
|
|
|
// Store the channel info if needed.
|
2015-08-18 01:08:58 +03:00
|
|
|
|
mWorkerPrivate->InitChannelInfo(channel);
|
2015-04-24 02:04:03 +03:00
|
|
|
|
|
2017-12-07 14:29:05 +03:00
|
|
|
|
// Our final channel principal should match the loading principal
|
2018-01-03 18:56:14 +03:00
|
|
|
|
// in terms of the origin. This used to be an assert, but it seems
|
|
|
|
|
// there are some rare cases where this check can fail in practice.
|
|
|
|
|
// Perhaps some browser script setting nsIChannel.owner, etc.
|
|
|
|
|
NS_ENSURE_TRUE(mWorkerPrivate->FinalChannelPrincipalIsValid(channel),
|
|
|
|
|
NS_ERROR_FAILURE);
|
2017-02-07 18:28:39 +03:00
|
|
|
|
|
2017-02-23 18:54:41 +03:00
|
|
|
|
// However, we must still override the principal since the nsIPrincipal
|
|
|
|
|
// URL may be different due to same-origin redirects. Unfortunately this
|
|
|
|
|
// URL must exactly match the final worker script URL in order to
|
|
|
|
|
// properly set the referrer header on fetch/xhr requests. If bug 1340694
|
|
|
|
|
// is ever fixed this can be removed.
|
|
|
|
|
rv = mWorkerPrivate->SetPrincipalFromChannel(channel);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
2017-03-29 05:20:32 +03:00
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp = mWorkerPrivate->GetCSP();
|
2016-06-30 07:31:59 +03:00
|
|
|
|
// We did inherit CSP in bug 1223647. If we do not already have a CSP, we
|
|
|
|
|
// should get it from the HTTP headers on the worker script.
|
2018-07-19 03:43:29 +03:00
|
|
|
|
if (StaticPrefs::security_csp_enable()) {
|
2017-03-29 05:20:32 +03:00
|
|
|
|
if (!csp) {
|
|
|
|
|
rv = mWorkerPrivate->SetCSPFromHeaderValues(tCspHeaderValue,
|
|
|
|
|
tCspROHeaderValue);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
} else {
|
|
|
|
|
csp->EnsureEventTarget(mWorkerPrivate->MainThreadEventTarget());
|
|
|
|
|
}
|
2016-06-30 07:31:59 +03:00
|
|
|
|
}
|
2017-02-23 18:54:42 +03:00
|
|
|
|
|
|
|
|
|
mWorkerPrivate->SetReferrerPolicyFromHeaderValue(tRPHeaderCValue);
|
|
|
|
|
|
2017-02-07 18:28:39 +03:00
|
|
|
|
WorkerPrivate* parent = mWorkerPrivate->GetParent();
|
2016-06-30 07:31:59 +03:00
|
|
|
|
if (parent) {
|
|
|
|
|
// XHR Params Allowed
|
|
|
|
|
mWorkerPrivate->SetXHRParamsAllowed(parent->XHRParamsAllowed());
|
|
|
|
|
}
|
2017-12-06 04:45:22 +03:00
|
|
|
|
|
|
|
|
|
if (chanLoadInfo) {
|
|
|
|
|
mController = chanLoadInfo->GetController();
|
|
|
|
|
}
|
2018-01-23 18:38:54 +03:00
|
|
|
|
|
|
|
|
|
// If we are loading a blob URL we must inherit the controller
|
|
|
|
|
// from the parent. This is a bit odd as the blob URL may have
|
|
|
|
|
// been created in a different context with a different controller.
|
|
|
|
|
// For now, though, this is what the spec says. See:
|
|
|
|
|
//
|
|
|
|
|
// https://github.com/w3c/ServiceWorker/issues/1261
|
|
|
|
|
//
|
|
|
|
|
if (IsBlobURI(mWorkerPrivate->GetBaseURI())) {
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mController.isNothing());
|
|
|
|
|
mController = mWorkerPrivate->GetParentController();
|
|
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
DataReceivedFromCache(uint32_t aIndex, const uint8_t* aString,
|
2015-05-25 21:21:05 +03:00
|
|
|
|
uint32_t aStringLen,
|
2015-07-02 15:54:00 +03:00
|
|
|
|
const mozilla::dom::ChannelInfo& aChannelInfo,
|
2017-02-14 18:06:38 +03:00
|
|
|
|
UniquePtr<PrincipalInfo> aPrincipalInfo,
|
|
|
|
|
const nsACString& aCSPHeaderValue,
|
2017-02-23 18:54:42 +03:00
|
|
|
|
const nsACString& aCSPReportOnlyHeaderValue,
|
|
|
|
|
const nsACString& aReferrerPolicyHeaderValue)
|
2015-03-19 21:41:42 +03:00
|
|
|
|
{
|
2015-03-19 02:14:30 +03:00
|
|
|
|
AssertIsOnMainThread();
|
2015-03-19 21:41:42 +03:00
|
|
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
|
|
|
|
MOZ_ASSERT(loadInfo.mCacheStatus == ScriptLoadInfo::Cached);
|
|
|
|
|
|
2015-07-14 18:08:38 +03:00
|
|
|
|
nsCOMPtr<nsIPrincipal> responsePrincipal =
|
|
|
|
|
PrincipalInfoToPrincipal(*aPrincipalInfo);
|
2017-02-14 18:06:38 +03:00
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(responsePrincipal);
|
2015-07-14 18:08:38 +03:00
|
|
|
|
|
|
|
|
|
nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
|
|
|
|
|
if (!principal) {
|
|
|
|
|
WorkerPrivate* parentWorker = mWorkerPrivate->GetParent();
|
|
|
|
|
MOZ_ASSERT(parentWorker, "Must have a parent!");
|
|
|
|
|
principal = parentWorker->GetPrincipal();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 15:06:55 +03:00
|
|
|
|
loadInfo.mMutedErrorFlag.emplace(!principal->Subsumes(responsePrincipal));
|
2015-07-14 18:08:38 +03:00
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
// May be null.
|
|
|
|
|
nsIDocument* parentDoc = mWorkerPrivate->GetDocument();
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(!loadInfo.mScriptTextBuf);
|
|
|
|
|
|
2015-03-19 02:14:30 +03:00
|
|
|
|
nsresult rv =
|
2017-05-08 09:24:22 +03:00
|
|
|
|
ScriptLoader::ConvertToUTF16(nullptr, aString, aStringLen,
|
|
|
|
|
NS_LITERAL_STRING("UTF-8"), parentDoc,
|
|
|
|
|
loadInfo.mScriptTextBuf,
|
|
|
|
|
loadInfo.mScriptTextLength);
|
2015-03-19 02:14:30 +03:00
|
|
|
|
if (NS_SUCCEEDED(rv) && IsMainWorkerScript()) {
|
|
|
|
|
nsCOMPtr<nsIURI> finalURI;
|
|
|
|
|
rv = NS_NewURI(getter_AddRefs(finalURI), loadInfo.mFullURL, nullptr, nullptr);
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
|
mWorkerPrivate->SetBaseURI(finalURI);
|
|
|
|
|
}
|
2015-04-11 00:14:44 +03:00
|
|
|
|
|
2017-05-26 19:53:35 +03:00
|
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
2017-02-14 18:06:38 +03:00
|
|
|
|
nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(principal);
|
|
|
|
|
|
|
|
|
|
bool equal = false;
|
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(responsePrincipal->Equals(principal, &equal));
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(equal);
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(responsePrincipal->GetCsp(getter_AddRefs(csp)));
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!csp);
|
|
|
|
|
#endif
|
2015-06-05 07:39:34 +03:00
|
|
|
|
|
2015-05-25 21:21:05 +03:00
|
|
|
|
mWorkerPrivate->InitChannelInfo(aChannelInfo);
|
2017-02-14 18:06:39 +03:00
|
|
|
|
|
2017-02-20 17:31:24 +03:00
|
|
|
|
nsILoadGroup* loadGroup = mWorkerPrivate->GetLoadGroup();
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(loadGroup);
|
|
|
|
|
|
|
|
|
|
// Override the principal on the WorkerPrivate. This is only necessary
|
|
|
|
|
// in order to get a principal with exactly the correct URL. The fetch
|
|
|
|
|
// referrer logic depends on the WorkerPrivate principal having a URL
|
2017-02-23 18:54:41 +03:00
|
|
|
|
// that matches the worker script URL. If bug 1340694 is ever fixed
|
|
|
|
|
// this can be removed.
|
2017-02-10 21:34:38 +03:00
|
|
|
|
rv = mWorkerPrivate->SetPrincipalOnMainThread(responsePrincipal, loadGroup);
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
|
2017-02-14 18:06:38 +03:00
|
|
|
|
|
|
|
|
|
rv = mWorkerPrivate->SetCSPFromHeaderValues(aCSPHeaderValue,
|
|
|
|
|
aCSPReportOnlyHeaderValue);
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
|
2017-02-23 18:54:42 +03:00
|
|
|
|
|
|
|
|
|
mWorkerPrivate->SetReferrerPolicyFromHeaderValue(aReferrerPolicyHeaderValue);
|
2015-03-19 02:14:30 +03:00
|
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
|
DataReceived();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoadingFinished(aIndex, rv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
DataReceived()
|
|
|
|
|
{
|
|
|
|
|
if (IsMainWorkerScript()) {
|
|
|
|
|
WorkerPrivate* parent = mWorkerPrivate->GetParent();
|
2012-09-15 22:51:55 +04:00
|
|
|
|
|
|
|
|
|
if (parent) {
|
2012-09-17 04:20:16 +04:00
|
|
|
|
// XHR Params Allowed
|
|
|
|
|
mWorkerPrivate->SetXHRParamsAllowed(parent->XHRParamsAllowed());
|
|
|
|
|
|
|
|
|
|
// Set Eval and ContentSecurityPolicy
|
2012-09-15 22:51:55 +04:00
|
|
|
|
mWorkerPrivate->SetCSP(parent->GetCSP());
|
|
|
|
|
mWorkerPrivate->SetEvalAllowed(parent->IsEvalAllowed());
|
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ExecuteFinishedScripts()
|
|
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
|
if (IsMainWorkerScript()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
|
mWorkerPrivate->WorkerScriptLoaded();
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-28 10:57:33 +04:00
|
|
|
|
uint32_t firstIndex = UINT32_MAX;
|
|
|
|
|
uint32_t lastIndex = UINT32_MAX;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
|
|
// Find firstIndex based on whether mExecutionScheduled is unset.
|
2012-08-22 19:56:38 +04:00
|
|
|
|
for (uint32_t index = 0; index < mLoadInfos.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
if (!mLoadInfos[index].mExecutionScheduled) {
|
|
|
|
|
firstIndex = index;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find lastIndex based on whether mChannel is set, and update
|
|
|
|
|
// mExecutionScheduled on the ones we're about to schedule.
|
2012-09-28 10:57:33 +04:00
|
|
|
|
if (firstIndex != UINT32_MAX) {
|
2012-08-22 19:56:38 +04:00
|
|
|
|
for (uint32_t index = firstIndex; index < mLoadInfos.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[index];
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (!loadInfo.Finished()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We can execute this one.
|
|
|
|
|
loadInfo.mExecutionScheduled = true;
|
|
|
|
|
|
|
|
|
|
lastIndex = index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
// This is the last index, we can unused things before the exection of the
|
|
|
|
|
// script and the stopping of the sync loop.
|
|
|
|
|
if (lastIndex == mLoadInfos.Length() - 1) {
|
|
|
|
|
mCacheCreator = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-28 10:57:33 +04:00
|
|
|
|
if (firstIndex != UINT32_MAX && lastIndex != UINT32_MAX) {
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<ScriptExecutorRunnable> runnable =
|
2015-03-19 21:41:42 +03:00
|
|
|
|
new ScriptExecutorRunnable(*this, mSyncLoopTarget, IsMainWorkerScript(),
|
|
|
|
|
firstIndex, lastIndex);
|
2016-02-26 23:23:12 +03:00
|
|
|
|
if (!runnable->Dispatch()) {
|
2013-10-23 17:16:49 +04:00
|
|
|
|
MOZ_ASSERT(false, "This should never fail!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-10 02:19:37 +03:00
|
|
|
|
NS_IMPL_ISUPPORTS(ScriptLoaderRunnable, nsIRunnable, nsINamed)
|
2015-11-17 01:41:03 +03:00
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
|
LoaderListener::OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext,
|
|
|
|
|
nsresult aStatus, uint32_t aStringLen,
|
|
|
|
|
const uint8_t* aString)
|
|
|
|
|
{
|
|
|
|
|
return mRunnable->OnStreamComplete(aLoader, mIndex, aStatus, aStringLen, aString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
|
LoaderListener::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
|
|
|
|
|
{
|
|
|
|
|
return mRunnable->OnStartRequest(aRequest, mIndex);
|
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
void
|
|
|
|
|
CachePromiseHandler::ResolvedCallback(JSContext* aCx,
|
|
|
|
|
JS::Handle<JS::Value> aValue)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
2015-03-27 01:56:02 +03:00
|
|
|
|
// May already have been canceled by CacheScriptLoader::Fail from
|
|
|
|
|
// CancelMainThread.
|
|
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::WritingToCache ||
|
|
|
|
|
mLoadInfo.mCacheStatus == ScriptLoadInfo::Cancel);
|
|
|
|
|
MOZ_ASSERT_IF(mLoadInfo.mCacheStatus == ScriptLoadInfo::Cancel, !mLoadInfo.mCachePromise);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
2015-03-25 03:11:57 +03:00
|
|
|
|
if (mLoadInfo.mCachePromise) {
|
2015-03-27 01:56:02 +03:00
|
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::Cached;
|
2015-03-25 03:11:57 +03:00
|
|
|
|
mLoadInfo.mCachePromise = nullptr;
|
|
|
|
|
mRunnable->MaybeExecuteFinishedScripts(mIndex);
|
|
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CachePromiseHandler::RejectedCallback(JSContext* aCx,
|
|
|
|
|
JS::Handle<JS::Value> aValue)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
2015-03-27 01:56:02 +03:00
|
|
|
|
// May already have been canceled by CacheScriptLoader::Fail from
|
|
|
|
|
// CancelMainThread.
|
|
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::WritingToCache ||
|
|
|
|
|
mLoadInfo.mCacheStatus == ScriptLoadInfo::Cancel);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::Cancel;
|
|
|
|
|
|
|
|
|
|
mLoadInfo.mCachePromise = nullptr;
|
|
|
|
|
|
|
|
|
|
// This will delete the cache object and will call LoadingFinished() with an
|
|
|
|
|
// error for each ongoing operation.
|
|
|
|
|
mRunnable->DeleteCache();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
|
CacheCreator::CreateCacheStorage(nsIPrincipal* aPrincipal)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(!mCacheStorage);
|
|
|
|
|
MOZ_ASSERT(aPrincipal);
|
|
|
|
|
|
|
|
|
|
nsIXPConnect* xpc = nsContentUtils::XPConnect();
|
|
|
|
|
MOZ_ASSERT(xpc, "This should never be null!");
|
|
|
|
|
|
2018-08-01 12:25:49 +03:00
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
|
jsapi.Init();
|
|
|
|
|
JSContext* cx = jsapi.cx();
|
2015-07-21 21:31:44 +03:00
|
|
|
|
JS::Rooted<JSObject*> sandbox(cx);
|
|
|
|
|
nsresult rv = xpc->CreateSandbox(cx, aPrincipal, sandbox.address());
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-01 12:25:49 +03:00
|
|
|
|
// The JSContext is not in a realm, so CreateSandbox returned an unwrapped
|
|
|
|
|
// global.
|
|
|
|
|
MOZ_ASSERT(JS_IsGlobalObject(sandbox));
|
|
|
|
|
|
2015-07-21 21:31:44 +03:00
|
|
|
|
mSandboxGlobalObject = xpc::NativeGlobal(sandbox);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (NS_WARN_IF(!mSandboxGlobalObject)) {
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-27 05:36:40 +03:00
|
|
|
|
// If we're in private browsing mode, don't even try to create the
|
|
|
|
|
// CacheStorage. Instead, just fail immediately to terminate the
|
|
|
|
|
// ServiceWorker load.
|
2016-09-20 04:13:00 +03:00
|
|
|
|
if (NS_WARN_IF(mOriginAttributes.mPrivateBrowsingId > 0)) {
|
2015-06-27 05:36:40 +03:00
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-28 06:19:24 +03:00
|
|
|
|
// Create a CacheStorage bypassing its trusted origin checks. The
|
|
|
|
|
// ServiceWorker has already performed its own checks before getting
|
|
|
|
|
// to this point.
|
2015-03-19 21:41:42 +03:00
|
|
|
|
ErrorResult error;
|
|
|
|
|
mCacheStorage =
|
2015-07-02 15:54:00 +03:00
|
|
|
|
CacheStorage::CreateOnMainThread(mozilla::dom::cache::CHROME_ONLY_NAMESPACE,
|
2015-03-19 21:41:42 +03:00
|
|
|
|
mSandboxGlobalObject,
|
2016-09-20 04:13:00 +03:00
|
|
|
|
aPrincipal,
|
2015-06-28 06:19:24 +03:00
|
|
|
|
true /* force trusted origin */,
|
2015-06-27 05:36:40 +03:00
|
|
|
|
error);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2015-04-27 16:18:51 +03:00
|
|
|
|
return error.StealNSResult();
|
2015-03-19 21:41:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
|
CacheCreator::Load(nsIPrincipal* aPrincipal)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(!mLoaders.IsEmpty());
|
|
|
|
|
|
|
|
|
|
nsresult rv = CreateCacheStorage(aPrincipal);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorResult error;
|
|
|
|
|
MOZ_ASSERT(!mCacheName.IsEmpty());
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<Promise> promise = mCacheStorage->Open(mCacheName, error);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2015-04-27 16:18:51 +03:00
|
|
|
|
return error.StealNSResult();
|
2015-03-19 21:41:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
promise->AppendNativeHandler(this);
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CacheCreator::FailLoaders(nsresult aRv)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
2015-03-27 20:00:59 +03:00
|
|
|
|
// Fail() can call LoadingFinished() which may call ExecuteFinishedScripts()
|
|
|
|
|
// which sets mCacheCreator to null, so hold a ref.
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<CacheCreator> kungfuDeathGrip = this;
|
2015-03-27 20:00:59 +03:00
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
for (uint32_t i = 0, len = mLoaders.Length(); i < len; ++i) {
|
|
|
|
|
mLoaders[i]->Fail(aRv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mLoaders.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CacheCreator::RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
FailLoaders(NS_ERROR_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CacheCreator::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
2017-02-01 17:48:07 +03:00
|
|
|
|
|
|
|
|
|
if (!aValue.isObject()) {
|
|
|
|
|
FailLoaders(NS_ERROR_FAILURE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
|
|
|
|
|
Cache* cache = nullptr;
|
2017-07-10 23:05:24 +03:00
|
|
|
|
nsresult rv = UNWRAP_OBJECT(Cache, &obj, cache);
|
2017-02-01 17:48:07 +03:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
FailLoaders(NS_ERROR_FAILURE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
|
|
|
|
mCache = cache;
|
2017-02-01 17:48:07 +03:00
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mCache);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
|
|
|
|
// If the worker is canceled, CancelMainThread() will have cleared the
|
2017-02-01 17:48:07 +03:00
|
|
|
|
// loaders via DeleteCache().
|
2015-03-19 21:41:42 +03:00
|
|
|
|
for (uint32_t i = 0, len = mLoaders.Length(); i < len; ++i) {
|
2017-02-01 17:48:07 +03:00
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mLoaders[i]);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
mLoaders[i]->Load(cache);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CacheCreator::DeleteCache()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
2017-01-10 19:20:16 +03:00
|
|
|
|
// This is called when the load is canceled which can occur before
|
|
|
|
|
// mCacheStorage is initialized.
|
2017-02-01 17:48:07 +03:00
|
|
|
|
if (mCacheStorage) {
|
|
|
|
|
// It's safe to do this while Cache::Match() and Cache::Put() calls are
|
|
|
|
|
// running.
|
2018-02-01 22:21:14 +03:00
|
|
|
|
RefPtr<Promise> promise = mCacheStorage->Delete(mCacheName, IgnoreErrors());
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
2017-02-01 17:48:07 +03:00
|
|
|
|
// We don't care to know the result of the promise object.
|
2015-03-19 21:41:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 17:48:07 +03:00
|
|
|
|
// Always call this here to ensure the loaders array is cleared.
|
2015-03-19 21:41:42 +03:00
|
|
|
|
FailLoaders(NS_ERROR_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CacheScriptLoader::Fail(nsresult aRv)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(NS_FAILED(aRv));
|
|
|
|
|
|
|
|
|
|
if (mFailed) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mFailed = true;
|
|
|
|
|
|
|
|
|
|
if (mPump) {
|
|
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::ReadingFromCache);
|
|
|
|
|
mPump->Cancel(aRv);
|
|
|
|
|
mPump = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::Cancel;
|
2015-03-25 03:11:57 +03:00
|
|
|
|
|
|
|
|
|
// Stop if the load was aborted on the main thread.
|
2015-03-27 01:56:02 +03:00
|
|
|
|
// Can't use Finished() because mCachePromise may still be true.
|
|
|
|
|
if (mLoadInfo.mLoadingFinished) {
|
|
|
|
|
MOZ_ASSERT(!mLoadInfo.mChannel);
|
|
|
|
|
MOZ_ASSERT_IF(mLoadInfo.mCachePromise,
|
|
|
|
|
mLoadInfo.mCacheStatus == ScriptLoadInfo::WritingToCache ||
|
|
|
|
|
mLoadInfo.mCacheStatus == ScriptLoadInfo::Cancel);
|
2015-03-25 03:11:57 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
mRunnable->LoadingFinished(mIndex, aRv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CacheScriptLoader::Load(Cache* aCache)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(aCache);
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), mLoadInfo.mURL, nullptr,
|
|
|
|
|
mBaseURI);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
Fail(rv);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsAutoCString spec;
|
|
|
|
|
rv = uri->GetSpec(spec);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
Fail(rv);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(mLoadInfo.mFullURL.IsEmpty());
|
|
|
|
|
CopyUTF8toUTF16(spec, mLoadInfo.mFullURL);
|
|
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
|
mozilla::dom::RequestOrUSVString request;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
request.SetAsUSVString().Rebind(mLoadInfo.mFullURL.Data(),
|
|
|
|
|
mLoadInfo.mFullURL.Length());
|
|
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
|
mozilla::dom::CacheQueryOptions params;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
|
// This JSContext will not end up executing JS code because here there are
|
|
|
|
|
// no ReadableStreams involved.
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
|
jsapi.Init();
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
ErrorResult error;
|
2017-08-11 04:04:55 +03:00
|
|
|
|
RefPtr<Promise> promise = aCache->Match(jsapi.cx(), request, params, error);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2015-04-27 16:18:52 +03:00
|
|
|
|
Fail(error.StealNSResult());
|
2015-03-19 21:41:42 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
promise->AppendNativeHandler(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CacheScriptLoader::RejectedCallback(JSContext* aCx,
|
|
|
|
|
JS::Handle<JS::Value> aValue)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::Uncached);
|
|
|
|
|
Fail(NS_ERROR_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CacheScriptLoader::ResolvedCallback(JSContext* aCx,
|
|
|
|
|
JS::Handle<JS::Value> aValue)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
// If we have already called 'Fail', we should not proceed.
|
|
|
|
|
if (mFailed) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 23:29:40 +03:00
|
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::Uncached);
|
|
|
|
|
|
2015-11-17 01:41:03 +03:00
|
|
|
|
nsresult rv;
|
|
|
|
|
|
2018-04-02 17:50:21 +03:00
|
|
|
|
// The ServiceWorkerScriptCache will store data for any scripts it
|
|
|
|
|
// it knows about. This is always at least the top level script.
|
|
|
|
|
// Depending on if a previous version of the service worker has
|
|
|
|
|
// been installed or not it may also know about importScripts(). We
|
|
|
|
|
// must handle loading and offlining new importScripts() here, however.
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (aValue.isUndefined()) {
|
2018-04-02 17:50:21 +03:00
|
|
|
|
// If this is the main script or we're not loading a new service worker
|
2018-06-05 20:49:11 +03:00
|
|
|
|
// then this is an error. This can happen for internal reasons, like
|
|
|
|
|
// storage was probably wiped without removing the service worker
|
|
|
|
|
// registration. It can also happen for exposed reasons like the
|
|
|
|
|
// service worker script calling importScripts() after install.
|
|
|
|
|
if (NS_WARN_IF(mIsWorkerScript || (mState != ServiceWorkerState::Parsed &&
|
|
|
|
|
mState != ServiceWorkerState::Installing))) {
|
2018-04-02 17:50:21 +03:00
|
|
|
|
Fail(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::ToBeCached;
|
2015-11-17 01:41:03 +03:00
|
|
|
|
rv = mRunnable->LoadScript(mIndex);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
Fail(rv);
|
|
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(aValue.isObject());
|
|
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
|
2015-07-02 15:54:00 +03:00
|
|
|
|
mozilla::dom::Response* response = nullptr;
|
2017-07-10 23:05:24 +03:00
|
|
|
|
rv = UNWRAP_OBJECT(Response, &obj, response);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
Fail(rv);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 18:06:38 +03:00
|
|
|
|
InternalHeaders* headers = response->GetInternalHeaders();
|
|
|
|
|
|
|
|
|
|
headers->Get(NS_LITERAL_CSTRING("content-security-policy"),
|
2018-02-01 22:21:14 +03:00
|
|
|
|
mCSPHeaderValue, IgnoreErrors());
|
2017-02-14 18:06:38 +03:00
|
|
|
|
headers->Get(NS_LITERAL_CSTRING("content-security-policy-report-only"),
|
2018-02-01 22:21:14 +03:00
|
|
|
|
mCSPReportOnlyHeaderValue, IgnoreErrors());
|
2017-02-23 18:54:42 +03:00
|
|
|
|
headers->Get(NS_LITERAL_CSTRING("referrer-policy"),
|
2018-02-01 22:21:14 +03:00
|
|
|
|
mReferrerPolicyHeaderValue, IgnoreErrors());
|
2017-02-14 18:06:38 +03:00
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
|
response->GetBody(getter_AddRefs(inputStream));
|
2015-05-25 21:21:05 +03:00
|
|
|
|
mChannelInfo = response->GetChannelInfo();
|
2015-07-02 15:54:00 +03:00
|
|
|
|
const UniquePtr<PrincipalInfo>& pInfo = response->GetPrincipalInfo();
|
2015-06-05 07:39:34 +03:00
|
|
|
|
if (pInfo) {
|
2015-07-02 15:54:00 +03:00
|
|
|
|
mPrincipalInfo = mozilla::MakeUnique<PrincipalInfo>(*pInfo);
|
2015-06-05 07:39:34 +03:00
|
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
|
|
|
|
if (!inputStream) {
|
|
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::Cached;
|
2015-06-05 07:39:34 +03:00
|
|
|
|
mRunnable->DataReceivedFromCache(mIndex, (uint8_t*)"", 0, mChannelInfo,
|
2018-05-30 22:15:35 +03:00
|
|
|
|
std::move(mPrincipalInfo), mCSPHeaderValue,
|
2017-02-23 18:54:42 +03:00
|
|
|
|
mCSPReportOnlyHeaderValue,
|
|
|
|
|
mReferrerPolicyHeaderValue);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mPump);
|
2017-06-30 20:52:06 +03:00
|
|
|
|
rv = NS_NewInputStreamPump(getter_AddRefs(mPump),
|
2017-11-22 13:18:57 +03:00
|
|
|
|
inputStream.forget(),
|
2017-06-30 20:52:06 +03:00
|
|
|
|
0, /* default segsize */
|
|
|
|
|
0, /* default segcount */
|
|
|
|
|
false, /* default closeWhenDone */
|
|
|
|
|
mMainThreadEventTarget);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
Fail(rv);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
|
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), this);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
Fail(rv);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rv = mPump->AsyncRead(loader, nullptr);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
mPump = nullptr;
|
|
|
|
|
Fail(rv);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThreadRetargetableRequest> rr = do_QueryInterface(mPump);
|
|
|
|
|
if (rr) {
|
|
|
|
|
nsCOMPtr<nsIEventTarget> sts =
|
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
|
rv = rr->RetargetDeliveryTo(sts);
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
|
NS_WARNING("Failed to dispatch the nsIInputStreamPump to a IO thread.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::ReadingFromCache;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
|
CacheScriptLoader::OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext,
|
|
|
|
|
nsresult aStatus, uint32_t aStringLen,
|
|
|
|
|
const uint8_t* aString)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
mPump = nullptr;
|
|
|
|
|
|
|
|
|
|
if (NS_FAILED(aStatus)) {
|
2016-03-22 23:22:15 +03:00
|
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::ReadingFromCache ||
|
|
|
|
|
mLoadInfo.mCacheStatus == ScriptLoadInfo::Cancel);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
Fail(aStatus);
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-22 23:22:15 +03:00
|
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::ReadingFromCache);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::Cached;
|
|
|
|
|
|
2015-06-05 07:39:34 +03:00
|
|
|
|
MOZ_ASSERT(mPrincipalInfo);
|
|
|
|
|
mRunnable->DataReceivedFromCache(mIndex, aString, aStringLen, mChannelInfo,
|
2018-05-30 22:15:35 +03:00
|
|
|
|
std::move(mPrincipalInfo), mCSPHeaderValue,
|
2017-02-23 18:54:42 +03:00
|
|
|
|
mCSPReportOnlyHeaderValue,
|
|
|
|
|
mReferrerPolicyHeaderValue);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 11:21:57 +03:00
|
|
|
|
class ChannelGetterRunnable final : public WorkerMainThreadRunnable
|
2013-03-08 02:27:21 +04:00
|
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
|
const nsAString& mScriptURL;
|
2018-01-23 18:38:54 +03:00
|
|
|
|
const ClientInfo mClientInfo;
|
2017-02-07 18:28:38 +03:00
|
|
|
|
WorkerLoadInfo& mLoadInfo;
|
2013-03-08 02:27:21 +04:00
|
|
|
|
nsresult mResult;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ChannelGetterRunnable(WorkerPrivate* aParentWorker,
|
2013-06-05 18:04:23 +04:00
|
|
|
|
const nsAString& aScriptURL,
|
2017-02-07 18:28:38 +03:00
|
|
|
|
WorkerLoadInfo& aLoadInfo)
|
2016-05-03 11:21:57 +03:00
|
|
|
|
: WorkerMainThreadRunnable(aParentWorker,
|
|
|
|
|
NS_LITERAL_CSTRING("ScriptLoader :: ChannelGetter"))
|
|
|
|
|
, mScriptURL(aScriptURL)
|
2018-05-02 16:29:26 +03:00
|
|
|
|
// ClientInfo should always be present since this should not be called
|
|
|
|
|
// if parent's status is greater than Running.
|
|
|
|
|
, mClientInfo(aParentWorker->GetClientInfo().ref())
|
2017-02-07 18:28:38 +03:00
|
|
|
|
, mLoadInfo(aLoadInfo)
|
2016-05-03 11:21:57 +03:00
|
|
|
|
, mResult(NS_ERROR_FAILURE)
|
2013-03-08 02:27:21 +04:00
|
|
|
|
{
|
2016-05-03 11:21:57 +03:00
|
|
|
|
MOZ_ASSERT(aParentWorker);
|
2013-03-08 02:27:21 +04:00
|
|
|
|
aParentWorker->AssertIsOnWorkerThread();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 11:21:57 +03:00
|
|
|
|
virtual bool
|
|
|
|
|
MainThreadRun() override
|
2013-03-08 02:27:21 +04:00
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
2017-02-07 18:28:39 +03:00
|
|
|
|
// Initialize the WorkerLoadInfo principal to our triggering principal
|
|
|
|
|
// before doing anything else. Normally we do this in the WorkerPrivate
|
|
|
|
|
// Constructor, but we can't do so off the main thread when creating
|
|
|
|
|
// a nested worker. So do it here instead.
|
2017-12-07 14:29:05 +03:00
|
|
|
|
mLoadInfo.mLoadingPrincipal = mWorkerPrivate->GetPrincipal();
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mLoadInfo.mLoadingPrincipal);
|
|
|
|
|
|
|
|
|
|
mLoadInfo.mPrincipal = mLoadInfo.mLoadingPrincipal;
|
2013-03-08 02:27:21 +04:00
|
|
|
|
|
|
|
|
|
// Figure out our base URI.
|
2016-05-03 11:21:57 +03:00
|
|
|
|
nsCOMPtr<nsIURI> baseURI = mWorkerPrivate->GetBaseURI();
|
2015-03-19 21:41:42 +03:00
|
|
|
|
MOZ_ASSERT(baseURI);
|
2013-03-08 02:27:21 +04:00
|
|
|
|
|
|
|
|
|
// May be null.
|
2016-05-03 11:21:57 +03:00
|
|
|
|
nsCOMPtr<nsIDocument> parentDoc = mWorkerPrivate->GetDocument();
|
2013-03-08 02:27:21 +04:00
|
|
|
|
|
2017-02-07 18:28:39 +03:00
|
|
|
|
mLoadInfo.mLoadGroup = mWorkerPrivate->GetLoadGroup();
|
2014-12-12 19:06:00 +03:00
|
|
|
|
|
2018-01-23 18:38:54 +03:00
|
|
|
|
Maybe<ClientInfo> clientInfo;
|
|
|
|
|
clientInfo.emplace(mClientInfo);
|
|
|
|
|
|
2013-03-08 02:27:21 +04:00
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2018-01-31 10:22:03 +03:00
|
|
|
|
mResult = workerinternals::
|
|
|
|
|
ChannelFromScriptURLMainThread(mLoadInfo.mLoadingPrincipal,
|
|
|
|
|
baseURI, parentDoc,
|
|
|
|
|
mLoadInfo.mLoadGroup,
|
|
|
|
|
mScriptURL,
|
|
|
|
|
clientInfo,
|
|
|
|
|
// Nested workers are always dedicated.
|
|
|
|
|
nsIContentPolicy::TYPE_INTERNAL_WORKER,
|
|
|
|
|
// Nested workers use default uri encoding.
|
|
|
|
|
true,
|
|
|
|
|
getter_AddRefs(channel));
|
2017-02-07 18:28:39 +03:00
|
|
|
|
NS_ENSURE_SUCCESS(mResult, true);
|
|
|
|
|
|
|
|
|
|
mResult = mLoadInfo.SetPrincipalFromChannel(channel);
|
|
|
|
|
NS_ENSURE_SUCCESS(mResult, true);
|
2013-03-08 02:27:21 +04:00
|
|
|
|
|
2017-02-07 18:28:39 +03:00
|
|
|
|
mLoadInfo.mChannel = channel.forget();
|
2016-05-03 11:21:57 +03:00
|
|
|
|
return true;
|
2013-03-08 02:27:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
|
GetResult() const
|
|
|
|
|
{
|
|
|
|
|
return mResult;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
|
private:
|
|
|
|
|
virtual ~ChannelGetterRunnable()
|
|
|
|
|
{ }
|
2013-03-08 02:27:21 +04:00
|
|
|
|
};
|
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
ScriptExecutorRunnable::ScriptExecutorRunnable(
|
|
|
|
|
ScriptLoaderRunnable& aScriptLoader,
|
2013-10-23 17:16:49 +04:00
|
|
|
|
nsIEventTarget* aSyncLoopTarget,
|
2015-03-19 21:41:42 +03:00
|
|
|
|
bool aIsWorkerScript,
|
2012-08-22 19:56:38 +04:00
|
|
|
|
uint32_t aFirstIndex,
|
|
|
|
|
uint32_t aLastIndex)
|
2013-10-23 17:16:49 +04:00
|
|
|
|
: MainThreadWorkerSyncRunnable(aScriptLoader.mWorkerPrivate, aSyncLoopTarget),
|
2015-03-19 21:41:42 +03:00
|
|
|
|
mScriptLoader(aScriptLoader), mIsWorkerScript(aIsWorkerScript),
|
|
|
|
|
mFirstIndex(aFirstIndex), mLastIndex(aLastIndex)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
|
MOZ_ASSERT(aFirstIndex <= aLastIndex);
|
|
|
|
|
MOZ_ASSERT(aLastIndex < aScriptLoader.mLoadInfos.Length());
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
|
bool
|
|
|
|
|
ScriptExecutorRunnable::IsDebuggerRunnable() const
|
|
|
|
|
{
|
|
|
|
|
// ScriptExecutorRunnable is used to execute both worker and debugger scripts.
|
|
|
|
|
// In the latter case, the runnable needs to be dispatched to the debugger
|
|
|
|
|
// queue.
|
|
|
|
|
return mScriptLoader.mWorkerScriptType == DebuggerScript;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 00:52:26 +03:00
|
|
|
|
bool
|
|
|
|
|
ScriptExecutorRunnable::PreRun(WorkerPrivate* aWorkerPrivate)
|
|
|
|
|
{
|
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
|
|
if (!mIsWorkerScript) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!aWorkerPrivate->GetJSContext()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(mFirstIndex == 0);
|
|
|
|
|
MOZ_ASSERT(!mScriptLoader.mRv.Failed());
|
|
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
|
jsapi.Init();
|
|
|
|
|
|
|
|
|
|
WorkerGlobalScope* globalScope =
|
|
|
|
|
aWorkerPrivate->GetOrCreateGlobalScope(jsapi.cx());
|
|
|
|
|
if (NS_WARN_IF(!globalScope)) {
|
|
|
|
|
NS_WARNING("Failed to make global!");
|
|
|
|
|
// There's no way to report the exception on jsapi right now, because there
|
|
|
|
|
// is no way to even enter a compartment on this thread anymore. Just clear
|
2016-05-14 03:09:50 +03:00
|
|
|
|
// the exception. We'll report some sort of error to our caller in
|
|
|
|
|
// ShutdownScriptLoader, but it will get squelched for the same reason we're
|
|
|
|
|
// squelching here: all the error reporting machinery relies on being able
|
|
|
|
|
// to enter a compartment to report the error.
|
2016-03-02 00:52:26 +03:00
|
|
|
|
jsapi.ClearException();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
bool
|
|
|
|
|
ScriptExecutorRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
|
{
|
2016-02-24 18:38:31 +03:00
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
nsTArray<ScriptLoadInfo>& loadInfos = mScriptLoader.mLoadInfos;
|
|
|
|
|
|
|
|
|
|
// Don't run if something else has already failed.
|
2012-08-22 19:56:38 +04:00
|
|
|
|
for (uint32_t index = 0; index < mFirstIndex; index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
ScriptLoadInfo& loadInfo = loadInfos.ElementAt(index);
|
|
|
|
|
|
|
|
|
|
NS_ASSERTION(!loadInfo.mChannel, "Should no longer have a channel!");
|
|
|
|
|
NS_ASSERTION(loadInfo.mExecutionScheduled, "Should be scheduled!");
|
|
|
|
|
|
|
|
|
|
if (!loadInfo.mExecutionResult) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-24 18:38:31 +03:00
|
|
|
|
// If nothing else has failed, our ErrorResult better not be a failure either.
|
|
|
|
|
MOZ_ASSERT(!mScriptLoader.mRv.Failed(), "Who failed it and why?");
|
|
|
|
|
|
2016-03-02 00:52:26 +03:00
|
|
|
|
// Slightly icky action at a distance, but there's no better place to stash
|
|
|
|
|
// this value, really.
|
|
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
2015-06-11 00:12:55 +03:00
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
|
for (uint32_t index = mFirstIndex; index <= mLastIndex; index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
ScriptLoadInfo& loadInfo = loadInfos.ElementAt(index);
|
|
|
|
|
|
|
|
|
|
NS_ASSERTION(!loadInfo.mChannel, "Should no longer have a channel!");
|
|
|
|
|
NS_ASSERTION(loadInfo.mExecutionScheduled, "Should be scheduled!");
|
|
|
|
|
NS_ASSERTION(!loadInfo.mExecutionResult, "Should not have executed yet!");
|
|
|
|
|
|
2016-02-24 18:38:31 +03:00
|
|
|
|
MOZ_ASSERT(!mScriptLoader.mRv.Failed(), "Who failed it and why?");
|
|
|
|
|
mScriptLoader.mRv.MightThrowJSException();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
if (NS_FAILED(loadInfo.mLoadResult)) {
|
2018-01-31 10:22:03 +03:00
|
|
|
|
workerinternals::ReportLoadError(mScriptLoader.mRv,
|
|
|
|
|
loadInfo.mLoadResult, loadInfo.mURL);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 23:13:04 +03:00
|
|
|
|
// If this is a top level script that succeeded, then mark the
|
2017-12-06 04:45:22 +03:00
|
|
|
|
// Client execution ready and possible controlled by a service worker.
|
2017-11-21 23:13:04 +03:00
|
|
|
|
if (mIsWorkerScript) {
|
2017-12-06 04:45:22 +03:00
|
|
|
|
if (mScriptLoader.mController.isSome()) {
|
|
|
|
|
aWorkerPrivate->Control(mScriptLoader.mController.ref());
|
|
|
|
|
}
|
2017-11-21 23:13:04 +03:00
|
|
|
|
aWorkerPrivate->ExecutionReady();
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
NS_ConvertUTF16toUTF8 filename(loadInfo.mURL);
|
|
|
|
|
|
2012-08-27 16:04:37 +04:00
|
|
|
|
JS::CompileOptions options(aCx);
|
2014-07-31 20:55:20 +04:00
|
|
|
|
options.setFileAndLine(filename.get(), 1)
|
|
|
|
|
.setNoScriptRval(true);
|
2014-04-25 18:11:57 +04:00
|
|
|
|
|
2015-07-14 18:08:38 +03:00
|
|
|
|
MOZ_ASSERT(loadInfo.mMutedErrorFlag.isSome());
|
|
|
|
|
options.setMutedErrors(loadInfo.mMutedErrorFlag.valueOr(true));
|
|
|
|
|
|
2014-04-25 18:11:57 +04:00
|
|
|
|
JS::SourceBufferHolder srcBuf(loadInfo.mScriptTextBuf,
|
|
|
|
|
loadInfo.mScriptTextLength,
|
|
|
|
|
JS::SourceBufferHolder::GiveOwnership);
|
|
|
|
|
loadInfo.mScriptTextBuf = nullptr;
|
|
|
|
|
loadInfo.mScriptTextLength = 0;
|
|
|
|
|
|
2016-02-24 18:38:31 +03:00
|
|
|
|
// Our ErrorResult still shouldn't be a failure.
|
|
|
|
|
MOZ_ASSERT(!mScriptLoader.mRv.Failed(), "Who failed it and why?");
|
2014-11-19 07:27:16 +03:00
|
|
|
|
JS::Rooted<JS::Value> unused(aCx);
|
2015-03-14 08:36:16 +03:00
|
|
|
|
if (!JS::Evaluate(aCx, options, srcBuf, &unused)) {
|
2016-02-24 18:38:31 +03:00
|
|
|
|
mScriptLoader.mRv.StealExceptionFromJSContext(aCx);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadInfo.mExecutionResult = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ScriptExecutorRunnable::PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
|
bool aRunResult)
|
|
|
|
|
{
|
2016-02-24 18:38:31 +03:00
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(aCx), "Who left an exception on there?");
|
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
nsTArray<ScriptLoadInfo>& loadInfos = mScriptLoader.mLoadInfos;
|
|
|
|
|
|
|
|
|
|
if (mLastIndex == loadInfos.Length() - 1) {
|
|
|
|
|
// All done. If anything failed then return false.
|
|
|
|
|
bool result = true;
|
2015-10-15 15:06:55 +03:00
|
|
|
|
bool mutedError = false;
|
2012-08-22 19:56:38 +04:00
|
|
|
|
for (uint32_t index = 0; index < loadInfos.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
if (!loadInfos[index].mExecutionResult) {
|
2016-02-24 18:38:31 +03:00
|
|
|
|
mutedError = loadInfos[index].mMutedErrorFlag.valueOr(true);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
result = false;
|
2016-02-24 18:38:31 +03:00
|
|
|
|
break;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-24 18:38:31 +03:00
|
|
|
|
// The only way we can get here with "result" false but without
|
|
|
|
|
// mScriptLoader.mRv being a failure is if we're loading the main worker
|
|
|
|
|
// script and GetOrCreateGlobalScope() fails. In that case we would have
|
|
|
|
|
// returned false from WorkerRun, so assert that.
|
|
|
|
|
MOZ_ASSERT_IF(!result && !mScriptLoader.mRv.Failed(),
|
2016-02-24 18:38:31 +03:00
|
|
|
|
!aRunResult);
|
2016-02-24 18:38:31 +03:00
|
|
|
|
ShutdownScriptLoader(aCx, aWorkerPrivate, result, mutedError);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
|
nsresult
|
2014-01-11 04:37:47 +04:00
|
|
|
|
ScriptExecutorRunnable::Cancel()
|
|
|
|
|
{
|
2014-05-20 16:34:25 +04:00
|
|
|
|
if (mLastIndex == mScriptLoader.mLoadInfos.Length() - 1) {
|
2015-10-15 15:06:55 +03:00
|
|
|
|
ShutdownScriptLoader(mWorkerPrivate->GetJSContext(), mWorkerPrivate,
|
2016-02-24 18:38:31 +03:00
|
|
|
|
false, false);
|
2014-05-20 16:34:25 +04:00
|
|
|
|
}
|
2014-01-13 21:41:02 +04:00
|
|
|
|
return MainThreadWorkerSyncRunnable::Cancel();
|
2014-01-11 04:37:47 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ScriptExecutorRunnable::ShutdownScriptLoader(JSContext* aCx,
|
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
2015-10-15 15:06:55 +03:00
|
|
|
|
bool aResult,
|
|
|
|
|
bool aMutedError)
|
2014-01-11 04:37:47 +04:00
|
|
|
|
{
|
2016-02-24 18:38:31 +03:00
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
|
MOZ_ASSERT(mLastIndex == mScriptLoader.mLoadInfos.Length() - 1);
|
|
|
|
|
|
2018-06-05 20:49:13 +03:00
|
|
|
|
if (mIsWorkerScript) {
|
2015-03-19 21:41:42 +03:00
|
|
|
|
aWorkerPrivate->SetLoadingWorkerScript(false);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 15:06:55 +03:00
|
|
|
|
if (!aResult) {
|
2016-02-24 18:38:31 +03:00
|
|
|
|
// At this point there are two possibilities:
|
2016-02-24 18:38:31 +03:00
|
|
|
|
//
|
|
|
|
|
// 1) mScriptLoader.mRv.Failed(). In that case we just want to leave it
|
|
|
|
|
// as-is, except if it has a JS exception and we need to mute JS
|
|
|
|
|
// exceptions. In that case, we log the exception without firing any
|
2016-02-24 18:38:31 +03:00
|
|
|
|
// events and then replace it on the ErrorResult with a NetworkError,
|
|
|
|
|
// per spec.
|
2016-02-24 18:38:31 +03:00
|
|
|
|
//
|
2016-02-24 18:38:31 +03:00
|
|
|
|
// 2) mScriptLoader.mRv succeeded. As far as I can tell, this can only
|
|
|
|
|
// happen when loading the main worker script and
|
|
|
|
|
// GetOrCreateGlobalScope() fails or if ScriptExecutorRunnable::Cancel
|
|
|
|
|
// got called. Does it matter what we throw in this case? I'm not
|
|
|
|
|
// sure...
|
2016-02-24 18:38:31 +03:00
|
|
|
|
if (mScriptLoader.mRv.Failed()) {
|
|
|
|
|
if (aMutedError && mScriptLoader.mRv.IsJSException()) {
|
|
|
|
|
LogExceptionToConsole(aCx, aWorkerPrivate);
|
2017-02-28 22:49:03 +03:00
|
|
|
|
mScriptLoader.mRv.ThrowWithCustomCleanup(NS_ERROR_DOM_NETWORK_ERR);
|
2016-02-24 18:38:31 +03:00
|
|
|
|
}
|
2015-10-15 15:06:55 +03:00
|
|
|
|
} else {
|
2017-02-28 22:49:03 +03:00
|
|
|
|
mScriptLoader.mRv.ThrowWithCustomCleanup(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2015-10-15 15:06:55 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-11 04:37:47 +04:00
|
|
|
|
aWorkerPrivate->StopSyncLoop(mSyncLoopTarget, aResult);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 06:05:00 +03:00
|
|
|
|
void
|
|
|
|
|
ScriptExecutorRunnable::LogExceptionToConsole(JSContext* aCx,
|
|
|
|
|
WorkerPrivate* aWorkerPrivate)
|
|
|
|
|
{
|
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
2016-02-24 18:38:31 +03:00
|
|
|
|
MOZ_ASSERT(mScriptLoader.mRv.IsJSException());
|
|
|
|
|
|
2015-10-15 06:05:00 +03:00
|
|
|
|
JS::Rooted<JS::Value> exn(aCx);
|
2016-02-24 18:38:31 +03:00
|
|
|
|
if (!ToJSValue(aCx, mScriptLoader.mRv, &exn)) {
|
2015-10-15 06:05:00 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-24 18:38:31 +03:00
|
|
|
|
// Now the exception state should all be in exn.
|
|
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(aCx));
|
|
|
|
|
MOZ_ASSERT(!mScriptLoader.mRv.Failed());
|
2015-10-15 06:05:00 +03:00
|
|
|
|
|
|
|
|
|
js::ErrorReport report(aCx);
|
2016-04-20 02:29:21 +03:00
|
|
|
|
if (!report.init(aCx, exn, js::ErrorReport::WithSideEffects)) {
|
2015-10-15 06:05:00 +03:00
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<xpc::ErrorReport> xpcReport = new xpc::ErrorReport();
|
2016-08-14 14:39:31 +03:00
|
|
|
|
xpcReport->Init(report.report(), report.toStringResult().c_str(),
|
2015-10-15 06:05:00 +03:00
|
|
|
|
aWorkerPrivate->IsChromeWorker(), aWorkerPrivate->WindowID());
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<AsyncErrorReporter> r = new AsyncErrorReporter(xpcReport);
|
2015-10-15 06:05:00 +03:00
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 15:06:55 +03:00
|
|
|
|
void
|
2016-02-29 22:52:42 +03:00
|
|
|
|
LoadAllScripts(WorkerPrivate* aWorkerPrivate,
|
2015-03-17 13:15:19 +03:00
|
|
|
|
nsTArray<ScriptLoadInfo>& aLoadInfos, bool aIsMainScript,
|
2015-10-15 15:06:55 +03:00
|
|
|
|
WorkerScriptType aWorkerScriptType, ErrorResult& aRv)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
{
|
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
NS_ASSERTION(!aLoadInfos.IsEmpty(), "Bad arguments!");
|
|
|
|
|
|
2018-07-12 20:33:41 +03:00
|
|
|
|
AutoSyncLoopHolder syncLoop(aWorkerPrivate, Canceling);
|
2017-01-05 12:05:32 +03:00
|
|
|
|
nsCOMPtr<nsIEventTarget> syncLoopTarget = syncLoop.GetEventTarget();
|
|
|
|
|
if (!syncLoopTarget) {
|
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2018-01-23 18:38:54 +03:00
|
|
|
|
Maybe<ClientInfo> clientInfo;
|
|
|
|
|
Maybe<ServiceWorkerDescriptor> controller;
|
|
|
|
|
if (!aIsMainScript) {
|
2018-05-02 16:29:26 +03:00
|
|
|
|
clientInfo = aWorkerPrivate->GetClientInfo();
|
2018-01-23 18:38:54 +03:00
|
|
|
|
controller = aWorkerPrivate->GetController();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<ScriptLoaderRunnable> loader =
|
2017-01-05 12:05:32 +03:00
|
|
|
|
new ScriptLoaderRunnable(aWorkerPrivate, syncLoopTarget, aLoadInfos,
|
2018-01-23 18:38:54 +03:00
|
|
|
|
clientInfo, controller,
|
2017-01-05 12:05:32 +03:00
|
|
|
|
aIsMainScript, aWorkerScriptType, aRv);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
|
|
NS_ASSERTION(aLoadInfos.IsEmpty(), "Should have swapped!");
|
|
|
|
|
|
2018-06-19 20:08:12 +03:00
|
|
|
|
RefPtr<StrongWorkerRef> workerRef =
|
|
|
|
|
StrongWorkerRef::Create(aWorkerPrivate, "ScriptLoader", [loader]() {
|
|
|
|
|
NS_DispatchToMainThread(NewRunnableMethod("ScriptLoader::CancelMainThreadWithBindingAborted",
|
|
|
|
|
loader,
|
|
|
|
|
&ScriptLoaderRunnable::CancelMainThreadWithBindingAborted));
|
|
|
|
|
});
|
2016-07-18 10:12:40 +03:00
|
|
|
|
|
2018-06-19 20:08:12 +03:00
|
|
|
|
if (NS_WARN_IF(!workerRef)) {
|
2015-10-15 15:06:55 +03:00
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
|
return;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-23 23:53:17 +04:00
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(loader))) {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
NS_ERROR("Failed to dispatch!");
|
2015-10-15 15:06:55 +03:00
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
|
return;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 15:06:55 +03:00
|
|
|
|
syncLoop.Run();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} /* anonymous namespace */
|
|
|
|
|
|
2018-01-31 10:22:03 +03:00
|
|
|
|
namespace workerinternals {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2013-03-08 02:27:21 +04:00
|
|
|
|
nsresult
|
|
|
|
|
ChannelFromScriptURLMainThread(nsIPrincipal* aPrincipal,
|
|
|
|
|
nsIURI* aBaseURI,
|
|
|
|
|
nsIDocument* aParentDoc,
|
2014-12-12 19:06:00 +03:00
|
|
|
|
nsILoadGroup* aLoadGroup,
|
2013-06-05 18:04:23 +04:00
|
|
|
|
const nsAString& aScriptURL,
|
2018-01-23 18:38:54 +03:00
|
|
|
|
const Maybe<ClientInfo>& aClientInfo,
|
2017-02-15 17:55:58 +03:00
|
|
|
|
nsContentPolicyType aMainScriptContentPolicyType,
|
2016-04-28 10:44:08 +03:00
|
|
|
|
bool aDefaultURIEncoding,
|
2013-03-08 02:27:21 +04:00
|
|
|
|
nsIChannel** aChannel)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIOService> ios(do_GetIOService());
|
|
|
|
|
|
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
|
NS_ASSERTION(secMan, "This should never be null!");
|
|
|
|
|
|
2018-01-24 19:17:31 +03:00
|
|
|
|
return ChannelFromScriptURL(aPrincipal, aBaseURI, aParentDoc, nullptr,
|
|
|
|
|
aLoadGroup, ios, secMan, aScriptURL, aClientInfo,
|
2018-01-23 18:38:54 +03:00
|
|
|
|
Maybe<ServiceWorkerDescriptor>(),
|
|
|
|
|
true, WorkerScript, aMainScriptContentPolicyType,
|
2017-02-15 17:55:58 +03:00
|
|
|
|
nsIRequest::LOAD_NORMAL, aDefaultURIEncoding,
|
|
|
|
|
aChannel);
|
2013-03-08 02:27:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
|
ChannelFromScriptURLWorkerThread(JSContext* aCx,
|
|
|
|
|
WorkerPrivate* aParent,
|
2013-06-05 18:04:23 +04:00
|
|
|
|
const nsAString& aScriptURL,
|
2017-02-07 18:28:38 +03:00
|
|
|
|
WorkerLoadInfo& aLoadInfo)
|
2013-03-08 02:27:21 +04:00
|
|
|
|
{
|
|
|
|
|
aParent->AssertIsOnWorkerThread();
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<ChannelGetterRunnable> getter =
|
2017-02-07 18:28:38 +03:00
|
|
|
|
new ChannelGetterRunnable(aParent, aScriptURL, aLoadInfo);
|
2013-03-08 02:27:21 +04:00
|
|
|
|
|
2016-05-03 11:21:57 +03:00
|
|
|
|
ErrorResult rv;
|
2018-07-12 20:33:41 +03:00
|
|
|
|
getter->Dispatch(Canceling, rv);
|
2016-05-03 11:21:57 +03:00
|
|
|
|
if (rv.Failed()) {
|
2013-03-08 02:27:21 +04:00
|
|
|
|
NS_ERROR("Failed to dispatch!");
|
2016-05-03 11:21:57 +03:00
|
|
|
|
return rv.StealNSResult();
|
2013-03-08 02:27:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return getter->GetResult();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 22:52:42 +03:00
|
|
|
|
void ReportLoadError(ErrorResult& aRv, nsresult aLoadResult,
|
2016-02-26 23:23:13 +03:00
|
|
|
|
const nsAString& aScriptURL)
|
2013-03-08 02:27:21 +04:00
|
|
|
|
{
|
2016-02-24 18:38:31 +03:00
|
|
|
|
MOZ_ASSERT(!aRv.Failed());
|
2013-03-08 02:27:21 +04:00
|
|
|
|
|
2016-02-24 18:38:31 +03:00
|
|
|
|
switch (aLoadResult) {
|
2013-03-08 02:27:21 +04:00
|
|
|
|
case NS_ERROR_FILE_NOT_FOUND:
|
|
|
|
|
case NS_ERROR_NOT_AVAILABLE:
|
2016-02-26 23:23:13 +03:00
|
|
|
|
aLoadResult = NS_ERROR_DOM_NETWORK_ERR;
|
2013-03-08 02:27:21 +04:00
|
|
|
|
break;
|
|
|
|
|
|
2015-08-30 20:31:52 +03:00
|
|
|
|
case NS_ERROR_MALFORMED_URI:
|
|
|
|
|
aLoadResult = NS_ERROR_DOM_SYNTAX_ERR;
|
2016-02-26 23:23:13 +03:00
|
|
|
|
break;
|
|
|
|
|
|
2016-02-24 18:38:31 +03:00
|
|
|
|
case NS_BINDING_ABORTED:
|
|
|
|
|
// Note: we used to pretend like we didn't set an exception for
|
|
|
|
|
// NS_BINDING_ABORTED, but then ShutdownScriptLoader did it anyway. The
|
|
|
|
|
// other callsite, in WorkerPrivate::Constructor, never passed in
|
|
|
|
|
// NS_BINDING_ABORTED. So just throw it directly here. Consumers will
|
2016-02-26 23:23:13 +03:00
|
|
|
|
// deal as needed. But note that we do NOT want to ThrowDOMException()
|
|
|
|
|
// for this case, because that will make it impossible for consumers to
|
|
|
|
|
// realize that our error was NS_BINDING_ABORTED.
|
|
|
|
|
aRv.Throw(aLoadResult);
|
|
|
|
|
return;
|
|
|
|
|
|
2013-03-08 02:27:21 +04:00
|
|
|
|
case NS_ERROR_DOM_SECURITY_ERR:
|
|
|
|
|
case NS_ERROR_DOM_SYNTAX_ERR:
|
2016-02-26 23:23:13 +03:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NS_ERROR_DOM_BAD_URI:
|
|
|
|
|
// This is actually a security error.
|
|
|
|
|
aLoadResult = NS_ERROR_DOM_SECURITY_ERR;
|
2013-03-08 02:27:21 +04:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2016-02-26 23:23:13 +03:00
|
|
|
|
// For lack of anything better, go ahead and throw a NetworkError here.
|
|
|
|
|
// We don't want to throw a JS exception, because for toplevel script
|
|
|
|
|
// loads that would get squelched.
|
|
|
|
|
aRv.ThrowDOMException(NS_ERROR_DOM_NETWORK_ERR,
|
2016-12-14 19:32:21 +03:00
|
|
|
|
nsPrintfCString("Failed to load worker script at %s (nsresult = 0x%" PRIx32 ")",
|
2016-02-26 23:23:13 +03:00
|
|
|
|
NS_ConvertUTF16toUTF8(aScriptURL).get(),
|
2016-12-14 19:32:21 +03:00
|
|
|
|
static_cast<uint32_t>(aLoadResult)));
|
2016-02-26 23:23:13 +03:00
|
|
|
|
return;
|
2013-03-08 02:27:21 +04:00
|
|
|
|
}
|
2016-02-26 23:23:13 +03:00
|
|
|
|
|
|
|
|
|
aRv.ThrowDOMException(aLoadResult,
|
|
|
|
|
NS_LITERAL_CSTRING("Failed to load worker script at \"") +
|
|
|
|
|
NS_ConvertUTF16toUTF8(aScriptURL) +
|
|
|
|
|
NS_LITERAL_CSTRING("\""));
|
2013-03-08 02:27:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 15:06:55 +03:00
|
|
|
|
void
|
2016-02-29 22:52:42 +03:00
|
|
|
|
LoadMainScript(WorkerPrivate* aWorkerPrivate,
|
|
|
|
|
const nsAString& aScriptURL,
|
2015-10-15 15:06:55 +03:00
|
|
|
|
WorkerScriptType aWorkerScriptType,
|
|
|
|
|
ErrorResult& aRv)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
{
|
|
|
|
|
nsTArray<ScriptLoadInfo> loadInfos;
|
|
|
|
|
|
|
|
|
|
ScriptLoadInfo* info = loadInfos.AppendElement();
|
2015-03-17 13:15:19 +03:00
|
|
|
|
info->mURL = aScriptURL;
|
2017-01-04 12:09:00 +03:00
|
|
|
|
info->mLoadFlags = aWorkerPrivate->GetLoadFlags();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2017-11-21 23:13:04 +03:00
|
|
|
|
// We are loading the main script, so the worker's Client must be
|
|
|
|
|
// reserved.
|
2018-05-02 16:29:26 +03:00
|
|
|
|
info->mReservedClientInfo = aWorkerPrivate->GetClientInfo();
|
2017-11-21 23:13:04 +03:00
|
|
|
|
|
2016-02-29 22:52:42 +03:00
|
|
|
|
LoadAllScripts(aWorkerPrivate, loadInfos, true, aWorkerScriptType, aRv);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
|
void
|
2016-02-29 22:52:42 +03:00
|
|
|
|
Load(WorkerPrivate* aWorkerPrivate,
|
2015-03-31 13:22:40 +03:00
|
|
|
|
const nsTArray<nsString>& aScriptURLs, WorkerScriptType aWorkerScriptType,
|
|
|
|
|
ErrorResult& aRv)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
{
|
2013-11-05 18:16:26 +04:00
|
|
|
|
const uint32_t urlCount = aScriptURLs.Length();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
|
if (!urlCount) {
|
|
|
|
|
return;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
|
if (urlCount > MAX_CONCURRENT_SCRIPTS) {
|
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
return;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsTArray<ScriptLoadInfo> loadInfos;
|
2013-11-05 18:16:26 +04:00
|
|
|
|
loadInfos.SetLength(urlCount);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
|
for (uint32_t index = 0; index < urlCount; index++) {
|
|
|
|
|
loadInfos[index].mURL = aScriptURLs[index];
|
2017-01-04 12:09:00 +03:00
|
|
|
|
loadInfos[index].mLoadFlags = aWorkerPrivate->GetLoadFlags();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 22:52:42 +03:00
|
|
|
|
LoadAllScripts(aWorkerPrivate, loadInfos, false, aWorkerScriptType, aRv);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 10:22:03 +03:00
|
|
|
|
} // namespace workerinternals
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
2018-01-31 10:22:03 +03:00
|
|
|
|
} // dom namespace
|
|
|
|
|
} // mozilla namespace
|