Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/script/ModuleLoadRequest.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "ModuleLoadRequest.h"
8
#include "ModuleScript.h"
9
#include "ScriptLoader.h"
10
11
namespace mozilla {
12
namespace dom {
13
14
#undef LOG
15
#define LOG(args) \
16
0
  MOZ_LOG(ScriptLoader::gScriptLoaderLog, mozilla::LogLevel::Debug, args)
17
18
0
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ModuleLoadRequest)
19
0
NS_INTERFACE_MAP_END_INHERITING(ScriptLoadRequest)
20
21
NS_IMPL_CYCLE_COLLECTION_INHERITED(ModuleLoadRequest, ScriptLoadRequest,
22
                                   mBaseURL,
23
                                   mLoader,
24
                                   mModuleScript,
25
                                   mImports)
26
27
NS_IMPL_ADDREF_INHERITED(ModuleLoadRequest, ScriptLoadRequest)
28
NS_IMPL_RELEASE_INHERITED(ModuleLoadRequest, ScriptLoadRequest)
29
30
ModuleLoadRequest::ModuleLoadRequest(nsIURI* aURI,
31
                                     ScriptFetchOptions* aFetchOptions,
32
                                     const SRIMetadata& aIntegrity,
33
                                     nsIURI* aReferrer,
34
                                     ScriptLoader* aLoader)
35
  : ScriptLoadRequest(ScriptKind::eModule,
36
                      aURI,
37
                      aFetchOptions,
38
                      aIntegrity,
39
                      aReferrer),
40
    mIsTopLevel(true),
41
    mLoader(aLoader),
42
    mVisitedSet(new VisitedURLSet())
43
0
{
44
0
  mVisitedSet->PutEntry(aURI);
45
0
}
46
47
ModuleLoadRequest::ModuleLoadRequest(nsIURI* aURI,
48
                                     ModuleLoadRequest* aParent)
49
  : ScriptLoadRequest(ScriptKind::eModule,
50
                      aURI,
51
                      aParent->mFetchOptions,
52
                      SRIMetadata(),
53
                      aParent->mURI),
54
    mIsTopLevel(false),
55
    mLoader(aParent->mLoader),
56
    mVisitedSet(aParent->mVisitedSet)
57
0
{
58
0
  MOZ_ASSERT(mVisitedSet->Contains(aURI));
59
0
60
0
  mIsInline = false;
61
0
  mScriptMode = aParent->mScriptMode;
62
0
}
63
64
void
65
ModuleLoadRequest::Cancel()
66
0
{
67
0
  ScriptLoadRequest::Cancel();
68
0
  mModuleScript = nullptr;
69
0
  mProgress = ScriptLoadRequest::Progress::eReady;
70
0
  CancelImports();
71
0
  mReady.RejectIfExists(NS_ERROR_DOM_ABORT_ERR, __func__);
72
0
}
73
74
void
75
ModuleLoadRequest::CancelImports()
76
0
{
77
0
  for (size_t i = 0; i < mImports.Length(); i++) {
78
0
    mImports[i]->Cancel();
79
0
  }
80
0
}
81
82
void
83
ModuleLoadRequest::SetReady()
84
0
{
85
0
  // Mark a module as ready to execute. This means that this module and all it
86
0
  // dependencies have had their source loaded, parsed as a module and the
87
0
  // modules instantiated.
88
0
  //
89
0
  // The mReady promise is used to ensure that when all dependencies of a module
90
0
  // have become ready, DependenciesLoaded is called on that module
91
0
  // request. This is set up in StartFetchingModuleDependencies.
92
0
93
#ifdef DEBUG
94
  for (size_t i = 0; i < mImports.Length(); i++) {
95
    MOZ_ASSERT(mImports[i]->IsReadyToRun());
96
  }
97
#endif
98
99
0
  ScriptLoadRequest::SetReady();
100
0
  mReady.ResolveIfExists(true, __func__);
101
0
}
102
103
void
104
ModuleLoadRequest::ModuleLoaded()
105
0
{
106
0
  // A module that was found to be marked as fetching in the module map has now
107
0
  // been loaded.
108
0
109
0
  LOG(("ScriptLoadRequest (%p): Module loaded", this));
110
0
111
0
  mModuleScript = mLoader->GetFetchedModule(mURI);
112
0
  if (!mModuleScript || mModuleScript->HasParseError()) {
113
0
    ModuleErrored();
114
0
    return;
115
0
  }
116
0
117
0
  mLoader->StartFetchingModuleDependencies(this);
118
0
}
119
120
void
121
ModuleLoadRequest::ModuleErrored()
122
0
{
123
0
  LOG(("ScriptLoadRequest (%p): Module errored", this));
124
0
125
0
  mLoader->CheckModuleDependenciesLoaded(this);
126
0
  MOZ_ASSERT(!mModuleScript || mModuleScript->HasParseError());
127
0
128
0
  CancelImports();
129
0
  SetReady();
130
0
  LoadFinished();
131
0
}
132
133
void
134
ModuleLoadRequest::DependenciesLoaded()
135
0
{
136
0
  // The module and all of its dependencies have been successfully fetched and
137
0
  // compiled.
138
0
139
0
  LOG(("ScriptLoadRequest (%p): Module dependencies loaded", this));
140
0
141
0
  MOZ_ASSERT(mModuleScript);
142
0
143
0
  mLoader->CheckModuleDependenciesLoaded(this);
144
0
  SetReady();
145
0
  LoadFinished();
146
0
}
147
148
void
149
ModuleLoadRequest::LoadFailed()
150
0
{
151
0
  // We failed to load the source text or an error occurred unrelated to the
152
0
  // content of the module (e.g. OOM).
153
0
154
0
  LOG(("ScriptLoadRequest (%p): Module load failed", this));
155
0
156
0
  MOZ_ASSERT(!mModuleScript);
157
0
158
0
  Cancel();
159
0
  LoadFinished();
160
0
}
161
162
void
163
ModuleLoadRequest::LoadFinished()
164
0
{
165
0
  mLoader->ProcessLoadedModuleTree(this);
166
0
167
0
  mLoader = nullptr;
168
0
}
169
170
} // dom namespace
171
} // mozilla namespace