Source code

Revision control

Other Tools

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 "Tickler.h"
7
8
#ifdef MOZ_USE_WIFI_TICKLER
9
# include "nsComponentManagerUtils.h"
10
# include "nsINamed.h"
11
# include "nsIPrefBranch.h"
12
# include "nsIPrefService.h"
13
# include "nsServiceManagerUtils.h"
14
# include "nsThreadUtils.h"
15
# include "prnetdb.h"
16
17
# include "mozilla/jni/Utils.h"
18
# include "GeneratedJNIWrappers.h"
19
20
namespace mozilla {
21
namespace net {
22
23
NS_IMPL_ISUPPORTS(Tickler, nsISupportsWeakReference, Tickler)
24
25
Tickler::Tickler()
26
: mLock("Tickler::mLock"),
27
mActive(false),
28
mCanceled(false),
29
mEnabled(false),
30
mDelay(16),
31
mDuration(TimeDuration::FromMilliseconds(400)),
32
mFD(nullptr) {
33
MOZ_ASSERT(NS_IsMainThread());
34
}
35
36
Tickler::~Tickler() {
37
// non main thread uses of the tickler should hold weak
38
// references to it if they must hold a reference at all
39
MOZ_ASSERT(NS_IsMainThread());
40
41
if (mThread) {
42
mThread->AsyncShutdown();
43
mThread = nullptr;
44
}
45
46
if (mTimer) mTimer->Cancel();
47
if (mFD) PR_Close(mFD);
48
}
49
50
nsresult Tickler::Init() {
51
MOZ_ASSERT(NS_IsMainThread());
52
MOZ_ASSERT(!mTimer);
53
MOZ_ASSERT(!mActive);
54
MOZ_ASSERT(!mThread);
55
MOZ_ASSERT(!mFD);
56
57
if (jni::IsAvailable()) {
58
java::GeckoAppShell::EnableNetworkNotifications();
59
}
60
61
mFD = PR_OpenUDPSocket(PR_AF_INET);
62
if (!mFD) return NS_ERROR_FAILURE;
63
64
// make sure new socket has a ttl of 1
65
// failure is not fatal.
66
PRSocketOptionData opt;
67
opt.option = PR_SockOpt_IpTimeToLive;
68
opt.value.ip_ttl = 1;
69
PR_SetSocketOption(mFD, &opt);
70
71
nsresult rv = NS_NewNamedThread("wifi tickler", getter_AddRefs(mThread));
72
if (NS_FAILED(rv)) return rv;
73
74
nsCOMPtr<nsITimer> tmpTimer = NS_NewTimer(mThread);
75
if (!tmpTimer) return NS_ERROR_OUT_OF_MEMORY;
76
77
mTimer.swap(tmpTimer);
78
79
mAddr.inet.family = PR_AF_INET;
80
mAddr.inet.port = PR_htons(4886);
81
mAddr.inet.ip = 0;
82
83
return NS_OK;
84
}
85
86
void Tickler::Tickle() {
87
MutexAutoLock lock(mLock);
88
MOZ_ASSERT(mThread);
89
mLastTickle = TimeStamp::Now();
90
if (!mActive) MaybeStartTickler();
91
}
92
93
void Tickler::PostCheckTickler() {
94
mLock.AssertCurrentThreadOwns();
95
mThread->Dispatch(NewRunnableMethod("net::Tickler::CheckTickler", this,
96
&Tickler::CheckTickler),
97
NS_DISPATCH_NORMAL);
98
return;
99
}
100
101
void Tickler::MaybeStartTicklerUnlocked() {
102
MutexAutoLock lock(mLock);
103
MaybeStartTickler();
104
}
105
106
void Tickler::MaybeStartTickler() {
107
mLock.AssertCurrentThreadOwns();
108
if (!NS_IsMainThread()) {
109
NS_DispatchToMainThread(
110
NewRunnableMethod("net::Tickler::MaybeStartTicklerUnlocked", this,
111
&Tickler::MaybeStartTicklerUnlocked));
112
return;
113
}
114
115
if (!mPrefs) mPrefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
116
if (mPrefs) {
117
int32_t val;
118
bool boolVal;
119
120
if (NS_SUCCEEDED(
121
mPrefs->GetBoolPref("network.tickle-wifi.enabled", &boolVal)))
122
mEnabled = boolVal;
123
124
if (NS_SUCCEEDED(
125
mPrefs->GetIntPref("network.tickle-wifi.duration", &val))) {
126
if (val < 1) val = 1;
127
if (val > 100000) val = 100000;
128
mDuration = TimeDuration::FromMilliseconds(val);
129
}
130
131
if (NS_SUCCEEDED(mPrefs->GetIntPref("network.tickle-wifi.delay", &val))) {
132
if (val < 1) val = 1;
133
if (val > 1000) val = 1000;
134
mDelay = static_cast<uint32_t>(val);
135
}
136
}
137
138
PostCheckTickler();
139
}
140
141
void Tickler::CheckTickler() {
142
MutexAutoLock lock(mLock);
143
MOZ_ASSERT(mThread == NS_GetCurrentThread());
144
145
bool shouldRun =
146
(!mCanceled) && ((TimeStamp::Now() - mLastTickle) <= mDuration);
147
148
if ((shouldRun && mActive) || (!shouldRun && !mActive))
149
return; // no change in state
150
151
if (mActive)
152
StopTickler();
153
else
154
StartTickler();
155
}
156
157
void Tickler::Cancel() {
158
MutexAutoLock lock(mLock);
159
MOZ_ASSERT(NS_IsMainThread());
160
mCanceled = true;
161
if (mThread) PostCheckTickler();
162
}
163
164
void Tickler::StopTickler() {
165
mLock.AssertCurrentThreadOwns();
166
MOZ_ASSERT(mThread == NS_GetCurrentThread());
167
MOZ_ASSERT(mTimer);
168
MOZ_ASSERT(mActive);
169
170
mTimer->Cancel();
171
mActive = false;
172
}
173
174
class TicklerTimer final : public nsITimerCallback, public nsINamed {
175
NS_DECL_THREADSAFE_ISUPPORTS
176
NS_DECL_NSITIMERCALLBACK
177
178
explicit TicklerTimer(Tickler* aTickler) {
179
mTickler = do_GetWeakReference(aTickler);
180
}
181
182
// nsINamed
183
NS_IMETHOD GetName(nsACString& aName) override {
184
aName.AssignLiteral("TicklerTimer");
185
return NS_OK;
186
}
187
188
private:
189
~TicklerTimer() {}
190
191
nsWeakPtr mTickler;
192
};
193
194
void Tickler::StartTickler() {
195
mLock.AssertCurrentThreadOwns();
196
MOZ_ASSERT(mThread == NS_GetCurrentThread());
197
MOZ_ASSERT(!mActive);
198
MOZ_ASSERT(mTimer);
199
200
if (NS_SUCCEEDED(mTimer->InitWithCallback(new TicklerTimer(this),
201
mEnabled ? mDelay : 1000,
202
nsITimer::TYPE_REPEATING_SLACK)))
203
mActive = true;
204
}
205
206
// argument should be in network byte order
207
void Tickler::SetIPV4Address(uint32_t address) { mAddr.inet.ip = address; }
208
209
// argument should be in network byte order
210
void Tickler::SetIPV4Port(uint16_t port) { mAddr.inet.port = port; }
211
212
NS_IMPL_ISUPPORTS(TicklerTimer, nsITimerCallback, nsINamed)
213
214
NS_IMETHODIMP TicklerTimer::Notify(nsITimer* timer) {
215
RefPtr<Tickler> tickler = do_QueryReferent(mTickler);
216
if (!tickler) return NS_ERROR_FAILURE;
217
MutexAutoLock lock(tickler->mLock);
218
219
if (!tickler->mFD) {
220
tickler->StopTickler();
221
return NS_ERROR_FAILURE;
222
}
223
224
if (tickler->mCanceled ||
225
((TimeStamp::Now() - tickler->mLastTickle) > tickler->mDuration)) {
226
tickler->StopTickler();
227
return NS_OK;
228
}
229
230
if (!tickler->mEnabled) return NS_OK;
231
232
PR_SendTo(tickler->mFD, "", 0, 0, &tickler->mAddr, 0);
233
return NS_OK;
234
}
235
236
} // namespace net
237
} // namespace mozilla
238
239
#else // not defined MOZ_USE_WIFI_TICKLER
240
241
namespace mozilla {
242
namespace net {
243
NS_IMPL_ISUPPORTS0(Tickler)
244
} // namespace net
245
} // namespace mozilla
246
247
#endif // defined MOZ_USE_WIFI_TICKLER