2013-11-19 15:14:07 -08:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* 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 "ServiceWorker.h"
|
|
|
|
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "SharedWorker.h"
|
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
|
2014-11-06 07:57:57 -08:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-11-19 15:14:07 -08:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2014-11-06 07:57:57 -08:00
|
|
|
#include "mozilla/dom/ServiceWorkerGlobalScopeBinding.h"
|
2013-11-19 15:14:07 -08:00
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
2014-11-06 07:57:57 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace workers {
|
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceWorkerVisible(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return Preferences::GetBool("dom.serviceWorkers.enabled", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceWorkerGlobalScope* scope = nullptr;
|
|
|
|
nsresult rv = UnwrapObject<prototypes::id::ServiceWorkerGlobalScope_workers,
|
|
|
|
mozilla::dom::ServiceWorkerGlobalScopeBinding_workers::NativeType>(aObj, scope);
|
|
|
|
return NS_SUCCEEDED(rv);
|
|
|
|
}
|
2013-11-19 15:14:07 -08:00
|
|
|
|
|
|
|
ServiceWorker::ServiceWorker(nsPIDOMWindow* aWindow,
|
|
|
|
SharedWorker* aSharedWorker)
|
|
|
|
: DOMEventTargetHelper(aWindow),
|
2014-08-14 08:35:21 -07:00
|
|
|
mState(ServiceWorkerState::Installing),
|
2013-11-19 15:14:07 -08:00
|
|
|
mSharedWorker(aSharedWorker)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mSharedWorker);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceWorker::~ServiceWorker()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ServiceWorker, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ServiceWorker, DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ServiceWorker)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(ServiceWorker, DOMEventTargetHelper,
|
|
|
|
mSharedWorker)
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
ServiceWorker::WrapObject(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
return ServiceWorkerBinding::Wrap(aCx, this);
|
|
|
|
}
|
2013-11-19 15:15:02 -08:00
|
|
|
|
|
|
|
WorkerPrivate*
|
|
|
|
ServiceWorker::GetWorkerPrivate() const
|
|
|
|
{
|
|
|
|
// At some point in the future, this may be optimized to terminate a worker
|
|
|
|
// that hasn't been used in a certain amount of time or when there is memory
|
|
|
|
// pressure or similar.
|
|
|
|
MOZ_ASSERT(mSharedWorker);
|
|
|
|
return mSharedWorker->GetWorkerPrivate();
|
|
|
|
}
|
2014-11-06 07:57:57 -08:00
|
|
|
|
|
|
|
} // namespace workers
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|