File: PendingScript.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (230 lines) | stat: -rw-r--r-- 8,552 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright (C) 2010 Google, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "core/dom/PendingScript.h"

#include "bindings/core/v8/ScriptSourceCode.h"
#include "core/dom/Element.h"
#include "core/frame/SubresourceIntegrity.h"
#include "platform/SharedBuffer.h"
#include "wtf/CurrentTime.h"

namespace blink {

PendingScript* PendingScript::create(Element* element,
                                     ScriptResource* resource) {
  return new PendingScript(element, resource);
}

PendingScript::PendingScript(Element* element, ScriptResource* resource)
    : m_watchingForLoad(false),
      m_element(element),
      m_integrityFailure(false),
      m_parserBlockingLoadStartTime(0),
      m_client(nullptr) {
  setScriptResource(resource);
  MemoryCoordinator::instance().registerClient(this);
}

PendingScript::~PendingScript() {}

void PendingScript::dispose() {
  stopWatchingForLoad();
  DCHECK(!m_client);
  DCHECK(!m_watchingForLoad);

  setScriptResource(nullptr);
  m_startingPosition = TextPosition::belowRangePosition();
  m_integrityFailure = false;
  m_parserBlockingLoadStartTime = 0;
  if (m_streamer)
    m_streamer->cancel();
  m_streamer = nullptr;
  m_element = nullptr;
}

void PendingScript::watchForLoad(PendingScriptClient* client) {
  DCHECK(!m_watchingForLoad);
  // addClient() will call streamingFinished() if the load is complete. Callers
  // who do not expect to be re-entered from this call should not call
  // watchForLoad for a PendingScript which isReady. We also need to set
  // m_watchingForLoad early, since addClient() can result in calling
  // notifyFinished and further stopWatchingForLoad().
  m_watchingForLoad = true;
  m_client = client;
  if (isReady())
    m_client->pendingScriptFinished(this);
}

void PendingScript::stopWatchingForLoad() {
  if (!m_watchingForLoad)
    return;
  DCHECK(resource());
  m_client = nullptr;
  m_watchingForLoad = false;
}

void PendingScript::streamingFinished() {
  DCHECK(resource());
  if (m_client)
    m_client->pendingScriptFinished(this);
}

void PendingScript::setElement(Element* element) {
  m_element = element;
}

void PendingScript::setScriptResource(ScriptResource* resource) {
  setResource(resource);
}

void PendingScript::markParserBlockingLoadStartTime() {
  DCHECK_EQ(m_parserBlockingLoadStartTime, 0.0);
  m_parserBlockingLoadStartTime = monotonicallyIncreasingTime();
}

void PendingScript::notifyFinished(Resource* resource) {
  // The following SRI checks need to be here because, unfortunately, fetches
  // are not done purely according to the Fetch spec. In particular,
  // different requests for the same resource do not have different
  // responses; the memory cache can (and will) return the exact same
  // Resource object.
  //
  // For different requests, the same Resource object will be returned and
  // will not be associated with the particular request.  Therefore, when the
  // body of the response comes in, there's no way to validate the integrity
  // of the Resource object against a particular request (since there may be
  // several pending requests all tied to the identical object, and the
  // actual requests are not stored).
  //
  // In order to simulate the correct behavior, Blink explicitly does the SRI
  // checks here, when a PendingScript tied to a particular request is
  // finished (and in the case of a StyleSheet, at the point of execution),
  // while having proper Fetch checks in the fetch module for use in the
  // fetch JavaScript API. In a future world where the ResourceFetcher uses
  // the Fetch algorithm, this should be fixed by having separate Response
  // objects (perhaps attached to identical Resource objects) per request.
  //
  // See https://crbug.com/500701 for more information.
  if (m_element) {
    DCHECK_EQ(resource->getType(), Resource::Script);
    ScriptResource* scriptResource = toScriptResource(resource);
    String integrityAttr =
        m_element->fastGetAttribute(HTMLNames::integrityAttr);

    // It is possible to get back a script resource with integrity metadata
    // for a request with an empty integrity attribute. In that case, the
    // integrity check should be skipped, so this check ensures that the
    // integrity attribute isn't empty in addition to checking if the
    // resource has empty integrity metadata.
    if (!integrityAttr.isEmpty() &&
        !scriptResource->integrityMetadata().isEmpty()) {
      ResourceIntegrityDisposition disposition =
          scriptResource->integrityDisposition();
      if (disposition == ResourceIntegrityDisposition::Failed) {
        // TODO(jww): This should probably also generate a console
        // message identical to the one produced by
        // CheckSubresourceIntegrity below. See https://crbug.com/585267.
        m_integrityFailure = true;
      } else if (disposition == ResourceIntegrityDisposition::NotChecked &&
                 resource->resourceBuffer()) {
        m_integrityFailure = !SubresourceIntegrity::CheckSubresourceIntegrity(
            scriptResource->integrityMetadata(), *m_element,
            resource->resourceBuffer()->data(),
            resource->resourceBuffer()->size(), resource->url(), *resource);
        scriptResource->setIntegrityDisposition(
            m_integrityFailure ? ResourceIntegrityDisposition::Failed
                               : ResourceIntegrityDisposition::Passed);
      }
    }
  }

  // If script streaming is in use, the client will be notified in
  // streamingFinished.
  if (m_streamer)
    m_streamer->notifyFinished(resource);
  else if (m_client)
    m_client->pendingScriptFinished(this);
}

void PendingScript::notifyAppendData(ScriptResource* resource) {
  if (m_streamer)
    m_streamer->notifyAppendData(resource);
}

DEFINE_TRACE(PendingScript) {
  visitor->trace(m_element);
  visitor->trace(m_streamer);
  visitor->trace(m_client);
  ResourceOwner<ScriptResource>::trace(visitor);
  MemoryCoordinatorClient::trace(visitor);
}

ScriptSourceCode PendingScript::getSource(const KURL& documentURL,
                                          bool& errorOccurred) const {
  if (resource()) {
    errorOccurred = resource()->errorOccurred() || m_integrityFailure;
    DCHECK(resource()->isLoaded());
    if (m_streamer && !m_streamer->streamingSuppressed())
      return ScriptSourceCode(m_streamer, resource());
    return ScriptSourceCode(resource());
  }
  errorOccurred = false;
  return ScriptSourceCode(m_element->textContent(), documentURL,
                          startingPosition());
}

void PendingScript::setStreamer(ScriptStreamer* streamer) {
  DCHECK(!m_streamer);
  DCHECK(!m_watchingForLoad);
  m_streamer = streamer;
}

bool PendingScript::isReady() const {
  if (resource() && !resource()->isLoaded())
    return false;
  if (m_streamer && !m_streamer->isFinished())
    return false;
  return true;
}

bool PendingScript::errorOccurred() const {
  if (resource())
    return resource()->errorOccurred();
  if (m_streamer && m_streamer->resource())
    return m_streamer->resource()->errorOccurred();
  return false;
}

void PendingScript::onMemoryStateChange(MemoryState state) {
  if (state != MemoryState::SUSPENDED)
    return;
  if (!m_streamer)
    return;
  m_streamer->cancel();
  m_streamer = nullptr;
}

}  // namespace blink