dom/network/src/Connection.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/network/src/Connection.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,102 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include <limits>
    1.10 +#include "mozilla/Hal.h"
    1.11 +#include "mozilla/dom/network/Connection.h"
    1.12 +#include "nsIDOMClassInfo.h"
    1.13 +#include "mozilla/Preferences.h"
    1.14 +#include "Constants.h"
    1.15 +
    1.16 +/**
    1.17 + * We have to use macros here because our leak analysis tool things we are
    1.18 + * leaking strings when we have |static const nsString|. Sad :(
    1.19 + */
    1.20 +#define CHANGE_EVENT_NAME NS_LITERAL_STRING("typechange")
    1.21 +
    1.22 +namespace mozilla {
    1.23 +namespace dom {
    1.24 +namespace network {
    1.25 +
    1.26 +NS_IMPL_QUERY_INTERFACE_INHERITED(Connection, DOMEventTargetHelper,
    1.27 +                                  nsINetworkProperties)
    1.28 +
    1.29 +// Don't use |Connection| alone, since that confuses nsTraceRefcnt since
    1.30 +// we're not the only class with that name.
    1.31 +NS_IMPL_ADDREF_INHERITED(dom::network::Connection, DOMEventTargetHelper)
    1.32 +NS_IMPL_RELEASE_INHERITED(dom::network::Connection, DOMEventTargetHelper)
    1.33 +
    1.34 +Connection::Connection()
    1.35 +  : mType(static_cast<ConnectionType>(kDefaultType))
    1.36 +  , mIsWifi(kDefaultIsWifi)
    1.37 +  , mDHCPGateway(kDefaultDHCPGateway)
    1.38 +{
    1.39 +  SetIsDOMBinding();
    1.40 +}
    1.41 +
    1.42 +void
    1.43 +Connection::Init(nsPIDOMWindow* aWindow)
    1.44 +{
    1.45 +  BindToOwner(aWindow);
    1.46 +
    1.47 +  hal::RegisterNetworkObserver(this);
    1.48 +
    1.49 +  hal::NetworkInformation networkInfo;
    1.50 +  hal::GetCurrentNetworkInformation(&networkInfo);
    1.51 +
    1.52 +  UpdateFromNetworkInfo(networkInfo);
    1.53 +}
    1.54 +
    1.55 +void
    1.56 +Connection::Shutdown()
    1.57 +{
    1.58 +  hal::UnregisterNetworkObserver(this);
    1.59 +}
    1.60 +
    1.61 +NS_IMETHODIMP
    1.62 +Connection::GetIsWifi(bool *aIsWifi)
    1.63 +{
    1.64 +  *aIsWifi = mIsWifi;
    1.65 +  return NS_OK;
    1.66 +}
    1.67 +
    1.68 +NS_IMETHODIMP
    1.69 +Connection::GetDhcpGateway(uint32_t *aGW)
    1.70 +{
    1.71 +  *aGW = mDHCPGateway;
    1.72 +  return NS_OK;
    1.73 +}
    1.74 +
    1.75 +void
    1.76 +Connection::UpdateFromNetworkInfo(const hal::NetworkInformation& aNetworkInfo)
    1.77 +{
    1.78 +  mType = static_cast<ConnectionType>(aNetworkInfo.type());
    1.79 +  mIsWifi = aNetworkInfo.isWifi();
    1.80 +  mDHCPGateway = aNetworkInfo.dhcpGateway();
    1.81 +}
    1.82 +
    1.83 +void
    1.84 +Connection::Notify(const hal::NetworkInformation& aNetworkInfo)
    1.85 +{
    1.86 +  ConnectionType previousType = mType;
    1.87 +
    1.88 +  UpdateFromNetworkInfo(aNetworkInfo);
    1.89 +
    1.90 +  if (previousType == mType) {
    1.91 +    return;
    1.92 +  }
    1.93 +
    1.94 +  DispatchTrustedEvent(CHANGE_EVENT_NAME);
    1.95 +}
    1.96 +
    1.97 +JSObject*
    1.98 +Connection::WrapObject(JSContext* aCx)
    1.99 +{
   1.100 +  return NetworkInformationBinding::Wrap(aCx, this);
   1.101 +}
   1.102 +
   1.103 +} // namespace network
   1.104 +} // namespace dom
   1.105 +} // namespace mozilla

mercurial