Bug 1842773 - Part 5: Add ArrayBuffer.prototype.{maxByteLength,resizable} getters...
[gecko.git] / dom / base / SameProcessMessageQueue.cpp
blobbfeffd508a3568a31323f3236f91715a3fcf8822
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "SameProcessMessageQueue.h"
8 #include "nsThreadUtils.h"
10 using namespace mozilla;
11 using namespace mozilla::dom;
13 SameProcessMessageQueue* SameProcessMessageQueue::sSingleton;
15 SameProcessMessageQueue::SameProcessMessageQueue() = default;
17 SameProcessMessageQueue::~SameProcessMessageQueue() {
18 // This code should run during shutdown, and we should already have pumped the
19 // event loop. So we should only see messages here if someone is sending
20 // messages pretty late in shutdown.
21 NS_WARNING_ASSERTION(mQueue.IsEmpty(),
22 "Shouldn't send messages during shutdown");
23 sSingleton = nullptr;
26 void SameProcessMessageQueue::Push(Runnable* aRunnable) {
27 mQueue.AppendElement(aRunnable);
28 NS_DispatchToCurrentThread(aRunnable);
31 void SameProcessMessageQueue::Flush() {
32 const nsTArray<RefPtr<Runnable>> queue = std::move(mQueue);
33 for (size_t i = 0; i < queue.Length(); i++) {
34 queue[i]->Run();
38 /* static */
39 SameProcessMessageQueue* SameProcessMessageQueue::Get() {
40 if (!sSingleton) {
41 sSingleton = new SameProcessMessageQueue();
43 return sSingleton;
46 SameProcessMessageQueue::Runnable::Runnable() : mDispatched(false) {}
48 NS_IMPL_ISUPPORTS(SameProcessMessageQueue::Runnable, nsIRunnable)
50 nsresult SameProcessMessageQueue::Runnable::Run() {
51 if (mDispatched) {
52 return NS_OK;
55 SameProcessMessageQueue* queue = SameProcessMessageQueue::Get();
56 queue->mQueue.RemoveElement(this);
58 mDispatched = true;
59 return HandleMessage();