Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/SameProcessMessageQueue.cpp
Line
Count
Source (jump to first uncovered line)
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
SameProcessMessageQueue::SameProcessMessageQueue()
16
0
{
17
0
}
18
19
SameProcessMessageQueue::~SameProcessMessageQueue()
20
0
{
21
0
  // This code should run during shutdown, and we should already have pumped the
22
0
  // event loop. So we should only see messages here if someone is sending
23
0
  // messages pretty late in shutdown.
24
0
  NS_WARNING_ASSERTION(mQueue.IsEmpty(),
25
0
                       "Shouldn't send messages during shutdown");
26
0
  sSingleton = nullptr;
27
0
}
28
29
void
30
SameProcessMessageQueue::Push(Runnable* aRunnable)
31
0
{
32
0
  mQueue.AppendElement(aRunnable);
33
0
  NS_DispatchToCurrentThread(aRunnable);
34
0
}
35
36
void
37
SameProcessMessageQueue::Flush()
38
0
{
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
0
  }
44
0
}
45
46
/* static */ SameProcessMessageQueue*
47
SameProcessMessageQueue::Get()
48
0
{
49
0
  if (!sSingleton) {
50
0
    sSingleton = new SameProcessMessageQueue();
51
0
  }
52
0
  return sSingleton;
53
0
}
54
55
SameProcessMessageQueue::Runnable::Runnable()
56
 : mDispatched(false)
57
0
{
58
0
}
59
60
NS_IMPL_ISUPPORTS(SameProcessMessageQueue::Runnable, nsIRunnable)
61
62
nsresult
63
SameProcessMessageQueue::Runnable::Run()
64
0
{
65
0
  if (mDispatched) {
66
0
    return NS_OK;
67
0
  }
68
0
69
0
  SameProcessMessageQueue* queue = SameProcessMessageQueue::Get();
70
0
  queue->mQueue.RemoveElement(this);
71
0
72
0
  mDispatched = true;
73
0
  return HandleMessage();
74
0
}