1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/icc/src/IccListener.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,130 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "IccListener.h" 1.9 + 1.10 +#include "Icc.h" 1.11 +#include "IccManager.h" 1.12 +#include "nsIDOMClassInfo.h" 1.13 +#include "nsIDOMIccInfo.h" 1.14 +#include "nsRadioInterfaceLayer.h" 1.15 + 1.16 +using namespace mozilla::dom; 1.17 + 1.18 +NS_IMPL_ISUPPORTS(IccListener, nsIIccListener) 1.19 + 1.20 +IccListener::IccListener(IccManager* aIccManager, uint32_t aClientId) 1.21 + : mClientId(aClientId) 1.22 + , mIccManager(aIccManager) 1.23 +{ 1.24 + MOZ_ASSERT(mIccManager); 1.25 + 1.26 + mProvider = do_GetService(NS_RILCONTENTHELPER_CONTRACTID); 1.27 + 1.28 + if (!mProvider) { 1.29 + NS_WARNING("Could not acquire nsIIccProvider!"); 1.30 + return; 1.31 + } 1.32 + 1.33 + nsCOMPtr<nsIDOMMozIccInfo> iccInfo; 1.34 + mProvider->GetIccInfo(mClientId, getter_AddRefs(iccInfo)); 1.35 + if (iccInfo) { 1.36 + nsString iccId; 1.37 + iccInfo->GetIccid(iccId); 1.38 + if (!iccId.IsEmpty()) { 1.39 + mIcc = new Icc(mIccManager->GetOwner(), mClientId, iccId); 1.40 + } 1.41 + } 1.42 + 1.43 + DebugOnly<nsresult> rv = mProvider->RegisterIccMsg(mClientId, this); 1.44 + NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), 1.45 + "Failed registering icc messages with provider"); 1.46 +} 1.47 + 1.48 +IccListener::~IccListener() 1.49 +{ 1.50 + Shutdown(); 1.51 +} 1.52 + 1.53 +void 1.54 +IccListener::Shutdown() 1.55 +{ 1.56 + if (mProvider) { 1.57 + mProvider->UnregisterIccMsg(mClientId, this); 1.58 + mProvider = nullptr; 1.59 + } 1.60 + 1.61 + if (mIcc) { 1.62 + mIcc->Shutdown(); 1.63 + mIcc = nullptr; 1.64 + } 1.65 + 1.66 + mIccManager = nullptr; 1.67 +} 1.68 + 1.69 +// nsIIccListener 1.70 + 1.71 +NS_IMETHODIMP 1.72 +IccListener::NotifyStkCommand(const nsAString& aMessage) 1.73 +{ 1.74 + if (!mIcc) { 1.75 + return NS_OK; 1.76 + } 1.77 + 1.78 + return mIcc->NotifyStkEvent(NS_LITERAL_STRING("stkcommand"), aMessage); 1.79 +} 1.80 + 1.81 +NS_IMETHODIMP 1.82 +IccListener::NotifyStkSessionEnd() 1.83 +{ 1.84 + if (!mIcc) { 1.85 + return NS_OK; 1.86 + } 1.87 + 1.88 + return mIcc->NotifyEvent(NS_LITERAL_STRING("stksessionend")); 1.89 +} 1.90 + 1.91 +NS_IMETHODIMP 1.92 +IccListener::NotifyCardStateChanged() 1.93 +{ 1.94 + if (!mIcc) { 1.95 + return NS_OK; 1.96 + } 1.97 + 1.98 + return mIcc->NotifyEvent(NS_LITERAL_STRING("cardstatechange")); 1.99 +} 1.100 + 1.101 +NS_IMETHODIMP 1.102 +IccListener::NotifyIccInfoChanged() 1.103 +{ 1.104 + nsCOMPtr<nsIDOMMozIccInfo> iccInfo; 1.105 + mProvider->GetIccInfo(mClientId, getter_AddRefs(iccInfo)); 1.106 + 1.107 + // Create/delete icc object based on current iccInfo. 1.108 + // 1. If the mIcc is nullptr and iccInfo has valid data, create icc object and 1.109 + // notify mIccManager a new icc is added. 1.110 + // 2. If the mIcc is not nullptr and iccInfo becomes to null, delete existed 1.111 + // icc object and notify mIccManager the icc is removed. 1.112 + if (!mIcc) { 1.113 + if (iccInfo) { 1.114 + nsString iccId; 1.115 + iccInfo->GetIccid(iccId); 1.116 + if (!iccId.IsEmpty()) { 1.117 + mIcc = new Icc(mIccManager->GetOwner(), mClientId, iccId); 1.118 + mIccManager->NotifyIccAdd(iccId); 1.119 + mIcc->NotifyEvent(NS_LITERAL_STRING("iccinfochange")); 1.120 + } 1.121 + } 1.122 + } else { 1.123 + mIcc->NotifyEvent(NS_LITERAL_STRING("iccinfochange")); 1.124 + if (!iccInfo) { 1.125 + nsString iccId = mIcc->GetIccId(); 1.126 + mIcc->Shutdown(); 1.127 + mIcc = nullptr; 1.128 + mIccManager->NotifyIccRemove(iccId); 1.129 + } 1.130 + } 1.131 + 1.132 + return NS_OK; 1.133 +}