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-01-28 11:39:50 +04:00
|
|
|
#include "nsIDOMCallEvent.h"
|
2012-01-10 02:28:47 +04:00
|
|
|
|
2012-05-15 08:13:06 +04:00
|
|
|
#include "DOMError.h"
|
2013-01-28 11:39:50 +04:00
|
|
|
#include "GeneratedEvents.h"
|
|
|
|
#include "nsDOMClassInfo.h"
|
|
|
|
#include "Telephony.h"
|
2013-03-06 13:53:23 +04:00
|
|
|
#include "nsITelephonyProvider.h"
|
2012-01-10 02:28:47 +04:00
|
|
|
|
|
|
|
USING_TELEPHONY_NAMESPACE
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<TelephonyCall>
|
|
|
|
TelephonyCall::Create(Telephony* aTelephony, const nsAString& aNumber,
|
2013-05-14 10:08:07 +04:00
|
|
|
uint16_t aCallState, uint32_t aCallIndex, bool aEmergency)
|
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;
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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_BUSY:
|
2012-01-10 02:28:47 +04:00
|
|
|
stateString.AssignLiteral("busy");
|
|
|
|
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!");
|
|
|
|
mTelephony->RemoveCall(this);
|
|
|
|
mLive = false;
|
|
|
|
} else if (!mLive) {
|
|
|
|
mTelephony->AddCall(this);
|
|
|
|
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,
|
|
|
|
nsIDOMTelephonyCall* aCall)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCall);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2013-03-09 15:34:29 +04:00
|
|
|
NS_NewDOMCallEvent(getter_AddRefs(event), this, nullptr, nullptr);
|
2013-01-28 11:39:50 +04:00
|
|
|
NS_ASSERTION(event, "This should never fail!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMCallEvent> callEvent = do_QueryInterface(event);
|
|
|
|
MOZ_ASSERT(callEvent);
|
|
|
|
nsresult rv = callEvent->InitCallEvent(aType, false, false, aCall);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return DispatchTrustedEvent(callEvent);
|
|
|
|
}
|
|
|
|
|
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?");
|
|
|
|
|
|
|
|
mError = DOMError::CreateWithName(aError);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-11-15 23:31:56 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(TelephonyCall,
|
|
|
|
nsDOMEventTargetHelper,
|
|
|
|
mTelephony)
|
2012-01-10 02:28:47 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TelephonyCall)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMTelephonyCall)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(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
|
|
|
|
|
|
|
DOMCI_DATA(TelephonyCall, TelephonyCall)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TelephonyCall::GetNumber(nsAString& aNumber)
|
|
|
|
{
|
|
|
|
aNumber.Assign(mNumber);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TelephonyCall::GetState(nsAString& aState)
|
|
|
|
{
|
|
|
|
aState.Assign(mState);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-14 10:08:07 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TelephonyCall::GetEmergency(bool* aEmergency)
|
|
|
|
{
|
|
|
|
*aEmergency = mEmergency;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-15 08:13:06 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TelephonyCall::GetError(nsIDOMDOMError** aError)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aError = mError);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TelephonyCall::Answer()
|
|
|
|
{
|
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!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-06 13:53:23 +04:00
|
|
|
nsresult rv = mTelephony->Provider()->AnswerCall(mCallIndex);
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-03-06 13:53:23 +04:00
|
|
|
ChangeStateInternal(nsITelephonyProvider::CALL_STATE_CONNECTING, true);
|
2012-01-10 02:28:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TelephonyCall::HangUp()
|
|
|
|
{
|
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!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-06 13:53:23 +04:00
|
|
|
nsresult rv = mCallState == nsITelephonyProvider::CALL_STATE_INCOMING ?
|
|
|
|
mTelephony->Provider()->RejectCall(mCallIndex) :
|
|
|
|
mTelephony->Provider()->HangUp(mCallIndex);
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-03-06 13:53:23 +04:00
|
|
|
ChangeStateInternal(nsITelephonyProvider::CALL_STATE_DISCONNECTING, true);
|
2012-01-10 02:28:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-05 12:12:42 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TelephonyCall::Hold()
|
|
|
|
{
|
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!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-01-28 11:39:50 +04:00
|
|
|
|
2013-03-06 13:53:23 +04:00
|
|
|
nsresult rv = mTelephony->Provider()->HoldCall(mCallIndex);
|
2012-04-05 12:12:42 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
2013-01-28 11:39:50 +04:00
|
|
|
|
2013-03-06 13:53:23 +04:00
|
|
|
ChangeStateInternal(nsITelephonyProvider::CALL_STATE_HOLDING, true);
|
2012-04-05 12:12:42 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TelephonyCall::Resume()
|
|
|
|
{
|
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!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-01-28 11:39:50 +04:00
|
|
|
|
2013-03-06 13:53:23 +04:00
|
|
|
nsresult rv = mTelephony->Provider()->ResumeCall(mCallIndex);
|
2012-04-05 12:12:42 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
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
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_IMPL_EVENT_HANDLER(TelephonyCall, statechange)
|
|
|
|
NS_IMPL_EVENT_HANDLER(TelephonyCall, dialing)
|
2012-03-16 07:13:42 +04:00
|
|
|
NS_IMPL_EVENT_HANDLER(TelephonyCall, alerting)
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_IMPL_EVENT_HANDLER(TelephonyCall, busy)
|
|
|
|
NS_IMPL_EVENT_HANDLER(TelephonyCall, connecting)
|
|
|
|
NS_IMPL_EVENT_HANDLER(TelephonyCall, connected)
|
|
|
|
NS_IMPL_EVENT_HANDLER(TelephonyCall, disconnecting)
|
|
|
|
NS_IMPL_EVENT_HANDLER(TelephonyCall, disconnected)
|
2012-04-05 12:12:42 +04:00
|
|
|
NS_IMPL_EVENT_HANDLER(TelephonyCall, holding)
|
|
|
|
NS_IMPL_EVENT_HANDLER(TelephonyCall, held)
|
|
|
|
NS_IMPL_EVENT_HANDLER(TelephonyCall, resuming)
|
2012-05-15 08:13:06 +04:00
|
|
|
NS_IMPL_EVENT_HANDLER(TelephonyCall, error)
|