Bug 1434342 P1 Add ServiceWorker::Create() factory method. r=asuth

This commit is contained in:
Ben Kelly 2018-01-31 09:10:25 -08:00
parent 99623eaeaa
commit 231fc8a398
3 changed files with 36 additions and 7 deletions

View File

@ -38,9 +38,36 @@ ServiceWorkerVisible(JSContext* aCx, JSObject* aObj)
return IS_INSTANCE_OF(ServiceWorkerGlobalScope, aObj);
}
ServiceWorker::ServiceWorker(nsPIDOMWindowInner* aWindow,
// static
already_AddRefed<ServiceWorker>
ServiceWorker::Create(nsIGlobalObject* aOwner,
const ServiceWorkerDescriptor& aDescriptor)
{
RefPtr<ServiceWorker> ref;
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
if (!swm) {
return ref.forget();
}
RefPtr<ServiceWorkerRegistrationInfo> reg =
swm->GetRegistration(aDescriptor.PrincipalInfo(), aDescriptor.Scope());
if (!reg) {
return ref.forget();
}
RefPtr<ServiceWorkerInfo> info = reg->GetByID(aDescriptor.Id());
if (!info) {
return ref.forget();
}
ref = new ServiceWorker(aOwner, info);
return ref.forget();
}
ServiceWorker::ServiceWorker(nsIGlobalObject* aGlobal,
ServiceWorkerInfo* aInfo)
: DOMEventTargetHelper(aWindow),
: DOMEventTargetHelper(aGlobal),
mInfo(aInfo)
{
MOZ_ASSERT(NS_IsMainThread());

View File

@ -11,7 +11,7 @@
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/ServiceWorkerBinding.h" // For ServiceWorkerState.
class nsPIDOMWindowInner;
class nsIGlobalObject;
namespace mozilla {
namespace dom {
@ -25,13 +25,15 @@ ServiceWorkerVisible(JSContext* aCx, JSObject* aObj);
class ServiceWorker final : public DOMEventTargetHelper
{
friend class ServiceWorkerInfo;
public:
NS_DECL_ISUPPORTS_INHERITED
IMPL_EVENT_HANDLER(statechange)
IMPL_EVENT_HANDLER(error)
static already_AddRefed<ServiceWorker>
Create(nsIGlobalObject* aOwner, const ServiceWorkerDescriptor& aDescriptor);
virtual JSObject*
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
@ -65,8 +67,7 @@ public:
const Sequence<JSObject*>& aTransferable, ErrorResult& aRv);
private:
// This class can only be created from ServiceWorkerInfo::GetOrCreateInstance().
ServiceWorker(nsPIDOMWindowInner* aWindow, ServiceWorkerInfo* aInfo);
ServiceWorker(nsIGlobalObject* aWindow, ServiceWorkerInfo* aInfo);
// This class is reference-counted and will be destroyed from Release().
~ServiceWorker();

View File

@ -277,7 +277,8 @@ ServiceWorkerInfo::GetOrCreateInstance(nsPIDOMWindowInner* aWindow)
}
if (!ref) {
ref = new ServiceWorker(aWindow, this);
nsCOMPtr<nsIGlobalObject> global(do_QueryInterface(aWindow));
ref = ServiceWorker::Create(global, mDescriptor);
}
return ref.forget();