ServiceWorkerUpdateJob.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_serviceworkerupdatejob_h
  6. #define mozilla_dom_workers_serviceworkerupdatejob_h
  7. #include "ServiceWorkerJob.h"
  8. #include "ServiceWorkerRegistrationInfo.h"
  9. #include "nsILoadGroup.h"
  10. namespace mozilla {
  11. namespace dom {
  12. namespace workers {
  13. class ServiceWorkerManager;
  14. // A job class that performs the Update and Install algorithms from the
  15. // service worker spec. This class is designed to be inherited and customized
  16. // as a different job type. This is necessary because the register job
  17. // performs largely the same operations as the update job, but has a few
  18. // different starting steps.
  19. class ServiceWorkerUpdateJob : public ServiceWorkerJob
  20. {
  21. public:
  22. // Construct an update job to be used only for updates.
  23. ServiceWorkerUpdateJob(nsIPrincipal* aPrincipal,
  24. const nsACString& aScope,
  25. const nsACString& aScriptSpec,
  26. nsILoadGroup* aLoadGroup);
  27. already_AddRefed<ServiceWorkerRegistrationInfo>
  28. GetRegistration() const;
  29. protected:
  30. // Construct an update job that is overriden as another job type.
  31. ServiceWorkerUpdateJob(Type aType,
  32. nsIPrincipal* aPrincipal,
  33. const nsACString& aScope,
  34. const nsACString& aScriptSpec,
  35. nsILoadGroup* aLoadGroup);
  36. virtual ~ServiceWorkerUpdateJob();
  37. // FailUpdateJob() must be called if an update job needs Finish() with
  38. // an error.
  39. void
  40. FailUpdateJob(ErrorResult& aRv);
  41. void
  42. FailUpdateJob(nsresult aRv);
  43. // The entry point when the update job is being used directly. Job
  44. // types overriding this class should override this method to
  45. // customize behavior.
  46. virtual void
  47. AsyncExecute() override;
  48. // Set the registration to be operated on by Update() or to be immediately
  49. // returned as a result of the job. This must be called before Update().
  50. void
  51. SetRegistration(ServiceWorkerRegistrationInfo* aRegistration);
  52. // Execute the bulk of the update job logic using the registration defined
  53. // by a previous SetRegistration() call. This can be called by the overriden
  54. // AsyncExecute() to complete the job. The Update() method will always call
  55. // Finish(). This method corresponds to the spec Update algorithm.
  56. void
  57. Update();
  58. private:
  59. class CompareCallback;
  60. class ContinueUpdateRunnable;
  61. class ContinueInstallRunnable;
  62. // Utility method called after a script is loaded and compared to
  63. // our current cached script.
  64. void
  65. ComparisonResult(nsresult aStatus,
  66. bool aInCacheAndEqual,
  67. const nsAString& aNewCacheName,
  68. const nsACString& aMaxScope);
  69. // Utility method called after evaluating the worker script.
  70. void
  71. ContinueUpdateAfterScriptEval(bool aScriptEvaluationResult);
  72. // Utility method corresponding to the spec Install algorithm.
  73. void
  74. Install(ServiceWorkerManager* aSWM);
  75. // Utility method called after the install event is handled.
  76. void
  77. ContinueAfterInstallEvent(bool aInstallEventSuccess);
  78. nsCOMPtr<nsILoadGroup> mLoadGroup;
  79. RefPtr<ServiceWorkerRegistrationInfo> mRegistration;
  80. };
  81. } // namespace workers
  82. } // namespace dom
  83. } // namespace mozilla
  84. #endif // mozilla_dom_workers_serviceworkerupdatejob_h