2014-08-19 17:56:00 +04: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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_ServiceWorkerRegistration_h
|
|
|
|
#define mozilla_dom_ServiceWorkerRegistration_h
|
|
|
|
|
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2015-01-26 21:31:38 +03:00
|
|
|
#include "mozilla/dom/ServiceWorkerBinding.h"
|
2014-08-19 17:56:00 +04:00
|
|
|
#include "mozilla/dom/ServiceWorkerCommon.h"
|
2015-04-09 01:22:36 +03:00
|
|
|
#include "mozilla/dom/workers/bindings/WorkerFeature.h"
|
2014-08-19 17:56:00 +04:00
|
|
|
|
2015-06-26 04:50:24 +03:00
|
|
|
// Support for Notification API extension.
|
|
|
|
#include "mozilla/dom/NotificationBinding.h"
|
|
|
|
|
2014-08-19 17:56:00 +04:00
|
|
|
class nsPIDOMWindow;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class Promise;
|
2015-04-11 06:19:28 +03:00
|
|
|
class PushManager;
|
2015-07-23 18:30:15 +03:00
|
|
|
class WorkerPushManager;
|
2015-04-09 01:22:36 +03:00
|
|
|
class WorkerListener;
|
2014-08-19 17:56:00 +04:00
|
|
|
|
|
|
|
namespace workers {
|
|
|
|
class ServiceWorker;
|
2015-04-09 01:22:36 +03:00
|
|
|
class WorkerPrivate;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace workers
|
2014-08-19 17:56:00 +04:00
|
|
|
|
2015-04-04 08:46:07 +03:00
|
|
|
bool
|
|
|
|
ServiceWorkerRegistrationVisible(JSContext* aCx, JSObject* aObj);
|
|
|
|
|
2015-09-15 22:58:32 +03:00
|
|
|
bool
|
|
|
|
ServiceWorkerNotificationAPIVisible(JSContext* aCx, JSObject* aObj);
|
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
// This class exists solely so that we can satisfy some WebIDL Func= attribute
|
|
|
|
// constraints. Func= converts the function name to a header file to include, in
|
|
|
|
// this case "ServiceWorkerRegistration.h".
|
|
|
|
class ServiceWorkerRegistration final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Something that we can feed into the Func webidl property to ensure that
|
|
|
|
// SetScope is never exposed to the user.
|
|
|
|
static bool
|
|
|
|
WebPushMethodHider(JSContext* unusedContext, JSObject* unusedObject) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-04-08 23:13:32 +03:00
|
|
|
// Used by ServiceWorkerManager to notify ServiceWorkerRegistrations of
|
|
|
|
// updatefound event and invalidating ServiceWorker instances.
|
|
|
|
class ServiceWorkerRegistrationListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType) AddRef() = 0;
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType) Release() = 0;
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
UpdateFound() = 0;
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
InvalidateWorkers(WhichServiceWorker aWhichOnes) = 0;
|
|
|
|
|
2015-11-23 21:38:08 +03:00
|
|
|
virtual void
|
|
|
|
RegistrationRemoved() = 0;
|
|
|
|
|
2015-04-08 23:13:32 +03:00
|
|
|
virtual void
|
|
|
|
GetScope(nsAString& aScope) const = 0;
|
|
|
|
};
|
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
class ServiceWorkerRegistrationBase : public DOMEventTargetHelper
|
2014-08-19 17:56:00 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
IMPL_EVENT_HANDLER(updatefound)
|
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
ServiceWorkerRegistrationBase(nsPIDOMWindow* aWindow,
|
|
|
|
const nsAString& aScope);
|
2014-08-19 17:56:00 +04:00
|
|
|
|
|
|
|
JSObject*
|
2015-04-04 01:18:55 +03:00
|
|
|
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override = 0;
|
2014-08-19 17:56:00 +04:00
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
virtual already_AddRefed<workers::ServiceWorker>
|
|
|
|
GetInstalling() = 0;
|
2014-08-19 17:56:00 +04:00
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
virtual already_AddRefed<workers::ServiceWorker>
|
|
|
|
GetWaiting() = 0;
|
2014-08-19 17:56:00 +04:00
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
virtual already_AddRefed<workers::ServiceWorker>
|
|
|
|
GetActive() = 0;
|
2014-08-19 17:56:00 +04:00
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
protected:
|
2015-04-07 23:09:07 +03:00
|
|
|
virtual ~ServiceWorkerRegistrationBase()
|
|
|
|
{ }
|
2015-04-04 01:18:55 +03:00
|
|
|
|
|
|
|
const nsString mScope;
|
|
|
|
};
|
|
|
|
|
2015-04-08 23:13:32 +03:00
|
|
|
class ServiceWorkerRegistrationMainThread final : public ServiceWorkerRegistrationBase,
|
|
|
|
public ServiceWorkerRegistrationListener
|
2015-04-04 01:18:55 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorkerRegistrationMainThread,
|
|
|
|
ServiceWorkerRegistrationBase)
|
|
|
|
|
2015-08-15 01:06:00 +03:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
Update(ErrorResult& aRv);
|
2015-03-20 04:09:10 +03:00
|
|
|
|
2014-08-19 17:56:00 +04:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
Unregister(ErrorResult& aRv);
|
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
JSObject*
|
|
|
|
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2014-08-19 17:56:00 +04:00
|
|
|
|
2015-06-26 04:50:24 +03:00
|
|
|
// Partial interface from Notification API.
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
ShowNotification(JSContext* aCx,
|
|
|
|
const nsAString& aTitle,
|
2015-06-26 04:50:24 +03:00
|
|
|
const NotificationOptions& aOptions,
|
|
|
|
ErrorResult& aRv);
|
2015-06-26 04:50:24 +03:00
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2015-06-26 04:50:24 +03:00
|
|
|
GetNotifications(const GetNotificationOptions& aOptions, ErrorResult& aRv);
|
2015-06-26 04:50:24 +03:00
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
already_AddRefed<workers::ServiceWorker>
|
|
|
|
GetInstalling() override;
|
2014-08-26 12:17:36 +04:00
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
already_AddRefed<workers::ServiceWorker>
|
|
|
|
GetWaiting() override;
|
2015-05-20 19:55:07 +03:00
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
already_AddRefed<workers::ServiceWorker>
|
|
|
|
GetActive() override;
|
2015-05-20 19:55:07 +03:00
|
|
|
|
2015-04-11 06:19:28 +03:00
|
|
|
already_AddRefed<PushManager>
|
|
|
|
GetPushManager(ErrorResult& aRv);
|
|
|
|
|
2015-04-07 23:09:07 +03:00
|
|
|
// DOMEventTargethelper
|
|
|
|
void DisconnectFromOwner() override
|
|
|
|
{
|
|
|
|
StopListeningForEvents();
|
|
|
|
ServiceWorkerRegistrationBase::DisconnectFromOwner();
|
|
|
|
}
|
|
|
|
|
2015-04-08 23:13:32 +03:00
|
|
|
// ServiceWorkerRegistrationListener
|
|
|
|
void
|
|
|
|
UpdateFound() override;
|
|
|
|
|
|
|
|
void
|
|
|
|
InvalidateWorkers(WhichServiceWorker aWhichOnes) override;
|
|
|
|
|
2015-11-23 21:38:08 +03:00
|
|
|
void
|
|
|
|
RegistrationRemoved() override;
|
|
|
|
|
2015-04-08 23:13:32 +03:00
|
|
|
void
|
|
|
|
GetScope(nsAString& aScope) const override
|
|
|
|
{
|
|
|
|
aScope = mScope;
|
|
|
|
}
|
|
|
|
|
2014-08-19 17:56:00 +04:00
|
|
|
private:
|
2015-11-23 21:38:08 +03:00
|
|
|
friend nsPIDOMWindow;
|
|
|
|
ServiceWorkerRegistrationMainThread(nsPIDOMWindow* aWindow,
|
|
|
|
const nsAString& aScope);
|
2015-04-04 08:46:07 +03:00
|
|
|
~ServiceWorkerRegistrationMainThread();
|
2014-08-19 17:56:00 +04:00
|
|
|
|
|
|
|
already_AddRefed<workers::ServiceWorker>
|
|
|
|
GetWorkerReference(WhichServiceWorker aWhichOne);
|
|
|
|
|
2015-04-07 23:09:07 +03:00
|
|
|
void
|
|
|
|
StartListeningForEvents();
|
|
|
|
|
|
|
|
void
|
|
|
|
StopListeningForEvents();
|
|
|
|
|
|
|
|
bool mListeningForEvents;
|
|
|
|
|
2014-08-19 17:56:00 +04:00
|
|
|
// The following properties are cached here to ensure JS equality is satisfied
|
|
|
|
// instead of acquiring a new worker instance from the ServiceWorkerManager
|
|
|
|
// for every access. A null value is considered a cache miss.
|
|
|
|
// These three may change to a new worker at any time.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<workers::ServiceWorker> mInstallingWorker;
|
|
|
|
RefPtr<workers::ServiceWorker> mWaitingWorker;
|
|
|
|
RefPtr<workers::ServiceWorker> mActiveWorker;
|
2014-08-19 17:56:00 +04:00
|
|
|
|
2015-04-11 06:19:28 +03:00
|
|
|
#ifndef MOZ_SIMPLEPUSH
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PushManager> mPushManager;
|
2015-04-11 06:19:28 +03:00
|
|
|
#endif
|
2015-04-04 01:18:55 +03:00
|
|
|
};
|
2015-04-11 06:19:28 +03:00
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
class ServiceWorkerRegistrationWorkerThread final : public ServiceWorkerRegistrationBase
|
2015-04-09 01:22:36 +03:00
|
|
|
, public workers::WorkerFeature
|
2015-04-04 01:18:55 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorkerRegistrationWorkerThread,
|
|
|
|
ServiceWorkerRegistrationBase)
|
|
|
|
|
2015-04-09 01:22:36 +03:00
|
|
|
ServiceWorkerRegistrationWorkerThread(workers::WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsAString& aScope);
|
2015-04-04 01:18:55 +03:00
|
|
|
|
2015-08-15 01:06:00 +03:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
Update(ErrorResult& aRv);
|
2015-04-04 01:18:55 +03:00
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2015-04-08 00:17:02 +03:00
|
|
|
Unregister(ErrorResult& aRv);
|
2015-04-04 01:18:55 +03:00
|
|
|
|
|
|
|
JSObject*
|
|
|
|
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
|
|
|
|
2015-06-26 04:50:24 +03:00
|
|
|
// Partial interface from Notification API.
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
ShowNotification(JSContext* aCx,
|
|
|
|
const nsAString& aTitle,
|
2015-06-26 04:50:24 +03:00
|
|
|
const NotificationOptions& aOptions,
|
|
|
|
ErrorResult& aRv);
|
2015-06-26 04:50:24 +03:00
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2015-06-26 04:50:24 +03:00
|
|
|
GetNotifications(const GetNotificationOptions& aOptions, ErrorResult& aRv);
|
2015-06-26 04:50:24 +03:00
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
already_AddRefed<workers::ServiceWorker>
|
|
|
|
GetInstalling() override;
|
|
|
|
|
|
|
|
already_AddRefed<workers::ServiceWorker>
|
|
|
|
GetWaiting() override;
|
|
|
|
|
|
|
|
already_AddRefed<workers::ServiceWorker>
|
|
|
|
GetActive() override;
|
|
|
|
|
|
|
|
void
|
2015-04-08 23:13:32 +03:00
|
|
|
GetScope(nsAString& aScope) const
|
|
|
|
{
|
|
|
|
aScope = mScope;
|
|
|
|
}
|
2015-04-04 01:18:55 +03:00
|
|
|
|
2015-04-09 01:22:36 +03:00
|
|
|
bool
|
2015-04-18 02:44:58 +03:00
|
|
|
Notify(JSContext* aCx, workers::Status aStatus) override;
|
2015-04-09 01:22:36 +03:00
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
already_AddRefed<WorkerPushManager>
|
|
|
|
GetPushManager(ErrorResult& aRv);
|
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
private:
|
2015-04-09 01:22:36 +03:00
|
|
|
enum Reason
|
|
|
|
{
|
|
|
|
RegistrationIsGoingAway = 0,
|
|
|
|
WorkerIsGoingAway,
|
|
|
|
};
|
|
|
|
|
|
|
|
~ServiceWorkerRegistrationWorkerThread();
|
|
|
|
|
|
|
|
void
|
|
|
|
InitListener();
|
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseListener(Reason aReason);
|
2015-04-04 01:18:55 +03:00
|
|
|
|
2015-04-09 01:22:36 +03:00
|
|
|
workers::WorkerPrivate* mWorkerPrivate;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerListener> mListener;
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
#ifndef MOZ_SIMPLEPUSH
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerPushManager> mPushManager;
|
2015-07-23 18:30:15 +03:00
|
|
|
#endif
|
2014-08-19 17:56:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-04-04 01:18:55 +03:00
|
|
|
#endif /* mozilla_dom_ServiceWorkerRegistration_h */
|