Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/workers/WorkerHolderToken.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
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "WorkerHolderToken.h"
8
9
#include "WorkerPrivate.h"
10
11
namespace mozilla {
12
namespace dom {
13
14
// static
15
already_AddRefed<WorkerHolderToken>
16
WorkerHolderToken::Create(WorkerPrivate* aWorkerPrivate,
17
                          WorkerStatus aShutdownStatus,
18
                          Behavior aBehavior)
19
0
{
20
0
  MOZ_DIAGNOSTIC_ASSERT(aWorkerPrivate);
21
0
  aWorkerPrivate->AssertIsOnWorkerThread();
22
0
23
0
  RefPtr<WorkerHolderToken> workerHolder =
24
0
    new WorkerHolderToken(aShutdownStatus, aBehavior);
25
0
26
0
  if (NS_WARN_IF(!workerHolder->HoldWorker(aWorkerPrivate, aShutdownStatus))) {
27
0
    return nullptr;
28
0
  }
29
0
30
0
  return workerHolder.forget();
31
0
}
32
33
void
34
WorkerHolderToken::AddListener(Listener* aListener)
35
0
{
36
0
  NS_ASSERT_OWNINGTHREAD(WorkerHolderToken);
37
0
  MOZ_ASSERT(aListener);
38
0
  MOZ_ASSERT(!mListenerList.Contains(aListener));
39
0
40
0
  mListenerList.AppendElement(aListener);
41
0
42
0
  // Allow an actor to be added after we've entered the Notifying case.  We
43
0
  // can't stop the actor creation from racing with out destruction of the
44
0
  // other actors and we need to wait for this extra one to close as well.
45
0
  // Signal it should destroy itself right away.
46
0
  if (mShuttingDown) {
47
0
    aListener->WorkerShuttingDown();
48
0
  }
49
0
}
50
51
void
52
WorkerHolderToken::RemoveListener(Listener* aListener)
53
0
{
54
0
  NS_ASSERT_OWNINGTHREAD(WorkerHolderToken);
55
0
  MOZ_ASSERT(aListener);
56
0
57
0
  DebugOnly<bool> removed = mListenerList.RemoveElement(aListener);
58
0
59
0
  MOZ_ASSERT(removed);
60
0
  MOZ_ASSERT(!mListenerList.Contains(aListener));
61
0
}
62
63
bool
64
WorkerHolderToken::IsShuttingDown() const
65
0
{
66
0
  return mShuttingDown;
67
0
}
68
69
WorkerPrivate*
70
WorkerHolderToken::GetWorkerPrivate() const
71
0
{
72
0
  NS_ASSERT_OWNINGTHREAD(WorkerHolderToken);
73
0
  return mWorkerPrivate;
74
0
}
75
76
WorkerHolderToken::WorkerHolderToken(WorkerStatus aShutdownStatus,
77
                                     Behavior aBehavior)
78
  : WorkerHolder("WorkerHolderToken", aBehavior)
79
  , mShutdownStatus(aShutdownStatus)
80
  , mShuttingDown(false)
81
0
{
82
0
}
83
84
WorkerHolderToken::~WorkerHolderToken()
85
0
{
86
0
  NS_ASSERT_OWNINGTHREAD(WorkerHolderToken);
87
0
  MOZ_ASSERT(mListenerList.IsEmpty());
88
0
}
89
90
bool
91
WorkerHolderToken::Notify(WorkerStatus aStatus)
92
0
{
93
0
  NS_ASSERT_OWNINGTHREAD(WorkerHolderToken);
94
0
95
0
  // When the service worker thread is stopped we will get Canceling,
96
0
  // but nothing higher than that.  We must shut things down at Canceling.
97
0
  if (aStatus < mShutdownStatus || mShuttingDown) {
98
0
    return true;
99
0
  }
100
0
101
0
  // Start the asynchronous destruction of our actors.  These will call back
102
0
  // into RemoveActor() once the actor is destroyed.
103
0
  nsTObserverArray<Listener*>::ForwardIterator iter(mListenerList);
104
0
  while (iter.HasMore()) {
105
0
    iter.GetNext()->WorkerShuttingDown();
106
0
  }
107
0
108
0
  // Set this after calling WorkerShuttingDown() on listener list in case
109
0
  // one callback triggers another listener to be added.
110
0
  mShuttingDown = true;
111
0
112
0
  return true;
113
0
}
114
115
} // dom namespace
116
} // mozilla namespace