2013-07-06 10:24:55 +00:00
|
|
|
/* -*- 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 "TelephonyCallGroup.h"
|
|
|
|
#include "mozilla/dom/TelephonyCallGroupBinding.h"
|
|
|
|
|
|
|
|
#include "CallEvent.h"
|
|
|
|
#include "CallsList.h"
|
2013-10-28 06:43:14 +00:00
|
|
|
#include "mozilla/dom/CallGroupErrorEvent.h"
|
2013-07-06 10:24:55 +00:00
|
|
|
#include "Telephony.h"
|
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
2013-09-07 06:19:53 +00:00
|
|
|
using mozilla::ErrorResult;
|
2013-07-06 10:24:55 +00:00
|
|
|
|
2014-01-07 02:53:23 +00:00
|
|
|
TelephonyCallGroup::TelephonyCallGroup(nsPIDOMWindow* aOwner)
|
2014-04-01 06:13:50 +00:00
|
|
|
: DOMEventTargetHelper(aOwner)
|
2014-01-07 02:53:23 +00:00
|
|
|
, mCallState(nsITelephonyProvider::CALL_STATE_UNKNOWN)
|
2013-07-06 10:24:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TelephonyCallGroup::~TelephonyCallGroup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<TelephonyCallGroup>
|
|
|
|
TelephonyCallGroup::Create(Telephony* aTelephony)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aTelephony, "Null telephony!");
|
|
|
|
|
2014-01-07 02:53:23 +00:00
|
|
|
nsRefPtr<TelephonyCallGroup> group =
|
|
|
|
new TelephonyCallGroup(aTelephony->GetOwner());
|
2013-07-06 10:24:55 +00:00
|
|
|
|
|
|
|
group->mTelephony = aTelephony;
|
|
|
|
group->mCallsList = new CallsList(aTelephony, group);
|
|
|
|
|
|
|
|
return group.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
TelephonyCallGroup::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return TelephonyCallGroupBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TelephonyCallGroup::AddCall(TelephonyCall* aCall)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mCalls.Contains(aCall), "Already know about this one!");
|
|
|
|
mCalls.AppendElement(aCall);
|
|
|
|
aCall->ChangeGroup(this);
|
|
|
|
NotifyCallsChanged(aCall);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TelephonyCallGroup::RemoveCall(TelephonyCall* aCall)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mCalls.Contains(aCall), "Didn't know about this one!");
|
|
|
|
mCalls.RemoveElement(aCall);
|
|
|
|
aCall->ChangeGroup(nullptr);
|
|
|
|
NotifyCallsChanged(aCall);
|
|
|
|
}
|
|
|
|
|
2013-10-28 06:43:14 +00:00
|
|
|
nsresult
|
|
|
|
TelephonyCallGroup::NotifyError(const nsAString& aName, const nsAString& aMessage)
|
|
|
|
{
|
|
|
|
CallGroupErrorEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mName = aName;
|
|
|
|
init.mMessage = aMessage;
|
|
|
|
|
|
|
|
nsRefPtr<CallGroupErrorEvent> event =
|
|
|
|
CallGroupErrorEvent::Constructor(this, NS_LITERAL_STRING("error"), init);
|
|
|
|
|
|
|
|
return DispatchTrustedEvent(event);
|
|
|
|
}
|
|
|
|
|
2013-07-06 10:24:55 +00:00
|
|
|
void
|
|
|
|
TelephonyCallGroup::ChangeState(uint16_t aCallState)
|
|
|
|
{
|
|
|
|
if (mCallState == aCallState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString stateString;
|
|
|
|
switch (aCallState) {
|
|
|
|
case nsITelephonyProvider::CALL_STATE_UNKNOWN:
|
|
|
|
break;
|
|
|
|
case nsITelephonyProvider::CALL_STATE_CONNECTED:
|
|
|
|
stateString.AssignLiteral("connected");
|
|
|
|
break;
|
|
|
|
case nsITelephonyProvider::CALL_STATE_HOLDING:
|
|
|
|
stateString.AssignLiteral("holding");
|
|
|
|
break;
|
|
|
|
case nsITelephonyProvider::CALL_STATE_HELD:
|
|
|
|
stateString.AssignLiteral("held");
|
|
|
|
break;
|
|
|
|
case nsITelephonyProvider::CALL_STATE_RESUMING:
|
|
|
|
stateString.AssignLiteral("resuming");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown state!");
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = stateString;
|
|
|
|
mCallState = aCallState;
|
|
|
|
|
|
|
|
nsresult rv = DispatchCallEvent(NS_LITERAL_STRING("statechange"), nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch specific event!");
|
|
|
|
}
|
|
|
|
if (!stateString.IsEmpty()) {
|
|
|
|
// This can change if the statechange handler called back here... Need to
|
|
|
|
// figure out something smarter.
|
|
|
|
if (mCallState == aCallState) {
|
|
|
|
rv = DispatchCallEvent(stateString, nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch specific event!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < mCalls.Length(); index++) {
|
|
|
|
nsRefPtr<TelephonyCall> call = mCalls[index];
|
|
|
|
call->ChangeState(aCallState);
|
|
|
|
|
|
|
|
MOZ_ASSERT(call->CallState() == aCallState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TelephonyCallGroup::NotifyCallsChanged(TelephonyCall* aCall)
|
|
|
|
{
|
|
|
|
return DispatchCallEvent(NS_LITERAL_STRING("callschanged"), aCall);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TelephonyCallGroup::DispatchCallEvent(const nsAString& aType,
|
|
|
|
TelephonyCall* aCall)
|
|
|
|
{
|
|
|
|
nsRefPtr<CallEvent> event = CallEvent::Create(this, aType, aCall, false, false);
|
|
|
|
return DispatchTrustedEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TelephonyCallGroup::CanConference(const TelephonyCall& aCall,
|
|
|
|
TelephonyCall* aSecondCall)
|
|
|
|
{
|
2014-02-21 09:52:13 +00:00
|
|
|
if (!aCall.Mergeable()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-06 10:24:55 +00:00
|
|
|
if (!aSecondCall) {
|
|
|
|
MOZ_ASSERT(!mCalls.IsEmpty());
|
|
|
|
|
|
|
|
return (mCallState == nsITelephonyProvider::CALL_STATE_CONNECTED &&
|
|
|
|
aCall.CallState() == nsITelephonyProvider::CALL_STATE_HELD) ||
|
|
|
|
(mCallState == nsITelephonyProvider::CALL_STATE_HELD &&
|
|
|
|
aCall.CallState() == nsITelephonyProvider::CALL_STATE_CONNECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mCallState == nsITelephonyProvider::CALL_STATE_UNKNOWN);
|
|
|
|
|
2013-10-31 12:05:51 +00:00
|
|
|
if (aCall.ServiceId() != aSecondCall->ServiceId()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-21 09:52:13 +00:00
|
|
|
if (!aSecondCall->Mergeable()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-06 10:24:55 +00:00
|
|
|
return (aCall.CallState() == nsITelephonyProvider::CALL_STATE_CONNECTED &&
|
|
|
|
aSecondCall->CallState() == nsITelephonyProvider::CALL_STATE_HELD) ||
|
|
|
|
(aCall.CallState() == nsITelephonyProvider::CALL_STATE_HELD &&
|
|
|
|
aSecondCall->CallState() == nsITelephonyProvider::CALL_STATE_CONNECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<TelephonyCall>
|
2013-10-31 12:05:51 +00:00
|
|
|
TelephonyCallGroup::GetCall(uint32_t aServiceId, uint32_t aCallIndex)
|
2013-07-06 10:24:55 +00:00
|
|
|
{
|
|
|
|
nsRefPtr<TelephonyCall> call;
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < mCalls.Length(); index++) {
|
|
|
|
nsRefPtr<TelephonyCall>& tempCall = mCalls[index];
|
2013-10-31 12:05:51 +00:00
|
|
|
if (tempCall->ServiceId() == aServiceId &&
|
|
|
|
tempCall->CallIndex() == aCallIndex) {
|
2013-07-06 10:24:55 +00:00
|
|
|
call = tempCall;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return call.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(TelephonyCallGroup)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(TelephonyCallGroup,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
2013-07-06 10:24:55 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCalls)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCallsList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTelephony)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(TelephonyCallGroup,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
2013-07-06 10:24:55 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCalls)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCallsList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mTelephony)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TelephonyCallGroup)
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2013-07-06 10:24:55 +00:00
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(TelephonyCallGroup, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(TelephonyCallGroup, DOMEventTargetHelper)
|
2013-07-06 10:24:55 +00:00
|
|
|
|
|
|
|
// WebIDL
|
|
|
|
already_AddRefed<CallsList>
|
|
|
|
TelephonyCallGroup::Calls() const
|
|
|
|
{
|
|
|
|
nsRefPtr<CallsList> list = mCallsList;
|
|
|
|
return list.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TelephonyCallGroup::Add(TelephonyCall& aCall,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!CanConference(aCall, nullptr)) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-31 12:05:51 +00:00
|
|
|
aRv = mTelephony->Provider()->ConferenceCall(aCall.ServiceId());
|
2013-07-06 10:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TelephonyCallGroup::Add(TelephonyCall& aCall,
|
|
|
|
TelephonyCall& aSecondCall,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!CanConference(aCall, &aSecondCall)) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-31 12:05:51 +00:00
|
|
|
aRv = mTelephony->Provider()->ConferenceCall(aCall.ServiceId());
|
2013-07-06 10:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TelephonyCallGroup::Remove(TelephonyCall& aCall, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mCallState != nsITelephonyProvider::CALL_STATE_CONNECTED) {
|
|
|
|
NS_WARNING("Remove call from a non-connected call group. Ignore!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-31 12:05:51 +00:00
|
|
|
uint32_t serviceId = aCall.ServiceId();
|
2013-07-06 10:24:55 +00:00
|
|
|
uint32_t callIndex = aCall.CallIndex();
|
|
|
|
|
2013-10-31 12:05:51 +00:00
|
|
|
nsRefPtr<TelephonyCall> call;
|
|
|
|
|
|
|
|
call = GetCall(serviceId, callIndex);
|
|
|
|
if (call) {
|
|
|
|
aRv = mTelephony->Provider()->SeparateCall(serviceId, callIndex);
|
2013-07-06 10:24:55 +00:00
|
|
|
} else {
|
|
|
|
NS_WARNING("Didn't have this call. Ignore!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TelephonyCallGroup::Hold(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mCallState != nsITelephonyProvider::CALL_STATE_CONNECTED) {
|
|
|
|
NS_WARNING("Hold non-connected call ignored!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-31 12:05:51 +00:00
|
|
|
MOZ_ASSERT(!mCalls.IsEmpty());
|
|
|
|
|
|
|
|
nsresult rv = mTelephony->Provider()->HoldConference(mCalls[0]->ServiceId());
|
2013-07-06 10:24:55 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChangeState(nsITelephonyProvider::CALL_STATE_HOLDING);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TelephonyCallGroup::Resume(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mCallState != nsITelephonyProvider::CALL_STATE_HELD) {
|
|
|
|
NS_WARNING("Resume non-held call ignored!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-31 12:05:51 +00:00
|
|
|
MOZ_ASSERT(!mCalls.IsEmpty());
|
|
|
|
|
|
|
|
nsresult rv = mTelephony->Provider()->ResumeConference(mCalls[0]->ServiceId());
|
2013-07-06 10:24:55 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChangeState(nsITelephonyProvider::CALL_STATE_RESUMING);
|
|
|
|
}
|