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"
|
2016-06-30 07:31:59 +03:00
|
|
|
#include "nsIDOMDocument.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"
|
|
|
|
#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"
|
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"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsScriptLoader.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"
|
|
|
|
#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"
|
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"
|
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"
|
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"
|
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
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
2016-03-16 21:51:11 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
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
|
|
|
|
|
|
|
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,
|
|
|
|
nsILoadGroup* loadGroup,
|
|
|
|
nsIIOService* ios,
|
|
|
|
nsIScriptSecurityManager* secMan,
|
2013-06-05 18:04:23 +04:00
|
|
|
const nsAString& aScriptURL,
|
2015-03-17 13:15:19 +03:00
|
|
|
bool aIsMainScript,
|
|
|
|
WorkerScriptType aWorkerScriptType,
|
2015-06-17 04:21:08 +03:00
|
|
|
nsContentPolicyType aContentPolicyType,
|
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
|
|
|
|
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.
|
|
|
|
bool isData = false;
|
|
|
|
if (aIsMainScript && NS_SUCCEEDED(uri->SchemeIs("data", &isData)) && isData) {
|
|
|
|
secFlags = nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL;
|
|
|
|
}
|
2015-11-16 22:13:43 +03:00
|
|
|
|
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,
|
2015-06-17 04:21:08 +03:00
|
|
|
aContentPolicyType,
|
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);
|
|
|
|
} 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));
|
|
|
|
|
2014-09-21 20:39:57 +04:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
uri,
|
2014-12-12 19:06:00 +03:00
|
|
|
principal,
|
2015-11-17 01:41:03 +03:00
|
|
|
secFlags,
|
2015-06-17 04:21:08 +03:00
|
|
|
aContentPolicyType,
|
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);
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
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
|
|
|
bool
|
|
|
|
ReadyToExecute()
|
|
|
|
{
|
|
|
|
return !mChannel && NS_SUCCEEDED(mLoadResult) && !mExecutionScheduled;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
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());
|
2015-03-20 02:39:01 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate->LoadScriptAsPartOfLoadingServiceWorkerScript());
|
2015-03-19 21:41:42 +03:00
|
|
|
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
|
|
|
|
, public nsIStreamLoaderObserver
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
|
|
|
|
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;
|
|
|
|
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;
|
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)
|
|
|
|
|
2016-07-18 10:12:40 +03:00
|
|
|
class ScriptLoaderHolder;
|
|
|
|
|
|
|
|
class ScriptLoaderRunnable final : public nsIRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
friend class ScriptExecutorRunnable;
|
2016-07-18 10:12:40 +03:00
|
|
|
friend class ScriptLoaderHolder;
|
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;
|
2015-03-17 13:15:19 +03:00
|
|
|
bool mIsMainScript;
|
|
|
|
WorkerScriptType mWorkerScriptType;
|
2011-07-17 23:09:13 +04:00
|
|
|
bool mCanceled;
|
|
|
|
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,
|
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),
|
2015-03-17 13:15:19 +03:00
|
|
|
mIsMainScript(aIsMainScript), mWorkerScriptType(aWorkerScriptType),
|
2015-10-15 15:06:55 +03:00
|
|
|
mCanceled(false), 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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
MOZ_ASSERT(channel == loadInfo.mChannel);
|
|
|
|
|
|
|
|
// 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);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ir->SetPrincipalInfo(Move(principalInfo));
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<mozilla::dom::Response> response =
|
2015-07-02 15:54:00 +03:00
|
|
|
new mozilla::dom::Response(mCacheCreator->Global(), ir);
|
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());
|
|
|
|
|
|
|
|
ErrorResult error;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> cachePromise =
|
2015-04-24 02:10:57 +03:00
|
|
|
mCacheCreator->Cache_()->Put(request, *response, error);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-07-18 10:12:40 +03:00
|
|
|
bool
|
|
|
|
Notify(Status aStatus)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (aStatus >= Terminating && !mCanceled) {
|
|
|
|
mCanceled = true;
|
|
|
|
|
2016-05-05 11:45:00 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
NS_DispatchToMainThread(NewRunnableMethod(this,
|
|
|
|
&ScriptLoaderRunnable::CancelMainThreadWithBindingAborted)));
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
bool
|
|
|
|
IsMainWorkerScript() const
|
|
|
|
{
|
|
|
|
return mIsMainScript && mWorkerScriptType == WorkerScript;
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
void
|
2016-02-24 18:38:31 +03:00
|
|
|
CancelMainThreadWithBindingAborted()
|
|
|
|
{
|
|
|
|
CancelMainThread(NS_BINDING_ABORTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
if (IsMainWorkerScript() && mWorkerPrivate->IsServiceWorker()) {
|
|
|
|
mWorkerPrivate->SetLoadingWorkerScript(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mWorkerPrivate->IsServiceWorker() ||
|
2015-03-20 02:39:01 +03:00
|
|
|
!mWorkerPrivate->LoadScriptAsPartOfLoadingServiceWorkerScript()) {
|
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());
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
WorkerPrivate* parentWorker = mWorkerPrivate->GetParent();
|
|
|
|
|
|
|
|
nsIPrincipal* principal = 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);
|
2014-12-12 19:06:00 +03:00
|
|
|
MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(loadGroup, principal));
|
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());
|
2015-03-19 21:41:42 +03:00
|
|
|
rv = ChannelFromScriptURL(principal, baseURI, parentDoc, loadGroup, ios,
|
|
|
|
secMan, loadInfo.mURL, 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
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
httpChannel->GetResponseHeader(
|
|
|
|
NS_LITERAL_CSTRING("content-security-policy"),
|
|
|
|
tCspHeaderValue);
|
|
|
|
|
|
|
|
httpChannel->GetResponseHeader(
|
|
|
|
NS_LITERAL_CSTRING("content-security-policy-report-only"),
|
|
|
|
tCspROHeaderValue);
|
2016-07-19 10:38:26 +03:00
|
|
|
|
|
|
|
httpChannel->GetResponseHeader(
|
|
|
|
NS_LITERAL_CSTRING("referrer-policy"),
|
|
|
|
tRPHeaderCValue);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// May be null.
|
|
|
|
nsIDocument* parentDoc = mWorkerPrivate->GetDocument();
|
|
|
|
|
|
|
|
// Use the regular nsScriptLoader for this grunt work! Should be just fine
|
|
|
|
// 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.
|
2011-07-17 23:09:13 +04:00
|
|
|
rv = nsScriptLoader::ConvertToUTF16(aLoadInfo.mChannel, aString, aStringLen,
|
2013-10-10 02:23:47 +04:00
|
|
|
NS_LITERAL_STRING("UTF-8"), parentDoc,
|
2014-04-25 18:11:57 +04:00
|
|
|
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));
|
|
|
|
MOZ_ASSERT(wcsp, "We sould have a CSP for the worker here");
|
|
|
|
if (wcsp) {
|
|
|
|
wcsp->LogViolationDetails(
|
|
|
|
nsIContentSecurityPolicy::VIOLATION_TYPE_REQUIRE_SRI_FOR_SCRIPT,
|
|
|
|
aLoadInfo.mURL, EmptyString(), 0, EmptyString(), EmptyString());
|
|
|
|
}
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
if (!mWorkerPrivate->GetCSP() && CSPService::sCSPEnabled) {
|
|
|
|
NS_ConvertASCIItoUTF16 cspHeaderValue(tCspHeaderValue);
|
|
|
|
NS_ConvertASCIItoUTF16 cspROHeaderValue(tCspROHeaderValue);
|
|
|
|
|
|
|
|
nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
|
|
|
|
MOZ_ASSERT(principal, "Should not be null");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
rv = principal->EnsureCSP(nullptr, getter_AddRefs(csp));
|
|
|
|
|
|
|
|
if (csp) {
|
|
|
|
// If there's a CSP header, apply it.
|
|
|
|
if (!cspHeaderValue.IsEmpty()) {
|
|
|
|
rv = CSP_AppendCSPFromHeader(csp, cspHeaderValue, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
// If there's a report-only CSP header, apply it.
|
|
|
|
if (!cspROHeaderValue.IsEmpty()) {
|
|
|
|
rv = CSP_AppendCSPFromHeader(csp, cspROHeaderValue, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set evalAllowed, default value is set in GetAllowsEval
|
|
|
|
bool evalAllowed = false;
|
|
|
|
bool reportEvalViolations = false;
|
|
|
|
rv = csp->GetAllowsEval(&reportEvalViolations, &evalAllowed);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mWorkerPrivate->SetCSP(csp);
|
|
|
|
mWorkerPrivate->SetEvalAllowed(evalAllowed);
|
|
|
|
mWorkerPrivate->SetReportCSPViolations(reportEvalViolations);
|
2016-07-05 06:47:13 +03:00
|
|
|
|
|
|
|
// Set ReferrerPolicy, default value is set in GetReferrerPolicy
|
|
|
|
bool hasReferrerPolicy = false;
|
2017-01-05 06:30:07 +03:00
|
|
|
uint32_t rp = mozilla::net::RP_Unset;
|
2016-07-05 06:47:13 +03:00
|
|
|
rv = csp->GetReferrerPolicy(&rp, &hasReferrerPolicy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-10-04 03:36:00 +03:00
|
|
|
|
|
|
|
if (hasReferrerPolicy) { //FIXME bug 1307366: move RP out of CSP code
|
2016-07-05 06:47:13 +03:00
|
|
|
mWorkerPrivate->SetReferrerPolicy(static_cast<net::ReferrerPolicy>(rp));
|
|
|
|
}
|
2016-06-30 07:31:59 +03:00
|
|
|
}
|
|
|
|
}
|
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());
|
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
}
|
|
|
|
|
2016-07-19 10:38:26 +03:00
|
|
|
NS_ConvertUTF8toUTF16 tRPHeaderValue(tRPHeaderCValue);
|
|
|
|
// If there's a Referrer-Policy header, apply it.
|
|
|
|
if (!tRPHeaderValue.IsEmpty()) {
|
|
|
|
net::ReferrerPolicy policy =
|
|
|
|
nsContentUtils::GetReferrerPolicyFromHeader(tRPHeaderValue);
|
|
|
|
if (policy != net::RP_Unset) {
|
|
|
|
mWorkerPrivate->SetReferrerPolicy(policy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2015-06-05 07:39:34 +03:00
|
|
|
UniquePtr<PrincipalInfo> aPrincipalInfo)
|
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);
|
|
|
|
|
|
|
|
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 =
|
2015-03-19 21:41:42 +03:00
|
|
|
nsScriptLoader::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
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
mozilla::DebugOnly<nsIPrincipal*> principal = mWorkerPrivate->GetPrincipal();
|
2015-04-11 00:14:44 +03:00
|
|
|
MOZ_ASSERT(principal);
|
|
|
|
nsILoadGroup* loadGroup = mWorkerPrivate->GetLoadGroup();
|
|
|
|
MOZ_ASSERT(loadGroup);
|
2015-06-05 07:39:34 +03:00
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
mozilla::DebugOnly<bool> equal = false;
|
2015-06-05 07:39:34 +03:00
|
|
|
MOZ_ASSERT(responsePrincipal && NS_SUCCEEDED(responsePrincipal->Equals(principal, &equal)));
|
|
|
|
MOZ_ASSERT(equal);
|
|
|
|
|
2015-05-25 21:21:05 +03:00
|
|
|
mWorkerPrivate->InitChannelInfo(aChannelInfo);
|
2017-02-07 18:28:39 +03:00
|
|
|
mWorkerPrivate->SetPrincipalOnMainThread(responsePrincipal, loadGroup);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-17 01:41:03 +03:00
|
|
|
NS_IMPL_ISUPPORTS(ScriptLoaderRunnable, nsIRunnable)
|
|
|
|
|
2016-07-18 10:12:40 +03:00
|
|
|
class MOZ_STACK_CLASS ScriptLoaderHolder final : public WorkerHolder
|
|
|
|
{
|
|
|
|
// Raw pointer because this holder object follows the mRunnable life-time.
|
|
|
|
ScriptLoaderRunnable* mRunnable;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ScriptLoaderHolder(ScriptLoaderRunnable* aRunnable)
|
|
|
|
: mRunnable(aRunnable)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
Notify(Status aStatus) override
|
|
|
|
{
|
|
|
|
mRunnable->Notify(aStatus);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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!");
|
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
mozilla::AutoSafeJSContext 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;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
false, /* privateBrowsing can't be true here */
|
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;
|
|
|
|
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.
|
|
|
|
IgnoredErrorResult rv;
|
|
|
|
RefPtr<Promise> promise = mCacheStorage->Delete(mCacheName, rv);
|
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
|
|
|
|
|
|
|
ErrorResult error;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = aCache->Match(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;
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
if (aValue.isUndefined()) {
|
|
|
|
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;
|
2015-11-17 01:41:03 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
Move(mPrincipalInfo));
|
2015-03-19 21:41:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mPump);
|
|
|
|
rv = NS_NewInputStreamPump(getter_AddRefs(mPump), inputStream);
|
|
|
|
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,
|
|
|
|
Move(mPrincipalInfo));
|
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;
|
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)
|
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.
|
|
|
|
mLoadInfo.mPrincipal = mWorkerPrivate->GetPrincipal();
|
|
|
|
MOZ_ASSERT(mLoadInfo.mPrincipal);
|
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
|
|
|
|
2013-03-08 02:27:21 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
mResult =
|
2017-02-07 18:28:39 +03:00
|
|
|
scriptloader::ChannelFromScriptURLMainThread(mLoadInfo.mPrincipal,
|
|
|
|
baseURI, parentDoc,
|
|
|
|
mLoadInfo.mLoadGroup,
|
2014-12-12 19:06:00 +03:00
|
|
|
mScriptURL,
|
2015-06-17 04:21:08 +03:00
|
|
|
// Nested workers are always dedicated.
|
|
|
|
nsIContentPolicy::TYPE_INTERNAL_WORKER,
|
2016-04-28 10:44:08 +03:00
|
|
|
// Nested workers use default uri encoding.
|
|
|
|
true,
|
2013-03-08 02:27:21 +04:00
|
|
|
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)) {
|
2016-02-29 22:52:42 +03:00
|
|
|
scriptloader::ReportLoadError(mScriptLoader.mRv,
|
2016-02-26 23:23:13 +03:00
|
|
|
loadInfo.mLoadResult, loadInfo.mURL);
|
2015-08-30 20:31:52 +03:00
|
|
|
// Top level scripts only!
|
|
|
|
if (mIsWorkerScript) {
|
|
|
|
aWorkerPrivate->MaybeDispatchLoadFailedRunnable();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-17 13:15:19 +03:00
|
|
|
if (mScriptLoader.mWorkerScriptType == DebuggerScript) {
|
|
|
|
options.setVersion(JSVERSION_LATEST);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (mIsWorkerScript && aWorkerPrivate->IsServiceWorker()) {
|
|
|
|
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);
|
2016-02-24 18:38:31 +03:00
|
|
|
mScriptLoader.mRv.Throw(NS_ERROR_DOM_NETWORK_ERR);
|
2016-02-24 18:38:31 +03:00
|
|
|
}
|
2015-10-15 15:06:55 +03:00
|
|
|
} else {
|
|
|
|
mScriptLoader.mRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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!");
|
|
|
|
|
2017-01-05 12:05:32 +03:00
|
|
|
AutoSyncLoopHolder syncLoop(aWorkerPrivate, Terminating);
|
|
|
|
nsCOMPtr<nsIEventTarget> syncLoopTarget = syncLoop.GetEventTarget();
|
|
|
|
if (!syncLoopTarget) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ScriptLoaderRunnable> loader =
|
2017-01-05 12:05:32 +03:00
|
|
|
new ScriptLoaderRunnable(aWorkerPrivate, syncLoopTarget, aLoadInfos,
|
|
|
|
aIsMainScript, aWorkerScriptType, aRv);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(aLoadInfos.IsEmpty(), "Should have swapped!");
|
|
|
|
|
2016-07-18 10:12:40 +03:00
|
|
|
ScriptLoaderHolder workerHolder(loader);
|
|
|
|
|
2016-08-18 17:11:04 +03:00
|
|
|
if (NS_WARN_IF(!workerHolder.HoldWorker(aWorkerPrivate, Terminating))) {
|
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 */
|
|
|
|
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace scriptloader {
|
|
|
|
|
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,
|
2015-06-17 04:21:08 +03:00
|
|
|
nsContentPolicyType aContentPolicyType,
|
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!");
|
|
|
|
|
2014-12-12 19:06:00 +03:00
|
|
|
return ChannelFromScriptURL(aPrincipal, aBaseURI, aParentDoc, aLoadGroup,
|
2015-06-17 04:21:08 +03:00
|
|
|
ios, secMan, aScriptURL, true, WorkerScript,
|
2015-10-06 16:37:07 +03:00
|
|
|
aContentPolicyType, nsIRequest::LOAD_NORMAL,
|
2016-04-28 10:44:08 +03:00
|
|
|
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;
|
2017-01-05 12:05:32 +03:00
|
|
|
getter->Dispatch(Terminating, 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,
|
|
|
|
nsPrintfCString("Failed to load worker script at %s (nsresult = 0x%x)",
|
|
|
|
NS_ConvertUTF16toUTF8(aScriptURL).get(),
|
|
|
|
aLoadResult));
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace scriptloader
|
|
|
|
|
|
|
|
END_WORKERS_NAMESPACE
|