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 file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "IccListener.h" michael@0: michael@0: #include "Icc.h" michael@0: #include "IccManager.h" michael@0: #include "nsIDOMClassInfo.h" michael@0: #include "nsIDOMIccInfo.h" michael@0: #include "nsRadioInterfaceLayer.h" michael@0: michael@0: using namespace mozilla::dom; michael@0: michael@0: NS_IMPL_ISUPPORTS(IccListener, nsIIccListener) michael@0: michael@0: IccListener::IccListener(IccManager* aIccManager, uint32_t aClientId) michael@0: : mClientId(aClientId) michael@0: , mIccManager(aIccManager) michael@0: { michael@0: MOZ_ASSERT(mIccManager); michael@0: michael@0: mProvider = do_GetService(NS_RILCONTENTHELPER_CONTRACTID); michael@0: michael@0: if (!mProvider) { michael@0: NS_WARNING("Could not acquire nsIIccProvider!"); michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr iccInfo; michael@0: mProvider->GetIccInfo(mClientId, getter_AddRefs(iccInfo)); michael@0: if (iccInfo) { michael@0: nsString iccId; michael@0: iccInfo->GetIccid(iccId); michael@0: if (!iccId.IsEmpty()) { michael@0: mIcc = new Icc(mIccManager->GetOwner(), mClientId, iccId); michael@0: } michael@0: } michael@0: michael@0: DebugOnly rv = mProvider->RegisterIccMsg(mClientId, this); michael@0: NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), michael@0: "Failed registering icc messages with provider"); michael@0: } michael@0: michael@0: IccListener::~IccListener() michael@0: { michael@0: Shutdown(); michael@0: } michael@0: michael@0: void michael@0: IccListener::Shutdown() michael@0: { michael@0: if (mProvider) { michael@0: mProvider->UnregisterIccMsg(mClientId, this); michael@0: mProvider = nullptr; michael@0: } michael@0: michael@0: if (mIcc) { michael@0: mIcc->Shutdown(); michael@0: mIcc = nullptr; michael@0: } michael@0: michael@0: mIccManager = nullptr; michael@0: } michael@0: michael@0: // nsIIccListener michael@0: michael@0: NS_IMETHODIMP michael@0: IccListener::NotifyStkCommand(const nsAString& aMessage) michael@0: { michael@0: if (!mIcc) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: return mIcc->NotifyStkEvent(NS_LITERAL_STRING("stkcommand"), aMessage); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: IccListener::NotifyStkSessionEnd() michael@0: { michael@0: if (!mIcc) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: return mIcc->NotifyEvent(NS_LITERAL_STRING("stksessionend")); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: IccListener::NotifyCardStateChanged() michael@0: { michael@0: if (!mIcc) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: return mIcc->NotifyEvent(NS_LITERAL_STRING("cardstatechange")); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: IccListener::NotifyIccInfoChanged() michael@0: { michael@0: nsCOMPtr iccInfo; michael@0: mProvider->GetIccInfo(mClientId, getter_AddRefs(iccInfo)); michael@0: michael@0: // Create/delete icc object based on current iccInfo. michael@0: // 1. If the mIcc is nullptr and iccInfo has valid data, create icc object and michael@0: // notify mIccManager a new icc is added. michael@0: // 2. If the mIcc is not nullptr and iccInfo becomes to null, delete existed michael@0: // icc object and notify mIccManager the icc is removed. michael@0: if (!mIcc) { michael@0: if (iccInfo) { michael@0: nsString iccId; michael@0: iccInfo->GetIccid(iccId); michael@0: if (!iccId.IsEmpty()) { michael@0: mIcc = new Icc(mIccManager->GetOwner(), mClientId, iccId); michael@0: mIccManager->NotifyIccAdd(iccId); michael@0: mIcc->NotifyEvent(NS_LITERAL_STRING("iccinfochange")); michael@0: } michael@0: } michael@0: } else { michael@0: mIcc->NotifyEvent(NS_LITERAL_STRING("iccinfochange")); michael@0: if (!iccInfo) { michael@0: nsString iccId = mIcc->GetIccId(); michael@0: mIcc->Shutdown(); michael@0: mIcc = nullptr; michael@0: mIccManager->NotifyIccRemove(iccId); michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: }