Bumping manifests a=b2g-bump
[gecko.git] / dom / network / Connection.cpp
blob744912296751b8ebaee13204d803fb7c18a4b593
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include <limits>
7 #include "mozilla/Hal.h"
8 #include "mozilla/dom/network/Connection.h"
9 #include "nsIDOMClassInfo.h"
10 #include "mozilla/Preferences.h"
11 #include "Constants.h"
13 /**
14 * We have to use macros here because our leak analysis tool things we are
15 * leaking strings when we have |static const nsString|. Sad :(
17 #define CHANGE_EVENT_NAME NS_LITERAL_STRING("typechange")
19 namespace mozilla {
20 namespace dom {
21 namespace network {
23 NS_IMPL_QUERY_INTERFACE_INHERITED(Connection, DOMEventTargetHelper,
24 nsINetworkProperties)
26 // Don't use |Connection| alone, since that confuses nsTraceRefcnt since
27 // we're not the only class with that name.
28 NS_IMPL_ADDREF_INHERITED(dom::network::Connection, DOMEventTargetHelper)
29 NS_IMPL_RELEASE_INHERITED(dom::network::Connection, DOMEventTargetHelper)
31 Connection::Connection(nsPIDOMWindow* aWindow)
32 : DOMEventTargetHelper(aWindow)
33 , mType(static_cast<ConnectionType>(kDefaultType))
34 , mIsWifi(kDefaultIsWifi)
35 , mDHCPGateway(kDefaultDHCPGateway)
37 hal::RegisterNetworkObserver(this);
39 hal::NetworkInformation networkInfo;
40 hal::GetCurrentNetworkInformation(&networkInfo);
42 UpdateFromNetworkInfo(networkInfo);
45 void
46 Connection::Shutdown()
48 hal::UnregisterNetworkObserver(this);
51 NS_IMETHODIMP
52 Connection::GetIsWifi(bool *aIsWifi)
54 *aIsWifi = mIsWifi;
55 return NS_OK;
58 NS_IMETHODIMP
59 Connection::GetDhcpGateway(uint32_t *aGW)
61 *aGW = mDHCPGateway;
62 return NS_OK;
65 void
66 Connection::UpdateFromNetworkInfo(const hal::NetworkInformation& aNetworkInfo)
68 mType = static_cast<ConnectionType>(aNetworkInfo.type());
69 mIsWifi = aNetworkInfo.isWifi();
70 mDHCPGateway = aNetworkInfo.dhcpGateway();
73 void
74 Connection::Notify(const hal::NetworkInformation& aNetworkInfo)
76 ConnectionType previousType = mType;
78 UpdateFromNetworkInfo(aNetworkInfo);
80 if (previousType == mType) {
81 return;
84 DispatchTrustedEvent(CHANGE_EVENT_NAME);
87 JSObject*
88 Connection::WrapObject(JSContext* aCx)
90 return NetworkInformationBinding::Wrap(aCx, this);
93 } // namespace network
94 } // namespace dom
95 } // namespace mozilla