2013-05-31 23:53:06 -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: */
|
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 "Telephony.h"
|
2013-07-15 01:27:19 -07:00
|
|
|
#include "mozilla/dom/TelephonyBinding.h"
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2013-01-27 23:39:50 -08:00
|
|
|
#include "nsIPermissionManager.h"
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-07-06 03:24:55 -07:00
|
|
|
#include "mozilla/dom/UnionTypes.h"
|
2012-02-24 05:14:37 -08:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2012-01-09 14:28:47 -08:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-22 09:05:26 -07:00
|
|
|
#include "nsCxPusher.h"
|
2012-02-24 05:14:37 -08:00
|
|
|
#include "nsNetUtil.h"
|
2013-01-27 23:39:50 -08:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-05-09 03:44:13 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
#include "CallEvent.h"
|
|
|
|
#include "CallsList.h"
|
2012-01-09 14:28:47 -08:00
|
|
|
#include "TelephonyCall.h"
|
2013-07-06 03:24:55 -07:00
|
|
|
#include "TelephonyCallGroup.h"
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-05-09 03:44:13 -07:00
|
|
|
using namespace mozilla::dom;
|
2013-09-06 23:19:53 -07:00
|
|
|
using mozilla::ErrorResult;
|
2013-09-11 20:47:14 -07:00
|
|
|
using mozilla::dom::telephony::kOutgoingPlaceholderCallIndex;
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2012-02-23 07:09:11 -08:00
|
|
|
namespace {
|
|
|
|
|
2012-02-24 05:14:37 -08:00
|
|
|
typedef nsAutoTArray<Telephony*, 2> TelephonyList;
|
|
|
|
|
|
|
|
TelephonyList* gTelephonyList;
|
|
|
|
|
2012-02-23 07:09:11 -08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
class Telephony::Listener : public nsITelephonyListener
|
|
|
|
{
|
|
|
|
Telephony* mTelephony;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_FORWARD_SAFE_NSITELEPHONYLISTENER(mTelephony)
|
|
|
|
|
|
|
|
Listener(Telephony* aTelephony)
|
|
|
|
: mTelephony(aTelephony)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mTelephony);
|
|
|
|
}
|
|
|
|
|
2013-09-06 23:19:53 -07:00
|
|
|
virtual ~Listener() {}
|
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
void
|
|
|
|
Disconnect()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mTelephony);
|
|
|
|
mTelephony = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-09 03:44:13 -07:00
|
|
|
class Telephony::EnumerationAck : public nsRunnable
|
|
|
|
{
|
|
|
|
nsRefPtr<Telephony> mTelephony;
|
|
|
|
|
|
|
|
public:
|
|
|
|
EnumerationAck(Telephony* aTelephony)
|
|
|
|
: mTelephony(aTelephony)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mTelephony);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
mTelephony->NotifyCallsChanged(nullptr);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-24 05:14:37 -08:00
|
|
|
Telephony::Telephony()
|
2013-07-15 01:27:19 -07:00
|
|
|
: mActiveCall(nullptr), mEnumerated(false)
|
2012-02-24 05:14:37 -08:00
|
|
|
{
|
|
|
|
if (!gTelephonyList) {
|
|
|
|
gTelephonyList = new TelephonyList();
|
|
|
|
}
|
|
|
|
|
|
|
|
gTelephonyList->AppendElement(this);
|
2013-07-15 01:27:19 -07:00
|
|
|
|
|
|
|
SetIsDOMBinding();
|
2012-02-24 05:14:37 -08:00
|
|
|
}
|
|
|
|
|
2012-01-09 14:28:47 -08:00
|
|
|
Telephony::~Telephony()
|
|
|
|
{
|
2013-08-20 00:57:19 -07:00
|
|
|
Shutdown();
|
2012-02-23 07:09:11 -08:00
|
|
|
|
2012-02-24 05:14:37 -08:00
|
|
|
NS_ASSERTION(gTelephonyList, "This should never be null!");
|
|
|
|
NS_ASSERTION(gTelephonyList->Contains(this), "Should be in the list!");
|
|
|
|
|
|
|
|
if (gTelephonyList->Length() == 1) {
|
|
|
|
delete gTelephonyList;
|
2012-07-30 07:20:58 -07:00
|
|
|
gTelephonyList = nullptr;
|
2013-08-20 00:57:19 -07:00
|
|
|
} else {
|
2012-02-24 05:14:37 -08:00
|
|
|
gTelephonyList->RemoveElement(this);
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-08-20 00:57:19 -07:00
|
|
|
void
|
|
|
|
Telephony::Shutdown()
|
|
|
|
{
|
|
|
|
if (mListener) {
|
|
|
|
mListener->Disconnect();
|
|
|
|
|
|
|
|
if (mProvider) {
|
2013-09-06 23:19:53 -07:00
|
|
|
mProvider->UnregisterListener(mListener);
|
2013-08-20 00:57:19 -07:00
|
|
|
mProvider = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mListener = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
JSObject*
|
|
|
|
Telephony::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return TelephonyBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
|
|
|
|
2012-01-09 14:28:47 -08:00
|
|
|
// static
|
|
|
|
already_AddRefed<Telephony>
|
2013-07-12 07:36:13 -07:00
|
|
|
Telephony::Create(nsPIDOMWindow* aOwner, ErrorResult& aRv)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aOwner, "Null owner!");
|
2013-07-12 07:36:13 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsITelephonyProvider> ril =
|
2013-09-06 23:19:53 -07:00
|
|
|
do_GetService(TELEPHONY_PROVIDER_CONTRACTID);
|
2013-07-12 07:36:13 -07:00
|
|
|
if (!ril) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aOwner);
|
2013-07-12 07:36:13 -07:00
|
|
|
if (!sgo) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
|
2013-07-12 07:36:13 -07:00
|
|
|
if (!scriptContext) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
nsRefPtr<Telephony> telephony = new Telephony();
|
|
|
|
|
2012-03-13 15:11:16 -07:00
|
|
|
telephony->BindToOwner(aOwner);
|
|
|
|
|
2013-07-12 07:36:13 -07:00
|
|
|
telephony->mProvider = ril;
|
2013-03-06 01:53:23 -08:00
|
|
|
telephony->mListener = new Listener(telephony);
|
2013-07-15 01:27:19 -07:00
|
|
|
telephony->mCallsList = new CallsList(telephony);
|
2013-07-06 03:24:55 -07:00
|
|
|
telephony->mGroup = TelephonyCallGroup::Create(telephony);
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-07-12 07:36:13 -07:00
|
|
|
nsresult rv = ril->EnumerateCalls(telephony->mListener);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-09-06 23:19:53 -07:00
|
|
|
rv = ril->RegisterListener(telephony->mListener);
|
2013-07-12 07:36:13 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-10-09 03:07:11 -07:00
|
|
|
|
2012-01-09 14:28:47 -08:00
|
|
|
return telephony.forget();
|
|
|
|
}
|
|
|
|
|
2012-02-24 05:14:37 -08:00
|
|
|
already_AddRefed<TelephonyCall>
|
|
|
|
Telephony::CreateNewDialingCall(const nsAString& aNumber)
|
|
|
|
{
|
|
|
|
nsRefPtr<TelephonyCall> call =
|
|
|
|
TelephonyCall::Create(this, aNumber,
|
2013-03-06 01:53:23 -08:00
|
|
|
nsITelephonyProvider::CALL_STATE_DIALING);
|
2012-02-24 05:14:37 -08:00
|
|
|
NS_ASSERTION(call, "This should never fail!");
|
|
|
|
|
|
|
|
NS_ASSERTION(mCalls.Contains(call), "Should have auto-added new call!");
|
|
|
|
|
|
|
|
return call.forget();
|
|
|
|
}
|
|
|
|
|
2012-01-09 14:28:47 -08:00
|
|
|
void
|
2012-02-24 05:14:37 -08:00
|
|
|
Telephony::NoteDialedCallFromOtherInstance(const nsAString& aNumber)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2012-02-24 05:14:37 -08:00
|
|
|
// We don't need to hang on to this call object, it is held alive by mCalls.
|
|
|
|
nsRefPtr<TelephonyCall> call = CreateNewDialingCall(aNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Telephony::NotifyCallsChanged(TelephonyCall* aCall)
|
|
|
|
{
|
2013-01-27 23:39:50 -08:00
|
|
|
return DispatchCallEvent(NS_LITERAL_STRING("callschanged"), aCall);
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
already_AddRefed<TelephonyCall>
|
2012-07-20 05:08:24 -07:00
|
|
|
Telephony::DialInternal(bool isEmergency,
|
|
|
|
const nsAString& aNumber,
|
2013-07-15 01:27:19 -07:00
|
|
|
ErrorResult& aRv)
|
2012-07-20 05:08:24 -07:00
|
|
|
{
|
2013-07-15 01:27:19 -07:00
|
|
|
if (aNumber.IsEmpty()) {
|
|
|
|
aRv.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-07-20 05:08:24 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < mCalls.Length(); index++) {
|
2012-07-20 05:08:24 -07:00
|
|
|
const nsRefPtr<TelephonyCall>& tempCall = mCalls[index];
|
|
|
|
if (tempCall->IsOutgoing() &&
|
2013-03-06 01:53:23 -08:00
|
|
|
tempCall->CallState() < nsITelephonyProvider::CALL_STATE_CONNECTED) {
|
2012-07-20 05:08:24 -07:00
|
|
|
// One call has been dialed already and we only support one outgoing call
|
|
|
|
// at a time.
|
|
|
|
NS_WARNING("Only permitted to dial one call at a time!");
|
2013-07-15 01:27:19 -07:00
|
|
|
aRv.Throw(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return nullptr;
|
2012-07-20 05:08:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-06 23:19:53 -07:00
|
|
|
nsresult rv = mProvider->Dial(aNumber, isEmergency);
|
2013-07-15 01:27:19 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-07-20 05:08:24 -07:00
|
|
|
|
|
|
|
nsRefPtr<TelephonyCall> call = CreateNewDialingCall(aNumber);
|
|
|
|
|
|
|
|
// Notify other telephony objects that we just dialed.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < gTelephonyList->Length(); index++) {
|
2012-07-20 05:08:24 -07:00
|
|
|
Telephony*& telephony = gTelephonyList->ElementAt(index);
|
|
|
|
if (telephony != this) {
|
|
|
|
nsRefPtr<Telephony> kungFuDeathGrip = telephony;
|
|
|
|
telephony->NoteDialedCallFromOtherInstance(aNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
return call.forget();
|
2012-07-20 05:08:24 -07:00
|
|
|
}
|
|
|
|
|
2013-07-06 03:24:55 -07:00
|
|
|
void
|
|
|
|
Telephony::UpdateActiveCall(TelephonyCall* aCall, bool aIsAdding)
|
|
|
|
{
|
|
|
|
if (aIsAdding) {
|
|
|
|
if (aCall->CallState() == nsITelephonyProvider::CALL_STATE_DIALING ||
|
|
|
|
aCall->CallState() == nsITelephonyProvider::CALL_STATE_ALERTING ||
|
|
|
|
aCall->CallState() == nsITelephonyProvider::CALL_STATE_CONNECTED) {
|
|
|
|
NS_ASSERTION(!mActiveCall, "Already have an active call!");
|
|
|
|
mActiveCall = aCall;
|
|
|
|
}
|
|
|
|
} else if (mActiveCall && mActiveCall->CallIndex() == aCall->CallIndex()) {
|
|
|
|
mActiveCall = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<TelephonyCall>
|
|
|
|
Telephony::GetCall(uint32_t aCallIndex)
|
|
|
|
{
|
|
|
|
nsRefPtr<TelephonyCall> call;
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < mCalls.Length(); index++) {
|
|
|
|
nsRefPtr<TelephonyCall>& tempCall = mCalls[index];
|
|
|
|
if (tempCall->CallIndex() == aCallIndex) {
|
|
|
|
call = tempCall;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return call.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Telephony::MoveCall(uint32_t aCallIndex, bool aIsConference)
|
|
|
|
{
|
|
|
|
nsRefPtr<TelephonyCall> call;
|
|
|
|
|
|
|
|
// Move a call to mGroup.
|
|
|
|
if (aIsConference) {
|
|
|
|
call = GetCall(aCallIndex);
|
|
|
|
if (call) {
|
|
|
|
RemoveCall(call);
|
|
|
|
mGroup->AddCall(call);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a call from mGroup.
|
|
|
|
call = mGroup->GetCall(aCallIndex);
|
|
|
|
if (call) {
|
|
|
|
mGroup->RemoveCall(call);
|
|
|
|
AddCall(call);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Telephony)
|
|
|
|
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(Telephony,
|
2012-02-08 11:22:01 -08:00
|
|
|
nsDOMEventTargetHelper)
|
2013-07-15 01:27:19 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCalls)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCallsList)
|
2013-07-06 03:24:55 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGroup)
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(Telephony,
|
2012-02-08 11:22:01 -08:00
|
|
|
nsDOMEventTargetHelper)
|
2013-08-20 00:57:19 -07:00
|
|
|
tmp->Shutdown();
|
2012-07-30 07:20:58 -07:00
|
|
|
tmp->mActiveCall = nullptr;
|
2013-07-15 01:27:19 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCalls)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCallsList)
|
2013-07-06 03:24:55 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mGroup)
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(Telephony)
|
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(Telephony, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(Telephony, nsDOMEventTargetHelper)
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
NS_IMPL_ISUPPORTS1(Telephony::Listener, nsITelephonyListener)
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
// Telephony WebIDL
|
2013-05-09 03:44:13 -07:00
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
already_AddRefed<TelephonyCall>
|
|
|
|
Telephony::Dial(const nsAString& aNumber, ErrorResult& aRv)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2013-07-15 01:27:19 -07:00
|
|
|
nsRefPtr<TelephonyCall> call = DialInternal(false, aNumber, aRv);
|
|
|
|
return call.forget();
|
2012-07-20 05:08:24 -07:00
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
already_AddRefed<TelephonyCall>
|
|
|
|
Telephony::DialEmergency(const nsAString& aNumber, ErrorResult& aRv)
|
2012-07-20 05:08:24 -07:00
|
|
|
{
|
2013-07-15 01:27:19 -07:00
|
|
|
nsRefPtr<TelephonyCall> call = DialInternal(true, aNumber, aRv);
|
|
|
|
return call.forget();
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
bool
|
|
|
|
Telephony::GetMuted(ErrorResult& aRv) const
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2013-07-15 01:27:19 -07:00
|
|
|
bool muted = false;
|
|
|
|
aRv = mProvider->GetMicrophoneMuted(&muted);
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
return muted;
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
void
|
|
|
|
Telephony::SetMuted(bool aMuted, ErrorResult& aRv)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2013-07-15 01:27:19 -07:00
|
|
|
aRv = mProvider->SetMicrophoneMuted(aMuted);
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
bool
|
|
|
|
Telephony::GetSpeakerEnabled(ErrorResult& aRv) const
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2013-07-15 01:27:19 -07:00
|
|
|
bool enabled = false;
|
|
|
|
aRv = mProvider->GetSpeakerEnabled(&enabled);
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
return enabled;
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
void
|
|
|
|
Telephony::SetSpeakerEnabled(bool aEnabled, ErrorResult& aRv)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2013-07-15 01:27:19 -07:00
|
|
|
aRv = mProvider->SetSpeakerEnabled(aEnabled);
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-07-06 03:24:55 -07:00
|
|
|
void
|
2013-09-17 08:16:02 -07:00
|
|
|
Telephony::GetActive(Nullable<OwningTelephonyCallOrTelephonyCallGroup>& aValue)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2013-07-06 03:24:55 -07:00
|
|
|
if (mActiveCall) {
|
|
|
|
aValue.SetValue().SetAsTelephonyCall() = mActiveCall;
|
|
|
|
} else if (mGroup->CallState() == nsITelephonyProvider::CALL_STATE_CONNECTED) {
|
|
|
|
aValue.SetValue().SetAsTelephonyCallGroup() = mGroup;
|
|
|
|
} else {
|
|
|
|
aValue.SetNull();
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
already_AddRefed<CallsList>
|
|
|
|
Telephony::Calls() const
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2013-07-15 01:27:19 -07:00
|
|
|
nsRefPtr<CallsList> list = mCallsList;
|
|
|
|
return list.forget();
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-07-06 03:24:55 -07:00
|
|
|
already_AddRefed<TelephonyCallGroup>
|
|
|
|
Telephony::ConferenceGroup() const
|
|
|
|
{
|
|
|
|
nsRefPtr<TelephonyCallGroup> group = mGroup;
|
|
|
|
return group.forget();
|
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
void
|
|
|
|
Telephony::StartTone(const nsAString& aDTMFChar, ErrorResult& aRv)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
|
|
|
if (aDTMFChar.IsEmpty()) {
|
|
|
|
NS_WARNING("Empty tone string will be ignored");
|
2013-07-15 01:27:19 -07:00
|
|
|
return;
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aDTMFChar.Length() > 1) {
|
2013-07-15 01:27:19 -07:00
|
|
|
aRv.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return;
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
aRv = mProvider->StartTone(aDTMFChar);
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-07-15 01:27:19 -07:00
|
|
|
void
|
|
|
|
Telephony::StopTone(ErrorResult& aRv)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2013-07-15 01:27:19 -07:00
|
|
|
aRv = mProvider->StopTone();
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-05-24 10:22:20 -07:00
|
|
|
// EventTarget
|
2013-05-09 03:44:13 -07:00
|
|
|
|
|
|
|
void
|
2013-05-24 10:22:20 -07:00
|
|
|
Telephony::EventListenerAdded(nsIAtom* aType)
|
2013-05-09 03:44:13 -07:00
|
|
|
{
|
2013-05-24 10:22:20 -07:00
|
|
|
if (aType == nsGkAtoms::oncallschanged) {
|
2013-05-09 03:44:13 -07:00
|
|
|
// Fire oncallschanged on the next tick if the calls array is ready.
|
|
|
|
EnqueueEnumerationAck();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsITelephonyListener
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Telephony::CallStateChanged(uint32_t aCallIndex, uint16_t aCallState,
|
2013-05-08 03:39:05 -07:00
|
|
|
const nsAString& aNumber, bool aIsActive,
|
2013-07-06 03:24:55 -07:00
|
|
|
bool aIsOutgoing, bool aIsEmergency,
|
|
|
|
bool aIsConference)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2012-01-11 18:17:26 -08:00
|
|
|
NS_ASSERTION(aCallIndex != kOutgoingPlaceholderCallIndex,
|
|
|
|
"This should never happen!");
|
|
|
|
|
|
|
|
nsRefPtr<TelephonyCall> modifiedCall;
|
|
|
|
nsRefPtr<TelephonyCall> outgoingCall;
|
|
|
|
|
2013-07-06 03:24:55 -07:00
|
|
|
// Update calls array first then state of a call in mCalls.
|
|
|
|
|
|
|
|
if (aIsConference) {
|
|
|
|
// Add the call into mGroup if it hasn't been there, otherwise we simply
|
|
|
|
// update its call state. We don't fire the statechange event on a call in
|
|
|
|
// conference here. Instead, the event will be fired later in
|
|
|
|
// TelephonyCallGroup::ChangeState(). Thus the sequence of firing the
|
|
|
|
// statechange events is guaranteed: first on TelephonyCallGroup then on
|
|
|
|
// individual TelephonyCall objects.
|
|
|
|
|
|
|
|
modifiedCall = mGroup->GetCall(aCallIndex);
|
|
|
|
if (modifiedCall) {
|
|
|
|
modifiedCall->ChangeStateInternal(aCallState, false);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The call becomes a conference call. Remove it from Telephony::mCalls and
|
|
|
|
// add it to mGroup.
|
|
|
|
modifiedCall = GetCall(aCallIndex);
|
|
|
|
if (modifiedCall) {
|
|
|
|
modifiedCall->ChangeStateInternal(aCallState, false);
|
|
|
|
mGroup->AddCall(modifiedCall);
|
|
|
|
RemoveCall(modifiedCall);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't find this call in mCalls or mGroup. Create a new call.
|
|
|
|
nsRefPtr<TelephonyCall> call =
|
|
|
|
TelephonyCall::Create(this, aNumber, aCallState, aCallIndex,
|
|
|
|
aIsEmergency, aIsConference);
|
|
|
|
NS_ASSERTION(call, "This should never fail!");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a conference call. Remove the call from mGroup if it has been there.
|
|
|
|
modifiedCall = mGroup->GetCall(aCallIndex);
|
|
|
|
if (modifiedCall) {
|
|
|
|
if (aCallState != nsITelephonyProvider::CALL_STATE_DISCONNECTED) {
|
|
|
|
if (modifiedCall->CallState() != aCallState) {
|
|
|
|
modifiedCall->ChangeState(aCallState);
|
|
|
|
}
|
|
|
|
mGroup->RemoveCall(modifiedCall);
|
|
|
|
AddCall(modifiedCall);
|
|
|
|
} else {
|
|
|
|
modifiedCall->ChangeState(aCallState);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update calls in mCalls.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < mCalls.Length(); index++) {
|
2012-01-09 14:28:47 -08:00
|
|
|
nsRefPtr<TelephonyCall>& tempCall = mCalls[index];
|
2012-01-11 18:17:26 -08:00
|
|
|
if (tempCall->CallIndex() == kOutgoingPlaceholderCallIndex) {
|
|
|
|
NS_ASSERTION(!outgoingCall, "More than one outgoing call not supported!");
|
2012-02-24 05:14:37 -08:00
|
|
|
NS_ASSERTION(tempCall->CallState() ==
|
2013-03-06 01:53:23 -08:00
|
|
|
nsITelephonyProvider::CALL_STATE_DIALING,
|
2012-01-11 18:17:26 -08:00
|
|
|
"Something really wrong here!");
|
|
|
|
// Stash this for later, we may need it if aCallIndex doesn't match one of
|
|
|
|
// our other calls.
|
|
|
|
outgoingCall = tempCall;
|
|
|
|
} else if (tempCall->CallIndex() == aCallIndex) {
|
|
|
|
// We already know about this call so just update its state.
|
|
|
|
modifiedCall = tempCall;
|
2012-07-30 07:20:58 -07:00
|
|
|
outgoingCall = nullptr;
|
2012-01-11 18:17:26 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2012-01-11 18:17:26 -08:00
|
|
|
// If nothing matched above and the call state isn't incoming but we do have
|
|
|
|
// an outgoing call then we must be seeing a status update for our outgoing
|
|
|
|
// call.
|
|
|
|
if (!modifiedCall &&
|
2013-03-06 01:53:23 -08:00
|
|
|
aCallState != nsITelephonyProvider::CALL_STATE_INCOMING &&
|
2012-01-11 18:17:26 -08:00
|
|
|
outgoingCall) {
|
|
|
|
outgoingCall->UpdateCallIndex(aCallIndex);
|
2013-05-13 23:08:07 -07:00
|
|
|
outgoingCall->UpdateEmergency(aIsEmergency);
|
2012-01-11 18:17:26 -08:00
|
|
|
modifiedCall.swap(outgoingCall);
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2012-01-11 18:17:26 -08:00
|
|
|
if (modifiedCall) {
|
|
|
|
// See if this should replace our current active call.
|
2012-05-14 21:13:06 -07:00
|
|
|
if (aIsActive) {
|
2012-06-08 00:09:15 -07:00
|
|
|
mActiveCall = modifiedCall;
|
2013-01-04 00:52:18 -08:00
|
|
|
} else if (mActiveCall && mActiveCall->CallIndex() == aCallIndex) {
|
|
|
|
mActiveCall = nullptr;
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
2012-01-11 18:17:26 -08:00
|
|
|
|
2012-06-08 00:09:15 -07:00
|
|
|
// Change state.
|
|
|
|
modifiedCall->ChangeState(aCallState);
|
|
|
|
|
2012-01-11 18:17:26 -08:00
|
|
|
return NS_OK;
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2013-01-04 00:52:18 -08:00
|
|
|
// Didn't know anything about this call before now.
|
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
if (aCallState == nsITelephonyProvider::CALL_STATE_DISCONNECTED) {
|
2013-01-04 00:52:18 -08:00
|
|
|
// Do nothing since we didn't know anything about it before now and it's
|
|
|
|
// been ended already.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
nsRefPtr<TelephonyCall> call =
|
2013-05-13 23:08:07 -07:00
|
|
|
TelephonyCall::Create(this, aNumber, aCallState, aCallIndex, aIsEmergency);
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_ASSERTION(call, "This should never fail!");
|
|
|
|
|
|
|
|
NS_ASSERTION(mCalls.Contains(call), "Should have auto-added new call!");
|
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
if (aCallState == nsITelephonyProvider::CALL_STATE_INCOMING) {
|
2013-01-27 23:39:50 -08:00
|
|
|
nsresult rv = DispatchCallEvent(NS_LITERAL_STRING("incoming"), call);
|
2012-10-25 03:31:37 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-06 03:24:55 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::ConferenceCallStateChanged(uint16_t aCallState)
|
|
|
|
{
|
|
|
|
mGroup->ChangeState(aCallState);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-09 03:44:13 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::EnumerateCallStateComplete()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mEnumerated);
|
|
|
|
|
|
|
|
mEnumerated = true;
|
|
|
|
|
|
|
|
if (NS_FAILED(NotifyCallsChanged(nullptr))) {
|
|
|
|
NS_WARNING("Failed to notify calls changed!");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Telephony::EnumerateCallState(uint32_t aCallIndex, uint16_t aCallState,
|
2012-01-09 14:28:47 -08:00
|
|
|
const nsAString& aNumber, bool aIsActive,
|
2013-05-13 23:08:07 -07:00
|
|
|
bool aIsOutgoing, bool aIsEmergency,
|
2013-09-06 23:19:53 -07:00
|
|
|
bool aIsConference)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2013-07-06 03:24:55 -07:00
|
|
|
nsRefPtr<TelephonyCall> call;
|
|
|
|
|
|
|
|
// We request calls enumeration in constructor, and the asynchronous result
|
|
|
|
// will be sent back through the callback function EnumerateCallState().
|
|
|
|
// However, it is likely to have call state changes, i.e. CallStateChanged()
|
|
|
|
// being called, before the enumeration result comes back. We'd make sure
|
|
|
|
// we don't somehow add duplicates due to the race condition.
|
|
|
|
call = aIsConference ? mGroup->GetCall(aCallIndex) : GetCall(aCallIndex);
|
|
|
|
if (call) {
|
|
|
|
// We have the call either in mCalls or in mGroup. Skip it.
|
|
|
|
return NS_OK;
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
2013-01-04 00:52:18 -08:00
|
|
|
|
2013-07-06 03:24:55 -07:00
|
|
|
if (MoveCall(aCallIndex, aIsConference)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't know anything about this call before now.
|
|
|
|
|
|
|
|
call = TelephonyCall::Create(this, aNumber, aCallState, aCallIndex,
|
|
|
|
aIsEmergency, aIsConference);
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_ASSERTION(call, "This should never fail!");
|
|
|
|
|
2013-07-06 03:24:55 -07:00
|
|
|
NS_ASSERTION(aIsConference ? mGroup->CallsArray().Contains(call) :
|
|
|
|
mCalls.Contains(call),
|
|
|
|
"Should have auto-added new call!");
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-05 13:28:14 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::SupplementaryServiceNotification(int32_t aCallIndex,
|
|
|
|
uint16_t aNotification)
|
|
|
|
{
|
|
|
|
nsRefPtr<TelephonyCall> associatedCall;
|
|
|
|
if (!mCalls.IsEmpty() && aCallIndex != -1) {
|
2013-07-06 03:24:55 -07:00
|
|
|
associatedCall = GetCall(aCallIndex);
|
2013-08-05 13:28:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
switch (aNotification) {
|
|
|
|
case nsITelephonyProvider::NOTIFICATION_REMOTE_HELD:
|
|
|
|
rv = DispatchCallEvent(NS_LITERAL_STRING("remoteheld"), associatedCall);
|
|
|
|
break;
|
|
|
|
case nsITelephonyProvider::NOTIFICATION_REMOTE_RESUMED:
|
|
|
|
rv = DispatchCallEvent(NS_LITERAL_STRING("remoteresumed"), associatedCall);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Got a bad notification!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-14 21:13:06 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Telephony::NotifyError(int32_t aCallIndex,
|
2012-06-20 20:00:52 -07:00
|
|
|
const nsAString& aError)
|
2012-05-14 21:13:06 -07:00
|
|
|
{
|
2012-06-20 20:00:52 -07:00
|
|
|
nsRefPtr<TelephonyCall> callToNotify;
|
|
|
|
if (!mCalls.IsEmpty()) {
|
2013-07-18 21:03:30 -07:00
|
|
|
// The connection is not established yet. Get the latest dialing call object.
|
2012-06-20 20:00:52 -07:00
|
|
|
if (aCallIndex == -1) {
|
2013-07-18 21:03:30 -07:00
|
|
|
nsRefPtr<TelephonyCall>& lastCall = mCalls[mCalls.Length() - 1];
|
|
|
|
if (lastCall->CallIndex() == kOutgoingPlaceholderCallIndex) {
|
|
|
|
callToNotify = lastCall;
|
|
|
|
}
|
2012-06-20 20:00:52 -07:00
|
|
|
} else {
|
|
|
|
// The connection has been established. Get the failed call.
|
2013-07-06 03:24:55 -07:00
|
|
|
callToNotify = GetCall(aCallIndex);
|
2012-06-21 19:23:55 -07:00
|
|
|
}
|
2012-05-14 21:13:06 -07:00
|
|
|
}
|
2012-06-20 20:00:52 -07:00
|
|
|
|
|
|
|
if (!callToNotify) {
|
|
|
|
NS_ERROR("Don't call me with a bad call index!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2012-05-14 21:13:06 -07:00
|
|
|
}
|
|
|
|
|
2012-07-20 05:08:24 -07:00
|
|
|
if (mActiveCall && mActiveCall->CallIndex() == callToNotify->CallIndex()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mActiveCall = nullptr;
|
2012-07-20 05:08:24 -07:00
|
|
|
}
|
|
|
|
|
2012-06-20 20:00:52 -07:00
|
|
|
// Set the call state to 'disconnected' and remove it from the calls list.
|
|
|
|
callToNotify->NotifyError(aError);
|
|
|
|
|
2012-05-14 21:13:06 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-30 02:08:53 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::NotifyCdmaCallWaiting(const nsAString& aNumber)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mActiveCall &&
|
|
|
|
mActiveCall->CallState() == nsITelephonyProvider::CALL_STATE_CONNECTED);
|
|
|
|
|
|
|
|
nsRefPtr<TelephonyCall> callToNotify = mActiveCall;
|
|
|
|
callToNotify->UpdateSecondNumber(aNumber);
|
|
|
|
DispatchCallEvent(NS_LITERAL_STRING("callschanged"), callToNotify);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-27 23:39:50 -08:00
|
|
|
nsresult
|
|
|
|
Telephony::DispatchCallEvent(const nsAString& aType,
|
2013-07-15 01:27:19 -07:00
|
|
|
TelephonyCall* aCall)
|
2013-01-27 23:39:50 -08:00
|
|
|
{
|
2013-08-05 13:28:14 -07:00
|
|
|
// The call may be null in following cases:
|
|
|
|
// 1. callschanged when notifying enumeration being completed
|
|
|
|
// 2. remoteheld/remoteresumed.
|
|
|
|
MOZ_ASSERT(aCall ||
|
|
|
|
aType.EqualsLiteral("callschanged") ||
|
|
|
|
aType.EqualsLiteral("remoteheld") ||
|
|
|
|
aType.EqualsLiteral("remtoeresumed"));
|
2013-01-27 23:39:50 -08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-05-09 03:44:13 -07:00
|
|
|
void
|
|
|
|
Telephony::EnqueueEnumerationAck()
|
|
|
|
{
|
|
|
|
if (!mEnumerated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> task = new EnumerationAck(this);
|
|
|
|
if (NS_FAILED(NS_DispatchToCurrentThread(task))) {
|
|
|
|
NS_WARNING("Failed to dispatch to current thread!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 07:36:13 -07:00
|
|
|
/* static */
|
|
|
|
bool
|
|
|
|
Telephony::CheckPermission(nsPIDOMWindow* aWindow)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2013-07-12 07:36:13 -07:00
|
|
|
MOZ_ASSERT(aWindow && aWindow->IsInnerWindow());
|
2012-05-23 18:27:53 -07:00
|
|
|
|
2012-08-06 00:33:37 -07:00
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
2013-07-12 07:36:13 -07:00
|
|
|
NS_ENSURE_TRUE(permMgr, false);
|
2012-08-06 00:33:37 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t permission;
|
2012-05-23 21:02:23 -07:00
|
|
|
nsresult rv =
|
2012-09-21 03:32:18 -07:00
|
|
|
permMgr->TestPermissionFromWindow(aWindow, "telephony", &permission);
|
2013-07-12 07:36:13 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2012-08-06 00:33:37 -07:00
|
|
|
|
|
|
|
if (permission != nsIPermissionManager::ALLOW_ACTION) {
|
2013-07-12 07:36:13 -07:00
|
|
|
return false;
|
2012-01-11 18:19:53 -08:00
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-07-12 07:36:13 -07:00
|
|
|
return true;
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|