From 4fb227aead45f4e9e0ca06148afdf5e9c3523403 Mon Sep 17 00:00:00 2001 From: Olli Pettay Date: Fri, 8 Jun 2012 23:45:13 +0300 Subject: [PATCH] Bug 756277 - XMLHttpRequest started from Cc['@mozilla.org/xmlextras/xmlhttprequest;1'] ends up bound to some random window, r=bz --HG-- extra : rebase_source : 480dbf41846fee3362697cb0bf537281f35845e0 --- content/base/src/nsXMLHttpRequest.cpp | 33 +++---------------- .../base/test/TestNativeXMLHttpRequest.cpp | 5 --- dom/base/nsDOMClassInfo.cpp | 9 ++++- .../tests/SimpleTest/specialpowersAPI.js | 3 +- .../tests/test_SpecialPowersExtension.html | 1 - 5 files changed, 13 insertions(+), 38 deletions(-) diff --git a/content/base/src/nsXMLHttpRequest.cpp b/content/base/src/nsXMLHttpRequest.cpp index 7c103874468..504d061a107 100644 --- a/content/base/src/nsXMLHttpRequest.cpp +++ b/content/base/src/nsXMLHttpRequest.cpp @@ -494,41 +494,16 @@ nsXMLHttpRequest::RootResultArrayBuffer() nsresult nsXMLHttpRequest::Init() { - // Set the original mPrincipal, if available. - // Get JSContext from stack. - nsCOMPtr stack = - do_GetService("@mozilla.org/js/xpc/ContextStack;1"); - - if (!stack) { - return NS_OK; - } - - JSContext *cx; - - if (NS_FAILED(stack->Peek(&cx)) || !cx) { - return NS_OK; - } - - nsIScriptSecurityManager *secMan = nsContentUtils::GetSecurityManager(); + nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager(); nsCOMPtr subjectPrincipal; if (secMan) { - nsresult rv = secMan->GetSubjectPrincipal(getter_AddRefs(subjectPrincipal)); - NS_ENSURE_SUCCESS(rv, rv); + secMan->GetSystemPrincipal(getter_AddRefs(subjectPrincipal)); } NS_ENSURE_STATE(subjectPrincipal); - - nsIScriptContext* context = GetScriptContextFromJSContext(cx); - nsCOMPtr window; - if (context) { - window = do_QueryInterface(context->GetGlobalObject()); - if (window) { - window = window->GetCurrentInnerWindow(); - } - } - - Construct(subjectPrincipal, window); + Construct(subjectPrincipal, nsnull); return NS_OK; } + /** * This Init method should only be called by C++ consumers. */ diff --git a/content/base/test/TestNativeXMLHttpRequest.cpp b/content/base/test/TestNativeXMLHttpRequest.cpp index 9864f74471b..13b638e3e43 100644 --- a/content/base/test/TestNativeXMLHttpRequest.cpp +++ b/content/base/test/TestNativeXMLHttpRequest.cpp @@ -43,11 +43,6 @@ nsresult TestNativeXMLHttpRequest() NS_NAMED_LITERAL_CSTRING(getString, "GET"); NS_NAMED_LITERAL_CSTRING(testURL, TEST_URL); const nsAString& empty = EmptyString(); - - printf("*** About to see an expected warning about mPrincipal:\n"); - rv = xhr->Open(getString, testURL, false, empty, empty); - printf("*** End of expected warning output.\n"); - TEST_ENSURE_FAILED(rv, "Open should have failed!"); nsCOMPtr secman = do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv); diff --git a/dom/base/nsDOMClassInfo.cpp b/dom/base/nsDOMClassInfo.cpp index 7ae5cb2c85f..2fdf6fcb12a 100644 --- a/dom/base/nsDOMClassInfo.cpp +++ b/dom/base/nsDOMClassInfo.cpp @@ -1685,7 +1685,6 @@ static const nsContractIDMapData kConstructorMap[] = NS_DEFINE_CONSTRUCTOR_DATA(FileReader, NS_FILEREADER_CONTRACTID) NS_DEFINE_CONSTRUCTOR_DATA(FormData, NS_FORMDATA_CONTRACTID) NS_DEFINE_CONSTRUCTOR_DATA(XMLSerializer, NS_XMLSERIALIZER_CONTRACTID) - NS_DEFINE_CONSTRUCTOR_DATA(XMLHttpRequest, NS_XMLHTTPREQUEST_CONTRACTID) NS_DEFINE_CONSTRUCTOR_DATA(WebSocket, NS_WEBSOCKET_CONTRACTID) NS_DEFINE_CONSTRUCTOR_DATA(XPathEvaluator, NS_XPATH_EVALUATOR_CONTRACTID) NS_DEFINE_CONSTRUCTOR_DATA(XSLTProcessor, @@ -1724,6 +1723,13 @@ NS_DOMStorageEventCtor(nsISupports** aInstancePtrResult) return CallQueryInterface(e, aInstancePtrResult); } +nsresult +NS_XMLHttpRequestCtor(nsISupports** aInstancePtrResult) +{ + nsXMLHttpRequest* xhr = new nsXMLHttpRequest(); + return CallQueryInterface(xhr, aInstancePtrResult); +} + struct nsConstructorFuncMapData { PRInt32 mDOMClassInfoID; @@ -1755,6 +1761,7 @@ static const nsConstructorFuncMapData kConstructorFuncMap[] = NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(DeviceLightEvent) NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(StorageEvent) NS_DEFINE_CONSTRUCTOR_FUNC_DATA(MozSmsFilter, sms::SmsFilter::NewSmsFilter) + NS_DEFINE_CONSTRUCTOR_FUNC_DATA(XMLHttpRequest, NS_XMLHttpRequestCtor) }; nsIXPConnect *nsDOMClassInfo::sXPConnect = nsnull; diff --git a/testing/mochitest/tests/SimpleTest/specialpowersAPI.js b/testing/mochitest/tests/SimpleTest/specialpowersAPI.js index f3a49cd3a3c..3a3addd3f02 100644 --- a/testing/mochitest/tests/SimpleTest/specialpowersAPI.js +++ b/testing/mochitest/tests/SimpleTest/specialpowersAPI.js @@ -771,8 +771,7 @@ SpecialPowersAPI.prototype = { }, createSystemXHR: function() { - return Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] - .createInstance(Ci.nsIXMLHttpRequest); + return this.wrap(Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest)); }, snapshotWindow: function (win, withCaret) { diff --git a/testing/mochitest/tests/test_SpecialPowersExtension.html b/testing/mochitest/tests/test_SpecialPowersExtension.html index 768b1ae781e..3273d452131 100644 --- a/testing/mochitest/tests/test_SpecialPowersExtension.html +++ b/testing/mochitest/tests/test_SpecialPowersExtension.html @@ -96,7 +96,6 @@ function starttest(){ // Try some basic stuff with XHR. var xhr2 = SpecialPowers.wrap(Components).classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest); - is(xhr2.toString(), SpecialPowers.unwrap(xhr2).toString(), "toString should be transparently delegated"); is(xhr.readyState, XMLHttpRequest.UNSENT, "Should be able to get props off privileged objects"); var testURI = SpecialPowers.wrap(Components).classes['@mozilla.org/network/standard-url;1'] .createInstance(Components.interfaces.nsIURI);