Line data Source code
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/. */
6 :
7 : #include "SameProcessMessageQueue.h"
8 : #include "nsThreadUtils.h"
9 :
10 : using namespace mozilla;
11 : using namespace mozilla::dom;
12 :
13 : SameProcessMessageQueue* SameProcessMessageQueue::sSingleton;
14 :
15 1 : SameProcessMessageQueue::SameProcessMessageQueue()
16 : {
17 1 : }
18 :
19 0 : SameProcessMessageQueue::~SameProcessMessageQueue()
20 : {
21 : // This code should run during shutdown, and we should already have pumped the
22 : // event loop. So we should only see messages here if someone is sending
23 : // messages pretty late in shutdown.
24 0 : NS_WARNING_ASSERTION(mQueue.IsEmpty(),
25 : "Shouldn't send messages during shutdown");
26 0 : sSingleton = nullptr;
27 0 : }
28 :
29 : void
30 1 : SameProcessMessageQueue::Push(Runnable* aRunnable)
31 : {
32 1 : mQueue.AppendElement(aRunnable);
33 1 : NS_DispatchToCurrentThread(aRunnable);
34 1 : }
35 :
36 : void
37 0 : SameProcessMessageQueue::Flush()
38 : {
39 0 : nsTArray<RefPtr<Runnable>> queue;
40 0 : mQueue.SwapElements(queue);
41 0 : for (size_t i = 0; i < queue.Length(); i++) {
42 0 : queue[i]->Run();
43 : }
44 0 : }
45 :
46 : /* static */ SameProcessMessageQueue*
47 2 : SameProcessMessageQueue::Get()
48 : {
49 2 : if (!sSingleton) {
50 1 : sSingleton = new SameProcessMessageQueue();
51 : }
52 2 : return sSingleton;
53 : }
54 :
55 1 : SameProcessMessageQueue::Runnable::Runnable()
56 1 : : mDispatched(false)
57 : {
58 1 : }
59 :
60 18 : NS_IMPL_ISUPPORTS(SameProcessMessageQueue::Runnable, nsIRunnable)
61 :
62 : nsresult
63 1 : SameProcessMessageQueue::Runnable::Run()
64 : {
65 1 : if (mDispatched) {
66 0 : return NS_OK;
67 : }
68 :
69 1 : SameProcessMessageQueue* queue = SameProcessMessageQueue::Get();
70 1 : queue->mQueue.RemoveElement(this);
71 :
72 1 : mDispatched = true;
73 1 : return HandleMessage();
74 9 : }
|