2012-01-16 04:23:44 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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-16 04:23:44 -08:00
|
|
|
|
2012-01-16 09:14:56 -08:00
|
|
|
#include <limits>
|
|
|
|
#include "mozilla/Hal.h"
|
2012-01-16 04:23:44 -08:00
|
|
|
#include "Connection.h"
|
|
|
|
#include "nsIDOMClassInfo.h"
|
2012-01-16 04:25:13 -08:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-01-16 09:14:56 -08:00
|
|
|
#include "nsDOMEvent.h"
|
|
|
|
#include "Constants.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We have to use macros here because our leak analysis tool things we are
|
|
|
|
* leaking strings when we have |static const nsString|. Sad :(
|
|
|
|
*/
|
|
|
|
#define CHANGE_EVENT_NAME NS_LITERAL_STRING("change")
|
2012-01-16 04:23:44 -08:00
|
|
|
|
|
|
|
DOMCI_DATA(MozConnection, mozilla::dom::network::Connection)
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace network {
|
|
|
|
|
2012-01-16 04:25:13 -08:00
|
|
|
const char* Connection::sMeteredPrefName = "dom.network.metered";
|
|
|
|
const bool Connection::sMeteredDefaultValue = false;
|
|
|
|
|
2012-01-16 09:14:56 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Connection)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(Connection,
|
2012-02-07 18:53:33 -08:00
|
|
|
nsDOMEventTargetHelper)
|
2012-01-16 09:14:56 -08:00
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(change)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(Connection,
|
2012-02-07 18:53:33 -08:00
|
|
|
nsDOMEventTargetHelper)
|
2012-01-16 09:14:56 -08:00
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(change)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(Connection)
|
2012-01-16 04:23:44 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMMozConnection)
|
2012-01-16 09:14:56 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMMozConnection)
|
2012-01-16 04:23:44 -08:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozConnection)
|
2012-02-07 18:53:33 -08:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
2012-01-16 09:14:56 -08:00
|
|
|
|
2012-02-07 18:53:33 -08:00
|
|
|
NS_IMPL_ADDREF_INHERITED(Connection, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(Connection, nsDOMEventTargetHelper)
|
2012-01-16 09:14:56 -08:00
|
|
|
|
|
|
|
Connection::Connection()
|
|
|
|
: mCanBeMetered(kDefaultCanBeMetered)
|
|
|
|
, mBandwidth(kDefaultBandwidth)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-12 17:56:07 -07:00
|
|
|
Connection::Init(nsPIDOMWindow* aWindow)
|
2012-01-16 09:14:56 -08:00
|
|
|
{
|
2012-03-12 17:56:07 -07:00
|
|
|
BindToOwner(aWindow);
|
2012-01-16 09:14:56 -08:00
|
|
|
|
|
|
|
hal::RegisterNetworkObserver(this);
|
|
|
|
|
|
|
|
hal::NetworkInformation networkInfo;
|
|
|
|
hal::GetCurrentNetworkInformation(&networkInfo);
|
|
|
|
|
|
|
|
UpdateFromNetworkInfo(networkInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Connection::Shutdown()
|
|
|
|
{
|
|
|
|
hal::UnregisterNetworkObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Connection::GetBandwidth(double* aBandwidth)
|
|
|
|
{
|
|
|
|
if (mBandwidth == kDefaultBandwidth) {
|
|
|
|
*aBandwidth = std::numeric_limits<double>::infinity();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-01-16 04:23:44 -08:00
|
|
|
|
2012-01-16 09:14:56 -08:00
|
|
|
*aBandwidth = mBandwidth;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-01-16 04:23:44 -08:00
|
|
|
|
2012-01-16 04:25:13 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Connection::GetMetered(bool* aMetered)
|
|
|
|
{
|
2012-01-16 09:14:56 -08:00
|
|
|
if (!mCanBeMetered) {
|
|
|
|
*aMetered = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-16 04:25:13 -08:00
|
|
|
*aMetered = Preferences::GetBool(sMeteredPrefName,
|
|
|
|
sMeteredDefaultValue);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-16 09:14:56 -08:00
|
|
|
NS_IMPL_EVENT_HANDLER(Connection, change)
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Connection::DispatchTrustedEventToSelf(const nsAString& aEventName)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nullptr, nullptr);
|
2012-01-16 09:14:56 -08:00
|
|
|
nsresult rv = event->InitEvent(aEventName, false, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-04-04 02:15:10 -07:00
|
|
|
rv = event->SetTrusted(true);
|
2012-01-16 09:14:56 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool dummy;
|
|
|
|
rv = DispatchEvent(event, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Connection::UpdateFromNetworkInfo(const hal::NetworkInformation& aNetworkInfo)
|
|
|
|
{
|
|
|
|
mBandwidth = aNetworkInfo.bandwidth();
|
|
|
|
mCanBeMetered = aNetworkInfo.canBeMetered();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Connection::Notify(const hal::NetworkInformation& aNetworkInfo)
|
|
|
|
{
|
|
|
|
double previousBandwidth = mBandwidth;
|
|
|
|
bool previousCanBeMetered = mCanBeMetered;
|
|
|
|
|
|
|
|
UpdateFromNetworkInfo(aNetworkInfo);
|
|
|
|
|
|
|
|
if (previousBandwidth == mBandwidth &&
|
|
|
|
previousCanBeMetered == mCanBeMetered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchTrustedEventToSelf(CHANGE_EVENT_NAME);
|
|
|
|
}
|
|
|
|
|
2012-01-16 04:23:44 -08:00
|
|
|
} // namespace network
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|