1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include <limits>
39 : #include "mozilla/Hal.h"
40 : #include "Connection.h"
41 : #include "nsIDOMClassInfo.h"
42 : #include "mozilla/Preferences.h"
43 : #include "nsDOMEvent.h"
44 : #include "Constants.h"
45 :
46 : /**
47 : * We have to use macros here because our leak analysis tool things we are
48 : * leaking strings when we have |static const nsString|. Sad :(
49 : */
50 : #define CHANGE_EVENT_NAME NS_LITERAL_STRING("change")
51 :
52 : DOMCI_DATA(MozConnection, mozilla::dom::network::Connection)
53 :
54 : namespace mozilla {
55 : namespace dom {
56 : namespace network {
57 :
58 : const char* Connection::sMeteredPrefName = "dom.network.metered";
59 : const bool Connection::sMeteredDefaultValue = false;
60 :
61 1464 : NS_IMPL_CYCLE_COLLECTION_CLASS(Connection)
62 :
63 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(Connection,
64 : nsDOMEventTargetHelper)
65 0 : NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(change)
66 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
67 :
68 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(Connection,
69 : nsDOMEventTargetHelper)
70 0 : NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(change)
71 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
72 :
73 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(Connection)
74 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMMozConnection)
75 0 : NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMMozConnection)
76 0 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozConnection)
77 0 : NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
78 :
79 0 : NS_IMPL_ADDREF_INHERITED(Connection, nsDOMEventTargetHelper)
80 0 : NS_IMPL_RELEASE_INHERITED(Connection, nsDOMEventTargetHelper)
81 :
82 0 : Connection::Connection()
83 : : mCanBeMetered(kDefaultCanBeMetered)
84 0 : , mBandwidth(kDefaultBandwidth)
85 : {
86 0 : }
87 :
88 : void
89 0 : Connection::Init(nsPIDOMWindow* aWindow)
90 : {
91 0 : BindToOwner(aWindow);
92 :
93 0 : hal::RegisterNetworkObserver(this);
94 :
95 0 : hal::NetworkInformation networkInfo;
96 0 : hal::GetCurrentNetworkInformation(&networkInfo);
97 :
98 0 : UpdateFromNetworkInfo(networkInfo);
99 0 : }
100 :
101 : void
102 0 : Connection::Shutdown()
103 : {
104 0 : hal::UnregisterNetworkObserver(this);
105 0 : }
106 :
107 : NS_IMETHODIMP
108 0 : Connection::GetBandwidth(double* aBandwidth)
109 : {
110 0 : if (mBandwidth == kDefaultBandwidth) {
111 0 : *aBandwidth = std::numeric_limits<double>::infinity();
112 0 : return NS_OK;
113 : }
114 :
115 0 : *aBandwidth = mBandwidth;
116 0 : return NS_OK;
117 : }
118 :
119 : NS_IMETHODIMP
120 0 : Connection::GetMetered(bool* aMetered)
121 : {
122 0 : if (!mCanBeMetered) {
123 0 : *aMetered = false;
124 0 : return NS_OK;
125 : }
126 :
127 : *aMetered = Preferences::GetBool(sMeteredPrefName,
128 0 : sMeteredDefaultValue);
129 0 : return NS_OK;
130 : }
131 :
132 0 : NS_IMPL_EVENT_HANDLER(Connection, change)
133 :
134 : nsresult
135 0 : Connection::DispatchTrustedEventToSelf(const nsAString& aEventName)
136 : {
137 0 : nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nsnull, nsnull);
138 0 : nsresult rv = event->InitEvent(aEventName, false, false);
139 0 : NS_ENSURE_SUCCESS(rv, rv);
140 :
141 0 : rv = event->SetTrusted(PR_TRUE);
142 0 : NS_ENSURE_SUCCESS(rv, rv);
143 :
144 : bool dummy;
145 0 : rv = DispatchEvent(event, &dummy);
146 0 : NS_ENSURE_SUCCESS(rv, rv);
147 :
148 0 : return NS_OK;
149 : }
150 :
151 : void
152 0 : Connection::UpdateFromNetworkInfo(const hal::NetworkInformation& aNetworkInfo)
153 : {
154 0 : mBandwidth = aNetworkInfo.bandwidth();
155 0 : mCanBeMetered = aNetworkInfo.canBeMetered();
156 0 : }
157 :
158 : void
159 0 : Connection::Notify(const hal::NetworkInformation& aNetworkInfo)
160 : {
161 0 : double previousBandwidth = mBandwidth;
162 0 : bool previousCanBeMetered = mCanBeMetered;
163 :
164 0 : UpdateFromNetworkInfo(aNetworkInfo);
165 :
166 0 : if (previousBandwidth == mBandwidth &&
167 : previousCanBeMetered == mCanBeMetered) {
168 0 : return;
169 : }
170 :
171 0 : DispatchTrustedEventToSelf(CHANGE_EVENT_NAME);
172 : }
173 :
174 : } // namespace network
175 : } // namespace dom
176 4392 : } // namespace mozilla
177 :
|