ServiceWorkerRegistrationInfo.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_serviceworkerregistrationinfo_h
  6. #define mozilla_dom_workers_serviceworkerregistrationinfo_h
  7. #include "mozilla/dom/workers/ServiceWorkerInfo.h"
  8. #include "mozilla/dom/workers/ServiceWorkerCommon.h"
  9. namespace mozilla {
  10. namespace dom {
  11. namespace workers {
  12. class ServiceWorkerRegistrationInfo final
  13. : public nsIServiceWorkerRegistrationInfo
  14. {
  15. uint32_t mControlledDocumentsCounter;
  16. enum
  17. {
  18. NoUpdate,
  19. NeedTimeCheckAndUpdate,
  20. NeedUpdate
  21. } mUpdateState;
  22. uint64_t mLastUpdateCheckTime;
  23. RefPtr<ServiceWorkerInfo> mEvaluatingWorker;
  24. RefPtr<ServiceWorkerInfo> mActiveWorker;
  25. RefPtr<ServiceWorkerInfo> mWaitingWorker;
  26. RefPtr<ServiceWorkerInfo> mInstallingWorker;
  27. virtual ~ServiceWorkerRegistrationInfo();
  28. public:
  29. NS_DECL_ISUPPORTS
  30. NS_DECL_NSISERVICEWORKERREGISTRATIONINFO
  31. const nsCString mScope;
  32. nsCOMPtr<nsIPrincipal> mPrincipal;
  33. nsTArray<nsCOMPtr<nsIServiceWorkerRegistrationInfoListener>> mListeners;
  34. // When unregister() is called on a registration, it is not immediately
  35. // removed since documents may be controlled. It is marked as
  36. // pendingUninstall and when all controlling documents go away, removed.
  37. bool mPendingUninstall;
  38. ServiceWorkerRegistrationInfo(const nsACString& aScope,
  39. nsIPrincipal* aPrincipal);
  40. already_AddRefed<ServiceWorkerInfo>
  41. Newest() const
  42. {
  43. RefPtr<ServiceWorkerInfo> newest;
  44. if (mInstallingWorker) {
  45. newest = mInstallingWorker;
  46. } else if (mWaitingWorker) {
  47. newest = mWaitingWorker;
  48. } else {
  49. newest = mActiveWorker;
  50. }
  51. return newest.forget();
  52. }
  53. already_AddRefed<ServiceWorkerInfo>
  54. GetServiceWorkerInfoById(uint64_t aId);
  55. void
  56. StartControllingADocument()
  57. {
  58. ++mControlledDocumentsCounter;
  59. }
  60. void
  61. StopControllingADocument()
  62. {
  63. MOZ_ASSERT(mControlledDocumentsCounter);
  64. --mControlledDocumentsCounter;
  65. }
  66. bool
  67. IsControllingDocuments() const
  68. {
  69. return mActiveWorker && mControlledDocumentsCounter;
  70. }
  71. void
  72. Clear();
  73. void
  74. TryToActivateAsync();
  75. void
  76. TryToActivate();
  77. void
  78. Activate();
  79. void
  80. FinishActivate(bool aSuccess);
  81. void
  82. RefreshLastUpdateCheckTime();
  83. bool
  84. IsLastUpdateCheckTimeOverOneDay() const;
  85. void
  86. NotifyListenersOnChange(WhichServiceWorker aChangedWorkers);
  87. void
  88. MaybeScheduleTimeCheckAndUpdate();
  89. void
  90. MaybeScheduleUpdate();
  91. bool
  92. CheckAndClearIfUpdateNeeded();
  93. ServiceWorkerInfo*
  94. GetEvaluating() const;
  95. ServiceWorkerInfo*
  96. GetInstalling() const;
  97. ServiceWorkerInfo*
  98. GetWaiting() const;
  99. ServiceWorkerInfo*
  100. GetActive() const;
  101. // Set the given worker as the evaluating service worker. The worker
  102. // state is not changed.
  103. void
  104. SetEvaluating(ServiceWorkerInfo* aServiceWorker);
  105. // Remove an existing evaluating worker, if present. The worker will
  106. // be transitioned to the Redundant state.
  107. void
  108. ClearEvaluating();
  109. // Remove an existing installing worker, if present. The worker will
  110. // be transitioned to the Redundant state.
  111. void
  112. ClearInstalling();
  113. // Transition the current evaluating worker to be the installing worker. The
  114. // worker's state is update to Installing.
  115. void
  116. TransitionEvaluatingToInstalling();
  117. // Transition the current installing worker to be the waiting worker. The
  118. // worker's state is updated to Installed.
  119. void
  120. TransitionInstallingToWaiting();
  121. // Override the current active worker. This is used during browser
  122. // initialization to load persisted workers. Its also used to propagate
  123. // active workers across child processes in e10s. This second use will
  124. // go away once the ServiceWorkerManager moves to the parent process.
  125. // The worker is transitioned to the Activated state.
  126. void
  127. SetActive(ServiceWorkerInfo* aServiceWorker);
  128. // Transition the current waiting worker to be the new active worker. The
  129. // worker is updated to the Activating state.
  130. void
  131. TransitionWaitingToActive();
  132. // Determine if the registration is actively performing work.
  133. bool
  134. IsIdle() const;
  135. };
  136. } // namespace workers
  137. } // namespace dom
  138. } // namespace mozilla
  139. #endif // mozilla_dom_workers_serviceworkerregistrationinfo_h