Files
palemoon27/dom/telephony/Telephony.cpp
T
roytam1 1e24b22ef4 remove nsINetUtil_ESR_38, fixup and import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1150366 - Remove duplicate d3.js. r=jsantell (71f991da6)
- Bug 1175352 - Refactor LoadInfo arguments to be more self contained (r=bent) (3f7d08164)
- Bug 1181533 - Add nsCRTGlue.h and prtime.h includes to nsDownloadManager.cpp to fix --disable-safe-browsing build bustage. r=froydnj (af338edf1)
- Bug 905127 - Part 1 - Make some functions from nsNetUtil not inline. r=jduell Bug 905127 - Part 2 - remove unnecessary nsNetUtil.h includes r=jduell Bug 905127 - Make some functions from nsNetUtil not inline. r=jduell (bc47d5b3a)
- Bug 1117650 - Part 1: Move all CORS tests into dom/security/test. r=sicking (6f727387c)
- Bug 1117650 - Part 2: Move all MixedContent tests into dom/security/test. r=tanvi (73f273829)
- Bug 1004703 - ignore 'unsafe-inline' if nonce- or hash-source specifed - tests (r=sstamm) (ebf411159)
- Bug 921493 - CSP: test whitelisting of scheme-relative sources (r=dveditz) (f0618b189)
- Bug 1139569 - Optimize update function for element editors in markup view;r=mratcliffe (c60d38dca)
- Partil of Bug 1139644 - Make markupview use sdk timers instead of individual timers for each window;r=pbrosset (8bc1a4d66)
- Bug 1146568 - Avoid unsafe CPOW usage warnings in browser_markupview_tag_edit_08.js; r=bgrins Making use of the devtools test frame-script to set the test node's attributes instead of using a CPOW. (61d7eed64)
- Bug 1146568 - Avoid unsafe CPOW usage in markupview tests, in assertAttributes helper; r=bgrins (efc492d5d)
- Bug 858038 - Allow moving elements in the markupview by drag/drop; r=pbrosset (51d79c15c)
- Bug 1139644 - Flash only relevant attributes in markup view when changed;r=pbrosset (29807fa46)
- Bug 1147128 - Make sure attribute shows up in markup view after removing and setting to the previous value;r=mratcliffe (1947f7417)
- Bug 1147325 - Clear box model timer on markup view destroy. r=pbrosset Prevents 'this._inspector.toolbox is null' spam in mochitest-dt (1d6808b4a)
- Bug 921102 - 1 - Linkify URIs in the inspector; r=tromey, miker This first part adds a parser for node attributes which, given some node information and an attribute name, generates a small AST-like array of objects that tells which parts of the attribute (if any) are links, and what they link to. Using this, the markup-view generates the right HTML structure to display these parts as links. This part 1 doesn't yet allow users to follow these links. (c500e270e)
- Bug 1139667 - CSP tests for fetch(). r=ckirschb (df030457f)
- Bug 1086999 - CSP: Asterisk (*) wildcard should not allow blob:, data:, or filesystem: when matching source expressions - tests (r=sstamm) (dee0f2239)
- Bug 1117650 - Part 3: Move all CSP tests into dom/security/test. r=sstamm (29c95475c)
- Bug 1146566 - 1 - Use devtools common frame-script in markupview tests and add helper; r=bgrins (b10e42468)
- Bug 1146566 - 2 - Avoid using CPOWs in test browser_markupview_tag_edit_03.js; r=bgrins This change updates the browser_markupview_tag_edit_03.js test by making use of the getDomElementInfo message listener to retrieve information about the tested nodes without having to go through CPOWs. (07f0ee3ff)
- Bug 1036324 - Adds option to walker.parents() to not traverse DocShellTreeItems of different types (24123a5d2)
- Bug 921102 - 2 - Markup-view tests for attribute links; r=miker (f6da81799)
- Bug 901250 - Add scroll into view menu item for the inspector. r=bgrins (bfc7518b1)
- Bug 994055 - Part 1: Add a toggle sidebar panel button to the inspector. r=miker This adds a new icon, next to the searchbox (like in the debugger) to expand or collapse the sidebar panel in the inspector. The state is *not* persisted yet when the toolbox is closed. The button is hidden when the toolbox host switches to 'side'. (87730e20a)
- Bug 994055 - Part 2: Add tests for the inspector sidebar toggle button. r=miker (2872c3fbd)
- Bug 921102 - 3 - Open/copy markup-view attribute links; r=bgrins This part adds contextual menu items that become enabled when the user right clicks on an attribute that has a link. Depending on the nature of the link, a new tab will be opened or a node selected. The user can also choose to copy the link in the clipboard. (9b93485f6)
- Bug 921102 - 4 - Tests for the open/copy links on markup-view attributes; r=bgrins (861ba51f6)
2021-05-21 09:14:47 +08:00

751 lines
20 KiB
C++

/* -*- 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 "Telephony.h"
#include "mozilla/Preferences.h"
#include "mozilla/dom/CallEvent.h"
#include "mozilla/dom/MozMobileConnectionBinding.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/TelephonyBinding.h"
#include "nsCharSeparatedTokenizer.h"
#include "nsContentUtils.h"
#include "nsIPermissionManager.h"
#include "nsIURI.h"
#include "nsPIDOMWindow.h"
#include "nsServiceManagerUtils.h"
#include "nsThreadUtils.h"
#include "CallsList.h"
#include "TelephonyCall.h"
#include "TelephonyCallGroup.h"
#include "TelephonyCallId.h"
#include "TelephonyDialCallback.h"
// Service instantiation
#include "ipc/TelephonyIPCService.h"
#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
#include "nsIGonkTelephonyService.h"
#endif
#include "nsXULAppAPI.h" // For XRE_GetProcessType()
using namespace mozilla::dom;
using namespace mozilla::dom::telephony;
using mozilla::ErrorResult;
class Telephony::Listener : public nsITelephonyListener
{
Telephony* mTelephony;
virtual ~Listener() {}
public:
NS_DECL_ISUPPORTS
NS_FORWARD_SAFE_NSITELEPHONYLISTENER(mTelephony)
explicit Listener(Telephony* aTelephony)
: mTelephony(aTelephony)
{
MOZ_ASSERT(mTelephony);
}
void
Disconnect()
{
MOZ_ASSERT(mTelephony);
mTelephony = nullptr;
}
};
Telephony::Telephony(nsPIDOMWindow* aOwner)
: DOMEventTargetHelper(aOwner)
{
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aOwner);
MOZ_ASSERT(global);
ErrorResult rv;
nsRefPtr<Promise> promise = Promise::Create(global, rv);
MOZ_ASSERT(!rv.Failed());
mReadyPromise = promise;
}
Telephony::~Telephony()
{
Shutdown();
}
void
Telephony::Shutdown()
{
if (mListener) {
mListener->Disconnect();
if (mService) {
mService->UnregisterListener(mListener);
mService = nullptr;
}
mListener = nullptr;
}
}
JSObject*
Telephony::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return TelephonyBinding::Wrap(aCx, this, aGivenProto);
}
// static
already_AddRefed<Telephony>
Telephony::Create(nsPIDOMWindow* aOwner, ErrorResult& aRv)
{
NS_ASSERTION(aOwner, "Null owner!");
nsCOMPtr<nsITelephonyService> ril =
do_GetService(TELEPHONY_SERVICE_CONTRACTID);
if (!ril) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aOwner);
if (!sgo) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
if (!scriptContext) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
nsRefPtr<Telephony> telephony = new Telephony(aOwner);
telephony->mService = ril;
telephony->mListener = new Listener(telephony);
telephony->mCallsList = new CallsList(telephony);
telephony->mGroup = TelephonyCallGroup::Create(telephony);
nsresult rv = ril->EnumerateCalls(telephony->mListener);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return telephony.forget();
}
// static
bool
Telephony::IsValidNumber(const nsAString& aNumber)
{
return !aNumber.IsEmpty();
}
// static
uint32_t
Telephony::GetNumServices() {
return mozilla::Preferences::GetInt("ril.numRadioInterfaces", 1);
}
// static
bool
Telephony::IsValidServiceId(uint32_t aServiceId)
{
return aServiceId < GetNumServices();
}
// static
bool
Telephony::IsActiveState(uint16_t aCallState) {
return aCallState == nsITelephonyService::CALL_STATE_DIALING ||
aCallState == nsITelephonyService::CALL_STATE_ALERTING ||
aCallState == nsITelephonyService::CALL_STATE_HOLDING ||
aCallState == nsITelephonyService::CALL_STATE_DISCONNECTING ||
aCallState == nsITelephonyService::CALL_STATE_CONNECTED;
}
uint32_t
Telephony::ProvidedOrDefaultServiceId(const Optional<uint32_t>& aServiceId)
{
if (aServiceId.WasPassed()) {
return aServiceId.Value();
} else {
uint32_t serviceId = 0;
mService->GetDefaultServiceId(&serviceId);
return serviceId;
}
}
bool
Telephony::HasDialingCall()
{
for (uint32_t i = 0; i < mCalls.Length(); i++) {
const nsRefPtr<TelephonyCall>& call = mCalls[i];
if (call->CallState() > nsITelephonyService::CALL_STATE_UNKNOWN &&
call->CallState() < nsITelephonyService::CALL_STATE_CONNECTED) {
return true;
}
}
return false;
}
already_AddRefed<Promise>
Telephony::DialInternal(uint32_t aServiceId, const nsAString& aNumber,
bool aEmergency, ErrorResult& aRv)
{
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
if (!global) {
return nullptr;
}
nsRefPtr<Promise> promise = Promise::Create(global, aRv);
if (aRv.Failed()) {
return nullptr;
}
if (!IsValidNumber(aNumber) || !IsValidServiceId(aServiceId)) {
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
return promise.forget();
}
// We only support one outgoing call at a time.
if (HasDialingCall()) {
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
return promise.forget();
}
nsCOMPtr<nsITelephonyDialCallback> callback =
new TelephonyDialCallback(GetOwner(), this, promise);
nsresult rv = mService->Dial(aServiceId, aNumber, aEmergency, callback);
if (NS_FAILED(rv)) {
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
return promise.forget();
}
return promise.forget();
}
already_AddRefed<TelephonyCallId>
Telephony::CreateCallId(nsITelephonyCallInfo *aInfo)
{
nsAutoString number;
nsAutoString name;
uint16_t numberPresentation;
uint16_t namePresentation;
aInfo->GetNumber(number);
aInfo->GetName(name);
aInfo->GetNumberPresentation(&numberPresentation);
aInfo->GetNamePresentation(&namePresentation);
return CreateCallId(number, numberPresentation, name, namePresentation);
}
already_AddRefed<TelephonyCallId>
Telephony::CreateCallId(const nsAString& aNumber, uint16_t aNumberPresentation,
const nsAString& aName, uint16_t aNamePresentation)
{
nsRefPtr<TelephonyCallId> id =
new TelephonyCallId(GetOwner(), aNumber, aNumberPresentation,
aName, aNamePresentation);
return id.forget();
}
already_AddRefed<TelephonyCall>
Telephony::CreateCall(TelephonyCallId* aId, uint32_t aServiceId,
uint32_t aCallIndex, uint16_t aCallState,
bool aEmergency, bool aConference,
bool aSwitchable, bool aMergeable)
{
// We don't have to create an already ended call.
if (aCallState == nsITelephonyService::CALL_STATE_DISCONNECTED) {
return nullptr;
}
nsRefPtr<TelephonyCall> call =
TelephonyCall::Create(this, aId, aServiceId, aCallIndex, aCallState,
aEmergency, aConference, aSwitchable, aMergeable);
NS_ASSERTION(call, "This should never fail!");
NS_ASSERTION(aConference ? mGroup->CallsArray().Contains(call)
: mCalls.Contains(call),
"Should have auto-added new call!");
return call.forget();
}
nsresult
Telephony::NotifyEvent(const nsAString& aType)
{
return DispatchCallEvent(aType, nullptr);
}
nsresult
Telephony::NotifyCallsChanged(TelephonyCall* aCall)
{
return DispatchCallEvent(NS_LITERAL_STRING("callschanged"), aCall);
}
already_AddRefed<TelephonyCall>
Telephony::GetCall(uint32_t aServiceId, uint32_t aCallIndex)
{
nsRefPtr<TelephonyCall> call;
for (uint32_t i = 0; i < mCalls.Length(); i++) {
nsRefPtr<TelephonyCall>& tempCall = mCalls[i];
if (tempCall->ServiceId() == aServiceId &&
tempCall->CallIndex() == aCallIndex) {
call = tempCall;
break;
}
}
return call.forget();
}
already_AddRefed<TelephonyCall>
Telephony::GetCallFromEverywhere(uint32_t aServiceId, uint32_t aCallIndex)
{
nsRefPtr<TelephonyCall> call = GetCall(aServiceId, aCallIndex);
if (!call) {
call = mGroup->GetCall(aServiceId, aCallIndex);
}
return call.forget();
}
nsresult
Telephony::HandleCallInfo(nsITelephonyCallInfo* aInfo)
{
uint32_t serviceId;
uint32_t callIndex;
uint16_t callState;
bool isEmergency;
bool isConference;
bool isSwitchable;
bool isMergeable;
aInfo->GetClientId(&serviceId);
aInfo->GetCallIndex(&callIndex);
aInfo->GetCallState(&callState);
aInfo->GetIsEmergency(&isEmergency);
aInfo->GetIsConference(&isConference);
aInfo->GetIsSwitchable(&isSwitchable);
aInfo->GetIsMergeable(&isMergeable);
nsRefPtr<TelephonyCall> call = GetCallFromEverywhere(serviceId, callIndex);
if (!call) {
nsRefPtr<TelephonyCallId> id = CreateCallId(aInfo);
call = CreateCall(id, serviceId, callIndex, callState, isEmergency,
isConference, isSwitchable, isMergeable);
if (call && callState == nsITelephonyService::CALL_STATE_INCOMING) {
nsresult rv = DispatchCallEvent(NS_LITERAL_STRING("incoming"), call);
NS_ENSURE_SUCCESS(rv, rv);
}
} else {
call->UpdateEmergency(isEmergency);
call->UpdateSwitchable(isSwitchable);
call->UpdateMergeable(isMergeable);
nsAutoString number;
aInfo->GetNumber(number);
nsRefPtr<TelephonyCallId> id = call->Id();
id->UpdateNumber(number);
// State changed.
if (call->CallState() != callState) {
if (callState == nsITelephonyService::CALL_STATE_DISCONNECTED) {
call->ChangeStateInternal(callState, true);
return NS_OK;
}
// 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.
bool fireEvent = !isConference;
call->ChangeStateInternal(callState, fireEvent);
}
// Group changed.
nsRefPtr<TelephonyCallGroup> group = call->GetGroup();
if (!group && isConference) {
// Add to conference.
NS_ASSERTION(mCalls.Contains(call), "Should in mCalls");
mGroup->AddCall(call);
RemoveCall(call);
} else if (group && !isConference) {
// Remove from conference.
NS_ASSERTION(mGroup->CallsArray().Contains(call), "Should in mGroup");
mGroup->RemoveCall(call);
AddCall(call);
}
}
return NS_OK;
}
NS_IMPL_CYCLE_COLLECTION_CLASS(Telephony)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(Telephony,
DOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCalls)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCallsList)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGroup)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReadyPromise)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(Telephony,
DOMEventTargetHelper)
tmp->Shutdown();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCalls)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCallsList)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mGroup)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mReadyPromise)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(Telephony)
// Telephony does not expose nsITelephonyListener. mListener is the exposed
// nsITelephonyListener and forwards the calls it receives to us.
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(Telephony, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(Telephony, DOMEventTargetHelper)
NS_IMPL_ISUPPORTS(Telephony::Listener, nsITelephonyListener)
// Telephony WebIDL
already_AddRefed<Promise>
Telephony::Dial(const nsAString& aNumber, const Optional<uint32_t>& aServiceId,
ErrorResult& aRv)
{
uint32_t serviceId = ProvidedOrDefaultServiceId(aServiceId);
nsRefPtr<Promise> promise = DialInternal(serviceId, aNumber, false, aRv);
return promise.forget();
}
already_AddRefed<Promise>
Telephony::DialEmergency(const nsAString& aNumber,
const Optional<uint32_t>& aServiceId,
ErrorResult& aRv)
{
uint32_t serviceId = ProvidedOrDefaultServiceId(aServiceId);
nsRefPtr<Promise> promise = DialInternal(serviceId, aNumber, true, aRv);
return promise.forget();
}
already_AddRefed<Promise>
Telephony::SendTones(const nsAString& aDTMFChars,
uint32_t aPauseDuration,
uint32_t aToneDuration,
const Optional<uint32_t>& aServiceId,
ErrorResult& aRv)
{
uint32_t serviceId = ProvidedOrDefaultServiceId(aServiceId);
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
if (!global) {
return nullptr;
}
nsRefPtr<Promise> promise = Promise::Create(global, aRv);
if (aRv.Failed()) {
return nullptr;
}
if (aDTMFChars.IsEmpty()) {
NS_WARNING("Empty tone string will be ignored");
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
return promise.forget();
}
if (!IsValidServiceId(serviceId)) {
aRv.Throw(NS_ERROR_INVALID_ARG);
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
return promise.forget();
}
nsCOMPtr<nsITelephonyCallback> callback =
new TelephonyCallback(promise);
aRv = mService->SendTones(serviceId, aDTMFChars, aPauseDuration,
aToneDuration, callback);
return promise.forget();
}
void
Telephony::StartTone(const nsAString& aDTMFChar,
const Optional<uint32_t>& aServiceId,
ErrorResult& aRv)
{
uint32_t serviceId = ProvidedOrDefaultServiceId(aServiceId);
if (aDTMFChar.IsEmpty()) {
NS_WARNING("Empty tone string will be ignored");
return;
}
if (aDTMFChar.Length() > 1 || !IsValidServiceId(serviceId)) {
aRv.Throw(NS_ERROR_INVALID_ARG);
return;
}
aRv = mService->StartTone(serviceId, aDTMFChar);
}
void
Telephony::StopTone(const Optional<uint32_t>& aServiceId, ErrorResult& aRv)
{
uint32_t serviceId = ProvidedOrDefaultServiceId(aServiceId);
if (!IsValidServiceId(serviceId)) {
aRv.Throw(NS_ERROR_INVALID_ARG);
return;
}
aRv = mService->StopTone(serviceId);
}
bool
Telephony::GetMuted(ErrorResult& aRv) const
{
bool muted = false;
aRv = mService->GetMicrophoneMuted(&muted);
return muted;
}
void
Telephony::SetMuted(bool aMuted, ErrorResult& aRv)
{
aRv = mService->SetMicrophoneMuted(aMuted);
}
bool
Telephony::GetSpeakerEnabled(ErrorResult& aRv) const
{
bool enabled = false;
aRv = mService->GetSpeakerEnabled(&enabled);
return enabled;
}
void
Telephony::SetSpeakerEnabled(bool aEnabled, ErrorResult& aRv)
{
aRv = mService->SetSpeakerEnabled(aEnabled);
}
void
Telephony::GetActive(Nullable<OwningTelephonyCallOrTelephonyCallGroup>& aValue)
{
if (mGroup->CallState() == nsITelephonyService::CALL_STATE_CONNECTED) {
aValue.SetValue().SetAsTelephonyCallGroup() = mGroup;
} else {
// Search the first active call.
for (uint32_t i = 0; i < mCalls.Length(); i++) {
if (IsActiveState(mCalls[i]->CallState())) {
aValue.SetValue().SetAsTelephonyCall() = mCalls[i];
return;
}
}
aValue.SetNull();
}
}
already_AddRefed<CallsList>
Telephony::Calls() const
{
nsRefPtr<CallsList> list = mCallsList;
return list.forget();
}
already_AddRefed<TelephonyCallGroup>
Telephony::ConferenceGroup() const
{
nsRefPtr<TelephonyCallGroup> group = mGroup;
return group.forget();
}
already_AddRefed<Promise>
Telephony::GetReady(ErrorResult& aRv) const
{
if (!mReadyPromise) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
nsRefPtr<Promise> promise = mReadyPromise;
return promise.forget();
}
// nsITelephonyListener
NS_IMETHODIMP
Telephony::CallStateChanged(nsITelephonyCallInfo* aInfo)
{
return HandleCallInfo(aInfo);
}
NS_IMETHODIMP
Telephony::EnumerateCallState(nsITelephonyCallInfo* aInfo)
{
return HandleCallInfo(aInfo);
}
NS_IMETHODIMP
Telephony::ConferenceCallStateChanged(uint16_t aCallState)
{
mGroup->ChangeState(aCallState);
return NS_OK;
}
NS_IMETHODIMP
Telephony::EnumerateCallStateComplete()
{
// Set conference state.
if (mGroup->CallsArray().Length() >= 2) {
const nsTArray<nsRefPtr<TelephonyCall> > &calls = mGroup->CallsArray();
uint16_t callState = calls[0]->CallState();
for (uint32_t i = 1; i < calls.Length(); i++) {
if (calls[i]->CallState() != callState) {
callState = nsITelephonyService::CALL_STATE_UNKNOWN;
break;
}
}
mGroup->ChangeState(callState);
}
if (mReadyPromise) {
mReadyPromise->MaybeResolve(JS::UndefinedHandleValue);
}
if (NS_FAILED(mService->RegisterListener(mListener))) {
NS_WARNING("Failed to register listener!");
}
return NS_OK;
}
NS_IMETHODIMP
Telephony::SupplementaryServiceNotification(uint32_t aServiceId,
int32_t aCallIndex,
uint16_t aNotification)
{
nsRefPtr<TelephonyCall> associatedCall;
if (!mCalls.IsEmpty()) {
associatedCall = GetCall(aServiceId, aCallIndex);
}
nsresult rv;
switch (aNotification) {
case nsITelephonyService::NOTIFICATION_REMOTE_HELD:
rv = DispatchCallEvent(NS_LITERAL_STRING("remoteheld"), associatedCall);
break;
case nsITelephonyService::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;
}
NS_IMETHODIMP
Telephony::NotifyError(uint32_t aServiceId,
int32_t aCallIndex,
const nsAString& aError)
{
nsRefPtr<TelephonyCall> callToNotify =
GetCallFromEverywhere(aServiceId, aCallIndex);
if (!callToNotify) {
NS_ERROR("Don't call me with a bad call index!");
return NS_ERROR_UNEXPECTED;
}
// Set the call state to 'disconnected' and remove it from the calls list.
callToNotify->NotifyError(aError);
return NS_OK;
}
NS_IMETHODIMP
Telephony::NotifyCdmaCallWaiting(uint32_t aServiceId, const nsAString& aNumber,
uint16_t aNumberPresentation,
const nsAString& aName,
uint16_t aNamePresentation)
{
MOZ_ASSERT(mCalls.Length() == 1);
nsRefPtr<TelephonyCall> callToNotify = mCalls[0];
MOZ_ASSERT(callToNotify && callToNotify->ServiceId() == aServiceId);
nsRefPtr<TelephonyCallId> id =
new TelephonyCallId(GetOwner(), aNumber, aNumberPresentation, aName,
aNamePresentation);
callToNotify->UpdateSecondId(id);
DispatchCallEvent(NS_LITERAL_STRING("callschanged"), callToNotify);
return NS_OK;
}
NS_IMETHODIMP
Telephony::NotifyConferenceError(const nsAString& aName,
const nsAString& aMessage)
{
mGroup->NotifyError(aName, aMessage);
return NS_OK;
}
nsresult
Telephony::DispatchCallEvent(const nsAString& aType,
TelephonyCall* aCall)
{
// If it is an incoming event, the call should not be null.
MOZ_ASSERT(!aType.EqualsLiteral("incoming") || aCall);
CallEventInit init;
init.mBubbles = false;
init.mCancelable = false;
init.mCall = aCall;
nsRefPtr<CallEvent> event = CallEvent::Constructor(this, aType, init);
return DispatchTrustedEvent(event);
}
already_AddRefed<nsITelephonyService>
NS_CreateTelephonyService()
{
nsCOMPtr<nsITelephonyService> service;
if (XRE_IsContentProcess()) {
service = new mozilla::dom::telephony::TelephonyIPCService();
} else {
#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
service = do_CreateInstance(GONK_TELEPHONY_SERVICE_CONTRACTID);
#endif
}
return service.forget();
}