From f756e7d379f7dd1acfb86bd8e59a6ae352cd3127 Mon Sep 17 00:00:00 2001 From: "Szu-Yu Chen [:aknow]" Date: Wed, 17 Dec 2014 06:57:23 +0800 Subject: [PATCH] Bug 1100895 - Part 2: Implement ready attribute. r=hsinyi --- dom/telephony/Telephony.cpp | 65 ++++++++++++------------------------- dom/telephony/Telephony.h | 13 +++----- 2 files changed, 25 insertions(+), 53 deletions(-) diff --git a/dom/telephony/Telephony.cpp b/dom/telephony/Telephony.cpp index 47863ae828fe..8d05798c4b8c 100644 --- a/dom/telephony/Telephony.cpp +++ b/dom/telephony/Telephony.cpp @@ -62,27 +62,17 @@ public: } }; -class Telephony::EnumerationAck : public nsRunnable -{ - nsRefPtr mTelephony; - -public: - explicit EnumerationAck(Telephony* aTelephony) - : mTelephony(aTelephony) - { - MOZ_ASSERT(mTelephony); - } - - NS_IMETHOD Run() - { - mTelephony->NotifyEvent(NS_LITERAL_STRING("ready")); - return NS_OK; - } -}; - Telephony::Telephony(nsPIDOMWindow* aOwner) - : DOMEventTargetHelper(aOwner), mEnumerated(false) + : DOMEventTargetHelper(aOwner) { + nsCOMPtr global = do_QueryInterface(aOwner); + MOZ_ASSERT(global); + + ErrorResult rv; + nsRefPtr promise = Promise::Create(global, rv); + MOZ_ASSERT(!rv.Failed()); + + mReadyPromise = promise; } Telephony::~Telephony() @@ -327,6 +317,7 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(Telephony, NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCalls) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCallsList) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGroup) + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReadyPromise) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(Telephony, @@ -335,6 +326,7 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(Telephony, NS_IMPL_CYCLE_COLLECTION_UNLINK(mCalls) NS_IMPL_CYCLE_COLLECTION_UNLINK(mCallsList) NS_IMPL_CYCLE_COLLECTION_UNLINK(mGroup) + NS_IMPL_CYCLE_COLLECTION_UNLINK(mReadyPromise) NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(Telephony) @@ -462,14 +454,16 @@ Telephony::ConferenceGroup() const return group.forget(); } -// EventTarget - -void -Telephony::EventListenerAdded(nsIAtom* aType) +already_AddRefed +Telephony::GetReady(ErrorResult& aRv) const { - if (aType == nsGkAtoms::onready) { - EnqueueEnumerationAck(); + if (!mReadyPromise) { + aRv.Throw(NS_ERROR_UNEXPECTED); + return nullptr; } + + nsRefPtr promise = mReadyPromise; + return promise.forget(); } // nsITelephonyListener @@ -548,8 +542,6 @@ Telephony::ConferenceCallStateChanged(uint16_t aCallState) NS_IMETHODIMP Telephony::EnumerateCallStateComplete() { - MOZ_ASSERT(!mEnumerated); - // Set conference state. if (mGroup->CallsArray().Length() >= 2) { const nsTArray > &calls = mGroup->CallsArray(); @@ -565,10 +557,8 @@ Telephony::EnumerateCallStateComplete() mGroup->ChangeState(callState); } - mEnumerated = true; - - if (NS_FAILED(NotifyEvent(NS_LITERAL_STRING("ready")))) { - NS_WARNING("Failed to notify ready!"); + if (mReadyPromise) { + mReadyPromise->MaybeResolve(JS::UndefinedHandleValue); } if (NS_FAILED(mService->RegisterListener(mListener))) { @@ -693,19 +683,6 @@ Telephony::DispatchCallEvent(const nsAString& aType, return DispatchTrustedEvent(event); } -void -Telephony::EnqueueEnumerationAck() -{ - if (!mEnumerated) { - return; - } - - nsCOMPtr task = new EnumerationAck(this); - if (NS_FAILED(NS_DispatchToCurrentThread(task))) { - NS_WARNING("Failed to dispatch to current thread!"); - } -} - already_AddRefed NS_CreateTelephonyService() { diff --git a/dom/telephony/Telephony.h b/dom/telephony/Telephony.h index 98ac27bfd131..c800bd1426d7 100644 --- a/dom/telephony/Telephony.h +++ b/dom/telephony/Telephony.h @@ -40,9 +40,7 @@ class Telephony MOZ_FINAL : public DOMEventTargetHelper, * also bug 775997 comment #51. */ class Listener; - class EnumerationAck; - friend class EnumerationAck; friend class telephony::TelephonyDialCallback; nsCOMPtr mService; @@ -53,7 +51,7 @@ class Telephony MOZ_FINAL : public DOMEventTargetHelper, nsRefPtr mGroup; - bool mEnumerated; + nsRefPtr mReadyPromise; public: NS_DECL_ISUPPORTS_INHERITED @@ -109,7 +107,9 @@ public: already_AddRefed ConferenceGroup() const; - IMPL_EVENT_HANDLER(ready) + already_AddRefed + GetReady(ErrorResult& aRv) const; + IMPL_EVENT_HANDLER(incoming) IMPL_EVENT_HANDLER(callschanged) IMPL_EVENT_HANDLER(remoteheld) @@ -146,8 +146,6 @@ public: return mCalls; } - virtual void EventListenerAdded(nsIAtom* aType) MOZ_OVERRIDE; - private: explicit Telephony(nsPIDOMWindow* aOwner); ~Telephony(); @@ -198,9 +196,6 @@ private: nsresult DispatchCallEvent(const nsAString& aType, TelephonyCall* aCall); - void - EnqueueEnumerationAck(); - already_AddRefed GetCall(uint32_t aServiceId, uint32_t aCallIndex);