Bug 472529, websockets (content patch)

--HG--
extra : rebase_source : 831a44f14acffa793cd3814d40a0d44bf152decb
This commit is contained in:
wfernandom2004@gmail.com 2010-06-17 14:27:52 +03:00
parent 5a47c02818
commit af2d03d1ec
9 changed files with 219 additions and 96 deletions

View File

@ -78,6 +78,7 @@ nsDOMFile.h \
nsLineBreaker.h \
nsReferencedElement.h \
nsXMLNameSpaceMap.h \
nsDOMEventTargetWrapperCache.h \
$(NULL)
EXPORTS_NAMESPACES = mozilla/dom

View File

@ -129,6 +129,7 @@ class nsIBidiKeyboard;
class nsIMIMEHeaderParam;
class nsIObserver;
class nsPresContext;
class nsIChannel;
#ifndef have_PrefChangedFunc_typedef
typedef int (*PR_CALLBACK PrefChangedFunc)(const char *, void *);
@ -1524,6 +1525,8 @@ public:
static already_AddRefed<nsIDocument>
GetDocumentFromScriptContext(nsIScriptContext *aScriptContext);
static PRBool CheckMayLoad(nsIPrincipal* aPrincipal, nsIChannel* aChannel);
/**
* The method checks whether the caller can access native anonymous content.
* If there is no JS in the stack or privileged JS is running, this

View File

@ -0,0 +1,101 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et tw=80 : */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Wellington Fernando de Macedo.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Wellington Fernando de Macedo <wfernandom2004@gmail.com> (original author)
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsDOMEventTargetWrapperCache_h__
#define nsDOMEventTargetWrapperCache_h__
#include "nsDOMEventTargetHelper.h"
#include "nsWrapperCache.h"
#include "nsIScriptContext.h"
// Base class intended to be used for objets like XMLHttpRequest,
// EventSource and WebSocket.
class nsDOMEventTargetWrapperCache : public nsDOMEventTargetHelper,
public nsWrapperCache
{
public:
NS_DECL_ISUPPORTS_INHERITED
class NS_CYCLE_COLLECTION_INNERCLASS
: public NS_CYCLE_COLLECTION_CLASSNAME(nsDOMEventTargetHelper)
{
NS_IMETHOD RootAndUnlinkJSObjects(void *p);
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED_BODY(nsDOMEventTargetWrapperCache,
nsDOMEventTargetHelper)
NS_IMETHOD_(void) Trace(void *p, TraceCallback cb, void *closure);
};
NS_CYCLE_COLLECTION_PARTICIPANT_INSTANCE
void GetParentObject(nsIScriptGlobalObject **aParentObject)
{
if (mOwner) {
CallQueryInterface(mOwner, aParentObject);
}
else {
*aParentObject = nsnull;
}
}
static nsDOMEventTargetWrapperCache* FromSupports(nsISupports* aSupports)
{
nsPIDOMEventTarget* target =
static_cast<nsPIDOMEventTarget*>(aSupports);
#ifdef DEBUG
{
nsCOMPtr<nsPIDOMEventTarget> target_qi =
do_QueryInterface(aSupports);
// If this assertion fires the QI implementation for the object in
// question doesn't use the nsPIDOMEventTarget pointer as the
// nsISupports pointer. That must be fixed, or we'll crash...
NS_ASSERTION(target_qi == target, "Uh, fix QI!");
}
#endif
return static_cast<nsDOMEventTargetWrapperCache*>(target);
}
protected:
nsDOMEventTargetWrapperCache() : nsDOMEventTargetHelper(), nsWrapperCache() {}
virtual ~nsDOMEventTargetWrapperCache();
};
#endif // nsDOMEventTargetWrapperCache_h__

View File

@ -91,6 +91,7 @@ CPPSRCS = \
nsDOMAttribute.cpp \
nsDOMAttributeMap.cpp \
nsDOMDocumentType.cpp \
nsDOMEventTargetWrapperCache.cpp \
nsDOMFile.cpp \
nsDOMFileReader.cpp \
nsDOMLists.cpp \

View File

@ -5091,6 +5091,17 @@ nsContentUtils::GetDocumentFromScriptContext(nsIScriptContext *aScriptContext)
return doc;
}
/* static */
PRBool
nsContentUtils::CheckMayLoad(nsIPrincipal* aPrincipal, nsIChannel* aChannel)
{
nsCOMPtr<nsIURI> channelURI;
nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(channelURI));
NS_ENSURE_SUCCESS(rv, PR_FALSE);
return NS_SUCCEEDED(aPrincipal->CheckMayLoad(channelURI, PR_FALSE));
}
nsContentTypeParser::nsContentTypeParser(const nsAString& aString)
: mString(aString), mService(nsnull)
{

View File

@ -0,0 +1,74 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et tw=80 : */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Wellington Fernando de Macedo.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Wellington Fernando de Macedo <wfernandom2004@gmail.com> (original author)
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsContentUtils.h"
#include "nsDOMEventTargetWrapperCache.h"
#include "nsIDocument.h"
nsDOMEventTargetWrapperCache::~nsDOMEventTargetWrapperCache()
{
nsISupports *supports = static_cast<nsPIDOMEventTarget*>(this);
nsContentUtils::ReleaseWrapper(supports, this);
}
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMEventTargetWrapperCache)
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsDOMEventTargetWrapperCache)
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsDOMEventTargetWrapperCache,
nsDOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_ROOT_BEGIN(nsDOMEventTargetWrapperCache)
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_ROOT_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsDOMEventTargetWrapperCache,
nsDOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMEventTargetWrapperCache)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(nsDOMEventTargetWrapperCache, nsDOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(nsDOMEventTargetWrapperCache, nsDOMEventTargetHelper)

View File

@ -503,21 +503,10 @@ nsACProxyListener::GetInterface(const nsIID & aIID, void **aResult)
/////////////////////////////////////////////
nsXHREventTarget::~nsXHREventTarget()
{
nsISupports *supports = static_cast<nsIXMLHttpRequestEventTarget*>(this);
nsContentUtils::ReleaseWrapper(supports, this);
}
NS_IMPL_CYCLE_COLLECTION_CLASS(nsXHREventTarget)
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsXHREventTarget)
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsXHREventTarget,
nsDOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
nsDOMEventTargetWrapperCache)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnLoadListener)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnErrorListener)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnAbortListener)
@ -525,12 +514,8 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsXHREventTarget,
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnProgressListener)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_ROOT_BEGIN(nsXHREventTarget)
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_ROOT_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsXHREventTarget,
nsDOMEventTargetHelper)
nsDOMEventTargetWrapperCache)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnLoadListener)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnErrorListener)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnAbortListener)
@ -539,12 +524,11 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsXHREventTarget,
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsXHREventTarget)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsIXMLHttpRequestEventTarget)
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetWrapperCache)
NS_IMPL_ADDREF_INHERITED(nsXHREventTarget, nsDOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(nsXHREventTarget, nsDOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(nsXHREventTarget, nsDOMEventTargetWrapperCache)
NS_IMPL_RELEASE_INHERITED(nsXHREventTarget, nsDOMEventTargetWrapperCache)
NS_IMETHODIMP
nsXHREventTarget::GetOnload(nsIDOMEventListener** aOnLoad)
@ -1566,36 +1550,20 @@ nsXMLHttpRequest::GetCurrentHttpChannel()
return httpChannel;
}
inline PRBool
IsSystemPrincipal(nsIPrincipal* aPrincipal)
{
PRBool isSystem = PR_FALSE;
nsContentUtils::GetSecurityManager()->
IsSystemPrincipal(aPrincipal, &isSystem);
return isSystem;
}
static PRBool
CheckMayLoad(nsIPrincipal* aPrincipal, nsIChannel* aChannel)
{
NS_ASSERTION(!IsSystemPrincipal(aPrincipal), "Shouldn't get here!");
nsCOMPtr<nsIURI> channelURI;
nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(channelURI));
NS_ENSURE_SUCCESS(rv, PR_FALSE);
return NS_SUCCEEDED(aPrincipal->CheckMayLoad(channelURI, PR_FALSE));
}
nsresult
nsXMLHttpRequest::CheckChannelForCrossSiteRequest(nsIChannel* aChannel)
{
nsresult rv;
// First check if this is a same-origin request, or if cross-site requests
// are enabled.
if ((mState & XML_HTTP_REQUEST_XSITEENABLED) ||
CheckMayLoad(mPrincipal, aChannel)) {
// First check if cross-site requests are enabled
if ((mState & XML_HTTP_REQUEST_XSITEENABLED)) {
return NS_OK;
}
// or if this is a same-origin request.
NS_ASSERTION(!nsContentUtils::IsSystemPrincipal(mPrincipal),
"Shouldn't get here!");
if (nsContentUtils::CheckMayLoad(mPrincipal, aChannel)) {
return NS_OK;
}
@ -1773,7 +1741,7 @@ nsXMLHttpRequest::OpenRequest(const nsACString& method,
if (NS_FAILED(rv)) return rv;
// Check if we're doing a cross-origin request.
if (IsSystemPrincipal(mPrincipal)) {
if (nsContentUtils::IsSystemPrincipal(mPrincipal)) {
// Chrome callers are always allowed to read from different origins.
mState |= XML_HTTP_REQUEST_XSITEENABLED;
}
@ -1930,7 +1898,7 @@ nsXMLHttpRequest::OnStartRequest(nsIRequest *request, nsISupports *ctxt)
NS_ENSURE_TRUE(channel, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIPrincipal> documentPrincipal = mPrincipal;
if (IsSystemPrincipal(documentPrincipal)) {
if (nsContentUtils::IsSystemPrincipal(documentPrincipal)) {
// Don't give this document the system principal. We need to keep track of
// mPrincipal being system because we use it for various security checks
// that should be passing, but the document data shouldn't get a system
@ -2399,7 +2367,7 @@ nsXMLHttpRequest::Send(nsIVariant *aBody)
if (httpChannel) {
httpChannel->GetRequestMethod(method); // If GET, method name will be uppercase
if (!IsSystemPrincipal(mPrincipal)) {
if (!nsContentUtils::IsSystemPrincipal(mPrincipal)) {
nsCOMPtr<nsIURI> codebase;
mPrincipal->GetURI(getter_AddRefs(codebase));

View File

@ -67,7 +67,7 @@
#include "nsITimer.h"
#include "nsIPrivateDOMEvent.h"
#include "nsDOMProgressEvent.h"
#include "nsDOMEventTargetHelper.h"
#include "nsDOMEventTargetWrapperCache.h"
class nsILoadGroup;
@ -137,57 +137,18 @@ private:
PRCList mList;
};
class nsXHREventTarget : public nsDOMEventTargetHelper,
public nsIXMLHttpRequestEventTarget,
public nsWrapperCache
class nsXHREventTarget : public nsDOMEventTargetWrapperCache,
public nsIXMLHttpRequestEventTarget
{
public:
virtual ~nsXHREventTarget();
virtual ~nsXHREventTarget() {}
NS_DECL_ISUPPORTS_INHERITED
class NS_CYCLE_COLLECTION_INNERCLASS
: public NS_CYCLE_COLLECTION_CLASSNAME(nsDOMEventTargetHelper)
{
NS_IMETHOD RootAndUnlinkJSObjects(void *p);
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED_BODY(nsXHREventTarget,
nsDOMEventTargetHelper)
NS_IMETHOD_(void) Trace(void *p, TraceCallback cb, void *closure);
};
NS_CYCLE_COLLECTION_PARTICIPANT_INSTANCE
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsXHREventTarget,
nsDOMEventTargetWrapperCache)
NS_DECL_NSIXMLHTTPREQUESTEVENTTARGET
NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetHelper::)
NS_FORWARD_NSIDOMNSEVENTTARGET(nsDOMEventTargetHelper::)
void GetParentObject(nsIScriptGlobalObject **aParentObject)
{
if (mOwner) {
CallQueryInterface(mOwner, aParentObject);
}
else {
*aParentObject = nsnull;
}
}
static nsXHREventTarget* FromSupports(nsISupports* aSupports)
{
nsPIDOMEventTarget* target =
static_cast<nsPIDOMEventTarget*>(aSupports);
#ifdef DEBUG
{
nsCOMPtr<nsPIDOMEventTarget> target_qi =
do_QueryInterface(aSupports);
// If this assertion fires the QI implementation for the object in
// question doesn't use the nsPIDOMEventTarget pointer as the
// nsISupports pointer. That must be fixed, or we'll crash...
NS_ASSERTION(target_qi == target, "Uh, fix QI!");
}
#endif
return static_cast<nsXHREventTarget*>(target);
}
protected:
nsRefPtr<nsDOMEventListenerWrapper> mOnLoadListener;
nsRefPtr<nsDOMEventListenerWrapper> mOnErrorListener;

View File

@ -68,6 +68,7 @@
#include "nsCSSValue.h"
#include "nsIRunnable.h"
#include "nsThreadUtils.h"
#include "nsDOMEventTargetWrapperCache.h"
// General helper includes
#include "nsGlobalWindow.h"
@ -7689,7 +7690,8 @@ NS_IMETHODIMP
nsEventTargetSH::PreCreate(nsISupports *nativeObj, JSContext *cx,
JSObject *globalObj, JSObject **parentObj)
{
nsXHREventTarget *target = nsXHREventTarget::FromSupports(nativeObj);
nsDOMEventTargetWrapperCache *target =
nsDOMEventTargetWrapperCache::FromSupports(nativeObj);
nsCOMPtr<nsIScriptGlobalObject> native_parent;
target->GetParentObject(getter_AddRefs(native_parent));
@ -7715,7 +7717,8 @@ nsEventTargetSH::AddProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
void
nsEventTargetSH::PreserveWrapper(nsISupports *aNative)
{
nsXHREventTarget *target = nsXHREventTarget::FromSupports(aNative);
nsDOMEventTargetWrapperCache *target =
nsDOMEventTargetWrapperCache::FromSupports(aNative);
nsContentUtils::PreserveWrapper(aNative, target);
}