2015-05-03 19:32:37 +00: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 11:12:37 +00: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 19:09:13 +00:00
|
|
|
|
|
|
|
#include "ScriptLoader.h"
|
|
|
|
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2016-03-17 10:03:05 +00:00
|
|
|
#include "nsIDocShell.h"
|
2016-06-30 04:31:59 +00:00
|
|
|
#include "nsIDOMDocument.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "nsIHttpChannel.h"
|
2015-07-02 14:30:06 +00:00
|
|
|
#include "nsIHttpChannelInternal.h"
|
2015-03-19 18:41:42 +00:00
|
|
|
#include "nsIInputStreamPump.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsIProtocolHandler.h"
|
2015-07-02 12:54:00 +00:00
|
|
|
#include "nsIScriptError.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsIStreamLoader.h"
|
2015-03-19 18:41:42 +00:00
|
|
|
#include "nsIStreamListenerTee.h"
|
|
|
|
#include "nsIThreadRetargetableRequest.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "nsIURI.h"
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
2015-10-15 03:05:00 +00:00
|
|
|
#include "jsfriendapi.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDocShellCID.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 02:17:00 +00:00
|
|
|
#include "nsIPipe.h"
|
|
|
|
#include "nsIOutputStream.h"
|
2016-03-16 18:51:11 +00:00
|
|
|
#include "nsPrintfCString.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "nsScriptLoader.h"
|
2013-09-23 17:25:00 +00:00
|
|
|
#include "nsString.h"
|
2015-03-19 18:41:42 +00:00
|
|
|
#include "nsStreamUtils.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXPCOM.h"
|
2013-03-07 22:27:21 +00:00
|
|
|
#include "xpcpublic.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2014-12-12 16:06:00 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/LoadContext.h"
|
2016-03-16 18:51:11 +00:00
|
|
|
#include "mozilla/Maybe.h"
|
2015-03-19 18:41:42 +00:00
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
|
|
|
#include "mozilla/dom/CacheBinding.h"
|
2015-06-05 04:39:34 +00:00
|
|
|
#include "mozilla/dom/cache/CacheTypes.h"
|
2015-03-19 18:41:42 +00:00
|
|
|
#include "mozilla/dom/cache/Cache.h"
|
|
|
|
#include "mozilla/dom/cache/CacheStorage.h"
|
2015-07-02 12:54:00 +00:00
|
|
|
#include "mozilla/dom/ChannelInfo.h"
|
2013-09-09 03:29:21 +00:00
|
|
|
#include "mozilla/dom/Exceptions.h"
|
2015-03-19 18:41:42 +00:00
|
|
|
#include "mozilla/dom/InternalResponse.h"
|
2016-06-30 04:31:59 +00:00
|
|
|
#include "mozilla/dom/nsCSPService.h"
|
|
|
|
#include "mozilla/dom/nsCSPUtils.h"
|
2015-03-19 18:41:42 +00:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-07-02 12:54:00 +00:00
|
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
2015-03-19 18:41:42 +00:00
|
|
|
#include "mozilla/dom/Response.h"
|
2016-03-16 18:51:11 +00:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2015-07-02 12:54:00 +00:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "Principal.h"
|
2016-06-23 08:53:14 +00:00
|
|
|
#include "WorkerHolder.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "WorkerPrivate.h"
|
2013-10-23 13:16:49 +00:00
|
|
|
#include "WorkerRunnable.h"
|
2015-07-02 12:54:00 +00:00
|
|
|
#include "WorkerScope.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
#define MAX_CONCURRENT_SCRIPTS 1000
|
|
|
|
|
2013-06-05 14:04:23 +00:00
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
2016-03-16 18:51:11 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2015-03-19 18:41:42 +00:00
|
|
|
using mozilla::dom::cache::Cache;
|
|
|
|
using mozilla::dom::cache::CacheStorage;
|
2015-07-02 12:54:00 +00:00
|
|
|
using mozilla::ipc::PrincipalInfo;
|
2013-06-05 14:04:23 +00:00
|
|
|
|
|
|
|
namespace {
|
2013-03-07 22:27:21 +00:00
|
|
|
|
2015-03-19 18:41:42 +00: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-07 22:27:21 +00:00
|
|
|
nsresult
|
|
|
|
ChannelFromScriptURL(nsIPrincipal* principal,
|
|
|
|
nsIURI* baseURI,
|
|
|
|
nsIDocument* parentDoc,
|
|
|
|
nsILoadGroup* loadGroup,
|
|
|
|
nsIIOService* ios,
|
|
|
|
nsIScriptSecurityManager* secMan,
|
2013-06-05 14:04:23 +00:00
|
|
|
const nsAString& aScriptURL,
|
2015-03-17 10:15:19 +00:00
|
|
|
bool aIsMainScript,
|
|
|
|
WorkerScriptType aWorkerScriptType,
|
2015-06-17 01:21:08 +00:00
|
|
|
nsContentPolicyType aContentPolicyType,
|
2015-10-06 13:37:07 +00:00
|
|
|
nsLoadFlags aLoadFlags,
|
2016-04-28 07:44:08 +00:00
|
|
|
bool aDefaultURIEncoding,
|
2013-06-05 14:04:23 +00:00
|
|
|
nsIChannel** aChannel)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-03-07 22:27:21 +00:00
|
|
|
|
2013-06-05 14:04:23 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2016-04-28 07:44:08 +00: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 14:04:23 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
2013-03-07 22:27:21 +00:00
|
|
|
|
2015-11-11 05:16:12 +00: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-16 22:41:03 +00: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-07 22:27:21 +00:00
|
|
|
|
2015-03-17 10:15:19 +00:00
|
|
|
if (aWorkerScriptType == DebuggerScript) {
|
2015-11-16 22:41:03 +00: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 19:13:43 +00:00
|
|
|
|
2015-11-16 22:41:03 +00:00
|
|
|
if (!isUIResource) {
|
2015-03-17 10:15:19 +00:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
2015-11-16 22:41:03 +00:00
|
|
|
|
|
|
|
secFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
|
2015-11-16 16:57:29 +00:00
|
|
|
}
|
2015-11-15 14:56:45 +00:00
|
|
|
|
2015-11-16 22:41:03 +00: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 19:13:43 +00:00
|
|
|
|
2013-06-05 14:04:23 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2015-11-11 05:16:12 +00: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 16:39:57 +00:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
uri,
|
|
|
|
parentDoc,
|
2015-11-16 22:41:03 +00:00
|
|
|
secFlags,
|
2015-06-17 01:21:08 +00:00
|
|
|
aContentPolicyType,
|
2014-09-21 16:39:57 +00:00
|
|
|
loadGroup,
|
|
|
|
nullptr, // aCallbacks
|
2015-10-06 13:37:07 +00:00
|
|
|
aLoadFlags,
|
2014-09-21 16:39:57 +00:00
|
|
|
ios);
|
|
|
|
} else {
|
2014-12-12 16:06:00 +00: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 16:39:57 +00:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
uri,
|
2014-12-12 16:06:00 +00:00
|
|
|
principal,
|
2015-11-16 22:41:03 +00:00
|
|
|
secFlags,
|
2015-06-17 01:21:08 +00:00
|
|
|
aContentPolicyType,
|
2014-09-21 16:39:57 +00:00
|
|
|
loadGroup,
|
|
|
|
nullptr, // aCallbacks
|
2015-10-06 13:37:07 +00:00
|
|
|
aLoadFlags,
|
2014-09-21 16:39:57 +00:00
|
|
|
ios);
|
|
|
|
}
|
|
|
|
|
2013-06-05 14:04:23 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-06-04 23:20:13 +00:00
|
|
|
if (nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(channel)) {
|
|
|
|
rv = nsContentUtils::SetFetchReferrerURIWithPolicy(principal, parentDoc,
|
2016-02-26 22:36:45 +00:00
|
|
|
httpChannel, mozilla::net::RP_Default);
|
2015-06-04 23:20:13 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 14:04:23 +00:00
|
|
|
channel.forget(aChannel);
|
|
|
|
return rv;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
struct ScriptLoadInfo
|
|
|
|
{
|
|
|
|
ScriptLoadInfo()
|
2014-04-25 14:11:57 +00:00
|
|
|
: mScriptTextBuf(nullptr)
|
|
|
|
, mScriptTextLength(0)
|
2015-03-19 18:41:42 +00:00
|
|
|
, mLoadResult(NS_ERROR_NOT_INITIALIZED)
|
|
|
|
, mLoadingFinished(false)
|
|
|
|
, mExecutionScheduled(false)
|
2014-04-25 14:11:57 +00:00
|
|
|
, mExecutionResult(false)
|
2015-03-19 18:41:42 +00:00
|
|
|
, mCacheStatus(Uncached)
|
2011-07-17 19:09:13 +00:00
|
|
|
{ }
|
|
|
|
|
2014-04-25 14:11:57 +00:00
|
|
|
~ScriptLoadInfo()
|
|
|
|
{
|
|
|
|
if (mScriptTextBuf) {
|
|
|
|
js_free(mScriptTextBuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
bool
|
|
|
|
ReadyToExecute()
|
|
|
|
{
|
|
|
|
return !mChannel && NS_SUCCEEDED(mLoadResult) && !mExecutionScheduled;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString mURL;
|
2015-03-19 18:41:42 +00: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 05:24:48 +00:00
|
|
|
RefPtr<Promise> mCachePromise;
|
2015-03-19 18:41:42 +00:00
|
|
|
|
2016-02-22 22:13:38 +00: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 19:09:13 +00:00
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
2014-07-22 04:43:21 +00:00
|
|
|
char16_t* mScriptTextBuf;
|
2014-04-25 14:11:57 +00:00
|
|
|
size_t mScriptTextLength;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
nsresult mLoadResult;
|
2015-03-19 18:41:42 +00:00
|
|
|
bool mLoadingFinished;
|
2011-07-17 19:09:13 +00:00
|
|
|
bool mExecutionScheduled;
|
|
|
|
bool mExecutionResult;
|
2015-03-19 18:41:42 +00: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;
|
|
|
|
|
2015-07-14 15:08:38 +00:00
|
|
|
Maybe<bool> mMutedErrorFlag;
|
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
bool Finished() const
|
|
|
|
{
|
|
|
|
return mLoadingFinished && !mCachePromise && !mChannel;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class ScriptLoaderRunnable;
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class ScriptExecutorRunnable final : public MainThreadWorkerSyncRunnable
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
ScriptLoaderRunnable& mScriptLoader;
|
2015-03-19 18:41:42 +00:00
|
|
|
bool mIsWorkerScript;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mFirstIndex;
|
|
|
|
uint32_t mLastIndex;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
ScriptExecutorRunnable(ScriptLoaderRunnable& aScriptLoader,
|
2015-03-19 18:41:42 +00:00
|
|
|
nsIEventTarget* aSyncLoopTarget,
|
|
|
|
bool aIsWorkerScript,
|
|
|
|
uint32_t aFirstIndex,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aLastIndex);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~ScriptExecutorRunnable()
|
|
|
|
{ }
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2015-03-17 10:15:19 +00:00
|
|
|
virtual bool
|
2015-03-21 16:28:04 +00:00
|
|
|
IsDebuggerRunnable() const override;
|
2015-03-17 10:15:19 +00:00
|
|
|
|
2016-03-01 21:52:26 +00:00
|
|
|
virtual bool
|
|
|
|
PreRun(WorkerPrivate* aWorkerPrivate) override;
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
virtual bool
|
2015-03-21 16:28:04 +00:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
virtual void
|
|
|
|
PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, bool aRunResult)
|
2015-03-21 16:28:04 +00:00
|
|
|
override;
|
2014-01-11 00:37:47 +00:00
|
|
|
|
2016-04-11 18:40:06 +00:00
|
|
|
nsresult
|
|
|
|
Cancel() override;
|
2014-01-11 00:37:47 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
ShutdownScriptLoader(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
2015-10-15 12:06:55 +00:00
|
|
|
bool aResult,
|
|
|
|
bool aMutedError);
|
2015-10-15 03:05:00 +00:00
|
|
|
|
|
|
|
void LogExceptionToConsole(JSContext* aCx,
|
|
|
|
WorkerPrivate* WorkerPrivate);
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
class CacheScriptLoader;
|
|
|
|
|
|
|
|
class CacheCreator final : public PromiseNativeHandler
|
|
|
|
{
|
|
|
|
public:
|
2015-07-09 06:56:00 +00:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
explicit CacheCreator(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mCacheName(aWorkerPrivate->ServiceWorkerCacheName())
|
2016-09-20 01:13:00 +00:00
|
|
|
, mOriginAttributes(aWorkerPrivate->GetOriginAttributes())
|
2015-03-19 18:41:42 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
|
2015-03-19 23:39:01 +00:00
|
|
|
MOZ_ASSERT(aWorkerPrivate->LoadScriptAsPartOfLoadingServiceWorkerScript());
|
2015-03-19 18:41:42 +00: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 05:24:48 +00:00
|
|
|
RefPtr<Cache> mCache;
|
|
|
|
RefPtr<CacheStorage> mCacheStorage;
|
2015-03-19 18:41:42 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> mSandboxGlobalObject;
|
2015-10-18 05:24:48 +00:00
|
|
|
nsTArray<RefPtr<CacheScriptLoader>> mLoaders;
|
2015-03-19 18:41:42 +00:00
|
|
|
|
|
|
|
nsString mCacheName;
|
2016-09-20 01:13:00 +00:00
|
|
|
PrincipalOriginAttributes mOriginAttributes;
|
2015-03-19 18:41:42 +00:00
|
|
|
};
|
|
|
|
|
2015-07-09 06:56:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS0(CacheCreator)
|
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
class CacheScriptLoader final : public PromiseNativeHandler
|
|
|
|
, public nsIStreamLoaderObserver
|
|
|
|
{
|
|
|
|
public:
|
2015-07-09 06:56:00 +00:00
|
|
|
NS_DECL_ISUPPORTS
|
2015-03-19 18:41:42 +00: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 05:24:48 +00:00
|
|
|
RefPtr<ScriptLoaderRunnable> mRunnable;
|
2015-03-19 18:41:42 +00:00
|
|
|
bool mIsWorkerScript;
|
|
|
|
bool mFailed;
|
|
|
|
nsCOMPtr<nsIInputStreamPump> mPump;
|
|
|
|
nsCOMPtr<nsIURI> mBaseURI;
|
2015-07-02 12:54:00 +00:00
|
|
|
mozilla::dom::ChannelInfo mChannelInfo;
|
2015-06-05 04:39:34 +00:00
|
|
|
UniquePtr<PrincipalInfo> mPrincipalInfo;
|
2015-03-19 18:41:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(CacheScriptLoader, nsIStreamLoaderObserver)
|
|
|
|
|
|
|
|
class CachePromiseHandler final : public PromiseNativeHandler
|
|
|
|
{
|
|
|
|
public:
|
2015-07-09 06:56:00 +00:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2015-03-19 18:41:42 +00: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 05:24:48 +00:00
|
|
|
RefPtr<ScriptLoaderRunnable> mRunnable;
|
2015-03-19 18:41:42 +00:00
|
|
|
ScriptLoadInfo& mLoadInfo;
|
|
|
|
uint32_t mIndex;
|
|
|
|
};
|
|
|
|
|
2015-07-09 06:56:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS0(CachePromiseHandler)
|
|
|
|
|
2015-11-16 22:41:03 +00: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 07:12:40 +00:00
|
|
|
class ScriptLoaderHolder;
|
|
|
|
|
|
|
|
class ScriptLoaderRunnable final : public nsIRunnable
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
friend class ScriptExecutorRunnable;
|
2016-07-18 07:12:40 +00:00
|
|
|
friend class ScriptLoaderHolder;
|
2015-03-19 18:41:42 +00:00
|
|
|
friend class CachePromiseHandler;
|
|
|
|
friend class CacheScriptLoader;
|
2015-11-16 22:41:03 +00:00
|
|
|
friend class LoaderListener;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2013-10-23 13:16:49 +00:00
|
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
2011-07-17 19:09:13 +00:00
|
|
|
nsTArray<ScriptLoadInfo> mLoadInfos;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<CacheCreator> mCacheCreator;
|
2015-03-17 10:15:19 +00:00
|
|
|
bool mIsMainScript;
|
|
|
|
WorkerScriptType mWorkerScriptType;
|
2011-07-17 19:09:13 +00:00
|
|
|
bool mCanceled;
|
|
|
|
bool mCanceledMainThread;
|
2015-10-15 12:06:55 +00:00
|
|
|
ErrorResult& mRv;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
public:
|
2013-07-19 02:21:20 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
ScriptLoaderRunnable(WorkerPrivate* aWorkerPrivate,
|
2013-10-23 13:16:49 +00:00
|
|
|
nsIEventTarget* aSyncLoopTarget,
|
2011-07-17 19:09:13 +00:00
|
|
|
nsTArray<ScriptLoadInfo>& aLoadInfos,
|
2015-03-17 10:15:19 +00:00
|
|
|
bool aIsMainScript,
|
2015-10-15 12:06:55 +00:00
|
|
|
WorkerScriptType aWorkerScriptType,
|
|
|
|
ErrorResult& aRv)
|
2013-10-23 13:16:49 +00:00
|
|
|
: mWorkerPrivate(aWorkerPrivate), mSyncLoopTarget(aSyncLoopTarget),
|
2015-03-17 10:15:19 +00:00
|
|
|
mIsMainScript(aIsMainScript), mWorkerScriptType(aWorkerScriptType),
|
2015-10-15 12:06:55 +00:00
|
|
|
mCanceled(false), mCanceledMainThread(false), mRv(aRv)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
2013-10-23 13:16:49 +00:00
|
|
|
MOZ_ASSERT(aSyncLoopTarget);
|
2015-03-17 10:15:19 +00:00
|
|
|
MOZ_ASSERT_IF(aIsMainScript, aLoadInfos.Length() == 1);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-04-01 17:35:13 +00:00
|
|
|
mLoadInfos.SwapElements(aLoadInfos);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
~ScriptLoaderRunnable()
|
|
|
|
{ }
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
NS_IMETHOD
|
2015-03-21 16:28:04 +00:00
|
|
|
Run() override
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2016-02-24 15:38:31 +00:00
|
|
|
nsresult rv = RunInternal();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
CancelMainThread(rv);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-19 18:41:42 +00: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 00:11:57 +00:00
|
|
|
if (loadInfo.Finished()) {
|
2015-03-19 18:41:42 +00:00
|
|
|
ExecuteFinishedScripts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 22:41:03 +00:00
|
|
|
nsresult
|
|
|
|
OnStreamComplete(nsIStreamLoader* aLoader, uint32_t aIndex,
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult aStatus, uint32_t aStringLen,
|
2015-11-16 22:41:03 +00:00
|
|
|
const uint8_t* aString)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-11-16 22:41:03 +00:00
|
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2015-11-16 22:41:03 +00:00
|
|
|
nsresult rv = OnStreamCompleteInternal(aLoader, aStatus, aStringLen,
|
|
|
|
aString, mLoadInfos[aIndex]);
|
|
|
|
LoadingFinished(aIndex, rv);
|
2011-07-17 19:09:13 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-16 22:41:03 +00:00
|
|
|
nsresult
|
|
|
|
OnStartRequest(nsIRequest* aRequest, uint32_t aIndex)
|
2015-04-23 23:10:57 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-11-16 22:41:03 +00:00
|
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
2015-04-23 23:10:57 +00:00
|
|
|
|
2015-12-09 23:21:00 +00: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 15:11:26 +00:00
|
|
|
aRequest->Cancel(NS_ERROR_FAILURE);
|
2015-12-09 23:21:00 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-11-16 22:41:03 +00:00
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
2015-04-23 23:10:57 +00: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 05:24:48 +00:00
|
|
|
RefPtr<mozilla::dom::InternalResponse> ir =
|
2015-07-02 12:54:00 +00:00
|
|
|
new mozilla::dom::InternalResponse(200, NS_LITERAL_CSTRING("OK"));
|
2016-06-01 21:02:29 +00:00
|
|
|
ir->SetBody(loadInfo.mCacheReadStream, InternalResponse::UNKNOWN_BODY_SIZE);
|
2016-02-22 22:13:38 +00: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-23 23:10:57 +00:00
|
|
|
|
2015-05-25 18:21:05 +00:00
|
|
|
// Set the channel info of the channel on the response so that it's
|
2015-04-23 23:10:57 +00:00
|
|
|
// saved in the cache.
|
2015-05-25 18:21:05 +00:00
|
|
|
ir->InitChannelInfo(channel);
|
2015-04-23 23:10:57 +00:00
|
|
|
|
2015-06-05 04:39:34 +00: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 05:24:48 +00:00
|
|
|
RefPtr<mozilla::dom::Response> response =
|
2015-07-02 12:54:00 +00:00
|
|
|
new mozilla::dom::Response(mCacheCreator->Global(), ir);
|
2015-04-23 23:10:57 +00:00
|
|
|
|
2015-07-02 12:54:00 +00:00
|
|
|
mozilla::dom::RequestOrUSVString request;
|
2015-04-23 23:10:57 +00:00
|
|
|
|
|
|
|
MOZ_ASSERT(!loadInfo.mFullURL.IsEmpty());
|
|
|
|
request.SetAsUSVString().Rebind(loadInfo.mFullURL.Data(),
|
|
|
|
loadInfo.mFullURL.Length());
|
|
|
|
|
|
|
|
ErrorResult error;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Promise> cachePromise =
|
2015-04-23 23:10:57 +00: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 05:24:48 +00:00
|
|
|
RefPtr<CachePromiseHandler> promiseHandler =
|
2015-11-16 22:41:03 +00:00
|
|
|
new CachePromiseHandler(this, loadInfo, aIndex);
|
2015-04-23 23:10:57 +00:00
|
|
|
cachePromise->AppendNativeHandler(promiseHandler);
|
|
|
|
|
|
|
|
loadInfo.mCachePromise.swap(cachePromise);
|
|
|
|
loadInfo.mCacheStatus = ScriptLoadInfo::WritingToCache;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-07-18 07:12:40 +00:00
|
|
|
bool
|
|
|
|
Notify(Status aStatus)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (aStatus >= Terminating && !mCanceled) {
|
|
|
|
mCanceled = true;
|
|
|
|
|
2016-05-05 08:45:00 +00:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
NS_DispatchToMainThread(NewRunnableMethod(this,
|
|
|
|
&ScriptLoaderRunnable::CancelMainThreadWithBindingAborted)));
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-17 10:15:19 +00:00
|
|
|
bool
|
|
|
|
IsMainWorkerScript() const
|
|
|
|
{
|
|
|
|
return mIsMainScript && mWorkerScriptType == WorkerScript;
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
void
|
2016-02-24 15:38:31 +00:00
|
|
|
CancelMainThreadWithBindingAborted()
|
|
|
|
{
|
|
|
|
CancelMainThread(NS_BINDING_ABORTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CancelMainThread(nsresult aCancelResult)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (mCanceledMainThread) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCanceledMainThread = true;
|
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
if (mCacheCreator) {
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsServiceWorker());
|
|
|
|
DeleteCache();
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
// Cancel all the channels that were already opened.
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t index = 0; index < mLoadInfos.Length(); index++) {
|
2011-07-17 19:09:13 +00:00
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[index];
|
|
|
|
|
2015-03-26 22:56:02 +00:00
|
|
|
// If promise or channel is non-null, their failures will lead to
|
2015-03-25 00:11:57 +00:00
|
|
|
// LoadingFinished being called.
|
|
|
|
bool callLoadingFinished = true;
|
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
if (loadInfo.mCachePromise) {
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsServiceWorker());
|
2016-02-24 15:38:31 +00:00
|
|
|
loadInfo.mCachePromise->MaybeReject(aCancelResult);
|
2015-03-19 18:41:42 +00:00
|
|
|
loadInfo.mCachePromise = nullptr;
|
2015-03-25 00:11:57 +00:00
|
|
|
callLoadingFinished = false;
|
2015-03-19 18:41:42 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 00:11:57 +00:00
|
|
|
if (loadInfo.mChannel) {
|
2016-02-24 15:38:31 +00:00
|
|
|
if (NS_SUCCEEDED(loadInfo.mChannel->Cancel(aCancelResult))) {
|
2015-03-25 00:11:57 +00:00
|
|
|
callLoadingFinished = false;
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Failed to cancel channel!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callLoadingFinished && !loadInfo.Finished()) {
|
2016-02-24 15:38:31 +00:00
|
|
|
LoadingFinished(index, aCancelResult);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExecuteFinishedScripts();
|
|
|
|
}
|
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
void
|
|
|
|
DeleteCache()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (!mCacheCreator) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCacheCreator->DeleteCache();
|
|
|
|
mCacheCreator = nullptr;
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
nsresult
|
|
|
|
RunInternal()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
if (IsMainWorkerScript() && mWorkerPrivate->IsServiceWorker()) {
|
|
|
|
mWorkerPrivate->SetLoadingWorkerScript(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mWorkerPrivate->IsServiceWorker() ||
|
2015-03-19 23:39:01 +00:00
|
|
|
!mWorkerPrivate->LoadScriptAsPartOfLoadingServiceWorkerScript()) {
|
2015-03-19 18:41:42 +00: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-16 22:41:03 +00:00
|
|
|
LoadingFinished(index, rv);
|
2015-03-19 18:41:42 +00: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 05:24:48 +00:00
|
|
|
RefPtr<CacheScriptLoader> loader =
|
2015-03-19 18:41:42 +00: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 19:09:13 +00:00
|
|
|
WorkerPrivate* parentWorker = mWorkerPrivate->GetParent();
|
|
|
|
|
|
|
|
// Figure out which principal to use.
|
|
|
|
nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
|
2014-12-12 16:06:00 +00:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = mWorkerPrivate->GetLoadGroup();
|
2011-07-17 19:09:13 +00:00
|
|
|
if (!principal) {
|
|
|
|
NS_ASSERTION(parentWorker, "Must have a principal!");
|
2015-03-17 10:15:19 +00:00
|
|
|
NS_ASSERTION(mIsMainScript, "Must have a principal for importScripts!");
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
principal = parentWorker->GetPrincipal();
|
2014-12-12 16:06:00 +00:00
|
|
|
loadGroup = parentWorker->GetLoadGroup();
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
NS_ASSERTION(principal, "This should never be null here!");
|
2014-12-12 16:06:00 +00:00
|
|
|
MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(loadGroup, principal));
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
// Figure out our base URI.
|
2015-03-19 18:41:42 +00:00
|
|
|
nsCOMPtr<nsIURI> baseURI = GetBaseURI(mIsMainScript, mWorkerPrivate);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
// May be null.
|
|
|
|
nsCOMPtr<nsIDocument> parentDoc = mWorkerPrivate->GetDocument();
|
|
|
|
|
2013-03-07 22:27:21 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2015-03-17 10:15:19 +00:00
|
|
|
if (IsMainWorkerScript()) {
|
2013-03-07 22:27:21 +00:00
|
|
|
// May be null.
|
2013-06-05 14:04:23 +00:00
|
|
|
channel = mWorkerPrivate->ForgetWorkerChannel();
|
2013-03-07 22:27:21 +00:00
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
nsCOMPtr<nsIIOService> ios(do_GetIOService());
|
|
|
|
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
NS_ASSERTION(secMan, "This should never be null!");
|
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
|
|
|
nsresult& rv = loadInfo.mLoadResult;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2015-10-06 13:37:07 +00:00
|
|
|
nsLoadFlags loadFlags = nsIRequest::LOAD_NORMAL;
|
|
|
|
|
2016-03-17 10:03:05 +00: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 14:58:21 +00:00
|
|
|
nsCOMPtr<nsIDocShell> docShell = window->GetDocShell();
|
2016-03-17 10:03:05 +00:00
|
|
|
if (docShell) {
|
|
|
|
nsresult rv = docShell->GetDefaultLoadFlags(&loadFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 13:37:07 +00:00
|
|
|
// If we are loading a script for a ServiceWorker then we must not
|
|
|
|
// try to intercept it. If the interception matches the current
|
|
|
|
// ServiceWorker's scope then we could deadlock the load.
|
|
|
|
if (mWorkerPrivate->IsServiceWorker()) {
|
|
|
|
loadFlags |= nsIChannel::LOAD_BYPASS_SERVICE_WORKER;
|
|
|
|
}
|
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
if (!channel) {
|
2016-04-28 07:44:08 +00: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 18:41:42 +00:00
|
|
|
rv = ChannelFromScriptURL(principal, baseURI, parentDoc, loadGroup, ios,
|
|
|
|
secMan, loadInfo.mURL, IsMainWorkerScript(),
|
2015-06-17 01:21:08 +00:00
|
|
|
mWorkerScriptType,
|
2015-10-06 13:37:07 +00:00
|
|
|
mWorkerPrivate->ContentPolicyType(), loadFlags,
|
2016-04-28 07:44:08 +00:00
|
|
|
useDefaultEncoding,
|
2015-06-17 01:21:08 +00:00
|
|
|
getter_AddRefs(channel));
|
2015-03-19 18:41:42 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
2015-03-19 18:41:42 +00:00
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
// We need to know which index we're on in OnStreamComplete so we know
|
|
|
|
// where to put the result.
|
2015-11-16 22:41:03 +00:00
|
|
|
RefPtr<LoaderListener> listener = new LoaderListener(this, aIndex);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
// We don't care about progress so just use the simple stream loader for
|
|
|
|
// OnStreamComplete notification only.
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
2015-11-16 22:41:03 +00:00
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), listener);
|
2015-03-19 18:41:42 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
if (loadInfo.mCacheStatus != ScriptLoadInfo::ToBeCached) {
|
2015-11-16 22:41:03 +00:00
|
|
|
rv = channel->AsyncOpen2(loader);
|
2015-03-19 18:41:42 +00: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-22 22:13:38 +00:00
|
|
|
rv = NS_NewPipe(getter_AddRefs(loadInfo.mCacheReadStream),
|
|
|
|
getter_AddRefs(writer), 0,
|
2015-03-19 18:41:42 +00: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-16 22:41:03 +00:00
|
|
|
rv = tee->Init(loader, writer, listener);
|
2015-03-19 18:41:42 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-11-16 22:41:03 +00:00
|
|
|
nsresult rv = channel->AsyncOpen2(tee);
|
2015-03-19 18:41:42 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
loadInfo.mChannel.swap(channel);
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-11-16 22:41:03 +00:00
|
|
|
OnStreamCompleteInternal(nsIStreamLoader* aLoader, nsresult aStatus,
|
|
|
|
uint32_t aStringLen, const uint8_t* aString,
|
|
|
|
ScriptLoadInfo& aLoadInfo)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (!aLoadInfo.mChannel) {
|
|
|
|
return NS_BINDING_ABORTED;
|
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
aLoadInfo.mChannel = nullptr;
|
2011-07-17 19:09:13 +00: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 15:08:38 +00: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 17:28:45 +00: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 07:12:40 +00:00
|
|
|
? false
|
2015-10-26 17:28:45 +00:00
|
|
|
: !principal->Subsumes(channelPrincipal));
|
2015-07-14 15:08:38 +00: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 19:09:13 +00:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request);
|
2016-07-19 07:38:26 +00:00
|
|
|
nsAutoCString tCspHeaderValue, tCspROHeaderValue, tRPHeaderCValue;
|
2016-06-30 04:31:59 +00:00
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
if (httpChannel) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool requestSucceeded;
|
2011-07-17 19:09:13 +00:00
|
|
|
rv = httpChannel->GetRequestSucceeded(&requestSucceeded);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!requestSucceeded) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2016-06-30 04:31:59 +00:00
|
|
|
|
|
|
|
httpChannel->GetResponseHeader(
|
|
|
|
NS_LITERAL_CSTRING("content-security-policy"),
|
|
|
|
tCspHeaderValue);
|
|
|
|
|
|
|
|
httpChannel->GetResponseHeader(
|
|
|
|
NS_LITERAL_CSTRING("content-security-policy-report-only"),
|
|
|
|
tCspROHeaderValue);
|
2016-07-19 07:38:26 +00:00
|
|
|
|
|
|
|
httpChannel->GetResponseHeader(
|
|
|
|
NS_LITERAL_CSTRING("referrer-policy"),
|
|
|
|
tRPHeaderCValue);
|
2011-07-17 19:09:13 +00: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-09 22:23:47 +00: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 19:09:13 +00:00
|
|
|
rv = nsScriptLoader::ConvertToUTF16(aLoadInfo.mChannel, aString, aStringLen,
|
2013-10-09 22:23:47 +00:00
|
|
|
NS_LITERAL_STRING("UTF-8"), parentDoc,
|
2014-04-25 14:11:57 +00:00
|
|
|
aLoadInfo.mScriptTextBuf,
|
|
|
|
aLoadInfo.mScriptTextLength);
|
2011-07-17 19:09:13 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-03-29 18:53:48 +00: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 19:09:13 +00: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));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the principal of the worker and its base URI if we just loaded the
|
|
|
|
// worker's primary script.
|
2015-03-17 10:15:19 +00:00
|
|
|
if (IsMainWorkerScript()) {
|
2011-07-17 19:09:13 +00:00
|
|
|
// Take care of the base URI first.
|
2011-11-04 16:32:17 +00:00
|
|
|
mWorkerPrivate->SetBaseURI(finalURI);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2015-05-25 18:21:05 +00:00
|
|
|
// Store the channel info if needed.
|
2015-08-17 22:08:58 +00:00
|
|
|
mWorkerPrivate->InitChannelInfo(channel);
|
2015-04-23 23:04:03 +00:00
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
// Now to figure out which principal to give this worker.
|
|
|
|
WorkerPrivate* parent = mWorkerPrivate->GetParent();
|
|
|
|
|
|
|
|
NS_ASSERTION(mWorkerPrivate->GetPrincipal() || parent,
|
|
|
|
"Must have one of these!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> loadPrincipal = mWorkerPrivate->GetPrincipal() ?
|
|
|
|
mWorkerPrivate->GetPrincipal() :
|
|
|
|
parent->GetPrincipal();
|
|
|
|
|
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
NS_ASSERTION(ssm, "Should never be null!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> channelPrincipal;
|
2014-08-06 23:05:40 +00:00
|
|
|
rv = ssm->GetChannelResultPrincipal(channel, getter_AddRefs(channelPrincipal));
|
2011-07-17 19:09:13 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-12-12 16:06:00 +00:00
|
|
|
nsCOMPtr<nsILoadGroup> channelLoadGroup;
|
|
|
|
rv = channel->GetLoadGroup(getter_AddRefs(channelLoadGroup));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
MOZ_ASSERT(channelLoadGroup);
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
// If the load principal is the system principal then the channel
|
|
|
|
// principal must also be the system principal (we do not allow chrome
|
2015-06-09 21:34:00 +00:00
|
|
|
// code to create workers with non-chrome scripts, and if we ever decide
|
|
|
|
// to change this we need to make sure we don't always set
|
|
|
|
// mPrincipalIsSystem to true in WorkerPrivate::GetLoadInfo()). Otherwise
|
|
|
|
// this channel principal must be same origin with the load principal (we
|
|
|
|
// check again here in case redirects changed the location of the script).
|
2011-07-17 19:09:13 +00:00
|
|
|
if (nsContentUtils::IsSystemPrincipal(loadPrincipal)) {
|
|
|
|
if (!nsContentUtils::IsSystemPrincipal(channelPrincipal)) {
|
2015-05-26 21:27:01 +00:00
|
|
|
// See if this is a resource URI. Since JSMs usually come from
|
|
|
|
// resource:// URIs we're currently considering all URIs with the
|
|
|
|
// URI_IS_UI_RESOURCE flag as valid for creating privileged workers.
|
|
|
|
bool isResource;
|
|
|
|
rv = NS_URIChainHasFlags(finalURI,
|
|
|
|
nsIProtocolHandler::URI_IS_UI_RESOURCE,
|
|
|
|
&isResource);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (isResource) {
|
|
|
|
// Assign the system principal to the resource:// worker only if it
|
|
|
|
// was loaded from code using the system principal.
|
|
|
|
channelPrincipal = loadPrincipal;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 16:06:00 +00:00
|
|
|
// The principal can change, but it should still match the original
|
|
|
|
// load group's appId and browser element flag.
|
|
|
|
MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(channelLoadGroup, channelPrincipal));
|
|
|
|
|
|
|
|
mWorkerPrivate->SetPrincipal(channelPrincipal, channelLoadGroup);
|
2016-06-30 04:31:59 +00: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 03:47:13 +00:00
|
|
|
|
|
|
|
// Set ReferrerPolicy, default value is set in GetReferrerPolicy
|
|
|
|
bool hasReferrerPolicy = false;
|
|
|
|
uint32_t rp = mozilla::net::RP_Default;
|
|
|
|
rv = csp->GetReferrerPolicy(&rp, &hasReferrerPolicy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (hasReferrerPolicy) {
|
|
|
|
mWorkerPrivate->SetReferrerPolicy(static_cast<net::ReferrerPolicy>(rp));
|
|
|
|
}
|
2016-06-30 04:31:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (parent) {
|
|
|
|
// XHR Params Allowed
|
|
|
|
mWorkerPrivate->SetXHRParamsAllowed(parent->XHRParamsAllowed());
|
|
|
|
}
|
2015-03-19 18:41:42 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 07:38:26 +00: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 18:41:42 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataReceivedFromCache(uint32_t aIndex, const uint8_t* aString,
|
2015-05-25 18:21:05 +00:00
|
|
|
uint32_t aStringLen,
|
2015-07-02 12:54:00 +00:00
|
|
|
const mozilla::dom::ChannelInfo& aChannelInfo,
|
2015-06-05 04:39:34 +00:00
|
|
|
UniquePtr<PrincipalInfo> aPrincipalInfo)
|
2015-03-19 18:41:42 +00:00
|
|
|
{
|
2015-03-18 23:14:30 +00:00
|
|
|
AssertIsOnMainThread();
|
2015-03-19 18:41:42 +00:00
|
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
|
|
|
MOZ_ASSERT(loadInfo.mCacheStatus == ScriptLoadInfo::Cached);
|
|
|
|
|
2015-07-14 15:08:38 +00: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 12:06:55 +00:00
|
|
|
loadInfo.mMutedErrorFlag.emplace(!principal->Subsumes(responsePrincipal));
|
2015-07-14 15:08:38 +00:00
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
// May be null.
|
|
|
|
nsIDocument* parentDoc = mWorkerPrivate->GetDocument();
|
|
|
|
|
|
|
|
MOZ_ASSERT(!loadInfo.mScriptTextBuf);
|
|
|
|
|
2015-03-18 23:14:30 +00:00
|
|
|
nsresult rv =
|
2015-03-19 18:41:42 +00:00
|
|
|
nsScriptLoader::ConvertToUTF16(nullptr, aString, aStringLen,
|
|
|
|
NS_LITERAL_STRING("UTF-8"), parentDoc,
|
|
|
|
loadInfo.mScriptTextBuf,
|
|
|
|
loadInfo.mScriptTextLength);
|
2015-03-18 23:14:30 +00: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-10 21:14:44 +00:00
|
|
|
|
2015-07-02 12:54:00 +00:00
|
|
|
mozilla::DebugOnly<nsIPrincipal*> principal = mWorkerPrivate->GetPrincipal();
|
2015-04-10 21:14:44 +00:00
|
|
|
MOZ_ASSERT(principal);
|
|
|
|
nsILoadGroup* loadGroup = mWorkerPrivate->GetLoadGroup();
|
|
|
|
MOZ_ASSERT(loadGroup);
|
2015-06-05 04:39:34 +00:00
|
|
|
|
2015-07-02 12:54:00 +00:00
|
|
|
mozilla::DebugOnly<bool> equal = false;
|
2015-06-05 04:39:34 +00:00
|
|
|
MOZ_ASSERT(responsePrincipal && NS_SUCCEEDED(responsePrincipal->Equals(principal, &equal)));
|
|
|
|
MOZ_ASSERT(equal);
|
|
|
|
|
2015-05-25 18:21:05 +00:00
|
|
|
mWorkerPrivate->InitChannelInfo(aChannelInfo);
|
2015-06-05 04:39:34 +00:00
|
|
|
mWorkerPrivate->SetPrincipal(responsePrincipal, loadGroup);
|
2015-03-18 23:14:30 +00:00
|
|
|
}
|
2015-03-19 18:41:42 +00:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
DataReceived();
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadingFinished(aIndex, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataReceived()
|
|
|
|
{
|
|
|
|
if (IsMainWorkerScript()) {
|
|
|
|
WorkerPrivate* parent = mWorkerPrivate->GetParent();
|
2012-09-15 18:51:55 +00:00
|
|
|
|
|
|
|
if (parent) {
|
2012-09-17 00:20:16 +00:00
|
|
|
// XHR Params Allowed
|
|
|
|
mWorkerPrivate->SetXHRParamsAllowed(parent->XHRParamsAllowed());
|
|
|
|
|
|
|
|
// Set Eval and ContentSecurityPolicy
|
2012-09-15 18:51:55 +00:00
|
|
|
mWorkerPrivate->SetCSP(parent->GetCSP());
|
|
|
|
mWorkerPrivate->SetEvalAllowed(parent->IsEvalAllowed());
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExecuteFinishedScripts()
|
|
|
|
{
|
2013-06-05 14:04:23 +00:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-03-17 10:15:19 +00:00
|
|
|
if (IsMainWorkerScript()) {
|
2013-06-05 14:04:23 +00:00
|
|
|
mWorkerPrivate->WorkerScriptLoaded();
|
|
|
|
}
|
|
|
|
|
2012-09-28 06:57:33 +00:00
|
|
|
uint32_t firstIndex = UINT32_MAX;
|
|
|
|
uint32_t lastIndex = UINT32_MAX;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
// Find firstIndex based on whether mExecutionScheduled is unset.
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t index = 0; index < mLoadInfos.Length(); index++) {
|
2011-07-17 19:09:13 +00: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 06:57:33 +00:00
|
|
|
if (firstIndex != UINT32_MAX) {
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t index = firstIndex; index < mLoadInfos.Length(); index++) {
|
2011-07-17 19:09:13 +00:00
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[index];
|
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
if (!loadInfo.Finished()) {
|
2011-07-17 19:09:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can execute this one.
|
|
|
|
loadInfo.mExecutionScheduled = true;
|
|
|
|
|
|
|
|
lastIndex = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 18:41:42 +00: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 06:57:33 +00:00
|
|
|
if (firstIndex != UINT32_MAX && lastIndex != UINT32_MAX) {
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ScriptExecutorRunnable> runnable =
|
2015-03-19 18:41:42 +00:00
|
|
|
new ScriptExecutorRunnable(*this, mSyncLoopTarget, IsMainWorkerScript(),
|
|
|
|
firstIndex, lastIndex);
|
2016-02-26 20:23:12 +00:00
|
|
|
if (!runnable->Dispatch()) {
|
2013-10-23 13:16:49 +00:00
|
|
|
MOZ_ASSERT(false, "This should never fail!");
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-16 22:41:03 +00:00
|
|
|
NS_IMPL_ISUPPORTS(ScriptLoaderRunnable, nsIRunnable)
|
|
|
|
|
2016-07-18 07:12:40 +00: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-16 22:41:03 +00: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 19:09:13 +00:00
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
void
|
|
|
|
CachePromiseHandler::ResolvedCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-03-26 22:56:02 +00: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 18:41:42 +00:00
|
|
|
|
2015-03-25 00:11:57 +00:00
|
|
|
if (mLoadInfo.mCachePromise) {
|
2015-03-26 22:56:02 +00:00
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::Cached;
|
2015-03-25 00:11:57 +00:00
|
|
|
mLoadInfo.mCachePromise = nullptr;
|
|
|
|
mRunnable->MaybeExecuteFinishedScripts(mIndex);
|
|
|
|
}
|
2015-03-19 18:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CachePromiseHandler::RejectedCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-03-26 22:56:02 +00:00
|
|
|
// May already have been canceled by CacheScriptLoader::Fail from
|
|
|
|
// CancelMainThread.
|
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::WritingToCache ||
|
|
|
|
mLoadInfo.mCacheStatus == ScriptLoadInfo::Cancel);
|
2015-03-19 18:41:42 +00: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 12:54:00 +00:00
|
|
|
mozilla::AutoSafeJSContext cx;
|
2015-07-21 18:31:44 +00:00
|
|
|
JS::Rooted<JSObject*> sandbox(cx);
|
|
|
|
nsresult rv = xpc->CreateSandbox(cx, aPrincipal, sandbox.address());
|
2015-03-19 18:41:42 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-07-21 18:31:44 +00:00
|
|
|
mSandboxGlobalObject = xpc::NativeGlobal(sandbox);
|
2015-03-19 18:41:42 +00:00
|
|
|
if (NS_WARN_IF(!mSandboxGlobalObject)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-06-27 02:36:40 +00: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 01:13:00 +00:00
|
|
|
if (NS_WARN_IF(mOriginAttributes.mPrivateBrowsingId > 0)) {
|
2015-06-27 02:36:40 +00:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
2015-06-28 03:19:24 +00: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 18:41:42 +00:00
|
|
|
ErrorResult error;
|
|
|
|
mCacheStorage =
|
2015-07-02 12:54:00 +00:00
|
|
|
CacheStorage::CreateOnMainThread(mozilla::dom::cache::CHROME_ONLY_NAMESPACE,
|
2015-03-19 18:41:42 +00:00
|
|
|
mSandboxGlobalObject,
|
2016-09-20 01:13:00 +00:00
|
|
|
aPrincipal,
|
|
|
|
false, /* privateBrowsing can't be true here */
|
2015-06-28 03:19:24 +00:00
|
|
|
true /* force trusted origin */,
|
2015-06-27 02:36:40 +00:00
|
|
|
error);
|
2015-03-19 18:41:42 +00:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2015-04-27 13:18:51 +00:00
|
|
|
return error.StealNSResult();
|
2015-03-19 18:41:42 +00: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 05:24:48 +00:00
|
|
|
RefPtr<Promise> promise = mCacheStorage->Open(mCacheName, error);
|
2015-03-19 18:41:42 +00:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2015-04-27 13:18:51 +00:00
|
|
|
return error.StealNSResult();
|
2015-03-19 18:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
promise->AppendNativeHandler(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CacheCreator::FailLoaders(nsresult aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-03-27 17:00:59 +00:00
|
|
|
// Fail() can call LoadingFinished() which may call ExecuteFinishedScripts()
|
|
|
|
// which sets mCacheCreator to null, so hold a ref.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<CacheCreator> kungfuDeathGrip = this;
|
2015-03-27 17:00:59 +00:00
|
|
|
|
2015-03-19 18:41:42 +00: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();
|
|
|
|
MOZ_ASSERT(aValue.isObject());
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
|
|
|
|
Cache* cache = nullptr;
|
|
|
|
nsresult rv = UNWRAP_OBJECT(Cache, obj, cache);
|
2016-03-28 17:28:15 +00:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(rv);
|
2015-03-19 18:41:42 +00:00
|
|
|
|
|
|
|
mCache = cache;
|
|
|
|
MOZ_ASSERT(mCache);
|
|
|
|
|
|
|
|
// If the worker is canceled, CancelMainThread() will have cleared the
|
|
|
|
// loaders.
|
|
|
|
for (uint32_t i = 0, len = mLoaders.Length(); i < len; ++i) {
|
|
|
|
mLoaders[i]->Load(cache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CacheCreator::DeleteCache()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
|
|
|
|
// It's safe to do this while Cache::Match() and Cache::Put() calls are
|
|
|
|
// running.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Promise> promise = mCacheStorage->Delete(mCacheName, rv);
|
2015-03-19 18:41:42 +00:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2016-07-22 14:50:10 +00:00
|
|
|
rv.SuppressException();
|
2015-03-19 18:41:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't care to know the result of the promise object.
|
|
|
|
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 00:11:57 +00:00
|
|
|
|
|
|
|
// Stop if the load was aborted on the main thread.
|
2015-03-26 22:56:02 +00: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 00:11:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-19 18:41:42 +00: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 12:54:00 +00:00
|
|
|
mozilla::dom::RequestOrUSVString request;
|
2015-03-19 18:41:42 +00:00
|
|
|
request.SetAsUSVString().Rebind(mLoadInfo.mFullURL.Data(),
|
|
|
|
mLoadInfo.mFullURL.Length());
|
|
|
|
|
2015-07-02 12:54:00 +00:00
|
|
|
mozilla::dom::CacheQueryOptions params;
|
2015-03-19 18:41:42 +00:00
|
|
|
|
|
|
|
ErrorResult error;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Promise> promise = aCache->Match(request, params, error);
|
2015-03-19 18:41:42 +00:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2015-04-27 13:18:52 +00:00
|
|
|
Fail(error.StealNSResult());
|
2015-03-19 18:41:42 +00: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 20:29:40 +00:00
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::Uncached);
|
|
|
|
|
2015-11-16 22:41:03 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
if (aValue.isUndefined()) {
|
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::ToBeCached;
|
2015-11-16 22:41:03 +00:00
|
|
|
rv = mRunnable->LoadScript(mIndex);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
Fail(rv);
|
|
|
|
}
|
2015-03-19 18:41:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aValue.isObject());
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
|
2015-07-02 12:54:00 +00:00
|
|
|
mozilla::dom::Response* response = nullptr;
|
2015-11-16 22:41:03 +00:00
|
|
|
rv = UNWRAP_OBJECT(Response, obj, response);
|
2015-03-19 18:41:42 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
Fail(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
response->GetBody(getter_AddRefs(inputStream));
|
2015-05-25 18:21:05 +00:00
|
|
|
mChannelInfo = response->GetChannelInfo();
|
2015-07-02 12:54:00 +00:00
|
|
|
const UniquePtr<PrincipalInfo>& pInfo = response->GetPrincipalInfo();
|
2015-06-05 04:39:34 +00:00
|
|
|
if (pInfo) {
|
2015-07-02 12:54:00 +00:00
|
|
|
mPrincipalInfo = mozilla::MakeUnique<PrincipalInfo>(*pInfo);
|
2015-06-05 04:39:34 +00:00
|
|
|
}
|
2015-03-19 18:41:42 +00:00
|
|
|
|
|
|
|
if (!inputStream) {
|
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::Cached;
|
2015-06-05 04:39:34 +00:00
|
|
|
mRunnable->DataReceivedFromCache(mIndex, (uint8_t*)"", 0, mChannelInfo,
|
|
|
|
Move(mPrincipalInfo));
|
2015-03-19 18:41:42 +00: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 20:22:15 +00:00
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::ReadingFromCache ||
|
|
|
|
mLoadInfo.mCacheStatus == ScriptLoadInfo::Cancel);
|
2015-03-19 18:41:42 +00:00
|
|
|
Fail(aStatus);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-22 20:22:15 +00:00
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::ReadingFromCache);
|
2015-03-19 18:41:42 +00:00
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::Cached;
|
|
|
|
|
2015-06-05 04:39:34 +00:00
|
|
|
MOZ_ASSERT(mPrincipalInfo);
|
|
|
|
mRunnable->DataReceivedFromCache(mIndex, aString, aStringLen, mChannelInfo,
|
|
|
|
Move(mPrincipalInfo));
|
2015-03-19 18:41:42 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:21:57 +00:00
|
|
|
class ChannelGetterRunnable final : public WorkerMainThreadRunnable
|
2013-03-07 22:27:21 +00:00
|
|
|
{
|
2013-06-05 14:04:23 +00:00
|
|
|
const nsAString& mScriptURL;
|
2013-03-07 22:27:21 +00:00
|
|
|
nsIChannel** mChannel;
|
|
|
|
nsresult mResult;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ChannelGetterRunnable(WorkerPrivate* aParentWorker,
|
2013-06-05 14:04:23 +00:00
|
|
|
const nsAString& aScriptURL,
|
2013-03-07 22:27:21 +00:00
|
|
|
nsIChannel** aChannel)
|
2016-05-03 08:21:57 +00:00
|
|
|
: WorkerMainThreadRunnable(aParentWorker,
|
|
|
|
NS_LITERAL_CSTRING("ScriptLoader :: ChannelGetter"))
|
|
|
|
, mScriptURL(aScriptURL)
|
|
|
|
, mChannel(aChannel)
|
|
|
|
, mResult(NS_ERROR_FAILURE)
|
2013-03-07 22:27:21 +00:00
|
|
|
{
|
2016-05-03 08:21:57 +00:00
|
|
|
MOZ_ASSERT(aParentWorker);
|
2013-03-07 22:27:21 +00:00
|
|
|
aParentWorker->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:21:57 +00:00
|
|
|
virtual bool
|
|
|
|
MainThreadRun() override
|
2013-03-07 22:27:21 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2016-05-03 08:21:57 +00:00
|
|
|
nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
|
2015-03-19 18:41:42 +00:00
|
|
|
MOZ_ASSERT(principal);
|
2013-03-07 22:27:21 +00:00
|
|
|
|
|
|
|
// Figure out our base URI.
|
2016-05-03 08:21:57 +00:00
|
|
|
nsCOMPtr<nsIURI> baseURI = mWorkerPrivate->GetBaseURI();
|
2015-03-19 18:41:42 +00:00
|
|
|
MOZ_ASSERT(baseURI);
|
2013-03-07 22:27:21 +00:00
|
|
|
|
|
|
|
// May be null.
|
2016-05-03 08:21:57 +00:00
|
|
|
nsCOMPtr<nsIDocument> parentDoc = mWorkerPrivate->GetDocument();
|
2013-03-07 22:27:21 +00:00
|
|
|
|
2016-05-03 08:21:57 +00:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = mWorkerPrivate->GetLoadGroup();
|
2014-12-12 16:06:00 +00:00
|
|
|
|
2013-03-07 22:27:21 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
mResult =
|
|
|
|
scriptloader::ChannelFromScriptURLMainThread(principal, baseURI,
|
2014-12-12 16:06:00 +00:00
|
|
|
parentDoc, loadGroup,
|
|
|
|
mScriptURL,
|
2015-06-17 01:21:08 +00:00
|
|
|
// Nested workers are always dedicated.
|
|
|
|
nsIContentPolicy::TYPE_INTERNAL_WORKER,
|
2016-04-28 07:44:08 +00:00
|
|
|
// Nested workers use default uri encoding.
|
|
|
|
true,
|
2013-03-07 22:27:21 +00:00
|
|
|
getter_AddRefs(channel));
|
|
|
|
if (NS_SUCCEEDED(mResult)) {
|
|
|
|
channel.forget(mChannel);
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:21:57 +00:00
|
|
|
return true;
|
2013-03-07 22:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetResult() const
|
|
|
|
{
|
|
|
|
return mResult;
|
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
private:
|
|
|
|
virtual ~ChannelGetterRunnable()
|
|
|
|
{ }
|
2013-03-07 22:27:21 +00:00
|
|
|
};
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
ScriptExecutorRunnable::ScriptExecutorRunnable(
|
|
|
|
ScriptLoaderRunnable& aScriptLoader,
|
2013-10-23 13:16:49 +00:00
|
|
|
nsIEventTarget* aSyncLoopTarget,
|
2015-03-19 18:41:42 +00:00
|
|
|
bool aIsWorkerScript,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aFirstIndex,
|
|
|
|
uint32_t aLastIndex)
|
2013-10-23 13:16:49 +00:00
|
|
|
: MainThreadWorkerSyncRunnable(aScriptLoader.mWorkerPrivate, aSyncLoopTarget),
|
2015-03-19 18:41:42 +00:00
|
|
|
mScriptLoader(aScriptLoader), mIsWorkerScript(aIsWorkerScript),
|
|
|
|
mFirstIndex(aFirstIndex), mLastIndex(aLastIndex)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
MOZ_ASSERT(aFirstIndex <= aLastIndex);
|
|
|
|
MOZ_ASSERT(aLastIndex < aScriptLoader.mLoadInfos.Length());
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 10:15:19 +00: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-01 21:52:26 +00: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 00:09:50 +00: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-01 21:52:26 +00:00
|
|
|
jsapi.ClearException();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
bool
|
|
|
|
ScriptExecutorRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
2016-02-24 15:38:31 +00:00
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
nsTArray<ScriptLoadInfo>& loadInfos = mScriptLoader.mLoadInfos;
|
|
|
|
|
|
|
|
// Don't run if something else has already failed.
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t index = 0; index < mFirstIndex; index++) {
|
2011-07-17 19:09:13 +00: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 15:38:31 +00: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-01 21:52:26 +00: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-10 21:12:55 +00:00
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t index = mFirstIndex; index <= mLastIndex; index++) {
|
2011-07-17 19:09:13 +00: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 15:38:31 +00:00
|
|
|
MOZ_ASSERT(!mScriptLoader.mRv.Failed(), "Who failed it and why?");
|
|
|
|
mScriptLoader.mRv.MightThrowJSException();
|
2011-07-17 19:09:13 +00:00
|
|
|
if (NS_FAILED(loadInfo.mLoadResult)) {
|
2016-02-29 19:52:42 +00:00
|
|
|
scriptloader::ReportLoadError(mScriptLoader.mRv,
|
2016-02-26 20:23:13 +00:00
|
|
|
loadInfo.mLoadResult, loadInfo.mURL);
|
2015-08-30 17:31:52 +00:00
|
|
|
// Top level scripts only!
|
|
|
|
if (mIsWorkerScript) {
|
|
|
|
aWorkerPrivate->MaybeDispatchLoadFailedRunnable();
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 filename(loadInfo.mURL);
|
|
|
|
|
2012-08-27 12:04:37 +00:00
|
|
|
JS::CompileOptions options(aCx);
|
2014-07-31 16:55:20 +00:00
|
|
|
options.setFileAndLine(filename.get(), 1)
|
|
|
|
.setNoScriptRval(true);
|
2014-04-25 14:11:57 +00:00
|
|
|
|
2015-03-17 10:15:19 +00:00
|
|
|
if (mScriptLoader.mWorkerScriptType == DebuggerScript) {
|
|
|
|
options.setVersion(JSVERSION_LATEST);
|
|
|
|
}
|
|
|
|
|
2015-07-14 15:08:38 +00:00
|
|
|
MOZ_ASSERT(loadInfo.mMutedErrorFlag.isSome());
|
|
|
|
options.setMutedErrors(loadInfo.mMutedErrorFlag.valueOr(true));
|
|
|
|
|
2014-04-25 14:11:57 +00:00
|
|
|
JS::SourceBufferHolder srcBuf(loadInfo.mScriptTextBuf,
|
|
|
|
loadInfo.mScriptTextLength,
|
|
|
|
JS::SourceBufferHolder::GiveOwnership);
|
|
|
|
loadInfo.mScriptTextBuf = nullptr;
|
|
|
|
loadInfo.mScriptTextLength = 0;
|
|
|
|
|
2016-02-24 15:38:31 +00:00
|
|
|
// Our ErrorResult still shouldn't be a failure.
|
|
|
|
MOZ_ASSERT(!mScriptLoader.mRv.Failed(), "Who failed it and why?");
|
2014-11-19 04:27:16 +00:00
|
|
|
JS::Rooted<JS::Value> unused(aCx);
|
2015-03-14 05:36:16 +00:00
|
|
|
if (!JS::Evaluate(aCx, options, srcBuf, &unused)) {
|
2016-02-24 15:38:31 +00:00
|
|
|
mScriptLoader.mRv.StealExceptionFromJSContext(aCx);
|
2011-07-17 19:09:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadInfo.mExecutionResult = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ScriptExecutorRunnable::PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aRunResult)
|
|
|
|
{
|
2016-02-24 15:38:31 +00:00
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(aCx), "Who left an exception on there?");
|
|
|
|
|
2011-07-17 19:09:13 +00: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 12:06:55 +00:00
|
|
|
bool mutedError = false;
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t index = 0; index < loadInfos.Length(); index++) {
|
2011-07-17 19:09:13 +00:00
|
|
|
if (!loadInfos[index].mExecutionResult) {
|
2016-02-24 15:38:31 +00:00
|
|
|
mutedError = loadInfos[index].mMutedErrorFlag.valueOr(true);
|
2011-07-17 19:09:13 +00:00
|
|
|
result = false;
|
2016-02-24 15:38:31 +00:00
|
|
|
break;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-24 15:38:31 +00: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 15:38:31 +00:00
|
|
|
!aRunResult);
|
2016-02-24 15:38:31 +00:00
|
|
|
ShutdownScriptLoader(aCx, aWorkerPrivate, result, mutedError);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 18:40:06 +00:00
|
|
|
nsresult
|
2014-01-11 00:37:47 +00:00
|
|
|
ScriptExecutorRunnable::Cancel()
|
|
|
|
{
|
2014-05-20 12:34:25 +00:00
|
|
|
if (mLastIndex == mScriptLoader.mLoadInfos.Length() - 1) {
|
2015-10-15 12:06:55 +00:00
|
|
|
ShutdownScriptLoader(mWorkerPrivate->GetJSContext(), mWorkerPrivate,
|
2016-02-24 15:38:31 +00:00
|
|
|
false, false);
|
2014-05-20 12:34:25 +00:00
|
|
|
}
|
2014-01-13 17:41:02 +00:00
|
|
|
return MainThreadWorkerSyncRunnable::Cancel();
|
2014-01-11 00:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ScriptExecutorRunnable::ShutdownScriptLoader(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
2015-10-15 12:06:55 +00:00
|
|
|
bool aResult,
|
|
|
|
bool aMutedError)
|
2014-01-11 00:37:47 +00:00
|
|
|
{
|
2016-02-24 15:38:31 +00:00
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2015-03-19 18:41:42 +00:00
|
|
|
MOZ_ASSERT(mLastIndex == mScriptLoader.mLoadInfos.Length() - 1);
|
|
|
|
|
|
|
|
if (mIsWorkerScript && aWorkerPrivate->IsServiceWorker()) {
|
|
|
|
aWorkerPrivate->SetLoadingWorkerScript(false);
|
|
|
|
}
|
|
|
|
|
2015-10-15 12:06:55 +00:00
|
|
|
if (!aResult) {
|
2016-02-24 15:38:31 +00:00
|
|
|
// At this point there are two possibilities:
|
2016-02-24 15:38:31 +00: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 15:38:31 +00:00
|
|
|
// events and then replace it on the ErrorResult with a NetworkError,
|
|
|
|
// per spec.
|
2016-02-24 15:38:31 +00:00
|
|
|
//
|
2016-02-24 15:38:31 +00: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 15:38:31 +00:00
|
|
|
if (mScriptLoader.mRv.Failed()) {
|
|
|
|
if (aMutedError && mScriptLoader.mRv.IsJSException()) {
|
|
|
|
LogExceptionToConsole(aCx, aWorkerPrivate);
|
2016-02-24 15:38:31 +00:00
|
|
|
mScriptLoader.mRv.Throw(NS_ERROR_DOM_NETWORK_ERR);
|
2016-02-24 15:38:31 +00:00
|
|
|
}
|
2015-10-15 12:06:55 +00:00
|
|
|
} else {
|
|
|
|
mScriptLoader.mRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-11 00:37:47 +00:00
|
|
|
aWorkerPrivate->StopSyncLoop(mSyncLoopTarget, aResult);
|
|
|
|
}
|
|
|
|
|
2015-10-15 03:05:00 +00:00
|
|
|
void
|
|
|
|
ScriptExecutorRunnable::LogExceptionToConsole(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2016-02-24 15:38:31 +00:00
|
|
|
MOZ_ASSERT(mScriptLoader.mRv.IsJSException());
|
|
|
|
|
2015-10-15 03:05:00 +00:00
|
|
|
JS::Rooted<JS::Value> exn(aCx);
|
2016-02-24 15:38:31 +00:00
|
|
|
if (!ToJSValue(aCx, mScriptLoader.mRv, &exn)) {
|
2015-10-15 03:05:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-24 15:38:31 +00:00
|
|
|
// Now the exception state should all be in exn.
|
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(aCx));
|
|
|
|
MOZ_ASSERT(!mScriptLoader.mRv.Failed());
|
2015-10-15 03:05:00 +00:00
|
|
|
|
|
|
|
js::ErrorReport report(aCx);
|
2016-04-19 23:29:21 +00:00
|
|
|
if (!report.init(aCx, exn, js::ErrorReport::WithSideEffects)) {
|
2015-10-15 03:05:00 +00:00
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<xpc::ErrorReport> xpcReport = new xpc::ErrorReport();
|
2015-10-15 03:05:00 +00:00
|
|
|
xpcReport->Init(report.report(), report.message(),
|
|
|
|
aWorkerPrivate->IsChromeWorker(), aWorkerPrivate->WindowID());
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<AsyncErrorReporter> r = new AsyncErrorReporter(xpcReport);
|
2015-10-15 03:05:00 +00:00
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
}
|
|
|
|
|
2015-10-15 12:06:55 +00:00
|
|
|
void
|
2016-02-29 19:52:42 +00:00
|
|
|
LoadAllScripts(WorkerPrivate* aWorkerPrivate,
|
2015-03-17 10:15:19 +00:00
|
|
|
nsTArray<ScriptLoadInfo>& aLoadInfos, bool aIsMainScript,
|
2015-10-15 12:06:55 +00:00
|
|
|
WorkerScriptType aWorkerScriptType, ErrorResult& aRv)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
NS_ASSERTION(!aLoadInfos.IsEmpty(), "Bad arguments!");
|
|
|
|
|
2012-12-21 20:14:47 +00:00
|
|
|
AutoSyncLoopHolder syncLoop(aWorkerPrivate);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ScriptLoaderRunnable> loader =
|
2013-10-23 13:16:49 +00:00
|
|
|
new ScriptLoaderRunnable(aWorkerPrivate, syncLoop.EventTarget(),
|
2015-10-15 12:06:55 +00:00
|
|
|
aLoadInfos, aIsMainScript, aWorkerScriptType,
|
|
|
|
aRv);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(aLoadInfos.IsEmpty(), "Should have swapped!");
|
|
|
|
|
2016-07-18 07:12:40 +00:00
|
|
|
ScriptLoaderHolder workerHolder(loader);
|
|
|
|
|
2016-08-18 14:11:04 +00:00
|
|
|
if (NS_WARN_IF(!workerHolder.HoldWorker(aWorkerPrivate, Terminating))) {
|
2015-10-15 12:06:55 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 19:53:17 +00:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(loader))) {
|
2011-07-17 19:09:13 +00:00
|
|
|
NS_ERROR("Failed to dispatch!");
|
2015-10-15 12:06:55 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 12:06:55 +00:00
|
|
|
syncLoop.Run();
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* anonymous namespace */
|
|
|
|
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace scriptloader {
|
|
|
|
|
2013-03-07 22:27:21 +00:00
|
|
|
nsresult
|
|
|
|
ChannelFromScriptURLMainThread(nsIPrincipal* aPrincipal,
|
|
|
|
nsIURI* aBaseURI,
|
|
|
|
nsIDocument* aParentDoc,
|
2014-12-12 16:06:00 +00:00
|
|
|
nsILoadGroup* aLoadGroup,
|
2013-06-05 14:04:23 +00:00
|
|
|
const nsAString& aScriptURL,
|
2015-06-17 01:21:08 +00:00
|
|
|
nsContentPolicyType aContentPolicyType,
|
2016-04-28 07:44:08 +00:00
|
|
|
bool aDefaultURIEncoding,
|
2013-03-07 22:27:21 +00:00
|
|
|
nsIChannel** aChannel)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIOService> ios(do_GetIOService());
|
|
|
|
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
NS_ASSERTION(secMan, "This should never be null!");
|
|
|
|
|
2014-12-12 16:06:00 +00:00
|
|
|
return ChannelFromScriptURL(aPrincipal, aBaseURI, aParentDoc, aLoadGroup,
|
2015-06-17 01:21:08 +00:00
|
|
|
ios, secMan, aScriptURL, true, WorkerScript,
|
2015-10-06 13:37:07 +00:00
|
|
|
aContentPolicyType, nsIRequest::LOAD_NORMAL,
|
2016-04-28 07:44:08 +00:00
|
|
|
aDefaultURIEncoding, aChannel);
|
2013-03-07 22:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ChannelFromScriptURLWorkerThread(JSContext* aCx,
|
|
|
|
WorkerPrivate* aParent,
|
2013-06-05 14:04:23 +00:00
|
|
|
const nsAString& aScriptURL,
|
2013-03-07 22:27:21 +00:00
|
|
|
nsIChannel** aChannel)
|
|
|
|
{
|
|
|
|
aParent->AssertIsOnWorkerThread();
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ChannelGetterRunnable> getter =
|
2016-05-03 08:21:57 +00:00
|
|
|
new ChannelGetterRunnable(aParent, aScriptURL, aChannel);
|
2013-03-07 22:27:21 +00:00
|
|
|
|
2016-05-03 08:21:57 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
getter->Dispatch(rv);
|
|
|
|
if (rv.Failed()) {
|
2013-03-07 22:27:21 +00:00
|
|
|
NS_ERROR("Failed to dispatch!");
|
2016-05-03 08:21:57 +00:00
|
|
|
return rv.StealNSResult();
|
2013-03-07 22:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return getter->GetResult();
|
|
|
|
}
|
|
|
|
|
2016-02-29 19:52:42 +00:00
|
|
|
void ReportLoadError(ErrorResult& aRv, nsresult aLoadResult,
|
2016-02-26 20:23:13 +00:00
|
|
|
const nsAString& aScriptURL)
|
2013-03-07 22:27:21 +00:00
|
|
|
{
|
2016-02-24 15:38:31 +00:00
|
|
|
MOZ_ASSERT(!aRv.Failed());
|
2013-03-07 22:27:21 +00:00
|
|
|
|
2016-02-24 15:38:31 +00:00
|
|
|
switch (aLoadResult) {
|
2013-03-07 22:27:21 +00:00
|
|
|
case NS_ERROR_FILE_NOT_FOUND:
|
|
|
|
case NS_ERROR_NOT_AVAILABLE:
|
2016-02-26 20:23:13 +00:00
|
|
|
aLoadResult = NS_ERROR_DOM_NETWORK_ERR;
|
2013-03-07 22:27:21 +00:00
|
|
|
break;
|
|
|
|
|
2015-08-30 17:31:52 +00:00
|
|
|
case NS_ERROR_MALFORMED_URI:
|
|
|
|
aLoadResult = NS_ERROR_DOM_SYNTAX_ERR;
|
2016-02-26 20:23:13 +00:00
|
|
|
break;
|
|
|
|
|
2016-02-24 15:38:31 +00: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 20:23:13 +00: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-07 22:27:21 +00:00
|
|
|
case NS_ERROR_DOM_SECURITY_ERR:
|
|
|
|
case NS_ERROR_DOM_SYNTAX_ERR:
|
2016-02-26 20:23:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NS_ERROR_DOM_BAD_URI:
|
|
|
|
// This is actually a security error.
|
|
|
|
aLoadResult = NS_ERROR_DOM_SECURITY_ERR;
|
2013-03-07 22:27:21 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-02-26 20:23:13 +00: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-07 22:27:21 +00:00
|
|
|
}
|
2016-02-26 20:23:13 +00:00
|
|
|
|
|
|
|
aRv.ThrowDOMException(aLoadResult,
|
|
|
|
NS_LITERAL_CSTRING("Failed to load worker script at \"") +
|
|
|
|
NS_ConvertUTF16toUTF8(aScriptURL) +
|
|
|
|
NS_LITERAL_CSTRING("\""));
|
2013-03-07 22:27:21 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 12:06:55 +00:00
|
|
|
void
|
2016-02-29 19:52:42 +00:00
|
|
|
LoadMainScript(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsAString& aScriptURL,
|
2015-10-15 12:06:55 +00:00
|
|
|
WorkerScriptType aWorkerScriptType,
|
|
|
|
ErrorResult& aRv)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
nsTArray<ScriptLoadInfo> loadInfos;
|
|
|
|
|
|
|
|
ScriptLoadInfo* info = loadInfos.AppendElement();
|
2015-03-17 10:15:19 +00:00
|
|
|
info->mURL = aScriptURL;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2016-02-29 19:52:42 +00:00
|
|
|
LoadAllScripts(aWorkerPrivate, loadInfos, true, aWorkerScriptType, aRv);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
void
|
2016-02-29 19:52:42 +00:00
|
|
|
Load(WorkerPrivate* aWorkerPrivate,
|
2015-03-31 10:22:40 +00:00
|
|
|
const nsTArray<nsString>& aScriptURLs, WorkerScriptType aWorkerScriptType,
|
|
|
|
ErrorResult& aRv)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2013-11-05 14:16:26 +00:00
|
|
|
const uint32_t urlCount = aScriptURLs.Length();
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
if (!urlCount) {
|
|
|
|
return;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
if (urlCount > MAX_CONCURRENT_SCRIPTS) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<ScriptLoadInfo> loadInfos;
|
2013-11-05 14:16:26 +00:00
|
|
|
loadInfos.SetLength(urlCount);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
for (uint32_t index = 0; index < urlCount; index++) {
|
|
|
|
loadInfos[index].mURL = aScriptURLs[index];
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 19:52:42 +00:00
|
|
|
LoadAllScripts(aWorkerPrivate, loadInfos, false, aWorkerScriptType, aRv);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace scriptloader
|
|
|
|
|
|
|
|
END_WORKERS_NAMESPACE
|