2012-01-10 02:28:47 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=40: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Telephony.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* The Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2011
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Ben Turner <bent.mozilla@gmail.com> (Original Author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "Telephony.h"
|
|
|
|
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIURI.h"
|
2012-02-24 17:14:37 +04:00
|
|
|
#include "nsIURL.h"
|
2012-01-10 02:28:47 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2012-02-24 17:14:37 +04:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2012-01-10 02:28:47 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDOMClassInfo.h"
|
2012-01-10 03:18:23 +04:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2012-02-24 17:14:37 +04:00
|
|
|
#include "nsNetUtil.h"
|
2012-01-10 02:28:47 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2012-01-10 03:18:23 +04:00
|
|
|
#include "SystemWorkerManager.h"
|
2012-01-10 02:28:47 +04:00
|
|
|
|
|
|
|
#include "CallEvent.h"
|
|
|
|
#include "TelephonyCall.h"
|
|
|
|
|
|
|
|
USING_TELEPHONY_NAMESPACE
|
|
|
|
using mozilla::Preferences;
|
|
|
|
|
|
|
|
#define DOM_TELEPHONY_APP_PHONE_URL_PREF "dom.telephony.app.phone.url"
|
|
|
|
|
2012-02-23 19:09:11 +04:00
|
|
|
namespace {
|
|
|
|
|
2012-02-24 17:14:37 +04:00
|
|
|
typedef nsAutoTArray<Telephony*, 2> TelephonyList;
|
|
|
|
|
|
|
|
TelephonyList* gTelephonyList;
|
|
|
|
|
2012-02-23 19:09:11 +04:00
|
|
|
template <class T>
|
|
|
|
inline nsresult
|
|
|
|
nsTArrayToJSArray(JSContext* aCx, JSObject* aGlobal,
|
|
|
|
const nsTArray<nsRefPtr<T> >& aSourceArray,
|
|
|
|
JSObject** aResultArray)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aCx, "Null context!");
|
|
|
|
NS_ASSERTION(aGlobal, "Null global!");
|
|
|
|
|
|
|
|
JSAutoRequest ar(aCx);
|
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(aCx, aGlobal)) {
|
|
|
|
NS_WARNING("Failed to enter compartment!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* arrayObj;
|
|
|
|
|
|
|
|
if (aSourceArray.IsEmpty()) {
|
|
|
|
arrayObj = JS_NewArrayObject(aCx, 0, nsnull);
|
|
|
|
} else {
|
|
|
|
nsTArray<jsval> valArray;
|
|
|
|
valArray.SetLength(aSourceArray.Length());
|
|
|
|
|
|
|
|
for (PRUint32 index = 0; index < valArray.Length(); index++) {
|
|
|
|
nsISupports* obj = aSourceArray[index]->ToISupports();
|
|
|
|
nsresult rv =
|
|
|
|
nsContentUtils::WrapNative(aCx, aGlobal, obj, &valArray[index]);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
arrayObj = JS_NewArrayObject(aCx, valArray.Length(), valArray.Elements());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!arrayObj) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX This is not what Jonas wants. He wants it to be live.
|
|
|
|
if (!JS_FreezeObject(aCx, arrayObj)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResultArray = arrayObj;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2012-02-24 17:14:37 +04:00
|
|
|
Telephony::Telephony()
|
|
|
|
: mActiveCall(nsnull), mCallsArray(nsnull), mRooted(false)
|
|
|
|
{
|
|
|
|
if (!gTelephonyList) {
|
|
|
|
gTelephonyList = new TelephonyList();
|
|
|
|
}
|
|
|
|
|
|
|
|
gTelephonyList->AppendElement(this);
|
|
|
|
}
|
|
|
|
|
2012-01-10 02:28:47 +04:00
|
|
|
Telephony::~Telephony()
|
|
|
|
{
|
2012-01-20 00:53:32 +04:00
|
|
|
if (mRIL && mRILTelephonyCallback) {
|
|
|
|
mRIL->UnregisterCallback(mRILTelephonyCallback);
|
2012-01-10 02:28:47 +04:00
|
|
|
}
|
2012-02-23 19:09:11 +04:00
|
|
|
|
|
|
|
if (mRooted) {
|
|
|
|
NS_DROP_JS_OBJECTS(this, Telephony);
|
|
|
|
}
|
2012-02-24 17:14:37 +04: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;
|
|
|
|
gTelephonyList = nsnull;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gTelephonyList->RemoveElement(this);
|
|
|
|
}
|
2012-01-10 02:28:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<Telephony>
|
2012-01-20 00:53:32 +04:00
|
|
|
Telephony::Create(nsPIDOMWindow* aOwner, nsIRadioInterfaceLayer* aRIL)
|
2012-01-10 02:28:47 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aOwner, "Null owner!");
|
2012-01-20 00:53:32 +04:00
|
|
|
NS_ASSERTION(aRIL, "Null RIL!");
|
2012-01-10 02:28:47 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aOwner);
|
|
|
|
NS_ENSURE_TRUE(sgo, nsnull);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
|
|
|
|
NS_ENSURE_TRUE(scriptContext, nsnull);
|
|
|
|
|
|
|
|
nsRefPtr<Telephony> telephony = new Telephony();
|
|
|
|
|
|
|
|
telephony->mOwner = aOwner;
|
|
|
|
telephony->mScriptContext.swap(scriptContext);
|
2012-01-20 00:53:32 +04:00
|
|
|
telephony->mRIL = aRIL;
|
|
|
|
telephony->mRILTelephonyCallback = new RILTelephonyCallback(telephony);
|
2012-01-10 02:28:47 +04:00
|
|
|
|
2012-01-20 00:53:32 +04:00
|
|
|
nsresult rv = aRIL->EnumerateCalls(telephony->mRILTelephonyCallback);
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
|
2012-01-20 00:53:32 +04:00
|
|
|
rv = aRIL->RegisterCallback(telephony->mRILTelephonyCallback);
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
|
|
|
|
return telephony.forget();
|
|
|
|
}
|
|
|
|
|
2012-02-24 17:14:37 +04:00
|
|
|
already_AddRefed<TelephonyCall>
|
|
|
|
Telephony::CreateNewDialingCall(const nsAString& aNumber)
|
|
|
|
{
|
|
|
|
nsRefPtr<TelephonyCall> call =
|
|
|
|
TelephonyCall::Create(this, aNumber,
|
|
|
|
nsIRadioInterfaceLayer::CALL_STATE_DIALING);
|
|
|
|
NS_ASSERTION(call, "This should never fail!");
|
|
|
|
|
|
|
|
NS_ASSERTION(mCalls.Contains(call), "Should have auto-added new call!");
|
|
|
|
|
|
|
|
return call.forget();
|
|
|
|
}
|
|
|
|
|
2012-01-10 02:28:47 +04:00
|
|
|
void
|
2012-02-24 17:14:37 +04:00
|
|
|
Telephony::NoteDialedCallFromOtherInstance(const nsAString& aNumber)
|
2012-01-10 02:28:47 +04:00
|
|
|
{
|
2012-02-24 17:14:37 +04: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)
|
|
|
|
{
|
|
|
|
nsRefPtr<CallEvent> event = CallEvent::Create(aCall);
|
|
|
|
NS_ASSERTION(event, "This should never fail!");
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
event->Dispatch(ToIDOMEventTarget(), NS_LITERAL_STRING("callschanged"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-01-10 02:28:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Telephony)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(Telephony,
|
2012-02-08 23:22:01 +04:00
|
|
|
nsDOMEventTargetHelper)
|
2012-02-23 19:09:11 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(incoming)
|
2012-02-24 17:14:37 +04:00
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(callschanged)
|
2012-01-10 02:28:47 +04:00
|
|
|
for (PRUint32 index = 0; index < tmp->mCalls.Length(); index++) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mCalls[i]");
|
|
|
|
cb.NoteXPCOMChild(tmp->mCalls[index]->ToISupports());
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2012-02-23 19:09:11 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(Telephony,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_CALLBACK(tmp->mCallsArray, "mCallsArray")
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(Telephony,
|
2012-02-08 23:22:01 +04:00
|
|
|
nsDOMEventTargetHelper)
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(incoming)
|
2012-02-24 17:14:37 +04:00
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(callschanged)
|
2012-01-10 02:28:47 +04:00
|
|
|
tmp->mCalls.Clear();
|
|
|
|
tmp->mActiveCall = nsnull;
|
2012-02-23 19:09:11 +04:00
|
|
|
tmp->mCallsArray = nsnull;
|
2012-01-10 02:28:47 +04: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 23:22:01 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
2012-01-10 02:28:47 +04:00
|
|
|
|
2012-02-08 23:22:01 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(Telephony, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(Telephony, nsDOMEventTargetHelper)
|
2012-01-10 02:28:47 +04:00
|
|
|
|
|
|
|
DOMCI_DATA(Telephony, Telephony)
|
|
|
|
|
2012-01-20 00:53:32 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(Telephony::RILTelephonyCallback, nsIRILTelephonyCallback)
|
2012-01-10 02:28:47 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::Dial(const nsAString& aNumber, nsIDOMTelephonyCall** aResult)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(!aNumber.IsEmpty());
|
|
|
|
|
2012-01-12 06:17:26 +04:00
|
|
|
for (PRUint32 index = 0; index < mCalls.Length(); index++) {
|
|
|
|
const nsRefPtr<TelephonyCall>& tempCall = mCalls[index];
|
|
|
|
if (tempCall->IsOutgoing() &&
|
2012-01-20 00:53:32 +04:00
|
|
|
tempCall->CallState() < nsIRadioInterfaceLayer::CALL_STATE_CONNECTED) {
|
2012-01-12 06:17:26 +04: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-20 00:53:32 +04:00
|
|
|
nsresult rv = mRIL->Dial(aNumber);
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-02-24 17:14:37 +04:00
|
|
|
nsRefPtr<TelephonyCall> call = CreateNewDialingCall(aNumber);
|
2012-01-10 02:28:47 +04:00
|
|
|
|
2012-02-24 17:14:37 +04:00
|
|
|
// Notify other telephony objects that we just dialed.
|
|
|
|
for (PRUint32 index = 0; index < gTelephonyList->Length(); index++) {
|
|
|
|
Telephony*& telephony = gTelephonyList->ElementAt(index);
|
|
|
|
if (telephony != this) {
|
|
|
|
nsRefPtr<Telephony> kungFuDeathGrip = telephony;
|
|
|
|
telephony->NoteDialedCallFromOtherInstance(aNumber);
|
|
|
|
}
|
|
|
|
}
|
2012-01-10 02:28:47 +04:00
|
|
|
|
|
|
|
call.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::GetMuted(bool* aMuted)
|
|
|
|
{
|
2012-01-20 00:53:32 +04:00
|
|
|
nsresult rv = mRIL->GetMicrophoneMuted(aMuted);
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::SetMuted(bool aMuted)
|
|
|
|
{
|
2012-01-20 00:53:32 +04:00
|
|
|
nsresult rv = mRIL->SetMicrophoneMuted(aMuted);
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::GetSpeakerEnabled(bool* aSpeakerEnabled)
|
|
|
|
{
|
2012-01-20 00:53:32 +04:00
|
|
|
nsresult rv = mRIL->GetSpeakerEnabled(aSpeakerEnabled);
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::SetSpeakerEnabled(bool aSpeakerEnabled)
|
|
|
|
{
|
2012-01-20 00:53:32 +04:00
|
|
|
nsresult rv = mRIL->SetSpeakerEnabled(aSpeakerEnabled);
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::GetActive(jsval* aActive)
|
|
|
|
{
|
|
|
|
if (!mActiveCall) {
|
|
|
|
aActive->setNull();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
nsContentUtils::WrapNative(mScriptContext->GetNativeContext(),
|
|
|
|
mScriptContext->GetNativeGlobal(),
|
|
|
|
mActiveCall->ToISupports(), aActive);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-02-23 19:09:11 +04:00
|
|
|
Telephony::GetCalls(jsval* aCalls)
|
2012-01-10 02:28:47 +04:00
|
|
|
{
|
2012-02-23 19:09:11 +04:00
|
|
|
JSObject* calls = mCallsArray;
|
2012-01-10 02:28:47 +04:00
|
|
|
if (!calls) {
|
2012-02-23 19:09:11 +04:00
|
|
|
nsresult rv =
|
|
|
|
nsTArrayToJSArray(mScriptContext->GetNativeContext(),
|
|
|
|
mScriptContext->GetNativeGlobal(), mCalls, &calls);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!mRooted) {
|
|
|
|
NS_HOLD_JS_OBJECTS(this, Telephony);
|
|
|
|
mRooted = true;
|
|
|
|
}
|
2012-01-10 02:28:47 +04:00
|
|
|
|
|
|
|
mCallsArray = calls;
|
|
|
|
}
|
|
|
|
|
2012-02-23 19:09:11 +04:00
|
|
|
aCalls->setObject(*calls);
|
2012-01-10 02:28:47 +04: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;
|
|
|
|
}
|
|
|
|
|
2012-01-20 00:53:32 +04:00
|
|
|
nsresult rv = mRIL->StartTone(aDTMFChar);
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::StopTone()
|
|
|
|
{
|
2012-01-20 00:53:32 +04:00
|
|
|
nsresult rv = mRIL->StopTone();
|
2012-01-10 02:28:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_EVENT_HANDLER(Telephony, incoming)
|
2012-02-24 17:14:37 +04:00
|
|
|
NS_IMPL_EVENT_HANDLER(Telephony, callschanged)
|
2012-01-10 02:28:47 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::CallStateChanged(PRUint32 aCallIndex, PRUint16 aCallState,
|
|
|
|
const nsAString& aNumber)
|
|
|
|
{
|
2012-01-12 06:17:26 +04:00
|
|
|
NS_ASSERTION(aCallIndex != kOutgoingPlaceholderCallIndex,
|
|
|
|
"This should never happen!");
|
|
|
|
|
|
|
|
nsRefPtr<TelephonyCall> modifiedCall;
|
|
|
|
nsRefPtr<TelephonyCall> outgoingCall;
|
|
|
|
|
2012-01-10 02:28:47 +04:00
|
|
|
for (PRUint32 index = 0; index < mCalls.Length(); index++) {
|
|
|
|
nsRefPtr<TelephonyCall>& tempCall = mCalls[index];
|
2012-01-12 06:17:26 +04:00
|
|
|
if (tempCall->CallIndex() == kOutgoingPlaceholderCallIndex) {
|
|
|
|
NS_ASSERTION(!outgoingCall, "More than one outgoing call not supported!");
|
2012-02-24 17:14:37 +04:00
|
|
|
NS_ASSERTION(tempCall->CallState() ==
|
|
|
|
nsIRadioInterfaceLayer::CALL_STATE_DIALING,
|
2012-01-12 06:17:26 +04: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;
|
|
|
|
outgoingCall = nsnull;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-10 02:28:47 +04:00
|
|
|
|
2012-01-12 06:17:26 +04: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 &&
|
2012-01-20 00:53:32 +04:00
|
|
|
aCallState != nsIRadioInterfaceLayer::CALL_STATE_INCOMING &&
|
2012-01-12 06:17:26 +04:00
|
|
|
outgoingCall) {
|
|
|
|
outgoingCall->UpdateCallIndex(aCallIndex);
|
|
|
|
modifiedCall.swap(outgoingCall);
|
|
|
|
}
|
2012-01-10 02:28:47 +04:00
|
|
|
|
2012-01-12 06:17:26 +04:00
|
|
|
if (modifiedCall) {
|
|
|
|
// Change state.
|
|
|
|
modifiedCall->ChangeState(aCallState);
|
|
|
|
|
|
|
|
// See if this should replace our current active call.
|
2012-01-20 00:53:32 +04:00
|
|
|
if (aCallState == nsIRadioInterfaceLayer::CALL_STATE_CONNECTED) {
|
2012-02-24 17:14:37 +04:00
|
|
|
mActiveCall = modifiedCall;
|
2012-01-10 02:28:47 +04:00
|
|
|
}
|
2012-01-12 06:17:26 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2012-01-10 02:28:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't know anything about this call before now, must be incoming.
|
2012-01-20 00:53:32 +04:00
|
|
|
NS_ASSERTION(aCallState == nsIRadioInterfaceLayer::CALL_STATE_INCOMING,
|
2012-01-10 02:28:47 +04:00
|
|
|
"Serious logic problem here!");
|
|
|
|
|
|
|
|
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!");
|
|
|
|
|
|
|
|
// Dispatch incoming event.
|
|
|
|
nsRefPtr<CallEvent> event = CallEvent::Create(call);
|
|
|
|
NS_ASSERTION(event, "This should never fail!");
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
event->Dispatch(ToIDOMEventTarget(), NS_LITERAL_STRING("incoming"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Telephony::EnumerateCallState(PRUint32 aCallIndex, PRUint16 aCallState,
|
|
|
|
const nsAString& aNumber, bool aIsActive,
|
|
|
|
bool* aContinue)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Make sure we don't somehow add duplicates.
|
|
|
|
for (PRUint32 index = 0; index < mCalls.Length(); index++) {
|
|
|
|
NS_ASSERTION(mCalls[index]->CallIndex() != aCallIndex,
|
|
|
|
"Something is really wrong here!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
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!");
|
|
|
|
|
|
|
|
if (aIsActive) {
|
|
|
|
NS_ASSERTION(!mActiveCall, "Already have an active call!");
|
|
|
|
mActiveCall = call;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aContinue = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewTelephony(nsPIDOMWindow* aWindow, nsIDOMTelephony** aTelephony)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aWindow, "Null pointer!");
|
|
|
|
|
|
|
|
// Make sure we're dealing with an inner window.
|
|
|
|
nsPIDOMWindow* innerWindow = aWindow->IsInnerWindow() ?
|
|
|
|
aWindow :
|
|
|
|
aWindow->GetCurrentInnerWindow();
|
|
|
|
NS_ENSURE_TRUE(innerWindow, NS_ERROR_FAILURE);
|
|
|
|
|
2012-01-12 06:19:53 +04:00
|
|
|
// Make sure we're being called from a window that we have permission to
|
|
|
|
// access.
|
2012-01-10 02:28:47 +04:00
|
|
|
if (!nsContentUtils::CanCallerAccess(innerWindow)) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
2012-01-12 06:19:53 +04:00
|
|
|
// Need the document in order to make security decisions.
|
2012-01-10 02:28:47 +04:00
|
|
|
nsCOMPtr<nsIDocument> document =
|
|
|
|
do_QueryInterface(innerWindow->GetExtantDocument());
|
|
|
|
NS_ENSURE_TRUE(document, NS_NOINTERFACE);
|
|
|
|
|
2012-01-12 06:19:53 +04:00
|
|
|
// Do security checks. We assume that chrome is always allowed and we also
|
|
|
|
// allow a single page specified by preferences.
|
|
|
|
if (!nsContentUtils::IsSystemPrincipal(document->NodePrincipal())) {
|
2012-02-24 17:14:37 +04:00
|
|
|
nsCOMPtr<nsIURI> originalURI;
|
2012-01-12 06:19:53 +04:00
|
|
|
nsresult rv =
|
2012-02-24 17:14:37 +04:00
|
|
|
document->NodePrincipal()->GetURI(getter_AddRefs(originalURI));
|
2012-01-12 06:19:53 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-01-10 02:28:47 +04:00
|
|
|
|
2012-02-24 17:14:37 +04:00
|
|
|
nsCOMPtr<nsIURI> documentURI;
|
|
|
|
rv = originalURI->Clone(getter_AddRefs(documentURI));
|
2012-01-12 06:19:53 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-01-10 02:28:47 +04:00
|
|
|
|
2012-02-24 17:14:37 +04:00
|
|
|
// Strip the query string (if there is one) before comparing.
|
|
|
|
nsCOMPtr<nsIURL> documentURL = do_QueryInterface(documentURI);
|
|
|
|
if (documentURL) {
|
|
|
|
rv = documentURL->SetQuery(EmptyCString());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool allowed = false;
|
|
|
|
|
2012-01-12 06:19:53 +04:00
|
|
|
// The pref may not exist but in that case we deny access just as we do if
|
|
|
|
// the url doesn't match.
|
2012-02-24 17:14:37 +04:00
|
|
|
nsCString whitelist;
|
|
|
|
if (NS_SUCCEEDED(Preferences::GetCString(DOM_TELEPHONY_APP_PHONE_URL_PREF,
|
|
|
|
&whitelist))) {
|
|
|
|
nsCOMPtr<nsIIOService> ios = do_GetIOService();
|
|
|
|
NS_ENSURE_TRUE(ios, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCCharSeparatedTokenizer tokenizer(whitelist, ',');
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
if (NS_SUCCEEDED(NS_NewURI(getter_AddRefs(uri), tokenizer.nextToken(),
|
|
|
|
nsnull, nsnull, ios))) {
|
|
|
|
rv = documentURI->EqualsExceptRef(uri, &allowed);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (allowed) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!allowed) {
|
2012-01-12 06:19:53 +04:00
|
|
|
*aTelephony = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2012-01-10 02:28:47 +04:00
|
|
|
|
2012-01-12 06:19:53 +04:00
|
|
|
// Security checks passed, make a telephony object.
|
|
|
|
nsIInterfaceRequestor* ireq = SystemWorkerManager::GetInterfaceRequestor();
|
|
|
|
NS_ENSURE_TRUE(ireq, NS_ERROR_UNEXPECTED);
|
2012-01-10 03:18:23 +04:00
|
|
|
|
2012-01-20 00:53:32 +04:00
|
|
|
nsCOMPtr<nsIRadioInterfaceLayer> ril = do_GetInterface(ireq);
|
|
|
|
NS_ENSURE_TRUE(ril, NS_ERROR_UNEXPECTED);
|
2012-01-12 06:19:53 +04:00
|
|
|
|
2012-01-20 00:53:32 +04:00
|
|
|
nsRefPtr<Telephony> telephony = Telephony::Create(innerWindow, ril);
|
2012-01-12 06:19:53 +04:00
|
|
|
NS_ENSURE_TRUE(telephony, NS_ERROR_UNEXPECTED);
|
2012-01-10 02:28:47 +04:00
|
|
|
|
|
|
|
telephony.forget(aTelephony);
|
|
|
|
return NS_OK;
|
|
|
|
}
|