2012-01-09 14:28:47 -08:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=40: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
#include "TelephonyCall.h"
|
2013-07-15 01:27:19 -07:00
|
|
|
#include "mozilla/dom/TelephonyCallBinding.h"
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-05-18 10:52:06 -07:00
|
|
|
#include "mozilla/dom/DOMError.h"
|
2013-07-15 01:27:19 -07:00
|
|
|
|
|
|
|
#include "CallEvent.h"
|
2013-01-27 23:39:50 -08:00
|
|
|
#include "Telephony.h"
|
2013-07-06 03:24:55 -07:00
|
|
|
#include "TelephonyCallGroup.h"
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
using namespace mozilla::dom;
|
2013-09-06 23:19:53 -07:00
|
|
|
using mozilla::ErrorResult;
|
2013-09-11 20:47:14 -07:00
|
|
|
using mozilla::dom::telephony::kOutgoingPlaceholderCallIndex;
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<TelephonyCall>
|
2013-10-31 05:05:51 -07:00
|
|
|
TelephonyCall::Create(Telephony* aTelephony, uint32_t aServiceId,
|
|
|
|
const nsAString& aNumber, uint16_t aCallState,
|
2014-02-21 01:52:13 -08:00
|
|
|
uint32_t aCallIndex, bool aEmergency, bool aIsConference,
|
|
|
|
bool aSwitchable, bool aMergeable)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aTelephony, "Null pointer!");
|
|
|
|
NS_ASSERTION(!aNumber.IsEmpty(), "Empty number!");
|
|
|
|
NS_ASSERTION(aCallIndex >= 1, "Invalid call index!");
|
|
|
|
|
2014-01-06 18:53:23 -08:00
|
|
|
nsRefPtr<TelephonyCall> call = new TelephonyCall(aTelephony->GetOwner());
|
2012-03-13 15:11:16 -07:00
|
|
|
|
2012-01-09 14:28:47 -08:00
|
|
|
call->mTelephony = aTelephony;
|
2013-10-31 05:05:51 -07:00
|
|
|
call->mServiceId = aServiceId;
|
2012-01-09 14:28:47 -08:00
|
|
|
call->mNumber = aNumber;
|
|
|
|
call->mCallIndex = aCallIndex;
|
2012-07-30 07:20:58 -07:00
|
|
|
call->mError = nullptr;
|
2013-05-13 23:08:07 -07:00
|
|
|
call->mEmergency = aEmergency;
|
2013-07-06 03:24:55 -07:00
|
|
|
call->mGroup = aIsConference ? aTelephony->ConferenceGroup() : nullptr;
|
2014-02-21 01:52:13 -08:00
|
|
|
call->mSwitchable = aSwitchable;
|
|
|
|
call->mMergeable = aMergeable;
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
call->ChangeStateInternal(aCallState, false);
|
|
|
|
|
|
|
|
return call.forget();
|
|
|
|
}
|
|
|
|
|
2014-01-06 18:53:23 -08:00
|
|
|
TelephonyCall::TelephonyCall(nsPIDOMWindow* aOwner)
|
2014-03-31 23:13:50 -07:00
|
|
|
: DOMEventTargetHelper(aOwner),
|
2014-01-06 18:53:23 -08:00
|
|
|
mCallIndex(kOutgoingPlaceholderCallIndex),
|
2013-03-06 01:53:23 -08:00
|
|
|
mCallState(nsITelephonyProvider::CALL_STATE_UNKNOWN),
|
2014-01-28 22:27:22 -08:00
|
|
|
mLive(false)
|
2013-03-06 01:53:23 -08:00
|
|
|
{
|
2013-07-15 01:27:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TelephonyCall::~TelephonyCall()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
TelephonyCall::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return TelephonyCallBinding::Wrap(aCx, aScope, this);
|
2013-03-06 01:53:23 -08:00
|
|
|
}
|
|
|
|
|
2012-01-09 14:28:47 -08:00
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
TelephonyCall::ChangeStateInternal(uint16_t aCallState, bool aFireEvents)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
|
|
|
nsRefPtr<TelephonyCall> kungFuDeathGrip(this);
|
|
|
|
|
|
|
|
nsString stateString;
|
|
|
|
switch (aCallState) {
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_DIALING:
|
2012-01-09 14:28:47 -08:00
|
|
|
stateString.AssignLiteral("dialing");
|
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_ALERTING:
|
2012-03-15 20:13:42 -07:00
|
|
|
stateString.AssignLiteral("alerting");
|
2012-01-09 14:28:47 -08:00
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_CONNECTING:
|
2012-01-09 14:28:47 -08:00
|
|
|
stateString.AssignLiteral("connecting");
|
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_CONNECTED:
|
2012-01-09 14:28:47 -08:00
|
|
|
stateString.AssignLiteral("connected");
|
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_HOLDING:
|
2012-01-09 14:28:47 -08:00
|
|
|
stateString.AssignLiteral("holding");
|
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_HELD:
|
2012-01-09 14:28:47 -08:00
|
|
|
stateString.AssignLiteral("held");
|
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_RESUMING:
|
2012-01-09 14:28:47 -08:00
|
|
|
stateString.AssignLiteral("resuming");
|
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_DISCONNECTING:
|
2012-01-09 14:28:47 -08:00
|
|
|
stateString.AssignLiteral("disconnecting");
|
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_DISCONNECTED:
|
2012-01-09 14:28:47 -08:00
|
|
|
stateString.AssignLiteral("disconnected");
|
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_INCOMING:
|
2012-01-09 14:28:47 -08:00
|
|
|
stateString.AssignLiteral("incoming");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown state!");
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = stateString;
|
|
|
|
mCallState = aCallState;
|
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
if (aCallState == nsITelephonyProvider::CALL_STATE_DISCONNECTED) {
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_ASSERTION(mLive, "Should be live!");
|
2013-07-06 03:24:55 -07:00
|
|
|
if (mGroup) {
|
|
|
|
mGroup->RemoveCall(this);
|
|
|
|
} else {
|
|
|
|
mTelephony->RemoveCall(this);
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
mLive = false;
|
|
|
|
} else if (!mLive) {
|
2013-07-06 03:24:55 -07:00
|
|
|
if (mGroup) {
|
|
|
|
mGroup->AddCall(this);
|
|
|
|
} else {
|
|
|
|
mTelephony->AddCall(this);
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
mLive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aFireEvents) {
|
2013-01-27 23:39:50 -08:00
|
|
|
nsresult rv = DispatchCallEvent(NS_LITERAL_STRING("statechange"), this);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch specific event!");
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This can change if the statechange handler called back here... Need to
|
|
|
|
// figure out something smarter.
|
|
|
|
if (mCallState == aCallState) {
|
2013-01-27 23:39:50 -08:00
|
|
|
rv = DispatchCallEvent(stateString, this);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_WARNING("Failed to dispatch specific event!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-27 23:39:50 -08:00
|
|
|
nsresult
|
|
|
|
TelephonyCall::DispatchCallEvent(const nsAString& aType,
|
2013-07-15 01:27:19 -07:00
|
|
|
TelephonyCall* aCall)
|
2013-01-27 23:39:50 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCall);
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
nsRefPtr<CallEvent> event = CallEvent::Create(this, aType, aCall, false, false);
|
2013-01-27 23:39:50 -08:00
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
return DispatchTrustedEvent(event);
|
2013-01-27 23:39:50 -08:00
|
|
|
}
|
|
|
|
|
2012-05-14 21:13:06 -07:00
|
|
|
void
|
|
|
|
TelephonyCall::NotifyError(const nsAString& aError)
|
|
|
|
{
|
2012-05-14 21:13:06 -07:00
|
|
|
// Set the error string
|
|
|
|
NS_ASSERTION(!mError, "Already have an error?");
|
|
|
|
|
2013-09-11 20:47:14 -07:00
|
|
|
mError = new DOMError(GetOwner(), aError);
|
2012-05-14 21:13:06 -07:00
|
|
|
|
2012-05-14 21:13:06 -07:00
|
|
|
// Do the state transitions
|
2013-03-06 01:53:23 -08:00
|
|
|
ChangeStateInternal(nsITelephonyProvider::CALL_STATE_DISCONNECTED, true);
|
2012-05-14 21:13:06 -07:00
|
|
|
|
2013-01-27 23:39:50 -08:00
|
|
|
nsresult rv = DispatchCallEvent(NS_LITERAL_STRING("error"), this);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-05-14 21:13:06 -07:00
|
|
|
NS_WARNING("Failed to dispatch error event!");
|
|
|
|
}
|
2012-05-14 21:13:06 -07:00
|
|
|
}
|
|
|
|
|
2013-07-06 03:24:55 -07:00
|
|
|
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_3(TelephonyCall,
|
2014-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper,
|
2013-05-27 13:43:09 -07:00
|
|
|
mTelephony,
|
2013-07-06 03:24:55 -07:00
|
|
|
mError,
|
|
|
|
mGroup);
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TelephonyCall)
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(TelephonyCall, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(TelephonyCall, DOMEventTargetHelper)
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
// TelephonyCall WebIDL
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
already_AddRefed<DOMError>
|
|
|
|
TelephonyCall::GetError() const
|
2013-05-13 23:08:07 -07:00
|
|
|
{
|
2013-07-15 01:27:19 -07:00
|
|
|
nsRefPtr<DOMError> error = mError;
|
|
|
|
return error.forget();
|
2013-05-13 23:08:07 -07:00
|
|
|
}
|
|
|
|
|
2013-07-06 03:24:55 -07:00
|
|
|
already_AddRefed<TelephonyCallGroup>
|
|
|
|
TelephonyCall::GetGroup() const
|
|
|
|
{
|
|
|
|
nsRefPtr<TelephonyCallGroup> group = mGroup;
|
|
|
|
return group.forget();
|
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
void
|
|
|
|
TelephonyCall::Answer(ErrorResult& aRv)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2013-03-06 01:53:23 -08:00
|
|
|
if (mCallState != nsITelephonyProvider::CALL_STATE_INCOMING) {
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_WARNING("Answer on non-incoming call ignored!");
|
2013-07-15 01:27:19 -07:00
|
|
|
return;
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-10-31 05:05:51 -07:00
|
|
|
nsresult rv = mTelephony->Provider()->AnswerCall(mServiceId, mCallIndex);
|
2013-07-15 01:27:19 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
ChangeStateInternal(nsITelephonyProvider::CALL_STATE_CONNECTING, true);
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
void
|
|
|
|
TelephonyCall::HangUp(ErrorResult& aRv)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2013-03-06 01:53:23 -08:00
|
|
|
if (mCallState == nsITelephonyProvider::CALL_STATE_DISCONNECTING ||
|
|
|
|
mCallState == nsITelephonyProvider::CALL_STATE_DISCONNECTED) {
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_WARNING("HangUp on previously disconnected call ignored!");
|
2013-07-15 01:27:19 -07:00
|
|
|
return;
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
nsresult rv = mCallState == nsITelephonyProvider::CALL_STATE_INCOMING ?
|
2013-10-31 05:05:51 -07:00
|
|
|
mTelephony->Provider()->RejectCall(mServiceId, mCallIndex) :
|
|
|
|
mTelephony->Provider()->HangUp(mServiceId, mCallIndex);
|
2013-07-15 01:27:19 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
ChangeStateInternal(nsITelephonyProvider::CALL_STATE_DISCONNECTING, true);
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
void
|
|
|
|
TelephonyCall::Hold(ErrorResult& aRv)
|
2012-04-05 01:12:42 -07:00
|
|
|
{
|
2013-03-06 01:53:23 -08:00
|
|
|
if (mCallState != nsITelephonyProvider::CALL_STATE_CONNECTED) {
|
2012-04-05 01:12:42 -07:00
|
|
|
NS_WARNING("Hold non-connected call ignored!");
|
2013-07-15 01:27:19 -07:00
|
|
|
return;
|
2012-04-05 01:12:42 -07:00
|
|
|
}
|
2013-01-27 23:39:50 -08:00
|
|
|
|
2013-07-06 03:24:55 -07:00
|
|
|
if (mGroup) {
|
|
|
|
NS_WARNING("Hold a call in conference ignored!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-21 01:52:13 -08:00
|
|
|
if (!mSwitchable) {
|
|
|
|
NS_WARNING("Hold a non-switchable call ignored!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-31 05:05:51 -07:00
|
|
|
nsresult rv = mTelephony->Provider()->HoldCall(mServiceId, mCallIndex);
|
2013-07-15 01:27:19 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2013-01-27 23:39:50 -08:00
|
|
|
|
2013-08-16 02:01:41 -07:00
|
|
|
if (!mSecondNumber.IsEmpty()) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
ChangeStateInternal(nsITelephonyProvider::CALL_STATE_HOLDING, true);
|
2012-04-05 01:12:42 -07:00
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
void
|
|
|
|
TelephonyCall::Resume(ErrorResult& aRv)
|
2012-04-05 01:12:42 -07:00
|
|
|
{
|
2013-03-06 01:53:23 -08:00
|
|
|
if (mCallState != nsITelephonyProvider::CALL_STATE_HELD) {
|
2012-04-05 01:12:42 -07:00
|
|
|
NS_WARNING("Resume non-held call ignored!");
|
2013-07-15 01:27:19 -07:00
|
|
|
return;
|
2012-04-05 01:12:42 -07:00
|
|
|
}
|
2013-01-27 23:39:50 -08:00
|
|
|
|
2013-07-06 03:24:55 -07:00
|
|
|
if (mGroup) {
|
|
|
|
NS_WARNING("Resume a call in conference ignored!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-21 01:52:13 -08:00
|
|
|
if (!mSwitchable) {
|
|
|
|
NS_WARNING("Resume a non-switchable call ignored!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-31 05:05:51 -07:00
|
|
|
nsresult rv = mTelephony->Provider()->ResumeCall(mServiceId, mCallIndex);
|
2013-07-15 01:27:19 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2013-01-27 23:39:50 -08:00
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
ChangeStateInternal(nsITelephonyProvider::CALL_STATE_RESUMING, true);
|
2012-04-05 01:12:42 -07:00
|
|
|
}
|