Files
palemoon27/dom/telephony/TelephonyCall.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

454 lines
13 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 "TelephonyCall.h"
#include "mozilla/dom/CallEvent.h"
#include "mozilla/dom/TelephonyCallBinding.h"
#include "mozilla/dom/telephony/TelephonyCallback.h"
#include "mozilla/dom/DOMError.h"
#include "nsPrintfCString.h"
#include "Telephony.h"
#include "TelephonyCallGroup.h"
#ifdef CONVERT_STRING_TO_NULLABLE_ENUM
#undef CONVERT_STRING_TO_NULLABLE_ENUM
#endif
#define CONVERT_STRING_TO_NULLABLE_ENUM(_string, _enumType, _enum) \
{ \
_enum.SetNull(); \
\
uint32_t i = 0; \
for (const EnumEntry* entry = _enumType##Values::strings; \
entry->value; \
++entry, ++i) { \
if (_string.EqualsASCII(entry->value)) { \
_enum.SetValue(static_cast<_enumType>(i)); \
break; \
} \
} \
}
#ifdef TELEPHONY_CALL_STATE
#undef TELEPHONY_CALL_STATE
#endif
#define TELEPHONY_CALL_STATE(_state) \
(TelephonyCallStateValues::strings[static_cast<int32_t>(_state)].value)
using namespace mozilla::dom;
using namespace mozilla::dom::telephony;
using mozilla::ErrorResult;
// static
TelephonyCallState
TelephonyCall::ConvertToTelephonyCallState(uint32_t aCallState)
{
switch (aCallState) {
case nsITelephonyService::CALL_STATE_DIALING:
return TelephonyCallState::Dialing;
case nsITelephonyService::CALL_STATE_ALERTING:
return TelephonyCallState::Alerting;
case nsITelephonyService::CALL_STATE_CONNECTED:
return TelephonyCallState::Connected;
case nsITelephonyService::CALL_STATE_HELD:
return TelephonyCallState::Held;
case nsITelephonyService::CALL_STATE_DISCONNECTED:
return TelephonyCallState::Disconnected;
case nsITelephonyService::CALL_STATE_INCOMING:
return TelephonyCallState::Incoming;
}
NS_NOTREACHED("Unknown state!");
return TelephonyCallState::Disconnected;
}
// static
already_AddRefed<TelephonyCall>
TelephonyCall::Create(Telephony* aTelephony,
TelephonyCallId* aId,
uint32_t aServiceId,
uint32_t aCallIndex,
TelephonyCallState aState,
bool aEmergency,
bool aConference,
bool aSwitchable,
bool aMergeable)
{
NS_ASSERTION(aTelephony, "Null aTelephony pointer!");
NS_ASSERTION(aId, "Null aId pointer!");
NS_ASSERTION(aCallIndex >= 1, "Invalid call index!");
RefPtr<TelephonyCall> call = new TelephonyCall(aTelephony->GetOwner());
call->mTelephony = aTelephony;
call->mId = aId;
call->mServiceId = aServiceId;
call->mCallIndex = aCallIndex;
call->mEmergency = aEmergency;
call->mGroup = aConference ? aTelephony->ConferenceGroup() : nullptr;
call->mSwitchable = aSwitchable;
call->mMergeable = aMergeable;
call->mError = nullptr;
call->ChangeStateInternal(aState, false);
return call.forget();
}
TelephonyCall::TelephonyCall(nsPIDOMWindow* aOwner)
: DOMEventTargetHelper(aOwner),
mLive(false)
{
}
TelephonyCall::~TelephonyCall()
{
}
JSObject*
TelephonyCall::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return TelephonyCallBinding::Wrap(aCx, this, aGivenProto);
}
void
TelephonyCall::ChangeStateInternal(TelephonyCallState aState, bool aFireEvents)
{
RefPtr<TelephonyCall> kungFuDeathGrip(this);
// Update current state
mState = aState;
// Handle disconnected calls
if (mState == TelephonyCallState::Disconnected) {
NS_ASSERTION(mLive, "Should be live!");
mLive = false;
if (mGroup) {
mGroup->RemoveCall(this);
} else {
mTelephony->RemoveCall(this);
}
} else if (!mLive) { // Handle newly added calls
mLive = true;
if (mGroup) {
mGroup->AddCall(this);
} else {
mTelephony->AddCall(this);
}
}
// Dispatch call state changed and call state event
if (aFireEvents) {
NotifyStateChanged();
}
}
nsresult
TelephonyCall::NotifyStateChanged()
{
// Since |mState| can be changed after statechange handler called back here,
// we must save current state. Maybe we should figure out something smarter.
TelephonyCallState prevState = mState;
nsresult res = DispatchCallEvent(NS_LITERAL_STRING("statechange"), this);
if (NS_FAILED(res)) {
NS_WARNING("Failed to dispatch specific event!");
}
// Check whether |mState| remains the same after the statechange handler.
if (mState != prevState) {
NS_WARNING("Call State has changed by statechange handler!");
return res;
}
res = DispatchCallEvent(NS_ConvertASCIItoUTF16(TELEPHONY_CALL_STATE(mState)),
this);
if (NS_FAILED(res)) {
NS_WARNING("Failed to dispatch a specific event!");
}
return res;
}
nsresult
TelephonyCall::DispatchCallEvent(const nsAString& aType,
TelephonyCall* aCall)
{
MOZ_ASSERT(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<Promise>
TelephonyCall::CreatePromise(ErrorResult& aRv)
{
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
if (!global) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<Promise> promise = Promise::Create(global, aRv);
if (aRv.Failed()) {
return nullptr;
}
return promise.forget();
}
void
TelephonyCall::NotifyError(const nsAString& aError)
{
// Set the error string
NS_ASSERTION(!mError, "Already have an error?");
mError = new DOMError(GetOwner(), aError);
nsresult rv = DispatchCallEvent(NS_LITERAL_STRING("error"), this);
if (NS_FAILED(rv)) {
NS_WARNING("Failed to dispatch error event!");
}
}
void
TelephonyCall::UpdateDisconnectedReason(const nsAString& aDisconnectedReason)
{
NS_ASSERTION(Substring(aDisconnectedReason,
aDisconnectedReason.Length() - 5).EqualsLiteral("Error"),
"Disconnected reason should end with 'Error'");
if (!mDisconnectedReason.IsNull()) {
return;
}
// There is no 'Error' suffix in the corresponding enum. We should skip
// that part for comparison.
CONVERT_STRING_TO_NULLABLE_ENUM(
Substring(aDisconnectedReason, 0, aDisconnectedReason.Length() - 5),
TelephonyCallDisconnectedReason,
mDisconnectedReason);
if (!aDisconnectedReason.EqualsLiteral("NormalCallClearingError")) {
NotifyError(aDisconnectedReason);
}
}
void
TelephonyCall::ChangeGroup(TelephonyCallGroup* aGroup)
{
mGroup = aGroup;
nsresult rv = DispatchCallEvent(NS_LITERAL_STRING("groupchange"), this);
if (NS_FAILED(rv)) {
NS_WARNING("Failed to dispatch error event!");
}
}
NS_IMPL_CYCLE_COLLECTION_INHERITED(TelephonyCall,
DOMEventTargetHelper,
mTelephony,
mError,
mGroup,
mId,
mSecondId);
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TelephonyCall)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(TelephonyCall, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(TelephonyCall, DOMEventTargetHelper)
// TelephonyCall WebIDL
already_AddRefed<TelephonyCallId>
TelephonyCall::Id() const
{
RefPtr<TelephonyCallId> id = mId;
return id.forget();
}
already_AddRefed<TelephonyCallId>
TelephonyCall::GetSecondId() const
{
RefPtr<TelephonyCallId> id = mSecondId;
return id.forget();
}
already_AddRefed<DOMError>
TelephonyCall::GetError() const
{
RefPtr<DOMError> error = mError;
return error.forget();
}
already_AddRefed<TelephonyCallGroup>
TelephonyCall::GetGroup() const
{
RefPtr<TelephonyCallGroup> group = mGroup;
return group.forget();
}
already_AddRefed<Promise>
TelephonyCall::Answer(ErrorResult& aRv)
{
RefPtr<Promise> promise = CreatePromise(aRv);
if (!promise) {
return nullptr;
}
if (mState != TelephonyCallState::Incoming) {
NS_WARNING(nsPrintfCString("Answer on non-incoming call is rejected!"
" (State: %s)",
TELEPHONY_CALL_STATE(mState)).get());
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
return promise.forget();
}
nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
aRv = mTelephony->Service()->AnswerCall(mServiceId, mCallIndex, callback);
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
return promise.forget();
}
already_AddRefed<Promise>
TelephonyCall::HangUp(ErrorResult& aRv)
{
RefPtr<Promise> promise = CreatePromise(aRv);
if (!promise) {
return nullptr;
}
if (mState == TelephonyCallState::Disconnected) {
NS_WARNING(nsPrintfCString("HangUp on a disconnected call is rejected!"
" (State: %s)",
TELEPHONY_CALL_STATE(mState)).get());
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
return promise.forget();
}
nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
aRv = mState == TelephonyCallState::Incoming ?
mTelephony->Service()->RejectCall(mServiceId, mCallIndex, callback) :
mTelephony->Service()->HangUpCall(mServiceId, mCallIndex, callback);
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
return promise.forget();
}
already_AddRefed<Promise>
TelephonyCall::Hold(ErrorResult& aRv)
{
RefPtr<Promise> promise = CreatePromise(aRv);
if (!promise) {
return nullptr;
}
nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
aRv = Hold(callback);
if (NS_WARN_IF(aRv.Failed() &&
!aRv.ErrorCodeIs(NS_ERROR_DOM_INVALID_STATE_ERR))) {
return nullptr;
}
return promise.forget();
}
nsresult
TelephonyCall::Hold(nsITelephonyCallback* aCallback)
{
if (mState != TelephonyCallState::Connected) {
NS_WARNING(nsPrintfCString("Hold non-connected call is rejected!"
" (State: %s)",
TELEPHONY_CALL_STATE(mState)).get());
aCallback->NotifyError(NS_LITERAL_STRING("InvalidStateError"));
return NS_ERROR_DOM_INVALID_STATE_ERR;
}
if (mGroup) {
NS_WARNING("Hold a call in conference is rejected!");
aCallback->NotifyError(NS_LITERAL_STRING("InvalidStateError"));
return NS_ERROR_DOM_INVALID_STATE_ERR;
}
if (!mSwitchable) {
NS_WARNING("Hold a non-switchable call is rejected!");
aCallback->NotifyError(NS_LITERAL_STRING("InvalidStateError"));
return NS_ERROR_DOM_INVALID_STATE_ERR;
}
nsresult rv = mTelephony->Service()->HoldCall(mServiceId, mCallIndex, aCallback);
if (NS_WARN_IF(NS_FAILED(rv))) {
return NS_ERROR_FAILURE;
}
if (mSecondId) {
// No state transition when we switch two numbers within one TelephonyCall
// object. Otherwise, the state here will be inconsistent with the backend
// RIL and will never be right.
return NS_OK;
}
return NS_OK;
}
already_AddRefed<Promise>
TelephonyCall::Resume(ErrorResult& aRv)
{
RefPtr<Promise> promise = CreatePromise(aRv);
if (!promise) {
return nullptr;
}
nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
aRv = Resume(callback);
if (NS_WARN_IF(aRv.Failed() &&
!aRv.ErrorCodeIs(NS_ERROR_DOM_INVALID_STATE_ERR))) {
return nullptr;
}
return promise.forget();
}
nsresult
TelephonyCall::Resume(nsITelephonyCallback* aCallback)
{
if (mState != TelephonyCallState::Held) {
NS_WARNING(nsPrintfCString("Resume non-held call is rejected!"
" (State: %s)",
TELEPHONY_CALL_STATE(mState)).get());
aCallback->NotifyError(NS_LITERAL_STRING("InvalidStateError"));
return NS_ERROR_DOM_INVALID_STATE_ERR;
}
if (mGroup) {
NS_WARNING("Resume a call in conference is rejected!");
aCallback->NotifyError(NS_LITERAL_STRING("InvalidStateError"));
return NS_ERROR_DOM_INVALID_STATE_ERR;
}
if (!mSwitchable) {
NS_WARNING("Resume a non-switchable call is rejected!");
aCallback->NotifyError(NS_LITERAL_STRING("InvalidStateError"));
return NS_ERROR_DOM_INVALID_STATE_ERR;
}
nsresult rv = mTelephony->Service()->ResumeCall(mServiceId, mCallIndex, aCallback);
if (NS_WARN_IF(NS_FAILED(rv))) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}