2016-04-09 03:28:25 +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 "ServiceWorkerRegistrationInfo.h"
|
|
|
|
|
2018-01-27 00:08:58 +03:00
|
|
|
#include "ServiceWorkerManager.h"
|
|
|
|
#include "ServiceWorkerPrivate.h"
|
|
|
|
|
2018-01-27 00:08:59 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2016-04-09 03:28:25 +03:00
|
|
|
|
2016-04-29 04:01:22 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ContinueActivateRunnable final : public LifeCycleEventCallback
|
|
|
|
{
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> mRegistration;
|
|
|
|
bool mSuccess;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ContinueActivateRunnable(const nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration)
|
|
|
|
: mRegistration(aRegistration)
|
|
|
|
, mSuccess(false)
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-29 04:01:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetResult(bool aResult) override
|
|
|
|
{
|
|
|
|
mSuccess = aResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-29 04:01:22 +03:00
|
|
|
mRegistration->FinishActivate(mSuccess);
|
|
|
|
mRegistration = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-04-09 03:28:25 +03:00
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::Clear()
|
|
|
|
{
|
2016-07-28 02:09:15 +03:00
|
|
|
if (mEvaluatingWorker) {
|
|
|
|
mEvaluatingWorker = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-14 22:23:16 +03:00
|
|
|
RefPtr<ServiceWorkerInfo> installing = mInstallingWorker.forget();
|
|
|
|
RefPtr<ServiceWorkerInfo> waiting = mWaitingWorker.forget();
|
|
|
|
RefPtr<ServiceWorkerInfo> active = mActiveWorker.forget();
|
|
|
|
|
|
|
|
UpdateRegistrationState();
|
|
|
|
|
|
|
|
if (installing) {
|
|
|
|
installing->UpdateState(ServiceWorkerState::Redundant);
|
|
|
|
installing->UpdateRedundantTime();
|
|
|
|
installing->WorkerPrivate()->NoteDeadServiceWorkerInfo();
|
2016-04-09 03:28:25 +03:00
|
|
|
// FIXME(nsm): Abort any inflight requests from installing worker.
|
|
|
|
}
|
|
|
|
|
2018-02-14 22:23:16 +03:00
|
|
|
if (waiting) {
|
|
|
|
waiting->UpdateState(ServiceWorkerState::Redundant);
|
|
|
|
waiting->UpdateRedundantTime();
|
|
|
|
waiting->WorkerPrivate()->NoteDeadServiceWorkerInfo();
|
2016-04-09 03:28:25 +03:00
|
|
|
}
|
|
|
|
|
2018-02-14 22:23:16 +03:00
|
|
|
if (active) {
|
|
|
|
active->UpdateState(ServiceWorkerState::Redundant);
|
|
|
|
active->UpdateRedundantTime();
|
|
|
|
active->WorkerPrivate()->NoteDeadServiceWorkerInfo();
|
2016-04-09 03:28:25 +03:00
|
|
|
}
|
2017-03-22 06:39:06 +03:00
|
|
|
|
|
|
|
NotifyChromeRegistrationListeners();
|
2016-04-09 03:28:25 +03:00
|
|
|
}
|
|
|
|
|
2017-08-16 09:18:52 +03:00
|
|
|
ServiceWorkerRegistrationInfo::ServiceWorkerRegistrationInfo(
|
|
|
|
const nsACString& aScope,
|
|
|
|
nsIPrincipal* aPrincipal,
|
|
|
|
ServiceWorkerUpdateViaCache aUpdateViaCache)
|
2018-01-31 19:29:49 +03:00
|
|
|
: mPrincipal(aPrincipal)
|
|
|
|
, mDescriptor(aPrincipal, aScope, aUpdateViaCache)
|
2018-01-31 19:29:49 +03:00
|
|
|
, mControlledClientsCounter(0)
|
2016-04-09 03:28:25 +03:00
|
|
|
, mUpdateState(NoUpdate)
|
2017-04-06 10:26:08 +03:00
|
|
|
, mCreationTime(PR_Now())
|
|
|
|
, mCreationTimeStamp(TimeStamp::Now())
|
|
|
|
, mLastUpdateTime(0)
|
2016-04-09 03:28:25 +03:00
|
|
|
, mPendingUninstall(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
ServiceWorkerRegistrationInfo::~ServiceWorkerRegistrationInfo()
|
|
|
|
{
|
2018-01-05 20:10:21 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsControllingClients());
|
2016-04-09 03:28:25 +03:00
|
|
|
}
|
|
|
|
|
2018-01-31 19:29:49 +03:00
|
|
|
const nsCString&
|
|
|
|
ServiceWorkerRegistrationInfo::Scope() const
|
|
|
|
{
|
2018-01-31 19:29:49 +03:00
|
|
|
return mDescriptor.Scope();
|
2018-01-31 19:29:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal*
|
|
|
|
ServiceWorkerRegistrationInfo::Principal() const
|
|
|
|
{
|
|
|
|
return mPrincipal;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceWorkerRegistrationInfo::IsPendingUninstall() const
|
|
|
|
{
|
|
|
|
return mPendingUninstall;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::SetPendingUninstall()
|
|
|
|
{
|
|
|
|
mPendingUninstall = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::ClearPendingUninstall()
|
|
|
|
{
|
|
|
|
mPendingUninstall = false;
|
|
|
|
}
|
|
|
|
|
2016-04-09 03:28:25 +03:00
|
|
|
NS_IMPL_ISUPPORTS(ServiceWorkerRegistrationInfo, nsIServiceWorkerRegistrationInfo)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerRegistrationInfo::GetPrincipal(nsIPrincipal** aPrincipal)
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-09 03:28:25 +03:00
|
|
|
NS_ADDREF(*aPrincipal = mPrincipal);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerRegistrationInfo::GetScope(nsAString& aScope)
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-01-31 19:29:49 +03:00
|
|
|
CopyUTF8toUTF16(Scope(), aScope);
|
2016-04-09 03:28:25 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerRegistrationInfo::GetScriptSpec(nsAString& aScriptSpec)
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-09 03:28:25 +03:00
|
|
|
RefPtr<ServiceWorkerInfo> newest = Newest();
|
|
|
|
if (newest) {
|
|
|
|
CopyUTF8toUTF16(newest->ScriptSpec(), aScriptSpec);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-08-16 09:21:29 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerRegistrationInfo::GetUpdateViaCache(uint16_t* aUpdateViaCache)
|
|
|
|
{
|
|
|
|
*aUpdateViaCache = static_cast<uint16_t>(GetUpdateViaCache());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-04-06 10:26:08 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerRegistrationInfo::GetLastUpdateTime(PRTime* _retval)
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-04-06 10:26:08 +03:00
|
|
|
MOZ_ASSERT(_retval);
|
|
|
|
*_retval = mLastUpdateTime;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-09 03:28:25 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerRegistrationInfo::GetInstallingWorker(nsIServiceWorkerInfo **aResult)
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-09 03:28:25 +03:00
|
|
|
nsCOMPtr<nsIServiceWorkerInfo> info = do_QueryInterface(mInstallingWorker);
|
|
|
|
info.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerRegistrationInfo::GetWaitingWorker(nsIServiceWorkerInfo **aResult)
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-09 03:28:25 +03:00
|
|
|
nsCOMPtr<nsIServiceWorkerInfo> info = do_QueryInterface(mWaitingWorker);
|
|
|
|
info.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerRegistrationInfo::GetActiveWorker(nsIServiceWorkerInfo **aResult)
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-09 03:28:25 +03:00
|
|
|
nsCOMPtr<nsIServiceWorkerInfo> info = do_QueryInterface(mActiveWorker);
|
|
|
|
info.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerRegistrationInfo::GetWorkerByID(uint64_t aID, nsIServiceWorkerInfo **aResult)
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-09 03:28:25 +03:00
|
|
|
MOZ_ASSERT(aResult);
|
|
|
|
|
|
|
|
RefPtr<ServiceWorkerInfo> info = GetServiceWorkerInfoById(aID);
|
|
|
|
// It is ok to return null for a missing service worker info.
|
|
|
|
info.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerRegistrationInfo::AddListener(
|
|
|
|
nsIServiceWorkerRegistrationInfoListener *aListener)
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-09 03:28:25 +03:00
|
|
|
|
|
|
|
if (!aListener || mListeners.Contains(aListener)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
mListeners.AppendElement(aListener);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerRegistrationInfo::RemoveListener(
|
|
|
|
nsIServiceWorkerRegistrationInfoListener *aListener)
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-09 03:28:25 +03:00
|
|
|
|
|
|
|
if (!aListener || !mListeners.Contains(aListener)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
mListeners.RemoveElement(aListener);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<ServiceWorkerInfo>
|
|
|
|
ServiceWorkerRegistrationInfo::GetServiceWorkerInfoById(uint64_t aId)
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-07-28 02:09:15 +03:00
|
|
|
|
2016-04-09 03:28:25 +03:00
|
|
|
RefPtr<ServiceWorkerInfo> serviceWorker;
|
2016-07-28 02:09:15 +03:00
|
|
|
if (mEvaluatingWorker && mEvaluatingWorker->ID() == aId) {
|
|
|
|
serviceWorker = mEvaluatingWorker;
|
|
|
|
} else if (mInstallingWorker && mInstallingWorker->ID() == aId) {
|
2016-04-09 03:28:25 +03:00
|
|
|
serviceWorker = mInstallingWorker;
|
|
|
|
} else if (mWaitingWorker && mWaitingWorker->ID() == aId) {
|
|
|
|
serviceWorker = mWaitingWorker;
|
|
|
|
} else if (mActiveWorker && mActiveWorker->ID() == aId) {
|
|
|
|
serviceWorker = mActiveWorker;
|
|
|
|
}
|
|
|
|
|
|
|
|
return serviceWorker.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::TryToActivateAsync()
|
|
|
|
{
|
2016-05-05 11:45:00 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
2017-06-12 22:34:10 +03:00
|
|
|
NS_DispatchToMainThread(NewRunnableMethod("ServiceWorkerRegistrationInfo::TryToActivate",
|
|
|
|
this,
|
2016-05-05 11:45:00 +03:00
|
|
|
&ServiceWorkerRegistrationInfo::TryToActivate)));
|
2016-04-09 03:28:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-07-28 03:36:10 +03:00
|
|
|
* TryToActivate should not be called directly, use TryToActivateAsync instead.
|
2016-04-09 03:28:25 +03:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::TryToActivate()
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-01-05 20:10:21 +03:00
|
|
|
bool controlling = IsControllingClients();
|
2016-07-28 03:36:10 +03:00
|
|
|
bool skipWaiting = mWaitingWorker && mWaitingWorker->SkipWaitingFlag();
|
2016-08-18 17:12:08 +03:00
|
|
|
bool idle = IsIdle();
|
2016-07-28 03:36:10 +03:00
|
|
|
if (idle && (!controlling || skipWaiting)) {
|
2016-04-09 03:28:25 +03:00
|
|
|
Activate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::Activate()
|
|
|
|
{
|
2016-04-17 14:29:53 +03:00
|
|
|
if (!mWaitingWorker) {
|
2016-04-09 03:28:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-13 01:00:36 +03:00
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
if (!swm) {
|
|
|
|
// browser shutdown began during async activation step
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-17 14:29:53 +03:00
|
|
|
TransitionWaitingToActive();
|
2016-04-09 03:28:25 +03:00
|
|
|
|
|
|
|
// FIXME(nsm): Unlink appcache if there is one.
|
|
|
|
|
|
|
|
// "Queue a task to fire a simple event named controllerchange..."
|
2017-12-12 23:44:48 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mActiveWorker);
|
|
|
|
swm->UpdateClientControllers(this);
|
2016-04-09 03:28:25 +03:00
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
nsCOMPtr<nsIRunnable> failRunnable = NewRunnableMethod<bool>(
|
2018-01-27 00:08:59 +03:00
|
|
|
"dom::ServiceWorkerRegistrationInfo::FinishActivate",
|
2017-06-12 22:34:10 +03:00
|
|
|
this,
|
|
|
|
&ServiceWorkerRegistrationInfo::FinishActivate,
|
|
|
|
false /* success */);
|
2016-04-09 03:28:25 +03:00
|
|
|
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> handle(
|
2017-06-14 04:27:17 +03:00
|
|
|
new nsMainThreadPtrHolder<ServiceWorkerRegistrationInfo>(
|
2017-10-27 20:45:52 +03:00
|
|
|
"ServiceWorkerRegistrationInfoProxy", this));
|
2016-04-09 03:28:25 +03:00
|
|
|
RefPtr<LifeCycleEventCallback> callback = new ContinueActivateRunnable(handle);
|
|
|
|
|
|
|
|
ServiceWorkerPrivate* workerPrivate = mActiveWorker->WorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
nsresult rv = workerPrivate->SendLifeCycleEvent(NS_LITERAL_STRING("activate"),
|
|
|
|
callback, failRunnable);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(failRunnable));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::FinishActivate(bool aSuccess)
|
|
|
|
{
|
|
|
|
if (mPendingUninstall || !mActiveWorker ||
|
|
|
|
mActiveWorker->State() != ServiceWorkerState::Activating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Activation never fails, so aSuccess is ignored.
|
|
|
|
mActiveWorker->UpdateState(ServiceWorkerState::Activated);
|
2017-04-06 10:26:08 +03:00
|
|
|
mActiveWorker->UpdateActivatedTime();
|
2018-01-31 19:29:49 +03:00
|
|
|
|
|
|
|
mDescriptor.SetWorkers(mInstallingWorker, mWaitingWorker, mActiveWorker);
|
|
|
|
|
2017-03-22 06:39:06 +03:00
|
|
|
NotifyChromeRegistrationListeners();
|
|
|
|
|
2016-04-09 03:28:25 +03:00
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
2017-01-13 01:00:36 +03:00
|
|
|
if (!swm) {
|
|
|
|
// browser shutdown started during async activation completion step
|
|
|
|
return;
|
|
|
|
}
|
2016-04-09 03:28:25 +03:00
|
|
|
swm->StoreRegistration(mPrincipal, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::RefreshLastUpdateCheckTime()
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-04-06 10:26:08 +03:00
|
|
|
|
|
|
|
mLastUpdateTime =
|
|
|
|
mCreationTime + static_cast<PRTime>((TimeStamp::Now() -
|
|
|
|
mCreationTimeStamp).ToMicroseconds());
|
|
|
|
NotifyChromeRegistrationListeners();
|
2016-04-09 03:28:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceWorkerRegistrationInfo::IsLastUpdateCheckTimeOverOneDay() const
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-09 03:28:25 +03:00
|
|
|
|
|
|
|
// For testing.
|
|
|
|
if (Preferences::GetBool("dom.serviceWorkers.testUpdateOverOneDay")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-06 05:39:52 +03:00
|
|
|
const int64_t kSecondsPerDay = 86400;
|
2017-10-10 23:50:36 +03:00
|
|
|
const int64_t nowMicros =
|
2017-04-06 05:39:52 +03:00
|
|
|
mCreationTime + static_cast<PRTime>((TimeStamp::Now() -
|
|
|
|
mCreationTimeStamp).ToMicroseconds());
|
2016-04-09 03:28:25 +03:00
|
|
|
|
2017-04-06 05:39:52 +03:00
|
|
|
// now < mLastUpdateTime if the system time is reset between storing
|
|
|
|
// and loading mLastUpdateTime from ServiceWorkerRegistrar.
|
2017-10-10 23:50:36 +03:00
|
|
|
if (nowMicros < mLastUpdateTime ||
|
|
|
|
(nowMicros - mLastUpdateTime) / PR_USEC_PER_SEC > kSecondsPerDay) {
|
2016-04-09 03:28:25 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-02-14 22:23:16 +03:00
|
|
|
ServiceWorkerRegistrationInfo::UpdateRegistrationState()
|
2016-04-09 03:28:25 +03:00
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-11-22 01:38:00 +03:00
|
|
|
|
2018-02-14 22:23:16 +03:00
|
|
|
mDescriptor.SetWorkers(mInstallingWorker, mWaitingWorker, mActiveWorker);
|
2016-04-17 14:29:53 +03:00
|
|
|
|
2018-02-14 22:23:16 +03:00
|
|
|
RefPtr<ServiceWorkerRegistrationInfo> self(this);
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"ServiceWorkerRegistrationInfo::UpdateRegistrationState",
|
|
|
|
[self = Move(self)] {
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
if (swm) {
|
|
|
|
swm->UpdateRegistrationListeners(self);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
|
2016-11-21 05:14:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::NotifyChromeRegistrationListeners()
|
|
|
|
{
|
2016-04-09 03:28:25 +03:00
|
|
|
nsTArray<nsCOMPtr<nsIServiceWorkerRegistrationInfoListener>> listeners(mListeners);
|
|
|
|
for (size_t index = 0; index < listeners.Length(); ++index) {
|
|
|
|
listeners[index]->OnChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::MaybeScheduleTimeCheckAndUpdate()
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-09 03:28:25 +03:00
|
|
|
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
if (!swm) {
|
|
|
|
// shutting down, do nothing
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mUpdateState == NoUpdate) {
|
|
|
|
mUpdateState = NeedTimeCheckAndUpdate;
|
|
|
|
}
|
|
|
|
|
2018-01-31 19:29:49 +03:00
|
|
|
swm->ScheduleUpdateTimer(mPrincipal, Scope());
|
2016-04-09 03:28:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::MaybeScheduleUpdate()
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-09 03:28:25 +03:00
|
|
|
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
if (!swm) {
|
|
|
|
// shutting down, do nothing
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mUpdateState = NeedUpdate;
|
|
|
|
|
2018-01-31 19:29:49 +03:00
|
|
|
swm->ScheduleUpdateTimer(mPrincipal, Scope());
|
2016-04-09 03:28:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceWorkerRegistrationInfo::CheckAndClearIfUpdateNeeded()
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-09 03:28:25 +03:00
|
|
|
|
|
|
|
bool result = mUpdateState == NeedUpdate ||
|
|
|
|
(mUpdateState == NeedTimeCheckAndUpdate &&
|
|
|
|
IsLastUpdateCheckTimeOverOneDay());
|
|
|
|
|
|
|
|
mUpdateState = NoUpdate;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-07-28 02:09:15 +03:00
|
|
|
ServiceWorkerInfo*
|
|
|
|
ServiceWorkerRegistrationInfo::GetEvaluating() const
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-07-28 02:09:15 +03:00
|
|
|
return mEvaluatingWorker;
|
|
|
|
}
|
|
|
|
|
2016-04-17 14:29:53 +03:00
|
|
|
ServiceWorkerInfo*
|
|
|
|
ServiceWorkerRegistrationInfo::GetInstalling() const
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-17 14:29:53 +03:00
|
|
|
return mInstallingWorker;
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceWorkerInfo*
|
|
|
|
ServiceWorkerRegistrationInfo::GetWaiting() const
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-17 14:29:53 +03:00
|
|
|
return mWaitingWorker;
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceWorkerInfo*
|
|
|
|
ServiceWorkerRegistrationInfo::GetActive() const
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-17 14:29:53 +03:00
|
|
|
return mActiveWorker;
|
|
|
|
}
|
|
|
|
|
2018-02-19 23:06:19 +03:00
|
|
|
ServiceWorkerInfo*
|
|
|
|
ServiceWorkerRegistrationInfo::GetByDescriptor(const ServiceWorkerDescriptor& aDescriptor) const
|
|
|
|
{
|
|
|
|
if (mActiveWorker && mActiveWorker->Descriptor().Matches(aDescriptor)) {
|
|
|
|
return mActiveWorker;
|
|
|
|
}
|
|
|
|
if (mWaitingWorker && mWaitingWorker->Descriptor().Matches(aDescriptor)) {
|
|
|
|
return mWaitingWorker;
|
|
|
|
}
|
|
|
|
if (mInstallingWorker && mInstallingWorker->Descriptor().Matches(aDescriptor)) {
|
|
|
|
return mInstallingWorker;
|
|
|
|
}
|
|
|
|
if (mEvaluatingWorker && mEvaluatingWorker->Descriptor().Matches(aDescriptor)) {
|
|
|
|
return mEvaluatingWorker;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-07-28 02:09:15 +03:00
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::SetEvaluating(ServiceWorkerInfo* aServiceWorker)
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-07-28 02:09:15 +03:00
|
|
|
MOZ_ASSERT(aServiceWorker);
|
|
|
|
MOZ_ASSERT(!mEvaluatingWorker);
|
|
|
|
MOZ_ASSERT(!mInstallingWorker);
|
|
|
|
MOZ_ASSERT(mWaitingWorker != aServiceWorker);
|
|
|
|
MOZ_ASSERT(mActiveWorker != aServiceWorker);
|
|
|
|
|
|
|
|
mEvaluatingWorker = aServiceWorker;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::ClearEvaluating()
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-07-28 02:09:15 +03:00
|
|
|
|
|
|
|
if (!mEvaluatingWorker) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mEvaluatingWorker->UpdateState(ServiceWorkerState::Redundant);
|
2017-04-06 10:26:08 +03:00
|
|
|
// We don't update the redundant time for the sw here, since we've not expose
|
|
|
|
// evalutingWorker yet.
|
2016-07-28 02:09:15 +03:00
|
|
|
mEvaluatingWorker = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-17 14:29:53 +03:00
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::ClearInstalling()
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-17 14:29:53 +03:00
|
|
|
|
|
|
|
if (!mInstallingWorker) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-14 22:23:16 +03:00
|
|
|
RefPtr<ServiceWorkerInfo> installing = mInstallingWorker.forget();
|
2017-03-22 06:39:06 +03:00
|
|
|
|
2018-02-14 22:23:16 +03:00
|
|
|
UpdateRegistrationState();
|
|
|
|
|
|
|
|
installing->UpdateState(ServiceWorkerState::Redundant);
|
|
|
|
installing->UpdateRedundantTime();
|
2018-01-31 19:29:49 +03:00
|
|
|
|
2017-03-22 06:39:06 +03:00
|
|
|
NotifyChromeRegistrationListeners();
|
2016-04-17 14:29:53 +03:00
|
|
|
}
|
|
|
|
|
2016-04-17 14:29:53 +03:00
|
|
|
void
|
2016-07-28 02:09:15 +03:00
|
|
|
ServiceWorkerRegistrationInfo::TransitionEvaluatingToInstalling()
|
2016-04-17 14:29:53 +03:00
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-07-28 02:09:15 +03:00
|
|
|
MOZ_ASSERT(mEvaluatingWorker);
|
2016-04-17 14:29:53 +03:00
|
|
|
MOZ_ASSERT(!mInstallingWorker);
|
|
|
|
|
2016-07-28 02:09:15 +03:00
|
|
|
mInstallingWorker = mEvaluatingWorker.forget();
|
2018-01-31 19:29:49 +03:00
|
|
|
|
2018-02-14 22:23:16 +03:00
|
|
|
UpdateRegistrationState();
|
|
|
|
|
|
|
|
mInstallingWorker->UpdateState(ServiceWorkerState::Installing);
|
2018-01-31 19:29:49 +03:00
|
|
|
|
2016-11-21 05:14:54 +03:00
|
|
|
NotifyChromeRegistrationListeners();
|
2016-04-17 14:29:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-04-17 14:29:53 +03:00
|
|
|
ServiceWorkerRegistrationInfo::TransitionInstallingToWaiting()
|
2016-04-17 14:29:53 +03:00
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-17 14:29:53 +03:00
|
|
|
MOZ_ASSERT(mInstallingWorker);
|
|
|
|
|
|
|
|
if (mWaitingWorker) {
|
|
|
|
MOZ_ASSERT(mInstallingWorker->CacheName() != mWaitingWorker->CacheName());
|
|
|
|
mWaitingWorker->UpdateState(ServiceWorkerState::Redundant);
|
2017-04-06 10:26:08 +03:00
|
|
|
mWaitingWorker->UpdateRedundantTime();
|
2016-04-17 14:29:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mWaitingWorker = mInstallingWorker.forget();
|
2018-02-14 22:23:16 +03:00
|
|
|
|
|
|
|
UpdateRegistrationState();
|
|
|
|
|
2016-04-17 14:29:53 +03:00
|
|
|
mWaitingWorker->UpdateState(ServiceWorkerState::Installed);
|
2017-04-06 10:26:08 +03:00
|
|
|
mWaitingWorker->UpdateInstalledTime();
|
2018-01-31 19:29:49 +03:00
|
|
|
|
2017-03-22 06:39:06 +03:00
|
|
|
NotifyChromeRegistrationListeners();
|
2016-04-17 14:29:53 +03:00
|
|
|
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
2017-01-13 01:00:36 +03:00
|
|
|
if (!swm) {
|
|
|
|
// browser shutdown began
|
|
|
|
return;
|
|
|
|
}
|
2016-04-17 14:29:53 +03:00
|
|
|
swm->StoreRegistration(mPrincipal, this);
|
2016-04-17 14:29:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::SetActive(ServiceWorkerInfo* aServiceWorker)
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-17 14:29:53 +03:00
|
|
|
MOZ_ASSERT(aServiceWorker);
|
|
|
|
|
|
|
|
// TODO: Assert installing, waiting, and active are nullptr once the SWM
|
|
|
|
// moves to the parent process. After that happens this code will
|
|
|
|
// only run for browser initialization and not for cross-process
|
|
|
|
// overrides.
|
|
|
|
MOZ_ASSERT(mInstallingWorker != aServiceWorker);
|
|
|
|
MOZ_ASSERT(mWaitingWorker != aServiceWorker);
|
|
|
|
MOZ_ASSERT(mActiveWorker != aServiceWorker);
|
|
|
|
|
|
|
|
if (mActiveWorker) {
|
|
|
|
MOZ_ASSERT(aServiceWorker->CacheName() != mActiveWorker->CacheName());
|
|
|
|
mActiveWorker->UpdateState(ServiceWorkerState::Redundant);
|
2017-04-06 10:26:08 +03:00
|
|
|
mActiveWorker->UpdateRedundantTime();
|
2016-04-17 14:29:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The active worker is being overriden due to initial load or
|
|
|
|
// another process activating a worker. Move straight to the
|
|
|
|
// Activated state.
|
2016-04-17 14:29:53 +03:00
|
|
|
mActiveWorker = aServiceWorker;
|
2016-04-17 14:29:53 +03:00
|
|
|
mActiveWorker->SetActivateStateUncheckedWithoutEvent(ServiceWorkerState::Activated);
|
2018-02-14 22:23:16 +03:00
|
|
|
|
2017-04-06 10:26:08 +03:00
|
|
|
// We don't need to update activated time when we load registration from
|
|
|
|
// registrar.
|
2018-02-14 22:23:16 +03:00
|
|
|
UpdateRegistrationState();
|
2018-01-31 19:29:49 +03:00
|
|
|
|
2017-03-22 06:39:06 +03:00
|
|
|
NotifyChromeRegistrationListeners();
|
2016-04-17 14:29:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::TransitionWaitingToActive()
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-17 14:29:53 +03:00
|
|
|
MOZ_ASSERT(mWaitingWorker);
|
|
|
|
|
|
|
|
if (mActiveWorker) {
|
|
|
|
MOZ_ASSERT(mWaitingWorker->CacheName() != mActiveWorker->CacheName());
|
|
|
|
mActiveWorker->UpdateState(ServiceWorkerState::Redundant);
|
2017-04-06 10:26:08 +03:00
|
|
|
mActiveWorker->UpdateRedundantTime();
|
2016-04-17 14:29:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We are transitioning from waiting to active normally, so go to
|
|
|
|
// the activating state.
|
|
|
|
mActiveWorker = mWaitingWorker.forget();
|
2018-02-14 22:23:16 +03:00
|
|
|
|
|
|
|
UpdateRegistrationState();
|
|
|
|
|
2016-04-17 14:29:53 +03:00
|
|
|
mActiveWorker->UpdateState(ServiceWorkerState::Activating);
|
2018-01-31 19:29:49 +03:00
|
|
|
|
2018-02-14 22:23:16 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r =
|
|
|
|
NS_NewRunnableFunction("ServiceWorkerRegistrationInfo::TransitionWaitingToActive",
|
|
|
|
[] {
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
if (swm) {
|
|
|
|
swm->CheckPendingReadyPromises();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
|
2018-01-31 19:29:49 +03:00
|
|
|
|
2017-03-22 06:39:06 +03:00
|
|
|
NotifyChromeRegistrationListeners();
|
2016-04-17 14:29:53 +03:00
|
|
|
}
|
|
|
|
|
2016-08-18 17:12:08 +03:00
|
|
|
bool
|
|
|
|
ServiceWorkerRegistrationInfo::IsIdle() const
|
|
|
|
{
|
|
|
|
return !mActiveWorker || mActiveWorker->WorkerPrivate()->IsIdle();
|
|
|
|
}
|
|
|
|
|
2017-08-16 09:18:52 +03:00
|
|
|
ServiceWorkerUpdateViaCache
|
|
|
|
ServiceWorkerRegistrationInfo::GetUpdateViaCache() const
|
2017-01-04 12:08:43 +03:00
|
|
|
{
|
2018-01-31 19:29:49 +03:00
|
|
|
return mDescriptor.UpdateViaCache();
|
2017-01-04 12:08:43 +03:00
|
|
|
}
|
|
|
|
|
2017-01-04 12:08:51 +03:00
|
|
|
void
|
2017-08-16 09:18:52 +03:00
|
|
|
ServiceWorkerRegistrationInfo::SetUpdateViaCache(
|
|
|
|
ServiceWorkerUpdateViaCache aUpdateViaCache)
|
2017-01-04 12:08:51 +03:00
|
|
|
{
|
2018-01-31 19:29:49 +03:00
|
|
|
mDescriptor.SetUpdateViaCache(aUpdateViaCache);
|
2018-02-14 22:23:16 +03:00
|
|
|
UpdateRegistrationState();
|
2017-01-04 12:08:51 +03:00
|
|
|
}
|
|
|
|
|
2017-04-05 04:59:48 +03:00
|
|
|
int64_t
|
|
|
|
ServiceWorkerRegistrationInfo::GetLastUpdateTime() const
|
|
|
|
{
|
|
|
|
return mLastUpdateTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistrationInfo::SetLastUpdateTime(const int64_t aTime)
|
|
|
|
{
|
|
|
|
if (aTime == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLastUpdateTime = aTime;
|
|
|
|
}
|
|
|
|
|
2018-01-31 19:29:49 +03:00
|
|
|
const ServiceWorkerRegistrationDescriptor&
|
|
|
|
ServiceWorkerRegistrationInfo::Descriptor() const
|
|
|
|
{
|
|
|
|
return mDescriptor;
|
|
|
|
}
|
|
|
|
|
2018-01-27 00:08:59 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|