1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/telephony/ipc/TelephonyIPCProvider.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,343 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "ipc/TelephonyIPCProvider.h" 1.10 + 1.11 +#include "mozilla/dom/ContentChild.h" 1.12 +#include "mozilla/dom/telephony/TelephonyChild.h" 1.13 +#include "mozilla/Preferences.h" 1.14 + 1.15 +USING_TELEPHONY_NAMESPACE 1.16 +using namespace mozilla::dom; 1.17 + 1.18 +namespace { 1.19 + 1.20 +const char* kPrefRilNumRadioInterfaces = "ril.numRadioInterfaces"; 1.21 +#define kPrefDefaultServiceId "dom.telephony.defaultServiceId" 1.22 +const char* kObservedPrefs[] = { 1.23 + kPrefDefaultServiceId, 1.24 + nullptr 1.25 +}; 1.26 + 1.27 +uint32_t 1.28 +getDefaultServiceId() 1.29 +{ 1.30 + int32_t id = mozilla::Preferences::GetInt(kPrefDefaultServiceId, 0); 1.31 + int32_t numRil = mozilla::Preferences::GetInt(kPrefRilNumRadioInterfaces, 1); 1.32 + 1.33 + if (id >= numRil || id < 0) { 1.34 + id = 0; 1.35 + } 1.36 + 1.37 + return id; 1.38 +} 1.39 + 1.40 +} // Anonymous namespace 1.41 + 1.42 +NS_IMPL_ISUPPORTS(TelephonyIPCProvider, 1.43 + nsITelephonyProvider, 1.44 + nsITelephonyListener, 1.45 + nsIObserver) 1.46 + 1.47 +TelephonyIPCProvider::TelephonyIPCProvider() 1.48 +{ 1.49 + // Deallocated in ContentChild::DeallocPTelephonyChild(). 1.50 + mPTelephonyChild = new TelephonyChild(this); 1.51 + ContentChild::GetSingleton()->SendPTelephonyConstructor(mPTelephonyChild); 1.52 + 1.53 + Preferences::AddStrongObservers(this, kObservedPrefs); 1.54 + mDefaultServiceId = getDefaultServiceId(); 1.55 +} 1.56 + 1.57 +TelephonyIPCProvider::~TelephonyIPCProvider() 1.58 +{ 1.59 + mPTelephonyChild->Send__delete__(mPTelephonyChild); 1.60 + mPTelephonyChild = nullptr; 1.61 +} 1.62 + 1.63 +/* 1.64 + * Implementation of nsIObserver. 1.65 + */ 1.66 + 1.67 +NS_IMETHODIMP 1.68 +TelephonyIPCProvider::Observe(nsISupports* aSubject, 1.69 + const char* aTopic, 1.70 + const char16_t* aData) 1.71 +{ 1.72 + if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) { 1.73 + nsDependentString data(aData); 1.74 + if (data.EqualsLiteral(kPrefDefaultServiceId)) { 1.75 + mDefaultServiceId = getDefaultServiceId(); 1.76 + } 1.77 + return NS_OK; 1.78 + } 1.79 + 1.80 + MOZ_ASSERT(false, "TelephonyIPCProvider got unexpected topic!"); 1.81 + return NS_ERROR_UNEXPECTED; 1.82 +} 1.83 + 1.84 +/* 1.85 + * Implementation of nsITelephonyProvider. 1.86 + */ 1.87 + 1.88 +NS_IMETHODIMP 1.89 +TelephonyIPCProvider::GetDefaultServiceId(uint32_t* aServiceId) 1.90 +{ 1.91 + *aServiceId = mDefaultServiceId; 1.92 + return NS_OK; 1.93 +} 1.94 + 1.95 +NS_IMETHODIMP 1.96 +TelephonyIPCProvider::RegisterListener(nsITelephonyListener *aListener) 1.97 +{ 1.98 + MOZ_ASSERT(!mListeners.Contains(aListener)); 1.99 + 1.100 + // nsTArray doesn't fail. 1.101 + mListeners.AppendElement(aListener); 1.102 + 1.103 + if (mListeners.Length() == 1) { 1.104 + mPTelephonyChild->SendRegisterListener(); 1.105 + } 1.106 + return NS_OK; 1.107 +} 1.108 + 1.109 +NS_IMETHODIMP 1.110 +TelephonyIPCProvider::UnregisterListener(nsITelephonyListener *aListener) 1.111 +{ 1.112 + MOZ_ASSERT(mListeners.Contains(aListener)); 1.113 + 1.114 + // We always have the element here, so it can't fail. 1.115 + mListeners.RemoveElement(aListener); 1.116 + 1.117 + if (!mListeners.Length()) { 1.118 + mPTelephonyChild->SendUnregisterListener(); 1.119 + } 1.120 + return NS_OK; 1.121 +} 1.122 + 1.123 +nsresult 1.124 +TelephonyIPCProvider::SendRequest(nsITelephonyListener *aListener, 1.125 + nsITelephonyCallback *aCallback, 1.126 + const IPCTelephonyRequest& aRequest) 1.127 +{ 1.128 + // Life time of newly allocated TelephonyRequestChild instance is managed by 1.129 + // IPDL itself. 1.130 + TelephonyRequestChild* actor = new TelephonyRequestChild(aListener, aCallback); 1.131 + mPTelephonyChild->SendPTelephonyRequestConstructor(actor, aRequest); 1.132 + return NS_OK; 1.133 +} 1.134 + 1.135 +NS_IMETHODIMP 1.136 +TelephonyIPCProvider::EnumerateCalls(nsITelephonyListener *aListener) 1.137 +{ 1.138 + return SendRequest(aListener, nullptr, EnumerateCallsRequest()); 1.139 +} 1.140 + 1.141 +NS_IMETHODIMP 1.142 +TelephonyIPCProvider::Dial(uint32_t aClientId, const nsAString& aNumber, 1.143 + bool aIsEmergency, nsITelephonyCallback *aCallback) 1.144 +{ 1.145 + return SendRequest(nullptr, aCallback, 1.146 + DialRequest(aClientId, nsString(aNumber), aIsEmergency)); 1.147 +} 1.148 + 1.149 +NS_IMETHODIMP 1.150 +TelephonyIPCProvider::HangUp(uint32_t aClientId, uint32_t aCallIndex) 1.151 +{ 1.152 + mPTelephonyChild->SendHangUpCall(aClientId, aCallIndex); 1.153 + return NS_OK; 1.154 +} 1.155 + 1.156 +NS_IMETHODIMP 1.157 +TelephonyIPCProvider::AnswerCall(uint32_t aClientId, uint32_t aCallIndex) 1.158 +{ 1.159 + mPTelephonyChild->SendAnswerCall(aClientId, aCallIndex); 1.160 + return NS_OK; 1.161 +} 1.162 + 1.163 +NS_IMETHODIMP 1.164 +TelephonyIPCProvider::RejectCall(uint32_t aClientId, uint32_t aCallIndex) 1.165 +{ 1.166 + mPTelephonyChild->SendRejectCall(aClientId, aCallIndex); 1.167 + return NS_OK; 1.168 +} 1.169 + 1.170 +NS_IMETHODIMP 1.171 +TelephonyIPCProvider::HoldCall(uint32_t aClientId, uint32_t aCallIndex) 1.172 +{ 1.173 + mPTelephonyChild->SendHoldCall(aClientId, aCallIndex); 1.174 + return NS_OK; 1.175 +} 1.176 + 1.177 +NS_IMETHODIMP 1.178 +TelephonyIPCProvider::ResumeCall(uint32_t aClientId, uint32_t aCallIndex) 1.179 +{ 1.180 + mPTelephonyChild->SendResumeCall(aClientId, aCallIndex); 1.181 + return NS_OK; 1.182 +} 1.183 + 1.184 +NS_IMETHODIMP 1.185 +TelephonyIPCProvider::ConferenceCall(uint32_t aClientId) 1.186 +{ 1.187 + mPTelephonyChild->SendConferenceCall(aClientId); 1.188 + return NS_OK; 1.189 +} 1.190 + 1.191 +NS_IMETHODIMP 1.192 +TelephonyIPCProvider::SeparateCall(uint32_t aClientId, uint32_t aCallIndex) 1.193 +{ 1.194 + mPTelephonyChild->SendSeparateCall(aClientId, aCallIndex); 1.195 + return NS_OK; 1.196 +} 1.197 + 1.198 +NS_IMETHODIMP 1.199 +TelephonyIPCProvider::HoldConference(uint32_t aClientId) 1.200 +{ 1.201 + mPTelephonyChild->SendHoldConference(aClientId); 1.202 + return NS_OK; 1.203 +} 1.204 + 1.205 +NS_IMETHODIMP 1.206 +TelephonyIPCProvider::ResumeConference(uint32_t aClientId) 1.207 +{ 1.208 + mPTelephonyChild->SendResumeConference(aClientId); 1.209 + return NS_OK; 1.210 +} 1.211 + 1.212 +NS_IMETHODIMP 1.213 +TelephonyIPCProvider::StartTone(uint32_t aClientId, const nsAString& aDtmfChar) 1.214 +{ 1.215 + mPTelephonyChild->SendStartTone(aClientId, nsString(aDtmfChar)); 1.216 + return NS_OK; 1.217 +} 1.218 + 1.219 +NS_IMETHODIMP 1.220 +TelephonyIPCProvider::StopTone(uint32_t aClientId) 1.221 +{ 1.222 + mPTelephonyChild->SendStopTone(aClientId); 1.223 + return NS_OK; 1.224 +} 1.225 + 1.226 +NS_IMETHODIMP 1.227 +TelephonyIPCProvider::GetMicrophoneMuted(bool* aMuted) 1.228 +{ 1.229 + mPTelephonyChild->SendGetMicrophoneMuted(aMuted); 1.230 + return NS_OK; 1.231 +} 1.232 + 1.233 +NS_IMETHODIMP 1.234 +TelephonyIPCProvider::SetMicrophoneMuted(bool aMuted) 1.235 +{ 1.236 + mPTelephonyChild->SendSetMicrophoneMuted(aMuted); 1.237 + return NS_OK; 1.238 +} 1.239 + 1.240 +NS_IMETHODIMP 1.241 +TelephonyIPCProvider::GetSpeakerEnabled(bool* aEnabled) 1.242 +{ 1.243 + mPTelephonyChild->SendGetSpeakerEnabled(aEnabled); 1.244 + return NS_OK; 1.245 +} 1.246 + 1.247 +NS_IMETHODIMP 1.248 +TelephonyIPCProvider::SetSpeakerEnabled(bool aEnabled) 1.249 +{ 1.250 + mPTelephonyChild->SendSetSpeakerEnabled(aEnabled); 1.251 + return NS_OK; 1.252 +} 1.253 + 1.254 +// nsITelephonyListener 1.255 + 1.256 +NS_IMETHODIMP 1.257 +TelephonyIPCProvider::CallStateChanged(uint32_t aClientId, 1.258 + uint32_t aCallIndex, 1.259 + uint16_t aCallState, 1.260 + const nsAString& aNumber, 1.261 + bool aIsActive, 1.262 + bool aIsOutgoing, 1.263 + bool aIsEmergency, 1.264 + bool aIsConference, 1.265 + bool aIsSwitchable, 1.266 + bool aIsMergeable) 1.267 +{ 1.268 + for (uint32_t i = 0; i < mListeners.Length(); i++) { 1.269 + mListeners[i]->CallStateChanged(aClientId, aCallIndex, aCallState, aNumber, 1.270 + aIsActive, aIsOutgoing, aIsEmergency, 1.271 + aIsConference, aIsSwitchable, aIsMergeable); 1.272 + } 1.273 + return NS_OK; 1.274 +} 1.275 + 1.276 +NS_IMETHODIMP 1.277 +TelephonyIPCProvider::ConferenceCallStateChanged(uint16_t aCallState) 1.278 +{ 1.279 + for (uint32_t i = 0; i < mListeners.Length(); i++) { 1.280 + mListeners[i]->ConferenceCallStateChanged(aCallState); 1.281 + } 1.282 + return NS_OK; 1.283 +} 1.284 + 1.285 +NS_IMETHODIMP 1.286 +TelephonyIPCProvider::EnumerateCallStateComplete() 1.287 +{ 1.288 + MOZ_CRASH("Not a EnumerateCalls request!"); 1.289 +} 1.290 + 1.291 +NS_IMETHODIMP 1.292 +TelephonyIPCProvider::EnumerateCallState(uint32_t aClientId, 1.293 + uint32_t aCallIndex, 1.294 + uint16_t aCallState, 1.295 + const nsAString& aNumber, 1.296 + bool aIsActive, 1.297 + bool aIsOutgoing, 1.298 + bool aIsEmergency, 1.299 + bool aIsConference, 1.300 + bool aIsSwitchable, 1.301 + bool aIsMergeable) 1.302 +{ 1.303 + MOZ_CRASH("Not a EnumerateCalls request!"); 1.304 +} 1.305 + 1.306 +NS_IMETHODIMP 1.307 +TelephonyIPCProvider::NotifyCdmaCallWaiting(uint32_t aClientId, 1.308 + const nsAString& aNumber) 1.309 +{ 1.310 + for (uint32_t i = 0; i < mListeners.Length(); i++) { 1.311 + mListeners[i]->NotifyCdmaCallWaiting(aClientId, aNumber); 1.312 + } 1.313 + return NS_OK; 1.314 +} 1.315 + 1.316 +NS_IMETHODIMP 1.317 +TelephonyIPCProvider::NotifyConferenceError(const nsAString& aName, 1.318 + const nsAString& aMessage) 1.319 +{ 1.320 + for (uint32_t i = 0; i < mListeners.Length(); i++) { 1.321 + mListeners[i]->NotifyConferenceError(aName, aMessage); 1.322 + } 1.323 + return NS_OK; 1.324 +} 1.325 + 1.326 +NS_IMETHODIMP 1.327 +TelephonyIPCProvider::NotifyError(uint32_t aClientId, int32_t aCallIndex, 1.328 + const nsAString& aError) 1.329 +{ 1.330 + for (uint32_t i = 0; i < mListeners.Length(); i++) { 1.331 + mListeners[i]->NotifyError(aClientId, aCallIndex, aError); 1.332 + } 1.333 + return NS_OK; 1.334 +} 1.335 + 1.336 +NS_IMETHODIMP 1.337 +TelephonyIPCProvider::SupplementaryServiceNotification(uint32_t aClientId, 1.338 + int32_t aCallIndex, 1.339 + uint16_t aNotification) 1.340 +{ 1.341 + for (uint32_t i = 0; i < mListeners.Length(); i++) { 1.342 + mListeners[i]->SupplementaryServiceNotification(aClientId, aCallIndex, 1.343 + aNotification); 1.344 + } 1.345 + return NS_OK; 1.346 +}