2013-07-06 03:24:55 -07: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 "CallsList.h"
|
2014-10-22 00:07:00 -07:00
|
|
|
#include "Telephony.h"
|
2014-06-10 18:23:34 -07:00
|
|
|
#include "mozilla/dom/CallEvent.h"
|
2013-10-27 23:43:14 -07:00
|
|
|
#include "mozilla/dom/CallGroupErrorEvent.h"
|
2014-10-22 00:07:00 -07:00
|
|
|
#include "mozilla/dom/TelephonyCallGroupBinding.h"
|
|
|
|
#include "mozilla/dom/telephony/TelephonyCallback.h"
|
2013-07-06 03:24:55 -07:00
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
2014-10-22 00:07:00 -07:00
|
|
|
using namespace mozilla::dom::telephony;
|
2013-09-06 23:19:53 -07:00
|
|
|
using mozilla::ErrorResult;
|
2013-07-06 03:24:55 -07:00
|
|
|
|
2014-01-06 18:53:23 -08:00
|
|
|
TelephonyCallGroup::TelephonyCallGroup(nsPIDOMWindow* aOwner)
|
2014-03-31 23:13:50 -07:00
|
|
|
: DOMEventTargetHelper(aOwner)
|
2014-06-03 07:15:25 -07:00
|
|
|
, mCallState(nsITelephonyService::CALL_STATE_UNKNOWN)
|
2013-07-06 03:24:55 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TelephonyCallGroup::~TelephonyCallGroup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<TelephonyCallGroup>
|
|
|
|
TelephonyCallGroup::Create(Telephony* aTelephony)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aTelephony, "Null telephony!");
|
|
|
|
|
2014-01-06 18:53:23 -08:00
|
|
|
nsRefPtr<TelephonyCallGroup> group =
|
|
|
|
new TelephonyCallGroup(aTelephony->GetOwner());
|
2013-07-06 03:24:55 -07:00
|
|
|
|
|
|
|
group->mTelephony = aTelephony;
|
|
|
|
group->mCallsList = new CallsList(aTelephony, group);
|
|
|
|
|
|
|
|
return group.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
TelephonyCallGroup::WrapObject(JSContext* aCx)
|
2013-07-06 03:24:55 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return TelephonyCallGroupBinding::Wrap(aCx, this);
|
2013-07-06 03:24:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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-27 23:43:14 -07: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 03:24:55 -07:00
|
|
|
void
|
|
|
|
TelephonyCallGroup::ChangeState(uint16_t aCallState)
|
|
|
|
{
|
|
|
|
if (mCallState == aCallState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString stateString;
|
|
|
|
switch (aCallState) {
|
2014-06-03 07:15:25 -07:00
|
|
|
case nsITelephonyService::CALL_STATE_UNKNOWN:
|
2013-07-06 03:24:55 -07:00
|
|
|
break;
|
2014-06-03 07:15:25 -07:00
|
|
|
case nsITelephonyService::CALL_STATE_CONNECTED:
|
2013-07-06 03:24:55 -07:00
|
|
|
stateString.AssignLiteral("connected");
|
|
|
|
break;
|
2014-06-03 07:15:25 -07:00
|
|
|
case nsITelephonyService::CALL_STATE_HOLDING:
|
2013-07-06 03:24:55 -07:00
|
|
|
stateString.AssignLiteral("holding");
|
|
|
|
break;
|
2014-06-03 07:15:25 -07:00
|
|
|
case nsITelephonyService::CALL_STATE_HELD:
|
2013-07-06 03:24:55 -07:00
|
|
|
stateString.AssignLiteral("held");
|
|
|
|
break;
|
2014-06-03 07:15:25 -07:00
|
|
|
case nsITelephonyService::CALL_STATE_RESUMING:
|
2013-07-06 03:24:55 -07:00
|
|
|
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)
|
|
|
|
{
|
2014-06-10 18:23:34 -07:00
|
|
|
CallEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mCall = aCall;
|
|
|
|
|
|
|
|
nsRefPtr<CallEvent> event = CallEvent::Constructor(this, aType, init);
|
2013-07-06 03:24:55 -07:00
|
|
|
return DispatchTrustedEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TelephonyCallGroup::CanConference(const TelephonyCall& aCall,
|
|
|
|
TelephonyCall* aSecondCall)
|
|
|
|
{
|
2014-02-21 01:52:13 -08:00
|
|
|
if (!aCall.Mergeable()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-06 03:24:55 -07:00
|
|
|
if (!aSecondCall) {
|
|
|
|
MOZ_ASSERT(!mCalls.IsEmpty());
|
|
|
|
|
2014-06-03 07:15:25 -07:00
|
|
|
return (mCallState == nsITelephonyService::CALL_STATE_CONNECTED &&
|
|
|
|
aCall.CallState() == nsITelephonyService::CALL_STATE_HELD) ||
|
|
|
|
(mCallState == nsITelephonyService::CALL_STATE_HELD &&
|
|
|
|
aCall.CallState() == nsITelephonyService::CALL_STATE_CONNECTED);
|
2013-07-06 03:24:55 -07:00
|
|
|
}
|
|
|
|
|
2014-06-03 07:15:25 -07:00
|
|
|
MOZ_ASSERT(mCallState == nsITelephonyService::CALL_STATE_UNKNOWN);
|
2013-07-06 03:24:55 -07:00
|
|
|
|
2013-10-31 05:05:51 -07:00
|
|
|
if (aCall.ServiceId() != aSecondCall->ServiceId()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-21 01:52:13 -08:00
|
|
|
if (!aSecondCall->Mergeable()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-03 07:15:25 -07:00
|
|
|
return (aCall.CallState() == nsITelephonyService::CALL_STATE_CONNECTED &&
|
|
|
|
aSecondCall->CallState() == nsITelephonyService::CALL_STATE_HELD) ||
|
|
|
|
(aCall.CallState() == nsITelephonyService::CALL_STATE_HELD &&
|
|
|
|
aSecondCall->CallState() == nsITelephonyService::CALL_STATE_CONNECTED);
|
2013-07-06 03:24:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<TelephonyCall>
|
2013-10-31 05:05:51 -07:00
|
|
|
TelephonyCallGroup::GetCall(uint32_t aServiceId, uint32_t aCallIndex)
|
2013-07-06 03:24:55 -07:00
|
|
|
{
|
|
|
|
nsRefPtr<TelephonyCall> call;
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < mCalls.Length(); index++) {
|
|
|
|
nsRefPtr<TelephonyCall>& tempCall = mCalls[index];
|
2013-10-31 05:05:51 -07:00
|
|
|
if (tempCall->ServiceId() == aServiceId &&
|
|
|
|
tempCall->CallIndex() == aCallIndex) {
|
2013-07-06 03:24:55 -07:00
|
|
|
call = tempCall;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return call.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(TelephonyCallGroup)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(TelephonyCallGroup,
|
2014-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper)
|
2013-07-06 03:24:55 -07: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-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper)
|
2013-07-06 03:24:55 -07: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-03-31 23:13:50 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2013-07-06 03:24:55 -07:00
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(TelephonyCallGroup, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(TelephonyCallGroup, DOMEventTargetHelper)
|
2013-07-06 03:24:55 -07: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;
|
|
|
|
}
|
|
|
|
|
2014-06-03 07:15:30 -07:00
|
|
|
aRv = mTelephony->Service()->ConferenceCall(aCall.ServiceId());
|
2013-07-06 03:24:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TelephonyCallGroup::Add(TelephonyCall& aCall,
|
|
|
|
TelephonyCall& aSecondCall,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!CanConference(aCall, &aSecondCall)) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-03 07:15:30 -07:00
|
|
|
aRv = mTelephony->Service()->ConferenceCall(aCall.ServiceId());
|
2013-07-06 03:24:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TelephonyCallGroup::Remove(TelephonyCall& aCall, ErrorResult& aRv)
|
|
|
|
{
|
2014-06-03 07:15:25 -07:00
|
|
|
if (mCallState != nsITelephonyService::CALL_STATE_CONNECTED) {
|
2013-07-06 03:24:55 -07:00
|
|
|
NS_WARNING("Remove call from a non-connected call group. Ignore!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-31 05:05:51 -07:00
|
|
|
uint32_t serviceId = aCall.ServiceId();
|
2013-07-06 03:24:55 -07:00
|
|
|
uint32_t callIndex = aCall.CallIndex();
|
|
|
|
|
2013-10-31 05:05:51 -07:00
|
|
|
nsRefPtr<TelephonyCall> call;
|
|
|
|
|
|
|
|
call = GetCall(serviceId, callIndex);
|
|
|
|
if (call) {
|
2014-06-03 07:15:30 -07:00
|
|
|
aRv = mTelephony->Service()->SeparateCall(serviceId, callIndex);
|
2013-07-06 03:24:55 -07:00
|
|
|
} else {
|
|
|
|
NS_WARNING("Didn't have this call. Ignore!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 00:07:00 -07:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
TelephonyCallGroup::HangUp(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mCalls.IsEmpty());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
|
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<Promise> promise = Promise::Create(global, aRv);
|
|
|
|
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
|
|
|
|
|
|
|
|
nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
|
|
|
|
aRv = mTelephony->Service()->HangUpConference(mCalls[0]->ServiceId(),
|
|
|
|
callback);
|
|
|
|
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2013-07-06 03:24:55 -07:00
|
|
|
void
|
|
|
|
TelephonyCallGroup::Hold(ErrorResult& aRv)
|
|
|
|
{
|
2014-06-03 07:15:25 -07:00
|
|
|
if (mCallState != nsITelephonyService::CALL_STATE_CONNECTED) {
|
2013-07-06 03:24:55 -07:00
|
|
|
NS_WARNING("Hold non-connected call ignored!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-31 05:05:51 -07:00
|
|
|
MOZ_ASSERT(!mCalls.IsEmpty());
|
|
|
|
|
2014-06-03 07:15:30 -07:00
|
|
|
nsresult rv = mTelephony->Service()->HoldConference(mCalls[0]->ServiceId());
|
2013-07-06 03:24:55 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-03 07:15:25 -07:00
|
|
|
ChangeState(nsITelephonyService::CALL_STATE_HOLDING);
|
2013-07-06 03:24:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TelephonyCallGroup::Resume(ErrorResult& aRv)
|
|
|
|
{
|
2014-06-03 07:15:25 -07:00
|
|
|
if (mCallState != nsITelephonyService::CALL_STATE_HELD) {
|
2013-07-06 03:24:55 -07:00
|
|
|
NS_WARNING("Resume non-held call ignored!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-31 05:05:51 -07:00
|
|
|
MOZ_ASSERT(!mCalls.IsEmpty());
|
|
|
|
|
2014-06-03 07:15:30 -07:00
|
|
|
nsresult rv = mTelephony->Service()->ResumeConference(mCalls[0]->ServiceId());
|
2013-07-06 03:24:55 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-03 07:15:25 -07:00
|
|
|
ChangeState(nsITelephonyService::CALL_STATE_RESUMING);
|
2013-07-06 03:24:55 -07:00
|
|
|
}
|