2013-07-11 19:39:36 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-09-22 07:04:57 +04:00
|
|
|
#include "Tickler.h"
|
|
|
|
|
|
|
|
#ifdef MOZ_USE_WIFI_TICKLER
|
2013-07-11 19:39:36 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-09-19 17:54:39 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2013-09-22 07:04:57 +04:00
|
|
|
#include "prnetdb.h"
|
2013-07-11 19:39:36 +04:00
|
|
|
|
2015-08-13 07:53:39 +03:00
|
|
|
#include "mozilla/jni/Utils.h"
|
|
|
|
#include "GeneratedJNIWrappers.h"
|
2013-07-11 19:39:36 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(Tickler, nsISupportsWeakReference, Tickler)
|
2014-01-02 21:55:12 +04:00
|
|
|
|
2013-07-11 19:39:36 +04:00
|
|
|
Tickler::Tickler()
|
|
|
|
: mLock("Tickler::mLock")
|
|
|
|
, mActive(false)
|
|
|
|
, mCanceled(false)
|
|
|
|
, mEnabled(false)
|
|
|
|
, mDelay(16)
|
|
|
|
, mDuration(TimeDuration::FromMilliseconds(400))
|
|
|
|
, mFD(nullptr)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
2013-08-21 00:49:22 +04:00
|
|
|
class TicklerThreadDestructor : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit TicklerThreadDestructor(nsIThread *aThread)
|
|
|
|
: mThread(aThread) { }
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override
|
2013-08-21 00:49:22 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mThread)
|
|
|
|
mThread->Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~TicklerThreadDestructor() { }
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
};
|
|
|
|
|
2013-07-11 19:39:36 +04:00
|
|
|
Tickler::~Tickler()
|
|
|
|
{
|
|
|
|
// non main thread uses of the tickler should hold weak
|
|
|
|
// references to it if they must hold a reference at all
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-08-21 00:49:22 +04:00
|
|
|
// Shutting down a thread can spin the event loop - which is a surprising
|
|
|
|
// thing to do from a dtor. Running it on its own event is safer.
|
2015-03-17 19:29:17 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event = new TicklerThreadDestructor(mThread);
|
2013-08-21 00:49:22 +04:00
|
|
|
if (NS_FAILED(NS_DispatchToCurrentThread(event))) {
|
2013-07-11 19:39:36 +04:00
|
|
|
mThread->Shutdown();
|
2013-08-21 00:49:22 +04:00
|
|
|
}
|
|
|
|
mThread = nullptr;
|
|
|
|
|
2013-07-11 19:39:36 +04:00
|
|
|
if (mTimer)
|
|
|
|
mTimer->Cancel();
|
|
|
|
if (mFD)
|
|
|
|
PR_Close(mFD);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Tickler::Init()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mTimer);
|
|
|
|
MOZ_ASSERT(!mActive);
|
|
|
|
MOZ_ASSERT(!mThread);
|
|
|
|
MOZ_ASSERT(!mFD);
|
|
|
|
|
2015-08-13 07:53:39 +03:00
|
|
|
if (jni::IsAvailable()) {
|
2015-01-10 03:33:57 +03:00
|
|
|
widget::GeckoAppShell::EnableNetworkNotifications();
|
2014-01-18 09:32:24 +04:00
|
|
|
}
|
2013-07-11 19:39:36 +04:00
|
|
|
|
|
|
|
mFD = PR_OpenUDPSocket(PR_AF_INET);
|
|
|
|
if (!mFD)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// make sure new socket has a ttl of 1
|
|
|
|
// failure is not fatal.
|
|
|
|
PRSocketOptionData opt;
|
|
|
|
opt.option = PR_SockOpt_IpTimeToLive;
|
|
|
|
opt.value.ip_ttl = 1;
|
|
|
|
PR_SetSocketOption(mFD, &opt);
|
|
|
|
|
|
|
|
nsresult rv = NS_NewNamedThread("wifi tickler",
|
|
|
|
getter_AddRefs(mThread));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> tmpTimer(do_CreateInstance(NS_TIMER_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = tmpTimer->SetTarget(mThread);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
mTimer.swap(tmpTimer);
|
|
|
|
|
|
|
|
mAddr.inet.family = PR_AF_INET;
|
|
|
|
mAddr.inet.port = PR_htons (4886);
|
|
|
|
mAddr.inet.ip = 0;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tickler::Tickle()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
MOZ_ASSERT(mThread);
|
|
|
|
mLastTickle = TimeStamp::Now();
|
|
|
|
if (!mActive)
|
|
|
|
MaybeStartTickler();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tickler::PostCheckTickler()
|
|
|
|
{
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
mThread->Dispatch(NS_NewRunnableMethod(this, &Tickler::CheckTickler),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tickler::MaybeStartTicklerUnlocked()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
MaybeStartTickler();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tickler::MaybeStartTickler()
|
|
|
|
{
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_DispatchToMainThread(
|
2014-05-23 23:53:17 +04:00
|
|
|
NS_NewRunnableMethod(this, &Tickler::MaybeStartTicklerUnlocked));
|
2013-07-11 19:39:36 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mPrefs)
|
|
|
|
mPrefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
if (mPrefs) {
|
|
|
|
int32_t val;
|
|
|
|
bool boolVal;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mPrefs->GetBoolPref("network.tickle-wifi.enabled", &boolVal)))
|
|
|
|
mEnabled = boolVal;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mPrefs->GetIntPref("network.tickle-wifi.duration", &val))) {
|
|
|
|
if (val < 1)
|
|
|
|
val = 1;
|
|
|
|
if (val > 100000)
|
|
|
|
val = 100000;
|
|
|
|
mDuration = TimeDuration::FromMilliseconds(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mPrefs->GetIntPref("network.tickle-wifi.delay", &val))) {
|
|
|
|
if (val < 1)
|
|
|
|
val = 1;
|
|
|
|
if (val > 1000)
|
|
|
|
val = 1000;
|
|
|
|
mDelay = static_cast<uint32_t>(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PostCheckTickler();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tickler::CheckTickler()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
MOZ_ASSERT(mThread == NS_GetCurrentThread());
|
|
|
|
|
|
|
|
bool shouldRun = (!mCanceled) &&
|
|
|
|
((TimeStamp::Now() - mLastTickle) <= mDuration);
|
|
|
|
|
|
|
|
if ((shouldRun && mActive) || (!shouldRun && !mActive))
|
|
|
|
return; // no change in state
|
|
|
|
|
|
|
|
if (mActive)
|
|
|
|
StopTickler();
|
|
|
|
else
|
|
|
|
StartTickler();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tickler::Cancel()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mCanceled = true;
|
|
|
|
if (mThread)
|
|
|
|
PostCheckTickler();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tickler::StopTickler()
|
|
|
|
{
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
MOZ_ASSERT(mThread == NS_GetCurrentThread());
|
|
|
|
MOZ_ASSERT(mTimer);
|
|
|
|
MOZ_ASSERT(mActive);
|
|
|
|
|
|
|
|
mTimer->Cancel();
|
|
|
|
mActive = false;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class TicklerTimer final : public nsITimerCallback
|
2013-07-11 19:39:36 +04:00
|
|
|
{
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-07-11 19:39:36 +04:00
|
|
|
NS_DECL_NSITIMERCALLBACK
|
|
|
|
|
|
|
|
TicklerTimer(Tickler *aTickler)
|
|
|
|
{
|
2014-01-02 21:55:12 +04:00
|
|
|
mTickler = do_GetWeakReference(aTickler);
|
2013-07-11 19:39:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-08-26 23:07:59 +04:00
|
|
|
~TicklerTimer() {}
|
|
|
|
|
2014-01-02 21:55:12 +04:00
|
|
|
nsWeakPtr mTickler;
|
2013-07-11 19:39:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
void Tickler::StartTickler()
|
|
|
|
{
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
MOZ_ASSERT(mThread == NS_GetCurrentThread());
|
|
|
|
MOZ_ASSERT(!mActive);
|
|
|
|
MOZ_ASSERT(mTimer);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mTimer->InitWithCallback(new TicklerTimer(this),
|
|
|
|
mEnabled ? mDelay : 1000,
|
|
|
|
nsITimer::TYPE_REPEATING_SLACK)))
|
|
|
|
mActive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// argument should be in network byte order
|
|
|
|
void Tickler::SetIPV4Address(uint32_t address)
|
|
|
|
{
|
|
|
|
mAddr.inet.ip = address;
|
|
|
|
}
|
|
|
|
|
|
|
|
// argument should be in network byte order
|
|
|
|
void Tickler::SetIPV4Port(uint16_t port)
|
|
|
|
{
|
|
|
|
mAddr.inet.port = port;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(TicklerTimer, nsITimerCallback)
|
2013-07-11 19:39:36 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP TicklerTimer::Notify(nsITimer *timer)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Tickler> tickler = do_QueryReferent(mTickler);
|
2013-07-11 19:39:36 +04:00
|
|
|
if (!tickler)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
MutexAutoLock lock(tickler->mLock);
|
|
|
|
|
|
|
|
if (!tickler->mFD) {
|
|
|
|
tickler->StopTickler();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tickler->mCanceled ||
|
|
|
|
((TimeStamp::Now() - tickler->mLastTickle) > tickler->mDuration)) {
|
|
|
|
tickler->StopTickler();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tickler->mEnabled)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
PR_SendTo(tickler->mFD, "", 0, 0, &tickler->mAddr, 0);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla::net
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#else // not defined MOZ_USE_WIFI_TICKLER
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
2014-01-02 21:55:12 +04:00
|
|
|
NS_IMPL_ISUPPORTS0(Tickler)
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
2013-07-11 19:39:36 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // defined MOZ_USE_WIFI_TICKLER
|
|
|
|
|