mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:18:48 +00:00
e4c3e62beb
- Bug 1216751 part 1. Restrict value iterators to interfaces that have indexed properties and pair iterators to interfaces that do not have indexed properties. r=qdot (6519f3f8c5) - Bug 1216751 part 2. For value iterators, "entries", "keys", and "values" must just come from Array.prototype. r=qdot (c0859f945c) - Bug 1216751 part 3. For pair iterators, @@iterator should be an alias for "entries". Similarly for maplikes and "entries" and setlikes and "values". r=qdot (bbe7c04782) - Bug 1216751 part 4. Implement forEach for iterable interfaces. r=qdot (8fdba677a4) - Bug 1216751 part 5. Remove the now-unnecessary value iterator infrastructure, since it's entirely handled via the %ArrayPrototype% methods now. r=qdot (88d3911694) - Bug 1231333 - part 1, JS engine: only allow futexWait in workers. r=luke (28e16fd2f9) - Bug 1231333 - part 2, DOM: only allow futexWait in workers. r=khuey (6c4dc98037) - Bug 1148990 - Don't ship bagheeraclient.js or tokenserverclient.js on Android. r=gps (aa9b22699a) - Bug 1216749 - Land the Firefox Kinto.js client (r=rnewman) (ea8c74e2ea) - Bug 1230221 - Convert JS callsites to use asyncOpen2 within services/ (r=sicking) (07ac8751f1) - Bug 1242965 - Make services/common eslintable. r=rnewman (0c84562750) - Bug 1055616 - Skip addons addons without a sourceURI or from a non-secure domain rather than treating them as errors. r=rnewman (7b8b738be0) - Bug 1229986 - get Sync tps tests starting again. r=whimboo (8cd0bf4f7f) - Bug 1003204: Removed CommonUtils.exceptionStr() in services/sync r=makh r=gfritzsche (830c106a29) - Bug 1003204: Removed CommonUtils.exceptionStr() in services/common/ r=gfritzsche (2c7bd4f8b5) - Bug 1234734 - Replace CommonUtils.stackTrace() with Log.stackTrace(). r=markh (3f0e88f192) - Bug 1241715 - get Sync TPS tests working locally by tweaking observers listened for and the authentication setup. r=whimboo (529b2f3d44) - Bug 1203736 - Convert H264::DecodeSPS assert to error return. r=jya (41c8c34c42) - Bug 1186716: Error if SPS NAL parsing failed. r=rillian (6c158be51e) - Bug 1187076 - Warn at end of SPS buffers. r=jya (2a49671261) - fix broken files (a090aad200) - Bug 1218217: avoid buffersize overflow even if codec is unbounded in dimensions r=pkerr (356140c947) - Bug 1218217: bustage fix for static assert r=bustage (e86dc5bf3a) - Bug 1041882 - Remove Froyo-specific support from libcubeb. r=snorp, r=padenot (e1f2d5283f) - Bug 1073319 - Enable AVX2 for libvpx on linux (update.py). r=rillian (934fd0a896) - Bug 1245027 - Move LOCAL_INCLUDES to moz.build in media/libvpx. r=mshal (7e56797d0e) - parts of Bug 1151175 - Update libvpx update.py for 1.4.0. (0e3f4a470f) - bits of 1178215 (bab7592703) - Bug 1218124 - Add vpx_once patch to update script. r=gerald (7b72a43382) - Bug 1225221 - vpx: Allow 8k video in update.sh. r=kinetik (9ec59f7737) - Bug 1224363 - Upstream update patch - r=rillian (4772921a5f) - Bug 1224361 - Upstream update patch - r=rillian (36ad6f1de4) - Bug 1233983 - Make libvpx build with clang-cl; r=rillian (5d98a8d888) - Bug 1224371 - Upstream update patch. r=jya (25164ba856) - Bug 1237848 - Updated update.py patch - r=rillian (69646eb6dc) - Bug 1184226 - Suppressing received packets when disabled, r=ekr (c8dfdb1a56) - Bug 1184226 - Disabling write on shutdown, r=ekr (d5a810dbe5) - Bug 1184226 - Updating transportlayerdtls logging levels, r=ekr (f3bc4a9889) - Bug 1137932: Unwind the stack before starting the DTLS handshake. r=mt (69dce8243a) - Bug 1214269 - read multiple DTLS packets from NSS if present. r=mt rjesup (e57b1628f5) - Bug 1235235 - Fix -Wimplicit-fallthrough warning in media/mtransport/. r=ekr (d56c9d1244) - Bug 1115483 - Accept a match on any a=fingerprint value. r=ekr (4a58378c09) - Bug 1167274 - Do the right thing when accessing the proxyinfo fails for some reason. r=mt (3ea23173ea) - Bug 1125292 - Sending ALPN header field for WebRTC calls, r=bwc (16fda60c39) - Bug 1167443 - Fix verification of end-of-candidates in mochitests. r=mt (8d74546e68) - Bug 1192813 - update the default candidate as new candidates arrive. r=bwc (490ac80af2) - Bug 1206981 - prevent ICE TCP from being turned off under e10s. r=jesup (a38afd56b8) - Bug 1234578 - Assert if PCM is destroyed improperly. r=rjesup (f1aa0d7cbc) - Bug 1164564 - WorkerDebugger.initialize should not return failure when called more than once;r=khuey (c316c83af7) - Bug 1211903 - WorkerDebugger should live on the main thread;r=khuey (5586888e77) - Bug 1164581 - Adding an overload for NS_ProxyRelease that accepts already_AddRefed, and removing all the others. r=bobbyholley (bc70230689) - Bug 1186750 part 1 - Inlinize trivial constructors and destructors of events in DeviceStorageRequestParent. r=dhylands (0fc6b594b1) - Bug 1186750 part 2 - Remove some unused member fields in events in DeviceStorageRequestParent. r=dhylands (d4be7e7031) - Bug 1186750 part 3 - Abstract CancelableFileEvent in DeviceStorageReqeustParent and use already_AddRefed&& for passing DeviceStorageFile parameter. r=dhylands (cea4df4465) - Bug 1186750 part 4 - Clear runnable list in DeviceStorageRequestParent when being destroyed. r=dhylands (a4d6018ce6) - Bug 1196315 - Ensure MIME service is only accessed on the main thread. r=dhylands (20c07f4baf) - Bug 1186750 part 5 - Convert nsDOMDeviceStorage::CheckPermission to take already_AddRefed&&. r=dhylands (7b2d0b415e) - Bug 1186750 part 6 - Remove unused and unimplemented method nsDOMDeviceStorage::StorePermission. r=dhylands (e6772e7b51) - Bug 1186750 part 7 - Convert DispatchToOwningThread and DispatchOrAbandon to take already_AddRefed&&. r=dhylands (5925568a22) - Bug 1186750 part 8 - Convert DeviceStorageUsedSpaceCache::Dispatch to use already_AddRef&&. r=dhylands (660b44eec7) - Bug 1186750 part 9 - Use already_AddRefed&& to initialize mFile of device storage requests. r=dhylands (c94464f412) - Bug 1186750 part 10 - Simplify code in DeviceStorageRequestParent::Dispatch. r=dhylands (debcc219ca) - Bug 1186750 part 11 - Convert all usage of Dispatch/NS_DispatchToMainThread in dom/devicestorage to pass in either already_AddRefed or raw pointer. r=dhylands (753694d0b5) - Bug 1059469: Part 1 - Add a log module for dump() calls. r=bent (d94c677e49) - Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent (1edc485b0f) - Bug 1243881 - patch 1 - unship performance.translateTime, r=bz (5a4afeea67) - Bug 1243881 - patch 2 - unship performance.translateTime, r=bz (5bf9557cd4) - Bug 1165722 - Replace JS_GetPropertyDescriptor usage in Xray code. r=bholley (e277cbcc78) - Bug 1243824. Add support for static functions and attributes on JSXrays. r=bholley (498d6c6034) - Bug 1228456 - SharedWorker should close the MessagePort in case the connecting runnable is not dispatched, r=smaug (c14a3e212f) - Bug 779707 - Add crashtest. (e86caca48e) - Bug 1228456 - add 'override' to the Cancel() method of a nsICancelableRunnable, rs=me (48db3b97e9) - Bug 1131323 - Enable SharedWorker loads to be intercepted through service workers; r=nsm (b2d972c5e3) - Bug 1173002 - Set worker system principal flag correctly when created from chrome, r=bz, a=kwierso. (ac9fc2980d) - bits of 1113429 backout (a862f16bb7) - bug 1206312 - add IndexedDatabaseManager include to IDBKeyRange. r=bz (bd6663f976) - Bug 1247117: De-namespace much of IndexedDB. r=baku (a996e3b443) - Bug 1196841: Update getAll/getAllKeys to match the spec and expose them. r=baku (7365769e04) - Bug 1196840: Make IDBTransaction::ObjectStoreNames const. r=baku (e7af2b0510) - Bug 1176165 - Fix the exception codes returned from functions that modify the IndexedDB schema, r=janv. (efa4e818d0) - Bug 935753 - Firefox displays the "This is a secure Firefox page" indicator on pages served by addons. r=MattN (77dced27ad) - Bug 925681 - Show identity block and reload icon in awesomebar in Australis' customization mode. ui-r=shorlander, r=Gijs (ffd1b2f6a4) - Bug 970382 - Add about:accounts to the list of chrome UIs with a special identity mode r=gavin (6d2817d087) - Bug 1051847 - Add trusted identity block to about:license and about:rights. r=dao (aa8dfe4d1d) - Bug 1094947 - The trusted identity block is not displayed for the about:downloads page. r=jaws (1c51faa077) - Bug 686281 - Implement CSS mask style; r=dbaron. (2f823c4a49) - Bug 686281 - Mask CSS parsing and Mask DOM API. r=dbaron (f9cc291131) - Bug 686281 - Mask CSS rendering; r=mstange (b26ba7ba7e) - Bug 686281 - Mask CSS animation; r=dbaron. (4ce1ba671e) - Bug 686281 - Mask CSS webkit-alias; r=dbaron. (c27f4023d6) - Bug 686281 - Mask mochitest; r=dbaron. (010fcdfd04) - Bug 686281 - Expands will-change of a shorthand prop to longhand ones; r=dbaron. (f8e4a6dcfd) - Bug 686281 - A static assertion to keep value correctness of NS_RULE_NODE_IS_ANIMATION_RULE; r=dbaron. (5ae87b576b) - Bug 686281 - Remove nsStyleSVGReset::mMask; r=dbaron (1e7a0dfb45) - Bug 686281 - mask-composite reftests; r=dbaron (7f769e196a) - Bug 686281 - Rename nsStyleSVGReset::mLayers to nsStyleSVGReset::mMask; Rename nsStyleBackground::mLayers to nsStyleBackground::mImage. r=dbaron (3bd4fc6e3b) - Bug 1241275 - Change the way -moz-window-dragging works. r=heycam,roc (5691f2dbf5) - Bug 1246892 - pass aCTF as a reference instead of value. r=roc (98b0e45063) - Bug 1234800 - Reinstate code that adjusts dirty rects for fixed-position frames in display ports. r=tn (44e55ebacb) - Bug 1234800 - Move this line to the right place. r=tn (1a86a7fc72) - Bug 1216832 - Handle preserve-3d visible regions during display list building by always transforming from the preserve-3d root each time. r=roc (1887af1172) - Bug 1231243 - In nsDisplayBackgroundImage::GetBoundsInternal(), take the union of the image bounds and the viewport bounds if APZ is enabled. r=mstange (87a1fa0ab4) - Bug 1246622 - Handle nested preserve-3d contexts when hit testing. r=roc (6eed51c734) - Bug 1235945 - Fix assertion error in some cases when running szip when debug flags are enabled for host tools. r=froydnj (3a0aa4f728) - Bug 1224798: Do not produce a clip mask if our context is entirely clipped out anyway. r=jrmuizel (3926a4ef7d) - Bug 1223604 - Disentangle nsSVGClipPathFrame::ApplyClipOrPaintClipMask and make the code easier to understand. r=Bas (c8c19a1b0d) - Bug 1204405: Don't access prefs off main thread in testing ProcessLink::Open(). r=khuey (301aa7259d) - Bug 1248896 - don't conditional compile on config ENABLE_TESTS in Nuwa. r=khuey (4f2fd275fd) - Bug 1232458 - use UniquePtr<T[]> instead of nsAutoArrayPtr<T> in WindowsDllBlocklist.cpp; r=aklotz (292071bdb5) - Bug 1247741 - Additional checks for pointer validity in LdrLoadDLL detour. r=aklotz (8ee48e8cf3) - Bug 1113930 - Move __libc_stack_end related code block from StackWalk.cpp in a non-OSX section. r=froydnj (4f0f9e2e66) - Bug 1113930 - Use the actual stack end address on x86 OSX and Android for the stack walker. r=froydnj (7371d9a508) - missing bit of Bug 1216681 (fdf69e362f) - Bug 1193593 - Test fingerprinting resistance for media queries in picture elements. r=heycam (6155b73c26) - Bug 1232829 - Detach obsolete DocumentTimeline from refresh driver when the document is reset; r=smaug (564680e2a0) - Bug 1075457, part 1 - Implement rendering for |clip-path:polygon()|. r=mstange, r=jwatt (76056caacd) - Bug 1075457, part 2 - Implement circle() and ellipse() for the |clip-path| property. r=mstange, r=jwatt (4b8b39c682) - Bug 1094571 - add unicode-range load tests. r=heycam (3358555411) - Bug 1216695 - Remove the Request.context specific bits from fetch-request-resources.https.html; r=bkelly (2315e50b97) - Bug 1193133 - Disable broken service worker wpt tests. r=bkelly (8f0205d5e7) - Bug 1199831: Fix a bunch of mixed-content violations in imported ServiceWorker WPTs. r=jdm (33f261ce91) - bit of Bug 603201 (325170577f) - Bug 1184798 - same origin, cors and no-cors load tests. r=bkelly (f8549dd0bb) - Bug 1210581: Test controlled worker loads (XHR, fetch, importScripts). r=ehsan (41a436df47) - Bug 1215196 - Fix web-platform-tests iframe scripts to avoid pulling in testharness.js in them; r=bkelly (a2edb0784c) - Bug 1242798 - Don't OSR into Ion on debuggee frames. (r=jandem) (21e17bdd9d) - Bug 1238658 - Allow setElem-accessor optimizations only for native baseHolder objects; r=efaust (12c9766a53) - Bug 1144630 - Follup: Fix review nit. (rs=evilpie) (67b5cc2c7f) - Bug 1182866 - Fix Baseline GETNAME stubs to check for uninitialized lexicals. (r=jandem) (dd47d2025a) - Bug 1189536 - Make fetch-request-xhr.https.html pass; r=bkelly (ce177226bf) - Bug 1188822 - Make service-workers/service-worker/fetch-request-resources.https.html pass. r=bkelly (3a5f3a6660)
2031 lines
58 KiB
C++
2031 lines
58 KiB
C++
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "ScriptLoader.h"
|
|
|
|
#include "nsIChannel.h"
|
|
#include "nsIContentPolicy.h"
|
|
#include "nsIContentSecurityPolicy.h"
|
|
#include "nsIHttpChannel.h"
|
|
#include "nsIHttpChannelInternal.h"
|
|
#include "nsIInputStreamPump.h"
|
|
#include "nsIIOService.h"
|
|
#include "nsIProtocolHandler.h"
|
|
#include "nsIScriptError.h"
|
|
#include "nsIScriptSecurityManager.h"
|
|
#include "nsIStreamLoader.h"
|
|
#include "nsIStreamListenerTee.h"
|
|
#include "nsIThreadRetargetableRequest.h"
|
|
#include "nsIURI.h"
|
|
|
|
#include "jsapi.h"
|
|
#include "jsfriendapi.h"
|
|
#include "nsError.h"
|
|
#include "nsContentPolicyUtils.h"
|
|
#include "nsContentUtils.h"
|
|
#include "nsDocShellCID.h"
|
|
#include "nsISupportsPrimitives.h"
|
|
#include "nsNetUtil.h"
|
|
#include "nsIPipe.h"
|
|
#include "nsIOutputStream.h"
|
|
#include "nsScriptLoader.h"
|
|
#include "nsString.h"
|
|
#include "nsStreamUtils.h"
|
|
#include "nsTArray.h"
|
|
#include "nsThreadUtils.h"
|
|
#include "nsXPCOM.h"
|
|
#include "xpcpublic.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
#include "mozilla/LoadContext.h"
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
|
#include "mozilla/dom/CacheBinding.h"
|
|
#include "mozilla/dom/cache/CacheTypes.h"
|
|
#include "mozilla/dom/cache/Cache.h"
|
|
#include "mozilla/dom/cache/CacheStorage.h"
|
|
#include "mozilla/dom/ChannelInfo.h"
|
|
#include "mozilla/dom/Exceptions.h"
|
|
#include "mozilla/dom/InternalResponse.h"
|
|
#include "mozilla/dom/Promise.h"
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
|
#include "mozilla/dom/Response.h"
|
|
#include "mozilla/UniquePtr.h"
|
|
#include "Principal.h"
|
|
#include "WorkerFeature.h"
|
|
#include "WorkerPrivate.h"
|
|
#include "WorkerRunnable.h"
|
|
#include "WorkerScope.h"
|
|
|
|
#define MAX_CONCURRENT_SCRIPTS 1000
|
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
using mozilla::dom::cache::Cache;
|
|
using mozilla::dom::cache::CacheStorage;
|
|
using mozilla::dom::Promise;
|
|
using mozilla::dom::PromiseNativeHandler;
|
|
using mozilla::ErrorResult;
|
|
using mozilla::ipc::PrincipalInfo;
|
|
using mozilla::UniquePtr;
|
|
|
|
namespace {
|
|
|
|
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;
|
|
}
|
|
|
|
nsresult
|
|
ChannelFromScriptURL(nsIPrincipal* principal,
|
|
nsIURI* baseURI,
|
|
nsIDocument* parentDoc,
|
|
nsILoadGroup* loadGroup,
|
|
nsIIOService* ios,
|
|
nsIScriptSecurityManager* secMan,
|
|
const nsAString& aScriptURL,
|
|
bool aIsMainScript,
|
|
WorkerScriptType aWorkerScriptType,
|
|
nsContentPolicyType aContentPolicyType,
|
|
nsLoadFlags aLoadFlags,
|
|
nsIChannel** aChannel)
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
nsresult rv;
|
|
nsCOMPtr<nsIURI> uri;
|
|
rv = nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(uri),
|
|
aScriptURL, parentDoc,
|
|
baseURI);
|
|
if (NS_FAILED(rv)) {
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
aLoadFlags |= nsIChannel::LOAD_CLASSIFY_URI;
|
|
uint32_t secFlags = aIsMainScript ? nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED
|
|
: nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS;
|
|
|
|
if (aWorkerScriptType == DebuggerScript) {
|
|
// 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;
|
|
}
|
|
|
|
if (!isUIResource) {
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
}
|
|
|
|
secFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
// 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) {
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
uri,
|
|
parentDoc,
|
|
secFlags,
|
|
aContentPolicyType,
|
|
loadGroup,
|
|
nullptr, // aCallbacks
|
|
aLoadFlags,
|
|
ios);
|
|
} else {
|
|
// 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));
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
uri,
|
|
principal,
|
|
secFlags,
|
|
aContentPolicyType,
|
|
loadGroup,
|
|
nullptr, // aCallbacks
|
|
aLoadFlags,
|
|
ios);
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(channel)) {
|
|
rv = nsContentUtils::SetFetchReferrerURIWithPolicy(principal, parentDoc,
|
|
httpChannel);
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
return rv;
|
|
}
|
|
}
|
|
|
|
channel.forget(aChannel);
|
|
return rv;
|
|
}
|
|
|
|
struct ScriptLoadInfo
|
|
{
|
|
ScriptLoadInfo()
|
|
: mScriptTextBuf(nullptr)
|
|
, mScriptTextLength(0)
|
|
, mLoadResult(NS_ERROR_NOT_INITIALIZED)
|
|
, mLoadingFinished(false)
|
|
, mExecutionScheduled(false)
|
|
, mExecutionResult(false)
|
|
, mCacheStatus(Uncached)
|
|
{ }
|
|
|
|
~ScriptLoadInfo()
|
|
{
|
|
if (mScriptTextBuf) {
|
|
js_free(mScriptTextBuf);
|
|
}
|
|
}
|
|
|
|
bool
|
|
ReadyToExecute()
|
|
{
|
|
return !mChannel && NS_SUCCEEDED(mLoadResult) && !mExecutionScheduled;
|
|
}
|
|
|
|
nsString mURL;
|
|
|
|
// 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.
|
|
RefPtr<Promise> mCachePromise;
|
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
|
char16_t* mScriptTextBuf;
|
|
size_t mScriptTextLength;
|
|
|
|
nsresult mLoadResult;
|
|
bool mLoadingFinished;
|
|
bool mExecutionScheduled;
|
|
bool mExecutionResult;
|
|
|
|
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;
|
|
|
|
Maybe<bool> mMutedErrorFlag;
|
|
|
|
bool Finished() const
|
|
{
|
|
return mLoadingFinished && !mCachePromise && !mChannel;
|
|
}
|
|
};
|
|
|
|
class ScriptLoaderRunnable;
|
|
|
|
class ScriptExecutorRunnable final : public MainThreadWorkerSyncRunnable
|
|
{
|
|
ScriptLoaderRunnable& mScriptLoader;
|
|
bool mIsWorkerScript;
|
|
uint32_t mFirstIndex;
|
|
uint32_t mLastIndex;
|
|
|
|
public:
|
|
ScriptExecutorRunnable(ScriptLoaderRunnable& aScriptLoader,
|
|
nsIEventTarget* aSyncLoopTarget,
|
|
bool aIsWorkerScript,
|
|
uint32_t aFirstIndex,
|
|
uint32_t aLastIndex);
|
|
|
|
private:
|
|
~ScriptExecutorRunnable()
|
|
{ }
|
|
|
|
virtual bool
|
|
IsDebuggerRunnable() const override;
|
|
|
|
virtual bool
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override;
|
|
|
|
virtual void
|
|
PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, bool aRunResult)
|
|
override;
|
|
|
|
NS_DECL_NSICANCELABLERUNNABLE
|
|
|
|
void
|
|
ShutdownScriptLoader(JSContext* aCx,
|
|
WorkerPrivate* aWorkerPrivate,
|
|
bool aResult,
|
|
bool aMutedError);
|
|
|
|
void LogExceptionToConsole(JSContext* aCx,
|
|
WorkerPrivate* WorkerPrivate);
|
|
};
|
|
|
|
class CacheScriptLoader;
|
|
|
|
class CacheCreator final : public PromiseNativeHandler
|
|
{
|
|
public:
|
|
NS_DECL_ISUPPORTS
|
|
|
|
explicit CacheCreator(WorkerPrivate* aWorkerPrivate)
|
|
: mCacheName(aWorkerPrivate->ServiceWorkerCacheName())
|
|
, mPrivateBrowsing(aWorkerPrivate->IsInPrivateBrowsing())
|
|
{
|
|
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
|
|
MOZ_ASSERT(aWorkerPrivate->LoadScriptAsPartOfLoadingServiceWorkerScript());
|
|
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);
|
|
|
|
RefPtr<Cache> mCache;
|
|
RefPtr<CacheStorage> mCacheStorage;
|
|
nsCOMPtr<nsIGlobalObject> mSandboxGlobalObject;
|
|
nsTArray<RefPtr<CacheScriptLoader>> mLoaders;
|
|
|
|
nsString mCacheName;
|
|
bool mPrivateBrowsing;
|
|
};
|
|
|
|
NS_IMPL_ISUPPORTS0(CacheCreator)
|
|
|
|
class CacheScriptLoader final : public PromiseNativeHandler
|
|
, public nsIStreamLoaderObserver
|
|
{
|
|
public:
|
|
NS_DECL_ISUPPORTS
|
|
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;
|
|
RefPtr<ScriptLoaderRunnable> mRunnable;
|
|
bool mIsWorkerScript;
|
|
bool mFailed;
|
|
nsCOMPtr<nsIInputStreamPump> mPump;
|
|
nsCOMPtr<nsIURI> mBaseURI;
|
|
mozilla::dom::ChannelInfo mChannelInfo;
|
|
UniquePtr<PrincipalInfo> mPrincipalInfo;
|
|
};
|
|
|
|
NS_IMPL_ISUPPORTS(CacheScriptLoader, nsIStreamLoaderObserver)
|
|
|
|
class CachePromiseHandler final : public PromiseNativeHandler
|
|
{
|
|
public:
|
|
NS_DECL_ISUPPORTS
|
|
|
|
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();
|
|
}
|
|
|
|
RefPtr<ScriptLoaderRunnable> mRunnable;
|
|
ScriptLoadInfo& mLoadInfo;
|
|
uint32_t mIndex;
|
|
};
|
|
|
|
NS_IMPL_ISUPPORTS0(CachePromiseHandler)
|
|
|
|
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)
|
|
|
|
class ScriptLoaderRunnable final : public WorkerFeature
|
|
, public nsIRunnable
|
|
{
|
|
friend class ScriptExecutorRunnable;
|
|
friend class CachePromiseHandler;
|
|
friend class CacheScriptLoader;
|
|
friend class LoaderListener;
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
|
nsTArray<ScriptLoadInfo> mLoadInfos;
|
|
RefPtr<CacheCreator> mCacheCreator;
|
|
nsCOMPtr<nsIInputStream> mReader;
|
|
bool mIsMainScript;
|
|
WorkerScriptType mWorkerScriptType;
|
|
bool mCanceled;
|
|
bool mCanceledMainThread;
|
|
ErrorResult& mRv;
|
|
|
|
public:
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
ScriptLoaderRunnable(WorkerPrivate* aWorkerPrivate,
|
|
nsIEventTarget* aSyncLoopTarget,
|
|
nsTArray<ScriptLoadInfo>& aLoadInfos,
|
|
bool aIsMainScript,
|
|
WorkerScriptType aWorkerScriptType,
|
|
ErrorResult& aRv)
|
|
: mWorkerPrivate(aWorkerPrivate), mSyncLoopTarget(aSyncLoopTarget),
|
|
mIsMainScript(aIsMainScript), mWorkerScriptType(aWorkerScriptType),
|
|
mCanceled(false), mCanceledMainThread(false), mRv(aRv)
|
|
{
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
MOZ_ASSERT(aSyncLoopTarget);
|
|
MOZ_ASSERT_IF(aIsMainScript, aLoadInfos.Length() == 1);
|
|
|
|
mLoadInfos.SwapElements(aLoadInfos);
|
|
}
|
|
|
|
private:
|
|
~ScriptLoaderRunnable()
|
|
{ }
|
|
|
|
NS_IMETHOD
|
|
Run() override
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
if (NS_FAILED(RunInternal())) {
|
|
CancelMainThread();
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
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.
|
|
if (loadInfo.Finished()) {
|
|
ExecuteFinishedScripts();
|
|
}
|
|
}
|
|
|
|
nsresult
|
|
OnStreamComplete(nsIStreamLoader* aLoader, uint32_t aIndex,
|
|
nsresult aStatus, uint32_t aStringLen,
|
|
const uint8_t* aString)
|
|
{
|
|
AssertIsOnMainThread();
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
|
|
|
nsresult rv = OnStreamCompleteInternal(aLoader, aStatus, aStringLen,
|
|
aString, mLoadInfos[aIndex]);
|
|
LoadingFinished(aIndex, rv);
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult
|
|
OnStartRequest(nsIRequest* aRequest, uint32_t aIndex)
|
|
{
|
|
AssertIsOnMainThread();
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
|
|
MOZ_ASSERT(channel == loadInfo.mChannel);
|
|
|
|
// We synthesize the result code, but its never exposed to content.
|
|
RefPtr<mozilla::dom::InternalResponse> ir =
|
|
new mozilla::dom::InternalResponse(200, NS_LITERAL_CSTRING("OK"));
|
|
ir->SetBody(mReader);
|
|
|
|
// Set the channel info of the channel on the response so that it's
|
|
// saved in the cache.
|
|
ir->InitChannelInfo(channel);
|
|
|
|
// 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));
|
|
|
|
RefPtr<mozilla::dom::Response> response =
|
|
new mozilla::dom::Response(mCacheCreator->Global(), ir);
|
|
|
|
mozilla::dom::RequestOrUSVString request;
|
|
|
|
MOZ_ASSERT(!loadInfo.mFullURL.IsEmpty());
|
|
request.SetAsUSVString().Rebind(loadInfo.mFullURL.Data(),
|
|
loadInfo.mFullURL.Length());
|
|
|
|
ErrorResult error;
|
|
RefPtr<Promise> cachePromise =
|
|
mCacheCreator->Cache_()->Put(request, *response, error);
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
nsresult rv = error.StealNSResult();
|
|
channel->Cancel(rv);
|
|
return rv;
|
|
}
|
|
|
|
RefPtr<CachePromiseHandler> promiseHandler =
|
|
new CachePromiseHandler(this, loadInfo, aIndex);
|
|
cachePromise->AppendNativeHandler(promiseHandler);
|
|
|
|
loadInfo.mCachePromise.swap(cachePromise);
|
|
loadInfo.mCacheStatus = ScriptLoadInfo::WritingToCache;
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
virtual bool
|
|
Notify(JSContext* aCx, Status aStatus) override
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (aStatus >= Terminating && !mCanceled) {
|
|
mCanceled = true;
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
NS_NewRunnableMethod(this, &ScriptLoaderRunnable::CancelMainThread);
|
|
NS_ASSERTION(runnable, "This should never fail!");
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(runnable))) {
|
|
JS_ReportError(aCx, "Failed to cancel script loader!");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
IsMainWorkerScript() const
|
|
{
|
|
return mIsMainScript && mWorkerScriptType == WorkerScript;
|
|
}
|
|
|
|
void
|
|
CancelMainThread()
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
if (mCanceledMainThread) {
|
|
return;
|
|
}
|
|
|
|
mCanceledMainThread = true;
|
|
|
|
if (mCacheCreator) {
|
|
MOZ_ASSERT(mWorkerPrivate->IsServiceWorker());
|
|
DeleteCache();
|
|
}
|
|
|
|
// Cancel all the channels that were already opened.
|
|
for (uint32_t index = 0; index < mLoadInfos.Length(); index++) {
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[index];
|
|
|
|
// If promise or channel is non-null, their failures will lead to
|
|
// LoadingFinished being called.
|
|
bool callLoadingFinished = true;
|
|
|
|
if (loadInfo.mCachePromise) {
|
|
MOZ_ASSERT(mWorkerPrivate->IsServiceWorker());
|
|
loadInfo.mCachePromise->MaybeReject(NS_BINDING_ABORTED);
|
|
loadInfo.mCachePromise = nullptr;
|
|
callLoadingFinished = false;
|
|
}
|
|
|
|
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()) {
|
|
LoadingFinished(index, NS_BINDING_ABORTED);
|
|
}
|
|
}
|
|
|
|
ExecuteFinishedScripts();
|
|
}
|
|
|
|
void
|
|
DeleteCache()
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
if (!mCacheCreator) {
|
|
return;
|
|
}
|
|
|
|
mCacheCreator->DeleteCache();
|
|
mCacheCreator = nullptr;
|
|
}
|
|
|
|
nsresult
|
|
RunInternal()
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
if (IsMainWorkerScript() && mWorkerPrivate->IsServiceWorker()) {
|
|
mWorkerPrivate->SetLoadingWorkerScript(true);
|
|
}
|
|
|
|
if (!mWorkerPrivate->IsServiceWorker() ||
|
|
!mWorkerPrivate->LoadScriptAsPartOfLoadingServiceWorkerScript()) {
|
|
for (uint32_t index = 0, len = mLoadInfos.Length(); index < len;
|
|
++index) {
|
|
nsresult rv = LoadScript(index);
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
LoadingFinished(index, rv);
|
|
return rv;
|
|
}
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
MOZ_ASSERT(!mCacheCreator);
|
|
mCacheCreator = new CacheCreator(mWorkerPrivate);
|
|
|
|
for (uint32_t index = 0, len = mLoadInfos.Length(); index < len; ++index) {
|
|
RefPtr<CacheScriptLoader> loader =
|
|
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());
|
|
|
|
WorkerPrivate* parentWorker = mWorkerPrivate->GetParent();
|
|
|
|
// Figure out which principal to use.
|
|
nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
|
|
nsCOMPtr<nsILoadGroup> loadGroup = mWorkerPrivate->GetLoadGroup();
|
|
if (!principal) {
|
|
NS_ASSERTION(parentWorker, "Must have a principal!");
|
|
NS_ASSERTION(mIsMainScript, "Must have a principal for importScripts!");
|
|
|
|
principal = parentWorker->GetPrincipal();
|
|
loadGroup = parentWorker->GetLoadGroup();
|
|
}
|
|
NS_ASSERTION(principal, "This should never be null here!");
|
|
MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(loadGroup, principal));
|
|
|
|
// Figure out our base URI.
|
|
nsCOMPtr<nsIURI> baseURI = GetBaseURI(mIsMainScript, mWorkerPrivate);
|
|
|
|
// May be null.
|
|
nsCOMPtr<nsIDocument> parentDoc = mWorkerPrivate->GetDocument();
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
if (IsMainWorkerScript()) {
|
|
// May be null.
|
|
channel = mWorkerPrivate->ForgetWorkerChannel();
|
|
}
|
|
|
|
nsCOMPtr<nsIIOService> ios(do_GetIOService());
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
NS_ASSERTION(secMan, "This should never be null!");
|
|
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
|
nsresult& rv = loadInfo.mLoadResult;
|
|
|
|
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;
|
|
}
|
|
|
|
if (!channel) {
|
|
rv = ChannelFromScriptURL(principal, baseURI, parentDoc, loadGroup, ios,
|
|
secMan, loadInfo.mURL, IsMainWorkerScript(),
|
|
mWorkerScriptType,
|
|
mWorkerPrivate->ContentPolicyType(), loadFlags,
|
|
getter_AddRefs(channel));
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
return rv;
|
|
}
|
|
}
|
|
|
|
// We need to know which index we're on in OnStreamComplete so we know
|
|
// where to put the result.
|
|
RefPtr<LoaderListener> listener = new LoaderListener(this, aIndex);
|
|
|
|
// We don't care about progress so just use the simple stream loader for
|
|
// OnStreamComplete notification only.
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), listener);
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
return rv;
|
|
}
|
|
|
|
if (loadInfo.mCacheStatus != ScriptLoadInfo::ToBeCached) {
|
|
rv = channel->AsyncOpen2(loader);
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
return rv;
|
|
}
|
|
} else {
|
|
nsCOMPtr<nsIOutputStream> writer;
|
|
|
|
// In case we return early.
|
|
loadInfo.mCacheStatus = ScriptLoadInfo::Cancel;
|
|
|
|
rv = NS_NewPipe(getter_AddRefs(mReader), getter_AddRefs(writer), 0,
|
|
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);
|
|
rv = tee->Init(loader, writer, listener);
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
return rv;
|
|
}
|
|
|
|
nsresult rv = channel->AsyncOpen2(tee);
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
return rv;
|
|
}
|
|
}
|
|
|
|
loadInfo.mChannel.swap(channel);
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult
|
|
OnStreamCompleteInternal(nsIStreamLoader* aLoader, nsresult aStatus,
|
|
uint32_t aStringLen, const uint8_t* aString,
|
|
ScriptLoadInfo& aLoadInfo)
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
if (!aLoadInfo.mChannel) {
|
|
return NS_BINDING_ABORTED;
|
|
}
|
|
|
|
aLoadInfo.mChannel = nullptr;
|
|
|
|
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);
|
|
|
|
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();
|
|
}
|
|
|
|
// 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));
|
|
|
|
// Make sure we're not seeing the result of a 404 or something by checking
|
|
// the 'requestSucceeded' attribute on the http channel.
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request);
|
|
if (httpChannel) {
|
|
bool requestSucceeded;
|
|
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.
|
|
// Unlike <script> tags, Worker scripts are always decoded as UTF-8,
|
|
// per spec. So we explicitly pass in the charset hint.
|
|
rv = nsScriptLoader::ConvertToUTF16(aLoadInfo.mChannel, aString, aStringLen,
|
|
NS_LITERAL_STRING("UTF-8"), parentDoc,
|
|
aLoadInfo.mScriptTextBuf,
|
|
aLoadInfo.mScriptTextLength);
|
|
if (NS_FAILED(rv)) {
|
|
return rv;
|
|
}
|
|
|
|
if (!aLoadInfo.mScriptTextLength && !aLoadInfo.mScriptTextBuf) {
|
|
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
|
|
NS_LITERAL_CSTRING("DOM"), parentDoc,
|
|
nsContentUtils::eDOM_PROPERTIES,
|
|
"EmptyWorkerSourceWarning");
|
|
} else if (!aLoadInfo.mScriptTextBuf) {
|
|
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);
|
|
|
|
if (principal->Subsumes(channelPrincipal)) {
|
|
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.
|
|
if (IsMainWorkerScript()) {
|
|
// Take care of the base URI first.
|
|
mWorkerPrivate->SetBaseURI(finalURI);
|
|
|
|
// Store the channel info if needed.
|
|
mWorkerPrivate->InitChannelInfo(channel);
|
|
|
|
// 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;
|
|
rv = ssm->GetChannelResultPrincipal(channel, getter_AddRefs(channelPrincipal));
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsILoadGroup> channelLoadGroup;
|
|
rv = channel->GetLoadGroup(getter_AddRefs(channelLoadGroup));
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
MOZ_ASSERT(channelLoadGroup);
|
|
|
|
// If the load principal is the system principal then the channel
|
|
// principal must also be the system principal (we do not allow chrome
|
|
// 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).
|
|
if (nsContentUtils::IsSystemPrincipal(loadPrincipal)) {
|
|
if (!nsContentUtils::IsSystemPrincipal(channelPrincipal)) {
|
|
// 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;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
DataReceived();
|
|
return NS_OK;
|
|
}
|
|
|
|
void
|
|
DataReceivedFromCache(uint32_t aIndex, const uint8_t* aString,
|
|
uint32_t aStringLen,
|
|
const mozilla::dom::ChannelInfo& aChannelInfo,
|
|
UniquePtr<PrincipalInfo> aPrincipalInfo)
|
|
{
|
|
AssertIsOnMainThread();
|
|
MOZ_ASSERT(aIndex < mLoadInfos.Length());
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
|
|
MOZ_ASSERT(loadInfo.mCacheStatus == ScriptLoadInfo::Cached);
|
|
|
|
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();
|
|
}
|
|
|
|
loadInfo.mMutedErrorFlag.emplace(!principal->Subsumes(responsePrincipal));
|
|
|
|
// May be null.
|
|
nsIDocument* parentDoc = mWorkerPrivate->GetDocument();
|
|
|
|
MOZ_ASSERT(!loadInfo.mScriptTextBuf);
|
|
|
|
nsresult rv =
|
|
nsScriptLoader::ConvertToUTF16(nullptr, aString, aStringLen,
|
|
NS_LITERAL_STRING("UTF-8"), parentDoc,
|
|
loadInfo.mScriptTextBuf,
|
|
loadInfo.mScriptTextLength);
|
|
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);
|
|
}
|
|
|
|
mozilla::DebugOnly<nsIPrincipal*> principal = mWorkerPrivate->GetPrincipal();
|
|
MOZ_ASSERT(principal);
|
|
nsILoadGroup* loadGroup = mWorkerPrivate->GetLoadGroup();
|
|
MOZ_ASSERT(loadGroup);
|
|
|
|
mozilla::DebugOnly<bool> equal = false;
|
|
MOZ_ASSERT(responsePrincipal && NS_SUCCEEDED(responsePrincipal->Equals(principal, &equal)));
|
|
MOZ_ASSERT(equal);
|
|
|
|
mWorkerPrivate->InitChannelInfo(aChannelInfo);
|
|
mWorkerPrivate->SetPrincipal(responsePrincipal, loadGroup);
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
DataReceived();
|
|
}
|
|
|
|
LoadingFinished(aIndex, rv);
|
|
}
|
|
|
|
void
|
|
DataReceived()
|
|
{
|
|
if (IsMainWorkerScript()) {
|
|
WorkerPrivate* parent = mWorkerPrivate->GetParent();
|
|
|
|
if (parent) {
|
|
// XHR Params Allowed
|
|
mWorkerPrivate->SetXHRParamsAllowed(parent->XHRParamsAllowed());
|
|
|
|
// Set Eval and ContentSecurityPolicy
|
|
mWorkerPrivate->SetCSP(parent->GetCSP());
|
|
mWorkerPrivate->SetEvalAllowed(parent->IsEvalAllowed());
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
ExecuteFinishedScripts()
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
if (IsMainWorkerScript()) {
|
|
mWorkerPrivate->WorkerScriptLoaded();
|
|
}
|
|
|
|
uint32_t firstIndex = UINT32_MAX;
|
|
uint32_t lastIndex = UINT32_MAX;
|
|
|
|
// Find firstIndex based on whether mExecutionScheduled is unset.
|
|
for (uint32_t index = 0; index < mLoadInfos.Length(); index++) {
|
|
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.
|
|
if (firstIndex != UINT32_MAX) {
|
|
for (uint32_t index = firstIndex; index < mLoadInfos.Length(); index++) {
|
|
ScriptLoadInfo& loadInfo = mLoadInfos[index];
|
|
|
|
if (!loadInfo.Finished()) {
|
|
break;
|
|
}
|
|
|
|
// We can execute this one.
|
|
loadInfo.mExecutionScheduled = true;
|
|
|
|
lastIndex = index;
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
if (firstIndex != UINT32_MAX && lastIndex != UINT32_MAX) {
|
|
RefPtr<ScriptExecutorRunnable> runnable =
|
|
new ScriptExecutorRunnable(*this, mSyncLoopTarget, IsMainWorkerScript(),
|
|
firstIndex, lastIndex);
|
|
if (!runnable->Dispatch(nullptr)) {
|
|
MOZ_ASSERT(false, "This should never fail!");
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
NS_IMPL_ISUPPORTS(ScriptLoaderRunnable, nsIRunnable)
|
|
|
|
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);
|
|
}
|
|
|
|
void
|
|
CachePromiseHandler::ResolvedCallback(JSContext* aCx,
|
|
JS::Handle<JS::Value> aValue)
|
|
{
|
|
AssertIsOnMainThread();
|
|
// 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);
|
|
|
|
if (mLoadInfo.mCachePromise) {
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::Cached;
|
|
mLoadInfo.mCachePromise = nullptr;
|
|
mRunnable->MaybeExecuteFinishedScripts(mIndex);
|
|
}
|
|
}
|
|
|
|
void
|
|
CachePromiseHandler::RejectedCallback(JSContext* aCx,
|
|
JS::Handle<JS::Value> aValue)
|
|
{
|
|
AssertIsOnMainThread();
|
|
// May already have been canceled by CacheScriptLoader::Fail from
|
|
// CancelMainThread.
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::WritingToCache ||
|
|
mLoadInfo.mCacheStatus == ScriptLoadInfo::Cancel);
|
|
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!");
|
|
|
|
mozilla::AutoSafeJSContext cx;
|
|
JS::Rooted<JSObject*> sandbox(cx);
|
|
nsresult rv = xpc->CreateSandbox(cx, aPrincipal, sandbox.address());
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
return rv;
|
|
}
|
|
|
|
mSandboxGlobalObject = xpc::NativeGlobal(sandbox);
|
|
if (NS_WARN_IF(!mSandboxGlobalObject)) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Create a CacheStorage bypassing its trusted origin checks. The
|
|
// ServiceWorker has already performed its own checks before getting
|
|
// to this point.
|
|
ErrorResult error;
|
|
mCacheStorage =
|
|
CacheStorage::CreateOnMainThread(mozilla::dom::cache::CHROME_ONLY_NAMESPACE,
|
|
mSandboxGlobalObject,
|
|
aPrincipal, mPrivateBrowsing,
|
|
true /* force trusted origin */,
|
|
error);
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
return error.StealNSResult();
|
|
}
|
|
|
|
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());
|
|
RefPtr<Promise> promise = mCacheStorage->Open(mCacheName, error);
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
return error.StealNSResult();
|
|
}
|
|
|
|
promise->AppendNativeHandler(this);
|
|
return NS_OK;
|
|
}
|
|
|
|
void
|
|
CacheCreator::FailLoaders(nsresult aRv)
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
// Fail() can call LoadingFinished() which may call ExecuteFinishedScripts()
|
|
// which sets mCacheCreator to null, so hold a ref.
|
|
RefPtr<CacheCreator> kungfuDeathGrip = this;
|
|
|
|
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.
|
|
RefPtr<Promise> promise = mCacheStorage->Delete(mCacheName, rv);
|
|
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;
|
|
|
|
// Stop if the load was aborted on the main thread.
|
|
// 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);
|
|
return;
|
|
}
|
|
|
|
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);
|
|
|
|
mozilla::dom::RequestOrUSVString request;
|
|
request.SetAsUSVString().Rebind(mLoadInfo.mFullURL.Data(),
|
|
mLoadInfo.mFullURL.Length());
|
|
|
|
mozilla::dom::CacheQueryOptions params;
|
|
|
|
ErrorResult error;
|
|
RefPtr<Promise> promise = aCache->Match(request, params, error);
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
Fail(error.StealNSResult());
|
|
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;
|
|
}
|
|
|
|
MOZ_ASSERT(mLoadInfo.mCacheStatus == ScriptLoadInfo::Uncached);
|
|
|
|
nsresult rv;
|
|
|
|
if (aValue.isUndefined()) {
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::ToBeCached;
|
|
rv = mRunnable->LoadScript(mIndex);
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
Fail(rv);
|
|
}
|
|
return;
|
|
}
|
|
|
|
MOZ_ASSERT(aValue.isObject());
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
|
|
mozilla::dom::Response* response = nullptr;
|
|
rv = UNWRAP_OBJECT(Response, obj, response);
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
Fail(rv);
|
|
return;
|
|
}
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
response->GetBody(getter_AddRefs(inputStream));
|
|
mChannelInfo = response->GetChannelInfo();
|
|
const UniquePtr<PrincipalInfo>& pInfo = response->GetPrincipalInfo();
|
|
if (pInfo) {
|
|
mPrincipalInfo = mozilla::MakeUnique<PrincipalInfo>(*pInfo);
|
|
}
|
|
|
|
if (!inputStream) {
|
|
mLoadInfo.mCacheStatus = ScriptLoadInfo::Cached;
|
|
mRunnable->DataReceivedFromCache(mIndex, (uint8_t*)"", 0, mChannelInfo,
|
|
Move(mPrincipalInfo));
|
|
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;
|
|
|
|
MOZ_ASSERT(mPrincipalInfo);
|
|
mRunnable->DataReceivedFromCache(mIndex, aString, aStringLen, mChannelInfo,
|
|
Move(mPrincipalInfo));
|
|
return NS_OK;
|
|
}
|
|
|
|
class ChannelGetterRunnable final : public nsRunnable
|
|
{
|
|
WorkerPrivate* mParentWorker;
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
|
const nsAString& mScriptURL;
|
|
nsIChannel** mChannel;
|
|
nsresult mResult;
|
|
|
|
public:
|
|
ChannelGetterRunnable(WorkerPrivate* aParentWorker,
|
|
nsIEventTarget* aSyncLoopTarget,
|
|
const nsAString& aScriptURL,
|
|
nsIChannel** aChannel)
|
|
: mParentWorker(aParentWorker), mSyncLoopTarget(aSyncLoopTarget),
|
|
mScriptURL(aScriptURL), mChannel(aChannel), mResult(NS_ERROR_FAILURE)
|
|
{
|
|
MOZ_ASSERT(mParentWorker);
|
|
aParentWorker->AssertIsOnWorkerThread();
|
|
MOZ_ASSERT(aSyncLoopTarget);
|
|
}
|
|
|
|
NS_IMETHOD
|
|
Run() override
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
nsIPrincipal* principal = mParentWorker->GetPrincipal();
|
|
MOZ_ASSERT(principal);
|
|
|
|
// Figure out our base URI.
|
|
nsCOMPtr<nsIURI> baseURI = mParentWorker->GetBaseURI();
|
|
MOZ_ASSERT(baseURI);
|
|
|
|
// May be null.
|
|
nsCOMPtr<nsIDocument> parentDoc = mParentWorker->GetDocument();
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = mParentWorker->GetLoadGroup();
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
mResult =
|
|
scriptloader::ChannelFromScriptURLMainThread(principal, baseURI,
|
|
parentDoc, loadGroup,
|
|
mScriptURL,
|
|
// Nested workers are always dedicated.
|
|
nsIContentPolicy::TYPE_INTERNAL_WORKER,
|
|
getter_AddRefs(channel));
|
|
if (NS_SUCCEEDED(mResult)) {
|
|
channel.forget(mChannel);
|
|
}
|
|
|
|
RefPtr<MainThreadStopSyncLoopRunnable> runnable =
|
|
new MainThreadStopSyncLoopRunnable(mParentWorker,
|
|
mSyncLoopTarget.forget(), true);
|
|
if (!runnable->Dispatch(nullptr)) {
|
|
NS_ERROR("This should never fail!");
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult
|
|
GetResult() const
|
|
{
|
|
return mResult;
|
|
}
|
|
|
|
private:
|
|
virtual ~ChannelGetterRunnable()
|
|
{ }
|
|
};
|
|
|
|
ScriptExecutorRunnable::ScriptExecutorRunnable(
|
|
ScriptLoaderRunnable& aScriptLoader,
|
|
nsIEventTarget* aSyncLoopTarget,
|
|
bool aIsWorkerScript,
|
|
uint32_t aFirstIndex,
|
|
uint32_t aLastIndex)
|
|
: MainThreadWorkerSyncRunnable(aScriptLoader.mWorkerPrivate, aSyncLoopTarget),
|
|
mScriptLoader(aScriptLoader), mIsWorkerScript(aIsWorkerScript),
|
|
mFirstIndex(aFirstIndex), mLastIndex(aLastIndex)
|
|
{
|
|
MOZ_ASSERT(aFirstIndex <= aLastIndex);
|
|
MOZ_ASSERT(aLastIndex < aScriptLoader.mLoadInfos.Length());
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
bool
|
|
ScriptExecutorRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
{
|
|
nsTArray<ScriptLoadInfo>& loadInfos = mScriptLoader.mLoadInfos;
|
|
|
|
// Don't run if something else has already failed.
|
|
for (uint32_t index = 0; index < mFirstIndex; index++) {
|
|
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;
|
|
}
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
JSAutoCompartment ac(aCx, global);
|
|
|
|
for (uint32_t index = mFirstIndex; index <= mLastIndex; index++) {
|
|
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)) {
|
|
scriptloader::ReportLoadError(aCx, loadInfo.mLoadResult);
|
|
// Top level scripts only!
|
|
if (mIsWorkerScript) {
|
|
aWorkerPrivate->MaybeDispatchLoadFailedRunnable();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
NS_ConvertUTF16toUTF8 filename(loadInfo.mURL);
|
|
|
|
JS::CompileOptions options(aCx);
|
|
options.setFileAndLine(filename.get(), 1)
|
|
.setNoScriptRval(true);
|
|
|
|
if (mScriptLoader.mWorkerScriptType == DebuggerScript) {
|
|
options.setVersion(JSVERSION_LATEST);
|
|
}
|
|
|
|
MOZ_ASSERT(loadInfo.mMutedErrorFlag.isSome());
|
|
options.setMutedErrors(loadInfo.mMutedErrorFlag.valueOr(true));
|
|
|
|
JS::SourceBufferHolder srcBuf(loadInfo.mScriptTextBuf,
|
|
loadInfo.mScriptTextLength,
|
|
JS::SourceBufferHolder::GiveOwnership);
|
|
loadInfo.mScriptTextBuf = nullptr;
|
|
loadInfo.mScriptTextLength = 0;
|
|
|
|
JS::Rooted<JS::Value> unused(aCx);
|
|
if (!JS::Evaluate(aCx, options, srcBuf, &unused)) {
|
|
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;
|
|
bool mutedError = false;
|
|
for (uint32_t index = 0; index < loadInfos.Length(); index++) {
|
|
if (!loadInfos[index].mExecutionResult) {
|
|
mutedError = loadInfos[index].mMutedErrorFlag.valueOr(true);
|
|
result = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
ShutdownScriptLoader(aCx, aWorkerPrivate, result, mutedError);
|
|
}
|
|
}
|
|
|
|
NS_IMETHODIMP
|
|
ScriptExecutorRunnable::Cancel()
|
|
{
|
|
if (mLastIndex == mScriptLoader.mLoadInfos.Length() - 1) {
|
|
ShutdownScriptLoader(mWorkerPrivate->GetJSContext(), mWorkerPrivate,
|
|
false, false);
|
|
}
|
|
return MainThreadWorkerSyncRunnable::Cancel();
|
|
}
|
|
|
|
void
|
|
ScriptExecutorRunnable::ShutdownScriptLoader(JSContext* aCx,
|
|
WorkerPrivate* aWorkerPrivate,
|
|
bool aResult,
|
|
bool aMutedError)
|
|
{
|
|
MOZ_ASSERT(mLastIndex == mScriptLoader.mLoadInfos.Length() - 1);
|
|
|
|
if (mIsWorkerScript && aWorkerPrivate->IsServiceWorker()) {
|
|
aWorkerPrivate->SetLoadingWorkerScript(false);
|
|
}
|
|
|
|
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)) {
|
|
LogExceptionToConsole(aCx, aWorkerPrivate);
|
|
mScriptLoader.mRv.Throw(NS_ERROR_FAILURE);
|
|
} else {
|
|
mScriptLoader.mRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
}
|
|
}
|
|
|
|
aWorkerPrivate->RemoveFeature(aCx, &mScriptLoader);
|
|
aWorkerPrivate->StopSyncLoop(mSyncLoopTarget, aResult);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
RefPtr<xpc::ErrorReport> xpcReport = new xpc::ErrorReport();
|
|
xpcReport->Init(report.report(), report.message(),
|
|
aWorkerPrivate->IsChromeWorker(), aWorkerPrivate->WindowID());
|
|
|
|
RefPtr<AsyncErrorReporter> r = new AsyncErrorReporter(xpcReport);
|
|
NS_DispatchToMainThread(r);
|
|
}
|
|
|
|
void
|
|
LoadAllScripts(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
nsTArray<ScriptLoadInfo>& aLoadInfos, bool aIsMainScript,
|
|
WorkerScriptType aWorkerScriptType, ErrorResult& aRv)
|
|
{
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
NS_ASSERTION(!aLoadInfos.IsEmpty(), "Bad arguments!");
|
|
|
|
AutoSyncLoopHolder syncLoop(aWorkerPrivate);
|
|
|
|
RefPtr<ScriptLoaderRunnable> loader =
|
|
new ScriptLoaderRunnable(aWorkerPrivate, syncLoop.EventTarget(),
|
|
aLoadInfos, aIsMainScript, aWorkerScriptType,
|
|
aRv);
|
|
|
|
NS_ASSERTION(aLoadInfos.IsEmpty(), "Should have swapped!");
|
|
|
|
if (!aWorkerPrivate->AddFeature(aCx, loader)) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(loader))) {
|
|
NS_ERROR("Failed to dispatch!");
|
|
|
|
aWorkerPrivate->RemoveFeature(aCx, loader);
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
|
|
syncLoop.Run();
|
|
}
|
|
|
|
} /* anonymous namespace */
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
namespace scriptloader {
|
|
|
|
nsresult
|
|
ChannelFromScriptURLMainThread(nsIPrincipal* aPrincipal,
|
|
nsIURI* aBaseURI,
|
|
nsIDocument* aParentDoc,
|
|
nsILoadGroup* aLoadGroup,
|
|
const nsAString& aScriptURL,
|
|
nsContentPolicyType aContentPolicyType,
|
|
nsIChannel** aChannel)
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
nsCOMPtr<nsIIOService> ios(do_GetIOService());
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
NS_ASSERTION(secMan, "This should never be null!");
|
|
|
|
return ChannelFromScriptURL(aPrincipal, aBaseURI, aParentDoc, aLoadGroup,
|
|
ios, secMan, aScriptURL, true, WorkerScript,
|
|
aContentPolicyType, nsIRequest::LOAD_NORMAL,
|
|
aChannel);
|
|
}
|
|
|
|
nsresult
|
|
ChannelFromScriptURLWorkerThread(JSContext* aCx,
|
|
WorkerPrivate* aParent,
|
|
const nsAString& aScriptURL,
|
|
nsIChannel** aChannel)
|
|
{
|
|
aParent->AssertIsOnWorkerThread();
|
|
|
|
AutoSyncLoopHolder syncLoop(aParent);
|
|
|
|
RefPtr<ChannelGetterRunnable> getter =
|
|
new ChannelGetterRunnable(aParent, syncLoop.EventTarget(), aScriptURL,
|
|
aChannel);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(getter))) {
|
|
NS_ERROR("Failed to dispatch!");
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
if (!syncLoop.Run()) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
return getter->GetResult();
|
|
}
|
|
|
|
void ReportLoadError(JSContext* aCx, nsresult aLoadResult)
|
|
{
|
|
switch (aLoadResult) {
|
|
case NS_BINDING_ABORTED:
|
|
// Canceled, don't set an exception.
|
|
break;
|
|
|
|
case NS_ERROR_FILE_NOT_FOUND:
|
|
case NS_ERROR_NOT_AVAILABLE:
|
|
Throw(aCx, NS_ERROR_DOM_NETWORK_ERR);
|
|
break;
|
|
|
|
case NS_ERROR_MALFORMED_URI:
|
|
aLoadResult = NS_ERROR_DOM_SYNTAX_ERR;
|
|
MOZ_FALLTHROUGH;
|
|
case NS_ERROR_DOM_SECURITY_ERR:
|
|
case NS_ERROR_DOM_SYNTAX_ERR:
|
|
Throw(aCx, aLoadResult);
|
|
break;
|
|
|
|
default:
|
|
JS_ReportError(aCx, "Failed to load script (nsresult = 0x%x)", aLoadResult);
|
|
}
|
|
}
|
|
|
|
void
|
|
LoadMainScript(JSContext* aCx, const nsAString& aScriptURL,
|
|
WorkerScriptType aWorkerScriptType,
|
|
ErrorResult& aRv)
|
|
{
|
|
WorkerPrivate* worker = GetWorkerPrivateFromContext(aCx);
|
|
NS_ASSERTION(worker, "This should never be null!");
|
|
|
|
nsTArray<ScriptLoadInfo> loadInfos;
|
|
|
|
ScriptLoadInfo* info = loadInfos.AppendElement();
|
|
info->mURL = aScriptURL;
|
|
|
|
LoadAllScripts(aCx, worker, loadInfos, true, aWorkerScriptType, aRv);
|
|
}
|
|
|
|
void
|
|
Load(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
const nsTArray<nsString>& aScriptURLs, WorkerScriptType aWorkerScriptType,
|
|
ErrorResult& aRv)
|
|
{
|
|
const uint32_t urlCount = aScriptURLs.Length();
|
|
|
|
if (!urlCount) {
|
|
return;
|
|
}
|
|
|
|
if (urlCount > MAX_CONCURRENT_SCRIPTS) {
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
return;
|
|
}
|
|
|
|
nsTArray<ScriptLoadInfo> loadInfos;
|
|
loadInfos.SetLength(urlCount);
|
|
|
|
for (uint32_t index = 0; index < urlCount; index++) {
|
|
loadInfos[index].mURL = aScriptURLs[index];
|
|
}
|
|
|
|
LoadAllScripts(aCx, aWorkerPrivate, loadInfos, false, aWorkerScriptType, aRv);
|
|
}
|
|
|
|
} // namespace scriptloader
|
|
|
|
END_WORKERS_NAMESPACE
|