1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
/* -*- 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 "ConnectionWorker.h"
#include "mozilla/Hal.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/WorkerRef.h"
#include "mozilla/dom/WorkerRunnable.h"
#include "mozilla/dom/WorkerScope.h"
namespace mozilla::dom::network {
class ConnectionProxy final : public hal::NetworkObserver {
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ConnectionProxy)
static already_AddRefed<ConnectionProxy> Create(
WorkerPrivate* aWorkerPrivate, ConnectionWorker* aConnection) {
RefPtr<ConnectionProxy> proxy = new ConnectionProxy(aConnection);
RefPtr<StrongWorkerRef> workerRef = StrongWorkerRef::Create(
aWorkerPrivate, "ConnectionProxy", [proxy]() { proxy->Shutdown(); });
if (NS_WARN_IF(!workerRef)) {
return nullptr;
}
proxy->mWorkerRef = new ThreadSafeWorkerRef(workerRef);
return proxy.forget();
}
ThreadSafeWorkerRef* WorkerRef() const { return mWorkerRef; }
// For IObserver - main-thread only.
void Notify(const hal::NetworkInformation& aNetworkInfo) override;
void Shutdown();
void Update(ConnectionType aType, bool aIsWifi, uint32_t aDHCPGateway) {
MOZ_ASSERT(mConnection);
MOZ_ASSERT(IsCurrentThreadRunningWorker());
mConnection->Update(aType, aIsWifi, aDHCPGateway, true);
}
private:
explicit ConnectionProxy(ConnectionWorker* aConnection)
: mConnection(aConnection) {}
~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;
RefPtr<ThreadSafeWorkerRef> mWorkerRef;
};
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,
"ConnectionWorker :: Initialize"_ns),
mProxy(aProxy),
mNetworkInfo(aNetworkInfo) {
MOZ_ASSERT(aProxy);
aWorkerPrivate->AssertIsOnWorkerThread();
}
bool MainThreadRun() override {
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,
"ConnectionWorker :: Shutdown"_ns),
mProxy(aProxy) {
MOZ_ASSERT(aProxy);
aWorkerPrivate->AssertIsOnWorkerThread();
}
bool MainThreadRun() override {
MOZ_ASSERT(NS_IsMainThread());
hal::UnregisterNetworkObserver(mProxy);
return true;
}
};
class NotifyRunnable final : public WorkerThreadRunnable {
private:
RefPtr<ConnectionProxy> mProxy;
const ConnectionType mConnectionType;
const bool mIsWifi;
const uint32_t mDHCPGateway;
public:
NotifyRunnable(WorkerPrivate* aWorkerPrivate, ConnectionProxy* aProxy,
ConnectionType aType, bool aIsWifi, uint32_t aDHCPGateway)
: WorkerThreadRunnable("NotifyRunnable"),
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) {
bool shouldResistFingerprinting =
aWorkerPrivate->ShouldResistFingerprinting(RFPTarget::NetworkConnection);
RefPtr<ConnectionWorker> c = new ConnectionWorker(shouldResistFingerprinting);
c->mProxy = ConnectionProxy::Create(aWorkerPrivate, c);
if (!c->mProxy) {
aRv.ThrowTypeError("The Worker thread is shutting down.");
return nullptr;
}
hal::NetworkInformation networkInfo;
RefPtr<InitializeRunnable> runnable =
new InitializeRunnable(aWorkerPrivate, c->mProxy, networkInfo);
runnable->Dispatch(aWorkerPrivate, Canceling, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
c->Update(static_cast<ConnectionType>(networkInfo.type()),
networkInfo.isWifi(), networkInfo.dhcpGateway(), false);
return c.forget();
}
ConnectionWorker::ConnectionWorker(bool aShouldResistFingerprinting)
: Connection(nullptr, aShouldResistFingerprinting) {
MOZ_ASSERT(IsCurrentThreadRunningWorker());
}
ConnectionWorker::~ConnectionWorker() { Shutdown(); }
void ConnectionWorker::ShutdownInternal() {
MOZ_ASSERT(IsCurrentThreadRunningWorker());
mProxy->Shutdown();
}
void ConnectionProxy::Notify(const hal::NetworkInformation& aNetworkInfo) {
MOZ_ASSERT(NS_IsMainThread());
RefPtr<NotifyRunnable> runnable =
new NotifyRunnable(mWorkerRef->Private(), this,
static_cast<ConnectionType>(aNetworkInfo.type()),
aNetworkInfo.isWifi(), aNetworkInfo.dhcpGateway());
runnable->Dispatch(mWorkerRef->Private());
}
void ConnectionProxy::Shutdown() {
MOZ_ASSERT(IsCurrentThreadRunningWorker());
// Already shut down.
if (!mConnection) {
return;
}
mConnection = nullptr;
RefPtr<ShutdownRunnable> runnable =
new ShutdownRunnable(mWorkerRef->Private(), this);
ErrorResult rv;
// This runnable _must_ be executed.
runnable->Dispatch(mWorkerRef->Private(), Killing, rv);
if (NS_WARN_IF(rv.Failed())) {
rv.SuppressException();
}
mWorkerRef = nullptr;
}
} // namespace mozilla::dom::network
|