Files
palemoon27/dom/telephony/Telephony.cpp
T
roytam1 7edec4880a import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 859764 - Part 5: Changes in Test Cases. r=echen (f1a5cd4fd3)
- Bug 859764 - Part 6: Changes in Payment. r=ferjm, r=smaug (2b47641375)
- Bug 1010756 - Helpful errors for using nsCOMPtr on non-XPCOM types; r=froydnj (6b1521c482)
- leftover (e1a24351e9)
- Bug 1192102 - Remove unused file embedded/android/GeckoSmsManager.java. r=blassey (ac05ae649d)
- var-let (8ddb529f96)
- Bug 1181466 - Fix observe function in SmsService/MmsService. r=btseng (ca93122404)
- Bug 733331 - Part 2: Update enabledGsmTableTuples when MCC changes in SmsService.js and fix segmentChars in SmsSegmentHelper.jsm. r=btseng (24fa23e4be)
- Bug 1173156 - Fix typo and add a Marionette test case. r=btseng (e7199eb55a)
- Bug 1132774 - [B2G][SMS] Enable DEBUG Flag in SmsService if the default value of "ril.debugging.enabled" is true. r=btseng (7300d24fb7)
- Bug 1169160 - [MobileConnection] Support more call barring program: all service, outgoing service and incoming service. r=hsinyi,aknow (872c2cc056)
- Bug 1169225 - [MobileConnectionService] Support setting/getting call waiting on all serviceClass. r=aknow (14c546a9ca)
- Bug 1110619 - Part 1: IDL Interface Changes. r=echen (beae2b4b77)
- Bug 1168064 - B2G RIL: filter out cell info with unknown values. r=echen (11dfc5a7e8)
- Bug 1159591 - Part 2: Move MMI logic from ril_worker to telephonyService (Call Forwarding). r=aknow (b4edb76863)
- Bug 1159591 - Part 3: Move MMI logic from ril_worker to telephonyService (Icc Lock). r=aknow (3faba76808)
- Bug 1138263 - Make TelephonyService.js JSHint friendly. r=hsinyi (454bd4c765)
- Bug 1159591 - Part 4: Move MMI logic from ril_worker to telephonyService (IMEI). r=aknow (57f423ecdd)
- Bug 1159591 - Part 5: Move MMI logic from ril_worker to telephonyService (CLIP). r=aknow (c6611dbcb8)
- Bug 1159591 - Part 6: Move MMI logic from ril_worker to telephonyService (CLIR). r=aknow (8c1fc03edb)
- Bug 1159591 - Part 7: Move MMI logic from ril_worker to telephonyService (Call Barring Password). r=aknow (5d8bcbf177)
- Bug 1159591 - Part 8: Move MMI logic from ril_worker to telephonyService (Call Barring). r=aknow (aa0b89aea1)
- Bug 1159591 - Part 9: Move MMI logic from ril_worker to telephonyService (Call Waiting). r=aknow (1d14d2b864)
- Bug 1159591 - Part 10: Move MMI logic from ril_worker to telephonyService (USSD). r=aknow (c92130b5b2)
- Bug 1159591 - Part 11: Move MMI consts from ril_consts to telephonyService. r=aknow (0e7b3fea9a)
- Bug 1159591 - Part 12: Move radio check for MMI to a common place. r=aknow (dcdc3178ad)
- Bug 1110619 - Part 2: Implementation Changes. r=echen (6d8e78d684)
- Bug 1110619 - Part 3: Bluetooth Changes. r=btian (9787727be0)
- Bug 1147736 - Part 1: Extend TelephonyCallInfo. r=aknow (2a426cc99f)
- Bug 1147736 - Part 2: Bypass NotifyError. r=aknow (6cd6fd6867)
- Bug 1147736 - Part 3: Deprecate NotifyError. r=aknow (e41c719442)
- Bug 1147736 - Part 4: Deprecate NotifyError(Bluetooth). r=btian (9c8f97bb22)
- Bug 1204817 - Delete the child property of a parent call only when the parent call exists. r=btseng (d35dc6b08f)
- Bug 1191205 - Cancel USSD sessions only when needed. r=edgar (01a72dbacb)
- Bug 1200134 - Control USSD Sessions with State-Transitions instead of Boolean. r=echen (a4e55b3d9d)
- Bug 1163511 - Use defineLazyModuleGetter. r=hsinyi (8a97a4912c)
- Bug 991582 - Part 2: Handle the result of RIL request in a consistent way. r=aknow (914ecc2bbb)
- Bug 1164248 - Handling of session/sessionEnded for notifyUssdReceived. r=edgar (a79df75d38)
- Bug 1223662 - Part 1: Check mmiServiceCode with correct constant. r=echen (abeb286050)
- Bug 1174673 - Part 1: Automatically resume the held call. r=hsinyi (280543af7d)
- Bug 1174673 - Part 2: Update test case. r=hsinyi (cfe19f1a52)
- Bug 1185156 - Fix bug in resuming held call. r=hsinyi (b96346d319)
- Bug 1162426 - Part 1: Provide TelephonyUtils. r=hsinyi (62b71e6e83)
- Bug 1162426 - Part 2: Test case. r=hsniyi (13a0b3c6f5)
- Bug 1171807 - Part 1: Add enums for TelephonyCall::State and TelephonyCallGroup::State (WebIDL). r=hsinyi (0b698eecc4)
- Bug 1145551 - DTMF should be sent using the active SIM, the given or the default one (in that order). r=aknow (6157636493)
- Bug 1171807 - Part 2: Move to enums and deprecate TelephonyCall.mCallState and TelephonyCallGroup.mCallState (DOM). r=btseng (5faef22d91)
- Bug 1168515 - do not block incall MMI requests on alerting state. r=aknow (b1f85c5789)
- Bug 1155072 - Part 1: Deprecate nsITelephonyListener.conferenceCallStateChanged (Telephony). r=btseng (e41d1a4bc9)
- Bug 1155072 - Part 2: Deprecate nsITelephonyListener.conferenceCallStateChanged (Bluetooth). r=btian (27e69fa89b)
- Bug 1166936 - JS Warning in TelephonyService.js r=aknow (3559d3ad3d)
- Bug 1191237 - Part 1: Enhance |TelephonyService.js|. r=aknow (86576a6d32)
- Bug 1202902 - Fix the world. (0dc256d67d)
- Bug 1161438 - Part 1 - Exporting contact to SIM should also return updated contact. r=echen (505d7d7f83)
- Bug 1159622 - Split test_icc_contact.js into read contact and add contact. r=echen (fec0c428df)
- Bug 1122376 - Support read SIM contact dialling number exceed 20 digits. r=echen (5d0599e93c)
- Bug 1161438 - Part 2 - marionette testcase. r=echen (1f0d18a479)
- Bug 1114937 - Part 5: Fix Test Case to Remove Contact with Correct Contact Id. r=echen (8d746fdbd2)
- Bug 1194149 - Continue importing contacts when there is no sufficient Type 2 USIM contact fields record. r=echen (e9be40dbf2)
- Bug 962995 - xpcshell tests for write ICC UCS2 characters for 0x81 and 0x82 encoding. r=echen (9500afaa4d)
- Bug 1161438 - Part 3 - xpcshell testcase. r=echen (01f7fb4514)
- Bug 1122376 - Support write SIM contact dialling number exceed 20 digits. r=echen (91133e286d)
- Bug 999300 - Part 1: Removed the Ril v5 legacy support. r=edgar (ded77fcb6f)
- Bug 999300 - Part 2: Update the related testcases. r=edgar (f77a8b96cc)
- Bug 1177146 - [Aries][RIL] Reply from QUERY_AVAILABLE_NETWORKS has extra strings. r=hsinyi (a6816cbbab)
- Bug 1043250 - Part 1: Update ril_worker and xpcshell test. r=btseng (8b9b25b5cf)
- Bug 1185406 - B2G RIL: Read 'pcscf' and expose it in nsIRilNetworkInfo. r=hsinyi (ce707ecb83)
- Bug 1174998 - Part 3: Read data call's MTU from network/apn settings. r=echen (bfa08d8380)
- Bug 1166320 - Make volume service safer to use off main thread. r=dhylands (b3976622ad)
- Bug 1177374 - Call realpath on volume mount points so thatVolumeService::GetVolumeByPath works properly. r=achen (aea97080eb)
- Bug 1195166 - AutoMounter: add ignore command to allow volumes to be ignored. r=alchen (f265d832c8)
- Bug 1196724 - Refactoring of AudioManager r=alwu (e5b896b7ce)
- Bug 1222564 - Save audio volume to database r=alwu (1303d01ae7)
- Bug 1164049 - Fix some mode lines in embedding/. r=smaug (79ddce4871)
2023-01-24 11:52:32 +08:00

854 lines
22 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Telephony.h"
#include "mozilla/Preferences.h"
#include "mozilla/dom/CallEvent.h"
#include "mozilla/dom/MozMobileConnectionBinding.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/TelephonyBinding.h"
#include "nsCharSeparatedTokenizer.h"
#include "nsContentUtils.h"
#include "nsIPermissionManager.h"
#include "nsIURI.h"
#include "nsPIDOMWindow.h"
#include "nsServiceManagerUtils.h"
#include "nsThreadUtils.h"
#include "CallsList.h"
#include "TelephonyCall.h"
#include "TelephonyCallGroup.h"
#include "TelephonyCallId.h"
#include "TelephonyDialCallback.h"
// Service instantiation
#include "ipc/TelephonyIPCService.h"
#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
#include "nsIGonkTelephonyService.h"
#endif
#include "nsXULAppAPI.h" // For XRE_GetProcessType()
using namespace mozilla::dom;
using namespace mozilla::dom::telephony;
using mozilla::ErrorResult;
class Telephony::Listener : public nsITelephonyListener
{
Telephony* mTelephony;
virtual ~Listener() {}
public:
NS_DECL_ISUPPORTS
NS_FORWARD_SAFE_NSITELEPHONYLISTENER(mTelephony)
explicit Listener(Telephony* aTelephony)
: mTelephony(aTelephony)
{
MOZ_ASSERT(mTelephony);
}
void
Disconnect()
{
MOZ_ASSERT(mTelephony);
mTelephony = nullptr;
}
};
Telephony::Telephony(nsPIDOMWindow* aOwner)
: DOMEventTargetHelper(aOwner),
mIsAudioStartPlaying(false),
mHaveDispatchedInterruptBeginEvent(false),
mMuted(AudioChannelService::IsAudioChannelMutedByDefault())
{
MOZ_ASSERT(aOwner);
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aOwner);
MOZ_ASSERT(global);
ErrorResult rv;
RefPtr<Promise> promise = Promise::Create(global, rv);
MOZ_ASSERT(!rv.Failed());
mReadyPromise = promise;
}
Telephony::~Telephony()
{
Shutdown();
}
void
Telephony::Shutdown()
{
if (mListener) {
mListener->Disconnect();
if (mService) {
mService->UnregisterListener(mListener);
mService = nullptr;
}
mListener = nullptr;
}
}
JSObject*
Telephony::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return TelephonyBinding::Wrap(aCx, this, aGivenProto);
}
// static
already_AddRefed<Telephony>
Telephony::Create(nsPIDOMWindow* aOwner, ErrorResult& aRv)
{
NS_ASSERTION(aOwner, "Null owner!");
nsCOMPtr<nsITelephonyService> ril =
do_GetService(TELEPHONY_SERVICE_CONTRACTID);
if (!ril) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aOwner);
if (!sgo) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
if (!scriptContext) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
RefPtr<Telephony> telephony = new Telephony(aOwner);
telephony->mService = ril;
telephony->mListener = new Listener(telephony);
telephony->mCallsList = new CallsList(telephony);
telephony->mGroup = TelephonyCallGroup::Create(telephony);
nsresult rv = ril->EnumerateCalls(telephony->mListener);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return telephony.forget();
}
// static
bool
Telephony::IsValidNumber(const nsAString& aNumber)
{
return !aNumber.IsEmpty();
}
// static
uint32_t
Telephony::GetNumServices() {
return mozilla::Preferences::GetInt("ril.numRadioInterfaces", 1);
}
// static
bool
Telephony::IsValidServiceId(uint32_t aServiceId)
{
return aServiceId < GetNumServices();
}
uint32_t
Telephony::GetServiceId(const Optional<uint32_t>& aServiceId,
bool aGetIfActiveCall)
{
if (aServiceId.WasPassed()) {
return aServiceId.Value();
} else if (aGetIfActiveCall) {
nsTArray<RefPtr<TelephonyCall> > &calls = mCalls;
if (mGroup->IsActive()) {
calls = mGroup->CallsArray();
}
for (uint32_t i = 0; i < calls.Length(); i++) {
if (calls[i]->IsActive()) {
return calls[i]->mServiceId;
}
}
}
uint32_t serviceId = 0;
mService->GetDefaultServiceId(&serviceId);
return serviceId;
}
already_AddRefed<Promise>
Telephony::DialInternal(uint32_t aServiceId, const nsAString& aNumber,
bool aEmergency, ErrorResult& aRv)
{
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
if (!global) {
return nullptr;
}
RefPtr<Promise> promise = Promise::Create(global, aRv);
if (aRv.Failed()) {
return nullptr;
}
if (!IsValidNumber(aNumber) || !IsValidServiceId(aServiceId)) {
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
return promise.forget();
}
nsCOMPtr<nsITelephonyDialCallback> callback =
new TelephonyDialCallback(GetOwner(), this, promise);
nsresult rv = mService->Dial(aServiceId, aNumber, aEmergency, callback);
if (NS_FAILED(rv)) {
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
return promise.forget();
}
return promise.forget();
}
already_AddRefed<TelephonyCallId>
Telephony::CreateCallId(nsITelephonyCallInfo *aInfo)
{
nsAutoString number;
nsAutoString name;
uint16_t numberPresentation;
uint16_t namePresentation;
aInfo->GetNumber(number);
aInfo->GetName(name);
aInfo->GetNumberPresentation(&numberPresentation);
aInfo->GetNamePresentation(&namePresentation);
return CreateCallId(number, numberPresentation, name, namePresentation);
}
already_AddRefed<TelephonyCallId>
Telephony::CreateCallId(const nsAString& aNumber, uint16_t aNumberPresentation,
const nsAString& aName, uint16_t aNamePresentation)
{
RefPtr<TelephonyCallId> id =
new TelephonyCallId(GetOwner(), aNumber, aNumberPresentation,
aName, aNamePresentation);
return id.forget();
}
already_AddRefed<TelephonyCall>
Telephony::CreateCall(TelephonyCallId* aId, uint32_t aServiceId,
uint32_t aCallIndex, TelephonyCallState aState,
bool aEmergency, bool aConference,
bool aSwitchable, bool aMergeable)
{
// We don't have to create an already ended call.
if (aState == TelephonyCallState::Disconnected) {
return nullptr;
}
RefPtr<TelephonyCall> call =
TelephonyCall::Create(this, aId, aServiceId, aCallIndex, aState,
aEmergency, aConference, aSwitchable, aMergeable);
NS_ASSERTION(call, "This should never fail!");
NS_ASSERTION(aConference ? mGroup->CallsArray().Contains(call)
: mCalls.Contains(call),
"Should have auto-added new call!");
return call.forget();
}
nsresult
Telephony::NotifyEvent(const nsAString& aType)
{
return DispatchCallEvent(aType, nullptr);
}
nsresult
Telephony::NotifyCallsChanged(TelephonyCall* aCall)
{
return DispatchCallEvent(NS_LITERAL_STRING("callschanged"), aCall);
}
already_AddRefed<TelephonyCall>
Telephony::GetCall(uint32_t aServiceId, uint32_t aCallIndex)
{
RefPtr<TelephonyCall> call;
for (uint32_t i = 0; i < mCalls.Length(); i++) {
RefPtr<TelephonyCall>& tempCall = mCalls[i];
if (tempCall->ServiceId() == aServiceId &&
tempCall->CallIndex() == aCallIndex) {
call = tempCall;
break;
}
}
return call.forget();
}
already_AddRefed<TelephonyCall>
Telephony::GetCallFromEverywhere(uint32_t aServiceId, uint32_t aCallIndex)
{
RefPtr<TelephonyCall> call = GetCall(aServiceId, aCallIndex);
if (!call) {
call = mGroup->GetCall(aServiceId, aCallIndex);
}
return call.forget();
}
nsresult
Telephony::HandleCallInfo(nsITelephonyCallInfo* aInfo)
{
uint32_t serviceId;
uint32_t callIndex;
uint16_t callState;
bool isEmergency;
bool isConference;
bool isSwitchable;
bool isMergeable;
aInfo->GetClientId(&serviceId);
aInfo->GetCallIndex(&callIndex);
aInfo->GetCallState(&callState);
aInfo->GetIsEmergency(&isEmergency);
aInfo->GetIsConference(&isConference);
aInfo->GetIsSwitchable(&isSwitchable);
aInfo->GetIsMergeable(&isMergeable);
TelephonyCallState state = TelephonyCall::ConvertToTelephonyCallState(callState);
RefPtr<TelephonyCall> call = GetCallFromEverywhere(serviceId, callIndex);
// Handle a newly created call.
if (!call) {
RefPtr<TelephonyCallId> id = CreateCallId(aInfo);
call = CreateCall(id, serviceId, callIndex, state, isEmergency,
isConference, isSwitchable, isMergeable);
// The newly created call is an incoming call.
if (call &&
state == TelephonyCallState::Incoming) {
nsresult rv = DispatchCallEvent(NS_LITERAL_STRING("incoming"), call);
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
}
// Update an existing call
call->UpdateEmergency(isEmergency);
call->UpdateSwitchable(isSwitchable);
call->UpdateMergeable(isMergeable);
nsAutoString number;
aInfo->GetNumber(number);
RefPtr<TelephonyCallId> id = call->Id();
id->UpdateNumber(number);
nsAutoString disconnectedReason;
aInfo->GetDisconnectedReason(disconnectedReason);
// State changed.
if (call->State() != state) {
if (state == TelephonyCallState::Disconnected) {
call->UpdateDisconnectedReason(disconnectedReason);
call->ChangeState(TelephonyCallState::Disconnected);
return NS_OK;
}
// We don't fire the statechange event on a call in conference here.
// Instead, the event will be fired later in
// TelephonyCallGroup::ChangeState(). Thus the sequence of firing the
// statechange events is guaranteed: first on TelephonyCallGroup then on
// individual TelephonyCall objects.
bool fireEvent = !isConference;
call->ChangeStateInternal(state, fireEvent);
}
// Group changed.
RefPtr<TelephonyCallGroup> group = call->GetGroup();
if (!group && isConference) {
// Add to conference.
NS_ASSERTION(mCalls.Contains(call), "Should in mCalls");
mGroup->AddCall(call);
RemoveCall(call);
} else if (group && !isConference) {
// Remove from conference.
NS_ASSERTION(mGroup->CallsArray().Contains(call), "Should in mGroup");
mGroup->RemoveCall(call);
AddCall(call);
}
return NS_OK;
}
NS_IMPL_CYCLE_COLLECTION_CLASS(Telephony)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(Telephony,
DOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCalls)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCallsList)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGroup)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReadyPromise)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(Telephony,
DOMEventTargetHelper)
tmp->Shutdown();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCalls)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCallsList)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mGroup)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mReadyPromise)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(Telephony)
// Telephony does not expose nsITelephonyListener. mListener is the exposed
// nsITelephonyListener and forwards the calls it receives to us.
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(Telephony, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(Telephony, DOMEventTargetHelper)
NS_IMPL_ISUPPORTS(Telephony::Listener, nsITelephonyListener)
// Telephony WebIDL
already_AddRefed<Promise>
Telephony::Dial(const nsAString& aNumber, const Optional<uint32_t>& aServiceId,
ErrorResult& aRv)
{
uint32_t serviceId = GetServiceId(aServiceId);
RefPtr<Promise> promise = DialInternal(serviceId, aNumber, false, aRv);
return promise.forget();
}
already_AddRefed<Promise>
Telephony::DialEmergency(const nsAString& aNumber,
const Optional<uint32_t>& aServiceId,
ErrorResult& aRv)
{
uint32_t serviceId = GetServiceId(aServiceId);
RefPtr<Promise> promise = DialInternal(serviceId, aNumber, true, aRv);
return promise.forget();
}
already_AddRefed<Promise>
Telephony::SendTones(const nsAString& aDTMFChars,
uint32_t aPauseDuration,
uint32_t aToneDuration,
const Optional<uint32_t>& aServiceId,
ErrorResult& aRv)
{
uint32_t serviceId = GetServiceId(aServiceId,
true /* aGetIfActiveCall */);
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
if (!global) {
return nullptr;
}
RefPtr<Promise> promise = Promise::Create(global, aRv);
if (aRv.Failed()) {
return nullptr;
}
if (aDTMFChars.IsEmpty()) {
NS_WARNING("Empty tone string will be ignored");
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
return promise.forget();
}
if (!IsValidServiceId(serviceId)) {
aRv.Throw(NS_ERROR_INVALID_ARG);
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
return promise.forget();
}
nsCOMPtr<nsITelephonyCallback> callback =
new TelephonyCallback(promise);
aRv = mService->SendTones(serviceId, aDTMFChars, aPauseDuration,
aToneDuration, callback);
return promise.forget();
}
void
Telephony::StartTone(const nsAString& aDTMFChar,
const Optional<uint32_t>& aServiceId,
ErrorResult& aRv)
{
uint32_t serviceId = GetServiceId(aServiceId,
true /* aGetIfActiveCall */);
if (aDTMFChar.IsEmpty()) {
NS_WARNING("Empty tone string will be ignored");
return;
}
if (aDTMFChar.Length() > 1 || !IsValidServiceId(serviceId)) {
aRv.Throw(NS_ERROR_INVALID_ARG);
return;
}
aRv = mService->StartTone(serviceId, aDTMFChar);
}
void
Telephony::StopTone(const Optional<uint32_t>& aServiceId, ErrorResult& aRv)
{
uint32_t serviceId = GetServiceId(aServiceId,
true /* aGetIfActiveCall */);
if (!IsValidServiceId(serviceId)) {
aRv.Throw(NS_ERROR_INVALID_ARG);
return;
}
aRv = mService->StopTone(serviceId);
}
void
Telephony::OwnAudioChannel(ErrorResult& aRv)
{
if (mAudioAgent) {
return;
}
mAudioAgent = do_CreateInstance("@mozilla.org/audiochannelagent;1");
MOZ_ASSERT(mAudioAgent);
aRv = mAudioAgent->Init(GetParentObject(),
(int32_t)AudioChannel::Telephony, this);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
aRv = HandleAudioAgentState();
if (NS_WARN_IF(aRv.Failed())) {
return;
}
}
nsresult
Telephony::HandleAudioAgentState()
{
if (!mAudioAgent) {
return NS_OK;
}
Nullable<OwningTelephonyCallOrTelephonyCallGroup> activeCall;
GetActive(activeCall);
nsresult rv;
// Only stop the agent when there's no call.
if ((!mCalls.Length() && !mGroup->CallsArray().Length()) &&
mIsAudioStartPlaying) {
mIsAudioStartPlaying = false;
rv = mAudioAgent->NotifyStoppedPlaying();
mAudioAgent = nullptr;
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
} else if (!activeCall.IsNull() && !mIsAudioStartPlaying) {
mIsAudioStartPlaying = true;
float volume;
bool muted;
rv = mAudioAgent->NotifyStartedPlaying(nsIAudioChannelAgent::AUDIO_AGENT_NOTIFY,
&volume, &muted);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// In B2G, the system app manages audio playback policy. If there is a new
// sound want to be playback, it must wait for the permission from the
// system app. It means that the sound would be muted first, and then be
// unmuted. For telephony, the behaviors are hold() first, then resume().
// However, the telephony service can't handle all these requests within a
// short period. The telephony service would reject our resume request,
// because the modem have not changed the call state yet. It causes that
// the telephony can't be resumed. Therefore, we don't mute the telephony
// at the beginning.
volume = 1.0;
muted = false;
rv = WindowVolumeChanged(volume, muted);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
return NS_OK;
}
bool
Telephony::GetMuted(ErrorResult& aRv) const
{
bool muted = false;
aRv = mService->GetMicrophoneMuted(&muted);
return muted;
}
void
Telephony::SetMuted(bool aMuted, ErrorResult& aRv)
{
aRv = mService->SetMicrophoneMuted(aMuted);
}
bool
Telephony::GetSpeakerEnabled(ErrorResult& aRv) const
{
bool enabled = false;
aRv = mService->GetSpeakerEnabled(&enabled);
return enabled;
}
void
Telephony::SetSpeakerEnabled(bool aEnabled, ErrorResult& aRv)
{
aRv = mService->SetSpeakerEnabled(aEnabled);
}
void
Telephony::GetActive(Nullable<OwningTelephonyCallOrTelephonyCallGroup>& aValue)
{
if (mGroup->IsActive()) {
aValue.SetValue().SetAsTelephonyCallGroup() = mGroup;
return;
}
// Search for the active call.
for (uint32_t i = 0; i < mCalls.Length(); i++) {
if (mCalls[i]->IsActive()) {
aValue.SetValue().SetAsTelephonyCall() = mCalls[i];
return;
}
}
// Nothing active found.
aValue.SetNull();
}
already_AddRefed<CallsList>
Telephony::Calls() const
{
RefPtr<CallsList> list = mCallsList;
return list.forget();
}
already_AddRefed<TelephonyCallGroup>
Telephony::ConferenceGroup() const
{
RefPtr<TelephonyCallGroup> group = mGroup;
return group.forget();
}
already_AddRefed<Promise>
Telephony::GetReady(ErrorResult& aRv) const
{
if (!mReadyPromise) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
RefPtr<Promise> promise = mReadyPromise;
return promise.forget();
}
// nsIAudioChannelAgentCallback
NS_IMETHODIMP
Telephony::WindowVolumeChanged(float aVolume, bool aMuted)
{
// It's impossible to put all the calls on-hold in the multi-call case.
if (mCalls.Length() > 1 ||
(mCalls.Length() == 1 && mGroup->CallsArray().Length())) {
return NS_ERROR_FAILURE;
}
ErrorResult rv;
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
RefPtr<Promise> promise = Promise::Create(global, rv);
if (NS_WARN_IF(rv.Failed())) {
return rv.StealNSResult();
}
bool isSingleCall = mCalls.Length();
nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
if (isSingleCall) {
rv = aMuted ? mCalls[0]->Hold(callback) : mCalls[0]->Resume(callback);
} else {
rv = aMuted ? mGroup->Hold(callback) : mGroup->Resume(callback);
}
if (NS_WARN_IF(rv.Failed())) {
return rv.StealNSResult();
}
// These events will be triggered when the telephony is interrupted by other
// audio channel.
if (mMuted != aMuted) {
mMuted = aMuted;
// We should not dispatch "mozinterruptend" when the system app initializes
// the telephony audio from muted to unmuted at the first time. The event
// "mozinterruptend" must be dispatched after the "mozinterruptbegin".
if (!mHaveDispatchedInterruptBeginEvent && mMuted) {
DispatchTrustedEvent(NS_LITERAL_STRING("mozinterruptbegin"));
mHaveDispatchedInterruptBeginEvent = mMuted;
} else if (mHaveDispatchedInterruptBeginEvent && !mMuted) {
DispatchTrustedEvent(NS_LITERAL_STRING("mozinterruptend"));
mHaveDispatchedInterruptBeginEvent = mMuted;
}
}
return NS_OK;
}
NS_IMETHODIMP
Telephony::WindowAudioCaptureChanged()
{
// Do nothing, it's useless for the telephony object.
return NS_OK;
}
// nsITelephonyListener
NS_IMETHODIMP
Telephony::CallStateChanged(uint32_t aLength, nsITelephonyCallInfo** aAllInfo)
{
// Update call state
nsresult rv;
for (uint32_t i = 0; i < aLength; ++i) {
rv = HandleCallInfo(aAllInfo[i]);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
// Update conference state
mGroup->ChangeState();
rv = HandleAudioAgentState();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
NS_IMETHODIMP
Telephony::EnumerateCallState(nsITelephonyCallInfo* aInfo)
{
return HandleCallInfo(aInfo);
}
NS_IMETHODIMP
Telephony::EnumerateCallStateComplete()
{
// Set conference state.
mGroup->ChangeState();
HandleAudioAgentState();
if (mReadyPromise) {
mReadyPromise->MaybeResolve(JS::UndefinedHandleValue);
}
if (NS_FAILED(mService->RegisterListener(mListener))) {
NS_WARNING("Failed to register listener!");
}
return NS_OK;
}
NS_IMETHODIMP
Telephony::SupplementaryServiceNotification(uint32_t aServiceId,
int32_t aCallIndex,
uint16_t aNotification)
{
RefPtr<TelephonyCall> associatedCall;
if (!mCalls.IsEmpty()) {
associatedCall = GetCall(aServiceId, aCallIndex);
}
nsresult rv;
switch (aNotification) {
case nsITelephonyService::NOTIFICATION_REMOTE_HELD:
rv = DispatchCallEvent(NS_LITERAL_STRING("remoteheld"), associatedCall);
break;
case nsITelephonyService::NOTIFICATION_REMOTE_RESUMED:
rv = DispatchCallEvent(NS_LITERAL_STRING("remoteresumed"), associatedCall);
break;
default:
NS_ERROR("Got a bad notification!");
return NS_ERROR_UNEXPECTED;
}
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
NS_IMETHODIMP
Telephony::NotifyCdmaCallWaiting(uint32_t aServiceId, const nsAString& aNumber,
uint16_t aNumberPresentation,
const nsAString& aName,
uint16_t aNamePresentation)
{
MOZ_ASSERT(mCalls.Length() == 1);
RefPtr<TelephonyCall> callToNotify = mCalls[0];
MOZ_ASSERT(callToNotify && callToNotify->ServiceId() == aServiceId);
RefPtr<TelephonyCallId> id =
new TelephonyCallId(GetOwner(), aNumber, aNumberPresentation, aName,
aNamePresentation);
callToNotify->UpdateSecondId(id);
DispatchCallEvent(NS_LITERAL_STRING("callschanged"), callToNotify);
return NS_OK;
}
NS_IMETHODIMP
Telephony::NotifyConferenceError(const nsAString& aName,
const nsAString& aMessage)
{
mGroup->NotifyError(aName, aMessage);
return NS_OK;
}
nsresult
Telephony::DispatchCallEvent(const nsAString& aType,
TelephonyCall* aCall)
{
// If it is an incoming event, the call should not be null.
MOZ_ASSERT(!aType.EqualsLiteral("incoming") || aCall);
CallEventInit init;
init.mBubbles = false;
init.mCancelable = false;
init.mCall = aCall;
RefPtr<CallEvent> event = CallEvent::Constructor(this, aType, init);
return DispatchTrustedEvent(event);
}
already_AddRefed<nsITelephonyService>
NS_CreateTelephonyService()
{
nsCOMPtr<nsITelephonyService> service;
if (XRE_IsContentProcess()) {
service = new mozilla::dom::telephony::TelephonyIPCService();
} else {
#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
service = do_CreateInstance(GONK_TELEPHONY_SERVICE_CONTRACTID);
#endif
}
return service.forget();
}