1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "XMLHttpRequest.h"
8 #include "XMLHttpRequestMainThread.h"
9 #include "XMLHttpRequestWorker.h"
10 #include "mozilla/BasePrincipal.h"
11 #include "mozilla/Logging.h"
12 #include "mozilla/StaticPrefs_network.h"
13 #include "mozilla/net/CookieJarSettings.h"
15 mozilla::LazyLogModule
gXMLHttpRequestLog("XMLHttpRequest");
17 namespace mozilla::dom
{
20 already_AddRefed
<XMLHttpRequest
> XMLHttpRequest::Constructor(
21 const GlobalObject
& aGlobal
, const MozXMLHttpRequestParameters
& aParams
,
23 if (NS_IsMainThread()) {
24 nsCOMPtr
<nsIGlobalObject
> global
=
25 do_QueryInterface(aGlobal
.GetAsSupports());
26 nsCOMPtr
<nsIScriptObjectPrincipal
> scriptPrincipal
=
27 do_QueryInterface(aGlobal
.GetAsSupports());
28 if (!global
|| !scriptPrincipal
) {
29 aRv
.Throw(NS_ERROR_FAILURE
);
33 nsCOMPtr
<nsICookieJarSettings
> cookieJarSettings
;
34 nsCOMPtr
<nsPIDOMWindowInner
> window
= do_QueryInterface(global
);
35 nsCOMPtr
<nsIPrincipal
> principal
= scriptPrincipal
->GetPrincipal();
37 Document
* document
= window
->GetExtantDoc();
38 if (NS_WARN_IF(!document
)) {
39 aRv
.Throw(NS_ERROR_FAILURE
);
43 cookieJarSettings
= document
->CookieJarSettings();
45 // We are here because this is a sandbox.
46 cookieJarSettings
= net::CookieJarSettings::Create(principal
);
49 RefPtr
<XMLHttpRequestMainThread
> req
= new XMLHttpRequestMainThread(global
);
50 req
->Construct(principal
, cookieJarSettings
, false);
53 if (aParams
.mMozAnon
.WasPassed()) {
54 isAnon
= aParams
.mMozAnon
.Value();
57 StaticPrefs::network_fetch_systemDefaultsToOmittingCredentials() &&
58 (aParams
.mMozSystem
|| principal
->IsSystemPrincipal());
60 req
->InitParameters(isAnon
, aParams
.mMozSystem
);
64 return XMLHttpRequestWorker::Construct(aGlobal
, aParams
, aRv
);
67 } // namespace mozilla::dom