ServiceWorker.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_workers_serviceworker_h__
  6. #define mozilla_dom_workers_serviceworker_h__
  7. #include "mozilla/DOMEventTargetHelper.h"
  8. #include "mozilla/dom/BindingDeclarations.h"
  9. #include "mozilla/dom/ServiceWorkerBinding.h" // For ServiceWorkerState.
  10. class nsPIDOMWindowInner;
  11. namespace mozilla {
  12. namespace dom {
  13. namespace workers {
  14. class ServiceWorkerInfo;
  15. class ServiceWorkerManager;
  16. class SharedWorker;
  17. bool
  18. ServiceWorkerVisible(JSContext* aCx, JSObject* aObj);
  19. class ServiceWorker final : public DOMEventTargetHelper
  20. {
  21. friend class ServiceWorkerInfo;
  22. public:
  23. NS_DECL_ISUPPORTS_INHERITED
  24. IMPL_EVENT_HANDLER(statechange)
  25. IMPL_EVENT_HANDLER(error)
  26. virtual JSObject*
  27. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  28. ServiceWorkerState
  29. State() const
  30. {
  31. return mState;
  32. }
  33. void
  34. SetState(ServiceWorkerState aState)
  35. {
  36. mState = aState;
  37. }
  38. void
  39. GetScriptURL(nsString& aURL) const;
  40. void
  41. DispatchStateChange(ServiceWorkerState aState)
  42. {
  43. DOMEventTargetHelper::DispatchTrustedEvent(NS_LITERAL_STRING("statechange"));
  44. }
  45. #ifdef XP_WIN
  46. #undef PostMessage
  47. #endif
  48. void
  49. PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
  50. const Optional<Sequence<JS::Value>>& aTransferable,
  51. ErrorResult& aRv);
  52. private:
  53. // This class can only be created from ServiceWorkerInfo::GetOrCreateInstance().
  54. ServiceWorker(nsPIDOMWindowInner* aWindow, ServiceWorkerInfo* aInfo);
  55. // This class is reference-counted and will be destroyed from Release().
  56. ~ServiceWorker();
  57. ServiceWorkerState mState;
  58. const RefPtr<ServiceWorkerInfo> mInfo;
  59. };
  60. } // namespace workers
  61. } // namespace dom
  62. } // namespace mozilla
  63. #endif // mozilla_dom_workers_serviceworker_h__