2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#include "ScriptLoader.h"
|
|
|
|
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
#include "nsIContentSecurityPolicy.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
2015-07-02 17:30:06 +03:00
|
|
|
#include "nsIHttpChannelInternal.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
#include "nsIInputStreamPump.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsIProtocolHandler.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "nsIScriptError.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsIStreamLoader.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
#include "nsIStreamListenerTee.h"
|
|
|
|
#include "nsIThreadRetargetableRequest.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsIURI.h"
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
2015-10-15 06:05:00 +03:00
|
|
|
#include "jsfriendapi.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDocShellCID.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsIPipe.h"
|
|
|
|
#include "nsIOutputStream.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsScriptLoader.h"
|
2013-09-23 21:25:00 +04:00
|
|
|
#include "nsString.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
#include "nsStreamUtils.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXPCOM.h"
|
2013-03-08 02:27:21 +04:00
|
|
|
#include "xpcpublic.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2014-12-12 19:06:00 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/LoadContext.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
|
|
|
#include "mozilla/dom/CacheBinding.h"
|
2015-06-05 07:39:34 +03:00
|
|
|
#include "mozilla/dom/cache/CacheTypes.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
#include "mozilla/dom/cache/Cache.h"
|
|
|
|
#include "mozilla/dom/cache/CacheStorage.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "mozilla/dom/ChannelInfo.h"
|
2013-09-09 07:29:21 +04:00
|
|
|
#include "mozilla/dom/Exceptions.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
#include "mozilla/dom/InternalResponse.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
2015-03-19 21:41:42 +03:00
|
|
|
#include "mozilla/dom/Response.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "Principal.h"
|
|
|
|
#include "WorkerFeature.h"
|
|
|
|
#include "WorkerPrivate.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "WorkerRunnable.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "WorkerScope.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#define MAX_CONCURRENT_SCRIPTS 1000
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
using mozilla::dom::cache::Cache;
|
|
|
|
using mozilla::dom::cache::CacheStorage;
|
|
|
|
using mozilla::dom::Promise;
|
|
|
|
using mozilla::dom::PromiseNativeHandler;
|
2015-07-02 15:54:00 +03:00
|
|
|
using mozilla::ErrorResult;
|
|
|
|
using mozilla::ipc::PrincipalInfo;
|
|
|
|
using mozilla::UniquePtr;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
namespace {
|
2013-03-08 02:27:21 +04:00
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
nsIURI*
|
|
|
|
GetBaseURI(bool aIsMainScript, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
nsIURI* baseURI;
|
|
|
|
WorkerPrivate* parentWorker = aWorkerPrivate->GetParent();
|
|
|
|
if (aIsMainScript) {
|
|
|
|
if (parentWorker) {
|
|
|
|
baseURI = parentWorker->GetBaseURI();
|
|
|
|
NS_ASSERTION(baseURI, "Should have been set already!");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// May be null.
|
|
|
|
baseURI = aWorkerPrivate->GetBaseURI();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
baseURI = aWorkerPrivate->GetBaseURI();
|
|
|
|
NS_ASSERTION(baseURI, "Should have been set already!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return baseURI;
|
|
|
|
}
|
|
|
|
|
2013-03-08 02:27:21 +04:00
|
|
|
nsresult
|
|
|
|
ChannelFromScriptURL(nsIPrincipal* principal,
|
|
|
|
nsIURI* baseURI,
|
|
|
|
nsIDocument* parentDoc,
|
|
|
|
nsILoadGroup* loadGroup,
|
|
|
|
nsIIOService* ios,
|
|
|
|
nsIScriptSecurityManager* secMan,
|
2013-06-05 18:04:23 +04:00
|
|
|
const nsAString& aScriptURL,
|
2015-03-17 13:15:19 +03:00
|
|
|
bool aIsMainScript,
|
|
|
|
WorkerScriptType aWorkerScriptType,
|
2015-06-17 04:21:08 +03:00
|
|
|
nsContentPolicyType aContentPolicyType,
|
2015-10-06 16:37:07 +03:00
|
|
|
nsLoadFlags aLoadFlags,
|
2013-06-05 18:04:23 +04:00
|
|
|
nsIChannel** aChannel)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-03-08 02:27:21 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(uri),
|
|
|
|
aScriptURL, parentDoc,
|
|
|
|
baseURI);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
2013-03-08 02:27:21 +04:00
|
|
|
|
2015-11-12 20:44:51 +03:00
|
|
|
int16_t shouldLoad = nsIContentPolicy::ACCEPT;
|
|
|
|
rv = NS_CheckContentLoadPolicy(aContentPolicyType, uri,
|
|
|
|
principal, parentDoc,
|
|
|
|
NS_LITERAL_CSTRING("text/javascript"),
|
|
|
|
nullptr, &shouldLoad,
|
|
|
|
nsContentUtils::GetContentPolicy(),
|
|
|
|
secMan);
|
|
|
|
if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) {
|
|
|
|
if (NS_FAILED(rv) || shouldLoad != nsIContentPolicy::REJECT_TYPE) {
|
|
|
|
return rv = NS_ERROR_CONTENT_BLOCKED;
|
|
|
|
}
|
|
|
|
return rv = NS_ERROR_CONTENT_BLOCKED_SHOW_ALT;
|
|
|
|
}
|
2013-03-08 02:27:21 +04:00
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
if (aWorkerScriptType == DebuggerScript) {
|
2015-11-12 20:44:51 +03:00
|
|
|
bool isChrome = false;
|
|
|
|
NS_ENSURE_SUCCESS(uri->SchemeIs("chrome", &isChrome),
|
|
|
|
NS_ERROR_DOM_SECURITY_ERR);
|
2015-11-11 21:22:08 +03:00
|
|
|
|
2015-11-12 20:44:51 +03:00
|
|
|
bool isResource = false;
|
|
|
|
NS_ENSURE_SUCCESS(uri->SchemeIs("resource", &isResource),
|
|
|
|
NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
|
|
|
|
if (!isChrome && !isResource) {
|
2015-03-17 13:15:19 +03:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
2015-11-12 20:44:51 +03:00
|
|
|
} else if (aIsMainScript) {
|
|
|
|
// We pass true as the 3rd argument to checkMayLoad here.
|
|
|
|
// This allows workers in sandboxed documents to load data URLs
|
|
|
|
// (and other URLs that inherit their principal from their
|
|
|
|
// creator.)
|
|
|
|
rv = principal->CheckMayLoad(uri, false, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SECURITY_ERR);
|
2015-11-11 19:59:46 +03:00
|
|
|
}
|
2015-11-12 20:44:51 +03:00
|
|
|
else {
|
|
|
|
rv = secMan->CheckLoadURIWithPrincipal(principal, uri, 0);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SECURITY_ERR);
|
2015-11-12 17:51:17 +03:00
|
|
|
}
|
2015-11-11 21:22:08 +03:00
|
|
|
|
2015-11-12 20:44:51 +03:00
|
|
|
aLoadFlags |= nsIChannel::LOAD_CLASSIFY_URI;
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2015-11-12 01:27:52 +03:00
|
|
|
// If we have the document, use it
|
|
|
|
if (parentDoc) {
|
2014-09-21 20:39:57 +04:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
uri,
|
|
|
|
parentDoc,
|
2015-11-12 20:44:51 +03:00
|
|
|
nsILoadInfo::SEC_NORMAL,
|
2015-06-17 04:21:08 +03:00
|
|
|
aContentPolicyType,
|
2014-09-21 20:39:57 +04:00
|
|
|
loadGroup,
|
|
|
|
nullptr, // aCallbacks
|
2015-10-06 16:37:07 +03:00
|
|
|
aLoadFlags,
|
2014-09-21 20:39:57 +04:00
|
|
|
ios);
|
|
|
|
} else {
|
2014-12-12 19:06:00 +03:00
|
|
|
// We must have a loadGroup with a load context for the principal to
|
|
|
|
// traverse the channel correctly.
|
|
|
|
MOZ_ASSERT(loadGroup);
|
|
|
|
MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(loadGroup, principal));
|
|
|
|
|
2014-09-21 20:39:57 +04:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
uri,
|
2014-12-12 19:06:00 +03:00
|
|
|
principal,
|
2015-11-12 20:44:51 +03:00
|
|
|
nsILoadInfo::SEC_NORMAL,
|
2015-06-17 04:21:08 +03:00
|
|
|
aContentPolicyType,
|
2014-09-21 20:39:57 +04:00
|
|
|
loadGroup,
|
|
|
|
nullptr, // aCallbacks
|
2015-10-06 16:37:07 +03:00
|
|
|
aLoadFlags,
|
2014-09-21 20:39:57 +04:00
|
|
|
ios);
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-06-05 02:20:13 +03:00
|
|
|
if (nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(channel)) {
|
|
|
|
rv = nsContentUtils::SetFetchReferrerURIWithPolicy(principal, parentDoc,
|
|
|
|
httpChannel);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
channel.forget(aChannel);
|
|
|
|
return rv;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
struct ScriptLoadInfo
|
|
|
|
{
|
|
|
|
ScriptLoadInfo()
|
2014-04-25 18:11:57 +04:00
|
|
|
: mScriptTextBuf(nullptr)
|
|
|
|
, mScriptTextLength(0)
|
2015-03-19 21:41:42 +03:00
|
|
|
, mLoadResult(NS_ERROR_NOT_INITIALIZED)
|
|
|
|
, mLoadingFinished(false)
|
|
|
|
, mExecutionScheduled(false)
|
2014-04-25 18:11:57 +04:00
|
|
|
, mExecutionResult(false)
|
2015-03-19 21:41:42 +03:00
|
|
|
, mCacheStatus(Uncached)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
2014-04-25 18:11:57 +04:00
|
|
|
~ScriptLoadInfo()
|
|
|
|
{
|
|
|
|
if (mScriptTextBuf) {
|
|
|
|
js_free(mScriptTextBuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
bool
|
|
|
|
ReadyToExecute()
|
|
|
|
{
|
|
|
|
return !mChannel && NS_SUCCEEDED(mLoadResult) && !mExecutionScheduled;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString mURL;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
|
|
// This full URL string is populated only if this object is used in a
|
|
|
|
// ServiceWorker.
|
|
|
|
nsString mFullURL;
|
|
|
|
|
|
|
|
// This promise is set only when the script is for a ServiceWorker but
|
|
|
|
// it's not in the cache yet. The promise is resolved when the full body is
|
|
|
|
// stored into the cache. mCachePromise will be set to nullptr after
|
|
|
|
// resolution.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> mCachePromise;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
2014-07-22 08:43:21 +04:00
|
|
|
char16_t* mScriptTextBuf;
|
2014-04-25 18:11:57 +04:00
|
|
|
size_t mScriptTextLength;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
nsresult mLoadResult;
|
2015-03-19 21:41:42 +03:00
|
|
|
bool mLoadingFinished;
|
2011-07-17 23:09:13 +04:00
|
|
|
bool mExecutionScheduled;
|
|
|
|
bool mExecutionResult;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
|
|
enum CacheStatus {
|
|
|
|
// By default a normal script is just loaded from the network. But for
|
|
|
|
// ServiceWorkers, we have to check if the cache contains the script and
|
|
|
|
// load it from the cache.
|
|
|
|
Uncached,
|
|
|
|
|
|
|
|
WritingToCache,
|
|
|
|
|
|
|
|
ReadingFromCache,
|
|
|
|
|
|
|
|
// This script has been loaded from the ServiceWorker cache.
|
|
|
|
Cached,
|
|
|
|
|
|
|
|
// This script must be stored in the ServiceWorker cache.
|
|
|
|
ToBeCached,
|
|
|
|
|
|
|
|
// Something went wrong or the worker went away.
|
|
|
|
Cancel
|
|
|
|
};
|
|
|
|
|
|
|
|
CacheStatus mCacheStatus;
|
|
|
|
|
2015-07-14 18:08:38 +03:00
|
|
|
Maybe<bool> mMutedErrorFlag;
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
bool Finished() const
|
|
|
|
{
|
|
|
|
return mLoadingFinished && !mCachePromise && !mChannel;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class ScriptLoaderRunnable;
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class ScriptExecutorRunnable final : public MainThreadWorkerSyncRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
ScriptLoaderRunnable& mScriptLoader;
|
2015-03-19 21:41:42 +03:00
|
|
|
bool mIsWorkerScript;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mFirstIndex;
|
|
|
|
uint32_t mLastIndex;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
ScriptExecutorRunnable(ScriptLoaderRunnable& aScriptLoader,
|
2015-03-19 21:41:42 +03:00
|
|
|
nsIEventTarget* aSyncLoopTarget,
|
|
|
|
bool aIsWorkerScript,
|
|
|
|
uint32_t aFirstIndex,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aLastIndex);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~ScriptExecutorRunnable()
|
|
|
|
{ }
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
IsDebuggerRunnable() const override;
|
2015-03-17 13:15:19 +03:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual void
|
|
|
|
PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, bool aRunResult)
|
2015-03-21 19:28:04 +03:00
|
|
|
override;
|
2014-01-11 04:37:47 +04:00
|
|
|
|
|
|
|
NS_DECL_NSICANCELABLERUNNABLE
|
|
|
|
|
|
|
|
void
|
|
|
|
ShutdownScriptLoader(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
2015-10-15 15:06:55 +03:00
|
|
|
bool aResult,
|
|
|
|
bool aMutedError);
|
2015-10-15 06:05:00 +03:00
|
|
|
|
|
|
|
void LogExceptionToConsole(JSContext* aCx,
|
|
|
|
WorkerPrivate* WorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
class CacheScriptLoader;
|
|
|
|
|
|
|
|
class CacheCreator final : public PromiseNativeHandler
|
|
|
|
{
|
|
|
|
public:
|
2015-07-09 09:56:00 +03:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
explicit CacheCreator(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mCacheName(aWorkerPrivate->ServiceWorkerCacheName())
|
2015-06-27 05:36:40 +03:00
|
|
|
, mPrivateBrowsing(aWorkerPrivate->IsInPrivateBrowsing())
|
2015-03-19 21:41:42 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
|
2015-03-20 02:39:01 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate->LoadScriptAsPartOfLoadingServiceWorkerScript());
|
2015-03-19 21:41:42 +03:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AddLoader(CacheScriptLoader* aLoader)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(!mCacheStorage);
|
|
|
|
mLoaders.AppendElement(aLoader);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
// Try to load from cache with aPrincipal used for cache access.
|
|
|
|
nsresult
|
|
|
|
Load(nsIPrincipal* aPrincipal);
|
|
|
|
|
|
|
|
Cache*
|
|
|
|
Cache_() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mCache);
|
|
|
|
return mCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIGlobalObject*
|
|
|
|
Global() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mSandboxGlobalObject);
|
|
|
|
return mSandboxGlobalObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DeleteCache();
|
|
|
|
|
|
|
|
private:
|
|
|
|
~CacheCreator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CreateCacheStorage(nsIPrincipal* aPrincipal);
|
|
|
|
|
|
|
|
void
|
|
|
|
FailLoaders(nsresult aRv);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Cache> mCache;
|
|
|
|
RefPtr<CacheStorage> mCacheStorage;
|
2015-03-19 21:41:42 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> mSandboxGlobalObject;
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<CacheScriptLoader>> mLoaders;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
|
|
nsString mCacheName;
|
2015-06-27 05:36:40 +03:00
|
|
|
bool mPrivateBrowsing;
|
2015-03-19 21:41:42 +03:00
|
|
|
};
|
|
|
|
|
2015-07-09 09:56:00 +03:00
|
|
|
NS_IMPL_ISUPPORTS0(CacheCreator)
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
class CacheScriptLoader final : public PromiseNativeHandler
|
|
|
|
, public nsIStreamLoaderObserver
|
|
|
|
{
|
|
|
|
public:
|
2015-07-09 09:56:00 +03:00
|
|
|
NS_DECL_ISUPPORTS
|
2015-03-19 21:41:42 +03:00
|
|
|
NS_DECL_NSISTREAMLOADEROBSERVER
|
|
|
|
|
|
|
|
CacheScriptLoader(WorkerPrivate* aWorkerPrivate, ScriptLoadInfo& aLoadInfo,
|
|
|
|
uint32_t aIndex, bool aIsWorkerScript,
|
|
|
|
ScriptLoaderRunnable* aRunnable)
|
|
|
|
: mLoadInfo(aLoadInfo)
|
|
|
|
, mIndex(aIndex)
|
|
|
|
, mRunnable(aRunnable)
|
|
|
|
, mIsWorkerScript(aIsWorkerScript)
|
|
|
|
, mFailed(false)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
|
|
|
|
mBaseURI = GetBaseURI(mIsWorkerScript, aWorkerPrivate);
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Fail(nsresult aRv);
|
|
|
|
|
|
|
|
void
|
|
|
|
Load(Cache* aCache);
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
~CacheScriptLoader()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptLoadInfo& mLoadInfo;
|
|
|
|
uint32_t mIndex;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ScriptLoaderRunnable> mRunnable;
|
2015-03-19 21:41:42 +03:00
|
|
|
bool mIsWorkerScript;
|
|
|
|
bool mFailed;
|
|
|
|
nsCOMPtr<nsIInputStreamPump> mPump;
|
|
|
|
nsCOMPtr<nsIURI> mBaseURI;
|
2015-07-02 15:54:00 +03:00
|
|
|
mozilla::dom::ChannelInfo mChannelInfo;
|
2015-06-05 07:39:34 +03:00
|
|
|
UniquePtr<PrincipalInfo> mPrincipalInfo;
|
2015-03-19 21:41:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(CacheScriptLoader, nsIStreamLoaderObserver)
|
|
|
|
|
|
|
|
class CachePromiseHandler final : public PromiseNativeHandler
|
|
|
|
{
|
|
|
|
public:
|
2015-07-09 09:56:00 +03:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
CachePromiseHandler(ScriptLoaderRunnable* aRunnable,
|
|
|
|
ScriptLoadInfo& aLoadInfo,
|
|
|
|
uint32_t aIndex)
|
|
|
|
: mRunnable(aRunnable)
|
|
|
|
, mLoadInfo(aLoadInfo)
|
|
|
|
, mIndex(aIndex)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
~CachePromiseHandler()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ScriptLoaderRunnable> mRunnable;
|
2015-03-19 21:41:42 +03:00
|
|
|
ScriptLoadInfo& mLoadInfo;
|
|
|
|
uint32_t mIndex;
|
|
|
|
};
|
|
|
|
|
2015-07-09 09:56:00 +03:00
|
|
|
NS_IMPL_ISUPPORTS0(CachePromiseHandler)
|
|
|
|
|
2015-11-12 20:44:51 +03:00
|
|
|
class ScriptLoaderRunnable final : public WorkerFeature,
|
|
|
|
public nsIRunnable,
|
|
|
|
public nsIStreamLoaderObserver,
|
|
|
|
public nsIRequestObserver
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
friend class ScriptExecutorRunnable;
|
2015-03-19 21:41:42 +03:00
|
|
|
friend class CachePromiseHandler;
|
|
|
|
friend class CacheScriptLoader;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2013-10-23 17:16:49 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
2011-07-17 23:09:13 +04:00
|
|
|
nsTArray<ScriptLoadInfo> mLoadInfos;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheCreator> mCacheCreator;
|
2015-04-24 02:10:57 +03:00
|
|
|
nsCOMPtr<nsIInputStream> mReader;
|
2015-03-17 13:15:19 +03:00
|
|
|
bool mIsMainScript;
|
|
|
|
WorkerScriptType mWorkerScriptType;
|
2011-07-17 23:09:13 +04:00
|
|
|
bool mCanceled;
|
|
|
|
bool mCanceledMainThread;
|
2015-10-15 15:06:55 +03:00
|
|
|
ErrorResult& mRv;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
ScriptLoaderRunnable(WorkerPrivate* aWorkerPrivate,
|
2013-10-23 17:16:49 +04:00
|
|
|
nsIEventTarget* aSyncLoopTarget,
|
2011-07-17 23:09:13 +04:00
|
|
|
nsTArray<ScriptLoadInfo>& aLoadInfos,
|
2015-03-17 13:15:19 +03:00
|
|
|
bool aIsMainScript,
|
2015-10-15 15:06:55 +03:00
|
|
|
WorkerScriptType aWorkerScriptType,
|
|
|
|
ErrorResult& aRv)
|
2013-10-23 17:16:49 +04:00
|
|
|
: mWorkerPrivate(aWorkerPrivate), mSyncLoopTarget(aSyncLoopTarget),
|
2015-03-17 13:15:19 +03:00
|
|
|
mIsMainScript(aIsMainScript), mWorkerScriptType(aWorkerScriptType),
|
2015-10-15 15:06:55 +03:00
|
|
|
mCanceled(false), mCanceledMainThread(false), mRv(aRv)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aSyncLoopTarget);
|
2015-03-17 13:15:19 +03:00
|
|
|
MOZ_ASSERT_IF(aIsMainScript, aLoadInfos.Length() == 1);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-04-01 21:35:13 +04:00
|
|
|
mLoadInfos.SwapElements(aLoadInfos);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~ScriptLoaderRunnable()
|
|
|
|
{ }
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_IMETHOD
|
2015-03-21 19:28:04 +03:00
|
|
|
Run() override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (NS_FAILED(RunInternal())) {
|
|
|
|
CancelMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
void
|
|
|
|
LoadingFinished(uint32_t aIndex, nsresult aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
|
|
|
|
|
|
|
loadInfo.mLoadResult = aRv;
|
|
|
|
|
|
|
|
MOZ_ASSERT(!loadInfo.mLoadingFinished);
|
|
|
|
loadInfo.mLoadingFinished = true;
|
|
|
|
|
|
|
|
MaybeExecuteFinishedScripts(aIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MaybeExecuteFinishedScripts(uint32_t aIndex)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
|
|
|
|
|
|
|
// We execute the last step if we don't have a pending operation with the
|
|
|
|
// cache and the loading is completed.
|
2015-03-25 03:11:57 +03:00
|
|
|
if (loadInfo.Finished()) {
|
2015-03-19 21:41:42 +03:00
|
|
|
ExecuteFinishedScripts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 20:44:51 +03:00
|
|
|
NS_IMETHOD
|
|
|
|
OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult aStatus, uint32_t aStringLen,
|
2015-11-12 20:44:51 +03:00
|
|
|
const uint8_t* aString) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-11-12 20:44:51 +03:00
|
|
|
nsCOMPtr<nsISupportsPRUint32> indexSupports(do_QueryInterface(aContext));
|
|
|
|
NS_ASSERTION(indexSupports, "This should never fail!");
|
|
|
|
|
|
|
|
uint32_t index = UINT32_MAX;
|
|
|
|
if (NS_FAILED(indexSupports->GetData(&index)) ||
|
|
|
|
index >= mLoadInfos.Length()) {
|
|
|
|
NS_ERROR("Bad index!");
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[index];
|
|
|
|
|
|
|
|
nsresult rv = OnStreamCompleteInternal(aLoader, aContext, aStatus,
|
|
|
|
aStringLen, aString, loadInfo);
|
|
|
|
LoadingFinished(index, rv);
|
2011-07-17 23:09:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-12 20:44:51 +03:00
|
|
|
NS_IMETHOD
|
|
|
|
OnStartRequest(nsIRequest* aRequest, nsISupports* aContext) override
|
2015-04-24 02:10:57 +03:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-11-12 20:44:51 +03:00
|
|
|
nsCOMPtr<nsISupportsPRUint32> indexSupports(do_QueryInterface(aContext));
|
|
|
|
MOZ_ASSERT(indexSupports, "This should never fail!");
|
|
|
|
|
|
|
|
uint32_t index = UINT32_MAX;
|
|
|
|
if (NS_FAILED(indexSupports->GetData(&index)) ||
|
|
|
|
index >= mLoadInfos.Length()) {
|
|
|
|
MOZ_CRASH("Bad index!");
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[index];
|
2015-04-24 02:10:57 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
|
|
|
|
MOZ_ASSERT(channel == loadInfo.mChannel);
|
|
|
|
|
|
|
|
// We synthesize the result code, but its never exposed to content.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<mozilla::dom::InternalResponse> ir =
|
2015-07-02 15:54:00 +03:00
|
|
|
new mozilla::dom::InternalResponse(200, NS_LITERAL_CSTRING("OK"));
|
2015-04-24 02:10:57 +03:00
|
|
|
ir->SetBody(mReader);
|
|
|
|
|
2015-05-25 21:21:05 +03:00
|
|
|
// Set the channel info of the channel on the response so that it's
|
2015-04-24 02:10:57 +03:00
|
|
|
// saved in the cache.
|
2015-05-25 21:21:05 +03:00
|
|
|
ir->InitChannelInfo(channel);
|
2015-04-24 02:10:57 +03:00
|
|
|
|
2015-06-05 07:39:34 +03:00
|
|
|
// Save the principal of the channel since its URI encodes the script URI
|
|
|
|
// rather than the ServiceWorkerRegistrationInfo URI.
|
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
NS_ASSERTION(ssm, "Should never be null!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> channelPrincipal;
|
|
|
|
nsresult rv = ssm->GetChannelResultPrincipal(channel, getter_AddRefs(channelPrincipal));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
channel->Cancel(rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<PrincipalInfo> principalInfo(new PrincipalInfo());
|
|
|
|
rv = PrincipalToPrincipalInfo(channelPrincipal, principalInfo.get());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
channel->Cancel(rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
ir->SetPrincipalInfo(Move(principalInfo));
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<mozilla::dom::Response> response =
|
2015-07-02 15:54:00 +03:00
|
|
|
new mozilla::dom::Response(mCacheCreator->Global(), ir);
|
2015-04-24 02:10:57 +03:00
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
mozilla::dom::RequestOrUSVString request;
|
2015-04-24 02:10:57 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(!loadInfo.mFullURL.IsEmpty());
|
|
|
|
request.SetAsUSVString().Rebind(loadInfo.mFullURL.Data(),
|
|
|
|
loadInfo.mFullURL.Length());
|
|
|
|
|
|
|
|
ErrorResult error;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> cachePromise =
|
2015-04-24 02:10:57 +03:00
|
|
|
mCacheCreator->Cache_()->Put(request, *response, error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
nsresult rv = error.StealNSResult();
|
|
|
|
channel->Cancel(rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CachePromiseHandler> promiseHandler =
|
2015-11-12 20:44:51 +03:00
|
|
|
new CachePromiseHandler(this, loadInfo, index);
|
2015-04-24 02:10:57 +03:00
|
|
|
cachePromise->AppendNativeHandler(promiseHandler);
|
|
|
|
|
|
|
|
loadInfo.mCachePromise.swap(cachePromise);
|
|
|
|
loadInfo.mCacheStatus = ScriptLoadInfo::WritingToCache;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-12 20:44:51 +03:00
|
|
|
NS_IMETHOD
|
|
|
|
OnStopRequest(nsIRequest* aRequest, nsISupports* aContext,
|
|
|
|
nsresult aStatusCode) override
|
|
|
|
{
|
|
|
|
// Nothing to do here!
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
Notify(JSContext* aCx, Status aStatus) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (aStatus >= Terminating && !mCanceled) {
|
|
|
|
mCanceled = true;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
NS_NewRunnableMethod(this, &ScriptLoaderRunnable::CancelMainThread);
|
|
|
|
NS_ASSERTION(runnable, "This should never fail!");
|
|
|
|
|
2014-05-23 23:53:17 +04:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(runnable))) {
|
2011-07-17 23:09:13 +04:00
|
|
|
JS_ReportError(aCx, "Failed to cancel script loader!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
bool
|
|
|
|
IsMainWorkerScript() const
|
|
|
|
{
|
|
|
|
return mIsMainScript && mWorkerScriptType == WorkerScript;
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
void
|
|
|
|
CancelMainThread()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (mCanceledMainThread) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCanceledMainThread = true;
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
if (mCacheCreator) {
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsServiceWorker());
|
|
|
|
DeleteCache();
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
// Cancel all the channels that were already opened.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < mLoadInfos.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[index];
|
|
|
|
|
2015-03-27 01:56:02 +03:00
|
|
|
// If promise or channel is non-null, their failures will lead to
|
2015-03-25 03:11:57 +03:00
|
|
|
// LoadingFinished being called.
|
|
|
|
bool callLoadingFinished = true;
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
if (loadInfo.mCachePromise) {
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsServiceWorker());
|
|
|
|
loadInfo.mCachePromise->MaybeReject(NS_BINDING_ABORTED);
|
|
|
|
loadInfo.mCachePromise = nullptr;
|
2015-03-25 03:11:57 +03:00
|
|
|
callLoadingFinished = false;
|
2015-03-19 21:41:42 +03:00
|
|
|
}
|
|
|
|
|
2015-03-25 03:11:57 +03:00
|
|
|
if (loadInfo.mChannel) {
|
|
|
|
if (NS_SUCCEEDED(loadInfo.mChannel->Cancel(NS_BINDING_ABORTED))) {
|
|
|
|
callLoadingFinished = false;
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Failed to cancel channel!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callLoadingFinished && !loadInfo.Finished()) {
|
2015-03-19 21:41:42 +03:00
|
|
|
LoadingFinished(index, NS_BINDING_ABORTED);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExecuteFinishedScripts();
|
|
|
|
}
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
void
|
|
|
|
DeleteCache()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (!mCacheCreator) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCacheCreator->DeleteCache();
|
|
|
|
mCacheCreator = nullptr;
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
nsresult
|
|
|
|
RunInternal()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
if (IsMainWorkerScript() && mWorkerPrivate->IsServiceWorker()) {
|
|
|
|
mWorkerPrivate->SetLoadingWorkerScript(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mWorkerPrivate->IsServiceWorker() ||
|
2015-03-20 02:39:01 +03:00
|
|
|
!mWorkerPrivate->LoadScriptAsPartOfLoadingServiceWorkerScript()) {
|
2015-03-19 21:41:42 +03:00
|
|
|
for (uint32_t index = 0, len = mLoadInfos.Length(); index < len;
|
|
|
|
++index) {
|
|
|
|
nsresult rv = LoadScript(index);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mCacheCreator);
|
|
|
|
mCacheCreator = new CacheCreator(mWorkerPrivate);
|
|
|
|
|
|
|
|
for (uint32_t index = 0, len = mLoadInfos.Length(); index < len; ++index) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheScriptLoader> loader =
|
2015-03-19 21:41:42 +03:00
|
|
|
new CacheScriptLoader(mWorkerPrivate, mLoadInfos[index], index,
|
|
|
|
IsMainWorkerScript(), this);
|
|
|
|
mCacheCreator->AddLoader(loader);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The worker may have a null principal on first load, but in that case its
|
|
|
|
// parent definitely will have one.
|
|
|
|
nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
|
|
|
|
if (!principal) {
|
|
|
|
WorkerPrivate* parentWorker = mWorkerPrivate->GetParent();
|
|
|
|
MOZ_ASSERT(parentWorker, "Must have a parent!");
|
|
|
|
principal = parentWorker->GetPrincipal();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mCacheCreator->Load(principal);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
LoadScript(uint32_t aIndex)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
WorkerPrivate* parentWorker = mWorkerPrivate->GetParent();
|
|
|
|
|
|
|
|
// Figure out which principal to use.
|
|
|
|
nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
|
2014-12-12 19:06:00 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = mWorkerPrivate->GetLoadGroup();
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!principal) {
|
|
|
|
NS_ASSERTION(parentWorker, "Must have a principal!");
|
2015-03-17 13:15:19 +03:00
|
|
|
NS_ASSERTION(mIsMainScript, "Must have a principal for importScripts!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
principal = parentWorker->GetPrincipal();
|
2014-12-12 19:06:00 +03:00
|
|
|
loadGroup = parentWorker->GetLoadGroup();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
NS_ASSERTION(principal, "This should never be null here!");
|
2014-12-12 19:06:00 +03:00
|
|
|
MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(loadGroup, principal));
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// Figure out our base URI.
|
2015-03-19 21:41:42 +03:00
|
|
|
nsCOMPtr<nsIURI> baseURI = GetBaseURI(mIsMainScript, mWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// May be null.
|
|
|
|
nsCOMPtr<nsIDocument> parentDoc = mWorkerPrivate->GetDocument();
|
|
|
|
|
2013-03-08 02:27:21 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2015-03-17 13:15:19 +03:00
|
|
|
if (IsMainWorkerScript()) {
|
2013-03-08 02:27:21 +04:00
|
|
|
// May be null.
|
2013-06-05 18:04:23 +04:00
|
|
|
channel = mWorkerPrivate->ForgetWorkerChannel();
|
2013-03-08 02:27:21 +04:00
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
nsCOMPtr<nsIIOService> ios(do_GetIOService());
|
|
|
|
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
NS_ASSERTION(secMan, "This should never be null!");
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
|
|
|
nsresult& rv = loadInfo.mLoadResult;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-10-06 16:37:07 +03:00
|
|
|
nsLoadFlags loadFlags = nsIRequest::LOAD_NORMAL;
|
|
|
|
|
|
|
|
// 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 21:41:42 +03:00
|
|
|
if (!channel) {
|
|
|
|
rv = ChannelFromScriptURL(principal, baseURI, parentDoc, loadGroup, ios,
|
|
|
|
secMan, loadInfo.mURL, IsMainWorkerScript(),
|
2015-06-17 04:21:08 +03:00
|
|
|
mWorkerScriptType,
|
2015-10-06 16:37:07 +03:00
|
|
|
mWorkerPrivate->ContentPolicyType(), loadFlags,
|
2015-06-17 04:21:08 +03:00
|
|
|
getter_AddRefs(channel));
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
// We need to know which index we're on in OnStreamComplete so we know
|
|
|
|
// where to put the result.
|
2015-11-12 20:44:51 +03:00
|
|
|
nsCOMPtr<nsISupportsPRUint32> indexSupports =
|
|
|
|
do_CreateInstance(NS_SUPPORTS_PRUINT32_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = indexSupports->SetData(aIndex);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
// We don't care about progress so just use the simple stream loader for
|
|
|
|
// OnStreamComplete notification only.
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
2015-11-12 20:44:51 +03:00
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), this);
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
if (loadInfo.mCacheStatus != ScriptLoadInfo::ToBeCached) {
|
2015-11-12 20:44:51 +03:00
|
|
|
rv = channel->AsyncOpen(loader, indexSupports);
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIOutputStream> writer;
|
|
|
|
|
|
|
|
// In case we return early.
|
|
|
|
loadInfo.mCacheStatus = ScriptLoadInfo::Cancel;
|
|
|
|
|
2015-04-24 02:10:57 +03:00
|
|
|
rv = NS_NewPipe(getter_AddRefs(mReader), getter_AddRefs(writer), 0,
|
2015-03-19 21:41:42 +03:00
|
|
|
UINT32_MAX, // unlimited size to avoid writer WOULD_BLOCK case
|
|
|
|
true, false); // non-blocking reader, blocking writer
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamListenerTee> tee =
|
|
|
|
do_CreateInstance(NS_STREAMLISTENERTEE_CONTRACTID);
|
2015-11-12 20:44:51 +03:00
|
|
|
rv = tee->Init(loader, writer, this);
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-11-12 20:44:51 +03:00
|
|
|
nsresult rv = channel->AsyncOpen(tee, indexSupports);
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
loadInfo.mChannel.swap(channel);
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-11-12 20:44:51 +03:00
|
|
|
OnStreamCompleteInternal(nsIStreamLoader* aLoader, nsISupports* aContext,
|
|
|
|
nsresult aStatus, uint32_t aStringLen,
|
|
|
|
const uint8_t* aString, ScriptLoadInfo& aLoadInfo)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (!aLoadInfo.mChannel) {
|
|
|
|
return NS_BINDING_ABORTED;
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
aLoadInfo.mChannel = nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
return aStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(aString, "This should never be null!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRequest> request;
|
|
|
|
nsresult rv = aLoader->GetRequest(getter_AddRefs(request));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-07-14 18:08:38 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
|
|
|
|
MOZ_ASSERT(channel);
|
|
|
|
|
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
NS_ASSERTION(ssm, "Should never be null!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> channelPrincipal;
|
|
|
|
rv = ssm->GetChannelResultPrincipal(channel, getter_AddRefs(channelPrincipal));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
|
|
|
|
if (!principal) {
|
|
|
|
WorkerPrivate* parentWorker = mWorkerPrivate->GetParent();
|
|
|
|
MOZ_ASSERT(parentWorker, "Must have a parent!");
|
|
|
|
principal = parentWorker->GetPrincipal();
|
|
|
|
}
|
|
|
|
|
2015-10-26 20:28:45 +03:00
|
|
|
// We don't mute the main worker script becase we've already done
|
|
|
|
// same-origin checks on them so we should be able to see their errors.
|
|
|
|
// Note that for data: url, where we allow it through the same-origin check
|
|
|
|
// but then give it a different origin.
|
|
|
|
aLoadInfo.mMutedErrorFlag.emplace(IsMainWorkerScript()
|
|
|
|
? false
|
|
|
|
: !principal->Subsumes(channelPrincipal));
|
2015-07-14 18:08:38 +03:00
|
|
|
|
|
|
|
// Make sure we're not seeing the result of a 404 or something by checking
|
|
|
|
// the 'requestSucceeded' attribute on the http channel.
|
2011-07-17 23:09:13 +04:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request);
|
|
|
|
if (httpChannel) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool requestSucceeded;
|
2011-07-17 23:09:13 +04:00
|
|
|
rv = httpChannel->GetRequestSucceeded(&requestSucceeded);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!requestSucceeded) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// May be null.
|
|
|
|
nsIDocument* parentDoc = mWorkerPrivate->GetDocument();
|
|
|
|
|
|
|
|
// Use the regular nsScriptLoader for this grunt work! Should be just fine
|
|
|
|
// because we're running on the main thread.
|
2013-10-10 02:23:47 +04:00
|
|
|
// Unlike <script> tags, Worker scripts are always decoded as UTF-8,
|
|
|
|
// per spec. So we explicitly pass in the charset hint.
|
2011-07-17 23:09:13 +04:00
|
|
|
rv = nsScriptLoader::ConvertToUTF16(aLoadInfo.mChannel, aString, aStringLen,
|
2013-10-10 02:23:47 +04:00
|
|
|
NS_LITERAL_STRING("UTF-8"), parentDoc,
|
2014-04-25 18:11:57 +04:00
|
|
|
aLoadInfo.mScriptTextBuf,
|
|
|
|
aLoadInfo.mScriptTextLength);
|
2011-07-17 23:09:13 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-03-29 21:53:48 +03:00
|
|
|
if (!aLoadInfo.mScriptTextLength && !aLoadInfo.mScriptTextBuf) {
|
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
|
|
|
|
NS_LITERAL_CSTRING("DOM"), parentDoc,
|
|
|
|
nsContentUtils::eDOM_PROPERTIES,
|
|
|
|
"EmptyWorkerSourceWarning");
|
|
|
|
} else if (!aLoadInfo.mScriptTextBuf) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out what we actually loaded.
|
|
|
|
nsCOMPtr<nsIURI> finalURI;
|
|
|
|
rv = NS_GetFinalChannelURI(channel, getter_AddRefs(finalURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCString filename;
|
|
|
|
rv = finalURI->GetSpec(filename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!filename.IsEmpty()) {
|
|
|
|
// This will help callers figure out what their script url resolved to in
|
|
|
|
// case of errors.
|
|
|
|
aLoadInfo.mURL.Assign(NS_ConvertUTF8toUTF16(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the principal of the worker and its base URI if we just loaded the
|
|
|
|
// worker's primary script.
|
2015-03-17 13:15:19 +03:00
|
|
|
if (IsMainWorkerScript()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
// Take care of the base URI first.
|
2011-11-04 20:32:17 +04:00
|
|
|
mWorkerPrivate->SetBaseURI(finalURI);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-05-25 21:21:05 +03:00
|
|
|
// Store the channel info if needed.
|
2015-08-18 01:08:58 +03:00
|
|
|
mWorkerPrivate->InitChannelInfo(channel);
|
2015-04-24 02:04:03 +03:00
|
|
|
|
2011-07-17 23:09:13 +04: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-07 03:05:40 +04:00
|
|
|
rv = ssm->GetChannelResultPrincipal(channel, getter_AddRefs(channelPrincipal));
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-12-12 19:06:00 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> channelLoadGroup;
|
|
|
|
rv = channel->GetLoadGroup(getter_AddRefs(channelLoadGroup));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
MOZ_ASSERT(channelLoadGroup);
|
|
|
|
|
2011-07-17 23:09:13 +04: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-10 00:34:00 +03: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 23:09:13 +04:00
|
|
|
if (nsContentUtils::IsSystemPrincipal(loadPrincipal)) {
|
|
|
|
if (!nsContentUtils::IsSystemPrincipal(channelPrincipal)) {
|
2015-05-27 00:27:01 +03: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 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
2015-11-12 20:44:51 +03:00
|
|
|
else {
|
|
|
|
// We exempt data urls and other URI's that inherit their
|
|
|
|
// principal again.
|
|
|
|
if (NS_FAILED(loadPrincipal->CheckMayLoad(finalURI, false, true))) {
|
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2014-12-12 19:06:00 +03: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);
|
2015-03-19 21:41:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
DataReceived();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataReceivedFromCache(uint32_t aIndex, const uint8_t* aString,
|
2015-05-25 21:21:05 +03:00
|
|
|
uint32_t aStringLen,
|
2015-07-02 15:54:00 +03:00
|
|
|
const mozilla::dom::ChannelInfo& aChannelInfo,
|
2015-06-05 07:39:34 +03:00
|
|
|
UniquePtr<PrincipalInfo> aPrincipalInfo)
|
2015-03-19 21:41:42 +03:00
|
|
|
{
|
2015-03-19 02:14:30 +03:00
|
|
|
AssertIsOnMainThread();
|
2015-03-19 21:41:42 +03:00
|
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
|
|
|
MOZ_ASSERT(loadInfo.mCacheStatus == ScriptLoadInfo::Cached);
|
|
|
|
|
2015-07-14 18:08:38 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> responsePrincipal =
|
|
|
|
PrincipalInfoToPrincipal(*aPrincipalInfo);
|
|
|
|
|
|
|
|
nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
|
|
|
|
if (!principal) {
|
|
|
|
WorkerPrivate* parentWorker = mWorkerPrivate->GetParent();
|
|
|
|
MOZ_ASSERT(parentWorker, "Must have a parent!");
|
|
|
|
principal = parentWorker->GetPrincipal();
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:06:55 +03:00
|
|
|
loadInfo.mMutedErrorFlag.emplace(!principal->Subsumes(responsePrincipal));
|
2015-07-14 18:08:38 +03:00
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
// May be null.
|
|
|
|
nsIDocument* parentDoc = mWorkerPrivate->GetDocument();
|
|
|
|
|
|
|
|
MOZ_ASSERT(!loadInfo.mScriptTextBuf);
|
|
|
|
|
2015-03-19 02:14:30 +03:00
|
|
|
nsresult rv =
|
2015-03-19 21:41:42 +03:00
|
|
|
nsScriptLoader::ConvertToUTF16(nullptr, aString, aStringLen,
|
|
|
|
NS_LITERAL_STRING("UTF-8"), parentDoc,
|
|
|
|
loadInfo.mScriptTextBuf,
|
|
|
|
loadInfo.mScriptTextLength);
|
2015-03-19 02:14:30 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && IsMainWorkerScript()) {
|
|
|
|
nsCOMPtr<nsIURI> finalURI;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(finalURI), loadInfo.mFullURL, nullptr, nullptr);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mWorkerPrivate->SetBaseURI(finalURI);
|
|
|
|
}
|
2015-04-11 00:14:44 +03:00
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
mozilla::DebugOnly<nsIPrincipal*> principal = mWorkerPrivate->GetPrincipal();
|
2015-04-11 00:14:44 +03:00
|
|
|
MOZ_ASSERT(principal);
|
|
|
|
nsILoadGroup* loadGroup = mWorkerPrivate->GetLoadGroup();
|
|
|
|
MOZ_ASSERT(loadGroup);
|
2015-06-05 07:39:34 +03:00
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
mozilla::DebugOnly<bool> equal = false;
|
2015-06-05 07:39:34 +03:00
|
|
|
MOZ_ASSERT(responsePrincipal && NS_SUCCEEDED(responsePrincipal->Equals(principal, &equal)));
|
|
|
|
MOZ_ASSERT(equal);
|
|
|
|
|
2015-05-25 21:21:05 +03:00
|
|
|
mWorkerPrivate->InitChannelInfo(aChannelInfo);
|
2015-06-05 07:39:34 +03:00
|
|
|
mWorkerPrivate->SetPrincipal(responsePrincipal, loadGroup);
|
2015-03-19 02:14:30 +03:00
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
DataReceived();
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadingFinished(aIndex, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataReceived()
|
|
|
|
{
|
|
|
|
if (IsMainWorkerScript()) {
|
|
|
|
WorkerPrivate* parent = mWorkerPrivate->GetParent();
|
2012-09-15 22:51:55 +04:00
|
|
|
|
|
|
|
if (parent) {
|
2012-09-17 04:20:16 +04:00
|
|
|
// XHR Params Allowed
|
|
|
|
mWorkerPrivate->SetXHRParamsAllowed(parent->XHRParamsAllowed());
|
|
|
|
|
|
|
|
// Set Eval and ContentSecurityPolicy
|
2012-09-15 22:51:55 +04:00
|
|
|
mWorkerPrivate->SetCSP(parent->GetCSP());
|
|
|
|
mWorkerPrivate->SetEvalAllowed(parent->IsEvalAllowed());
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExecuteFinishedScripts()
|
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
if (IsMainWorkerScript()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
mWorkerPrivate->WorkerScriptLoaded();
|
|
|
|
}
|
|
|
|
|
2012-09-28 10:57:33 +04:00
|
|
|
uint32_t firstIndex = UINT32_MAX;
|
|
|
|
uint32_t lastIndex = UINT32_MAX;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// Find firstIndex based on whether mExecutionScheduled is unset.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < mLoadInfos.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!mLoadInfos[index].mExecutionScheduled) {
|
|
|
|
firstIndex = index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find lastIndex based on whether mChannel is set, and update
|
|
|
|
// mExecutionScheduled on the ones we're about to schedule.
|
2012-09-28 10:57:33 +04:00
|
|
|
if (firstIndex != UINT32_MAX) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = firstIndex; index < mLoadInfos.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[index];
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
if (!loadInfo.Finished()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can execute this one.
|
|
|
|
loadInfo.mExecutionScheduled = true;
|
|
|
|
|
|
|
|
lastIndex = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
// This is the last index, we can unused things before the exection of the
|
|
|
|
// script and the stopping of the sync loop.
|
|
|
|
if (lastIndex == mLoadInfos.Length() - 1) {
|
|
|
|
mCacheCreator = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-09-28 10:57:33 +04:00
|
|
|
if (firstIndex != UINT32_MAX && lastIndex != UINT32_MAX) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ScriptExecutorRunnable> runnable =
|
2015-03-19 21:41:42 +03:00
|
|
|
new ScriptExecutorRunnable(*this, mSyncLoopTarget, IsMainWorkerScript(),
|
|
|
|
firstIndex, lastIndex);
|
2012-07-30 18:20:58 +04:00
|
|
|
if (!runnable->Dispatch(nullptr)) {
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(false, "This should never fail!");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-12 20:44:51 +03:00
|
|
|
NS_IMPL_ISUPPORTS(ScriptLoaderRunnable, nsIRunnable,
|
|
|
|
nsIStreamLoaderObserver,
|
|
|
|
nsIRequestObserver)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
void
|
|
|
|
CachePromiseHandler::ResolvedCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-03-27 01:56:02 +03:00
|
|
|
// May already have been canceled by CacheScriptLoader::Fail from
|
|
|
|
// CancelMainThread.
|
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::WritingToCache ||
|
|
|
|
mLoadInfo.mCacheStatus == ScriptLoadInfo::Cancel);
|
|
|
|
MOZ_ASSERT_IF(mLoadInfo.mCacheStatus == ScriptLoadInfo::Cancel, !mLoadInfo.mCachePromise);
|
2015-03-19 21:41:42 +03:00
|
|
|
|
2015-03-25 03:11:57 +03:00
|
|
|
if (mLoadInfo.mCachePromise) {
|
2015-03-27 01:56:02 +03:00
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::Cached;
|
2015-03-25 03:11:57 +03:00
|
|
|
mLoadInfo.mCachePromise = nullptr;
|
|
|
|
mRunnable->MaybeExecuteFinishedScripts(mIndex);
|
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CachePromiseHandler::RejectedCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-03-27 01:56:02 +03:00
|
|
|
// May already have been canceled by CacheScriptLoader::Fail from
|
|
|
|
// CancelMainThread.
|
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::WritingToCache ||
|
|
|
|
mLoadInfo.mCacheStatus == ScriptLoadInfo::Cancel);
|
2015-03-19 21:41:42 +03:00
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::Cancel;
|
|
|
|
|
|
|
|
mLoadInfo.mCachePromise = nullptr;
|
|
|
|
|
|
|
|
// This will delete the cache object and will call LoadingFinished() with an
|
|
|
|
// error for each ongoing operation.
|
|
|
|
mRunnable->DeleteCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheCreator::CreateCacheStorage(nsIPrincipal* aPrincipal)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(!mCacheStorage);
|
|
|
|
MOZ_ASSERT(aPrincipal);
|
|
|
|
|
|
|
|
nsIXPConnect* xpc = nsContentUtils::XPConnect();
|
|
|
|
MOZ_ASSERT(xpc, "This should never be null!");
|
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
mozilla::AutoSafeJSContext cx;
|
2015-07-21 21:31:44 +03:00
|
|
|
JS::Rooted<JSObject*> sandbox(cx);
|
|
|
|
nsresult rv = xpc->CreateSandbox(cx, aPrincipal, sandbox.address());
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-07-21 21:31:44 +03:00
|
|
|
mSandboxGlobalObject = xpc::NativeGlobal(sandbox);
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(!mSandboxGlobalObject)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-06-27 05:36:40 +03:00
|
|
|
// If we're in private browsing mode, don't even try to create the
|
|
|
|
// CacheStorage. Instead, just fail immediately to terminate the
|
|
|
|
// ServiceWorker load.
|
|
|
|
if (NS_WARN_IF(mPrivateBrowsing)) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
2015-06-28 06:19:24 +03:00
|
|
|
// Create a CacheStorage bypassing its trusted origin checks. The
|
|
|
|
// ServiceWorker has already performed its own checks before getting
|
|
|
|
// to this point.
|
2015-03-19 21:41:42 +03:00
|
|
|
ErrorResult error;
|
|
|
|
mCacheStorage =
|
2015-07-02 15:54:00 +03:00
|
|
|
CacheStorage::CreateOnMainThread(mozilla::dom::cache::CHROME_ONLY_NAMESPACE,
|
2015-03-19 21:41:42 +03:00
|
|
|
mSandboxGlobalObject,
|
2015-06-27 05:36:40 +03:00
|
|
|
aPrincipal, mPrivateBrowsing,
|
2015-06-28 06:19:24 +03:00
|
|
|
true /* force trusted origin */,
|
2015-06-27 05:36:40 +03:00
|
|
|
error);
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return error.StealNSResult();
|
2015-03-19 21:41:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheCreator::Load(nsIPrincipal* aPrincipal)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(!mLoaders.IsEmpty());
|
|
|
|
|
|
|
|
nsresult rv = CreateCacheStorage(aPrincipal);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorResult error;
|
|
|
|
MOZ_ASSERT(!mCacheName.IsEmpty());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = mCacheStorage->Open(mCacheName, error);
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return error.StealNSResult();
|
2015-03-19 21:41:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
promise->AppendNativeHandler(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CacheCreator::FailLoaders(nsresult aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-03-27 20:00:59 +03:00
|
|
|
// Fail() can call LoadingFinished() which may call ExecuteFinishedScripts()
|
|
|
|
// which sets mCacheCreator to null, so hold a ref.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheCreator> kungfuDeathGrip = this;
|
2015-03-27 20:00:59 +03:00
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
for (uint32_t i = 0, len = mLoaders.Length(); i < len; ++i) {
|
|
|
|
mLoaders[i]->Fail(aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
mLoaders.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CacheCreator::RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
FailLoaders(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CacheCreator::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aValue.isObject());
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
|
|
|
|
Cache* cache = nullptr;
|
|
|
|
nsresult rv = UNWRAP_OBJECT(Cache, obj, cache);
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
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 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = mCacheStorage->Delete(mCacheName, rv);
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
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 03:11:57 +03:00
|
|
|
|
|
|
|
// Stop if the load was aborted on the main thread.
|
2015-03-27 01:56:02 +03:00
|
|
|
// Can't use Finished() because mCachePromise may still be true.
|
|
|
|
if (mLoadInfo.mLoadingFinished) {
|
|
|
|
MOZ_ASSERT(!mLoadInfo.mChannel);
|
|
|
|
MOZ_ASSERT_IF(mLoadInfo.mCachePromise,
|
|
|
|
mLoadInfo.mCacheStatus == ScriptLoadInfo::WritingToCache ||
|
|
|
|
mLoadInfo.mCacheStatus == ScriptLoadInfo::Cancel);
|
2015-03-25 03:11:57 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
mRunnable->LoadingFinished(mIndex, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CacheScriptLoader::Load(Cache* aCache)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aCache);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), mLoadInfo.mURL, nullptr,
|
|
|
|
mBaseURI);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
Fail(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString spec;
|
|
|
|
rv = uri->GetSpec(spec);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
Fail(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mLoadInfo.mFullURL.IsEmpty());
|
|
|
|
CopyUTF8toUTF16(spec, mLoadInfo.mFullURL);
|
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
mozilla::dom::RequestOrUSVString request;
|
2015-03-19 21:41:42 +03:00
|
|
|
request.SetAsUSVString().Rebind(mLoadInfo.mFullURL.Data(),
|
|
|
|
mLoadInfo.mFullURL.Length());
|
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
mozilla::dom::CacheQueryOptions params;
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
|
|
ErrorResult error;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = aCache->Match(request, params, error);
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2015-04-27 16:18:52 +03:00
|
|
|
Fail(error.StealNSResult());
|
2015-03-19 21:41:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
promise->AppendNativeHandler(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CacheScriptLoader::RejectedCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::Uncached);
|
|
|
|
Fail(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CacheScriptLoader::ResolvedCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
// If we have already called 'Fail', we should not proceed.
|
|
|
|
if (mFailed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-27 23:29:40 +03:00
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::Uncached);
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
if (aValue.isUndefined()) {
|
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::ToBeCached;
|
2015-11-12 20:44:51 +03:00
|
|
|
mRunnable->LoadScript(mIndex);
|
2015-03-19 21:41:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aValue.isObject());
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
|
2015-07-02 15:54:00 +03:00
|
|
|
mozilla::dom::Response* response = nullptr;
|
2015-11-12 20:44:51 +03:00
|
|
|
nsresult rv = UNWRAP_OBJECT(Response, obj, response);
|
2015-03-19 21:41:42 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
Fail(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
response->GetBody(getter_AddRefs(inputStream));
|
2015-05-25 21:21:05 +03:00
|
|
|
mChannelInfo = response->GetChannelInfo();
|
2015-07-02 15:54:00 +03:00
|
|
|
const UniquePtr<PrincipalInfo>& pInfo = response->GetPrincipalInfo();
|
2015-06-05 07:39:34 +03:00
|
|
|
if (pInfo) {
|
2015-07-02 15:54:00 +03:00
|
|
|
mPrincipalInfo = mozilla::MakeUnique<PrincipalInfo>(*pInfo);
|
2015-06-05 07:39:34 +03:00
|
|
|
}
|
2015-03-19 21:41:42 +03:00
|
|
|
|
|
|
|
if (!inputStream) {
|
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::Cached;
|
2015-06-05 07:39:34 +03:00
|
|
|
mRunnable->DataReceivedFromCache(mIndex, (uint8_t*)"", 0, mChannelInfo,
|
|
|
|
Move(mPrincipalInfo));
|
2015-03-19 21:41:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mPump);
|
|
|
|
rv = NS_NewInputStreamPump(getter_AddRefs(mPump), inputStream);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
Fail(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
Fail(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mPump->AsyncRead(loader, nullptr);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mPump = nullptr;
|
|
|
|
Fail(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThreadRetargetableRequest> rr = do_QueryInterface(mPump);
|
|
|
|
if (rr) {
|
|
|
|
nsCOMPtr<nsIEventTarget> sts =
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
rv = rr->RetargetDeliveryTo(sts);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the nsIInputStreamPump to a IO thread.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::ReadingFromCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CacheScriptLoader::OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext,
|
|
|
|
nsresult aStatus, uint32_t aStringLen,
|
|
|
|
const uint8_t* aString)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::ReadingFromCache);
|
|
|
|
|
|
|
|
mPump = nullptr;
|
|
|
|
|
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
Fail(aStatus);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::Cached;
|
|
|
|
|
2015-06-05 07:39:34 +03:00
|
|
|
MOZ_ASSERT(mPrincipalInfo);
|
|
|
|
mRunnable->DataReceivedFromCache(mIndex, aString, aStringLen, mChannelInfo,
|
|
|
|
Move(mPrincipalInfo));
|
2015-03-19 21:41:42 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-14 18:08:36 +03:00
|
|
|
class ChannelGetterRunnable final : public nsRunnable
|
2013-03-08 02:27:21 +04:00
|
|
|
{
|
|
|
|
WorkerPrivate* mParentWorker;
|
2013-10-23 17:16:49 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
2013-06-05 18:04:23 +04:00
|
|
|
const nsAString& mScriptURL;
|
2013-03-08 02:27:21 +04:00
|
|
|
nsIChannel** mChannel;
|
|
|
|
nsresult mResult;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ChannelGetterRunnable(WorkerPrivate* aParentWorker,
|
2013-10-23 17:16:49 +04:00
|
|
|
nsIEventTarget* aSyncLoopTarget,
|
2013-06-05 18:04:23 +04:00
|
|
|
const nsAString& aScriptURL,
|
2013-03-08 02:27:21 +04:00
|
|
|
nsIChannel** aChannel)
|
2013-10-23 17:16:49 +04:00
|
|
|
: mParentWorker(aParentWorker), mSyncLoopTarget(aSyncLoopTarget),
|
2013-03-08 02:27:21 +04:00
|
|
|
mScriptURL(aScriptURL), mChannel(aChannel), mResult(NS_ERROR_FAILURE)
|
|
|
|
{
|
2014-12-12 19:06:00 +03:00
|
|
|
MOZ_ASSERT(mParentWorker);
|
2013-03-08 02:27:21 +04:00
|
|
|
aParentWorker->AssertIsOnWorkerThread();
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aSyncLoopTarget);
|
2013-03-08 02:27:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2015-03-21 19:28:04 +03:00
|
|
|
Run() override
|
2013-03-08 02:27:21 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsIPrincipal* principal = mParentWorker->GetPrincipal();
|
2015-03-19 21:41:42 +03:00
|
|
|
MOZ_ASSERT(principal);
|
2013-03-08 02:27:21 +04:00
|
|
|
|
|
|
|
// Figure out our base URI.
|
|
|
|
nsCOMPtr<nsIURI> baseURI = mParentWorker->GetBaseURI();
|
2015-03-19 21:41:42 +03:00
|
|
|
MOZ_ASSERT(baseURI);
|
2013-03-08 02:27:21 +04:00
|
|
|
|
|
|
|
// May be null.
|
|
|
|
nsCOMPtr<nsIDocument> parentDoc = mParentWorker->GetDocument();
|
|
|
|
|
2014-12-12 19:06:00 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = mParentWorker->GetLoadGroup();
|
|
|
|
|
2013-03-08 02:27:21 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
mResult =
|
|
|
|
scriptloader::ChannelFromScriptURLMainThread(principal, baseURI,
|
2014-12-12 19:06:00 +03:00
|
|
|
parentDoc, loadGroup,
|
|
|
|
mScriptURL,
|
2015-06-17 04:21:08 +03:00
|
|
|
// Nested workers are always dedicated.
|
|
|
|
nsIContentPolicy::TYPE_INTERNAL_WORKER,
|
2013-03-08 02:27:21 +04:00
|
|
|
getter_AddRefs(channel));
|
|
|
|
if (NS_SUCCEEDED(mResult)) {
|
|
|
|
channel.forget(mChannel);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MainThreadStopSyncLoopRunnable> runnable =
|
2013-10-23 17:16:49 +04:00
|
|
|
new MainThreadStopSyncLoopRunnable(mParentWorker,
|
|
|
|
mSyncLoopTarget.forget(), true);
|
2013-03-08 02:27:21 +04:00
|
|
|
if (!runnable->Dispatch(nullptr)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetResult() const
|
|
|
|
{
|
|
|
|
return mResult;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual ~ChannelGetterRunnable()
|
|
|
|
{ }
|
2013-03-08 02:27:21 +04:00
|
|
|
};
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
ScriptExecutorRunnable::ScriptExecutorRunnable(
|
|
|
|
ScriptLoaderRunnable& aScriptLoader,
|
2013-10-23 17:16:49 +04:00
|
|
|
nsIEventTarget* aSyncLoopTarget,
|
2015-03-19 21:41:42 +03:00
|
|
|
bool aIsWorkerScript,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFirstIndex,
|
|
|
|
uint32_t aLastIndex)
|
2013-10-23 17:16:49 +04:00
|
|
|
: MainThreadWorkerSyncRunnable(aScriptLoader.mWorkerPrivate, aSyncLoopTarget),
|
2015-03-19 21:41:42 +03:00
|
|
|
mScriptLoader(aScriptLoader), mIsWorkerScript(aIsWorkerScript),
|
|
|
|
mFirstIndex(aFirstIndex), mLastIndex(aLastIndex)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aFirstIndex <= aLastIndex);
|
|
|
|
MOZ_ASSERT(aLastIndex < aScriptLoader.mLoadInfos.Length());
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
bool
|
|
|
|
ScriptExecutorRunnable::IsDebuggerRunnable() const
|
|
|
|
{
|
|
|
|
// ScriptExecutorRunnable is used to execute both worker and debugger scripts.
|
|
|
|
// In the latter case, the runnable needs to be dispatched to the debugger
|
|
|
|
// queue.
|
|
|
|
return mScriptLoader.mWorkerScriptType == DebuggerScript;
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
bool
|
|
|
|
ScriptExecutorRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
nsTArray<ScriptLoadInfo>& loadInfos = mScriptLoader.mLoadInfos;
|
|
|
|
|
|
|
|
// Don't run if something else has already failed.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < mFirstIndex; index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
ScriptLoadInfo& loadInfo = loadInfos.ElementAt(index);
|
|
|
|
|
|
|
|
NS_ASSERTION(!loadInfo.mChannel, "Should no longer have a channel!");
|
|
|
|
NS_ASSERTION(loadInfo.mExecutionScheduled, "Should be scheduled!");
|
|
|
|
|
|
|
|
if (!loadInfo.mExecutionResult) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 00:12:55 +03:00
|
|
|
JS::Rooted<JSObject*> global(aCx);
|
|
|
|
|
|
|
|
if (mIsWorkerScript) {
|
|
|
|
WorkerGlobalScope* globalScope =
|
|
|
|
aWorkerPrivate->GetOrCreateGlobalScope(aCx);
|
|
|
|
if (NS_WARN_IF(!globalScope)) {
|
|
|
|
NS_WARNING("Failed to make global!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
global.set(globalScope->GetWrapper());
|
|
|
|
} else {
|
|
|
|
global.set(JS::CurrentGlobalOrNull(aCx));
|
2014-07-30 08:00:37 +04:00
|
|
|
}
|
|
|
|
|
2015-06-11 00:12:55 +03:00
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
|
|
|
JSAutoCompartment ac(aCx, global);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = mFirstIndex; index <= mLastIndex; index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
ScriptLoadInfo& loadInfo = loadInfos.ElementAt(index);
|
|
|
|
|
|
|
|
NS_ASSERTION(!loadInfo.mChannel, "Should no longer have a channel!");
|
|
|
|
NS_ASSERTION(loadInfo.mExecutionScheduled, "Should be scheduled!");
|
|
|
|
NS_ASSERTION(!loadInfo.mExecutionResult, "Should not have executed yet!");
|
|
|
|
|
|
|
|
if (NS_FAILED(loadInfo.mLoadResult)) {
|
2015-08-30 20:31:52 +03:00
|
|
|
scriptloader::ReportLoadError(aCx, loadInfo.mLoadResult);
|
|
|
|
// Top level scripts only!
|
|
|
|
if (mIsWorkerScript) {
|
|
|
|
aWorkerPrivate->MaybeDispatchLoadFailedRunnable();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 filename(loadInfo.mURL);
|
|
|
|
|
2012-08-27 16:04:37 +04:00
|
|
|
JS::CompileOptions options(aCx);
|
2014-07-31 20:55:20 +04:00
|
|
|
options.setFileAndLine(filename.get(), 1)
|
|
|
|
.setNoScriptRval(true);
|
2014-04-25 18:11:57 +04:00
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
if (mScriptLoader.mWorkerScriptType == DebuggerScript) {
|
|
|
|
options.setVersion(JSVERSION_LATEST);
|
|
|
|
}
|
|
|
|
|
2015-07-14 18:08:38 +03:00
|
|
|
MOZ_ASSERT(loadInfo.mMutedErrorFlag.isSome());
|
|
|
|
options.setMutedErrors(loadInfo.mMutedErrorFlag.valueOr(true));
|
|
|
|
|
2014-04-25 18:11:57 +04:00
|
|
|
JS::SourceBufferHolder srcBuf(loadInfo.mScriptTextBuf,
|
|
|
|
loadInfo.mScriptTextLength,
|
|
|
|
JS::SourceBufferHolder::GiveOwnership);
|
|
|
|
loadInfo.mScriptTextBuf = nullptr;
|
|
|
|
loadInfo.mScriptTextLength = 0;
|
|
|
|
|
2014-11-19 07:27:16 +03:00
|
|
|
JS::Rooted<JS::Value> unused(aCx);
|
2015-03-14 08:36:16 +03:00
|
|
|
if (!JS::Evaluate(aCx, options, srcBuf, &unused)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadInfo.mExecutionResult = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ScriptExecutorRunnable::PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aRunResult)
|
|
|
|
{
|
|
|
|
nsTArray<ScriptLoadInfo>& loadInfos = mScriptLoader.mLoadInfos;
|
|
|
|
|
|
|
|
if (mLastIndex == loadInfos.Length() - 1) {
|
|
|
|
// All done. If anything failed then return false.
|
|
|
|
bool result = true;
|
2015-10-15 15:06:55 +03:00
|
|
|
bool mutedError = false;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < loadInfos.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!loadInfos[index].mExecutionResult) {
|
2015-10-15 15:06:55 +03:00
|
|
|
mutedError = loadInfos[index].mMutedErrorFlag.valueOr(true);
|
2011-07-17 23:09:13 +04:00
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:06:55 +03:00
|
|
|
ShutdownScriptLoader(aCx, aWorkerPrivate, result, mutedError);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-11 04:37:47 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ScriptExecutorRunnable::Cancel()
|
|
|
|
{
|
2014-05-20 16:34:25 +04:00
|
|
|
if (mLastIndex == mScriptLoader.mLoadInfos.Length() - 1) {
|
2015-10-15 15:06:55 +03:00
|
|
|
ShutdownScriptLoader(mWorkerPrivate->GetJSContext(), mWorkerPrivate,
|
|
|
|
false, false);
|
2014-05-20 16:34:25 +04:00
|
|
|
}
|
2014-01-13 21:41:02 +04:00
|
|
|
return MainThreadWorkerSyncRunnable::Cancel();
|
2014-01-11 04:37:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ScriptExecutorRunnable::ShutdownScriptLoader(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
2015-10-15 15:06:55 +03:00
|
|
|
bool aResult,
|
|
|
|
bool aMutedError)
|
2014-01-11 04:37:47 +04:00
|
|
|
{
|
2015-03-19 21:41:42 +03:00
|
|
|
MOZ_ASSERT(mLastIndex == mScriptLoader.mLoadInfos.Length() - 1);
|
|
|
|
|
|
|
|
if (mIsWorkerScript && aWorkerPrivate->IsServiceWorker()) {
|
|
|
|
aWorkerPrivate->SetLoadingWorkerScript(false);
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:06:55 +03:00
|
|
|
if (!aResult) {
|
|
|
|
// If this error has to be muted, we have to clear the pending exception,
|
|
|
|
// if any, and use the ErrorResult object to throw a new exception.
|
|
|
|
if (aMutedError && JS_IsExceptionPending(aCx)) {
|
2015-10-15 06:05:00 +03:00
|
|
|
LogExceptionToConsole(aCx, aWorkerPrivate);
|
2015-10-15 15:06:55 +03:00
|
|
|
mScriptLoader.mRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
} else {
|
|
|
|
mScriptLoader.mRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-11 04:37:47 +04:00
|
|
|
aWorkerPrivate->RemoveFeature(aCx, &mScriptLoader);
|
|
|
|
aWorkerPrivate->StopSyncLoop(mSyncLoopTarget, aResult);
|
|
|
|
}
|
|
|
|
|
2015-10-15 06:05:00 +03:00
|
|
|
void
|
|
|
|
ScriptExecutorRunnable::LogExceptionToConsole(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> exn(aCx);
|
|
|
|
if (!JS_GetPendingException(aCx, &exn)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
|
|
|
|
js::ErrorReport report(aCx);
|
|
|
|
if (!report.init(aCx, exn)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<xpc::ErrorReport> xpcReport = new xpc::ErrorReport();
|
2015-10-15 06:05:00 +03:00
|
|
|
xpcReport->Init(report.report(), report.message(),
|
|
|
|
aWorkerPrivate->IsChromeWorker(), aWorkerPrivate->WindowID());
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncErrorReporter> r = new AsyncErrorReporter(xpcReport);
|
2015-10-15 06:05:00 +03:00
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:06:55 +03:00
|
|
|
void
|
2011-07-17 23:09:13 +04:00
|
|
|
LoadAllScripts(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
2015-03-17 13:15:19 +03:00
|
|
|
nsTArray<ScriptLoadInfo>& aLoadInfos, bool aIsMainScript,
|
2015-10-15 15:06:55 +03:00
|
|
|
WorkerScriptType aWorkerScriptType, ErrorResult& aRv)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
NS_ASSERTION(!aLoadInfos.IsEmpty(), "Bad arguments!");
|
|
|
|
|
2012-12-22 00:14:47 +04:00
|
|
|
AutoSyncLoopHolder syncLoop(aWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ScriptLoaderRunnable> loader =
|
2013-10-23 17:16:49 +04:00
|
|
|
new ScriptLoaderRunnable(aWorkerPrivate, syncLoop.EventTarget(),
|
2015-10-15 15:06:55 +03:00
|
|
|
aLoadInfos, aIsMainScript, aWorkerScriptType,
|
|
|
|
aRv);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(aLoadInfos.IsEmpty(), "Should have swapped!");
|
|
|
|
|
|
|
|
if (!aWorkerPrivate->AddFeature(aCx, loader)) {
|
2015-10-15 15:06:55 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2014-05-23 23:53:17 +04:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(loader))) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_ERROR("Failed to dispatch!");
|
|
|
|
|
|
|
|
aWorkerPrivate->RemoveFeature(aCx, loader);
|
2015-10-15 15:06:55 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2015-10-15 15:06:55 +03:00
|
|
|
syncLoop.Run();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* anonymous namespace */
|
|
|
|
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace scriptloader {
|
|
|
|
|
2013-03-08 02:27:21 +04:00
|
|
|
nsresult
|
|
|
|
ChannelFromScriptURLMainThread(nsIPrincipal* aPrincipal,
|
|
|
|
nsIURI* aBaseURI,
|
|
|
|
nsIDocument* aParentDoc,
|
2014-12-12 19:06:00 +03:00
|
|
|
nsILoadGroup* aLoadGroup,
|
2013-06-05 18:04:23 +04:00
|
|
|
const nsAString& aScriptURL,
|
2015-06-17 04:21:08 +03:00
|
|
|
nsContentPolicyType aContentPolicyType,
|
2013-03-08 02:27:21 +04:00
|
|
|
nsIChannel** aChannel)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIOService> ios(do_GetIOService());
|
|
|
|
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
NS_ASSERTION(secMan, "This should never be null!");
|
|
|
|
|
2014-12-12 19:06:00 +03:00
|
|
|
return ChannelFromScriptURL(aPrincipal, aBaseURI, aParentDoc, aLoadGroup,
|
2015-06-17 04:21:08 +03:00
|
|
|
ios, secMan, aScriptURL, true, WorkerScript,
|
2015-10-06 16:37:07 +03:00
|
|
|
aContentPolicyType, nsIRequest::LOAD_NORMAL,
|
|
|
|
aChannel);
|
2013-03-08 02:27:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ChannelFromScriptURLWorkerThread(JSContext* aCx,
|
|
|
|
WorkerPrivate* aParent,
|
2013-06-05 18:04:23 +04:00
|
|
|
const nsAString& aScriptURL,
|
2013-03-08 02:27:21 +04:00
|
|
|
nsIChannel** aChannel)
|
|
|
|
{
|
|
|
|
aParent->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
AutoSyncLoopHolder syncLoop(aParent);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ChannelGetterRunnable> getter =
|
2013-10-23 17:16:49 +04:00
|
|
|
new ChannelGetterRunnable(aParent, syncLoop.EventTarget(), aScriptURL,
|
|
|
|
aChannel);
|
2013-03-08 02:27:21 +04:00
|
|
|
|
2014-05-23 23:53:17 +04:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(getter))) {
|
2013-03-08 02:27:21 +04:00
|
|
|
NS_ERROR("Failed to dispatch!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
if (!syncLoop.Run()) {
|
2013-03-08 02:27:21 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getter->GetResult();
|
|
|
|
}
|
|
|
|
|
2015-08-30 20:31:52 +03:00
|
|
|
void ReportLoadError(JSContext* aCx, nsresult aLoadResult)
|
2013-03-08 02:27:21 +04:00
|
|
|
{
|
|
|
|
switch (aLoadResult) {
|
|
|
|
case NS_BINDING_ABORTED:
|
|
|
|
// Canceled, don't set an exception.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NS_ERROR_FILE_NOT_FOUND:
|
|
|
|
case NS_ERROR_NOT_AVAILABLE:
|
2015-08-30 20:31:52 +03:00
|
|
|
Throw(aCx, NS_ERROR_DOM_NETWORK_ERR);
|
2013-03-08 02:27:21 +04:00
|
|
|
break;
|
|
|
|
|
2015-08-30 20:31:52 +03:00
|
|
|
case NS_ERROR_MALFORMED_URI:
|
|
|
|
aLoadResult = NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
// fall through
|
2013-03-08 02:27:21 +04:00
|
|
|
case NS_ERROR_DOM_SECURITY_ERR:
|
|
|
|
case NS_ERROR_DOM_SYNTAX_ERR:
|
2013-09-09 07:29:21 +04:00
|
|
|
Throw(aCx, aLoadResult);
|
2013-03-08 02:27:21 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-01-24 05:20:53 +04:00
|
|
|
JS_ReportError(aCx, "Failed to load script (nsresult = 0x%x)", aLoadResult);
|
2013-03-08 02:27:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:06:55 +03:00
|
|
|
void
|
2015-03-17 13:15:19 +03:00
|
|
|
LoadMainScript(JSContext* aCx, const nsAString& aScriptURL,
|
2015-10-15 15:06:55 +03:00
|
|
|
WorkerScriptType aWorkerScriptType,
|
|
|
|
ErrorResult& aRv)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
WorkerPrivate* worker = GetWorkerPrivateFromContext(aCx);
|
|
|
|
NS_ASSERTION(worker, "This should never be null!");
|
|
|
|
|
|
|
|
nsTArray<ScriptLoadInfo> loadInfos;
|
|
|
|
|
|
|
|
ScriptLoadInfo* info = loadInfos.AppendElement();
|
2015-03-17 13:15:19 +03:00
|
|
|
info->mURL = aScriptURL;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-10-15 15:06:55 +03:00
|
|
|
LoadAllScripts(aCx, worker, loadInfos, true, aWorkerScriptType, aRv);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
void
|
|
|
|
Load(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
2015-03-31 13:22:40 +03:00
|
|
|
const nsTArray<nsString>& aScriptURLs, WorkerScriptType aWorkerScriptType,
|
|
|
|
ErrorResult& aRv)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-11-05 18:16:26 +04:00
|
|
|
const uint32_t urlCount = aScriptURLs.Length();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!urlCount) {
|
|
|
|
return;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (urlCount > MAX_CONCURRENT_SCRIPTS) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<ScriptLoadInfo> loadInfos;
|
2013-11-05 18:16:26 +04:00
|
|
|
loadInfos.SetLength(urlCount);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
for (uint32_t index = 0; index < urlCount; index++) {
|
|
|
|
loadInfos[index].mURL = aScriptURLs[index];
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2015-10-15 15:06:55 +03:00
|
|
|
LoadAllScripts(aCx, aWorkerPrivate, loadInfos, false, aWorkerScriptType, aRv);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace scriptloader
|
|
|
|
|
|
|
|
END_WORKERS_NAMESPACE
|