From f1bcf565b43da3ea8c16f0483df10df88ebac314 Mon Sep 17 00:00:00 2001 From: Vicamo Yang Date: Wed, 6 Mar 2013 17:53:19 +0800 Subject: [PATCH] Bug 834160 - Part 4/7: fix Voicemail. r=smaug, f=htsai --- dom/voicemail/Makefile.in | 1 + dom/voicemail/Voicemail.cpp | 85 +++++++++++++++++++++---------------- dom/voicemail/Voicemail.h | 35 ++++++--------- 3 files changed, 64 insertions(+), 57 deletions(-) diff --git a/dom/voicemail/Makefile.in b/dom/voicemail/Makefile.in index 27194107fe3..74fc74c237e 100644 --- a/dom/voicemail/Makefile.in +++ b/dom/voicemail/Makefile.in @@ -32,6 +32,7 @@ XPIDLSRCS = \ nsIDOMMozVoicemail.idl \ nsIDOMMozVoicemailEvent.idl \ nsIDOMMozVoicemailStatus.idl \ + nsIVoicemailProvider.idl \ $(NULL) include $(topsrcdir)/config/rules.mk diff --git a/dom/voicemail/Voicemail.cpp b/dom/voicemail/Voicemail.cpp index 66e0f21f629..9dac86135cd 100644 --- a/dom/voicemail/Voicemail.cpp +++ b/dom/voicemail/Voicemail.cpp @@ -11,14 +11,37 @@ #include "mozilla/Services.h" #include "nsContentUtils.h" #include "nsDOMClassInfo.h" -#include "nsRadioInterfaceLayer.h" #include "nsServiceManagerUtils.h" #include "GeneratedEvents.h" -DOMCI_DATA(MozVoicemail, mozilla::dom::Voicemail) +#define NS_RILCONTENTHELPER_CONTRACTID "@mozilla.org/ril/content-helper;1" -namespace mozilla { -namespace dom { +using namespace mozilla::dom; + +class Voicemail::Listener : public nsIVoicemailListener +{ + Voicemail* mVoicemail; + +public: + NS_DECL_ISUPPORTS + NS_FORWARD_SAFE_NSIVOICEMAILLISTENER(mVoicemail) + + Listener(Voicemail* aVoicemail) + : mVoicemail(aVoicemail) + { + MOZ_ASSERT(mVoicemail); + } + + void Disconnect() + { + MOZ_ASSERT(mVoicemail); + mVoicemail = nullptr; + } +}; + +NS_IMPL_ISUPPORTS1(Voicemail::Listener, nsIVoicemailListener) + +DOMCI_DATA(MozVoicemail, Voicemail) NS_INTERFACE_MAP_BEGIN(Voicemail) NS_INTERFACE_MAP_ENTRY(nsIDOMMozVoicemail) @@ -28,31 +51,24 @@ NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper) NS_IMPL_ADDREF_INHERITED(Voicemail, nsDOMEventTargetHelper) NS_IMPL_RELEASE_INHERITED(Voicemail, nsDOMEventTargetHelper) -NS_IMPL_ISUPPORTS1(Voicemail::RILVoicemailCallback, nsIRILVoicemailCallback) - -Voicemail::Voicemail(nsPIDOMWindow* aWindow, nsIRILContentHelper* aRIL) - : mRIL(aRIL) +Voicemail::Voicemail(nsPIDOMWindow* aWindow, + nsIVoicemailProvider* aProvider) + : mProvider(aProvider) { BindToOwner(aWindow); - mRILVoicemailCallback = new RILVoicemailCallback(this); - - nsresult rv = aRIL->RegisterVoicemailCallback(mRILVoicemailCallback); - if (NS_FAILED(rv)) { - NS_WARNING("Failed registering voicemail callback with RIL"); - } - - rv = aRIL->RegisterVoicemailMsg(); - if (NS_FAILED(rv)) { - NS_WARNING("Failed registering voicemail messages with RIL"); - } + mListener = new Listener(this); + DebugOnly rv = mProvider->RegisterVoicemailMsg(mListener); + NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), + "Failed registering voicemail messages with provider"); } Voicemail::~Voicemail() { - if (mRIL && mRILVoicemailCallback) { - mRIL->UnregisterVoicemailCallback(mRILVoicemailCallback); - } + MOZ_ASSERT(mProvider && mListener); + + mListener->Disconnect(); + mProvider->UnregisterVoicemailMsg(mListener); } // nsIDOMMozVoicemail @@ -62,34 +78,34 @@ Voicemail::GetStatus(nsIDOMMozVoicemailStatus** aStatus) { *aStatus = nullptr; - NS_ENSURE_STATE(mRIL); - return mRIL->GetVoicemailStatus(aStatus); + NS_ENSURE_STATE(mProvider); + return mProvider->GetVoicemailStatus(aStatus); } NS_IMETHODIMP Voicemail::GetNumber(nsAString& aNumber) { - NS_ENSURE_STATE(mRIL); + NS_ENSURE_STATE(mProvider); aNumber.SetIsVoid(true); - return mRIL->GetVoicemailNumber(aNumber); + return mProvider->GetVoicemailNumber(aNumber); } NS_IMETHODIMP Voicemail::GetDisplayName(nsAString& aDisplayName) { - NS_ENSURE_STATE(mRIL); + NS_ENSURE_STATE(mProvider); aDisplayName.SetIsVoid(true); - return mRIL->GetVoicemailDisplayName(aDisplayName); + return mProvider->GetVoicemailDisplayName(aDisplayName); } NS_IMPL_EVENT_HANDLER(Voicemail, statuschanged) -// nsIRILVoicemailCallback +// nsIVoicemailListener NS_IMETHODIMP -Voicemail::VoicemailNotification(nsIDOMMozVoicemailStatus* aStatus) +Voicemail::NotifyStatusChanged(nsIDOMMozVoicemailStatus* aStatus) { nsCOMPtr event; NS_NewDOMMozVoicemailEvent(getter_AddRefs(event), nullptr, nullptr); @@ -102,9 +118,6 @@ Voicemail::VoicemailNotification(nsIDOMMozVoicemailStatus* aStatus) return DispatchTrustedEvent(ce); } -} // namespace dom -} // namespace mozilla - nsresult NS_NewVoicemail(nsPIDOMWindow* aWindow, nsIDOMMozVoicemail** aVoicemail) { @@ -112,12 +125,12 @@ NS_NewVoicemail(nsPIDOMWindow* aWindow, nsIDOMMozVoicemail** aVoicemail) aWindow : aWindow->GetCurrentInnerWindow(); - nsCOMPtr ril = + nsCOMPtr provider = do_GetService(NS_RILCONTENTHELPER_CONTRACTID); - NS_ENSURE_STATE(ril); + NS_ENSURE_STATE(provider); nsRefPtr voicemail = - new mozilla::dom::Voicemail(innerWindow, ril); + new mozilla::dom::Voicemail(innerWindow, provider); voicemail.forget(aVoicemail); return NS_OK; } diff --git a/dom/voicemail/Voicemail.h b/dom/voicemail/Voicemail.h index 547b5431b20..40befe036de 100644 --- a/dom/voicemail/Voicemail.h +++ b/dom/voicemail/Voicemail.h @@ -10,10 +10,9 @@ #include "nsDOMEvent.h" #include "nsDOMEventTargetHelper.h" #include "nsIDOMMozVoicemail.h" -#include "nsIRadioInterfaceLayer.h" +#include "nsIVoicemailProvider.h" class nsPIDOMWindow; -class nsIRILContentHelper; class nsIDOMMozVoicemailStatus; namespace mozilla { @@ -22,34 +21,28 @@ namespace dom { class Voicemail : public nsDOMEventTargetHelper, public nsIDOMMozVoicemail { + /** + * Class Voicemail doesn't actually inherit nsIVoicemailListener. Instead, it + * owns an nsIVoicemailListener derived instance mListener and passes it to + * nsIVoicemailProvider. The onreceived events are first delivered to + * mListener and then forwarded to its owner, Voicemail. See also bug 775997 + * comment #51. + */ + class Listener; + public: NS_DECL_ISUPPORTS_INHERITED NS_DECL_NSIDOMMOZVOICEMAIL - NS_DECL_NSIRILVOICEMAILCALLBACK + NS_DECL_NSIVOICEMAILLISTENER NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetHelper::) - Voicemail(nsPIDOMWindow* aWindow, nsIRILContentHelper* aRIL); + Voicemail(nsPIDOMWindow* aWindow, nsIVoicemailProvider* aProvider); virtual ~Voicemail(); private: - nsCOMPtr mRIL; - nsCOMPtr mRILVoicemailCallback; - - class RILVoicemailCallback : public nsIRILVoicemailCallback - { - Voicemail* mVoicemail; - - public: - NS_DECL_ISUPPORTS - NS_FORWARD_NSIRILVOICEMAILCALLBACK(mVoicemail->) - - RILVoicemailCallback(Voicemail* aVoicemail) - : mVoicemail(aVoicemail) - { - NS_ASSERTION(mVoicemail, "Null pointer!"); - } - }; + nsCOMPtr mProvider; + nsRefPtr mListener; }; } // namespace dom