mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:30:27 +00:00
f18df9acf8
- Bug 1239496 - Load the UA sheet lazily so we can invalidate it for pref changes. Make it depend on the "layout.css.grid.enabled" pref. r=heycam (e4a1ac18b4)
- Bug 1225192 - fix eslint complaints about css-logic.js; r=pbrosset (ef19819c21)
- Bug 1225192 - remove leading whitespace before prettifying css; r=pbrosset (6b39d08907)
- Bug 1230491 - rewrite CssLogic.isContentStylesheet; r=bgrins (14c86cee67)
- Bug 1230491 - add CSSStyleSheet::parsingMode; r=heycam,bz (9ecabe232e)
- Bug 1239336 - set parsing mode on about:PreferenceStyleSheet; r=heycam,bgrins (cac07e099f)
- Bug 1224433 - Part 1: Add reftest. r=roc (28c8a47812)
- Bug 1195491 - use nsCOMPtr in RDFContentSinkImpl; r=bsmedberg (9804abe22e)
- Bug 1205713 - Merge ImageHostOverlay/ImageClientOverlay to ImageHost/ImageClient r=nical (2871e70466)
- Bug 1234472 - Add gonk sideband stream handling to gfx ipc r=nical (559f788a10)
- Bug 1049296 - Handle Sideband stream compositing in HwcComposer2D r=mwu,nical (b6dbac1a1b)
- Bug 1237508 - Odin: move GC allocation up to fix spurious hazard warning (there was an AutoKeepAtoms already) (r=hazard-red) (b5d0e54993)
- Bug 1229399: Group all IR's enums into a single one; r=luke (a7cfedc16a)
- Bug 1229399: Make {Get,Set}{Loc,Glo} opcodes type-independent; r=luke (0ed8e44098)
- Bug 1239211 - Odin: tweak wasm::Decoder (r=bbouvier) (980e08dc1e)
- Bug 1229399: Implement write/readVarU32 and use it for locals/globals; r=luke (5a6f654a14)
- Bug 1229399: Unite all the Emit* functions in WasmIonCompile; r=luke (64e517d18f)
- Bug 1240524: Fix AsmJS checkedValueType of Bool32x4; r=luke (d98ee763c7)
- Bug 1229399: Add list of wasm opcodes described in v8 design document; r=luke (1e05832624)
- Bug 1241339 - ObserverService may keep Console object alive until shutdown, r=baku (330d9e7bc0)
- Bug 1239177 - Odin: make calls more like wasm (r=bbouvier) (4bab27c7bc)
- Bug 1234985 - Odin: hoist read/writeOp into wasm::Decoder/Encoder (r=bbouvier) (de3ba3c3f9)
- Bug 1229399: Add test file forgotten in previous rebase/checkin; r=bustage (a2bdda0e86)
- Bug 1234985 - Odin: tweak ModuleGenerator interface for Baldr use (r=bbouvier) (5f52fd6777)
- Bug 1234985 - BaldrMonkey: testing-only (not content visible) wasm hello world (r=bbouvier) (e49e7f4db2)
- Bug 1234985 - Fix js/src/asmjs build errors with unified builds disabled. r=luke (acb7954471)
- Bug 1231335 - part 1, runtime switch for SAB+Atomics. r=waldo (973eb7edc1)
- Bug 1231335 - part 2, asm.js runtime SAB+Atomics gating. r=luke (b40a8d0b3f)
- Bug 1225040 - generalize jsapi-tests for SAB. r=arai (29240f91e1)
- Bug 1232211 - Don't perform various SharedArrayBuffer-centric typed array tests in builds with SharedArrayBuffer disabled (i.e. Aurora at next uplift). rs=lth over IRC, responding to cassandra!philor's concerns (f3df4b0c17)
- Bug 1231335 - part 3, testing functions and test cases. r=waldo (b91910ea28)
- Bug 1231335 - part 4, js shell. r=waldo (4a96addbde)
- Bug 1234985 - Odin: move mutedError and displayURL into AsmJSModule (r=bbouvier) (6be0172a15)
- Bug 1229399: Remove expression statements opcodes in wasm; r=luke (a046188e98)
- Bug 1229399: Allow to peek into the internal wasm IR; r=luke (f4b0497d89)
- Bug 1234985 - Odin: make names optional and supplied at the end (r=b ouvier) (ee7864fe7e)
- Bug 1238679 - Rename s/SIMD/Simd/ in type and function names. r=bbouvier (1d6c277644)
- Bug 1238679 - Add enum class SIMDOperation. r=bbouvier (eb7ce840da)
- Bug 1187232 - Access TypedArray's [[ArrayLength]] instead of performing property access; r=Waldo (a66850e72f)
- Bug 1239068 - Inline "PossiblyTypedArrayLength" intrinsic; r=Waldo (1a39045055)
- Bug 1238679 - Put JSJitInfo::depth in anonymous union. r=nbp (64d6859ee2)
- Bug 1238679 - Provide per-operation JSJitInfo for the SIMD functions. r=bbouvier (cd242c1f5d)
- Backed out changeset 7bda46f81215 (bug 1225031) for spidermonkey test failures (7c7de9af43)
- Bug 1225031 - get rid of the AnyTypedArray abstraction (updated). r=waldo (a8e3593280)
- Bug 1237284: Make inlineSimd* functions take MIRType to avoid an indirection; r=jolesen (c046f5ac06)
- Bug 1238679 - Make inlineSimdLoad/Store take a MIRType argument. r=bbouvier (7aceb59fc1)
- Bug 1238679 - Implement main SIMD inlining dispatch. r=bbouvier (3b19775b14)
- Bug 1237445 - Use GCHashMap for VMWrapperMap, r=terrence (4a5b3a1575)
- Bug 1241454: Hoist SimdTypeDescr::Type into SimdType, unify AsmJSSimdType and SimdType; r=jolesen (6cf5f03517)
- Bug 1241454: Unify AsmJSSimdOperation and SimdOperation; r=jolesen (7506dec0e4)
- Bug 1241454: Change SIMD opcodes encoding in wasm; r=luke, r=jolesen (afa2b74cab)
- Bug 1242804 - Baldr: check enum limits (r=bbouvier) (1a68daa1c3)
- Bug 1242804 - Baldr: add local/get_local/set_local (r=bbouvier) (cc2448d2e9)
- Bug 1243239 - Baldr: tighten signature index check (r=bbouvier) (fd7081ab39)
- Bug 1243373: Ensure all declared functions are defined; r=luke (3c42295668)
- Bug 1243031: Throw when WebAssembly is not supported; r=luke (ade7cdc6a6)
- Bug 1242772 - Baldr: fix unterminated string literal (r=bbouvier) (273e26e669)
- Bug 1241886: Fix debug assertion if we're in dead code; r=luke (59c009191b)
- Bug 1234985 - Odin: use VarU32 for integer literals (r=bbouvier) (62c7292462)
- Bug 1234985 - Odin: stop requiring explicit final return (r=bbouvier) (2a8bf1639e)
- Bug 1242949: Implement Block in WebAssembly; r=luke (e8997f2a2e)
- Bug 1242949: Add explicit keyword to WasmAstBlock ctor; r=bustage (239cdcf5d4)
- Bug 1243626 - Baldr: tweak block text format (r=bbouvier) (a6fe9248ce)
- Bug 1243252 - Baldr: refactor exports (r=bbouvier) (49ca6f519f)
- Bug 1243632 - Baldr: check TypeError vs. SyntaxError (r=bbouvier) (c70ab8e8ec)
- Bug 1243252 - Baldr: add import section (r=bbouvier) (16b98660c8)
- Bug 1243633 - Baldr: add call/call_import (r=bbouvier) (63dd480e7d)
- Bug 1234985 - Hoist CStringHasher (r=sfink) (c019cff4b3)
- Bug 1237445 - Implement GCRekeyableHashMap, r=terrence (c4acd75ad0)
- Bug 1244272 - BaldrMonkey: Implement support for i32.add. r=luke (ede8918dfe)
- Bug 1244272 - BaldrMonkey: Implement the rest of the i32 binary operators. r=luke (bf11a38b86)
- Bug 1244272 - BaldrMonkey: Implement the f32 and 64 operators. r=luke (2ea37d6a95)
- Bug 1244403 - Baldr: move kind out of WasmAstNode and into WasmAstExpr (r=bbouvier) (498fd0b4a5)
- Silence a GCC warning in JitCompartment.h. No bug#, rs=jandem on IRC. (a23ccc42b3)
- Bug 1237445 - Use GCHashMap for ICStubCodeMap, r=terrence (b1d970f3c9)
- Bug 1244403 - Baldr: remove unnecessary 'end' argument from (r=bbouvier) (8da28c587c)
- Bug 1244403 - Baldr: expose export func entry offsets directly (r=bbouvier) (c6f82070fb)
- Bug 1234862 - Part 2: Always use DefaultGCPolicy with GCVector; r=sfink (6d8cf44216)
- Bug 1237445 - Use GCHashTables for ObjectGroup sweeping, r=terrence (7fd47cc6fb)
- Bug 1237445 - Use GCHashMap for (CCW) WrapperMap, r=terrence (cf6788b6a4)
- Bug 1237447 - Disable the crashreporter when crashing intentionally in TestPLDHash. r=njn (d4744f47d4)
- Bug 1237445 - Rekey ArrayObjectTable, r=terrence (2ccfca974c)
- Bug 1234862 - Part 3: Always use DefaultGCPolicy for GCHashTables; r=sfink (d0f2472c1b)
- Bug 1234862 - Part 4: Always use DefaultGCPolicy for TraceableFifo; r=sfink (069e134534)
- Bug 1239494 - Use GCHashMap to simplify nsWrapperCache; r=jonco (0bb0069fc4)
- Bug 1234862 - Part 5: Rename DefaultGCPolicy to GCPolicy; r=sfink (cb93f0c133)
- Bug 1234862 - Part 5.1: Followup comment fixes; r=sfink (bd2930ddf2)
- Fix recent non-unified build bustage (no bug, rs=sunfish) (6c40eba8bc)
- Bug 1243815: Put hard limits to some variable lengths in wasm; r=luke (1df12aa08e)
- Bug 1244403 - Baldr: factor out DynamicLinkData (r=bbouvier) (f257b901a3)
- Bug 1244403 - Baldr: put import section first (r=bbouvier) (47e726c907)
- Bug 1244571 - BaldrMonkey: Implement the unary operators. r=luke (e22132ccb1)
- Bug 1244571 - BaldrMonkey: Implement the comparison operators. r=luke (aa6e7a71cd)
- Bug 1244571 - BaldrMonkey: Implement parsing, encoding, and decoding for the conversion operators. r=luke (73d65dc416)
- Bug 1244571 - BaldrMonkey: Type-check the unary and binary operators. r=luke (4d7d41d49b)
- Bug 1244571 - BaldrMonkey: Implement parsing, encoding, and decoding for i64 operators. r=luke (c42a9b5496)
- Bug 1244571 - BaldrMonkey: Improve the readability of the parser code. r=luke (298c9c57a5)
- Bug 1244571 - BaldrMonkey: Fail decoding for operators which are not yet implemented r=luke (fdd4deff21)
- Bug 1245250 - BaldrMonkey: Refacfor min/max to make variadicity AsmJS-specific r=luke (f30e0471b9)
- Bug 1242342: Add If and IfElse to WebAssembly; r=luke (653223fdd3)
- Bug 1247755 - Baldr: disallow duplicate signature table entries (r=sunfish) (419fd0be57)
- Bug 1234397 - dispatch on the correct value. r=luke (4c7d5f5105)
- Bug 1233863 - ARM64: Allow test to pass with --no-asmjs, and when no JIT exists. r=luke (b89522c52c)
- Bug 1244405 - Odin: switch to dynamic page size, move heap constants to Wasm (r=bbouvier) (31d4b684b7)
- Bug 1234038 - Self-host ArrayBuffer.prototype.slice. r=lth (ab81205e4a)
- Bug 1121937 - Implement %TypedArray%.prototype.sort; r=jorendorff (aef7bb1228)
- Bug 1101256 - Implement detachment checks for %TypedArray% methods (patch 1 of 2); r=Waldo (cb13a5078b)
- Bug 1101256 - Implement detachment checks for %TypedArray% methods (patch 2 of 2); r=Waldo (b429b87a3d)
- Bug 715181 - Self-host Array.sort; r=till (f87f85634c)
- Bug 1079844 - Adjust TypedArray.js comments to refer to detachment rather than neutering. r=mrrrgn (ac2b366958)
- Bug 1079844 - Rename scattered bits of 'neuter' terminology to detachment terminology. r=jandem (ae20a9677c)
- Bug 1079844 - Rename JS_ARRAYBUFFER_NEUTERED_FLAG to use detachment terminology. r=sfink (670281d8b0)
- Bug 1079844 - Rename isNeutered() to isDetached(), and rename isNeutered() on views to hasDetachedBuffer(). r=sfink (b5595746aa)
- Bug 1079844 - Convert the JS_NeuterArrayBufferObject API to detachment terminology. r=till (2d176589bd)
- Bug 1079844 - Rename JS_IsNeuteredArrayBufferObject to JS_IsDetachedArrayBufferObject. r=till (9607cf673d)
- Bug 1079844 - Refer to "detaching" instead of "neutering" of ArrayBuffers, in JIT optimization tracking code. r=jandem (a4a17233d0)
- Bug 1079844 - Rename TI's ObjectKey flag to use detachment terminology. r=jandem (8865017109)
- Bug 1079844 - Rename CheckForNeuteredTypedObject and the flag underlying it to use detachment terminology. r=jandem (67ace7669e)
- Bug 1079844 - Rename ABO::neuter to ABO::detach. r=sfink (37c71de755)
- Bug 1079844 - Rename ArrayBufferViewObject::neuter to ABVO::notifyBufferDetached. Detachment applies only to ArrayBuffers, and the methods didn't actually neuter anything, so "notify" makes more sense as the verb here. r=sfink (2ebf7779a7)
- Bug 1079844 - Rename ABO::neuterView to a standalone NoteViewBufferWasDetached function. r=sfink (3ce2977123)
- Bug 1244405 - Odin: refactor ArrayBufferObject::createForWasm out of prepareForAsmJS (r=bbouvier) (3f5ed523f0)
- Bug 1243633 - Odin: tidy up call emitting (r=bbouvier) (89256a6d57)
- Bug 1243633 - Odin: switch to lineOrBytecode from line/column (r=bbouvier) (ded5ff23de)
- Bug 1242342: Set return type accordingly to the compilation mode; r=luke (f4cdff1644)
- Bug 1244405 - Baldr: fix wasm function export name (r=bbouvier) (c6ea5f8194)
- Bug 1244405 - Baldr: add memory section (r=bbouvier) (c98fc66e50)
- Bug 1243252 - Remove const restriction on CharsToNewUTF8CharsZ (r=jandem) (9ef020aff6)
- fix neutered/detached (2a675ae62e)
- Bug 1242342: Replace Ternary by IfElse which return expressions; r=luke (aa1535de89)
- Bug 1244405 - Baldr: add memory exports (r=bbouvier) (9a495599df)
- Bug 1240583 - Odin: remove the sync interrupt stub (r=bbouvier) (a3811b0cc5)
- Bug 1242043 - {Array,%TypedArray%}.prototype.{i,lastI}ndexOf should never return -0. r=jorendorff (485f5d62dc)
- Bug 1180290 - Part 1: Add prefix parameter to IdToFunctionName. r=till (e33ed94f64)
- Bug 1180290 - Part 2: Handle prefix in DefinePropertyById. r=till,smaug (5c3333ef68)
- Bug 1180290 - Part 3: Use canonical name in native getter. r=till (213b6d1b7f)
- Bug 1180290 - Part 4: Add tests for builtin getter name. r=till (005f1e4da7)
- Bug 1180290 - Part 5: Remove getter/setter variant for ThrowInvalidThis message. r=bz (fa8224af50)
- Bug 1236548 - Allow JS_UNINITIALIZED_LEXICAL in jit::InvokeFunction. (r=Waldo) (33b1f88506)
- Bug 1212533 - Change the out-param of js/JS::Construct from MutableHandleValue to MutableHandleObject. r=arai, r=jorendorff (7910b24126)
- Bug 1233857 - Teach the JIT how to put individual elements' edges in the store buffer; r=jandem (7d0b35b028)
- Bug 1236548 - Followup: Make added test actually pass. (rs=Waldo) (0f1e6861b1)
- Bug 1216166 - Unify CustomEvent.cpp with the rest of the unified sources, r=smaug (eacd905246)
- Bug 1233831 - Part 0: JSAPI plumbing for nursery collection callbacks; r=terrence (1417ff456a)
- Bug 1233831 - Part 1: Call the callback on nursery collections; r=terrence (cf720fc8c9)
- Bug 1233831 - Part 2: Expose JS::gcreason::ExplainReason to embedders; r=terrence (5eaceb2a58)
- Bug 1233831 - Part 3: Install a callback to trace nursery collections; r=mccr8 (13bc8d19ff)
- Bug 1233831 - Part 4: Add metadata for the minor GC markers; r=vporof (cb42e1e42a)
- Bug 1233831 - Part 5: Test the minor GC markers; r=vporof (5f8ae59242)
- Bug 1218468 - Remove worker markers on each check in browser_timelineMarkers-02.js, r=me (33efc054c0)
- Bug 1233831 - Follow up: Expand sanitizeMarkers to filter out minor GC markers; r=jsantell (b99fb1dbbc)
- Bug 1239314 - Make marking validation a normal zeal mode; r=jonco (4668054d11)
- Bug 1233857 - Follow up: Add a new GC zeal mode for the elements edges barrier; r=terrence (61a7db4357)
- Bug 1245767 - Allow combining different gczeal modes. r=terrence (8d90fd103e)
- Bug 1242262 - Remove the last vestiges of JS_GC_MARKING_VALIDATION (it's a GC Zeal mode now). r=terrence (3ca086aed4)
- Bug 1249795 - Clear strong refs to message managers once we've destroyed nsFrameLoader, r=billm (b562395eb9)
- Bug 1244405 - Baldr: add memory segments (r=sunfish) (c7850ee495)
- Bug 1246433 - BaldrMonkey: Implement integer and float literals. r=luke (d668caa1bd)
- Bug 1240583 - Odin: refactor x86/x64 loads/stores (r=sunfish) (df85bc0aa3)
- Bug 1240583 - Odin: replace retargetWithOffset (r=bbouvier) (627815cb6d)
- Bug 757969 - use __thread in ThreadLocal; r=froydnj (5e06f7ef9e)
- bug 1244128 - stop exporting js friend api symbols from libxul r=glandium (894792316d)
- Bug 912337 - Make a new Error subclass: Debugger.DebuggeeWouldRun. (r=jimb) (a020dfd40c)
- Bug 1248101 - Make dtoa_malloc infallible. r=sfink. (0828c1b15b)
- Bug 1245737 (part 1) - Enable |Omit_Private_Memory| for our dtoa implementation. r=bhackett. (aaa0ebad66)
- Bug 1245737 (part 0) - Fix a leak in DtoaState. r=sfink. (3007242e86)
- Bug 1245737 (part 2) - Don't bother measuring DtoaState. r=bhackett. (db33501ddd)
- Bug 1243888 - Derive RootKind automatically from TraceKind; r=sfink (08421e1c6c)
- Bug 1244358 - Support all types in PersistentRooted; r=sfink (bc1a8ada00)
- Bug 1248233. Make js::ReportOutOfMemory respect the autoJSAPIOwnsErrorReporting() flag on the context options. r=luke (5ee05b61e6)
- Bug 1235151 - Factor all the install.rdfs used for langpacks. r=mshal (0e10679502)
- Bug 1235151 - Use #include in toolkit/locales/generic/install.rdf instead of -I on the preprocessor command line. r=mshal (31fdfea3de)
- Bug 1227388 - Finish removing dehydra support. r=mshal (aa43dfce95)
- Bug 1205814 - mozilla eslint plug: allow top level var only r=pbrosset (f247252d4b)
- Bug 1218409 - Eslint rule that checks for balanced listeners. r=miker (45d42a1014)
- Bug 1205814 - Missed a brief description in rst docs r=pbrosset (13fb24c5c4)
- Bug 1218425 - ESLint rule that warns against aArg notation in function params; r=miker (e811f2c8f7)
- Bug 1217851 - Fix mozplugin's import-headjs-globals on Windows. r=pbro (685f450fcb)
- Bug 1218412 - Create ESLint rule to check for CPOWS in browser mochitests r=pbrosset (c505ab4ed3)
- Bug 1217922 - eslint head.js plugin does not seem to work r=pbrosset (8d3782057e)
- Bug 1224735 - don't use "for..of" in eslint plugin. r=miker (90ecebec24)
- Bug 1225289 - Make eslint plugin code conform to .eslintrc r=pbrosset (007bf4c6b8)
- Bug 1222232 - Help mach eslint find espree and escope r=pbrosset (d0f7c5987b)
- Bug 1229858: Add a preprocessor to convert XBL into JavaScript blocks for eslint. r=miker (99bd8d62c4)
- Bug 1230300: Fix mach eslint to pass command arguments through to eslint. r=gps (7b54b30a8f)
- Bug 1231720 - Log when ESLint finishes running. r=nalexander (12705e260d)
- Bug 1234164 - Fix Ion GETNAME stubs to check for uninitialized lexicals. (r=efaust) (561d6e2d1c)
- Bug 1239519 - Fix debug-only initializion order issue detected by ASan. r=luke (8aa8b63fb6)
- Bug 1242840 - Drop profiler invalidation event if we hit OOM and make invalidation infallible r=jandem (8dfe4d99a5)
- Bug 1232386 - Fix NativeObject::growSlotsStatic to recover from OOM. r=terrence (284f47d6b1)
- Bug 1235032 - check value of obj, remake while () {} to do { } while(). r=jorendorff (0cb646a76e)
- Bug 1228327 - Fix bogus assert in GenerateDenseElementHole, add some comments. r=bhackett (e7fc0db6a0)
- Bug 1232859 - Don't crash or abort compilation if we're unable to compile the regex stubs. r=nbp (0ec48d44b5)
- Bug 1237566 - Bake in global this-value in IonBuilder::jsop_functionthis in some cases. r=h4writer (bd1fb0e8d8)
- Bug 1000780 - Part 7: Fix perf regressions introduced in part 1. r=jandem (629452aa16)
- Bug 1240100: IonMonkey: Magic values cannot flow through an instruction, r=jandem (f644caf377)
- Bug 1244098 - Attempt to fold JSOP_IN to false in IonBuilder based on TI. r=bhackett (e964e1d2bf)
- Bug 1238592 - IonMonkey: Optimize away any OSR fixup blocks that are ultimately unreachable. r=nbp (3e7ad0d61a)
- Bug 1245171 - Ensure enough ballast space in js::jit::AddKeepAliveInstructions. r=jandem (ba48c30163)
- Bug 1244828 - Ensure enough ballast space in TypeAnalyzer::adjustPhiInputs. r=h4writer (126c899284)
- Bug 1244828 - Ensure enough ballast space in TypeAnalyzer::adjustInputs. r=h4writer (0bf3d584b9)
- Bug 1112537 - Optimize String#split('foo').join('bar') pattern. r=nbp (75fb0e4124)
2466 lines
61 KiB
C++
2466 lines
61 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 "XMLHttpRequest.h"
|
|
|
|
#include "nsIDOMEvent.h"
|
|
#include "nsIDOMEventListener.h"
|
|
#include "nsIRunnable.h"
|
|
#include "nsIXMLHttpRequest.h"
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "jsfriendapi.h"
|
|
#include "mozilla/ArrayUtils.h"
|
|
#include "mozilla/dom/Exceptions.h"
|
|
#include "mozilla/dom/File.h"
|
|
#include "mozilla/dom/ProgressEvent.h"
|
|
#include "mozilla/dom/StructuredCloneHolder.h"
|
|
#include "nsComponentManagerUtils.h"
|
|
#include "nsContentUtils.h"
|
|
#include "nsFormData.h"
|
|
#include "nsJSUtils.h"
|
|
#include "nsThreadUtils.h"
|
|
#include "nsVariant.h"
|
|
|
|
#include "RuntimeService.h"
|
|
#include "WorkerPrivate.h"
|
|
#include "WorkerRunnable.h"
|
|
#include "XMLHttpRequestUpload.h"
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
/**
|
|
* XMLHttpRequest in workers
|
|
*
|
|
* XHR in workers is implemented by proxying calls/events/etc between the
|
|
* worker thread and an nsXMLHttpRequest on the main thread. The glue
|
|
* object here is the Proxy, which lives on both threads. All other objects
|
|
* live on either the main thread (the nsXMLHttpRequest) or the worker thread
|
|
* (the worker and XHR private objects).
|
|
*
|
|
* The main thread XHR is always operated in async mode, even for sync XHR
|
|
* in workers. Calls made on the worker thread are proxied to the main thread
|
|
* synchronously (meaning the worker thread is blocked until the call
|
|
* returns). Each proxied call spins up a sync queue, which captures any
|
|
* synchronously dispatched events and ensures that they run synchronously
|
|
* on the worker as well. Asynchronously dispatched events are posted to the
|
|
* worker thread to run asynchronously. Some of the XHR state is mirrored on
|
|
* the worker thread to avoid needing a cross-thread call on every property
|
|
* access.
|
|
*
|
|
* The XHR private is stored in the private slot of the XHR JSObject on the
|
|
* worker thread. It is destroyed when that JSObject is GCd. The private
|
|
* roots its JSObject while network activity is in progress. It also
|
|
* adds itself as a feature to the worker to give itself a chance to clean up
|
|
* if the worker goes away during an XHR call. It is important that the
|
|
* rooting and feature registration (collectively called pinning) happens at
|
|
* the proper times. If we pin for too long we can cause memory leaks or even
|
|
* shutdown hangs. If we don't pin for long enough we introduce a GC hazard.
|
|
*
|
|
* The XHR is pinned from the time Send is called to roughly the time loadend
|
|
* is received. There are some complications involved with Abort and XHR
|
|
* reuse. We maintain a counter on the main thread of how many times Send was
|
|
* called on this XHR, and we decrement the counter every time we receive a
|
|
* loadend event. When the counter reaches zero we dispatch a runnable to the
|
|
* worker thread to unpin the XHR. We only decrement the counter if the
|
|
* dispatch was successful, because the worker may no longer be accepting
|
|
* regular runnables. In the event that we reach Proxy::Teardown and there
|
|
* the outstanding Send count is still non-zero, we dispatch a control
|
|
* runnable which is guaranteed to run.
|
|
*
|
|
* NB: Some of this could probably be simplified now that we have the
|
|
* inner/outer channel ids.
|
|
*/
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
class Proxy final : public nsIDOMEventListener
|
|
{
|
|
public:
|
|
// Read on multiple threads.
|
|
WorkerPrivate* mWorkerPrivate;
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
|
|
|
// XHR Params:
|
|
bool mMozAnon;
|
|
bool mMozSystem;
|
|
|
|
// Only touched on the main thread.
|
|
RefPtr<nsXMLHttpRequest> mXHR;
|
|
nsCOMPtr<nsIXMLHttpRequestUpload> mXHRUpload;
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
|
nsCOMPtr<nsIEventTarget> mSyncEventResponseTarget;
|
|
uint32_t mInnerEventStreamId;
|
|
uint32_t mInnerChannelId;
|
|
uint32_t mOutstandingSendCount;
|
|
|
|
// Only touched on the worker thread.
|
|
uint32_t mOuterEventStreamId;
|
|
uint32_t mOuterChannelId;
|
|
uint32_t mOpenCount;
|
|
uint64_t mLastLoaded;
|
|
uint64_t mLastTotal;
|
|
uint64_t mLastUploadLoaded;
|
|
uint64_t mLastUploadTotal;
|
|
bool mIsSyncXHR;
|
|
bool mLastLengthComputable;
|
|
bool mLastUploadLengthComputable;
|
|
bool mSeenLoadStart;
|
|
bool mSeenUploadLoadStart;
|
|
|
|
// Only touched on the main thread.
|
|
bool mUploadEventListenersAttached;
|
|
bool mMainThreadSeenLoadStart;
|
|
bool mInOpen;
|
|
bool mArrayBufferResponseWasTransferred;
|
|
|
|
public:
|
|
Proxy(XMLHttpRequest* aXHRPrivate, bool aMozAnon, bool aMozSystem)
|
|
: mWorkerPrivate(nullptr), mXMLHttpRequestPrivate(aXHRPrivate),
|
|
mMozAnon(aMozAnon), mMozSystem(aMozSystem),
|
|
mInnerEventStreamId(0), mInnerChannelId(0), mOutstandingSendCount(0),
|
|
mOuterEventStreamId(0), mOuterChannelId(0), mOpenCount(0), mLastLoaded(0),
|
|
mLastTotal(0), mLastUploadLoaded(0), mLastUploadTotal(0), mIsSyncXHR(false),
|
|
mLastLengthComputable(false), mLastUploadLengthComputable(false),
|
|
mSeenLoadStart(false), mSeenUploadLoadStart(false),
|
|
mUploadEventListenersAttached(false), mMainThreadSeenLoadStart(false),
|
|
mInOpen(false), mArrayBufferResponseWasTransferred(false)
|
|
{ }
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
NS_DECL_NSIDOMEVENTLISTENER
|
|
|
|
bool
|
|
Init();
|
|
|
|
void
|
|
Teardown(bool aSendUnpin);
|
|
|
|
bool
|
|
AddRemoveEventListeners(bool aUpload, bool aAdd);
|
|
|
|
void
|
|
Reset()
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
if (mUploadEventListenersAttached) {
|
|
AddRemoveEventListeners(true, false);
|
|
}
|
|
}
|
|
|
|
already_AddRefed<nsIEventTarget>
|
|
GetEventTarget()
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
nsCOMPtr<nsIEventTarget> target = mSyncEventResponseTarget ?
|
|
mSyncEventResponseTarget :
|
|
mSyncLoopTarget;
|
|
return target.forget();
|
|
}
|
|
|
|
private:
|
|
~Proxy()
|
|
{
|
|
MOZ_ASSERT(!mXHR);
|
|
MOZ_ASSERT(!mXHRUpload);
|
|
MOZ_ASSERT(!mOutstandingSendCount);
|
|
}
|
|
};
|
|
|
|
class WorkerThreadProxySyncRunnable : public nsRunnable
|
|
{
|
|
protected:
|
|
WorkerPrivate* mWorkerPrivate;
|
|
RefPtr<Proxy> mProxy;
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
|
|
|
private:
|
|
class ResponseRunnable final: public MainThreadStopSyncLoopRunnable
|
|
{
|
|
RefPtr<Proxy> mProxy;
|
|
nsresult mErrorCode;
|
|
|
|
public:
|
|
ResponseRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
nsresult aErrorCode)
|
|
: MainThreadStopSyncLoopRunnable(aWorkerPrivate, aProxy->GetEventTarget(),
|
|
NS_SUCCEEDED(aErrorCode)),
|
|
mProxy(aProxy), mErrorCode(aErrorCode)
|
|
{
|
|
MOZ_ASSERT(aProxy);
|
|
}
|
|
|
|
private:
|
|
~ResponseRunnable()
|
|
{ }
|
|
|
|
virtual void
|
|
MaybeSetException(JSContext* aCx) override
|
|
{
|
|
MOZ_ASSERT(NS_FAILED(mErrorCode));
|
|
|
|
Throw(aCx, mErrorCode);
|
|
}
|
|
};
|
|
|
|
public:
|
|
WorkerThreadProxySyncRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy)
|
|
: mWorkerPrivate(aWorkerPrivate), mProxy(aProxy)
|
|
{
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
MOZ_ASSERT(aProxy);
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
}
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
bool
|
|
Dispatch(JSContext* aCx)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
AutoSyncLoopHolder syncLoop(mWorkerPrivate);
|
|
mSyncLoopTarget = syncLoop.EventTarget();
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(this))) {
|
|
JS_ReportError(aCx, "Failed to dispatch to main thread!");
|
|
return false;
|
|
}
|
|
|
|
return syncLoop.Run();
|
|
}
|
|
|
|
protected:
|
|
virtual ~WorkerThreadProxySyncRunnable()
|
|
{ }
|
|
|
|
virtual nsresult
|
|
MainThreadRun() = 0;
|
|
|
|
private:
|
|
NS_DECL_NSIRUNNABLE
|
|
};
|
|
|
|
class SendRunnable final
|
|
: public WorkerThreadProxySyncRunnable
|
|
, public StructuredCloneHolder
|
|
{
|
|
nsString mStringBody;
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
|
bool mHasUploadListeners;
|
|
|
|
public:
|
|
SendRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
const nsAString& aStringBody)
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy)
|
|
, StructuredCloneHolder(CloningSupported, TransferringNotSupported,
|
|
SameProcessDifferentThread)
|
|
, mStringBody(aStringBody)
|
|
, mHasUploadListeners(false)
|
|
{
|
|
}
|
|
|
|
void SetHaveUploadListeners(bool aHasUploadListeners)
|
|
{
|
|
mHasUploadListeners = aHasUploadListeners;
|
|
}
|
|
|
|
void SetSyncLoopTarget(nsIEventTarget* aSyncLoopTarget)
|
|
{
|
|
mSyncLoopTarget = aSyncLoopTarget;
|
|
}
|
|
|
|
private:
|
|
~SendRunnable()
|
|
{ }
|
|
|
|
virtual nsresult
|
|
MainThreadRun() override;
|
|
};
|
|
|
|
END_WORKERS_NAMESPACE
|
|
|
|
namespace {
|
|
|
|
inline void
|
|
ConvertResponseTypeToString(XMLHttpRequestResponseType aType,
|
|
nsString& aString)
|
|
{
|
|
using namespace
|
|
mozilla::dom::XMLHttpRequestResponseTypeValues;
|
|
|
|
size_t index = static_cast<size_t>(aType);
|
|
MOZ_ASSERT(index < ArrayLength(strings), "Codegen gave us a bad value!");
|
|
|
|
aString.AssignASCII(strings[index].value, strings[index].length);
|
|
}
|
|
|
|
inline XMLHttpRequestResponseType
|
|
ConvertStringToResponseType(const nsAString& aString)
|
|
{
|
|
using namespace
|
|
mozilla::dom::XMLHttpRequestResponseTypeValues;
|
|
|
|
for (size_t index = 0; index < ArrayLength(strings) - 1; index++) {
|
|
if (aString.EqualsASCII(strings[index].value, strings[index].length)) {
|
|
return static_cast<XMLHttpRequestResponseType>(index);
|
|
}
|
|
}
|
|
|
|
MOZ_CRASH("Don't know anything about this response type!");
|
|
}
|
|
|
|
enum
|
|
{
|
|
STRING_abort = 0,
|
|
STRING_error,
|
|
STRING_load,
|
|
STRING_loadstart,
|
|
STRING_progress,
|
|
STRING_timeout,
|
|
STRING_readystatechange,
|
|
STRING_loadend,
|
|
|
|
STRING_COUNT,
|
|
|
|
STRING_LAST_XHR = STRING_loadend,
|
|
STRING_LAST_EVENTTARGET = STRING_timeout
|
|
};
|
|
|
|
static_assert(STRING_LAST_XHR >= STRING_LAST_EVENTTARGET, "Bad string setup!");
|
|
static_assert(STRING_LAST_XHR == STRING_COUNT - 1, "Bad string setup!");
|
|
|
|
const char* const sEventStrings[] = {
|
|
// nsIXMLHttpRequestEventTarget event types, supported by both XHR and Upload.
|
|
"abort",
|
|
"error",
|
|
"load",
|
|
"loadstart",
|
|
"progress",
|
|
"timeout",
|
|
|
|
// nsIXMLHttpRequest event types, supported only by XHR.
|
|
"readystatechange",
|
|
"loadend",
|
|
};
|
|
|
|
static_assert(MOZ_ARRAY_LENGTH(sEventStrings) == STRING_COUNT,
|
|
"Bad string count!");
|
|
|
|
class MainThreadProxyRunnable : public MainThreadWorkerSyncRunnable
|
|
{
|
|
protected:
|
|
RefPtr<Proxy> mProxy;
|
|
|
|
MainThreadProxyRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy)
|
|
: MainThreadWorkerSyncRunnable(aWorkerPrivate, aProxy->GetEventTarget()),
|
|
mProxy(aProxy)
|
|
{
|
|
MOZ_ASSERT(aProxy);
|
|
}
|
|
|
|
virtual ~MainThreadProxyRunnable()
|
|
{ }
|
|
};
|
|
|
|
class XHRUnpinRunnable final : public MainThreadWorkerControlRunnable
|
|
{
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
|
|
|
public:
|
|
XHRUnpinRunnable(WorkerPrivate* aWorkerPrivate,
|
|
XMLHttpRequest* aXHRPrivate)
|
|
: MainThreadWorkerControlRunnable(aWorkerPrivate),
|
|
mXMLHttpRequestPrivate(aXHRPrivate)
|
|
{
|
|
MOZ_ASSERT(aXHRPrivate);
|
|
}
|
|
|
|
private:
|
|
~XHRUnpinRunnable()
|
|
{ }
|
|
|
|
bool
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
{
|
|
if (mXMLHttpRequestPrivate->SendInProgress()) {
|
|
mXMLHttpRequestPrivate->Unpin();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
};
|
|
|
|
class AsyncTeardownRunnable final : public nsRunnable
|
|
{
|
|
RefPtr<Proxy> mProxy;
|
|
|
|
public:
|
|
explicit AsyncTeardownRunnable(Proxy* aProxy)
|
|
: mProxy(aProxy)
|
|
{
|
|
MOZ_ASSERT(aProxy);
|
|
}
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
private:
|
|
~AsyncTeardownRunnable()
|
|
{ }
|
|
|
|
NS_IMETHOD
|
|
Run() override
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
// This means the XHR was GC'd, so we can't be pinned, and we don't need to
|
|
// try to unpin.
|
|
mProxy->Teardown(/* aSendUnpin */ false);
|
|
mProxy = nullptr;
|
|
|
|
return NS_OK;
|
|
}
|
|
};
|
|
|
|
class LoadStartDetectionRunnable final : public nsRunnable,
|
|
public nsIDOMEventListener
|
|
{
|
|
WorkerPrivate* mWorkerPrivate;
|
|
RefPtr<Proxy> mProxy;
|
|
RefPtr<nsXMLHttpRequest> mXHR;
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
|
nsString mEventType;
|
|
uint32_t mChannelId;
|
|
bool mReceivedLoadStart;
|
|
|
|
class ProxyCompleteRunnable final : public MainThreadProxyRunnable
|
|
{
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
|
uint32_t mChannelId;
|
|
|
|
public:
|
|
ProxyCompleteRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
XMLHttpRequest* aXHRPrivate, uint32_t aChannelId)
|
|
: MainThreadProxyRunnable(aWorkerPrivate, aProxy),
|
|
mXMLHttpRequestPrivate(aXHRPrivate), mChannelId(aChannelId)
|
|
{ }
|
|
|
|
private:
|
|
~ProxyCompleteRunnable()
|
|
{ }
|
|
|
|
virtual bool
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
{
|
|
if (mChannelId != mProxy->mOuterChannelId) {
|
|
// Threads raced, this event is now obsolete.
|
|
return true;
|
|
}
|
|
|
|
if (mSyncLoopTarget) {
|
|
aWorkerPrivate->StopSyncLoop(mSyncLoopTarget, true);
|
|
}
|
|
|
|
if (mXMLHttpRequestPrivate->SendInProgress()) {
|
|
mXMLHttpRequestPrivate->Unpin();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
NS_IMETHOD
|
|
Cancel() override
|
|
{
|
|
// This must run!
|
|
nsresult rv = MainThreadProxyRunnable::Cancel();
|
|
nsresult rv2 = Run();
|
|
return NS_FAILED(rv) ? rv : rv2;
|
|
}
|
|
};
|
|
|
|
public:
|
|
LoadStartDetectionRunnable(Proxy* aProxy, XMLHttpRequest* aXHRPrivate)
|
|
: mWorkerPrivate(aProxy->mWorkerPrivate), mProxy(aProxy), mXHR(aProxy->mXHR),
|
|
mXMLHttpRequestPrivate(aXHRPrivate), mChannelId(mProxy->mInnerChannelId),
|
|
mReceivedLoadStart(false)
|
|
{
|
|
AssertIsOnMainThread();
|
|
mEventType.AssignWithConversion(sEventStrings[STRING_loadstart]);
|
|
}
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
NS_DECL_NSIRUNNABLE
|
|
NS_DECL_NSIDOMEVENTLISTENER
|
|
|
|
bool
|
|
RegisterAndDispatch()
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
if (NS_FAILED(mXHR->AddEventListener(mEventType, this, false, false, 2))) {
|
|
NS_WARNING("Failed to add event listener!");
|
|
return false;
|
|
}
|
|
|
|
return NS_SUCCEEDED(NS_DispatchToCurrentThread(this));
|
|
}
|
|
|
|
private:
|
|
~LoadStartDetectionRunnable()
|
|
{
|
|
AssertIsOnMainThread();
|
|
}
|
|
};
|
|
|
|
class EventRunnable final : public MainThreadProxyRunnable
|
|
, public StructuredCloneHolder
|
|
{
|
|
nsString mType;
|
|
nsString mResponseType;
|
|
JS::Heap<JS::Value> mResponse;
|
|
nsString mResponseText;
|
|
nsString mResponseURL;
|
|
nsCString mStatusText;
|
|
uint64_t mLoaded;
|
|
uint64_t mTotal;
|
|
uint32_t mEventStreamId;
|
|
uint32_t mStatus;
|
|
uint16_t mReadyState;
|
|
bool mUploadEvent;
|
|
bool mProgressEvent;
|
|
bool mLengthComputable;
|
|
bool mUseCachedArrayBufferResponse;
|
|
nsresult mResponseTextResult;
|
|
nsresult mStatusResult;
|
|
nsresult mResponseResult;
|
|
|
|
public:
|
|
class MOZ_RAII StateDataAutoRooter : private JS::CustomAutoRooter
|
|
{
|
|
XMLHttpRequest::StateData* mStateData;
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
|
|
|
public:
|
|
explicit StateDataAutoRooter(JSContext* aCx, XMLHttpRequest::StateData* aData
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
: CustomAutoRooter(aCx), mStateData(aData)
|
|
{
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
}
|
|
|
|
private:
|
|
virtual void trace(JSTracer* aTrc)
|
|
{
|
|
JS::TraceEdge(aTrc, &mStateData->mResponse,
|
|
"XMLHttpRequest::StateData::mResponse");
|
|
}
|
|
};
|
|
|
|
EventRunnable(Proxy* aProxy, bool aUploadEvent, const nsString& aType,
|
|
bool aLengthComputable, uint64_t aLoaded, uint64_t aTotal)
|
|
: MainThreadProxyRunnable(aProxy->mWorkerPrivate, aProxy),
|
|
StructuredCloneHolder(CloningSupported, TransferringNotSupported,
|
|
SameProcessDifferentThread),
|
|
mType(aType), mResponse(JS::UndefinedValue()), mLoaded(aLoaded),
|
|
mTotal(aTotal), mEventStreamId(aProxy->mInnerEventStreamId), mStatus(0),
|
|
mReadyState(0), mUploadEvent(aUploadEvent), mProgressEvent(true),
|
|
mLengthComputable(aLengthComputable), mUseCachedArrayBufferResponse(false),
|
|
mResponseTextResult(NS_OK), mStatusResult(NS_OK), mResponseResult(NS_OK)
|
|
{ }
|
|
|
|
EventRunnable(Proxy* aProxy, bool aUploadEvent, const nsString& aType)
|
|
: MainThreadProxyRunnable(aProxy->mWorkerPrivate, aProxy),
|
|
StructuredCloneHolder(CloningSupported, TransferringNotSupported,
|
|
SameProcessDifferentThread),
|
|
mType(aType), mResponse(JS::UndefinedValue()), mLoaded(0), mTotal(0),
|
|
mEventStreamId(aProxy->mInnerEventStreamId), mStatus(0), mReadyState(0),
|
|
mUploadEvent(aUploadEvent), mProgressEvent(false), mLengthComputable(0),
|
|
mUseCachedArrayBufferResponse(false), mResponseTextResult(NS_OK),
|
|
mStatusResult(NS_OK), mResponseResult(NS_OK)
|
|
{ }
|
|
|
|
private:
|
|
~EventRunnable()
|
|
{ }
|
|
|
|
virtual bool
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override;
|
|
|
|
virtual bool
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override;
|
|
};
|
|
|
|
class SyncTeardownRunnable final : public WorkerThreadProxySyncRunnable
|
|
{
|
|
public:
|
|
SyncTeardownRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy)
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy)
|
|
{ }
|
|
|
|
private:
|
|
~SyncTeardownRunnable()
|
|
{ }
|
|
|
|
virtual nsresult
|
|
MainThreadRun() override
|
|
{
|
|
mProxy->Teardown(/* aSendUnpin */ true);
|
|
MOZ_ASSERT(!mProxy->mSyncLoopTarget);
|
|
return NS_OK;
|
|
}
|
|
};
|
|
|
|
class SetBackgroundRequestRunnable final :
|
|
public WorkerThreadProxySyncRunnable
|
|
{
|
|
bool mValue;
|
|
|
|
public:
|
|
SetBackgroundRequestRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
bool aValue)
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mValue(aValue)
|
|
{ }
|
|
|
|
private:
|
|
~SetBackgroundRequestRunnable()
|
|
{ }
|
|
|
|
virtual nsresult
|
|
MainThreadRun() override
|
|
{
|
|
return mProxy->mXHR->SetMozBackgroundRequest(mValue);
|
|
}
|
|
};
|
|
|
|
class SetWithCredentialsRunnable final :
|
|
public WorkerThreadProxySyncRunnable
|
|
{
|
|
bool mValue;
|
|
|
|
public:
|
|
SetWithCredentialsRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
bool aValue)
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mValue(aValue)
|
|
{ }
|
|
|
|
private:
|
|
~SetWithCredentialsRunnable()
|
|
{ }
|
|
|
|
virtual nsresult
|
|
MainThreadRun() override
|
|
{
|
|
return mProxy->mXHR->SetWithCredentials(mValue);
|
|
}
|
|
};
|
|
|
|
class SetResponseTypeRunnable final : public WorkerThreadProxySyncRunnable
|
|
{
|
|
nsString mResponseType;
|
|
|
|
public:
|
|
SetResponseTypeRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
const nsAString& aResponseType)
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy),
|
|
mResponseType(aResponseType)
|
|
{ }
|
|
|
|
void
|
|
GetResponseType(nsAString& aResponseType)
|
|
{
|
|
aResponseType.Assign(mResponseType);
|
|
}
|
|
|
|
private:
|
|
~SetResponseTypeRunnable()
|
|
{ }
|
|
|
|
virtual nsresult
|
|
MainThreadRun() override
|
|
{
|
|
nsresult rv = mProxy->mXHR->SetResponseType(mResponseType);
|
|
mResponseType.Truncate();
|
|
if (NS_SUCCEEDED(rv)) {
|
|
rv = mProxy->mXHR->GetResponseType(mResponseType);
|
|
}
|
|
return rv;
|
|
}
|
|
};
|
|
|
|
class SetTimeoutRunnable final : public WorkerThreadProxySyncRunnable
|
|
{
|
|
uint32_t mTimeout;
|
|
|
|
public:
|
|
SetTimeoutRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
uint32_t aTimeout)
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mTimeout(aTimeout)
|
|
{ }
|
|
|
|
private:
|
|
~SetTimeoutRunnable()
|
|
{ }
|
|
|
|
virtual nsresult
|
|
MainThreadRun() override
|
|
{
|
|
return mProxy->mXHR->SetTimeout(mTimeout);
|
|
}
|
|
};
|
|
|
|
class AbortRunnable final : public WorkerThreadProxySyncRunnable
|
|
{
|
|
public:
|
|
AbortRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy)
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy)
|
|
{ }
|
|
|
|
private:
|
|
~AbortRunnable()
|
|
{ }
|
|
|
|
virtual nsresult
|
|
MainThreadRun() override;
|
|
};
|
|
|
|
class GetAllResponseHeadersRunnable final :
|
|
public WorkerThreadProxySyncRunnable
|
|
{
|
|
nsCString& mResponseHeaders;
|
|
|
|
public:
|
|
GetAllResponseHeadersRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
nsCString& aResponseHeaders)
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy),
|
|
mResponseHeaders(aResponseHeaders)
|
|
{ }
|
|
|
|
private:
|
|
~GetAllResponseHeadersRunnable()
|
|
{ }
|
|
|
|
virtual nsresult
|
|
MainThreadRun() override
|
|
{
|
|
mProxy->mXHR->GetAllResponseHeaders(mResponseHeaders);
|
|
return NS_OK;
|
|
}
|
|
};
|
|
|
|
class GetResponseHeaderRunnable final : public WorkerThreadProxySyncRunnable
|
|
{
|
|
const nsCString mHeader;
|
|
nsCString& mValue;
|
|
|
|
public:
|
|
GetResponseHeaderRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
const nsACString& aHeader, nsCString& aValue)
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mHeader(aHeader),
|
|
mValue(aValue)
|
|
{ }
|
|
|
|
private:
|
|
~GetResponseHeaderRunnable()
|
|
{ }
|
|
|
|
virtual nsresult
|
|
MainThreadRun() override
|
|
{
|
|
return mProxy->mXHR->GetResponseHeader(mHeader, mValue);
|
|
}
|
|
};
|
|
|
|
class OpenRunnable final : public WorkerThreadProxySyncRunnable
|
|
{
|
|
nsCString mMethod;
|
|
nsString mURL;
|
|
Optional<nsAString> mUser;
|
|
nsString mUserStr;
|
|
Optional<nsAString> mPassword;
|
|
nsString mPasswordStr;
|
|
bool mBackgroundRequest;
|
|
bool mWithCredentials;
|
|
uint32_t mTimeout;
|
|
|
|
public:
|
|
OpenRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
const nsACString& aMethod, const nsAString& aURL,
|
|
const Optional<nsAString>& aUser,
|
|
const Optional<nsAString>& aPassword,
|
|
bool aBackgroundRequest, bool aWithCredentials,
|
|
uint32_t aTimeout)
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mMethod(aMethod),
|
|
mURL(aURL), mBackgroundRequest(aBackgroundRequest),
|
|
mWithCredentials(aWithCredentials), mTimeout(aTimeout)
|
|
{
|
|
if (aUser.WasPassed()) {
|
|
mUserStr = aUser.Value();
|
|
mUser = &mUserStr;
|
|
}
|
|
if (aPassword.WasPassed()) {
|
|
mPasswordStr = aPassword.Value();
|
|
mPassword = &mPasswordStr;
|
|
}
|
|
}
|
|
|
|
private:
|
|
~OpenRunnable()
|
|
{ }
|
|
|
|
virtual nsresult
|
|
MainThreadRun() override
|
|
{
|
|
WorkerPrivate* oldWorker = mProxy->mWorkerPrivate;
|
|
mProxy->mWorkerPrivate = mWorkerPrivate;
|
|
|
|
nsresult rv = MainThreadRunInternal();
|
|
|
|
mProxy->mWorkerPrivate = oldWorker;
|
|
return rv;
|
|
}
|
|
|
|
nsresult
|
|
MainThreadRunInternal();
|
|
};
|
|
|
|
class SetRequestHeaderRunnable final : public WorkerThreadProxySyncRunnable
|
|
{
|
|
nsCString mHeader;
|
|
nsCString mValue;
|
|
|
|
public:
|
|
SetRequestHeaderRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
const nsACString& aHeader, const nsACString& aValue)
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mHeader(aHeader),
|
|
mValue(aValue)
|
|
{ }
|
|
|
|
private:
|
|
~SetRequestHeaderRunnable()
|
|
{ }
|
|
|
|
virtual nsresult
|
|
MainThreadRun() override
|
|
{
|
|
return mProxy->mXHR->SetRequestHeader(mHeader, mValue);
|
|
}
|
|
};
|
|
|
|
class OverrideMimeTypeRunnable final : public WorkerThreadProxySyncRunnable
|
|
{
|
|
nsString mMimeType;
|
|
|
|
public:
|
|
OverrideMimeTypeRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
|
|
const nsAString& aMimeType)
|
|
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mMimeType(aMimeType)
|
|
{ }
|
|
|
|
private:
|
|
~OverrideMimeTypeRunnable()
|
|
{ }
|
|
|
|
virtual nsresult
|
|
MainThreadRun() override
|
|
{
|
|
mProxy->mXHR->OverrideMimeType(mMimeType);
|
|
return NS_OK;
|
|
}
|
|
};
|
|
|
|
class AutoUnpinXHR
|
|
{
|
|
XMLHttpRequest* mXMLHttpRequestPrivate;
|
|
|
|
public:
|
|
explicit AutoUnpinXHR(XMLHttpRequest* aXMLHttpRequestPrivate)
|
|
: mXMLHttpRequestPrivate(aXMLHttpRequestPrivate)
|
|
{
|
|
MOZ_ASSERT(aXMLHttpRequestPrivate);
|
|
}
|
|
|
|
~AutoUnpinXHR()
|
|
{
|
|
if (mXMLHttpRequestPrivate) {
|
|
mXMLHttpRequestPrivate->Unpin();
|
|
}
|
|
}
|
|
|
|
void Clear()
|
|
{
|
|
mXMLHttpRequestPrivate = nullptr;
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
bool
|
|
Proxy::Init()
|
|
{
|
|
AssertIsOnMainThread();
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
if (mXHR) {
|
|
return true;
|
|
}
|
|
|
|
nsPIDOMWindow* ownerWindow = mWorkerPrivate->GetWindow();
|
|
if (ownerWindow) {
|
|
ownerWindow = ownerWindow->GetOuterWindow();
|
|
if (!ownerWindow) {
|
|
NS_ERROR("No outer window?!");
|
|
return false;
|
|
}
|
|
|
|
nsPIDOMWindow* innerWindow = ownerWindow->GetCurrentInnerWindow();
|
|
if (mWorkerPrivate->GetWindow() != innerWindow) {
|
|
NS_WARNING("Window has navigated, cannot create XHR here.");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
mXHR = new nsXMLHttpRequest();
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(ownerWindow);
|
|
if (NS_FAILED(mXHR->Init(mWorkerPrivate->GetPrincipal(),
|
|
mWorkerPrivate->GetScriptContext(),
|
|
global, mWorkerPrivate->GetBaseURI(),
|
|
mWorkerPrivate->GetLoadGroup()))) {
|
|
mXHR = nullptr;
|
|
return false;
|
|
}
|
|
|
|
mXHR->SetParameters(mMozAnon, mMozSystem);
|
|
|
|
if (NS_FAILED(mXHR->GetUpload(getter_AddRefs(mXHRUpload)))) {
|
|
mXHR = nullptr;
|
|
return false;
|
|
}
|
|
|
|
if (!AddRemoveEventListeners(false, true)) {
|
|
mXHRUpload = nullptr;
|
|
mXHR = nullptr;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void
|
|
Proxy::Teardown(bool aSendUnpin)
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
if (mXHR) {
|
|
Reset();
|
|
|
|
// NB: We are intentionally dropping events coming from xhr.abort on the
|
|
// floor.
|
|
AddRemoveEventListeners(false, false);
|
|
mXHR->Abort();
|
|
|
|
if (mOutstandingSendCount) {
|
|
if (aSendUnpin) {
|
|
RefPtr<XHRUnpinRunnable> runnable =
|
|
new XHRUnpinRunnable(mWorkerPrivate, mXMLHttpRequestPrivate);
|
|
if (!runnable->Dispatch(nullptr)) {
|
|
NS_RUNTIMEABORT("We're going to hang at shutdown anyways.");
|
|
}
|
|
}
|
|
|
|
if (mSyncLoopTarget) {
|
|
// We have an unclosed sync loop. Fix that now.
|
|
RefPtr<MainThreadStopSyncLoopRunnable> runnable =
|
|
new MainThreadStopSyncLoopRunnable(mWorkerPrivate,
|
|
mSyncLoopTarget.forget(),
|
|
false);
|
|
if (!runnable->Dispatch(nullptr)) {
|
|
NS_RUNTIMEABORT("We're going to hang at shutdown anyways.");
|
|
}
|
|
}
|
|
|
|
mOutstandingSendCount = 0;
|
|
}
|
|
|
|
mWorkerPrivate = nullptr;
|
|
mXHRUpload = nullptr;
|
|
mXHR = nullptr;
|
|
}
|
|
|
|
MOZ_ASSERT(!mWorkerPrivate);
|
|
MOZ_ASSERT(!mSyncLoopTarget);
|
|
}
|
|
|
|
bool
|
|
Proxy::AddRemoveEventListeners(bool aUpload, bool aAdd)
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
NS_ASSERTION(!aUpload ||
|
|
(mUploadEventListenersAttached && !aAdd) ||
|
|
(!mUploadEventListenersAttached && aAdd),
|
|
"Messed up logic for upload listeners!");
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target =
|
|
aUpload ?
|
|
do_QueryInterface(mXHRUpload) :
|
|
do_QueryInterface(static_cast<nsIXMLHttpRequest*>(mXHR.get()));
|
|
NS_ASSERTION(target, "This should never fail!");
|
|
|
|
uint32_t lastEventType = aUpload ? STRING_LAST_EVENTTARGET : STRING_LAST_XHR;
|
|
|
|
nsAutoString eventType;
|
|
for (uint32_t index = 0; index <= lastEventType; index++) {
|
|
eventType = NS_ConvertASCIItoUTF16(sEventStrings[index]);
|
|
if (aAdd) {
|
|
if (NS_FAILED(target->AddEventListener(eventType, this, false))) {
|
|
return false;
|
|
}
|
|
}
|
|
else if (NS_FAILED(target->RemoveEventListener(eventType, this, false))) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (aUpload) {
|
|
mUploadEventListenersAttached = aAdd;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
NS_IMPL_ISUPPORTS(Proxy, nsIDOMEventListener)
|
|
|
|
NS_IMETHODIMP
|
|
Proxy::HandleEvent(nsIDOMEvent* aEvent)
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
if (!mWorkerPrivate || !mXMLHttpRequestPrivate) {
|
|
NS_ERROR("Shouldn't get here!");
|
|
return NS_OK;
|
|
}
|
|
|
|
nsString type;
|
|
if (NS_FAILED(aEvent->GetType(type))) {
|
|
NS_WARNING("Failed to get event type!");
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target;
|
|
if (NS_FAILED(aEvent->GetTarget(getter_AddRefs(target)))) {
|
|
NS_WARNING("Failed to get target!");
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
nsCOMPtr<nsIXMLHttpRequestUpload> uploadTarget = do_QueryInterface(target);
|
|
ProgressEvent* progressEvent = aEvent->InternalDOMEvent()->AsProgressEvent();
|
|
|
|
RefPtr<EventRunnable> runnable;
|
|
|
|
if (mInOpen && type.EqualsASCII(sEventStrings[STRING_readystatechange])) {
|
|
uint16_t readyState = 0;
|
|
if (NS_SUCCEEDED(mXHR->GetReadyState(&readyState)) &&
|
|
readyState == nsIXMLHttpRequest::OPENED) {
|
|
mInnerEventStreamId++;
|
|
}
|
|
}
|
|
|
|
if (progressEvent) {
|
|
runnable = new EventRunnable(this, !!uploadTarget, type,
|
|
progressEvent->LengthComputable(),
|
|
progressEvent->Loaded(),
|
|
progressEvent->Total());
|
|
}
|
|
else {
|
|
runnable = new EventRunnable(this, !!uploadTarget, type);
|
|
}
|
|
|
|
{
|
|
AutoSafeJSContext cx;
|
|
JSAutoRequest ar(cx);
|
|
|
|
JS::Rooted<JS::Value> value(cx);
|
|
if (!GetOrCreateDOMReflectorNoWrap(cx, mXHR, &value)) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
JS::Rooted<JSObject*> scope(cx, &value.toObject());
|
|
JSAutoCompartment ac(cx, scope);
|
|
|
|
runnable->Dispatch(cx);
|
|
}
|
|
|
|
if (!uploadTarget) {
|
|
if (type.EqualsASCII(sEventStrings[STRING_loadstart])) {
|
|
NS_ASSERTION(!mMainThreadSeenLoadStart, "Huh?!");
|
|
mMainThreadSeenLoadStart = true;
|
|
}
|
|
else if (mMainThreadSeenLoadStart &&
|
|
type.EqualsASCII(sEventStrings[STRING_loadend])) {
|
|
mMainThreadSeenLoadStart = false;
|
|
|
|
RefPtr<LoadStartDetectionRunnable> runnable =
|
|
new LoadStartDetectionRunnable(this, mXMLHttpRequestPrivate);
|
|
if (!runnable->RegisterAndDispatch()) {
|
|
NS_WARNING("Failed to dispatch LoadStartDetectionRunnable!");
|
|
}
|
|
}
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(WorkerThreadProxySyncRunnable, nsRunnable)
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(AsyncTeardownRunnable, nsRunnable)
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(LoadStartDetectionRunnable, nsRunnable,
|
|
nsIDOMEventListener)
|
|
|
|
NS_IMETHODIMP
|
|
LoadStartDetectionRunnable::Run()
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
if (NS_FAILED(mXHR->RemoveEventListener(mEventType, this, false))) {
|
|
NS_WARNING("Failed to remove event listener!");
|
|
}
|
|
|
|
if (!mReceivedLoadStart) {
|
|
if (mProxy->mOutstandingSendCount > 1) {
|
|
mProxy->mOutstandingSendCount--;
|
|
} else if (mProxy->mOutstandingSendCount == 1) {
|
|
mProxy->Reset();
|
|
|
|
RefPtr<ProxyCompleteRunnable> runnable =
|
|
new ProxyCompleteRunnable(mWorkerPrivate, mProxy,
|
|
mXMLHttpRequestPrivate, mChannelId);
|
|
if (runnable->Dispatch(nullptr)) {
|
|
mProxy->mWorkerPrivate = nullptr;
|
|
mProxy->mSyncLoopTarget = nullptr;
|
|
mProxy->mOutstandingSendCount--;
|
|
}
|
|
}
|
|
}
|
|
|
|
mProxy = nullptr;
|
|
mXHR = nullptr;
|
|
mXMLHttpRequestPrivate = nullptr;
|
|
return NS_OK;
|
|
}
|
|
|
|
NS_IMETHODIMP
|
|
LoadStartDetectionRunnable::HandleEvent(nsIDOMEvent* aEvent)
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
#ifdef DEBUG
|
|
{
|
|
nsString type;
|
|
if (NS_SUCCEEDED(aEvent->GetType(type))) {
|
|
MOZ_ASSERT(type == mEventType);
|
|
}
|
|
else {
|
|
NS_WARNING("Failed to get event type!");
|
|
}
|
|
}
|
|
#endif
|
|
|
|
mReceivedLoadStart = true;
|
|
return NS_OK;
|
|
}
|
|
|
|
bool
|
|
EventRunnable::PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
RefPtr<nsXMLHttpRequest>& xhr = mProxy->mXHR;
|
|
MOZ_ASSERT(xhr);
|
|
|
|
if (NS_FAILED(xhr->GetResponseType(mResponseType))) {
|
|
MOZ_ASSERT(false, "This should never fail!");
|
|
}
|
|
|
|
mResponseTextResult = xhr->GetResponseText(mResponseText);
|
|
if (NS_SUCCEEDED(mResponseTextResult)) {
|
|
mResponseResult = mResponseTextResult;
|
|
if (mResponseText.IsVoid()) {
|
|
mResponse.setNull();
|
|
}
|
|
}
|
|
else {
|
|
JS::Rooted<JS::Value> response(aCx);
|
|
mResponseResult = xhr->GetResponse(aCx, &response);
|
|
if (NS_SUCCEEDED(mResponseResult)) {
|
|
if (!response.isGCThing()) {
|
|
mResponse = response;
|
|
} else {
|
|
bool doClone = true;
|
|
JS::Rooted<JS::Value> transferable(aCx);
|
|
JS::Rooted<JSObject*> obj(aCx, response.isObjectOrNull() ?
|
|
response.toObjectOrNull() : nullptr);
|
|
if (obj && JS_IsArrayBufferObject(obj)) {
|
|
// Use cached response if the arraybuffer has been transfered.
|
|
if (mProxy->mArrayBufferResponseWasTransferred) {
|
|
MOZ_ASSERT(JS_IsDetachedArrayBufferObject(obj));
|
|
mUseCachedArrayBufferResponse = true;
|
|
doClone = false;
|
|
} else {
|
|
MOZ_ASSERT(!JS_IsDetachedArrayBufferObject(obj));
|
|
JS::AutoValueArray<1> argv(aCx);
|
|
argv[0].set(response);
|
|
obj = JS_NewArrayObject(aCx, argv);
|
|
if (obj) {
|
|
transferable.setObject(*obj);
|
|
// Only cache the response when the readyState is DONE.
|
|
if (xhr->ReadyState() == nsIXMLHttpRequest::DONE) {
|
|
mProxy->mArrayBufferResponseWasTransferred = true;
|
|
}
|
|
} else {
|
|
mResponseResult = NS_ERROR_OUT_OF_MEMORY;
|
|
doClone = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (doClone) {
|
|
ErrorResult rv;
|
|
Write(aCx, response, transferable, rv);
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
NS_WARNING("Failed to clone response!");
|
|
mResponseResult = rv.StealNSResult();
|
|
mProxy->mArrayBufferResponseWasTransferred = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
mStatusResult = xhr->GetStatus(&mStatus);
|
|
|
|
xhr->GetStatusText(mStatusText);
|
|
|
|
mReadyState = xhr->ReadyState();
|
|
|
|
xhr->GetResponseURL(mResponseURL);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
EventRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
{
|
|
if (mEventStreamId != mProxy->mOuterEventStreamId) {
|
|
// Threads raced, this event is now obsolete.
|
|
return true;
|
|
}
|
|
|
|
if (!mProxy->mXMLHttpRequestPrivate) {
|
|
// Object was finalized, bail.
|
|
return true;
|
|
}
|
|
|
|
if (mType.EqualsASCII(sEventStrings[STRING_loadstart])) {
|
|
if (mUploadEvent) {
|
|
mProxy->mSeenUploadLoadStart = true;
|
|
}
|
|
else {
|
|
mProxy->mSeenLoadStart = true;
|
|
}
|
|
}
|
|
else if (mType.EqualsASCII(sEventStrings[STRING_loadend])) {
|
|
if (mUploadEvent) {
|
|
mProxy->mSeenUploadLoadStart = false;
|
|
}
|
|
else {
|
|
if (!mProxy->mSeenLoadStart) {
|
|
// We've already dispatched premature abort events.
|
|
return true;
|
|
}
|
|
mProxy->mSeenLoadStart = false;
|
|
}
|
|
}
|
|
else if (mType.EqualsASCII(sEventStrings[STRING_abort])) {
|
|
if ((mUploadEvent && !mProxy->mSeenUploadLoadStart) ||
|
|
(!mUploadEvent && !mProxy->mSeenLoadStart)) {
|
|
// We've already dispatched premature abort events.
|
|
return true;
|
|
}
|
|
}
|
|
else if (mType.EqualsASCII(sEventStrings[STRING_readystatechange])) {
|
|
if (mReadyState == 4 && !mUploadEvent && !mProxy->mSeenLoadStart) {
|
|
// We've already dispatched premature abort events.
|
|
return true;
|
|
}
|
|
}
|
|
|
|
if (mProgressEvent) {
|
|
// Cache these for premature abort events.
|
|
if (mUploadEvent) {
|
|
mProxy->mLastUploadLengthComputable = mLengthComputable;
|
|
mProxy->mLastUploadLoaded = mLoaded;
|
|
mProxy->mLastUploadTotal = mTotal;
|
|
}
|
|
else {
|
|
mProxy->mLastLengthComputable = mLengthComputable;
|
|
mProxy->mLastLoaded = mLoaded;
|
|
mProxy->mLastTotal = mTotal;
|
|
}
|
|
}
|
|
|
|
nsAutoPtr<XMLHttpRequest::StateData> state(new XMLHttpRequest::StateData());
|
|
StateDataAutoRooter rooter(aCx, state);
|
|
|
|
state->mResponseTextResult = mResponseTextResult;
|
|
state->mResponseText = mResponseText;
|
|
|
|
if (NS_SUCCEEDED(mResponseTextResult)) {
|
|
MOZ_ASSERT(mResponse.isUndefined() || mResponse.isNull());
|
|
state->mResponseResult = mResponseTextResult;
|
|
state->mResponse = mResponse;
|
|
}
|
|
else {
|
|
state->mResponseResult = mResponseResult;
|
|
|
|
if (NS_SUCCEEDED(mResponseResult)) {
|
|
if (HasData()) {
|
|
MOZ_ASSERT(mResponse.isUndefined());
|
|
|
|
ErrorResult rv;
|
|
JS::Rooted<JS::Value> response(aCx);
|
|
Read(nullptr, aCx, &response, rv);
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
rv.SuppressException();
|
|
return false;
|
|
}
|
|
|
|
state->mResponse = response;
|
|
}
|
|
else {
|
|
state->mResponse = mResponse;
|
|
}
|
|
}
|
|
}
|
|
|
|
state->mStatusResult = mStatusResult;
|
|
state->mStatus = mStatus;
|
|
|
|
state->mStatusText = mStatusText;
|
|
|
|
state->mReadyState = mReadyState;
|
|
|
|
state->mResponseURL = mResponseURL;
|
|
|
|
XMLHttpRequest* xhr = mProxy->mXMLHttpRequestPrivate;
|
|
xhr->UpdateState(*state, mUseCachedArrayBufferResponse);
|
|
|
|
if (mUploadEvent && !xhr->GetUploadObjectNoCreate()) {
|
|
return true;
|
|
}
|
|
|
|
JS::Rooted<JSString*> type(aCx,
|
|
JS_NewUCStringCopyN(aCx, mType.get(), mType.Length()));
|
|
if (!type) {
|
|
return false;
|
|
}
|
|
|
|
nsXHREventTarget* target;
|
|
if (mUploadEvent) {
|
|
target = xhr->GetUploadObjectNoCreate();
|
|
}
|
|
else {
|
|
target = xhr;
|
|
}
|
|
|
|
MOZ_ASSERT(target);
|
|
|
|
RefPtr<Event> event;
|
|
if (mProgressEvent) {
|
|
ProgressEventInit init;
|
|
init.mBubbles = false;
|
|
init.mCancelable = false;
|
|
init.mLengthComputable = mLengthComputable;
|
|
init.mLoaded = mLoaded;
|
|
init.mTotal = mTotal;
|
|
|
|
event = ProgressEvent::Constructor(target, mType, init);
|
|
}
|
|
else {
|
|
event = NS_NewDOMEvent(target, nullptr, nullptr);
|
|
|
|
if (event) {
|
|
event->InitEvent(mType, false, false);
|
|
}
|
|
}
|
|
|
|
if (!event) {
|
|
return false;
|
|
}
|
|
|
|
event->SetTrusted(true);
|
|
|
|
target->DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
|
|
|
// After firing the event set mResponse to JSVAL_NULL for chunked response
|
|
// types.
|
|
if (StringBeginsWith(mResponseType, NS_LITERAL_STRING("moz-chunked-"))) {
|
|
xhr->NullResponseText();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
NS_IMETHODIMP
|
|
WorkerThreadProxySyncRunnable::Run()
|
|
{
|
|
AssertIsOnMainThread();
|
|
|
|
nsCOMPtr<nsIEventTarget> tempTarget;
|
|
mSyncLoopTarget.swap(tempTarget);
|
|
|
|
mProxy->mSyncEventResponseTarget.swap(tempTarget);
|
|
|
|
nsresult rv = MainThreadRun();
|
|
|
|
RefPtr<ResponseRunnable> response =
|
|
new ResponseRunnable(mWorkerPrivate, mProxy, rv);
|
|
if (!response->Dispatch(nullptr)) {
|
|
MOZ_ASSERT(false, "Failed to dispatch response!");
|
|
}
|
|
|
|
mProxy->mSyncEventResponseTarget.swap(tempTarget);
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult
|
|
AbortRunnable::MainThreadRun()
|
|
{
|
|
mProxy->mInnerEventStreamId++;
|
|
|
|
WorkerPrivate* oldWorker = mProxy->mWorkerPrivate;
|
|
mProxy->mWorkerPrivate = mWorkerPrivate;
|
|
|
|
mProxy->mXHR->Abort();
|
|
|
|
mProxy->mWorkerPrivate = oldWorker;
|
|
|
|
mProxy->Reset();
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult
|
|
OpenRunnable::MainThreadRunInternal()
|
|
{
|
|
if (!mProxy->Init()) {
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
}
|
|
|
|
nsresult rv;
|
|
|
|
if (mBackgroundRequest) {
|
|
rv = mProxy->mXHR->SetMozBackgroundRequest(mBackgroundRequest);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
}
|
|
|
|
if (mWithCredentials) {
|
|
rv = mProxy->mXHR->SetWithCredentials(mWithCredentials);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
}
|
|
|
|
if (mTimeout) {
|
|
rv = mProxy->mXHR->SetTimeout(mTimeout);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
}
|
|
|
|
MOZ_ASSERT(!mProxy->mInOpen);
|
|
mProxy->mInOpen = true;
|
|
|
|
ErrorResult rv2;
|
|
mProxy->mXHR->Open(mMethod, mURL, true, mUser, mPassword, rv2);
|
|
|
|
MOZ_ASSERT(mProxy->mInOpen);
|
|
mProxy->mInOpen = false;
|
|
|
|
if (rv2.Failed()) {
|
|
return rv2.StealNSResult();
|
|
}
|
|
|
|
return mProxy->mXHR->SetResponseType(NS_LITERAL_STRING("text"));
|
|
}
|
|
|
|
|
|
nsresult
|
|
SendRunnable::MainThreadRun()
|
|
{
|
|
nsCOMPtr<nsIVariant> variant;
|
|
|
|
if (HasData()) {
|
|
AutoSafeJSContext cx;
|
|
JSAutoRequest ar(cx);
|
|
|
|
nsIXPConnect* xpc = nsContentUtils::XPConnect();
|
|
MOZ_ASSERT(xpc);
|
|
|
|
ErrorResult rv;
|
|
|
|
JS::Rooted<JS::Value> body(cx);
|
|
Read(nullptr, cx, &body, rv);
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
return rv.StealNSResult();
|
|
}
|
|
|
|
rv = xpc->JSValToVariant(cx, body, getter_AddRefs(variant));
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
return rv.StealNSResult();
|
|
}
|
|
}
|
|
else {
|
|
RefPtr<nsVariant> wvariant = new nsVariant();
|
|
|
|
if (NS_FAILED(wvariant->SetAsAString(mStringBody))) {
|
|
MOZ_ASSERT(false, "This should never fail!");
|
|
}
|
|
|
|
variant = wvariant;
|
|
}
|
|
|
|
// Send() has been already called.
|
|
if (mProxy->mWorkerPrivate) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
mProxy->mWorkerPrivate = mWorkerPrivate;
|
|
|
|
MOZ_ASSERT(!mProxy->mSyncLoopTarget);
|
|
mProxy->mSyncLoopTarget.swap(mSyncLoopTarget);
|
|
|
|
if (mHasUploadListeners) {
|
|
NS_ASSERTION(!mProxy->mUploadEventListenersAttached, "Huh?!");
|
|
if (!mProxy->AddRemoveEventListeners(true, true)) {
|
|
MOZ_ASSERT(false, "This should never fail!");
|
|
}
|
|
}
|
|
|
|
mProxy->mArrayBufferResponseWasTransferred = false;
|
|
|
|
mProxy->mInnerChannelId++;
|
|
|
|
nsresult rv = mProxy->mXHR->Send(variant);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
mProxy->mOutstandingSendCount++;
|
|
|
|
if (!mHasUploadListeners) {
|
|
NS_ASSERTION(!mProxy->mUploadEventListenersAttached, "Huh?!");
|
|
if (!mProxy->AddRemoveEventListeners(true, true)) {
|
|
MOZ_ASSERT(false, "This should never fail!");
|
|
}
|
|
}
|
|
}
|
|
|
|
return rv;
|
|
}
|
|
|
|
XMLHttpRequest::XMLHttpRequest(WorkerPrivate* aWorkerPrivate)
|
|
: mWorkerPrivate(aWorkerPrivate),
|
|
mResponseType(XMLHttpRequestResponseType::Text), mTimeout(0),
|
|
mRooted(false), mBackgroundRequest(false), mWithCredentials(false),
|
|
mCanceled(false), mMozAnon(false), mMozSystem(false)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
mozilla::HoldJSObjects(this);
|
|
}
|
|
|
|
XMLHttpRequest::~XMLHttpRequest()
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
ReleaseProxy(XHRIsGoingAway);
|
|
|
|
MOZ_ASSERT(!mRooted);
|
|
|
|
mozilla::DropJSObjects(this);
|
|
}
|
|
|
|
NS_IMPL_ADDREF_INHERITED(XMLHttpRequest, nsXHREventTarget)
|
|
NS_IMPL_RELEASE_INHERITED(XMLHttpRequest, nsXHREventTarget)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(XMLHttpRequest)
|
|
NS_INTERFACE_MAP_END_INHERITING(nsXHREventTarget)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(XMLHttpRequest)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(XMLHttpRequest,
|
|
nsXHREventTarget)
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mUpload)
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(XMLHttpRequest,
|
|
nsXHREventTarget)
|
|
tmp->ReleaseProxy(XHRIsGoingAway);
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mUpload)
|
|
tmp->mStateData.mResponse.setUndefined();
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(XMLHttpRequest,
|
|
nsXHREventTarget)
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mStateData.mResponse)
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
JSObject*
|
|
XMLHttpRequest::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
{
|
|
return XMLHttpRequestBinding_workers::Wrap(aCx, this, aGivenProto);
|
|
}
|
|
|
|
// static
|
|
already_AddRefed<XMLHttpRequest>
|
|
XMLHttpRequest::Constructor(const GlobalObject& aGlobal,
|
|
const MozXMLHttpRequestParameters& aParams,
|
|
ErrorResult& aRv)
|
|
{
|
|
JSContext* cx = aGlobal.Context();
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx);
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
RefPtr<XMLHttpRequest> xhr = new XMLHttpRequest(workerPrivate);
|
|
|
|
if (workerPrivate->XHRParamsAllowed()) {
|
|
if (aParams.mMozSystem)
|
|
xhr->mMozAnon = true;
|
|
else
|
|
xhr->mMozAnon = aParams.mMozAnon;
|
|
xhr->mMozSystem = aParams.mMozSystem;
|
|
}
|
|
|
|
return xhr.forget();
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::ReleaseProxy(ReleaseType aType)
|
|
{
|
|
// Can't assert that we're on the worker thread here because mWorkerPrivate
|
|
// may be gone.
|
|
|
|
if (mProxy) {
|
|
if (aType == XHRIsGoingAway) {
|
|
// We're in a GC finalizer, so we can't do a sync call here (and we don't
|
|
// need to).
|
|
RefPtr<AsyncTeardownRunnable> runnable =
|
|
new AsyncTeardownRunnable(mProxy);
|
|
mProxy = nullptr;
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(runnable))) {
|
|
NS_ERROR("Failed to dispatch teardown runnable!");
|
|
}
|
|
} else {
|
|
// This isn't necessary if the worker is going away or the XHR is going
|
|
// away.
|
|
if (aType == Default) {
|
|
// Don't let any more events run.
|
|
mProxy->mOuterEventStreamId++;
|
|
}
|
|
|
|
// We need to make a sync call here.
|
|
RefPtr<SyncTeardownRunnable> runnable =
|
|
new SyncTeardownRunnable(mWorkerPrivate, mProxy);
|
|
mProxy = nullptr;
|
|
|
|
if (!runnable->Dispatch(nullptr)) {
|
|
NS_ERROR("Failed to dispatch teardown runnable!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::MaybePin(ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mRooted) {
|
|
return;
|
|
}
|
|
|
|
JSContext* cx = GetCurrentThreadJSContext();
|
|
|
|
if (!mWorkerPrivate->AddFeature(cx, this)) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
mRooted = true;
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::MaybeDispatchPrematureAbortEvents(ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
MOZ_ASSERT(mProxy);
|
|
|
|
// Only send readystatechange event when state changed.
|
|
bool isStateChanged = false;
|
|
if (mStateData.mReadyState != 4) {
|
|
isStateChanged = true;
|
|
mStateData.mReadyState = 4;
|
|
}
|
|
|
|
if (mProxy->mSeenUploadLoadStart) {
|
|
MOZ_ASSERT(mUpload);
|
|
|
|
DispatchPrematureAbortEvent(mUpload, NS_LITERAL_STRING("abort"), true,
|
|
aRv);
|
|
if (aRv.Failed()) {
|
|
return;
|
|
}
|
|
|
|
DispatchPrematureAbortEvent(mUpload, NS_LITERAL_STRING("loadend"), true,
|
|
aRv);
|
|
if (aRv.Failed()) {
|
|
return;
|
|
}
|
|
|
|
mProxy->mSeenUploadLoadStart = false;
|
|
}
|
|
|
|
if (mProxy->mSeenLoadStart) {
|
|
if (isStateChanged) {
|
|
DispatchPrematureAbortEvent(this, NS_LITERAL_STRING("readystatechange"),
|
|
false, aRv);
|
|
if (aRv.Failed()) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
DispatchPrematureAbortEvent(this, NS_LITERAL_STRING("abort"), false, aRv);
|
|
if (aRv.Failed()) {
|
|
return;
|
|
}
|
|
|
|
DispatchPrematureAbortEvent(this, NS_LITERAL_STRING("loadend"), false,
|
|
aRv);
|
|
if (aRv.Failed()) {
|
|
return;
|
|
}
|
|
|
|
mProxy->mSeenLoadStart = false;
|
|
}
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::DispatchPrematureAbortEvent(EventTarget* aTarget,
|
|
const nsAString& aEventType,
|
|
bool aUploadTarget,
|
|
ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
MOZ_ASSERT(aTarget);
|
|
|
|
if (!mProxy) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
|
|
RefPtr<Event> event;
|
|
if (aEventType.EqualsLiteral("readystatechange")) {
|
|
event = NS_NewDOMEvent(aTarget, nullptr, nullptr);
|
|
event->InitEvent(aEventType, false, false);
|
|
}
|
|
else {
|
|
ProgressEventInit init;
|
|
init.mBubbles = false;
|
|
init.mCancelable = false;
|
|
if (aUploadTarget) {
|
|
init.mLengthComputable = mProxy->mLastUploadLengthComputable;
|
|
init.mLoaded = mProxy->mLastUploadLoaded;
|
|
init.mTotal = mProxy->mLastUploadTotal;
|
|
}
|
|
else {
|
|
init.mLengthComputable = mProxy->mLastLengthComputable;
|
|
init.mLoaded = mProxy->mLastLoaded;
|
|
init.mTotal = mProxy->mLastTotal;
|
|
}
|
|
event = ProgressEvent::Constructor(aTarget, aEventType, init);
|
|
}
|
|
|
|
if (!event) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
|
|
event->SetTrusted(true);
|
|
|
|
aTarget->DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::Unpin()
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mRooted, "Mismatched calls to Unpin!");
|
|
|
|
JSContext* cx = GetCurrentThreadJSContext();
|
|
|
|
mWorkerPrivate->RemoveFeature(cx, this);
|
|
|
|
mRooted = false;
|
|
|
|
NS_RELEASE_THIS();
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::SendInternal(SendRunnable* aRunnable,
|
|
ErrorResult& aRv)
|
|
{
|
|
MOZ_ASSERT(aRunnable);
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
// No send() calls when open is running.
|
|
if (mProxy->mOpenCount) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
|
|
bool hasUploadListeners = mUpload ? mUpload->HasListeners() : false;
|
|
|
|
MaybePin(aRv);
|
|
if (aRv.Failed()) {
|
|
return;
|
|
}
|
|
|
|
AutoUnpinXHR autoUnpin(this);
|
|
Maybe<AutoSyncLoopHolder> autoSyncLoop;
|
|
|
|
nsCOMPtr<nsIEventTarget> syncLoopTarget;
|
|
bool isSyncXHR = mProxy->mIsSyncXHR;
|
|
if (isSyncXHR) {
|
|
autoSyncLoop.emplace(mWorkerPrivate);
|
|
syncLoopTarget = autoSyncLoop->EventTarget();
|
|
}
|
|
|
|
mProxy->mOuterChannelId++;
|
|
|
|
JSContext* cx = mWorkerPrivate->GetJSContext();
|
|
|
|
aRunnable->SetSyncLoopTarget(syncLoopTarget);
|
|
aRunnable->SetHaveUploadListeners(hasUploadListeners);
|
|
|
|
if (!aRunnable->Dispatch(cx)) {
|
|
// Dispatch() may have spun the event loop and we may have already unrooted.
|
|
// If so we don't want autoUnpin to try again.
|
|
if (!mRooted) {
|
|
autoUnpin.Clear();
|
|
}
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
|
|
if (!isSyncXHR) {
|
|
autoUnpin.Clear();
|
|
MOZ_ASSERT(!autoSyncLoop);
|
|
return;
|
|
}
|
|
|
|
autoUnpin.Clear();
|
|
|
|
if (!autoSyncLoop->Run()) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
}
|
|
}
|
|
|
|
bool
|
|
XMLHttpRequest::Notify(JSContext* aCx, Status aStatus)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
MOZ_ASSERT(mWorkerPrivate->GetJSContext() == aCx);
|
|
|
|
if (aStatus >= Canceling && !mCanceled) {
|
|
mCanceled = true;
|
|
ReleaseProxy(WorkerIsGoingAway);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::Open(const nsACString& aMethod, const nsAString& aUrl,
|
|
bool aAsync, const Optional<nsAString>& aUser,
|
|
const Optional<nsAString>& aPassword, ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
if (mProxy) {
|
|
MaybeDispatchPrematureAbortEvents(aRv);
|
|
if (aRv.Failed()) {
|
|
return;
|
|
}
|
|
}
|
|
else {
|
|
mProxy = new Proxy(this, mMozAnon, mMozSystem);
|
|
}
|
|
|
|
mProxy->mOuterEventStreamId++;
|
|
|
|
RefPtr<OpenRunnable> runnable =
|
|
new OpenRunnable(mWorkerPrivate, mProxy, aMethod, aUrl, aUser, aPassword,
|
|
mBackgroundRequest, mWithCredentials,
|
|
mTimeout);
|
|
|
|
++mProxy->mOpenCount;
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
|
if (mProxy && !--mProxy->mOpenCount) {
|
|
ReleaseProxy();
|
|
}
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
|
|
// We have been released in one of the nested Open() calls.
|
|
if (!mProxy) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
|
|
--mProxy->mOpenCount;
|
|
mProxy->mIsSyncXHR = !aAsync;
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::SetRequestHeader(const nsACString& aHeader,
|
|
const nsACString& aValue, ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
if (!mProxy) {
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
return;
|
|
}
|
|
|
|
RefPtr<SetRequestHeaderRunnable> runnable =
|
|
new SetRequestHeaderRunnable(mWorkerPrivate, mProxy, aHeader, aValue);
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::SetTimeout(uint32_t aTimeout, ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
mTimeout = aTimeout;
|
|
|
|
if (!mProxy) {
|
|
// Open may not have been called yet, in which case we'll handle the
|
|
// timeout in OpenRunnable.
|
|
return;
|
|
}
|
|
|
|
RefPtr<SetTimeoutRunnable> runnable =
|
|
new SetTimeoutRunnable(mWorkerPrivate, mProxy, aTimeout);
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::SetWithCredentials(bool aWithCredentials, ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
mWithCredentials = aWithCredentials;
|
|
|
|
if (!mProxy) {
|
|
// Open may not have been called yet, in which case we'll handle the
|
|
// credentials in OpenRunnable.
|
|
return;
|
|
}
|
|
|
|
RefPtr<SetWithCredentialsRunnable> runnable =
|
|
new SetWithCredentialsRunnable(mWorkerPrivate, mProxy, aWithCredentials);
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::SetMozBackgroundRequest(bool aBackgroundRequest,
|
|
ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
mBackgroundRequest = aBackgroundRequest;
|
|
|
|
if (!mProxy) {
|
|
// Open may not have been called yet, in which case we'll handle the
|
|
// background request in OpenRunnable.
|
|
return;
|
|
}
|
|
|
|
RefPtr<SetBackgroundRequestRunnable> runnable =
|
|
new SetBackgroundRequestRunnable(mWorkerPrivate, mProxy,
|
|
aBackgroundRequest);
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
}
|
|
|
|
XMLHttpRequestUpload*
|
|
XMLHttpRequest::GetUpload(ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return nullptr;
|
|
}
|
|
|
|
if (!mUpload) {
|
|
mUpload = XMLHttpRequestUpload::Create(this);
|
|
|
|
if (!mUpload) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
return mUpload;
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::Send(ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
if (!mProxy) {
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
return;
|
|
}
|
|
|
|
RefPtr<SendRunnable> sendRunnable =
|
|
new SendRunnable(mWorkerPrivate, mProxy, NullString());
|
|
|
|
// Nothing to clone.
|
|
SendInternal(sendRunnable, aRv);
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::Send(const nsAString& aBody, ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
if (!mProxy) {
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
return;
|
|
}
|
|
|
|
RefPtr<SendRunnable> sendRunnable =
|
|
new SendRunnable(mWorkerPrivate, mProxy, aBody);
|
|
|
|
// Nothing to clone.
|
|
SendInternal(sendRunnable, aRv);
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::Send(JS::Handle<JSObject*> aBody, ErrorResult& aRv)
|
|
{
|
|
JSContext* cx = mWorkerPrivate->GetJSContext();
|
|
|
|
MOZ_ASSERT(aBody);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
if (!mProxy) {
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
return;
|
|
}
|
|
|
|
JS::Rooted<JS::Value> valToClone(cx);
|
|
if (JS_IsArrayBufferObject(aBody) || JS_IsArrayBufferViewObject(aBody)) {
|
|
valToClone.setObject(*aBody);
|
|
}
|
|
else {
|
|
JS::Rooted<JS::Value> obj(cx, JS::ObjectValue(*aBody));
|
|
JSString* bodyStr = JS::ToString(cx, obj);
|
|
if (!bodyStr) {
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
return;
|
|
}
|
|
valToClone.setString(bodyStr);
|
|
}
|
|
|
|
RefPtr<SendRunnable> sendRunnable =
|
|
new SendRunnable(mWorkerPrivate, mProxy, EmptyString());
|
|
|
|
sendRunnable->Write(cx, valToClone, aRv);
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
return;
|
|
}
|
|
|
|
SendInternal(sendRunnable, aRv);
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::Send(Blob& aBody, ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
JSContext* cx = mWorkerPrivate->GetJSContext();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
if (!mProxy) {
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
return;
|
|
}
|
|
|
|
JS::Rooted<JS::Value> value(cx);
|
|
if (!GetOrCreateDOMReflector(cx, &aBody, &value)) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
|
|
RefPtr<BlobImpl> blobImpl = aBody.Impl();
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
aRv = blobImpl->SetMutable(false);
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
return;
|
|
}
|
|
|
|
RefPtr<SendRunnable> sendRunnable =
|
|
new SendRunnable(mWorkerPrivate, mProxy, EmptyString());
|
|
|
|
sendRunnable->Write(cx, value, aRv);
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
return;
|
|
}
|
|
|
|
SendInternal(sendRunnable, aRv);
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::Send(nsFormData& aBody, ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
JSContext* cx = mWorkerPrivate->GetJSContext();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
if (!mProxy) {
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
return;
|
|
}
|
|
|
|
JS::Rooted<JS::Value> value(cx);
|
|
if (!GetOrCreateDOMReflector(cx, &aBody, &value)) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
|
|
RefPtr<SendRunnable> sendRunnable =
|
|
new SendRunnable(mWorkerPrivate, mProxy, EmptyString());
|
|
|
|
sendRunnable->Write(cx, value, aRv);
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
return;
|
|
}
|
|
|
|
SendInternal(sendRunnable, aRv);
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::Send(const ArrayBuffer& aBody, ErrorResult& aRv)
|
|
{
|
|
JS::Rooted<JSObject*> obj(mWorkerPrivate->GetJSContext(), aBody.Obj());
|
|
return Send(obj, aRv);
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::Send(const ArrayBufferView& aBody, ErrorResult& aRv)
|
|
{
|
|
if (JS_IsTypedArrayObject(aBody.Obj()) &&
|
|
JS_GetTypedArraySharedness(aBody.Obj())) {
|
|
// Throw if the object is mapping shared memory (must opt in).
|
|
aRv.ThrowTypeError<MSG_TYPEDARRAY_IS_SHARED>(NS_LITERAL_STRING("Argument of XMLHttpRequest.send"));
|
|
return;
|
|
}
|
|
JS::Rooted<JSObject*> obj(mWorkerPrivate->GetJSContext(), aBody.Obj());
|
|
return Send(obj, aRv);
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::Abort(ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
if (!mProxy) {
|
|
return;
|
|
}
|
|
|
|
MaybeDispatchPrematureAbortEvents(aRv);
|
|
if (aRv.Failed()) {
|
|
return;
|
|
}
|
|
|
|
mProxy->mOuterEventStreamId++;
|
|
|
|
RefPtr<AbortRunnable> runnable = new AbortRunnable(mWorkerPrivate, mProxy);
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::GetResponseHeader(const nsACString& aHeader,
|
|
nsACString& aResponseHeader, ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
if (!mProxy) {
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
return;
|
|
}
|
|
|
|
nsCString responseHeader;
|
|
RefPtr<GetResponseHeaderRunnable> runnable =
|
|
new GetResponseHeaderRunnable(mWorkerPrivate, mProxy, aHeader,
|
|
responseHeader);
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
aResponseHeader = responseHeader;
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::GetAllResponseHeaders(nsACString& aResponseHeaders,
|
|
ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
if (!mProxy) {
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
return;
|
|
}
|
|
|
|
nsCString responseHeaders;
|
|
RefPtr<GetAllResponseHeadersRunnable> runnable =
|
|
new GetAllResponseHeadersRunnable(mWorkerPrivate, mProxy, responseHeaders);
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
|
|
aResponseHeaders = responseHeaders;
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::OverrideMimeType(const nsAString& aMimeType, ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
// We're supposed to throw if the state is not OPENED or HEADERS_RECEIVED. We
|
|
// can detect OPENED really easily but we can't detect HEADERS_RECEIVED in a
|
|
// non-racy way until the XHR state machine actually runs on this thread
|
|
// (bug 671047). For now we're going to let this work only if the Send()
|
|
// method has not been called, unless the send has been aborted.
|
|
if (!mProxy || (SendInProgress() &&
|
|
(mProxy->mSeenLoadStart ||
|
|
mStateData.mReadyState > nsIXMLHttpRequest::OPENED))) {
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
return;
|
|
}
|
|
|
|
RefPtr<OverrideMimeTypeRunnable> runnable =
|
|
new OverrideMimeTypeRunnable(mWorkerPrivate, mProxy, aMimeType);
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::SetResponseType(XMLHttpRequestResponseType aResponseType,
|
|
ErrorResult& aRv)
|
|
{
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (mCanceled) {
|
|
aRv.ThrowUncatchableException();
|
|
return;
|
|
}
|
|
|
|
if (!mProxy || (SendInProgress() &&
|
|
(mProxy->mSeenLoadStart ||
|
|
mStateData.mReadyState > nsIXMLHttpRequest::OPENED))) {
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
return;
|
|
}
|
|
|
|
// "document" is fine for the main thread but not for a worker. Short-circuit
|
|
// that here.
|
|
if (aResponseType == XMLHttpRequestResponseType::Document) {
|
|
return;
|
|
}
|
|
|
|
nsString responseType;
|
|
ConvertResponseTypeToString(aResponseType, responseType);
|
|
|
|
RefPtr<SetResponseTypeRunnable> runnable =
|
|
new SetResponseTypeRunnable(mWorkerPrivate, mProxy, responseType);
|
|
if (!runnable->Dispatch(mWorkerPrivate->GetJSContext())) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
|
|
nsString acceptedResponseTypeString;
|
|
runnable->GetResponseType(acceptedResponseTypeString);
|
|
|
|
mResponseType = ConvertStringToResponseType(acceptedResponseTypeString);
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::GetResponse(JSContext* /* unused */,
|
|
JS::MutableHandle<JS::Value> aResponse,
|
|
ErrorResult& aRv)
|
|
{
|
|
if (NS_SUCCEEDED(mStateData.mResponseTextResult) &&
|
|
mStateData.mResponse.isUndefined()) {
|
|
MOZ_ASSERT(NS_SUCCEEDED(mStateData.mResponseResult));
|
|
|
|
if (mStateData.mResponseText.IsEmpty()) {
|
|
mStateData.mResponse =
|
|
JS_GetEmptyStringValue(mWorkerPrivate->GetJSContext());
|
|
} else {
|
|
JSString* str =
|
|
JS_NewUCStringCopyN(mWorkerPrivate->GetJSContext(),
|
|
mStateData.mResponseText.get(),
|
|
mStateData.mResponseText.Length());
|
|
|
|
if (!str) {
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
return;
|
|
}
|
|
|
|
mStateData.mResponse.setString(str);
|
|
}
|
|
}
|
|
|
|
JS::ExposeValueToActiveJS(mStateData.mResponse);
|
|
aRv = mStateData.mResponseResult;
|
|
aResponse.set(mStateData.mResponse);
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::GetResponseText(nsAString& aResponseText, ErrorResult& aRv)
|
|
{
|
|
aRv = mStateData.mResponseTextResult;
|
|
aResponseText = mStateData.mResponseText;
|
|
}
|
|
|
|
void
|
|
XMLHttpRequest::UpdateState(const StateData& aStateData,
|
|
bool aUseCachedArrayBufferResponse)
|
|
{
|
|
if (aUseCachedArrayBufferResponse) {
|
|
MOZ_ASSERT(mStateData.mResponse.isObject() &&
|
|
JS_IsArrayBufferObject(&mStateData.mResponse.toObject()));
|
|
|
|
JS::Rooted<JS::Value> response(mWorkerPrivate->GetJSContext(),
|
|
mStateData.mResponse);
|
|
mStateData = aStateData;
|
|
mStateData.mResponse = response;
|
|
}
|
|
else {
|
|
mStateData = aStateData;
|
|
}
|
|
}
|