Backed out changeset 12313cce9281 (bug 1083449) for suspicion of causing B2G debug...
[gecko.git] / dom / network / Connection.cpp
blobcde8f36ac04c6001490d62d5b0df30dd7fe6302a
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()
32 : mType(static_cast<ConnectionType>(kDefaultType))
33 , mIsWifi(kDefaultIsWifi)
34 , mDHCPGateway(kDefaultDHCPGateway)
36 SetIsDOMBinding();
39 void
40 Connection::Init(nsPIDOMWindow* aWindow)
42 BindToOwner(aWindow);
44 hal::RegisterNetworkObserver(this);
46 hal::NetworkInformation networkInfo;
47 hal::GetCurrentNetworkInformation(&networkInfo);
49 UpdateFromNetworkInfo(networkInfo);
52 void
53 Connection::Shutdown()
55 hal::UnregisterNetworkObserver(this);
58 NS_IMETHODIMP
59 Connection::GetIsWifi(bool *aIsWifi)
61 *aIsWifi = mIsWifi;
62 return NS_OK;
65 NS_IMETHODIMP
66 Connection::GetDhcpGateway(uint32_t *aGW)
68 *aGW = mDHCPGateway;
69 return NS_OK;
72 void
73 Connection::UpdateFromNetworkInfo(const hal::NetworkInformation& aNetworkInfo)
75 mType = static_cast<ConnectionType>(aNetworkInfo.type());
76 mIsWifi = aNetworkInfo.isWifi();
77 mDHCPGateway = aNetworkInfo.dhcpGateway();
80 void
81 Connection::Notify(const hal::NetworkInformation& aNetworkInfo)
83 ConnectionType previousType = mType;
85 UpdateFromNetworkInfo(aNetworkInfo);
87 if (previousType == mType) {
88 return;
91 DispatchTrustedEvent(CHANGE_EVENT_NAME);
94 JSObject*
95 Connection::WrapObject(JSContext* aCx)
97 return NetworkInformationBinding::Wrap(aCx, this);
100 } // namespace network
101 } // namespace dom
102 } // namespace mozilla