michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include "mozilla/Hal.h" michael@0: #include "mozilla/dom/network/Connection.h" michael@0: #include "nsIDOMClassInfo.h" michael@0: #include "mozilla/Preferences.h" michael@0: #include "Constants.h" michael@0: michael@0: /** michael@0: * We have to use macros here because our leak analysis tool things we are michael@0: * leaking strings when we have |static const nsString|. Sad :( michael@0: */ michael@0: #define CHANGE_EVENT_NAME NS_LITERAL_STRING("typechange") michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: namespace network { michael@0: michael@0: NS_IMPL_QUERY_INTERFACE_INHERITED(Connection, DOMEventTargetHelper, michael@0: nsINetworkProperties) michael@0: michael@0: // Don't use |Connection| alone, since that confuses nsTraceRefcnt since michael@0: // we're not the only class with that name. michael@0: NS_IMPL_ADDREF_INHERITED(dom::network::Connection, DOMEventTargetHelper) michael@0: NS_IMPL_RELEASE_INHERITED(dom::network::Connection, DOMEventTargetHelper) michael@0: michael@0: Connection::Connection() michael@0: : mType(static_cast(kDefaultType)) michael@0: , mIsWifi(kDefaultIsWifi) michael@0: , mDHCPGateway(kDefaultDHCPGateway) michael@0: { michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: void michael@0: Connection::Init(nsPIDOMWindow* aWindow) michael@0: { michael@0: BindToOwner(aWindow); michael@0: michael@0: hal::RegisterNetworkObserver(this); michael@0: michael@0: hal::NetworkInformation networkInfo; michael@0: hal::GetCurrentNetworkInformation(&networkInfo); michael@0: michael@0: UpdateFromNetworkInfo(networkInfo); michael@0: } michael@0: michael@0: void michael@0: Connection::Shutdown() michael@0: { michael@0: hal::UnregisterNetworkObserver(this); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: Connection::GetIsWifi(bool *aIsWifi) michael@0: { michael@0: *aIsWifi = mIsWifi; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: Connection::GetDhcpGateway(uint32_t *aGW) michael@0: { michael@0: *aGW = mDHCPGateway; michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: Connection::UpdateFromNetworkInfo(const hal::NetworkInformation& aNetworkInfo) michael@0: { michael@0: mType = static_cast(aNetworkInfo.type()); michael@0: mIsWifi = aNetworkInfo.isWifi(); michael@0: mDHCPGateway = aNetworkInfo.dhcpGateway(); michael@0: } michael@0: michael@0: void michael@0: Connection::Notify(const hal::NetworkInformation& aNetworkInfo) michael@0: { michael@0: ConnectionType previousType = mType; michael@0: michael@0: UpdateFromNetworkInfo(aNetworkInfo); michael@0: michael@0: if (previousType == mType) { michael@0: return; michael@0: } michael@0: michael@0: DispatchTrustedEvent(CHANGE_EVENT_NAME); michael@0: } michael@0: michael@0: JSObject* michael@0: Connection::WrapObject(JSContext* aCx) michael@0: { michael@0: return NetworkInformationBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: } // namespace network michael@0: } // namespace dom michael@0: } // namespace mozilla