Source code

Revision control

Other Tools

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 "ServiceWorkerJobQueue.h"
8
9
#include "nsThreadUtils.h"
10
#include "ServiceWorkerJob.h"
11
#include "mozilla/dom/WorkerCommon.h"
12
13
namespace mozilla {
14
namespace dom {
15
16
class ServiceWorkerJobQueue::Callback final
17
: public ServiceWorkerJob::Callback {
18
RefPtr<ServiceWorkerJobQueue> mQueue;
19
20
~Callback() {}
21
22
public:
23
explicit Callback(ServiceWorkerJobQueue* aQueue) : mQueue(aQueue) {
24
MOZ_ASSERT(NS_IsMainThread());
25
MOZ_ASSERT(mQueue);
26
}
27
28
virtual void JobFinished(ServiceWorkerJob* aJob,
29
ErrorResult& aStatus) override {
30
MOZ_ASSERT(NS_IsMainThread());
31
mQueue->JobFinished(aJob);
32
}
33
34
NS_INLINE_DECL_REFCOUNTING(ServiceWorkerJobQueue::Callback, override)
35
};
36
37
ServiceWorkerJobQueue::~ServiceWorkerJobQueue() {
38
MOZ_ASSERT(NS_IsMainThread());
39
MOZ_ASSERT(mJobList.IsEmpty());
40
}
41
42
void ServiceWorkerJobQueue::JobFinished(ServiceWorkerJob* aJob) {
43
MOZ_ASSERT(NS_IsMainThread());
44
MOZ_ASSERT(aJob);
45
46
// XXX There are some corner cases where jobs can double-complete. Until
47
// we track all these down we do a non-fatal assert in debug builds and
48
// a runtime check to verify the queue is in the correct state.
49
NS_ASSERTION(!mJobList.IsEmpty(),
50
"Job queue should contain the job that just completed.");
51
NS_ASSERTION(mJobList.SafeElementAt(0, nullptr) == aJob,
52
"Job queue should contain the job that just completed.");
53
if (NS_WARN_IF(mJobList.SafeElementAt(0, nullptr) != aJob)) {
54
return;
55
}
56
57
mJobList.RemoveElementAt(0);
58
59
if (mJobList.IsEmpty()) {
60
return;
61
}
62
63
RunJob();
64
}
65
66
void ServiceWorkerJobQueue::RunJob() {
67
MOZ_ASSERT(NS_IsMainThread());
68
MOZ_ASSERT(!mJobList.IsEmpty());
69
MOZ_ASSERT(mJobList[0]->GetState() == ServiceWorkerJob::State::Initial);
70
71
RefPtr<Callback> callback = new Callback(this);
72
mJobList[0]->Start(callback);
73
}
74
75
ServiceWorkerJobQueue::ServiceWorkerJobQueue() {
76
MOZ_ASSERT(NS_IsMainThread());
77
}
78
79
void ServiceWorkerJobQueue::ScheduleJob(ServiceWorkerJob* aJob) {
80
MOZ_ASSERT(NS_IsMainThread());
81
MOZ_ASSERT(aJob);
82
MOZ_ASSERT(!mJobList.Contains(aJob));
83
84
if (mJobList.IsEmpty()) {
85
mJobList.AppendElement(aJob);
86
RunJob();
87
return;
88
}
89
90
MOZ_ASSERT(mJobList[0]->GetState() == ServiceWorkerJob::State::Started);
91
92
RefPtr<ServiceWorkerJob>& tailJob = mJobList[mJobList.Length() - 1];
93
if (!tailJob->ResultCallbacksInvoked() && aJob->IsEquivalentTo(tailJob)) {
94
tailJob->StealResultCallbacksFrom(aJob);
95
return;
96
}
97
98
mJobList.AppendElement(aJob);
99
}
100
101
void ServiceWorkerJobQueue::CancelAll() {
102
MOZ_ASSERT(NS_IsMainThread());
103
104
for (RefPtr<ServiceWorkerJob>& job : mJobList) {
105
job->Cancel();
106
}
107
108
// Remove jobs that are queued but not started since they should never
109
// run after being canceled. This means throwing away all jobs except
110
// for the job at the front of the list.
111
if (!mJobList.IsEmpty()) {
112
MOZ_ASSERT(mJobList[0]->GetState() == ServiceWorkerJob::State::Started);
113
mJobList.TruncateLength(1);
114
}
115
}
116
117
} // namespace dom
118
} // namespace mozilla