Trunk patch for Bug 491134, r+sr=jst

This commit is contained in:
Olli Pettay 2009-06-15 11:27:29 +03:00
parent c7b86bd0ba
commit 42ec618ee6
15 changed files with 627 additions and 715 deletions

View File

@ -57,6 +57,9 @@ _TEST_FILES = test_feed_discovery.html \
offlineChild2.html \ offlineChild2.html \
offlineChild2.cacheManifest \ offlineChild2.cacheManifest \
offlineChild2.cacheManifest^headers^ \ offlineChild2.cacheManifest^headers^ \
offlineEvent.html \
offlineEvent.cacheManifest \
offlineEvent.cacheManifest^headers^ \
$(NULL) $(NULL)
# The following tests are disabled because they are unreliable: # The following tests are disabled because they are unreliable:

View File

@ -0,0 +1,2 @@
CACHE MANIFEST
offlineChild.html

View File

@ -0,0 +1 @@
Content-Type: text/cache-manifest

View File

@ -0,0 +1,9 @@
<html manifest="offlineEvent.cacheManifest">
<head>
<title></title>
</head>
<body>
<h1>Child</h1>
</body>
</html>

View File

@ -19,6 +19,8 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=462856
notification --> notification -->
<iframe name="testFrame3" src="http://example.com/tests/browser/base/content/test/offlineChild.html"></iframe> <iframe name="testFrame3" src="http://example.com/tests/browser/base/content/test/offlineChild.html"></iframe>
<iframe id="eventsTestFrame" src="offlineEvent.html"></iframe>
<div id="content" style="display: none"> <div id="content" style="display: none">
</div> </div>
<pre id="test"> <pre id="test">
@ -44,7 +46,55 @@ window.addEventListener("message", function(event) {
} }
}, false); }, false);
var count = 0;
var expectedEvent = "";
function eventHandler(evt) {
++count;
is(evt.type, expectedEvent, "Wrong event!");
}
function testEventHandling() {
var events = [ "checking",
"error",
"noupdate",
"downloading",
"progress",
"updateready",
"cached",
"obsolete"];
var w = document.getElementById("eventsTestFrame").contentWindow;
var e;
for (var i = 0; i < events.length; ++i) {
count = 0;
expectedEvent = events[i];
e = w.document.createEvent("event");
e.initEvent(expectedEvent, true, true);
w.applicationCache["on" + expectedEvent] = eventHandler;
w.applicationCache.addEventListener(expectedEvent, eventHandler, true);
w.applicationCache.dispatchEvent(e);
is(count, 2, "Wrong number events!");
w.applicationCache["on" + expectedEvent] = null;
w.applicationCache.removeEventListener(expectedEvent, eventHandler, true);
w.applicationCache.dispatchEvent(e);
is(count, 2, "Wrong number events!");
}
// Test some random event.
count = 0;
expectedEvent = "foo";
e = w.document.createEvent("event");
e.initEvent(expectedEvent, true, true);
w.applicationCache.addEventListener(expectedEvent, eventHandler, true);
w.applicationCache.dispatchEvent(e);
is(count, 1, "Wrong number events!");
w.applicationCache.removeEventListener(expectedEvent, eventHandler, true);
w.applicationCache.dispatchEvent(e);
is(count, 1, "Wrong number events!");
}
function loaded() { function loaded() {
testEventHandling();
// Click the notification bar's "Allow" button. This should kick // Click the notification bar's "Allow" button. This should kick
// off updates, which will eventually lead to getting messages from // off updates, which will eventually lead to getting messages from
// the children. // the children.

View File

@ -1394,6 +1394,16 @@ public:
nsString& aOrigin); nsString& aOrigin);
static nsresult GetUTFOrigin(nsIURI* aURI, nsString& aOrigin); static nsresult GetUTFOrigin(nsIURI* aURI, nsString& aOrigin);
/**
* Gets the nsIDocument given the script context. Will return nsnull on failure.
*
* @param aScriptContext the script context to get the document for; can be null
*
* @return the document associated with the script context
*/
static already_AddRefed<nsIDocument>
GetDocumentFromScriptContext(nsIScriptContext *aScriptContext);
private: private:
static PRBool InitializeEventTable(); static PRBool InitializeEventTable();

View File

@ -4800,6 +4800,27 @@ nsContentUtils::GetUTFOrigin(nsIURI* aURI, nsString& aOrigin)
return NS_OK; return NS_OK;
} }
/* static */
already_AddRefed<nsIDocument>
nsContentUtils::GetDocumentFromScriptContext(nsIScriptContext *aScriptContext)
{
if (!aScriptContext)
return nsnull;
nsCOMPtr<nsIDOMWindow> window =
do_QueryInterface(aScriptContext->GetGlobalObject());
nsIDocument *doc = nsnull;
if (window) {
nsCOMPtr<nsIDOMDocument> domdoc;
window->GetDocument(getter_AddRefs(domdoc));
if (domdoc) {
CallQueryInterface(domdoc, &doc);
}
}
return doc;
}
nsContentTypeParser::nsContentTypeParser(const nsAString& aString) nsContentTypeParser::nsContentTypeParser(const nsAString& aString)
: mString(aString), mService(nsnull) : mString(aString), mService(nsnull)
{ {

View File

@ -160,28 +160,6 @@ private:
nsCOMPtr<nsPIDOMWindow> mWindow; nsCOMPtr<nsPIDOMWindow> mWindow;
}; };
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMEventListenerWrapper)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMEventListenerWrapper)
NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
NS_INTERFACE_MAP_END_AGGREGATED(mListener)
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMEventListenerWrapper)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMEventListenerWrapper)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDOMEventListenerWrapper)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mListener)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMEventListenerWrapper)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mListener)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMETHODIMP
nsDOMEventListenerWrapper::HandleEvent(nsIDOMEvent* aEvent)
{
return mListener->HandleEvent(aEvent);
}
// This helper function adds the given load flags to the request's existing // This helper function adds the given load flags to the request's existing
// load flags. // load flags.
@ -515,175 +493,37 @@ nsACProxyListener::GetInterface(const nsIID & aIID, void **aResult)
return QueryInterface(aIID, aResult); return QueryInterface(aIID, aResult);
} }
/**
* Gets the nsIDocument given the script context. Will return nsnull on failure.
*
* @param aScriptContext the script context to get the document for; can be null
*
* @return the document associated with the script context
*/
static already_AddRefed<nsIDocument>
GetDocumentFromScriptContext(nsIScriptContext *aScriptContext)
{
if (!aScriptContext)
return nsnull;
nsCOMPtr<nsIDOMWindow> window =
do_QueryInterface(aScriptContext->GetGlobalObject());
nsIDocument *doc = nsnull;
if (window) {
nsCOMPtr<nsIDOMDocument> domdoc;
window->GetDocument(getter_AddRefs(domdoc));
if (domdoc) {
CallQueryInterface(domdoc, &doc);
}
}
return doc;
}
///////////////////////////////////////////// /////////////////////////////////////////////
NS_IMPL_CYCLE_COLLECTION_CLASS(nsXHREventTarget) NS_IMPL_CYCLE_COLLECTION_CLASS(nsXHREventTarget)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXHREventTarget) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsXHREventTarget,
nsDOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_PRESERVED_WRAPPER NS_IMPL_CYCLE_COLLECTION_TRAVERSE_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnLoadListener) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnLoadListener)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnErrorListener) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnErrorListener)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnAbortListener) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnAbortListener)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnLoadStartListener) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnLoadStartListener)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnProgressListener) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnProgressListener)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mListenerManager)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mScriptContext)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOwner)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXHREventTarget) NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsXHREventTarget,
nsDOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnLoadListener) NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnLoadListener)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnErrorListener) NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnErrorListener)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnAbortListener) NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnAbortListener)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnLoadStartListener) NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnLoadStartListener)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnProgressListener) NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnProgressListener)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mListenerManager)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mScriptContext)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOwner)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN(nsXHREventTarget) NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsXHREventTarget)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(nsXHREventTarget)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIXMLHttpRequestEventTarget)
NS_INTERFACE_MAP_ENTRY(nsIXMLHttpRequestEventTarget) NS_INTERFACE_MAP_ENTRY(nsIXMLHttpRequestEventTarget)
NS_INTERFACE_MAP_ENTRY(nsPIDOMEventTarget) NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget)
NS_INTERFACE_MAP_ENTRY(nsIDOMNSEventTarget)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF_AMBIGUOUS(nsXHREventTarget, NS_IMPL_ADDREF_INHERITED(nsXHREventTarget, nsDOMEventTargetHelper)
nsIXMLHttpRequestEventTarget) NS_IMPL_RELEASE_INHERITED(nsXHREventTarget, nsDOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTING_RELEASE_AMBIGUOUS(nsXHREventTarget,
nsIXMLHttpRequestEventTarget)
NS_IMETHODIMP
nsXHREventTarget::AddEventListener(const nsAString& aType,
nsIDOMEventListener* aListener,
PRBool aUseCapture)
{
nsresult rv;
nsIScriptContext* context =
GetContextForEventHandlers(&rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDocument> doc = GetDocumentFromScriptContext(context);
PRBool wantsUntrusted = doc && !nsContentUtils::IsChromeDoc(doc);
return AddEventListener(aType, aListener, aUseCapture, wantsUntrusted);
}
NS_IMETHODIMP
nsXHREventTarget::RemoveEventListener(const nsAString& aType,
nsIDOMEventListener* aListener,
PRBool aUseCapture)
{
nsCOMPtr<nsIEventListenerManager> elm;
GetListenerManager(PR_FALSE, getter_AddRefs(elm));
if (elm) {
PRInt32 flags = aUseCapture ? NS_EVENT_FLAG_CAPTURE : NS_EVENT_FLAG_BUBBLE;
elm->RemoveEventListenerByType(aListener, aType, flags, nsnull);
}
return NS_OK;
}
NS_IMETHODIMP
nsXHREventTarget::AddEventListener(const nsAString& aType,
nsIDOMEventListener *aListener,
PRBool aUseCapture,
PRBool aWantsUntrusted)
{
nsCOMPtr<nsIEventListenerManager> elm;
GetListenerManager(PR_TRUE, getter_AddRefs(elm));
NS_ENSURE_STATE(elm);
PRInt32 flags = aUseCapture ? NS_EVENT_FLAG_CAPTURE : NS_EVENT_FLAG_BUBBLE;
if (aWantsUntrusted) {
flags |= NS_PRIV_EVENT_UNTRUSTED_PERMITTED;
}
return elm->AddEventListenerByType(aListener, aType, flags, nsnull);
}
NS_IMETHODIMP
nsXHREventTarget::GetScriptTypeID(PRUint32 *aLang)
{
*aLang = mLang;
return NS_OK;
}
NS_IMETHODIMP
nsXHREventTarget::SetScriptTypeID(PRUint32 aLang)
{
mLang = aLang;
return NS_OK;
}
NS_IMETHODIMP
nsXHREventTarget::DispatchEvent(nsIDOMEvent* aEvent, PRBool* aRetVal)
{
nsEventStatus status = nsEventStatus_eIgnore;
nsresult rv =
nsEventDispatcher::DispatchDOMEvent(static_cast<nsPIDOMEventTarget*>(this),
nsnull, aEvent, nsnull, &status);
*aRetVal = (status != nsEventStatus_eConsumeNoDefault);
return rv;
}
nsresult
nsXHREventTarget::RemoveAddEventListener(const nsAString& aType,
nsRefPtr<nsDOMEventListenerWrapper>& aCurrent,
nsIDOMEventListener* aNew)
{
if (aCurrent) {
RemoveEventListener(aType, aCurrent, PR_FALSE);
aCurrent = nsnull;
}
if (aNew) {
aCurrent = new nsDOMEventListenerWrapper(aNew);
NS_ENSURE_TRUE(aCurrent, NS_ERROR_OUT_OF_MEMORY);
AddEventListener(aType, aCurrent, PR_FALSE);
}
return NS_OK;
}
nsresult
nsXHREventTarget::GetInnerEventListener(nsRefPtr<nsDOMEventListenerWrapper>& aWrapper,
nsIDOMEventListener** aListener)
{
NS_ENSURE_ARG_POINTER(aListener);
if (aWrapper) {
NS_ADDREF(*aListener = aWrapper->GetInner());
} else {
*aListener = nsnull;
}
return NS_OK;
}
NS_IMETHODIMP NS_IMETHODIMP
nsXHREventTarget::GetOnload(nsIDOMEventListener** aOnLoad) nsXHREventTarget::GetOnload(nsIDOMEventListener** aOnLoad)
@ -750,94 +590,6 @@ nsXHREventTarget::SetOnprogress(nsIDOMEventListener* aOnprogress)
mOnProgressListener, aOnprogress); mOnProgressListener, aOnprogress);
} }
nsresult
nsXHREventTarget::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
{
aVisitor.mCanHandle = PR_TRUE;
aVisitor.mParentTarget = nsnull;
return NS_OK;
}
nsresult
nsXHREventTarget::PostHandleEvent(nsEventChainPostVisitor& aVisitor)
{
return NS_OK;
}
nsresult
nsXHREventTarget::DispatchDOMEvent(nsEvent* aEvent, nsIDOMEvent* aDOMEvent,
nsPresContext* aPresContext,
nsEventStatus* aEventStatus)
{
return
nsEventDispatcher::DispatchDOMEvent(static_cast<nsPIDOMEventTarget*>(this),
aEvent, aDOMEvent, aPresContext,
aEventStatus);
}
nsresult
nsXHREventTarget::GetListenerManager(PRBool aCreateIfNotFound,
nsIEventListenerManager** aResult)
{
if (!mListenerManager) {
if (!aCreateIfNotFound) {
*aResult = nsnull;
return NS_OK;
}
nsresult rv = NS_NewEventListenerManager(getter_AddRefs(mListenerManager));
NS_ENSURE_SUCCESS(rv, rv);
mListenerManager->SetListenerTarget(static_cast<nsPIDOMEventTarget*>(this));
}
NS_ADDREF(*aResult = mListenerManager);
return NS_OK;
}
nsresult
nsXHREventTarget::AddEventListenerByIID(nsIDOMEventListener *aListener,
const nsIID& aIID)
{
nsCOMPtr<nsIEventListenerManager> elm;
GetListenerManager(PR_TRUE, getter_AddRefs(elm));
if (elm) {
elm->AddEventListenerByIID(aListener, aIID, NS_EVENT_FLAG_BUBBLE);
}
return NS_OK;
}
nsresult
nsXHREventTarget::RemoveEventListenerByIID(nsIDOMEventListener *aListener,
const nsIID& aIID)
{
nsCOMPtr<nsIEventListenerManager> elm;
GetListenerManager(PR_FALSE, getter_AddRefs(elm));
if (elm) {
return elm->RemoveEventListenerByIID(aListener, aIID, NS_EVENT_FLAG_BUBBLE);
}
return NS_OK;
}
nsresult
nsXHREventTarget::GetSystemEventGroup(nsIDOMEventGroup** aGroup)
{
nsCOMPtr<nsIEventListenerManager> elm;
nsresult rv = GetListenerManager(PR_TRUE, getter_AddRefs(elm));
if (elm) {
return elm->GetSystemEventGroupLM(aGroup);
}
return rv;
}
nsIScriptContext*
nsXHREventTarget::GetContextForEventHandlers(nsresult* aRv)
{
*aRv = CheckInnerWindowCorrectness();
if (NS_FAILED(*aRv)) {
return nsnull;
}
return mScriptContext;
}
///////////////////////////////////////////// /////////////////////////////////////////////
nsXMLHttpRequestUpload::~nsXMLHttpRequestUpload() nsXMLHttpRequestUpload::~nsXMLHttpRequestUpload()
@ -1666,7 +1418,8 @@ nsXMLHttpRequest::GetLoadGroup(nsILoadGroup **aLoadGroup)
return NS_OK; return NS_OK;
} }
nsCOMPtr<nsIDocument> doc = GetDocumentFromScriptContext(mScriptContext); nsCOMPtr<nsIDocument> doc =
nsContentUtils::GetDocumentFromScriptContext(mScriptContext);
if (doc) { if (doc) {
*aLoadGroup = doc->GetDocumentLoadGroup().get(); // already_AddRefed *aLoadGroup = doc->GetDocumentLoadGroup().get(); // already_AddRefed
} }
@ -1893,7 +1646,8 @@ nsXMLHttpRequest::OpenRequest(const nsACString& method,
mState &= ~XML_HTTP_REQUEST_MPART_HEADERS; mState &= ~XML_HTTP_REQUEST_MPART_HEADERS;
nsCOMPtr<nsIDocument> doc = GetDocumentFromScriptContext(mScriptContext); nsCOMPtr<nsIDocument> doc =
nsContentUtils::GetDocumentFromScriptContext(mScriptContext);
nsCOMPtr<nsIURI> baseURI; nsCOMPtr<nsIURI> baseURI;
if (mBaseURI) { if (mBaseURI) {
@ -2219,7 +1973,8 @@ nsXMLHttpRequest::OnStartRequest(nsIRequest *request, nsISupports *ctxt)
if (mState & XML_HTTP_REQUEST_PARSEBODY) { if (mState & XML_HTTP_REQUEST_PARSEBODY) {
nsCOMPtr<nsIURI> baseURI, docURI; nsCOMPtr<nsIURI> baseURI, docURI;
nsCOMPtr<nsIDocument> doc = GetDocumentFromScriptContext(mScriptContext); nsCOMPtr<nsIDocument> doc =
nsContentUtils::GetDocumentFromScriptContext(mScriptContext);
if (doc) { if (doc) {
docURI = doc->GetDocumentURI(); docURI = doc->GetDocumentURI();

View File

@ -40,11 +40,8 @@
#include "nsIXMLHttpRequest.h" #include "nsIXMLHttpRequest.h"
#include "nsISupportsUtils.h" #include "nsISupportsUtils.h"
#include "nsCOMPtr.h"
#include "nsString.h" #include "nsString.h"
#include "nsIDOMLoadListener.h" #include "nsIDOMLoadListener.h"
#include "nsIDOMEventTarget.h"
#include "nsIDOMNSEventTarget.h"
#include "nsIDOMDocument.h" #include "nsIDOMDocument.h"
#include "nsIURI.h" #include "nsIURI.h"
#include "nsIHttpChannel.h" #include "nsIHttpChannel.h"
@ -60,20 +57,17 @@
#include "nsCOMArray.h" #include "nsCOMArray.h"
#include "nsJSUtils.h" #include "nsJSUtils.h"
#include "nsTArray.h" #include "nsTArray.h"
#include "nsCycleCollectionParticipant.h"
#include "nsIJSNativeInitializer.h" #include "nsIJSNativeInitializer.h"
#include "nsPIDOMWindow.h"
#include "nsIDOMLSProgressEvent.h" #include "nsIDOMLSProgressEvent.h"
#include "nsClassHashtable.h" #include "nsClassHashtable.h"
#include "nsHashKeys.h" #include "nsHashKeys.h"
#include "prclist.h" #include "prclist.h"
#include "prtime.h" #include "prtime.h"
#include "nsIEventListenerManager.h"
#include "nsIDOMNSEvent.h" #include "nsIDOMNSEvent.h"
#include "nsITimer.h" #include "nsITimer.h"
#include "nsIPrivateDOMEvent.h" #include "nsIPrivateDOMEvent.h"
#include "nsDOMProgressEvent.h" #include "nsDOMProgressEvent.h"
#include "nsIScriptGlobalObject.h" #include "nsDOMEventTargetHelper.h"
class nsILoadGroup; class nsILoadGroup;
@ -143,73 +137,18 @@ private:
PRCList mList; PRCList mList;
}; };
class nsDOMEventListenerWrapper : public nsIDOMEventListener class nsXHREventTarget : public nsDOMEventTargetHelper,
{ public nsIXMLHttpRequestEventTarget,
public:
nsDOMEventListenerWrapper(nsIDOMEventListener* aListener)
: mListener(aListener) {}
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(nsDOMEventListenerWrapper)
NS_DECL_NSIDOMEVENTLISTENER
nsIDOMEventListener* GetInner() { return mListener; }
protected:
nsCOMPtr<nsIDOMEventListener> mListener;
};
class nsXHREventTarget : public nsIXMLHttpRequestEventTarget,
public nsPIDOMEventTarget,
public nsIDOMNSEventTarget,
public nsWrapperCache public nsWrapperCache
{ {
public: public:
nsXHREventTarget() : mLang(nsIProgrammingLanguage::JAVASCRIPT) {}
virtual ~nsXHREventTarget() {} virtual ~nsXHREventTarget() {}
NS_DECL_CYCLE_COLLECTING_ISUPPORTS NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsXHREventTarget, NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsXHREventTarget,
nsIXMLHttpRequestEventTarget) nsDOMEventTargetHelper)
NS_DECL_NSIDOMNSEVENTTARGET
NS_DECL_NSIXMLHTTPREQUESTEVENTTARGET NS_DECL_NSIXMLHTTPREQUESTEVENTTARGET
NS_DECL_NSIDOMEVENTTARGET NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetHelper::)
// nsPIDOMEventTarget NS_FORWARD_NSIDOMNSEVENTTARGET(nsDOMEventTargetHelper::)
virtual nsresult PreHandleEvent(nsEventChainPreVisitor& aVisitor);
virtual nsresult PostHandleEvent(nsEventChainPostVisitor& aVisitor);
virtual nsresult DispatchDOMEvent(nsEvent* aEvent, nsIDOMEvent* aDOMEvent,
nsPresContext* aPresContext,
nsEventStatus* aEventStatus);
virtual nsresult GetListenerManager(PRBool aCreateIfNotFound,
nsIEventListenerManager** aResult);
virtual nsresult AddEventListenerByIID(nsIDOMEventListener *aListener,
const nsIID& aIID);
virtual nsresult RemoveEventListenerByIID(nsIDOMEventListener *aListener,
const nsIID& aIID);
virtual nsresult GetSystemEventGroup(nsIDOMEventGroup** aGroup);
virtual nsIScriptContext* GetContextForEventHandlers(nsresult* aRv);
PRBool HasListenersFor(const nsAString& aType)
{
return mListenerManager && mListenerManager->HasListenersFor(aType);
}
nsresult RemoveAddEventListener(const nsAString& aType,
nsRefPtr<nsDOMEventListenerWrapper>& aCurrent,
nsIDOMEventListener* aNew);
nsresult GetInnerEventListener(nsRefPtr<nsDOMEventListenerWrapper>& aWrapper,
nsIDOMEventListener** aListener);
nsresult CheckInnerWindowCorrectness()
{
if (mOwner) {
NS_ASSERTION(mOwner->IsInnerWindow(), "Should have inner window here!\n");
nsPIDOMWindow* outer = mOwner->GetOuterWindow();
if (!outer || outer->GetCurrentInnerWindow() != mOwner) {
return NS_ERROR_FAILURE;
}
}
return NS_OK;
}
void GetParentObject(nsIScriptGlobalObject **aParentObject) void GetParentObject(nsIScriptGlobalObject **aParentObject)
{ {
@ -223,15 +162,15 @@ public:
static nsXHREventTarget* FromSupports(nsISupports* aSupports) static nsXHREventTarget* FromSupports(nsISupports* aSupports)
{ {
nsIXMLHttpRequestEventTarget* target = nsPIDOMEventTarget* target =
static_cast<nsIXMLHttpRequestEventTarget*>(aSupports); static_cast<nsPIDOMEventTarget*>(aSupports);
#ifdef DEBUG #ifdef DEBUG
{ {
nsCOMPtr<nsIXMLHttpRequestEventTarget> target_qi = nsCOMPtr<nsPIDOMEventTarget> target_qi =
do_QueryInterface(aSupports); do_QueryInterface(aSupports);
// If this assertion fires the QI implementation for the object in // If this assertion fires the QI implementation for the object in
// question doesn't use the nsIXMLHttpRequestEventTarget pointer as the // question doesn't use the nsPIDOMEventTarget pointer as the
// nsISupports pointer. That must be fixed, or we'll crash... // nsISupports pointer. That must be fixed, or we'll crash...
NS_ASSERTION(target_qi == target, "Uh, fix QI!"); NS_ASSERTION(target_qi == target, "Uh, fix QI!");
} }
@ -246,11 +185,6 @@ protected:
nsRefPtr<nsDOMEventListenerWrapper> mOnAbortListener; nsRefPtr<nsDOMEventListenerWrapper> mOnAbortListener;
nsRefPtr<nsDOMEventListenerWrapper> mOnLoadStartListener; nsRefPtr<nsDOMEventListenerWrapper> mOnLoadStartListener;
nsRefPtr<nsDOMEventListenerWrapper> mOnProgressListener; nsRefPtr<nsDOMEventListenerWrapper> mOnProgressListener;
nsCOMPtr<nsIEventListenerManager> mListenerManager;
PRUint32 mLang;
// These may be null (native callers or xpcshell).
nsCOMPtr<nsIScriptContext> mScriptContext;
nsCOMPtr<nsPIDOMWindow> mOwner; // Inner window.
}; };
class nsXMLHttpRequestUpload : public nsXHREventTarget, class nsXMLHttpRequestUpload : public nsXHREventTarget,
@ -335,6 +269,8 @@ public:
NS_IMETHOD Initialize(nsISupports* aOwner, JSContext* cx, JSObject* obj, NS_IMETHOD Initialize(nsISupports* aOwner, JSContext* cx, JSObject* obj,
PRUint32 argc, jsval* argv); PRUint32 argc, jsval* argv);
NS_FORWARD_NSIDOMEVENTTARGET(nsXHREventTarget::)
NS_FORWARD_NSIDOMNSEVENTTARGET(nsXHREventTarget::)
// This creates a trusted readystatechange event, which is not cancelable and // This creates a trusted readystatechange event, which is not cancelable and
// doesn't bubble. // doesn't bubble.

View File

@ -96,6 +96,7 @@ CPPSRCS = \
nsDOMDataTransfer.cpp \ nsDOMDataTransfer.cpp \
nsDOMNotifyPaintEvent.cpp \ nsDOMNotifyPaintEvent.cpp \
nsDOMSimpleGestureEvent.cpp \ nsDOMSimpleGestureEvent.cpp \
nsDOMEventTargetHelper.cpp \
$(NULL) $(NULL)
# we don't want the shared lib, but we want to force the creation of a static lib. # we don't want the shared lib, but we want to force the creation of a static lib.

View File

@ -0,0 +1,285 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** 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
* Mozilla Corporation
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Olli Pettay <Olli.Pettay@helsinki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either 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 "nsDOMEventTargetHelper.h"
#include "nsContentUtils.h"
#include "nsEventDispatcher.h"
#include "nsGUIEvent.h"
#include "nsIDocument.h"
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMEventListenerWrapper)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMEventListenerWrapper)
NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
NS_INTERFACE_MAP_END_AGGREGATED(mListener)
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMEventListenerWrapper)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMEventListenerWrapper)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDOMEventListenerWrapper)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mListener)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMEventListenerWrapper)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mListener)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMETHODIMP
nsDOMEventListenerWrapper::HandleEvent(nsIDOMEvent* aEvent)
{
return mListener->HandleEvent(aEvent);
}
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mListenerManager)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mScriptContext)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOwner)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mListenerManager)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mScriptContext)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOwner)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMEventTargetHelper)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsPIDOMEventTarget)
NS_INTERFACE_MAP_ENTRY(nsPIDOMEventTarget)
NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget)
NS_INTERFACE_MAP_ENTRY(nsIDOMNSEventTarget)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF_AMBIGUOUS(nsDOMEventTargetHelper,
nsPIDOMEventTarget)
NS_IMPL_CYCLE_COLLECTING_RELEASE_AMBIGUOUS(nsDOMEventTargetHelper,
nsPIDOMEventTarget)
NS_IMETHODIMP
nsDOMEventTargetHelper::AddEventListener(const nsAString& aType,
nsIDOMEventListener* aListener,
PRBool aUseCapture)
{
nsresult rv;
nsIScriptContext* context =
GetContextForEventHandlers(&rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromScriptContext(context);
PRBool wantsUntrusted = doc && !nsContentUtils::IsChromeDoc(doc);
return AddEventListener(aType, aListener, aUseCapture, wantsUntrusted);
}
NS_IMETHODIMP
nsDOMEventTargetHelper::RemoveEventListener(const nsAString& aType,
nsIDOMEventListener* aListener,
PRBool aUseCapture)
{
nsCOMPtr<nsIEventListenerManager> elm;
GetListenerManager(PR_FALSE, getter_AddRefs(elm));
if (elm) {
PRInt32 flags = aUseCapture ? NS_EVENT_FLAG_CAPTURE : NS_EVENT_FLAG_BUBBLE;
elm->RemoveEventListenerByType(aListener, aType, flags, nsnull);
}
return NS_OK;
}
NS_IMETHODIMP
nsDOMEventTargetHelper::AddEventListener(const nsAString& aType,
nsIDOMEventListener *aListener,
PRBool aUseCapture,
PRBool aWantsUntrusted)
{
nsCOMPtr<nsIEventListenerManager> elm;
GetListenerManager(PR_TRUE, getter_AddRefs(elm));
NS_ENSURE_STATE(elm);
PRInt32 flags = aUseCapture ? NS_EVENT_FLAG_CAPTURE : NS_EVENT_FLAG_BUBBLE;
if (aWantsUntrusted) {
flags |= NS_PRIV_EVENT_UNTRUSTED_PERMITTED;
}
return elm->AddEventListenerByType(aListener, aType, flags, nsnull);
}
NS_IMETHODIMP
nsDOMEventTargetHelper::GetScriptTypeID(PRUint32 *aLang)
{
*aLang = mLang;
return NS_OK;
}
NS_IMETHODIMP
nsDOMEventTargetHelper::SetScriptTypeID(PRUint32 aLang)
{
mLang = aLang;
return NS_OK;
}
NS_IMETHODIMP
nsDOMEventTargetHelper::DispatchEvent(nsIDOMEvent* aEvent, PRBool* aRetVal)
{
nsEventStatus status = nsEventStatus_eIgnore;
nsresult rv =
nsEventDispatcher::DispatchDOMEvent(static_cast<nsPIDOMEventTarget*>(this),
nsnull, aEvent, nsnull, &status);
*aRetVal = (status != nsEventStatus_eConsumeNoDefault);
return rv;
}
nsresult
nsDOMEventTargetHelper::RemoveAddEventListener(const nsAString& aType,
nsRefPtr<nsDOMEventListenerWrapper>& aCurrent,
nsIDOMEventListener* aNew)
{
if (aCurrent) {
RemoveEventListener(aType, aCurrent, PR_FALSE);
aCurrent = nsnull;
}
if (aNew) {
aCurrent = new nsDOMEventListenerWrapper(aNew);
NS_ENSURE_TRUE(aCurrent, NS_ERROR_OUT_OF_MEMORY);
AddEventListener(aType, aCurrent, PR_FALSE);
}
return NS_OK;
}
nsresult
nsDOMEventTargetHelper::GetInnerEventListener(nsRefPtr<nsDOMEventListenerWrapper>& aWrapper,
nsIDOMEventListener** aListener)
{
NS_ENSURE_ARG_POINTER(aListener);
if (aWrapper) {
NS_ADDREF(*aListener = aWrapper->GetInner());
} else {
*aListener = nsnull;
}
return NS_OK;
}
nsresult
nsDOMEventTargetHelper::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
{
aVisitor.mCanHandle = PR_TRUE;
aVisitor.mParentTarget = nsnull;
return NS_OK;
}
nsresult
nsDOMEventTargetHelper::PostHandleEvent(nsEventChainPostVisitor& aVisitor)
{
return NS_OK;
}
nsresult
nsDOMEventTargetHelper::DispatchDOMEvent(nsEvent* aEvent,
nsIDOMEvent* aDOMEvent,
nsPresContext* aPresContext,
nsEventStatus* aEventStatus)
{
return
nsEventDispatcher::DispatchDOMEvent(static_cast<nsPIDOMEventTarget*>(this),
aEvent, aDOMEvent, aPresContext,
aEventStatus);
}
nsresult
nsDOMEventTargetHelper::GetListenerManager(PRBool aCreateIfNotFound,
nsIEventListenerManager** aResult)
{
if (!mListenerManager) {
if (!aCreateIfNotFound) {
*aResult = nsnull;
return NS_OK;
}
nsresult rv = NS_NewEventListenerManager(getter_AddRefs(mListenerManager));
NS_ENSURE_SUCCESS(rv, rv);
mListenerManager->SetListenerTarget(static_cast<nsPIDOMEventTarget*>(this));
}
NS_ADDREF(*aResult = mListenerManager);
return NS_OK;
}
nsresult
nsDOMEventTargetHelper::AddEventListenerByIID(nsIDOMEventListener *aListener,
const nsIID& aIID)
{
nsCOMPtr<nsIEventListenerManager> elm;
GetListenerManager(PR_TRUE, getter_AddRefs(elm));
if (elm) {
elm->AddEventListenerByIID(aListener, aIID, NS_EVENT_FLAG_BUBBLE);
}
return NS_OK;
}
nsresult
nsDOMEventTargetHelper::RemoveEventListenerByIID(nsIDOMEventListener *aListener,
const nsIID& aIID)
{
nsCOMPtr<nsIEventListenerManager> elm;
GetListenerManager(PR_FALSE, getter_AddRefs(elm));
if (elm) {
return elm->RemoveEventListenerByIID(aListener, aIID, NS_EVENT_FLAG_BUBBLE);
}
return NS_OK;
}
nsresult
nsDOMEventTargetHelper::GetSystemEventGroup(nsIDOMEventGroup** aGroup)
{
nsCOMPtr<nsIEventListenerManager> elm;
nsresult rv = GetListenerManager(PR_TRUE, getter_AddRefs(elm));
if (elm) {
return elm->GetSystemEventGroupLM(aGroup);
}
return rv;
}
nsIScriptContext*
nsDOMEventTargetHelper::GetContextForEventHandlers(nsresult* aRv)
{
*aRv = CheckInnerWindowCorrectness();
if (NS_FAILED(*aRv)) {
return nsnull;
}
return mScriptContext;
}

View File

@ -0,0 +1,127 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** 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
* Mozilla Corporation
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Olli Pettay <Olli.Pettay@helsinki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either 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 nsDOMEventTargetHelper_h_
#define nsDOMEventTargetHelper_h_
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "nsPIDOMEventTarget.h"
#include "nsIDOMEventListener.h"
#include "nsIDOMNSEventTarget.h"
#include "nsIDOMEventTarget.h"
#include "nsCycleCollectionParticipant.h"
#include "nsPIDOMWindow.h"
#include "nsIScriptGlobalObject.h"
#include "nsIEventListenerManager.h"
class nsDOMEventListenerWrapper : public nsIDOMEventListener
{
public:
nsDOMEventListenerWrapper(nsIDOMEventListener* aListener)
: mListener(aListener) {}
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(nsDOMEventListenerWrapper)
NS_DECL_NSIDOMEVENTLISTENER
nsIDOMEventListener* GetInner() { return mListener; }
protected:
nsCOMPtr<nsIDOMEventListener> mListener;
};
class nsDOMEventTargetHelper : public nsPIDOMEventTarget,
public nsIDOMEventTarget,
public nsIDOMNSEventTarget
{
public:
nsDOMEventTargetHelper() : mLang(nsIProgrammingLanguage::JAVASCRIPT) {}
virtual ~nsDOMEventTargetHelper() {}
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsDOMEventTargetHelper,
nsPIDOMEventTarget)
NS_DECL_NSIDOMNSEVENTTARGET
NS_DECL_NSIDOMEVENTTARGET
// nsPIDOMEventTarget
virtual nsresult PreHandleEvent(nsEventChainPreVisitor& aVisitor);
virtual nsresult PostHandleEvent(nsEventChainPostVisitor& aVisitor);
virtual nsresult DispatchDOMEvent(nsEvent* aEvent, nsIDOMEvent* aDOMEvent,
nsPresContext* aPresContext,
nsEventStatus* aEventStatus);
virtual nsresult GetListenerManager(PRBool aCreateIfNotFound,
nsIEventListenerManager** aResult);
virtual nsresult AddEventListenerByIID(nsIDOMEventListener *aListener,
const nsIID& aIID);
virtual nsresult RemoveEventListenerByIID(nsIDOMEventListener *aListener,
const nsIID& aIID);
virtual nsresult GetSystemEventGroup(nsIDOMEventGroup** aGroup);
virtual nsIScriptContext* GetContextForEventHandlers(nsresult* aRv);
PRBool HasListenersFor(const nsAString& aType)
{
return mListenerManager && mListenerManager->HasListenersFor(aType);
}
nsresult RemoveAddEventListener(const nsAString& aType,
nsRefPtr<nsDOMEventListenerWrapper>& aCurrent,
nsIDOMEventListener* aNew);
nsresult GetInnerEventListener(nsRefPtr<nsDOMEventListenerWrapper>& aWrapper,
nsIDOMEventListener** aListener);
nsresult CheckInnerWindowCorrectness()
{
if (mOwner) {
NS_ASSERTION(mOwner->IsInnerWindow(), "Should have inner window here!\n");
nsPIDOMWindow* outer = mOwner->GetOuterWindow();
if (!outer || outer->GetCurrentInnerWindow() != mOwner) {
return NS_ERROR_FAILURE;
}
}
return NS_OK;
}
protected:
nsCOMPtr<nsIEventListenerManager> mListenerManager;
PRUint32 mLang;
// These may be null (native callers or xpcshell).
nsCOMPtr<nsIScriptContext> mScriptContext;
nsCOMPtr<nsPIDOMWindow> mOwner; // Inner window.
};
#endif // nsDOMEventTargetHelper_h_

View File

@ -2884,11 +2884,14 @@ nsGlobalWindow::GetApplicationCache(nsIDOMOfflineResourceList **aApplicationCach
nsCOMPtr<nsIURI> manifestURI; nsCOMPtr<nsIURI> manifestURI;
nsContentUtils::GetOfflineAppManifest(doc, getter_AddRefs(manifestURI)); nsContentUtils::GetOfflineAppManifest(doc, getter_AddRefs(manifestURI));
nsDOMOfflineResourceList* applicationCache = nsIScriptContext* scriptContext = GetContext();
new nsDOMOfflineResourceList(manifestURI, uri, this); NS_ENSURE_STATE(scriptContext);
if (!applicationCache) nsRefPtr<nsDOMOfflineResourceList> applicationCache =
return NS_ERROR_OUT_OF_MEMORY; new nsDOMOfflineResourceList(manifestURI, uri, this, scriptContext);
NS_ENSURE_TRUE(applicationCache, NS_ERROR_OUT_OF_MEMORY);
applicationCache->Init();
mApplicationCache = applicationCache; mApplicationCache = applicationCache;
} }

View File

@ -81,19 +81,10 @@ static const char kMaxEntriesPref[] = "offline.max_site_resources";
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMOfflineResourceList) NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMOfflineResourceList)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMOfflineResourceList) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsDOMOfflineResourceList,
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mWindow) nsDOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mCacheUpdate) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mCacheUpdate)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mCheckingListeners)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mErrorListeners)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mNoUpdateListeners)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mDownloadingListeners)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mProgressListeners)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mCachedListeners)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mUpdateReadyListeners)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mObsoleteListeners)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnCheckingListener) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnCheckingListener)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnErrorListener) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnErrorListener)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnNoUpdateListener) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnNoUpdateListener)
@ -103,27 +94,14 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMOfflineResourceList)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnUpdateReadyListener) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnUpdateReadyListener)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnObsoleteListener) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnObsoleteListener)
for (PRUint32 i = 0; i < tmp->mPendingEvents.Length(); i++) { NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mPendingEvents);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mPendingEvents[i].event);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mPendingEvents[i].listener);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mPendingEvents[i].listeners);
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDOMOfflineResourceList) NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsDOMOfflineResourceList,
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mWindow) nsDOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mCacheUpdate) NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mCacheUpdate)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mCheckingListeners)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mErrorListeners)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mNoUpdateListeners)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mDownloadingListeners)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mProgressListeners)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mCachedListeners)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mUpdateReadyListeners)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mObsoleteListeners)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnCheckingListener) NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnCheckingListener)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnErrorListener) NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnErrorListener)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnNoUpdateListener) NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnNoUpdateListener)
@ -133,37 +111,33 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDOMOfflineResourceList)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnUpdateReadyListener) NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnUpdateReadyListener)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnObsoleteListener) NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnObsoleteListener)
for (PRUint32 i = 0; i < tmp->mPendingEvents.Length(); i++) { NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mPendingEvents)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mPendingEvents[i].event);
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mPendingEvents[i].listener);
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mPendingEvents[i].listeners);
}
NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMOfflineResourceList) NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMOfflineResourceList)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMOfflineResourceList)
NS_INTERFACE_MAP_ENTRY(nsIDOMOfflineResourceList) NS_INTERFACE_MAP_ENTRY(nsIDOMOfflineResourceList)
NS_INTERFACE_MAP_ENTRY(nsIOfflineCacheUpdateObserver) NS_INTERFACE_MAP_ENTRY(nsIOfflineCacheUpdateObserver)
NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget)
NS_INTERFACE_MAP_ENTRY(nsIObserver) NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(OfflineResourceList) NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(OfflineResourceList)
NS_INTERFACE_MAP_END NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMOfflineResourceList) NS_IMPL_ADDREF_INHERITED(nsDOMOfflineResourceList, nsDOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMOfflineResourceList) NS_IMPL_RELEASE_INHERITED(nsDOMOfflineResourceList, nsDOMEventTargetHelper)
nsDOMOfflineResourceList::nsDOMOfflineResourceList(nsIURI *aManifestURI, nsDOMOfflineResourceList::nsDOMOfflineResourceList(nsIURI *aManifestURI,
nsIURI *aDocumentURI, nsIURI *aDocumentURI,
nsIDOMWindow *aWindow) nsPIDOMWindow *aWindow,
nsIScriptContext* aScriptContext)
: mInitialized(PR_FALSE) : mInitialized(PR_FALSE)
, mManifestURI(aManifestURI) , mManifestURI(aManifestURI)
, mDocumentURI(aDocumentURI) , mDocumentURI(aDocumentURI)
, mCachedKeys(nsnull) , mCachedKeys(nsnull)
, mCachedKeysCount(0) , mCachedKeysCount(0)
{ {
mWindow = do_GetWeakReference(aWindow); mOwner = aWindow;
mScriptContext = aScriptContext;
} }
nsDOMOfflineResourceList::~nsDOMOfflineResourceList() nsDOMOfflineResourceList::~nsDOMOfflineResourceList()
@ -234,15 +208,6 @@ nsDOMOfflineResourceList::Init()
void void
nsDOMOfflineResourceList::Disconnect() nsDOMOfflineResourceList::Disconnect()
{ {
mCheckingListeners.Clear();
mErrorListeners.Clear();
mNoUpdateListeners.Clear();
mDownloadingListeners.Clear();
mProgressListeners.Clear();
mCachedListeners.Clear();
mUpdateReadyListeners.Clear();
mObsoleteListeners.Clear();
mOnCheckingListener = nsnull; mOnCheckingListener = nsnull;
mOnErrorListener = nsnull; mOnErrorListener = nsnull;
mOnNoUpdateListener = nsnull; mOnNoUpdateListener = nsnull;
@ -253,6 +218,11 @@ nsDOMOfflineResourceList::Disconnect()
mOnObsoleteListener = nsnull; mOnObsoleteListener = nsnull;
mPendingEvents.Clear(); mPendingEvents.Clear();
if (mListenerManager) {
mListenerManager->Disconnect();
mListenerManager = nsnull;
}
} }
// //
@ -572,348 +542,128 @@ nsDOMOfflineResourceList::SwapCache()
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::GetOnchecking(nsIDOMEventListener **aOnchecking) nsDOMOfflineResourceList::GetOnchecking(nsIDOMEventListener **aOnchecking)
{ {
nsresult rv = Init(); return GetInnerEventListener(mOnCheckingListener, aOnchecking);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_ARG_POINTER(aOnchecking);
NS_IF_ADDREF(*aOnchecking = mOnCheckingListener);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::SetOnchecking(nsIDOMEventListener *aOnchecking) nsDOMOfflineResourceList::SetOnchecking(nsIDOMEventListener *aOnchecking)
{ {
nsresult rv = Init(); return RemoveAddEventListener(NS_LITERAL_STRING(CHECKING_STR),
NS_ENSURE_SUCCESS(rv, rv); mOnCheckingListener, aOnchecking);
mOnCheckingListener = aOnchecking;
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::GetOnerror(nsIDOMEventListener **aOnerror) nsDOMOfflineResourceList::GetOnerror(nsIDOMEventListener **aOnerror)
{ {
nsresult rv = Init(); return GetInnerEventListener(mOnErrorListener, aOnerror);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_ARG_POINTER(aOnerror);
NS_IF_ADDREF(*aOnerror = mOnErrorListener);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::SetOnerror(nsIDOMEventListener *aOnerror) nsDOMOfflineResourceList::SetOnerror(nsIDOMEventListener *aOnerror)
{ {
nsresult rv = Init(); return RemoveAddEventListener(NS_LITERAL_STRING(ERROR_STR), mOnErrorListener,
NS_ENSURE_SUCCESS(rv, rv); aOnerror);
mOnErrorListener = aOnerror;
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::GetOnnoupdate(nsIDOMEventListener **aOnnoupdate) nsDOMOfflineResourceList::GetOnnoupdate(nsIDOMEventListener **aOnnoupdate)
{ {
nsresult rv = Init(); return GetInnerEventListener(mOnNoUpdateListener, aOnnoupdate);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_ARG_POINTER(aOnnoupdate);
NS_IF_ADDREF(*aOnnoupdate = mOnNoUpdateListener);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::SetOnnoupdate(nsIDOMEventListener *aOnnoupdate) nsDOMOfflineResourceList::SetOnnoupdate(nsIDOMEventListener *aOnnoupdate)
{ {
nsresult rv = Init(); return RemoveAddEventListener(NS_LITERAL_STRING(NOUPDATE_STR),
NS_ENSURE_SUCCESS(rv, rv); mOnNoUpdateListener, aOnnoupdate);
mOnNoUpdateListener = aOnnoupdate;
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::GetOndownloading(nsIDOMEventListener **aOndownloading) nsDOMOfflineResourceList::GetOndownloading(nsIDOMEventListener **aOndownloading)
{ {
nsresult rv = Init(); return GetInnerEventListener(mOnDownloadingListener, aOndownloading);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_ARG_POINTER(aOndownloading);
NS_IF_ADDREF(*aOndownloading = mOnDownloadingListener);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::SetOndownloading(nsIDOMEventListener *aOndownloading) nsDOMOfflineResourceList::SetOndownloading(nsIDOMEventListener *aOndownloading)
{ {
nsresult rv = Init(); return RemoveAddEventListener(NS_LITERAL_STRING(DOWNLOADING_STR),
NS_ENSURE_SUCCESS(rv, rv); mOnDownloadingListener, aOndownloading);
mOnDownloadingListener = aOndownloading;
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::GetOnprogress(nsIDOMEventListener **aOnprogress) nsDOMOfflineResourceList::GetOnprogress(nsIDOMEventListener **aOnprogress)
{ {
nsresult rv = Init(); return GetInnerEventListener(mOnProgressListener, aOnprogress);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_ARG_POINTER(aOnprogress);
NS_IF_ADDREF(*aOnprogress = mOnProgressListener);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::SetOnprogress(nsIDOMEventListener *aOnprogress) nsDOMOfflineResourceList::SetOnprogress(nsIDOMEventListener *aOnprogress)
{ {
nsresult rv = Init(); return RemoveAddEventListener(NS_LITERAL_STRING(PROGRESS_STR),
NS_ENSURE_SUCCESS(rv, rv); mOnProgressListener, aOnprogress);
mOnProgressListener = aOnprogress;
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::GetOnupdateready(nsIDOMEventListener **aOnupdateready) nsDOMOfflineResourceList::GetOnupdateready(nsIDOMEventListener **aOnupdateready)
{ {
nsresult rv = Init(); return GetInnerEventListener(mOnUpdateReadyListener, aOnupdateready);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_ARG_POINTER(aOnupdateready);
NS_IF_ADDREF(*aOnupdateready = mOnUpdateReadyListener);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::SetOncached(nsIDOMEventListener *aOncached) nsDOMOfflineResourceList::SetOncached(nsIDOMEventListener *aOncached)
{ {
nsresult rv = Init(); return RemoveAddEventListener(NS_LITERAL_STRING(CACHED_STR),
NS_ENSURE_SUCCESS(rv, rv); mOnCachedListener, aOncached);
mOnCachedListener = aOncached;
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::GetOncached(nsIDOMEventListener **aOncached) nsDOMOfflineResourceList::GetOncached(nsIDOMEventListener **aOncached)
{ {
nsresult rv = Init(); return GetInnerEventListener(mOnCachedListener, aOncached);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_ARG_POINTER(aOncached);
NS_IF_ADDREF(*aOncached = mOnCachedListener);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::SetOnupdateready(nsIDOMEventListener *aOnupdateready) nsDOMOfflineResourceList::SetOnupdateready(nsIDOMEventListener *aOnupdateready)
{ {
nsresult rv = Init(); return RemoveAddEventListener(NS_LITERAL_STRING(UPDATEREADY_STR),
NS_ENSURE_SUCCESS(rv, rv); mOnUpdateReadyListener, aOnupdateready);
mOnUpdateReadyListener = aOnupdateready;
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::GetOnobsolete(nsIDOMEventListener **aOnobsolete) nsDOMOfflineResourceList::GetOnobsolete(nsIDOMEventListener **aOnobsolete)
{ {
nsresult rv = Init(); return GetInnerEventListener(mOnObsoleteListener, aOnobsolete);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_ARG_POINTER(aOnobsolete);
NS_IF_ADDREF(*aOnobsolete = mOnObsoleteListener);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::SetOnobsolete(nsIDOMEventListener *aOnobsolete) nsDOMOfflineResourceList::SetOnobsolete(nsIDOMEventListener *aOnobsolete)
{ {
nsresult rv = Init(); return RemoveAddEventListener(NS_LITERAL_STRING(OBSOLETE_STR),
NS_ENSURE_SUCCESS(rv, rv); mOnObsoleteListener, aOnobsolete);
mOnObsoleteListener = aOnobsolete;
return NS_OK;
}
NS_IMETHODIMP
nsDOMOfflineResourceList::AddEventListener(const nsAString& aType,
nsIDOMEventListener *aListener,
PRBool aUseCapture)
{
nsresult rv = Init();
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_ARG(aListener);
nsCOMArray<nsIDOMEventListener> *array;
#define IMPL_ADD_LISTENER(_type, _member) \
if (aType.EqualsLiteral(_type)) { \
array = &(_member); \
} else
IMPL_ADD_LISTENER(CHECKING_STR, mCheckingListeners)
IMPL_ADD_LISTENER(ERROR_STR, mErrorListeners)
IMPL_ADD_LISTENER(NOUPDATE_STR, mNoUpdateListeners)
IMPL_ADD_LISTENER(DOWNLOADING_STR, mDownloadingListeners)
IMPL_ADD_LISTENER(PROGRESS_STR, mProgressListeners)
IMPL_ADD_LISTENER(CACHED_STR, mCachedListeners)
IMPL_ADD_LISTENER(UPDATEREADY_STR, mUpdateReadyListeners)
IMPL_ADD_LISTENER(OBSOLETE_STR, mObsoleteListeners)
{
return NS_ERROR_INVALID_ARG;
}
array->AppendObject(aListener);
#undef IMPL_ADD_LISTENER
return NS_OK;
}
NS_IMETHODIMP
nsDOMOfflineResourceList::RemoveEventListener(const nsAString &aType,
nsIDOMEventListener *aListener,
PRBool aUseCapture)
{
nsresult rv = Init();
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_ARG(aListener);
nsCOMArray<nsIDOMEventListener> *array;
#define IMPL_REMOVE_LISTENER(_type, _member) \
if (aType.EqualsLiteral(_type)) { \
array = &(_member); \
} else
IMPL_REMOVE_LISTENER(CHECKING_STR, mCheckingListeners)
IMPL_REMOVE_LISTENER(ERROR_STR, mErrorListeners)
IMPL_REMOVE_LISTENER(NOUPDATE_STR, mNoUpdateListeners)
IMPL_REMOVE_LISTENER(DOWNLOADING_STR, mDownloadingListeners)
IMPL_REMOVE_LISTENER(PROGRESS_STR, mProgressListeners)
IMPL_REMOVE_LISTENER(CACHED_STR, mCachedListeners)
IMPL_REMOVE_LISTENER(UPDATEREADY_STR, mUpdateReadyListeners)
IMPL_REMOVE_LISTENER(OBSOLETE_STR, mObsoleteListeners)
{
return NS_ERROR_INVALID_ARG;
}
// Allow a caller to remove O(N^2) behavior by removing end-to-start.
for (PRUint32 i = array->Count() - 1; i != PRUint32(-1); --i) {
if (array->ObjectAt(i) == aListener) {
array->RemoveObjectAt(i);
break;
}
}
#undef IMPL_REMOVE_LISTENER
return NS_OK;
}
NS_IMETHODIMP
nsDOMOfflineResourceList::DispatchEvent(nsIDOMEvent *evt, PRBool *_retval)
{
// Ignored
return NS_OK;
}
static nsresult
CheckInnerWindowCorrectness(nsPIDOMWindow* aOwner)
{
NS_ASSERTION(aOwner->IsInnerWindow(), "Should have inner window here!\n");
NS_ENSURE_STATE(aOwner && aOwner->GetOuterWindow() &&
aOwner->GetOuterWindow()->GetCurrentInnerWindow() == aOwner);
return NS_OK;
}
void
nsDOMOfflineResourceList::NotifyEventListeners(nsIDOMEventListener *aListener,
const nsCOMArray<nsIDOMEventListener>& aListeners,
nsIDOMEvent* aEvent)
{
// XXXbz wouldn't it be easier to just have an actual nsEventListenerManager
// to work with or something? I feel like we're duplicating code here...
//
// (and this was duplicated from XMLHttpRequest)
if (!aEvent)
return;
nsCOMPtr<nsIJSContextStack> stack;
JSContext *cx = nsnull;
nsCOMPtr<nsIScriptGlobalObject> scriptGlobal = do_QueryReferent(mWindow);
nsCOMPtr<nsPIDOMWindow> owner = do_QueryReferent(mWindow);
if (!scriptGlobal || NS_FAILED(CheckInnerWindowCorrectness(owner)))
return;
nsCOMPtr<nsIScriptContext> context = scriptGlobal->GetContext();
if (context) {
stack = do_GetService("@mozilla.org/js/xpc/ContextStack;1");
if (stack) {
cx = (JSContext *)context->GetNativeContext();
if (cx) {
stack->Push(cx);
}
}
}
nsCOMArray<nsIDOMEventListener> listeners = aListeners;
PRInt32 count = listeners.Count();
if (aListener) {
aListener->HandleEvent(aEvent);
}
for (PRInt32 index = 0; index < count; ++index) {
nsIDOMEventListener* listener = listeners[index];
if (listener) {
if (NS_FAILED(CheckInnerWindowCorrectness(owner))) {
break;
}
listener->HandleEvent(aEvent);
}
}
if (cx) {
stack->Pop(&cx);
}
} }
void void
nsDOMOfflineResourceList::FirePendingEvents() nsDOMOfflineResourceList::FirePendingEvents()
{ {
for (PRUint32 i = 0; i < mPendingEvents.Length(); i++) { for (PRInt32 i = 0; i < mPendingEvents.Count(); ++i) {
const PendingEvent &pending = mPendingEvents[i]; PRBool dummy;
NotifyEventListeners(pending.listener, pending.listeners, pending.event); nsCOMPtr<nsIDOMEvent> event = mPendingEvents[i];
DispatchEvent(event, &dummy);
} }
mPendingEvents.Clear(); mPendingEvents.Clear();
} }
nsresult nsresult
nsDOMOfflineResourceList::SendEvent(const nsAString &aEventName, nsDOMOfflineResourceList::SendEvent(const nsAString &aEventName)
nsIDOMEventListener *aListener,
const nsCOMArray<nsIDOMEventListener> &aListeners)
{ {
if (!aListener && aListeners.Count() == 0) {
return NS_OK;
}
// Don't send events to closed windows // Don't send events to closed windows
nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow); if (!mOwner) {
if (!window) {
return NS_OK; return NS_OK;
} }
if (!window->GetDocShell()) { if (!mOwner->GetDocShell()) {
return NS_OK; return NS_OK;
} }
@ -930,26 +680,18 @@ nsDOMOfflineResourceList::SendEvent(const nsAString &aEventName,
event->InitEvent(aEventName, PR_FALSE, PR_TRUE); event->InitEvent(aEventName, PR_FALSE, PR_TRUE);
privevent->SetTarget(this);
privevent->SetCurrentTarget(this);
privevent->SetOriginalTarget(this);
// We assume anyone that managed to call SendEvent is trusted // We assume anyone that managed to call SendEvent is trusted
privevent->SetTrusted(PR_TRUE); privevent->SetTrusted(PR_TRUE);
// If the window is frozen or we're still catching up on events that were // If the window is frozen or we're still catching up on events that were
// queued while frozen, save the event for later. // queued while frozen, save the event for later.
if (window->IsFrozen() || mPendingEvents.Length() > 0) { if (mOwner->IsFrozen() || mPendingEvents.Count() > 0) {
PendingEvent *pending = mPendingEvents.AppendElement(); mPendingEvents.AppendObject(event);
pending->event = event;
pending->listener = aListener;
pending->listeners.SetCapacity(aListeners.Count());
pending->listeners.AppendObjects(aListeners);
return NS_OK; return NS_OK;
} }
NotifyEventListeners(aListener, aListeners, event); PRBool dummy;
DispatchEvent(event, &dummy);
return NS_OK; return NS_OK;
} }
@ -984,7 +726,7 @@ nsDOMOfflineResourceList::Observe(nsISupports *aSubject,
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::Error(nsIOfflineCacheUpdate *aUpdate) nsDOMOfflineResourceList::Error(nsIOfflineCacheUpdate *aUpdate)
{ {
SendEvent(NS_LITERAL_STRING(ERROR_STR), mOnErrorListener, mErrorListeners); SendEvent(NS_LITERAL_STRING(ERROR_STR));
return NS_OK; return NS_OK;
} }
@ -992,24 +734,21 @@ nsDOMOfflineResourceList::Error(nsIOfflineCacheUpdate *aUpdate)
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::Checking(nsIOfflineCacheUpdate *aUpdate) nsDOMOfflineResourceList::Checking(nsIOfflineCacheUpdate *aUpdate)
{ {
SendEvent(NS_LITERAL_STRING(CHECKING_STR), SendEvent(NS_LITERAL_STRING(CHECKING_STR));
mOnCheckingListener, mCheckingListeners);
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::NoUpdate(nsIOfflineCacheUpdate *aUpdate) nsDOMOfflineResourceList::NoUpdate(nsIOfflineCacheUpdate *aUpdate)
{ {
SendEvent(NS_LITERAL_STRING(NOUPDATE_STR), SendEvent(NS_LITERAL_STRING(NOUPDATE_STR));
mOnNoUpdateListener, mNoUpdateListeners);
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::Downloading(nsIOfflineCacheUpdate *aUpdate) nsDOMOfflineResourceList::Downloading(nsIOfflineCacheUpdate *aUpdate)
{ {
SendEvent(NS_LITERAL_STRING(DOWNLOADING_STR), SendEvent(NS_LITERAL_STRING(DOWNLOADING_STR));
mOnDownloadingListener, mDownloadingListeners);
return NS_OK; return NS_OK;
} }
@ -1017,8 +756,7 @@ NS_IMETHODIMP
nsDOMOfflineResourceList::ItemStarted(nsIOfflineCacheUpdate *aUpdate, nsDOMOfflineResourceList::ItemStarted(nsIOfflineCacheUpdate *aUpdate,
nsIDOMLoadStatus *aItem) nsIDOMLoadStatus *aItem)
{ {
SendEvent(NS_LITERAL_STRING(PROGRESS_STR), SendEvent(NS_LITERAL_STRING(PROGRESS_STR));
mOnProgressListener, mProgressListeners);
return NS_OK; return NS_OK;
} }
@ -1032,8 +770,7 @@ nsDOMOfflineResourceList::ItemCompleted(nsIOfflineCacheUpdate *aUpdate,
NS_IMETHODIMP NS_IMETHODIMP
nsDOMOfflineResourceList::Obsolete(nsIOfflineCacheUpdate *aUpdate) nsDOMOfflineResourceList::Obsolete(nsIOfflineCacheUpdate *aUpdate)
{ {
SendEvent(NS_LITERAL_STRING(OBSOLETE_STR), SendEvent(NS_LITERAL_STRING(OBSOLETE_STR));
mOnObsoleteListener, mObsoleteListeners);
return NS_OK; return NS_OK;
} }
@ -1088,12 +825,7 @@ nsDOMOfflineResourceList::UpdateAdded(nsIOfflineCacheUpdate *aUpdate)
already_AddRefed<nsIApplicationCacheContainer> already_AddRefed<nsIApplicationCacheContainer>
nsDOMOfflineResourceList::GetDocumentAppCacheContainer() nsDOMOfflineResourceList::GetDocumentAppCacheContainer()
{ {
nsCOMPtr<nsIDOMWindow> window = do_QueryReferent(mWindow); nsCOMPtr<nsIWebNavigation> webnav = do_GetInterface(mOwner);
if (!window) {
return nsnull;
}
nsCOMPtr<nsIWebNavigation> webnav = do_GetInterface(window);
if (!webnav) { if (!webnav) {
return nsnull; return nsnull;
} }
@ -1140,11 +872,9 @@ nsDOMOfflineResourceList::UpdateCompleted(nsIOfflineCacheUpdate *aUpdate)
if (NS_SUCCEEDED(rv) && succeeded && !partial) { if (NS_SUCCEEDED(rv) && succeeded && !partial) {
if (isUpgrade) { if (isUpgrade) {
SendEvent(NS_LITERAL_STRING(UPDATEREADY_STR), SendEvent(NS_LITERAL_STRING(UPDATEREADY_STR));
mOnUpdateReadyListener, mUpdateReadyListeners);
} else { } else {
SendEvent(NS_LITERAL_STRING(CACHED_STR), SendEvent(NS_LITERAL_STRING(CACHED_STR));
mOnCachedListener, mCachedListeners);
} }
} }

View File

@ -52,48 +52,43 @@
#include "nsWeakReference.h" #include "nsWeakReference.h"
#include "nsCOMArray.h" #include "nsCOMArray.h"
#include "nsIDOMEventListener.h" #include "nsIDOMEventListener.h"
#include "nsIDOMEventTarget.h"
#include "nsDOMEvent.h" #include "nsDOMEvent.h"
#include "nsIObserver.h" #include "nsIObserver.h"
#include "nsIScriptContext.h" #include "nsIScriptContext.h"
#include "nsCycleCollectionParticipant.h" #include "nsCycleCollectionParticipant.h"
#include "nsPIDOMWindow.h"
#include "nsDOMEventTargetHelper.h"
class nsIDOMWindow; class nsIDOMWindow;
class nsDOMOfflineResourceList : public nsIDOMOfflineResourceList, class nsDOMOfflineResourceList : public nsDOMEventTargetHelper,
public nsIDOMOfflineResourceList,
public nsIObserver, public nsIObserver,
public nsIOfflineCacheUpdateObserver, public nsIOfflineCacheUpdateObserver,
public nsIDOMEventTarget,
public nsSupportsWeakReference public nsSupportsWeakReference
{ {
public: public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIDOMOFFLINERESOURCELIST NS_DECL_NSIDOMOFFLINERESOURCELIST
NS_DECL_NSIOBSERVER NS_DECL_NSIOBSERVER
NS_DECL_NSIOFFLINECACHEUPDATEOBSERVER NS_DECL_NSIOFFLINECACHEUPDATEOBSERVER
NS_DECL_NSIDOMEVENTTARGET
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsDOMOfflineResourceList, NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsDOMOfflineResourceList,
nsIDOMOfflineResourceList) nsDOMEventTargetHelper)
nsDOMOfflineResourceList(nsIURI* aManifestURI, nsDOMOfflineResourceList(nsIURI* aManifestURI,
nsIURI* aDocumentURI, nsIURI* aDocumentURI,
nsIDOMWindow* aWindow); nsPIDOMWindow* aWindow,
nsIScriptContext* aScriptContext);
virtual ~nsDOMOfflineResourceList(); virtual ~nsDOMOfflineResourceList();
void FirePendingEvents(); void FirePendingEvents();
void Disconnect(); void Disconnect();
private:
nsresult Init(); nsresult Init();
void NotifyEventListeners(nsIDOMEventListener *aListener, private:
const nsCOMArray<nsIDOMEventListener>& aListeners, nsresult SendEvent(const nsAString &aEventName);
nsIDOMEvent* aEvent);
nsresult SendEvent(const nsAString &aEventName,
nsIDOMEventListener *aListener,
const nsCOMArray<nsIDOMEventListener> &aListeners);
nsresult UpdateAdded(nsIOfflineCacheUpdate *aUpdate); nsresult UpdateAdded(nsIOfflineCacheUpdate *aUpdate);
nsresult UpdateCompleted(nsIOfflineCacheUpdate *aUpdate); nsresult UpdateCompleted(nsIOfflineCacheUpdate *aUpdate);
@ -114,7 +109,6 @@ private:
nsCString mManifestSpec; nsCString mManifestSpec;
nsCOMPtr<nsIURI> mDocumentURI; nsCOMPtr<nsIURI> mDocumentURI;
nsCOMPtr<nsIWeakReference> mWindow;
nsCOMPtr<nsIApplicationCacheService> mApplicationCacheService; nsCOMPtr<nsIApplicationCacheService> mApplicationCacheService;
nsCOMPtr<nsIOfflineCacheUpdate> mCacheUpdate; nsCOMPtr<nsIOfflineCacheUpdate> mCacheUpdate;
@ -122,31 +116,16 @@ private:
char **mCachedKeys; char **mCachedKeys;
PRUint32 mCachedKeysCount; PRUint32 mCachedKeysCount;
nsCOMArray<nsIDOMEventListener> mCheckingListeners; nsRefPtr<nsDOMEventListenerWrapper> mOnCheckingListener;
nsCOMArray<nsIDOMEventListener> mErrorListeners; nsRefPtr<nsDOMEventListenerWrapper> mOnErrorListener;
nsCOMArray<nsIDOMEventListener> mNoUpdateListeners; nsRefPtr<nsDOMEventListenerWrapper> mOnNoUpdateListener;
nsCOMArray<nsIDOMEventListener> mDownloadingListeners; nsRefPtr<nsDOMEventListenerWrapper> mOnDownloadingListener;
nsCOMArray<nsIDOMEventListener> mProgressListeners; nsRefPtr<nsDOMEventListenerWrapper> mOnProgressListener;
nsCOMArray<nsIDOMEventListener> mCachedListeners; nsRefPtr<nsDOMEventListenerWrapper> mOnCachedListener;
nsCOMArray<nsIDOMEventListener> mUpdateReadyListeners; nsRefPtr<nsDOMEventListenerWrapper> mOnUpdateReadyListener;
nsCOMArray<nsIDOMEventListener> mObsoleteListeners; nsRefPtr<nsDOMEventListenerWrapper> mOnObsoleteListener;
nsCOMPtr<nsIDOMEventListener> mOnCheckingListener; nsCOMArray<nsIDOMEvent> mPendingEvents;
nsCOMPtr<nsIDOMEventListener> mOnErrorListener;
nsCOMPtr<nsIDOMEventListener> mOnNoUpdateListener;
nsCOMPtr<nsIDOMEventListener> mOnDownloadingListener;
nsCOMPtr<nsIDOMEventListener> mOnProgressListener;
nsCOMPtr<nsIDOMEventListener> mOnCachedListener;
nsCOMPtr<nsIDOMEventListener> mOnUpdateReadyListener;
nsCOMPtr<nsIDOMEventListener> mOnObsoleteListener;
struct PendingEvent {
nsCOMPtr<nsIDOMEvent> event;
nsCOMPtr<nsIDOMEventListener> listener;
nsCOMArray<nsIDOMEventListener> listeners;
};
nsTArray<PendingEvent> mPendingEvents;
}; };
#endif #endif