2016-12-16 10:50:23 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
#include "mozilla/Hal.h"
|
|
|
|
#include "ConnectionWorker.h"
|
2018-03-13 23:17:02 +03:00
|
|
|
#include "mozilla/dom/WorkerRef.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
2016-12-16 10:50:23 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace network {
|
|
|
|
|
2018-08-13 04:57:50 +03:00
|
|
|
class ConnectionProxy final : public hal::NetworkObserver {
|
2016-12-16 10:50:23 +03:00
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ConnectionProxy)
|
|
|
|
|
|
|
|
static already_AddRefed<ConnectionProxy> Create(
|
|
|
|
WorkerPrivate* aWorkerPrivate, ConnectionWorker* aConnection) {
|
2018-03-13 23:17:02 +03:00
|
|
|
RefPtr<ConnectionProxy> proxy = new ConnectionProxy(aConnection);
|
|
|
|
|
|
|
|
RefPtr<StrongWorkerRef> workerRef = StrongWorkerRef::Create(
|
|
|
|
aWorkerPrivate, "ConnectionProxy", [proxy]() { proxy->Shutdown(); });
|
|
|
|
if (NS_WARN_IF(!workerRef)) {
|
2016-12-16 10:50:23 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-13 23:17:02 +03:00
|
|
|
proxy->mWorkerRef = new ThreadSafeWorkerRef(workerRef);
|
2016-12-16 10:50:23 +03:00
|
|
|
return proxy.forget();
|
|
|
|
}
|
|
|
|
|
2018-03-13 23:17:02 +03:00
|
|
|
ThreadSafeWorkerRef* WorkerRef() const { return mWorkerRef; }
|
|
|
|
|
2016-12-16 10:50:23 +03:00
|
|
|
// For IObserver - main-thread only.
|
|
|
|
void Notify(const hal::NetworkInformation& aNetworkInfo) override;
|
|
|
|
|
|
|
|
void Shutdown();
|
|
|
|
|
2017-07-03 00:11:46 +03:00
|
|
|
void Update(ConnectionType aType, bool aIsWifi, uint32_t aDHCPGateway) {
|
2016-12-16 10:50:23 +03:00
|
|
|
MOZ_ASSERT(mConnection);
|
2018-03-13 23:17:02 +03:00
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
2016-12-16 10:50:23 +03:00
|
|
|
mConnection->Update(aType, aIsWifi, aDHCPGateway, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-03-13 23:17:02 +03:00
|
|
|
explicit ConnectionProxy(ConnectionWorker* aConnection)
|
|
|
|
: mConnection(aConnection) {}
|
2016-12-16 10:50:23 +03:00
|
|
|
|
|
|
|
~ConnectionProxy() = default;
|
|
|
|
|
|
|
|
// Raw pointer because the ConnectionWorker keeps alive the proxy.
|
|
|
|
// This is touched only on the worker-thread and it's nullified when the
|
|
|
|
// shutdown procedure starts.
|
|
|
|
ConnectionWorker* mConnection;
|
|
|
|
|
2018-03-13 23:17:02 +03:00
|
|
|
RefPtr<ThreadSafeWorkerRef> mWorkerRef;
|
2016-12-16 10:50:23 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// This class initializes the hal observer on the main-thread.
|
|
|
|
class InitializeRunnable : public WorkerMainThreadRunnable {
|
|
|
|
private:
|
|
|
|
// raw pointer because this is a sync runnable.
|
|
|
|
ConnectionProxy* mProxy;
|
|
|
|
hal::NetworkInformation& mNetworkInfo;
|
|
|
|
|
|
|
|
public:
|
|
|
|
InitializeRunnable(WorkerPrivate* aWorkerPrivate, ConnectionProxy* aProxy,
|
|
|
|
hal::NetworkInformation& aNetworkInfo)
|
|
|
|
: WorkerMainThreadRunnable(
|
|
|
|
aWorkerPrivate,
|
|
|
|
NS_LITERAL_CSTRING("ConnectionWorker :: Initialize")),
|
|
|
|
mProxy(aProxy),
|
|
|
|
mNetworkInfo(aNetworkInfo) {
|
|
|
|
MOZ_ASSERT(aProxy);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
bool MainThreadRun() override {
|
2016-12-16 10:50:23 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
hal::RegisterNetworkObserver(mProxy);
|
|
|
|
hal::GetCurrentNetworkInformation(&mNetworkInfo);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This class turns down the hal observer on the main-thread.
|
|
|
|
class ShutdownRunnable : public WorkerMainThreadRunnable {
|
|
|
|
private:
|
|
|
|
// raw pointer because this is a sync runnable.
|
|
|
|
ConnectionProxy* mProxy;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ShutdownRunnable(WorkerPrivate* aWorkerPrivate, ConnectionProxy* aProxy)
|
|
|
|
: WorkerMainThreadRunnable(
|
|
|
|
aWorkerPrivate, NS_LITERAL_CSTRING("ConnectionWorker :: Shutdown")),
|
|
|
|
mProxy(aProxy) {
|
|
|
|
MOZ_ASSERT(aProxy);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
bool MainThreadRun() override {
|
2016-12-16 10:50:23 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
hal::UnregisterNetworkObserver(mProxy);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class NotifyRunnable : public WorkerRunnable {
|
|
|
|
private:
|
|
|
|
RefPtr<ConnectionProxy> mProxy;
|
|
|
|
|
|
|
|
const ConnectionType mConnectionType;
|
|
|
|
const bool mIsWifi;
|
2017-07-03 00:11:46 +03:00
|
|
|
const uint32_t mDHCPGateway;
|
2016-12-16 10:50:23 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
NotifyRunnable(WorkerPrivate* aWorkerPrivate, ConnectionProxy* aProxy,
|
2017-07-03 00:11:46 +03:00
|
|
|
ConnectionType aType, bool aIsWifi, uint32_t aDHCPGateway)
|
2016-12-16 10:50:23 +03:00
|
|
|
: WorkerRunnable(aWorkerPrivate),
|
|
|
|
mProxy(aProxy),
|
|
|
|
mConnectionType(aType),
|
|
|
|
mIsWifi(aIsWifi),
|
|
|
|
mDHCPGateway(aDHCPGateway) {
|
|
|
|
MOZ_ASSERT(aProxy);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
mProxy->Update(mConnectionType, mIsWifi, mDHCPGateway);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<ConnectionWorker> ConnectionWorker::Create(
|
|
|
|
WorkerPrivate* aWorkerPrivate, ErrorResult& aRv) {
|
2018-03-13 23:17:02 +03:00
|
|
|
RefPtr<ConnectionWorker> c = new ConnectionWorker();
|
2016-12-16 10:50:23 +03:00
|
|
|
c->mProxy = ConnectionProxy::Create(aWorkerPrivate, c);
|
|
|
|
if (!c->mProxy) {
|
|
|
|
aRv.ThrowTypeError<MSG_WORKER_THREAD_SHUTTING_DOWN>();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hal::NetworkInformation networkInfo;
|
|
|
|
RefPtr<InitializeRunnable> runnable =
|
|
|
|
new InitializeRunnable(aWorkerPrivate, c->mProxy, networkInfo);
|
|
|
|
|
2018-07-12 20:33:41 +03:00
|
|
|
runnable->Dispatch(Canceling, aRv);
|
2016-12-16 10:50:23 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->Update(static_cast<ConnectionType>(networkInfo.type()),
|
|
|
|
networkInfo.isWifi(), networkInfo.dhcpGateway(), false);
|
|
|
|
return c.forget();
|
|
|
|
}
|
|
|
|
|
2018-03-13 23:17:02 +03:00
|
|
|
ConnectionWorker::ConnectionWorker() : Connection(nullptr) {
|
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
2016-12-16 10:50:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionWorker::~ConnectionWorker() { Shutdown(); }
|
|
|
|
|
|
|
|
void ConnectionWorker::ShutdownInternal() {
|
2018-03-13 23:17:02 +03:00
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
2016-12-16 10:50:23 +03:00
|
|
|
mProxy->Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionProxy::Notify(const hal::NetworkInformation& aNetworkInfo) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
RefPtr<NotifyRunnable> runnable =
|
2018-03-13 23:17:02 +03:00
|
|
|
new NotifyRunnable(mWorkerRef->Private(), this,
|
2016-12-16 10:50:23 +03:00
|
|
|
static_cast<ConnectionType>(aNetworkInfo.type()),
|
|
|
|
aNetworkInfo.isWifi(), aNetworkInfo.dhcpGateway());
|
|
|
|
runnable->Dispatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionProxy::Shutdown() {
|
2018-03-13 23:17:02 +03:00
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
2016-12-16 10:50:23 +03:00
|
|
|
|
|
|
|
// Already shut down.
|
|
|
|
if (!mConnection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mConnection = nullptr;
|
|
|
|
|
|
|
|
RefPtr<ShutdownRunnable> runnable =
|
2018-03-13 23:17:02 +03:00
|
|
|
new ShutdownRunnable(mWorkerRef->Private(), this);
|
2016-12-16 10:50:23 +03:00
|
|
|
|
|
|
|
ErrorResult rv;
|
2017-01-05 12:05:32 +03:00
|
|
|
// This runnable _must_ be executed.
|
|
|
|
runnable->Dispatch(Killing, rv);
|
2016-12-16 10:50:23 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
|
|
|
}
|
|
|
|
|
2018-03-13 23:17:02 +03:00
|
|
|
mWorkerRef = nullptr;
|
2016-12-16 10:50:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace network
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|