ServiceWorkerInfo.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_serviceworkerinfo_h
  6. #define mozilla_dom_workers_serviceworkerinfo_h
  7. #include "mozilla/dom/ServiceWorkerBinding.h" // For ServiceWorkerState
  8. #include "nsIServiceWorkerManager.h"
  9. #include "Workers.h"
  10. namespace mozilla {
  11. namespace dom {
  12. namespace workers {
  13. class ServiceWorker;
  14. class ServiceWorkerPrivate;
  15. /*
  16. * Wherever the spec treats a worker instance and a description of said worker
  17. * as the same thing; i.e. "Resolve foo with
  18. * _GetNewestWorker(serviceWorkerRegistration)", we represent the description
  19. * by this class and spawn a ServiceWorker in the right global when required.
  20. */
  21. class ServiceWorkerInfo final : public nsIServiceWorkerInfo
  22. {
  23. private:
  24. nsCOMPtr<nsIPrincipal> mPrincipal;
  25. const nsCString mScope;
  26. const nsCString mScriptSpec;
  27. const nsString mCacheName;
  28. ServiceWorkerState mState;
  29. PrincipalOriginAttributes mOriginAttributes;
  30. // This id is shared with WorkerPrivate to match requests issued by service
  31. // workers to their corresponding serviceWorkerInfo.
  32. uint64_t mServiceWorkerID;
  33. // We hold rawptrs since the ServiceWorker constructor and destructor ensure
  34. // addition and removal.
  35. // There is a high chance of there being at least one ServiceWorker
  36. // associated with this all the time.
  37. AutoTArray<ServiceWorker*, 1> mInstances;
  38. RefPtr<ServiceWorkerPrivate> mServiceWorkerPrivate;
  39. bool mSkipWaitingFlag;
  40. ~ServiceWorkerInfo();
  41. // Generates a unique id for the service worker, with zero being treated as
  42. // invalid.
  43. uint64_t
  44. GetNextID() const;
  45. public:
  46. NS_DECL_ISUPPORTS
  47. NS_DECL_NSISERVICEWORKERINFO
  48. class ServiceWorkerPrivate*
  49. WorkerPrivate() const
  50. {
  51. MOZ_ASSERT(mServiceWorkerPrivate);
  52. return mServiceWorkerPrivate;
  53. }
  54. nsIPrincipal*
  55. GetPrincipal() const
  56. {
  57. return mPrincipal;
  58. }
  59. const nsCString&
  60. ScriptSpec() const
  61. {
  62. return mScriptSpec;
  63. }
  64. const nsCString&
  65. Scope() const
  66. {
  67. return mScope;
  68. }
  69. bool SkipWaitingFlag() const
  70. {
  71. AssertIsOnMainThread();
  72. return mSkipWaitingFlag;
  73. }
  74. void SetSkipWaitingFlag()
  75. {
  76. AssertIsOnMainThread();
  77. mSkipWaitingFlag = true;
  78. }
  79. ServiceWorkerInfo(nsIPrincipal* aPrincipal,
  80. const nsACString& aScope,
  81. const nsACString& aScriptSpec,
  82. const nsAString& aCacheName);
  83. ServiceWorkerState
  84. State() const
  85. {
  86. return mState;
  87. }
  88. const PrincipalOriginAttributes&
  89. GetOriginAttributes() const
  90. {
  91. return mOriginAttributes;
  92. }
  93. const nsString&
  94. CacheName() const
  95. {
  96. return mCacheName;
  97. }
  98. uint64_t
  99. ID() const
  100. {
  101. return mServiceWorkerID;
  102. }
  103. void
  104. UpdateState(ServiceWorkerState aState);
  105. // Only used to set initial state when loading from disk!
  106. void
  107. SetActivateStateUncheckedWithoutEvent(ServiceWorkerState aState)
  108. {
  109. AssertIsOnMainThread();
  110. mState = aState;
  111. }
  112. void
  113. AppendWorker(ServiceWorker* aWorker);
  114. void
  115. RemoveWorker(ServiceWorker* aWorker);
  116. already_AddRefed<ServiceWorker>
  117. GetOrCreateInstance(nsPIDOMWindowInner* aWindow);
  118. };
  119. } // namespace workers
  120. } // namespace dom
  121. } // namespace mozilla
  122. #endif // mozilla_dom_workers_serviceworkerinfo_h