dom/network/src/Connection.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:63cfb7a81882
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/. */
5
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"
12
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 :(
16 */
17 #define CHANGE_EVENT_NAME NS_LITERAL_STRING("typechange")
18
19 namespace mozilla {
20 namespace dom {
21 namespace network {
22
23 NS_IMPL_QUERY_INTERFACE_INHERITED(Connection, DOMEventTargetHelper,
24 nsINetworkProperties)
25
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)
30
31 Connection::Connection()
32 : mType(static_cast<ConnectionType>(kDefaultType))
33 , mIsWifi(kDefaultIsWifi)
34 , mDHCPGateway(kDefaultDHCPGateway)
35 {
36 SetIsDOMBinding();
37 }
38
39 void
40 Connection::Init(nsPIDOMWindow* aWindow)
41 {
42 BindToOwner(aWindow);
43
44 hal::RegisterNetworkObserver(this);
45
46 hal::NetworkInformation networkInfo;
47 hal::GetCurrentNetworkInformation(&networkInfo);
48
49 UpdateFromNetworkInfo(networkInfo);
50 }
51
52 void
53 Connection::Shutdown()
54 {
55 hal::UnregisterNetworkObserver(this);
56 }
57
58 NS_IMETHODIMP
59 Connection::GetIsWifi(bool *aIsWifi)
60 {
61 *aIsWifi = mIsWifi;
62 return NS_OK;
63 }
64
65 NS_IMETHODIMP
66 Connection::GetDhcpGateway(uint32_t *aGW)
67 {
68 *aGW = mDHCPGateway;
69 return NS_OK;
70 }
71
72 void
73 Connection::UpdateFromNetworkInfo(const hal::NetworkInformation& aNetworkInfo)
74 {
75 mType = static_cast<ConnectionType>(aNetworkInfo.type());
76 mIsWifi = aNetworkInfo.isWifi();
77 mDHCPGateway = aNetworkInfo.dhcpGateway();
78 }
79
80 void
81 Connection::Notify(const hal::NetworkInformation& aNetworkInfo)
82 {
83 ConnectionType previousType = mType;
84
85 UpdateFromNetworkInfo(aNetworkInfo);
86
87 if (previousType == mType) {
88 return;
89 }
90
91 DispatchTrustedEvent(CHANGE_EVENT_NAME);
92 }
93
94 JSObject*
95 Connection::WrapObject(JSContext* aCx)
96 {
97 return NetworkInformationBinding::Wrap(aCx, this);
98 }
99
100 } // namespace network
101 } // namespace dom
102 } // namespace mozilla

mercurial