ServiceWorkerContainer.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_serviceworkercontainer_h__
  6. #define mozilla_dom_serviceworkercontainer_h__
  7. #include "mozilla/DOMEventTargetHelper.h"
  8. class nsPIDOMWindowInner;
  9. namespace mozilla {
  10. namespace dom {
  11. class Promise;
  12. struct RegistrationOptions;
  13. namespace workers {
  14. class ServiceWorker;
  15. } // namespace workers
  16. // Lightweight serviceWorker APIs collection.
  17. class ServiceWorkerContainer final : public DOMEventTargetHelper
  18. {
  19. public:
  20. NS_DECL_ISUPPORTS_INHERITED
  21. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorkerContainer, DOMEventTargetHelper)
  22. IMPL_EVENT_HANDLER(controllerchange)
  23. IMPL_EVENT_HANDLER(error)
  24. IMPL_EVENT_HANDLER(message)
  25. static bool IsEnabled(JSContext* aCx, JSObject* aGlobal);
  26. explicit ServiceWorkerContainer(nsPIDOMWindowInner* aWindow);
  27. virtual JSObject*
  28. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  29. already_AddRefed<Promise>
  30. Register(const nsAString& aScriptURL,
  31. const RegistrationOptions& aOptions,
  32. ErrorResult& aRv);
  33. already_AddRefed<workers::ServiceWorker>
  34. GetController();
  35. already_AddRefed<Promise>
  36. GetRegistration(const nsAString& aDocumentURL,
  37. ErrorResult& aRv);
  38. already_AddRefed<Promise>
  39. GetRegistrations(ErrorResult& aRv);
  40. Promise*
  41. GetReady(ErrorResult& aRv);
  42. // Testing only.
  43. void
  44. GetScopeForUrl(const nsAString& aUrl, nsString& aScope, ErrorResult& aRv);
  45. // DOMEventTargetHelper
  46. void DisconnectFromOwner() override;
  47. // Invalidates |mControllerWorker| and dispatches a "controllerchange"
  48. // event.
  49. void
  50. ControllerChanged(ErrorResult& aRv);
  51. private:
  52. ~ServiceWorkerContainer();
  53. void RemoveReadyPromise();
  54. // This only changes when a worker hijacks everything in its scope by calling
  55. // claim.
  56. RefPtr<workers::ServiceWorker> mControllerWorker;
  57. RefPtr<Promise> mReadyPromise;
  58. };
  59. } // namespace dom
  60. } // namespace mozilla
  61. #endif /* mozilla_dom_workers_serviceworkercontainer_h__ */