Bug 1588241 - P2. Add ability to use lambdas for channel event handlers. r=mayhemer

Remove the need to create separate runnable classes. It's far more readable and remove the need to duplicate lots of code.

We unfortunately need to capture "this" in a ref counter to get around the static analyzer complaining about capturing this by value, even thouch the ChannelEventQueue guarantees that this will outlive the event.

Differential Revision: https://phabricator.services.mozilla.com/D52261

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jean-Yves Avenard 2019-11-15 02:59:21 +00:00
Родитель 801075cd07
Коммит a3e1b708bd
3 изменённых файлов: 261 добавлений и 468 удалений

Просмотреть файл

@ -70,6 +70,58 @@ class NeckoTargetChannelEvent : public ChannelEvent {
T* mChild;
};
class ChannelFunctionEvent : public ChannelEvent {
public:
ChannelFunctionEvent(
std::function<already_AddRefed<nsIEventTarget>()>&& aGetEventTarget,
std::function<void()>&& aCallback)
: mGetEventTarget(std::move(aGetEventTarget)),
mCallback(std::move(aCallback)) {}
void Run() override { mCallback(); }
already_AddRefed<nsIEventTarget> GetEventTarget() override {
return mGetEventTarget();
}
private:
const std::function<already_AddRefed<nsIEventTarget>()> mGetEventTarget;
const std::function<void()> mCallback;
};
// UnsafePtr is a work-around our static analyzer that requires all
// ref-counted objects to be captured in lambda via a RefPtr
// The ChannelEventQueue makes it safe to capture "this" by pointer only.
// This is required as work-around to prevent cycles until bug 1596295
// is resolved.
template <typename T>
class UnsafePtr {
public:
explicit UnsafePtr(T* aPtr) : mPtr(aPtr) {}
T& operator*() const { return *mPtr; }
T* operator->() const {
MOZ_ASSERT(mPtr, "dereferencing a null pointer");
return mPtr;
}
operator T*() const& { return mPtr; }
explicit operator bool() const { return mPtr != nullptr; }
private:
T* const mPtr;
};
class NeckoTargetChannelFunctionEvent : public ChannelFunctionEvent {
public:
template <typename T>
NeckoTargetChannelFunctionEvent(T* aChild, std::function<void()>&& aCallback)
: ChannelFunctionEvent(
[child = UnsafePtr<T>(aChild)]() {
MOZ_ASSERT(child);
return child->GetNeckoTarget();
},
std::move(aCallback)) {}
};
// Workaround for Necko re-entrancy dangers. We buffer IPDL messages in a
// queue if still dispatching previous one(s) to listeners/observers.
// Otherwise synchronous XMLHttpRequests and/or other code that spins the

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -523,26 +523,11 @@ class HttpChannelChild final : public PHttpChannelChild,
// Collect telemetry for the successful rate of OMT.
void CollectOMTTelemetry();
friend class AssociateApplicationCacheEvent;
friend class StartRequestEvent;
friend class StopRequestEvent;
friend class TransportAndDataEvent;
friend class MaybeDivertOnDataHttpEvent;
friend class MaybeDivertOnStopHttpEvent;
friend class ProgressEvent;
friend class StatusEvent;
friend class FailedAsyncOpenEvent;
friend class Redirect1Event;
friend class Redirect3Event;
friend class DeleteSelfEvent;
friend class HttpFlushedForDiversionEvent;
friend class CancelEvent;
friend class HttpAsyncAborter<HttpChannelChild>;
friend class InterceptStreamListener;
friend class InterceptedChannelContent;
friend class HttpBackgroundChannelChild;
friend class NeckoTargetChannelEvent<HttpChannelChild>;
friend class ContinueDoNotifyListenerEvent;
friend class NeckoTargetChannelFunctionEvent;
};
NS_DEFINE_STATIC_IID_ACCESSOR(HttpChannelChild, HTTP_CHANNEL_CHILD_IID)