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"
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
USING_TELEPHONY_NAMESPACE
|
2013-07-15 01:27:19 -07:00
|
|
|
using namespace mozilla::dom;
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<TelephonyCall>
|
|
|
|
TelephonyCall::Create(Telephony* aTelephony, const nsAString& aNumber,
|
2013-05-13 23:08:07 -07:00
|
|
|
uint16_t aCallState, uint32_t aCallIndex, bool aEmergency)
|
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!");
|
|
|
|
|
|
|
|
nsRefPtr<TelephonyCall> call = new TelephonyCall();
|
|
|
|
|
2012-03-13 15:11:16 -07:00
|
|
|
call->BindToOwner(aTelephony->GetOwner());
|
|
|
|
|
2012-01-09 14:28:47 -08:00
|
|
|
call->mTelephony = aTelephony;
|
|
|
|
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;
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
call->ChangeStateInternal(aCallState, false);
|
|
|
|
|
|
|
|
return call.forget();
|
|
|
|
}
|
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
TelephonyCall::TelephonyCall()
|
|
|
|
: mCallIndex(kOutgoingPlaceholderCallIndex),
|
|
|
|
mCallState(nsITelephonyProvider::CALL_STATE_UNKNOWN),
|
|
|
|
mLive(false),
|
|
|
|
mOutgoing(false)
|
|
|
|
{
|
2013-07-15 01:27:19 -07:00
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
|
|
|
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_DIALING) {
|
2012-01-11 18:17:26 -08:00
|
|
|
mOutgoing = true;
|
|
|
|
}
|
|
|
|
|
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!");
|
|
|
|
mTelephony->RemoveCall(this);
|
|
|
|
mLive = false;
|
|
|
|
} else if (!mLive) {
|
|
|
|
mTelephony->AddCall(this);
|
|
|
|
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-05-18 10:52:06 -07:00
|
|
|
mError = new mozilla::dom::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-05-27 13:43:09 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_2(TelephonyCall,
|
2012-11-15 11:31:56 -08:00
|
|
|
nsDOMEventTargetHelper,
|
2013-05-27 13:43:09 -07:00
|
|
|
mTelephony,
|
|
|
|
mError);
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TelephonyCall)
|
2012-02-08 11:22:01 -08:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2012-02-08 11:22:01 -08:00
|
|
|
NS_IMPL_ADDREF_INHERITED(TelephonyCall, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(TelephonyCall, nsDOMEventTargetHelper)
|
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-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-03-06 01:53:23 -08:00
|
|
|
nsresult rv = mTelephony->Provider()->AnswerCall(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 ?
|
|
|
|
mTelephony->Provider()->RejectCall(mCallIndex) :
|
|
|
|
mTelephony->Provider()->HangUp(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-03-06 01:53:23 -08:00
|
|
|
nsresult rv = mTelephony->Provider()->HoldCall(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_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-03-06 01:53:23 -08:00
|
|
|
nsresult rv = mTelephony->Provider()->ResumeCall(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
|
|
|
}
|