mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-28 15:23:51 +00:00
Bug 1434342 P1 Add ServiceWorker::Create() factory method. r=asuth
This commit is contained in:
parent
99623eaeaa
commit
231fc8a398
@ -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());
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user