michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "Voicemail.h" michael@0: michael@0: #include "mozilla/dom/MozVoicemailBinding.h" michael@0: #include "nsIDOMMozVoicemailStatus.h" michael@0: #include "nsIDOMMozVoicemailEvent.h" michael@0: michael@0: #include "mozilla/Preferences.h" michael@0: #include "mozilla/Services.h" michael@0: #include "nsDOMClassInfo.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "GeneratedEvents.h" michael@0: michael@0: #define NS_RILCONTENTHELPER_CONTRACTID "@mozilla.org/ril/content-helper;1" michael@0: const char* kPrefRilNumRadioInterfaces = "ril.numRadioInterfaces"; michael@0: michael@0: using namespace mozilla::dom; michael@0: michael@0: class Voicemail::Listener : public nsIVoicemailListener michael@0: { michael@0: Voicemail* mVoicemail; michael@0: michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_FORWARD_SAFE_NSIVOICEMAILLISTENER(mVoicemail) michael@0: michael@0: Listener(Voicemail* aVoicemail) michael@0: : mVoicemail(aVoicemail) michael@0: { michael@0: MOZ_ASSERT(mVoicemail); michael@0: } michael@0: michael@0: void Disconnect() michael@0: { michael@0: MOZ_ASSERT(mVoicemail); michael@0: mVoicemail = nullptr; michael@0: } michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(Voicemail::Listener, nsIVoicemailListener) michael@0: michael@0: Voicemail::Voicemail(nsPIDOMWindow* aWindow, michael@0: nsIVoicemailProvider* aProvider) michael@0: : DOMEventTargetHelper(aWindow) michael@0: , mProvider(aProvider) michael@0: { michael@0: mListener = new Listener(this); michael@0: DebugOnly rv = mProvider->RegisterVoicemailMsg(mListener); michael@0: NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), michael@0: "Failed registering voicemail messages with provider"); michael@0: } michael@0: michael@0: Voicemail::~Voicemail() michael@0: { michael@0: MOZ_ASSERT(mProvider && mListener); michael@0: michael@0: mListener->Disconnect(); michael@0: mProvider->UnregisterVoicemailMsg(mListener); michael@0: } michael@0: michael@0: JSObject* michael@0: Voicemail::WrapObject(JSContext* aCx) michael@0: { michael@0: return MozVoicemailBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: bool michael@0: Voicemail::IsValidServiceId(uint32_t aServiceId) const michael@0: { michael@0: uint32_t numClients = mozilla::Preferences::GetUint(kPrefRilNumRadioInterfaces, 1); michael@0: michael@0: return aServiceId < numClients; michael@0: } michael@0: michael@0: bool michael@0: Voicemail::PassedOrDefaultServiceId(const Optional& aServiceId, michael@0: uint32_t& aResult) const michael@0: { michael@0: if (aServiceId.WasPassed()) { michael@0: if (!IsValidServiceId(aServiceId.Value())) { michael@0: return false; michael@0: } michael@0: aResult = aServiceId.Value(); michael@0: } else { michael@0: mProvider->GetVoicemailDefaultServiceId(&aResult); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // MozVoicemail WebIDL michael@0: michael@0: already_AddRefed michael@0: Voicemail::GetStatus(const Optional& aServiceId, michael@0: ErrorResult& aRv) const michael@0: { michael@0: if (!mProvider) { michael@0: aRv.Throw(NS_ERROR_UNEXPECTED); michael@0: return nullptr; michael@0: } michael@0: michael@0: uint32_t id = 0; michael@0: if (!PassedOrDefaultServiceId(aServiceId, id)) { michael@0: aRv.Throw(NS_ERROR_INVALID_ARG); michael@0: return nullptr; michael@0: } michael@0: nsCOMPtr status; michael@0: nsresult rv = mProvider->GetVoicemailStatus(id, getter_AddRefs(status)); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return nullptr; michael@0: } michael@0: michael@0: return status.forget(); michael@0: } michael@0: michael@0: void michael@0: Voicemail::GetNumber(const Optional& aServiceId, nsString& aNumber, michael@0: ErrorResult& aRv) const michael@0: { michael@0: aNumber.SetIsVoid(true); michael@0: michael@0: if (!mProvider) { michael@0: aRv.Throw(NS_ERROR_UNEXPECTED); michael@0: return; michael@0: } michael@0: michael@0: uint32_t id = 0; michael@0: if (!PassedOrDefaultServiceId(aServiceId, id)) { michael@0: aRv.Throw(NS_ERROR_INVALID_ARG); michael@0: return; michael@0: } michael@0: michael@0: aRv = mProvider->GetVoicemailNumber(id, aNumber); michael@0: } michael@0: michael@0: void michael@0: Voicemail::GetDisplayName(const Optional& aServiceId, nsString& aDisplayName, michael@0: ErrorResult& aRv) const michael@0: { michael@0: aDisplayName.SetIsVoid(true); michael@0: michael@0: if (!mProvider) { michael@0: aRv.Throw(NS_ERROR_UNEXPECTED); michael@0: return; michael@0: } michael@0: michael@0: uint32_t id = 0; michael@0: if (!PassedOrDefaultServiceId(aServiceId, id)) { michael@0: aRv.Throw(NS_ERROR_INVALID_ARG); michael@0: return; michael@0: } michael@0: michael@0: aRv = mProvider->GetVoicemailDisplayName(id, aDisplayName); michael@0: } michael@0: michael@0: // nsIVoicemailListener michael@0: michael@0: NS_IMETHODIMP michael@0: Voicemail::NotifyStatusChanged(nsIDOMMozVoicemailStatus* aStatus) michael@0: { michael@0: nsCOMPtr event; michael@0: NS_NewDOMMozVoicemailEvent(getter_AddRefs(event), this, nullptr, nullptr); michael@0: michael@0: nsCOMPtr ce = do_QueryInterface(event); michael@0: nsresult rv = ce->InitMozVoicemailEvent(NS_LITERAL_STRING("statuschanged"), michael@0: false, false, aStatus); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return DispatchTrustedEvent(ce); michael@0: } michael@0: michael@0: nsresult michael@0: NS_NewVoicemail(nsPIDOMWindow* aWindow, Voicemail** aVoicemail) michael@0: { michael@0: nsPIDOMWindow* innerWindow = aWindow->IsInnerWindow() ? michael@0: aWindow : michael@0: aWindow->GetCurrentInnerWindow(); michael@0: michael@0: nsCOMPtr provider = michael@0: do_GetService(NS_RILCONTENTHELPER_CONTRACTID); michael@0: NS_ENSURE_STATE(provider); michael@0: michael@0: nsRefPtr voicemail = new Voicemail(innerWindow, provider); michael@0: voicemail.forget(aVoicemail); michael@0: return NS_OK; michael@0: }