forked from mirrors/gecko-dev
WorkerRunnable no longer keeps a raw pointer(mWorkerPrivate) for the associated WorkerPrivate in this patch. Removing the WorkerRunnable::mWorkerPrivate needs to fix the following problems. 1. Thread assertions in WorkerRunnable::Dispatch() To fix this problem, the associated WorkerPrivate is as a parameter and passed to WorkerRunnable::Dispatch() for the dispatching thread assertions. This associated WorkerPrivate is also propagated to PreDispatch() and PostDispatch() for the children classes of WorkerRunnable() 2. Get the associated WorkerPrivate in WorkerRunnable::Run() for environment setup(GlobabObject, JSContext setting for the runnable) - For WorkerThreadRunnable Since WorkerThreadRunnable is supposed to run on the worker thread, it does not need to keep a raw pointer to WorkerPrivate as its class member. GetCurrentThreadWorkerPrivate() should always get the correct WorkerPrivate for WorkerThreadRunnable. - For WorkerParentThreadRunnable WorkerParentRef is introduced to keep a RefPtr<WorkerPrivate> for WorkerParentThreadRunnable instead of using a raw pointer. Checking the associated WorkerPrivate existence by WorkerParentRef at the beginning of WorkerParentThreadRunnable::Run(). If the Worker has already shut down, WorkerParentThreadRunnable cannot do anything with the associated WorkerPrivate, so WorkerParentThreadRunnable::Run() will return NS_OK directly but with a warning. The associated WorkerPrivate is also passed into WorkerRun(), PreRun(), and PostRun(), so the majority of implementations of child classes of WorkerRunnable do not need to be changed. If there are any cases in which the child classes of WorkerThreadRunnable/WorkerParentThreadRunnable want to keep the associated WorkerPrivate, they should use WorkerRefs instead of raw pointers. Depends on D205679 Differential Revision: https://phabricator.services.mozilla.com/D207039
212 lines
6.3 KiB
C++
212 lines
6.3 KiB
C++
/* -*- 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"
|
|
#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::NavigatorConnection);
|
|
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(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(Killing, rv);
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
rv.SuppressException();
|
|
}
|
|
|
|
mWorkerRef = nullptr;
|
|
}
|
|
|
|
} // namespace mozilla::dom::network
|