Files
palemoon27/dom/mobileconnection/MobileConnection.cpp
T
roytam1 7edec4880a import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 859764 - Part 5: Changes in Test Cases. r=echen (f1a5cd4fd3)
- Bug 859764 - Part 6: Changes in Payment. r=ferjm, r=smaug (2b47641375)
- Bug 1010756 - Helpful errors for using nsCOMPtr on non-XPCOM types; r=froydnj (6b1521c482)
- leftover (e1a24351e9)
- Bug 1192102 - Remove unused file embedded/android/GeckoSmsManager.java. r=blassey (ac05ae649d)
- var-let (8ddb529f96)
- Bug 1181466 - Fix observe function in SmsService/MmsService. r=btseng (ca93122404)
- Bug 733331 - Part 2: Update enabledGsmTableTuples when MCC changes in SmsService.js and fix segmentChars in SmsSegmentHelper.jsm. r=btseng (24fa23e4be)
- Bug 1173156 - Fix typo and add a Marionette test case. r=btseng (e7199eb55a)
- Bug 1132774 - [B2G][SMS] Enable DEBUG Flag in SmsService if the default value of "ril.debugging.enabled" is true. r=btseng (7300d24fb7)
- Bug 1169160 - [MobileConnection] Support more call barring program: all service, outgoing service and incoming service. r=hsinyi,aknow (872c2cc056)
- Bug 1169225 - [MobileConnectionService] Support setting/getting call waiting on all serviceClass. r=aknow (14c546a9ca)
- Bug 1110619 - Part 1: IDL Interface Changes. r=echen (beae2b4b77)
- Bug 1168064 - B2G RIL: filter out cell info with unknown values. r=echen (11dfc5a7e8)
- Bug 1159591 - Part 2: Move MMI logic from ril_worker to telephonyService (Call Forwarding). r=aknow (b4edb76863)
- Bug 1159591 - Part 3: Move MMI logic from ril_worker to telephonyService (Icc Lock). r=aknow (3faba76808)
- Bug 1138263 - Make TelephonyService.js JSHint friendly. r=hsinyi (454bd4c765)
- Bug 1159591 - Part 4: Move MMI logic from ril_worker to telephonyService (IMEI). r=aknow (57f423ecdd)
- Bug 1159591 - Part 5: Move MMI logic from ril_worker to telephonyService (CLIP). r=aknow (c6611dbcb8)
- Bug 1159591 - Part 6: Move MMI logic from ril_worker to telephonyService (CLIR). r=aknow (8c1fc03edb)
- Bug 1159591 - Part 7: Move MMI logic from ril_worker to telephonyService (Call Barring Password). r=aknow (5d8bcbf177)
- Bug 1159591 - Part 8: Move MMI logic from ril_worker to telephonyService (Call Barring). r=aknow (aa0b89aea1)
- Bug 1159591 - Part 9: Move MMI logic from ril_worker to telephonyService (Call Waiting). r=aknow (1d14d2b864)
- Bug 1159591 - Part 10: Move MMI logic from ril_worker to telephonyService (USSD). r=aknow (c92130b5b2)
- Bug 1159591 - Part 11: Move MMI consts from ril_consts to telephonyService. r=aknow (0e7b3fea9a)
- Bug 1159591 - Part 12: Move radio check for MMI to a common place. r=aknow (dcdc3178ad)
- Bug 1110619 - Part 2: Implementation Changes. r=echen (6d8e78d684)
- Bug 1110619 - Part 3: Bluetooth Changes. r=btian (9787727be0)
- Bug 1147736 - Part 1: Extend TelephonyCallInfo. r=aknow (2a426cc99f)
- Bug 1147736 - Part 2: Bypass NotifyError. r=aknow (6cd6fd6867)
- Bug 1147736 - Part 3: Deprecate NotifyError. r=aknow (e41c719442)
- Bug 1147736 - Part 4: Deprecate NotifyError(Bluetooth). r=btian (9c8f97bb22)
- Bug 1204817 - Delete the child property of a parent call only when the parent call exists. r=btseng (d35dc6b08f)
- Bug 1191205 - Cancel USSD sessions only when needed. r=edgar (01a72dbacb)
- Bug 1200134 - Control USSD Sessions with State-Transitions instead of Boolean. r=echen (a4e55b3d9d)
- Bug 1163511 - Use defineLazyModuleGetter. r=hsinyi (8a97a4912c)
- Bug 991582 - Part 2: Handle the result of RIL request in a consistent way. r=aknow (914ecc2bbb)
- Bug 1164248 - Handling of session/sessionEnded for notifyUssdReceived. r=edgar (a79df75d38)
- Bug 1223662 - Part 1: Check mmiServiceCode with correct constant. r=echen (abeb286050)
- Bug 1174673 - Part 1: Automatically resume the held call. r=hsinyi (280543af7d)
- Bug 1174673 - Part 2: Update test case. r=hsinyi (cfe19f1a52)
- Bug 1185156 - Fix bug in resuming held call. r=hsinyi (b96346d319)
- Bug 1162426 - Part 1: Provide TelephonyUtils. r=hsinyi (62b71e6e83)
- Bug 1162426 - Part 2: Test case. r=hsniyi (13a0b3c6f5)
- Bug 1171807 - Part 1: Add enums for TelephonyCall::State and TelephonyCallGroup::State (WebIDL). r=hsinyi (0b698eecc4)
- Bug 1145551 - DTMF should be sent using the active SIM, the given or the default one (in that order). r=aknow (6157636493)
- Bug 1171807 - Part 2: Move to enums and deprecate TelephonyCall.mCallState and TelephonyCallGroup.mCallState (DOM). r=btseng (5faef22d91)
- Bug 1168515 - do not block incall MMI requests on alerting state. r=aknow (b1f85c5789)
- Bug 1155072 - Part 1: Deprecate nsITelephonyListener.conferenceCallStateChanged (Telephony). r=btseng (e41d1a4bc9)
- Bug 1155072 - Part 2: Deprecate nsITelephonyListener.conferenceCallStateChanged (Bluetooth). r=btian (27e69fa89b)
- Bug 1166936 - JS Warning in TelephonyService.js r=aknow (3559d3ad3d)
- Bug 1191237 - Part 1: Enhance |TelephonyService.js|. r=aknow (86576a6d32)
- Bug 1202902 - Fix the world. (0dc256d67d)
- Bug 1161438 - Part 1 - Exporting contact to SIM should also return updated contact. r=echen (505d7d7f83)
- Bug 1159622 - Split test_icc_contact.js into read contact and add contact. r=echen (fec0c428df)
- Bug 1122376 - Support read SIM contact dialling number exceed 20 digits. r=echen (5d0599e93c)
- Bug 1161438 - Part 2 - marionette testcase. r=echen (1f0d18a479)
- Bug 1114937 - Part 5: Fix Test Case to Remove Contact with Correct Contact Id. r=echen (8d746fdbd2)
- Bug 1194149 - Continue importing contacts when there is no sufficient Type 2 USIM contact fields record. r=echen (e9be40dbf2)
- Bug 962995 - xpcshell tests for write ICC UCS2 characters for 0x81 and 0x82 encoding. r=echen (9500afaa4d)
- Bug 1161438 - Part 3 - xpcshell testcase. r=echen (01f7fb4514)
- Bug 1122376 - Support write SIM contact dialling number exceed 20 digits. r=echen (91133e286d)
- Bug 999300 - Part 1: Removed the Ril v5 legacy support. r=edgar (ded77fcb6f)
- Bug 999300 - Part 2: Update the related testcases. r=edgar (f77a8b96cc)
- Bug 1177146 - [Aries][RIL] Reply from QUERY_AVAILABLE_NETWORKS has extra strings. r=hsinyi (a6816cbbab)
- Bug 1043250 - Part 1: Update ril_worker and xpcshell test. r=btseng (8b9b25b5cf)
- Bug 1185406 - B2G RIL: Read 'pcscf' and expose it in nsIRilNetworkInfo. r=hsinyi (ce707ecb83)
- Bug 1174998 - Part 3: Read data call's MTU from network/apn settings. r=echen (bfa08d8380)
- Bug 1166320 - Make volume service safer to use off main thread. r=dhylands (b3976622ad)
- Bug 1177374 - Call realpath on volume mount points so thatVolumeService::GetVolumeByPath works properly. r=achen (aea97080eb)
- Bug 1195166 - AutoMounter: add ignore command to allow volumes to be ignored. r=alchen (f265d832c8)
- Bug 1196724 - Refactoring of AudioManager r=alwu (e5b896b7ce)
- Bug 1222564 - Save audio volume to database r=alwu (1303d01ae7)
- Bug 1164049 - Fix some mode lines in embedding/. r=smaug (79ddce4871)
2023-01-24 11:52:32 +08:00

1173 lines
31 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 "mozilla/dom/MobileConnection.h"
#include "MobileConnectionCallback.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/dom/CFStateChangeEvent.h"
#include "mozilla/dom/DataErrorEvent.h"
#include "mozilla/dom/MozClirModeEvent.h"
#include "mozilla/dom/MozEmergencyCbModeEvent.h"
#include "mozilla/dom/MozOtaStatusEvent.h"
#include "mozilla/dom/ToJSValue.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "nsIDOMDOMRequest.h"
#include "nsIIccInfo.h"
#include "nsIPermissionManager.h"
#include "nsIVariant.h"
#include "nsJSON.h"
#include "nsJSUtils.h"
#include "nsServiceManagerUtils.h"
#define MOBILECONN_ERROR_INVALID_PARAMETER NS_LITERAL_STRING("InvalidParameter")
#define MOBILECONN_ERROR_INVALID_PASSWORD NS_LITERAL_STRING("InvalidPassword")
#ifdef CONVERT_STRING_TO_NULLABLE_ENUM
#undef CONVERT_STRING_TO_NULLABLE_ENUM
#endif
#define CONVERT_STRING_TO_NULLABLE_ENUM(_string, _enumType, _enum) \
{ \
uint32_t i = 0; \
for (const EnumEntry* entry = _enumType##Values::strings; \
entry->value; \
++entry, ++i) { \
if (_string.EqualsASCII(entry->value)) { \
_enum.SetValue(static_cast<_enumType>(i)); \
} \
} \
}
using mozilla::ErrorResult;
using namespace mozilla::dom;
using namespace mozilla::dom::mobileconnection;
class MobileConnection::Listener final : public nsIMobileConnectionListener
, public nsIIccListener
{
MobileConnection* mMobileConnection;
public:
NS_DECL_ISUPPORTS
NS_FORWARD_SAFE_NSIMOBILECONNECTIONLISTENER(mMobileConnection)
NS_FORWARD_SAFE_NSIICCLISTENER(mMobileConnection)
explicit Listener(MobileConnection* aMobileConnection)
: mMobileConnection(aMobileConnection)
{
MOZ_ASSERT(mMobileConnection);
}
void Disconnect()
{
MOZ_ASSERT(mMobileConnection);
mMobileConnection = nullptr;
}
private:
~Listener()
{
MOZ_ASSERT(!mMobileConnection);
}
};
NS_IMPL_ISUPPORTS(MobileConnection::Listener, nsIMobileConnectionListener,
nsIIccListener)
NS_IMPL_CYCLE_COLLECTION_CLASS(MobileConnection)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MobileConnection,
DOMEventTargetHelper)
// Don't traverse mListener because it doesn't keep any reference to
// MobileConnection but a raw pointer instead. Neither does mMobileConnection
// because it's an xpcom service owned object and is only released at shutting
// down.
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mVoice)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mData)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mIccHandler)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MobileConnection,
DOMEventTargetHelper)
tmp->Shutdown();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mVoice)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mData)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mIccHandler)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MobileConnection)
// MobileConnection does not expose nsIMobileConnectionListener. mListener is
// the exposed nsIMobileConnectionListener and forwards the calls it receives
// to us.
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(MobileConnection, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(MobileConnection, DOMEventTargetHelper)
MobileConnection::MobileConnection(nsPIDOMWindow* aWindow, uint32_t aClientId)
: DOMEventTargetHelper(aWindow)
, mClientId(aClientId)
{
nsCOMPtr<nsIMobileConnectionService> service =
do_GetService(NS_MOBILE_CONNECTION_SERVICE_CONTRACTID);
// Per WebAPI design, mIccId should be null instead of an empty string when no
// SIM card is inserted. Set null as default value.
mIccId.SetIsVoid(true);
// Not being able to acquire the service isn't fatal since we check
// for it explicitly below.
if (!service) {
NS_WARNING("Could not acquire nsIMobileConnectionService!");
return;
}
nsresult rv = service->GetItemByServiceId(mClientId,
getter_AddRefs(mMobileConnection));
if (NS_FAILED(rv) || !mMobileConnection) {
NS_WARNING("Could not acquire nsIMobileConnection!");
return;
}
mListener = new Listener(this);
mVoice = new MobileConnectionInfo(GetOwner());
mData = new MobileConnectionInfo(GetOwner());
if (CheckPermission("mobileconnection")) {
DebugOnly<nsresult> rv = mMobileConnection->RegisterListener(mListener);
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed registering mobile connection messages with service");
UpdateVoice();
UpdateData();
nsCOMPtr<nsIIccService> iccService = do_GetService(ICC_SERVICE_CONTRACTID);
if (iccService) {
iccService->GetIccByServiceId(mClientId, getter_AddRefs(mIccHandler));
}
if (!mIccHandler) {
NS_WARNING("Could not acquire nsIMobileConnection or nsIIcc!");
return;
}
rv = mIccHandler->RegisterListener(mListener);
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed registering icc messages with service");
UpdateIccId();
}
}
void
MobileConnection::Shutdown()
{
if (mListener) {
if (mMobileConnection) {
mMobileConnection->UnregisterListener(mListener);
}
if (mIccHandler) {
mIccHandler->UnregisterListener(mListener);
}
mListener->Disconnect();
mListener = nullptr;
}
}
MobileConnection::~MobileConnection()
{
Shutdown();
}
void
MobileConnection::DisconnectFromOwner()
{
DOMEventTargetHelper::DisconnectFromOwner();
// Event listeners can't be handled anymore, so we can shutdown
// the MobileConnection.
Shutdown();
}
JSObject*
MobileConnection::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return MozMobileConnectionBinding::Wrap(aCx, this, aGivenProto);
}
bool
MobileConnection::CheckPermission(const char* aType) const
{
nsCOMPtr<nsIPermissionManager> permMgr =
mozilla::services::GetPermissionManager();
NS_ENSURE_TRUE(permMgr, false);
uint32_t permission = nsIPermissionManager::DENY_ACTION;
permMgr->TestPermissionFromWindow(GetOwner(), aType, &permission);
return permission == nsIPermissionManager::ALLOW_ACTION;
}
void
MobileConnection::UpdateVoice()
{
if (!mMobileConnection) {
return;
}
nsCOMPtr<nsIMobileConnectionInfo> info;
mMobileConnection->GetVoice(getter_AddRefs(info));
mVoice->Update(info);
}
void
MobileConnection::UpdateData()
{
if (!mMobileConnection) {
return;
}
nsCOMPtr<nsIMobileConnectionInfo> info;
mMobileConnection->GetData(getter_AddRefs(info));
mData->Update(info);
}
bool
MobileConnection::UpdateIccId()
{
nsAutoString iccId;
nsCOMPtr<nsIIccInfo> iccInfo;
if (mIccHandler &&
NS_SUCCEEDED(mIccHandler->GetIccInfo(getter_AddRefs(iccInfo))) &&
iccInfo) {
iccInfo->GetIccid(iccId);
} else {
iccId.SetIsVoid(true);
}
if (!mIccId.Equals(iccId)) {
mIccId = iccId;
return true;
}
return false;
}
nsresult
MobileConnection::NotifyError(nsIDOMDOMRequest* aRequest, const nsAString& aMessage)
{
nsCOMPtr<nsIDOMRequestService> rs = do_GetService(DOMREQUEST_SERVICE_CONTRACTID);
NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
return rs->FireErrorAsync(aRequest, aMessage);
}
bool
MobileConnection::IsValidPassword(const nsAString& aPassword)
{
// Check valid PIN for supplementary services. See TS.22.004 clause 5.2.
if (aPassword.IsEmpty() || aPassword.Length() != 4) {
return false;
}
nsresult rv;
int32_t password = nsString(aPassword).ToInteger(&rv);
return NS_SUCCEEDED(rv) && password >= 0 && password <= 9999;
}
bool
MobileConnection::IsValidCallForwardingReason(int32_t aReason)
{
return aReason >= nsIMobileConnection::CALL_FORWARD_REASON_UNCONDITIONAL &&
aReason <= nsIMobileConnection::CALL_FORWARD_REASON_ALL_CONDITIONAL_CALL_FORWARDING;
}
bool
MobileConnection::IsValidCallForwardingAction(int32_t aAction)
{
return aAction >= nsIMobileConnection::CALL_FORWARD_ACTION_DISABLE &&
aAction <= nsIMobileConnection::CALL_FORWARD_ACTION_ERASURE &&
// Set operation doesn't allow "query" action.
aAction != nsIMobileConnection::CALL_FORWARD_ACTION_QUERY_STATUS;
}
bool
MobileConnection::IsValidCallBarringProgram(int32_t aProgram)
{
return aProgram >= nsIMobileConnection::CALL_BARRING_PROGRAM_ALL_OUTGOING &&
aProgram <= nsIMobileConnection::CALL_BARRING_PROGRAM_INCOMING_SERVICE;
}
bool
MobileConnection::IsValidCallBarringOptions(const MozCallBarringOptions& aOptions,
bool isSetting)
{
if (!aOptions.mServiceClass.WasPassed() || aOptions.mServiceClass.Value().IsNull() ||
!aOptions.mProgram.WasPassed() || aOptions.mProgram.Value().IsNull() ||
!IsValidCallBarringProgram(aOptions.mProgram.Value().Value())) {
return false;
}
// For setting callbarring options, |enabled| and |password| are required.
if (isSetting &&
(!aOptions.mEnabled.WasPassed() || aOptions.mEnabled.Value().IsNull() ||
!aOptions.mPassword.WasPassed() || aOptions.mPassword.Value().IsVoid())) {
return false;
}
return true;
}
bool
MobileConnection::IsValidCallForwardingOptions(const MozCallForwardingOptions& aOptions)
{
if (!aOptions.mReason.WasPassed() || aOptions.mReason.Value().IsNull() ||
!aOptions.mAction.WasPassed() || aOptions.mAction.Value().IsNull() ||
!IsValidCallForwardingReason(aOptions.mReason.Value().Value()) ||
!IsValidCallForwardingAction(aOptions.mAction.Value().Value())) {
return false;
}
return true;
}
// WebIDL interface
void
MobileConnection::GetLastKnownNetwork(nsString& aRetVal) const
{
aRetVal.SetIsVoid(true);
if (!mMobileConnection) {
return;
}
mMobileConnection->GetLastKnownNetwork(aRetVal);
}
void
MobileConnection::GetLastKnownHomeNetwork(nsString& aRetVal) const
{
aRetVal.SetIsVoid(true);
if (!mMobileConnection) {
return;
}
mMobileConnection->GetLastKnownHomeNetwork(aRetVal);
}
// All fields below require the "mobileconnection" permission.
MobileConnectionInfo*
MobileConnection::Voice() const
{
return mVoice;
}
MobileConnectionInfo*
MobileConnection::Data() const
{
return mData;
}
void
MobileConnection::GetIccId(nsString& aRetVal) const
{
aRetVal = mIccId;
}
Nullable<MobileNetworkSelectionMode>
MobileConnection::GetNetworkSelectionMode() const
{
Nullable<MobileNetworkSelectionMode> retVal =
Nullable<MobileNetworkSelectionMode>();
if (!mMobileConnection) {
return retVal;
}
int32_t mode = nsIMobileConnection::NETWORK_SELECTION_MODE_UNKNOWN;
if (NS_SUCCEEDED(mMobileConnection->GetNetworkSelectionMode(&mode)) &&
mode != nsIMobileConnection::NETWORK_SELECTION_MODE_UNKNOWN) {
MOZ_ASSERT(mode < static_cast<int32_t>(MobileNetworkSelectionMode::EndGuard_));
retVal.SetValue(static_cast<MobileNetworkSelectionMode>(mode));
}
return retVal;
}
Nullable<MobileRadioState>
MobileConnection::GetRadioState() const
{
Nullable<MobileRadioState> retVal = Nullable<MobileRadioState>();
if (!mMobileConnection) {
return retVal;
}
int32_t state = nsIMobileConnection::MOBILE_RADIO_STATE_UNKNOWN;
if (NS_SUCCEEDED(mMobileConnection->GetRadioState(&state)) &&
state != nsIMobileConnection::MOBILE_RADIO_STATE_UNKNOWN) {
MOZ_ASSERT(state < static_cast<int32_t>(MobileRadioState::EndGuard_));
retVal.SetValue(static_cast<MobileRadioState>(state));
}
return retVal;
}
void
MobileConnection::GetSupportedNetworkTypes(nsTArray<MobileNetworkType>& aTypes) const
{
if (!mMobileConnection) {
return;
}
int32_t* types = nullptr;
uint32_t length = 0;
nsresult rv = mMobileConnection->GetSupportedNetworkTypes(&types, &length);
NS_ENSURE_SUCCESS_VOID(rv);
for (uint32_t i = 0; i < length; ++i) {
int32_t type = types[i];
MOZ_ASSERT(type < static_cast<int32_t>(MobileNetworkType::EndGuard_));
aTypes.AppendElement(static_cast<MobileNetworkType>(type));
}
free(types);
}
already_AddRefed<DOMRequest>
MobileConnection::GetNetworks(ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv = mMobileConnection->GetNetworks(requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::SelectNetwork(MobileNetworkInfo& aNetwork, ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv = mMobileConnection->SelectNetwork(&aNetwork, requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::SelectNetworkAutomatically(ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv =
mMobileConnection->SelectNetworkAutomatically(requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::SetPreferredNetworkType(MobilePreferredNetworkType& aType,
ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
int32_t type = static_cast<int32_t>(aType);
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv =
mMobileConnection->SetPreferredNetworkType(type, requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::GetPreferredNetworkType(ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv = mMobileConnection->GetPreferredNetworkType(requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::SetRoamingPreference(MobileRoamingMode& aMode,
ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
int32_t mode = static_cast<int32_t>(aMode);
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv = mMobileConnection->SetRoamingPreference(mode, requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::GetRoamingPreference(ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv = mMobileConnection->GetRoamingPreference(requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::SetVoicePrivacyMode(bool aEnabled, ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv =
mMobileConnection->SetVoicePrivacyMode(aEnabled, requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::GetVoicePrivacyMode(ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv = mMobileConnection->GetVoicePrivacyMode(requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::GetCallForwardingOption(uint16_t aReason, ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
if (!IsValidCallForwardingReason(aReason)) {
nsresult rv = NotifyError(request, MOBILECONN_ERROR_INVALID_PARAMETER);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv = mMobileConnection->GetCallForwarding(aReason, requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::SetCallForwardingOption(const MozCallForwardingOptions& aOptions,
ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
if (!IsValidCallForwardingOptions(aOptions)) {
nsresult rv = NotifyError(request, MOBILECONN_ERROR_INVALID_PARAMETER);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
// Fill in optional attributes.
uint16_t timeSeconds = 0;
if (aOptions.mTimeSeconds.WasPassed() && !aOptions.mTimeSeconds.Value().IsNull()) {
timeSeconds = aOptions.mTimeSeconds.Value().Value();
}
uint16_t serviceClass = nsIMobileConnection::ICC_SERVICE_CLASS_NONE;
if (aOptions.mServiceClass.WasPassed() && !aOptions.mServiceClass.Value().IsNull()) {
serviceClass = aOptions.mServiceClass.Value().Value();
}
nsAutoString number;
if (aOptions.mNumber.WasPassed()) {
number = aOptions.mNumber.Value();
} else {
number.SetIsVoid(true);
}
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv = mMobileConnection->SetCallForwarding(aOptions.mAction.Value().Value(),
aOptions.mReason.Value().Value(),
number,
timeSeconds,
serviceClass,
requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::GetCallBarringOption(const MozCallBarringOptions& aOptions,
ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
if (!IsValidCallBarringOptions(aOptions, false)) {
nsresult rv = NotifyError(request, MOBILECONN_ERROR_INVALID_PARAMETER);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
// Fill in optional attributes.
nsAutoString password;
if (aOptions.mPassword.WasPassed()) {
password = aOptions.mPassword.Value();
} else {
password.SetIsVoid(true);
}
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv = mMobileConnection->GetCallBarring(aOptions.mProgram.Value().Value(),
password,
aOptions.mServiceClass.Value().Value(),
requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::SetCallBarringOption(const MozCallBarringOptions& aOptions,
ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
if (!IsValidCallBarringOptions(aOptions, true)) {
nsresult rv = NotifyError(request, MOBILECONN_ERROR_INVALID_PARAMETER);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv = mMobileConnection->SetCallBarring(aOptions.mProgram.Value().Value(),
aOptions.mEnabled.Value().Value(),
aOptions.mPassword.Value(),
aOptions.mServiceClass.Value().Value(),
requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::ChangeCallBarringPassword(const MozCallBarringOptions& aOptions,
ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
if (!aOptions.mPin.WasPassed() || aOptions.mPin.Value().IsVoid() ||
!aOptions.mNewPin.WasPassed() || aOptions.mNewPin.Value().IsVoid() ||
!IsValidPassword(aOptions.mPin.Value()) ||
!IsValidPassword(aOptions.mNewPin.Value())) {
nsresult rv = NotifyError(request, MOBILECONN_ERROR_INVALID_PASSWORD);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv =
mMobileConnection->ChangeCallBarringPassword(aOptions.mPin.Value(),
aOptions.mNewPin.Value(),
requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::GetCallWaitingOption(ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv = mMobileConnection->GetCallWaiting(requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::SetCallWaitingOption(bool aEnabled, ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv = mMobileConnection->SetCallWaiting(aEnabled,
nsIMobileConnection::ICC_SERVICE_CLASS_VOICE,
requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::GetCallingLineIdRestriction(ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv =
mMobileConnection->GetCallingLineIdRestriction(requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::SetCallingLineIdRestriction(uint16_t aMode,
ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv =
mMobileConnection->SetCallingLineIdRestriction(aMode, requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::ExitEmergencyCbMode(ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv = mMobileConnection->ExitEmergencyCbMode(requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
already_AddRefed<DOMRequest>
MobileConnection::SetRadioEnabled(bool aEnabled, ErrorResult& aRv)
{
if (!mMobileConnection) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<DOMRequest> request = new DOMRequest(GetOwner());
RefPtr<MobileConnectionCallback> requestCallback =
new MobileConnectionCallback(GetOwner(), request);
nsresult rv = mMobileConnection->SetRadioEnabled(aEnabled, requestCallback);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return request.forget();
}
// nsIMobileConnectionListener
NS_IMETHODIMP
MobileConnection::NotifyVoiceChanged()
{
if (!CheckPermission("mobileconnection")) {
return NS_OK;
}
UpdateVoice();
return DispatchTrustedEvent(NS_LITERAL_STRING("voicechange"));
}
NS_IMETHODIMP
MobileConnection::NotifyDataChanged()
{
if (!CheckPermission("mobileconnection")) {
return NS_OK;
}
UpdateData();
return DispatchTrustedEvent(NS_LITERAL_STRING("datachange"));
}
NS_IMETHODIMP
MobileConnection::NotifyDataError(const nsAString& aMessage)
{
if (!CheckPermission("mobileconnection")) {
return NS_OK;
}
DataErrorEventInit init;
init.mBubbles = false;
init.mCancelable = false;
init.mMessage = aMessage;
RefPtr<DataErrorEvent> event =
DataErrorEvent::Constructor(this, NS_LITERAL_STRING("dataerror"), init);
return DispatchTrustedEvent(event);
}
NS_IMETHODIMP
MobileConnection::NotifyCFStateChanged(unsigned short aAction,
unsigned short aReason,
const nsAString& aNumber,
unsigned short aSeconds,
unsigned short aServiceClass)
{
if (!CheckPermission("mobileconnection")) {
return NS_OK;
}
CFStateChangeEventInit init;
init.mBubbles = false;
init.mCancelable = false;
init.mAction = aAction;
init.mReason = aReason;
init.mNumber = aNumber;
init.mTimeSeconds = aSeconds;
init.mServiceClass = aServiceClass;
RefPtr<CFStateChangeEvent> event =
CFStateChangeEvent::Constructor(this, NS_LITERAL_STRING("cfstatechange"), init);
return DispatchTrustedEvent(event);
}
NS_IMETHODIMP
MobileConnection::NotifyEmergencyCbModeChanged(bool aActive,
uint32_t aTimeoutMs)
{
if (!CheckPermission("mobileconnection")) {
return NS_OK;
}
MozEmergencyCbModeEventInit init;
init.mBubbles = false;
init.mCancelable = false;
init.mActive = aActive;
init.mTimeoutMs = aTimeoutMs;
RefPtr<MozEmergencyCbModeEvent> event =
MozEmergencyCbModeEvent::Constructor(this, NS_LITERAL_STRING("emergencycbmodechange"), init);
return DispatchTrustedEvent(event);
}
NS_IMETHODIMP
MobileConnection::NotifyOtaStatusChanged(const nsAString& aStatus)
{
if (!CheckPermission("mobileconnection")) {
return NS_OK;
}
MozOtaStatusEventInit init;
init.mBubbles = false;
init.mCancelable = false;
init.mStatus = aStatus;
RefPtr<MozOtaStatusEvent> event =
MozOtaStatusEvent::Constructor(this, NS_LITERAL_STRING("otastatuschange"), init);
return DispatchTrustedEvent(event);
}
NS_IMETHODIMP
MobileConnection::NotifyRadioStateChanged()
{
if (!CheckPermission("mobileconnection")) {
return NS_OK;
}
return DispatchTrustedEvent(NS_LITERAL_STRING("radiostatechange"));
}
NS_IMETHODIMP
MobileConnection::NotifyClirModeChanged(uint32_t aMode)
{
if (!CheckPermission("mobileconnection")) {
return NS_OK;
}
MozClirModeEventInit init;
init.mBubbles = false;
init.mCancelable = false;
init.mMode = aMode;
RefPtr<MozClirModeEvent> event =
MozClirModeEvent::Constructor(this, NS_LITERAL_STRING("clirmodechange"), init);
return DispatchTrustedEvent(event);
}
NS_IMETHODIMP
MobileConnection::NotifyLastKnownNetworkChanged()
{
return NS_OK;
}
NS_IMETHODIMP
MobileConnection::NotifyLastKnownHomeNetworkChanged()
{
return NS_OK;
}
NS_IMETHODIMP
MobileConnection::NotifyNetworkSelectionModeChanged()
{
return NS_OK;
}
NS_IMETHODIMP
MobileConnection::NotifyDeviceIdentitiesChanged()
{
// To be supported when bug 1222870 is required in m-c.
return NS_OK;
}
// nsIIccListener
NS_IMETHODIMP
MobileConnection::NotifyStkCommand(nsIStkProactiveCmd *aStkProactiveCmd)
{
return NS_OK;
}
NS_IMETHODIMP
MobileConnection::NotifyStkSessionEnd()
{
return NS_OK;
}
NS_IMETHODIMP
MobileConnection::NotifyCardStateChanged()
{
return NS_OK;
}
NS_IMETHODIMP
MobileConnection::NotifyIccInfoChanged()
{
if (!CheckPermission("mobileconnection")) {
return NS_OK;
}
if (!UpdateIccId()) {
return NS_OK;
}
RefPtr<AsyncEventDispatcher> asyncDispatcher =
new AsyncEventDispatcher(this, NS_LITERAL_STRING("iccchange"), false);
return asyncDispatcher->PostDOMEvent();
}