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 "Telephony.h"
|
|
|
|
|
|
|
|
#include "nsIURI.h"
|
2013-01-27 23:39:50 -08:00
|
|
|
#include "nsIDOMCallEvent.h"
|
2012-01-09 14:28:47 -08:00
|
|
|
#include "nsPIDOMWindow.h"
|
2013-01-27 23:39:50 -08:00
|
|
|
#include "nsIPermissionManager.h"
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-01-27 23:39:50 -08:00
|
|
|
#include "GeneratedEvents.h"
|
2012-01-09 14:28:47 -08:00
|
|
|
#include "jsapi.h"
|
2012-02-24 05:14:37 -08:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2012-01-09 14:28:47 -08:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDOMClassInfo.h"
|
2012-02-24 05:14:37 -08:00
|
|
|
#include "nsNetUtil.h"
|
2013-01-27 23:39:50 -08:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2012-09-28 10:43:49 -07:00
|
|
|
#include "nsTArrayHelpers.h"
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
#include "TelephonyCall.h"
|
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
#define NS_RILCONTENTHELPER_CONTRACTID "@mozilla.org/ril/content-helper;1"
|
|
|
|
|
2012-01-09 14:28:47 -08:00
|
|
|
USING_TELEPHONY_NAMESPACE
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Disconnect()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mTelephony);
|
|
|
|
mTelephony = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-24 05:14:37 -08:00
|
|
|
Telephony::Telephony()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mActiveCall(nullptr), mCallsArray(nullptr), mRooted(false)
|
2012-02-24 05:14:37 -08:00
|
|
|
{
|
|
|
|
if (!gTelephonyList) {
|
|
|
|
gTelephonyList = new TelephonyList();
|
|
|
|
}
|
|
|
|
|
|
|
|
gTelephonyList->AppendElement(this);
|
|
|
|
}
|
|
|
|
|
2012-01-09 14:28:47 -08:00
|
|
|
Telephony::~Telephony()
|
|
|
|
{
|
2013-03-06 01:53:23 -08:00
|
|
|
if (mListener) {
|
|
|
|
mListener->Disconnect();
|
2013-02-18 08:09:40 -08:00
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
if (mProvider) {
|
|
|
|
mProvider->UnregisterTelephonyMsg(mListener);
|
2013-02-18 08:09:40 -08:00
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
2012-02-23 07:09:11 -08:00
|
|
|
|
|
|
|
if (mRooted) {
|
2012-11-27 17:37:57 -08:00
|
|
|
mCallsArray = nullptr;
|
2012-02-23 07:09:11 -08:00
|
|
|
NS_DROP_JS_OBJECTS(this, Telephony);
|
|
|
|
}
|
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;
|
2012-02-24 05:14:37 -08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
gTelephonyList->RemoveElement(this);
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<Telephony>
|
2013-03-06 01:53:23 -08:00
|
|
|
Telephony::Create(nsPIDOMWindow* aOwner, nsITelephonyProvider* aProvider)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aOwner, "Null owner!");
|
2013-03-06 01:53:23 -08:00
|
|
|
NS_ASSERTION(aProvider, "Null provider!");
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aOwner);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(sgo, nullptr);
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(scriptContext, 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-03-06 01:53:23 -08:00
|
|
|
telephony->mProvider = aProvider;
|
|
|
|
telephony->mListener = new Listener(telephony);
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
nsresult rv = aProvider->EnumerateCalls(telephony->mListener);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
rv = aProvider->RegisterTelephonyMsg(telephony->mListener);
|
2012-10-09 03:07:11 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
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-03-06 01:53:23 -08:00
|
|
|
if (aCall->CallState() == nsITelephonyProvider::CALL_STATE_DIALING ||
|
|
|
|
aCall->CallState() == nsITelephonyProvider::CALL_STATE_ALERTING ||
|
|
|
|
aCall->CallState() == nsITelephonyProvider::CALL_STATE_CONNECTED) {
|
2013-01-04 00:52:18 -08:00
|
|
|
NS_ASSERTION(!mActiveCall, "Already have an active call!");
|
2012-06-08 00:09:15 -07:00
|
|
|
mActiveCall = aCall;
|
2013-01-04 00:52:18 -08:00
|
|
|
} else if (mActiveCall && mActiveCall->CallIndex() == aCall->CallIndex()) {
|
|
|
|
mActiveCall = nullptr;
|
2012-06-08 00:09:15 -07:00
|
|
|
}
|
|
|
|
|
2013-01-27 23:39:50 -08:00
|
|
|
return DispatchCallEvent(NS_LITERAL_STRING("callschanged"), aCall);
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2012-07-20 05:08:24 -07:00
|
|
|
nsresult
|
|
|
|
Telephony::DialInternal(bool isEmergency,
|
|
|
|
const nsAString& aNumber,
|
|
|
|
nsIDOMTelephonyCall** aResult)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(!aNumber.IsEmpty());
|
|
|
|
|
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!");
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
if (isEmergency) {
|
2013-03-06 01:53:23 -08:00
|
|
|
rv = mProvider->DialEmergency(aNumber);
|
2012-07-20 05:08:24 -07:00
|
|
|
} else {
|
2013-03-06 01:53:23 -08:00
|
|
|
rv = mProvider->Dial(aNumber);
|
2012-07-20 05:08:24 -07:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
call.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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)
|
2012-02-23 07:09:11 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < tmp->mCalls.Length(); index++) {
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mCalls[i]");
|
|
|
|
cb.NoteXPCOMChild(tmp->mCalls[index]->ToISupports());
|
|
|
|
}
|
2013-03-06 01:53:23 -08:00
|
|
|
// Don't traverse mListener because it doesn't keep any reference to
|
|
|
|
// Telephony but a raw pointer instead.
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2012-02-23 07:09:11 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(Telephony,
|
|
|
|
nsDOMEventTargetHelper)
|
2012-05-15 09:56:39 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mCallsArray)
|
2012-02-23 07:09:11 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(Telephony,
|
2012-02-08 11:22:01 -08:00
|
|
|
nsDOMEventTargetHelper)
|
2012-01-09 14:28:47 -08:00
|
|
|
tmp->mCalls.Clear();
|
2012-07-30 07:20:58 -07:00
|
|
|
tmp->mActiveCall = nullptr;
|
|
|
|
tmp->mCallsArray = nullptr;
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(Telephony)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMTelephony)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(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
|
|
|
|
|
|
|
DOMCI_DATA(Telephony, Telephony)
|
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
NS_IMPL_ISUPPORTS1(Telephony::Listener, nsITelephonyListener)
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::Dial(const nsAString& aNumber, nsIDOMTelephonyCall** aResult)
|
|
|
|
{
|
2012-07-20 05:08:24 -07:00
|
|
|
DialInternal(false, aNumber, aResult);
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2012-07-20 05:08:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2012-07-20 05:08:24 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::DialEmergency(const nsAString& aNumber, nsIDOMTelephonyCall** aResult)
|
|
|
|
{
|
|
|
|
DialInternal(true, aNumber, aResult);
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::GetMuted(bool* aMuted)
|
|
|
|
{
|
2013-03-06 01:53:23 -08:00
|
|
|
nsresult rv = mProvider->GetMicrophoneMuted(aMuted);
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::SetMuted(bool aMuted)
|
|
|
|
{
|
2013-03-06 01:53:23 -08:00
|
|
|
nsresult rv = mProvider->SetMicrophoneMuted(aMuted);
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::GetSpeakerEnabled(bool* aSpeakerEnabled)
|
|
|
|
{
|
2013-03-06 01:53:23 -08:00
|
|
|
nsresult rv = mProvider->GetSpeakerEnabled(aSpeakerEnabled);
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::SetSpeakerEnabled(bool aSpeakerEnabled)
|
|
|
|
{
|
2013-03-06 01:53:23 -08:00
|
|
|
nsresult rv = mProvider->SetSpeakerEnabled(aSpeakerEnabled);
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::GetActive(jsval* aActive)
|
|
|
|
{
|
|
|
|
if (!mActiveCall) {
|
|
|
|
aActive->setNull();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-13 15:11:16 -07:00
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-02-26 11:04:13 -08:00
|
|
|
AutoPushJSContext cx(sc ? sc->GetNativeContext() : nullptr);
|
2012-03-13 15:11:16 -07:00
|
|
|
if (sc) {
|
|
|
|
rv =
|
2013-02-26 11:04:13 -08:00
|
|
|
nsContentUtils::WrapNative(cx, sc->GetNativeGlobal(),
|
2012-03-13 15:11:16 -07:00
|
|
|
mActiveCall->ToISupports(), aActive);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-02-23 07:09:11 -08:00
|
|
|
Telephony::GetCalls(jsval* aCalls)
|
2012-01-09 14:28:47 -08:00
|
|
|
{
|
2012-02-23 07:09:11 -08:00
|
|
|
JSObject* calls = mCallsArray;
|
2012-01-09 14:28:47 -08:00
|
|
|
if (!calls) {
|
2012-03-13 15:11:16 -07:00
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
2012-02-23 07:09:11 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-02-26 11:04:13 -08:00
|
|
|
AutoPushJSContext cx(sc ? sc->GetNativeContext() : nullptr);
|
2012-03-13 15:11:16 -07:00
|
|
|
if (sc) {
|
2013-02-26 11:04:13 -08:00
|
|
|
rv = nsTArrayToJSArray(cx, mCalls, &calls);
|
2012-03-13 15:11:16 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-02-23 07:09:11 -08:00
|
|
|
|
2012-03-13 15:11:16 -07:00
|
|
|
if (!mRooted) {
|
|
|
|
NS_HOLD_JS_OBJECTS(this, Telephony);
|
|
|
|
mRooted = true;
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2012-03-13 15:11:16 -07:00
|
|
|
mCallsArray = calls;
|
|
|
|
} else {
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
|
|
|
|
2012-02-23 07:09:11 -08:00
|
|
|
aCalls->setObject(*calls);
|
2012-01-09 14:28:47 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::StartTone(const nsAString& aDTMFChar)
|
|
|
|
{
|
|
|
|
if (aDTMFChar.IsEmpty()) {
|
|
|
|
NS_WARNING("Empty tone string will be ignored");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aDTMFChar.Length() > 1) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
nsresult rv = mProvider->StartTone(aDTMFChar);
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::StopTone()
|
|
|
|
{
|
2013-03-06 01:53:23 -08:00
|
|
|
nsresult rv = mProvider->StopTone();
|
2012-01-09 14:28:47 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_EVENT_HANDLER(Telephony, incoming)
|
2012-02-24 05:14:37 -08:00
|
|
|
NS_IMPL_EVENT_HANDLER(Telephony, callschanged)
|
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,
|
2012-05-14 21:13:06 -07:00
|
|
|
const nsAString& aNumber, bool aIsActive)
|
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;
|
|
|
|
|
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);
|
|
|
|
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 =
|
|
|
|
TelephonyCall::Create(this, aNumber, aCallState, aCallIndex);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
bool* aContinue)
|
|
|
|
{
|
|
|
|
// Make sure we don't somehow add duplicates.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < mCalls.Length(); index++) {
|
2013-01-04 00:52:18 -08:00
|
|
|
nsRefPtr<TelephonyCall>& tempCall = mCalls[index];
|
|
|
|
if (tempCall->CallIndex() == aCallIndex) {
|
|
|
|
// We have the call already. Skip it.
|
|
|
|
*aContinue = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
}
|
2013-01-04 00:52:18 -08:00
|
|
|
|
2012-01-09 14:28:47 -08:00
|
|
|
nsRefPtr<TelephonyCall> call =
|
|
|
|
TelephonyCall::Create(this, aNumber, aCallState, aCallIndex);
|
|
|
|
NS_ASSERTION(call, "This should never fail!");
|
|
|
|
|
|
|
|
NS_ASSERTION(mCalls.Contains(call), "Should have auto-added new call!");
|
|
|
|
|
|
|
|
*aContinue = true;
|
|
|
|
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()) {
|
|
|
|
// The connection is not established yet. Get the latest call object.
|
|
|
|
if (aCallIndex == -1) {
|
|
|
|
callToNotify = mCalls[mCalls.Length() - 1];
|
|
|
|
} else {
|
|
|
|
// The connection has been established. Get the failed call.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < mCalls.Length(); index++) {
|
2012-06-20 20:00:52 -07:00
|
|
|
nsRefPtr<TelephonyCall>& call = mCalls[index];
|
|
|
|
if (call->CallIndex() == aCallIndex) {
|
|
|
|
callToNotify = call;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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-01-27 23:39:50 -08:00
|
|
|
nsresult
|
|
|
|
Telephony::DispatchCallEvent(const nsAString& aType,
|
|
|
|
nsIDOMTelephonyCall* aCall)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCall);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2013-03-09 03:34:29 -08:00
|
|
|
NS_NewDOMCallEvent(getter_AddRefs(event), this, nullptr, nullptr);
|
2013-01-27 23:39:50 -08: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-01-09 14:28:47 -08:00
|
|
|
nsresult
|
|
|
|
NS_NewTelephony(nsPIDOMWindow* aWindow, nsIDOMTelephony** aTelephony)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aWindow, "Null pointer!");
|
|
|
|
|
|
|
|
nsPIDOMWindow* innerWindow = aWindow->IsInnerWindow() ?
|
2012-05-23 21:02:23 -07:00
|
|
|
aWindow :
|
|
|
|
aWindow->GetCurrentInnerWindow();
|
2012-05-23 18:27:53 -07:00
|
|
|
|
2012-08-06 00:33:37 -07:00
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(permMgr, NS_ERROR_UNEXPECTED);
|
|
|
|
|
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);
|
2012-05-23 21:02:23 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-08-06 00:33:37 -07:00
|
|
|
|
|
|
|
if (permission != nsIPermissionManager::ALLOW_ACTION) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*aTelephony = nullptr;
|
2012-05-23 21:02:23 -07:00
|
|
|
return NS_OK;
|
2012-01-11 18:19:53 -08:00
|
|
|
}
|
2012-01-09 14:28:47 -08:00
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
nsCOMPtr<nsITelephonyProvider> ril =
|
2012-04-24 08:44:42 -07:00
|
|
|
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
|
2012-01-19 12:53:32 -08:00
|
|
|
NS_ENSURE_TRUE(ril, NS_ERROR_UNEXPECTED);
|
2012-01-11 18:19:53 -08:00
|
|
|
|
2012-01-19 12:53:32 -08:00
|
|
|
nsRefPtr<Telephony> telephony = Telephony::Create(innerWindow, ril);
|
2012-01-11 18:19:53 -08:00
|
|
|
NS_ENSURE_TRUE(telephony, NS_ERROR_UNEXPECTED);
|
2012-01-09 14:28:47 -08:00
|
|
|
|
|
|
|
telephony.forget(aTelephony);
|
|
|
|
return NS_OK;
|
|
|
|
}
|