2014-05-04 23:36:20 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2014-09-21 00:24:42 -07:00
|
|
|
#include "mozilla/dom/MobileConnectionInfo.h"
|
2014-05-04 23:36:20 -07:00
|
|
|
|
2014-08-14 18:47:15 -07:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
|
|
|
|
2014-05-04 23:36:20 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
|
2014-09-25 07:10:42 -07:00
|
|
|
#ifdef CONVERT_STRING_TO_NULLABLE_ENUM
|
|
|
|
#undef CONVERT_STRING_TO_NULLABLE_ENUM
|
|
|
|
#endif
|
2014-05-04 23:36:20 -07:00
|
|
|
#define CONVERT_STRING_TO_NULLABLE_ENUM(_string, _enumType, _enum) \
|
|
|
|
{ \
|
|
|
|
_enum.SetNull(); \
|
|
|
|
\
|
|
|
|
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)); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2014-03-08 08:10:47 -08:00
|
|
|
#define CONVERT_NULLABLE_ENUM_TO_STRING(_enumType, _enum, _string) \
|
|
|
|
{ \
|
|
|
|
if (_enum.IsNull()) { \
|
|
|
|
_string.SetIsVoid(true); \
|
|
|
|
} else { \
|
|
|
|
uint32_t index = uint32_t(_enum.Value()); \
|
|
|
|
_string.AssignASCII(_enumType##Values::strings[index].value, \
|
|
|
|
_enumType##Values::strings[index].length); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2014-05-04 23:36:20 -07:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MobileConnectionInfo, mWindow,
|
|
|
|
mNetworkInfo, mCellInfo)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(MobileConnectionInfo)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(MobileConnectionInfo)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MobileConnectionInfo)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2014-03-08 08:10:47 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMobileConnectionInfo)
|
2014-05-04 23:36:20 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
MobileConnectionInfo::MobileConnectionInfo(nsPIDOMWindow* aWindow)
|
|
|
|
: mConnected(false)
|
|
|
|
, mEmergencyCallsOnly(false)
|
|
|
|
, mRoaming(false)
|
|
|
|
, mWindow(aWindow)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-01 03:09:08 -07:00
|
|
|
MobileConnectionInfo::MobileConnectionInfo(const nsAString& aState,
|
|
|
|
bool aConnected,
|
|
|
|
bool aEmergencyCallsOnly,
|
|
|
|
bool aRoaming,
|
|
|
|
nsIMobileNetworkInfo* aNetworkInfo,
|
|
|
|
const nsAString& aType,
|
|
|
|
const Nullable<int32_t>& aSignalStrength,
|
|
|
|
const Nullable<uint16_t>& aRelSignalStrength,
|
|
|
|
nsIMobileCellInfo* aCellInfo)
|
|
|
|
: mConnected(aConnected)
|
|
|
|
, mEmergencyCallsOnly(aEmergencyCallsOnly)
|
|
|
|
, mRoaming(aRoaming)
|
|
|
|
, mSignalStrength(aSignalStrength)
|
|
|
|
, mRelSignalStrength(aRelSignalStrength)
|
|
|
|
{
|
|
|
|
// The instance created by this way is only used for IPC stuff. It won't be
|
2014-10-07 02:44:49 -07:00
|
|
|
// exposed to JS directly, we will clone this instance to the one that is
|
|
|
|
// maintained in MobileConnectionChild.
|
2014-07-01 03:09:08 -07:00
|
|
|
|
|
|
|
// Update mState and mType
|
|
|
|
CONVERT_STRING_TO_NULLABLE_ENUM(aState, MobileConnectionState, mState);
|
|
|
|
CONVERT_STRING_TO_NULLABLE_ENUM(aType, MobileConnectionType, mType);
|
|
|
|
|
|
|
|
// Update mNetworkInfo
|
|
|
|
if (aNetworkInfo) {
|
|
|
|
mNetworkInfo = new MobileNetworkInfo(mWindow);
|
|
|
|
mNetworkInfo->Update(aNetworkInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update mCellInfo
|
|
|
|
if (aCellInfo) {
|
|
|
|
mCellInfo = new MobileCellInfo(mWindow);
|
|
|
|
mCellInfo->Update(aCellInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-04 23:36:20 -07:00
|
|
|
void
|
|
|
|
MobileConnectionInfo::Update(nsIMobileConnectionInfo* aInfo)
|
|
|
|
{
|
|
|
|
if (!aInfo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aInfo->GetConnected(&mConnected);
|
|
|
|
aInfo->GetEmergencyCallsOnly(&mEmergencyCallsOnly);
|
|
|
|
aInfo->GetRoaming(&mRoaming);
|
|
|
|
|
|
|
|
// Update mState
|
|
|
|
nsAutoString state;
|
|
|
|
aInfo->GetState(state);
|
|
|
|
CONVERT_STRING_TO_NULLABLE_ENUM(state, MobileConnectionState, mState);
|
|
|
|
|
|
|
|
// Update mType
|
|
|
|
nsAutoString type;
|
|
|
|
aInfo->GetType(type);
|
|
|
|
CONVERT_STRING_TO_NULLABLE_ENUM(type, MobileConnectionType, mType);
|
|
|
|
|
|
|
|
// Update mSignalStrength
|
2014-09-23 19:04:40 -07:00
|
|
|
AutoJSContext cx;
|
2015-01-13 23:59:06 -08:00
|
|
|
JS::Rooted<JS::Value> signalStrength(cx, JS::UndefinedValue());
|
2014-05-04 23:36:20 -07:00
|
|
|
aInfo->GetSignalStrength(&signalStrength);
|
|
|
|
if (signalStrength.isNumber()) {
|
|
|
|
mSignalStrength.SetValue(signalStrength.toNumber());
|
|
|
|
} else {
|
|
|
|
mSignalStrength.SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update mRelSignalStrength
|
2015-01-13 23:59:06 -08:00
|
|
|
JS::Rooted<JS::Value> relSignalStrength(cx, JS::UndefinedValue());
|
2014-05-04 23:36:20 -07:00
|
|
|
aInfo->GetRelSignalStrength(&relSignalStrength);
|
|
|
|
if (relSignalStrength.isNumber()) {
|
|
|
|
mRelSignalStrength.SetValue(relSignalStrength.toNumber());
|
|
|
|
} else {
|
|
|
|
mRelSignalStrength.SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update mNetworkInfo
|
|
|
|
nsCOMPtr<nsIMobileNetworkInfo> networkInfo;
|
|
|
|
aInfo->GetNetwork(getter_AddRefs(networkInfo));
|
|
|
|
if (networkInfo) {
|
|
|
|
if (!mNetworkInfo) {
|
|
|
|
mNetworkInfo = new MobileNetworkInfo(mWindow);
|
|
|
|
}
|
|
|
|
mNetworkInfo->Update(networkInfo);
|
|
|
|
} else {
|
|
|
|
mNetworkInfo = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update mCellInfo
|
|
|
|
nsCOMPtr<nsIMobileCellInfo> cellInfo;
|
|
|
|
aInfo->GetCell(getter_AddRefs(cellInfo));
|
|
|
|
if (cellInfo) {
|
|
|
|
if (!mCellInfo) {
|
|
|
|
mCellInfo = new MobileCellInfo(mWindow);
|
|
|
|
}
|
|
|
|
mCellInfo->Update(cellInfo);
|
|
|
|
} else {
|
|
|
|
mCellInfo = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
MobileConnectionInfo::WrapObject(JSContext* aCx)
|
|
|
|
{
|
|
|
|
return MozMobileConnectionInfoBinding::Wrap(aCx, this);
|
|
|
|
}
|
2014-03-08 08:10:47 -08:00
|
|
|
|
|
|
|
// nsIMobileConnectionInfo
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileConnectionInfo::GetState(nsAString& aState)
|
|
|
|
{
|
|
|
|
CONVERT_NULLABLE_ENUM_TO_STRING(MobileConnectionState, mState, aState);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileConnectionInfo::GetConnected(bool* aConnected)
|
|
|
|
{
|
|
|
|
*aConnected = Connected();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileConnectionInfo::GetEmergencyCallsOnly(bool* aEmergencyCallsOnly)
|
|
|
|
{
|
|
|
|
*aEmergencyCallsOnly = EmergencyCallsOnly();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileConnectionInfo::GetRoaming(bool* aRoaming)
|
|
|
|
{
|
|
|
|
*aRoaming = Roaming();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileConnectionInfo::GetNetwork(nsIMobileNetworkInfo** aInfo)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aInfo = GetNetwork());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileConnectionInfo::GetType(nsAString& aType)
|
|
|
|
{
|
|
|
|
CONVERT_NULLABLE_ENUM_TO_STRING(MobileConnectionType, mType, aType);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileConnectionInfo::GetSignalStrength(JS::MutableHandle<JS::Value> aSignal)
|
|
|
|
{
|
|
|
|
if (mSignalStrength.IsNull()) {
|
|
|
|
aSignal.setNull();
|
|
|
|
} else {
|
|
|
|
aSignal.setInt32(mSignalStrength.Value());
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileConnectionInfo::GetRelSignalStrength(JS::MutableHandle<JS::Value> aSignal)
|
|
|
|
{
|
|
|
|
if (mRelSignalStrength.IsNull()) {
|
|
|
|
aSignal.setNull();
|
|
|
|
} else {
|
|
|
|
aSignal.setNumber(uint32_t(mRelSignalStrength.Value()));
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileConnectionInfo::GetCell(nsIMobileCellInfo** aInfo)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aInfo = GetCell());
|
|
|
|
return NS_OK;
|
|
|
|
}
|