From 87763f254ad193cd274b3fabd8a4907b3be0112c Mon Sep 17 00:00:00 2001 From: Kyle Machulis Date: Thu, 23 Aug 2012 09:52:29 -0700 Subject: [PATCH] Bug 783426: Patch 1 - Async DOMRequest Firing; r=sicking --- dom/base/DOMRequest.cpp | 77 +++++++++++++++++++++++++++++++++++ dom/base/nsIDOMDOMRequest.idl | 4 +- 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/dom/base/DOMRequest.cpp b/dom/base/DOMRequest.cpp index 7dcf3d41761..2a15785d5ef 100644 --- a/dom/base/DOMRequest.cpp +++ b/dom/base/DOMRequest.cpp @@ -12,6 +12,7 @@ #include "nsEventDispatcher.h" #include "nsDOMEvent.h" #include "nsContentUtils.h" +#include "nsThreadUtils.h" using mozilla::dom::DOMRequest; using mozilla::dom::DOMRequestService; @@ -222,3 +223,79 @@ DOMRequestService::FireError(nsIDOMDOMRequest* aRequest, return NS_OK; } + +class FireSuccessAsyncTask : public nsRunnable +{ +public: + FireSuccessAsyncTask(nsIDOMDOMRequest* aRequest, + const jsval& aResult) : + mReq(aRequest), + mResult(aResult) + { + JSContext* cx = mReq->GetJSContextForEventHandlers(); + JS_AddValueRoot(cx, &mResult); + } + + nsresult + Run() + { + static_cast(mReq)->FireSuccess(mResult); + return NS_OK; + } + + ~FireSuccessAsyncTask() + { + JSContext* cx = mReq->GetJSContextForEventHandlers(); + JS_RemoveValueRoot(cx, &mResult); + } +private: + nsIDOMDOMRequest* mReq; + jsval mResult; +}; + +class FireErrorAsyncTask : public nsRunnable +{ +public: + FireErrorAsyncTask(nsIDOMDOMRequest* aRequest, + const nsAString& aError) : + mReq(aRequest), + mError(aError) + { + } + + nsresult + Run() + { + static_cast(mReq)->FireError(mError); + return NS_OK; + } +private: + nsIDOMDOMRequest* mReq; + nsString mError; +}; + +NS_IMETHODIMP +DOMRequestService::FireSuccessAsync(nsIDOMDOMRequest* aRequest, + const jsval& aResult) +{ + NS_ENSURE_STATE(aRequest); + nsCOMPtr asyncTask = new FireSuccessAsyncTask(aRequest, aResult); + if (NS_FAILED(NS_DispatchToMainThread(asyncTask))) { + NS_WARNING("Failed to dispatch to main thread!"); + return NS_ERROR_FAILURE; + } + return NS_OK; +} + +NS_IMETHODIMP +DOMRequestService::FireErrorAsync(nsIDOMDOMRequest* aRequest, + const nsAString& aError) +{ + NS_ENSURE_STATE(aRequest); + nsCOMPtr asyncTask = new FireErrorAsyncTask(aRequest, aError); + if (NS_FAILED(NS_DispatchToMainThread(asyncTask))) { + NS_WARNING("Failed to dispatch to main thread!"); + return NS_ERROR_FAILURE; + } + return NS_OK; +} diff --git a/dom/base/nsIDOMDOMRequest.idl b/dom/base/nsIDOMDOMRequest.idl index b4498633353..0088272c02b 100644 --- a/dom/base/nsIDOMDOMRequest.idl +++ b/dom/base/nsIDOMDOMRequest.idl @@ -21,11 +21,13 @@ interface nsIDOMDOMRequest : nsIDOMEventTarget attribute nsIDOMEventListener onerror; }; -[scriptable, builtinclass, uuid(eebcdf29-f8fa-4c36-bbc7-2146b1cbaf7b)] +[scriptable, builtinclass, uuid(10996de9-e6f6-4058-97bd-45f1fe065eb5)] interface nsIDOMRequestService : nsISupports { nsIDOMDOMRequest createRequest(in nsIDOMWindow window); void fireSuccess(in nsIDOMDOMRequest request, in jsval result); void fireError(in nsIDOMDOMRequest request, in DOMString error); + void fireSuccessAsync(in nsIDOMDOMRequest request, in jsval result); + void fireErrorAsync(in nsIDOMDOMRequest request, in DOMString error); };