XMLHttpRequest.cpp (2337B)
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/. */ 6 7 #include "XMLHttpRequest.h" 8 9 #include "XMLHttpRequestMainThread.h" 10 #include "XMLHttpRequestWorker.h" 11 #include "mozilla/BasePrincipal.h" 12 #include "mozilla/Logging.h" 13 #include "mozilla/StaticPrefs_network.h" 14 #include "mozilla/net/CookieJarSettings.h" 15 16 mozilla::LazyLogModule gXMLHttpRequestLog("XMLHttpRequest"); 17 18 namespace mozilla::dom { 19 20 /* static */ 21 already_AddRefed<XMLHttpRequest> XMLHttpRequest::Constructor( 22 const GlobalObject& aGlobal, const MozXMLHttpRequestParameters& aParams, 23 ErrorResult& aRv) { 24 if (NS_IsMainThread()) { 25 nsCOMPtr<nsIGlobalObject> global = 26 do_QueryInterface(aGlobal.GetAsSupports()); 27 nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal = 28 do_QueryInterface(aGlobal.GetAsSupports()); 29 if (!global || !scriptPrincipal) { 30 aRv.Throw(NS_ERROR_FAILURE); 31 return nullptr; 32 } 33 34 nsCOMPtr<nsICookieJarSettings> cookieJarSettings; 35 nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(global); 36 nsCOMPtr<nsIPrincipal> principal = scriptPrincipal->GetPrincipal(); 37 if (window) { 38 Document* document = window->GetExtantDoc(); 39 if (NS_WARN_IF(!document)) { 40 aRv.Throw(NS_ERROR_FAILURE); 41 return nullptr; 42 } 43 44 cookieJarSettings = document->CookieJarSettings(); 45 } else { 46 // We are here because this is a sandbox. 47 cookieJarSettings = net::CookieJarSettings::Create(principal); 48 } 49 50 RefPtr<XMLHttpRequestMainThread> req = new XMLHttpRequestMainThread(global); 51 req->Construct(principal, cookieJarSettings, false); 52 53 bool isAnon = false; 54 if (aParams.mMozAnon.WasPassed()) { 55 isAnon = aParams.mMozAnon.Value(); 56 } else { 57 isAnon = 58 StaticPrefs::network_fetch_systemDefaultsToOmittingCredentials() && 59 (aParams.mMozSystem || principal->IsSystemPrincipal()); 60 } 61 req->InitParameters(isAnon, aParams.mMozSystem); 62 return req.forget(); 63 } 64 65 return XMLHttpRequestWorker::Construct(aGlobal, aParams, aRv); 66 } 67 68 } // namespace mozilla::dom