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