no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / dom / xhr / XMLHttpRequest.cpp
blob8807c5515a92ba516ea5c1fa05bc9e1a210dea34
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 {
19 /* static */
20 already_AddRefed<XMLHttpRequest> XMLHttpRequest::Constructor(
21 const GlobalObject& aGlobal, const MozXMLHttpRequestParameters& aParams,
22 ErrorResult& aRv) {
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);
30 return nullptr;
33 nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
34 nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(global);
35 nsCOMPtr<nsIPrincipal> principal = scriptPrincipal->GetPrincipal();
36 if (window) {
37 Document* document = window->GetExtantDoc();
38 if (NS_WARN_IF(!document)) {
39 aRv.Throw(NS_ERROR_FAILURE);
40 return nullptr;
43 cookieJarSettings = document->CookieJarSettings();
44 } else {
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);
52 bool isAnon = false;
53 if (aParams.mMozAnon.WasPassed()) {
54 isAnon = aParams.mMozAnon.Value();
55 } else {
56 isAnon =
57 StaticPrefs::network_fetch_systemDefaultsToOmittingCredentials() &&
58 (aParams.mMozSystem || principal->IsSystemPrincipal());
60 req->InitParameters(isAnon, aParams.mMozSystem);
61 return req.forget();
64 return XMLHttpRequestWorker::Construct(aGlobal, aParams, aRv);
67 } // namespace mozilla::dom