ModuleLoadRequest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include "ModuleLoadRequest.h"
  6. #include "ModuleScript.h"
  7. #include "ScriptLoader.h"
  8. namespace mozilla {
  9. namespace dom {
  10. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ModuleLoadRequest)
  11. NS_INTERFACE_MAP_END_INHERITING(ScriptLoadRequest)
  12. NS_IMPL_CYCLE_COLLECTION_INHERITED(ModuleLoadRequest, ScriptLoadRequest,
  13. mBaseURL,
  14. mLoader,
  15. mModuleScript,
  16. mImports)
  17. NS_IMPL_ADDREF_INHERITED(ModuleLoadRequest, ScriptLoadRequest)
  18. NS_IMPL_RELEASE_INHERITED(ModuleLoadRequest, ScriptLoadRequest)
  19. ModuleLoadRequest::ModuleLoadRequest(nsIURI* aURI,
  20. nsIScriptElement* aElement,
  21. uint32_t aVersion,
  22. CORSMode aCORSMode,
  23. const SRIMetadata &aIntegrity,
  24. nsIURI* aReferrer,
  25. mozilla::net::ReferrerPolicy aReferrerPolicy,
  26. ScriptLoader* aLoader)
  27. : ScriptLoadRequest(ScriptKind::Module,
  28. aURI,
  29. aElement,
  30. aVersion,
  31. aCORSMode,
  32. aIntegrity,
  33. aReferrer,
  34. aReferrerPolicy),
  35. mIsTopLevel(true),
  36. mLoader(aLoader),
  37. mVisitedSet(new VisitedURLSet())
  38. {
  39. mVisitedSet->PutEntry(aURI);
  40. }
  41. ModuleLoadRequest::ModuleLoadRequest(nsIURI* aURI,
  42. ModuleLoadRequest* aParent)
  43. : ScriptLoadRequest(ScriptKind::Module,
  44. aURI,
  45. aParent->mElement,
  46. aParent->mJSVersion,
  47. aParent->mCORSMode,
  48. SRIMetadata(),
  49. aParent->mURI,
  50. aParent->mReferrerPolicy),
  51. mIsTopLevel(false),
  52. mLoader(aParent->mLoader),
  53. mVisitedSet(aParent->mVisitedSet)
  54. {
  55. MOZ_ASSERT(mVisitedSet->Contains(aURI));
  56. mIsInline = false;
  57. mScriptMode = aParent->mScriptMode;
  58. }
  59. void ModuleLoadRequest::Cancel()
  60. {
  61. ScriptLoadRequest::Cancel();
  62. mModuleScript = nullptr;
  63. mProgress = ScriptLoadRequest::Progress::Ready;
  64. CancelImports();
  65. mReady.RejectIfExists(NS_ERROR_DOM_ABORT_ERR, __func__);
  66. }
  67. void
  68. ModuleLoadRequest::CancelImports()
  69. {
  70. for (size_t i = 0; i < mImports.Length(); i++) {
  71. mImports[i]->Cancel();
  72. }
  73. }
  74. void
  75. ModuleLoadRequest::SetReady()
  76. {
  77. // Mark a module as ready to execute. This means that this module and all it
  78. // dependencies have had their source loaded, parsed as a module and the
  79. // modules instantiated.
  80. //
  81. // The mReady promise is used to ensure that when all dependencies of a module
  82. // have become ready, DependenciesLoaded is called on that module
  83. // request. This is set up in StartFetchingModuleDependencies.
  84. #ifdef DEBUG
  85. for (size_t i = 0; i < mImports.Length(); i++) {
  86. MOZ_ASSERT(mImports[i]->IsReadyToRun());
  87. }
  88. #endif
  89. ScriptLoadRequest::SetReady();
  90. mReady.ResolveIfExists(true, __func__);
  91. }
  92. void
  93. ModuleLoadRequest::ModuleLoaded()
  94. {
  95. // A module that was found to be marked as fetching in the module map has now
  96. // been loaded.
  97. mModuleScript = mLoader->GetFetchedModule(mURI);
  98. if (!mModuleScript || mModuleScript->HasParseError()) {
  99. ModuleErrored();
  100. return;
  101. }
  102. mLoader->StartFetchingModuleDependencies(this);
  103. }
  104. void
  105. ModuleLoadRequest::ModuleErrored()
  106. {
  107. mLoader->CheckModuleDependenciesLoaded(this);
  108. MOZ_ASSERT(!mModuleScript || mModuleScript->HasParseError());
  109. CancelImports();
  110. SetReady();
  111. LoadFinished();
  112. }
  113. void
  114. ModuleLoadRequest::DependenciesLoaded()
  115. {
  116. // The module and all of its dependencies have been successfully fetched and
  117. // compiled.
  118. MOZ_ASSERT(mModuleScript);
  119. mLoader->CheckModuleDependenciesLoaded(this);
  120. SetReady();
  121. LoadFinished();
  122. }
  123. void
  124. ModuleLoadRequest::LoadFailed()
  125. {
  126. // We failed to load the source text or an error occurred unrelated to the
  127. // content of the module (e.g. OOM).
  128. MOZ_ASSERT(!mModuleScript);
  129. Cancel();
  130. LoadFinished();
  131. }
  132. void
  133. ModuleLoadRequest::LoadFinished()
  134. {
  135. mLoader->ProcessLoadedModuleTree(this);
  136. mLoader = nullptr;
  137. }
  138. } // dom namespace
  139. } // mozilla namespace