Skip to content

Instantly share code, notes, and snippets.

@FarooqAR
Last active March 13, 2020 10:48
Show Gist options
  • Save FarooqAR/7684a0922c49f10b28d6f86372c6f135 to your computer and use it in GitHub Desktop.
Save FarooqAR/7684a0922c49f10b28d6f86372c6f135 to your computer and use it in GitHub Desktop.
Potential required work for Server-sent events (gsoc)
struct Message {
nsString mEventName;
nsString mLastEventID;
nsString mData;
};
void EventSourceEventService::EventSourceMessageReceived(
uint64_t aInnerWindowID,
uint64_t aHttpChannelId,
const nsACString& aEventName,
const nsACString& aLastEventID,
const nsACString& aData,
nsIEventTarget* aTarget
);
void EventSourceEventService::EventSourceConnectionOpened(
uint64_t mInnerWindowID,
uint64_t httpChannelId,
nsIEventTarget* aTarget
);
void EventSourceEventService::EventSourceConnectionClosed(
uint64_t mInnerWindowID,
uint64_t httpChannelId,
nsIEventTarget* aTarget
);
class EventSourceBaseRunnable : public Runnable {
public:
EventSourceBaseRunnable(uint32_t aHttpChannelId, uint64_t aInnerWindowID)
: Runnable("net::EventSourceBaseRunnable"),
mHttpChannelId(aHttpChannelId),
mInnerWindowID(aInnerWindowID) {}
NS_IMETHOD Run() override {
MOZ_ASSERT(NS_IsMainThread());
RefPtr<EventSourceEventService> service =
EventSourceEventService::GetOrCreate();
MOZ_ASSERT(service);
EventSourceEventService::WindowListeners listeners;
service->GetListeners(mInnerWindowID, listeners);
for (uint32_t i = 0; i < listeners.Length(); ++i) {
DoWork(listeners[i]);
}
return NS_OK;
}
protected:
~EventSourceBaseRunnable() = default;
virtual void DoWork(nsIEventSourceEventListener* aListener) = 0;
uint32_t mHttpChannelId;
uint64_t mInnerWindowID;
};
class EventSourceMessageAvailableRunnable final : public EventSourceBaseRunnable {
public:
EventSourceMessageAvailableRunnable(uint64_t aInnerWindowID,
uint64_t aHttpChannelId,
const nsACString& aEventName,
const nsACString& aLastEventID,
const nsACString& aData)
: EventSourceBaseRunnable(aHttpChannelId, aInnerWindowID),
mEventName(aEventName),
mLastEventID(aLastEventID),
mData(aData){}
private:
virtual void DoWork(nsIEventSourceEventListener* aListener) override {
DebugOnly<nsresult> rv = aListener->EventReceived(
mInnerWindowID, aHttpChannelId, mEventName, mLastEventID, mData);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "EventReceived failed");
}
const nsCString mEventName;
const nsCString mLastEventID;
const nsCString mData;
};
class EventSourceConnectionOpenedRunnable final : public EventSourceBaseRunnable {
public:
EventSourceConnectionOpenedRunnable(uint32_t aInnerWindowID, uint64_t aHttpChannelId)
: EventSourceBaseRunnable(aHttpChannelId, aInnerWindowID){}
private:
virtual void DoWork(nsIEventSourceEventListener* aListener) override {
DebugOnly<nsresult> rv =
aListener->EventSourceConnectionOpened(mHttpChannelId, mInnerWindowID);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "EventSourceConnectionOpened failed");
}
};
class EventSourceConnectionClosedRunnable final : public EventSourceBaseRunnable {
public:
EventSourceConnectionClosedRunnable(uint32_t aInnerWindowID, uint64_t aHttpChannelId)
: EventSourceBaseRunnable(aHttpChannelId, aInnerWindowID){}
private:
virtual void DoWork(nsIEventSourceEventListener* aListener) override {
DebugOnly<nsresult> rv =
aListener->EventSourceConnectionClosed(mHttpChannelId, mInnerWindowID);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "EventSourceConnectionClosed failed");
}
};
already_AddRefed<EventSourceEventService> EventSourceEventService::GetOrCreate() {
MOZ_ASSERT(NS_IsMainThread());
if (!gEventSourceEventService) {
gEventSourceEventService = new EventSourceEventService();
}
RefPtr<EventSourceEventService> service = gEventSourceEventService.get();
return service.forget();
}
void EventSourceEventService::EventSourceMessageReceived(
uint64_t aInnerWindowID,
uint64_t aHttpChannelId,
const nsACString& aEventName,
const nsACString& aLastEventID,
const nsACString& aData,
nsIEventTarget* aTarget
) {
RefPtr<EventSourceMessageAvailableRunnable> runnable =
new EventSourceMessageAvailableRunnable(
aInnerWindowID,
aHttpChannelId,
aMessage.mEventName,
aMessage.mLastEventID,
aMessage.mData
);
DebugOnly<nsresult> rv = aTarget
? aTarget->Dispatch(runnable, NS_DISPATCH_NORMAL)
: NS_DispatchToMainThread(runnable);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "NS_DispatchToMainThread failed");
}
mozilla::ipc::IPCResult EventSourceEventListener::RecvEventReceived(
uint64_t aInnerWindowID,
uint64_t aHttpChannelId,
const nsACString& aEventName,
const nsACString& aLastEventID,
const nsACString& aData) {
if (mService) {
nsCOMPtr<nsIEventTarget> target = GetNeckoTarget();
mService->EventSourceMessageReceived(
aInnerWindowID,
aHttpChannelId,
aEventName,
aLastEventID,
aData,
target);
}
return IPC_OK();
}
mozilla::ipc::IPCResult EventSourceEventListener::RecvEventSourceConnectionClosed(
const uint64_t& aHttpChannelId,
const uint64_t& aInnerWindowID) {
if (mService) {
nsCOMPtr<nsIEventTarget> target = GetNeckoTarget();
mService->EventSourceConnectionClosed(aHttpChannelId, mInnerWindowID, target);
}
return IPC_OK();
}
mozilla::ipc::IPCResult EventSourceEventListener::RecvEventSourceConnectionOpened(
const uint64_t& aHttpChannelId,
const uint64_t& aInnerWindowID) {
if (mService) {
nsCOMPtr<nsIEventTarget> target = GetNeckoTarget();
mService->EventSourceConnectionOpened(aHttpChannelId, mInnerWindowID, target);
}
return IPC_OK();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment