2016-04-06 23:27:22 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "ServiceWorkerJobQueue.h"
|
|
|
|
|
2018-07-10 02:02:39 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2016-04-06 23:27:22 +03:00
|
|
|
#include "ServiceWorkerJob.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerCommon.h"
|
2016-04-06 23:27:22 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
class ServiceWorkerJobQueue::Callback final
|
|
|
|
: public ServiceWorkerJob::Callback {
|
|
|
|
RefPtr<ServiceWorkerJobQueue> mQueue;
|
2016-04-06 23:27:22 +03:00
|
|
|
|
|
|
|
~Callback() {}
|
|
|
|
|
|
|
|
public:
|
2016-04-06 23:27:23 +03:00
|
|
|
explicit Callback(ServiceWorkerJobQueue* aQueue) : mQueue(aQueue) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-06 23:27:22 +03:00
|
|
|
MOZ_ASSERT(mQueue);
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
virtual void JobFinished(ServiceWorkerJob* aJob,
|
|
|
|
ErrorResult& aStatus) override {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-06 23:27:22 +03:00
|
|
|
mQueue->JobFinished(aJob);
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
NS_INLINE_DECL_REFCOUNTING(ServiceWorkerJobQueue::Callback, override)
|
2016-04-06 23:27:22 +03:00
|
|
|
};
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
ServiceWorkerJobQueue::~ServiceWorkerJobQueue() {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-06 23:27:22 +03:00
|
|
|
MOZ_ASSERT(mJobList.IsEmpty());
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
void ServiceWorkerJobQueue::JobFinished(ServiceWorkerJob* aJob) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-06 23:27:22 +03:00
|
|
|
MOZ_ASSERT(aJob);
|
|
|
|
|
|
|
|
// XXX There are some corner cases where jobs can double-complete. Until
|
|
|
|
// we track all these down we do a non-fatal assert in debug builds and
|
|
|
|
// a runtime check to verify the queue is in the correct state.
|
|
|
|
NS_ASSERTION(!mJobList.IsEmpty(),
|
|
|
|
"Job queue should contain the job that just completed.");
|
|
|
|
NS_ASSERTION(mJobList.SafeElementAt(0, nullptr) == aJob,
|
|
|
|
"Job queue should contain the job that just completed.");
|
|
|
|
if (NS_WARN_IF(mJobList.SafeElementAt(0, nullptr) != aJob)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mJobList.RemoveElementAt(0);
|
|
|
|
|
|
|
|
if (mJobList.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RunJob();
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
void ServiceWorkerJobQueue::RunJob() {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-06 23:27:22 +03:00
|
|
|
MOZ_ASSERT(!mJobList.IsEmpty());
|
2016-04-06 23:27:23 +03:00
|
|
|
MOZ_ASSERT(mJobList[0]->GetState() == ServiceWorkerJob::State::Initial);
|
2016-04-06 23:27:22 +03:00
|
|
|
|
|
|
|
RefPtr<Callback> callback = new Callback(this);
|
|
|
|
mJobList[0]->Start(callback);
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
ServiceWorkerJobQueue::ServiceWorkerJobQueue() {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-06 23:27:22 +03:00
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
void ServiceWorkerJobQueue::ScheduleJob(ServiceWorkerJob* aJob) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-06 23:27:22 +03:00
|
|
|
MOZ_ASSERT(aJob);
|
|
|
|
MOZ_ASSERT(!mJobList.Contains(aJob));
|
|
|
|
|
|
|
|
if (mJobList.IsEmpty()) {
|
|
|
|
mJobList.AppendElement(aJob);
|
|
|
|
RunJob();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
MOZ_ASSERT(mJobList[0]->GetState() == ServiceWorkerJob::State::Started);
|
2016-04-06 23:27:22 +03:00
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
RefPtr<ServiceWorkerJob>& tailJob = mJobList[mJobList.Length() - 1];
|
2016-04-06 23:27:23 +03:00
|
|
|
if (!tailJob->ResultCallbacksInvoked() && aJob->IsEquivalentTo(tailJob)) {
|
2016-04-06 23:27:22 +03:00
|
|
|
tailJob->StealResultCallbacksFrom(aJob);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mJobList.AppendElement(aJob);
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
void ServiceWorkerJobQueue::CancelAll() {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-06 23:27:22 +03:00
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
for (RefPtr<ServiceWorkerJob>& job : mJobList) {
|
2016-04-06 23:27:22 +03:00
|
|
|
job->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove jobs that are queued but not started since they should never
|
|
|
|
// run after being canceled. This means throwing away all jobs except
|
|
|
|
// for the job at the front of the list.
|
|
|
|
if (!mJobList.IsEmpty()) {
|
2016-04-06 23:27:23 +03:00
|
|
|
MOZ_ASSERT(mJobList[0]->GetState() == ServiceWorkerJob::State::Started);
|
2016-04-06 23:27:22 +03:00
|
|
|
mJobList.TruncateLength(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|