[Resource Timing] Gather timing information with reliable responseEnd time
[WebKit-https.git] / Source / WebCore / workers / WorkerScriptLoader.cpp
1 /*
2  * Copyright (C) 2009 Apple Inc. All Rights Reserved.
3  * Copyright (C) 2009, 2011 Google Inc. All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
25  */
26
27 #include "config.h"
28 #include "WorkerScriptLoader.h"
29
30 #include "ContentSecurityPolicy.h"
31 #include "ResourceResponse.h"
32 #include "ScriptExecutionContext.h"
33 #include "TextResourceDecoder.h"
34 #include "WorkerGlobalScope.h"
35 #include "WorkerScriptLoaderClient.h"
36 #include "WorkerThreadableLoader.h"
37 #include <wtf/Ref.h>
38
39 namespace WebCore {
40
41 WorkerScriptLoader::WorkerScriptLoader()
42 {
43 }
44
45 WorkerScriptLoader::~WorkerScriptLoader()
46 {
47 }
48
49 void WorkerScriptLoader::loadSynchronously(ScriptExecutionContext* scriptExecutionContext, const URL& url, FetchOptions::Mode mode, ContentSecurityPolicyEnforcement contentSecurityPolicyEnforcement, const String& initiatorIdentifier)
50 {
51     ASSERT(scriptExecutionContext);
52
53     m_url = url;
54
55     std::unique_ptr<ResourceRequest> request(createResourceRequest(initiatorIdentifier));
56     if (!request)
57         return;
58
59     ASSERT_WITH_SECURITY_IMPLICATION(is<WorkerGlobalScope>(scriptExecutionContext));
60
61     // Only used for importScripts that prescribes NoCors mode.
62     ASSERT(mode == FetchOptions::Mode::NoCors);
63
64     ThreadableLoaderOptions options;
65     options.credentials = FetchOptions::Credentials::Include;
66     options.mode = mode;
67     options.sendLoadCallbacks = SendCallbacks;
68     options.contentSecurityPolicyEnforcement = contentSecurityPolicyEnforcement;
69
70     WorkerThreadableLoader::loadResourceSynchronously(downcast<WorkerGlobalScope>(*scriptExecutionContext), WTFMove(*request), *this, options);
71 }
72
73 void WorkerScriptLoader::loadAsynchronously(ScriptExecutionContext* scriptExecutionContext, const URL& url, FetchOptions::Mode mode, ContentSecurityPolicyEnforcement contentSecurityPolicyEnforcement, const String& initiatorIdentifier, WorkerScriptLoaderClient* client)
74 {
75     ASSERT(client);
76     ASSERT(scriptExecutionContext);
77
78     m_client = client;
79     m_url = url;
80
81     std::unique_ptr<ResourceRequest> request(createResourceRequest(initiatorIdentifier));
82     if (!request)
83         return;
84
85     // Only used for loading worker scripts in classic mode.
86     // FIXME: We should add an option to set credential mode.
87     ASSERT(mode == FetchOptions::Mode::SameOrigin);
88
89     ThreadableLoaderOptions options;
90     options.credentials = FetchOptions::Credentials::SameOrigin;
91     options.mode = mode;
92     options.sendLoadCallbacks = SendCallbacks;
93     options.contentSecurityPolicyEnforcement = contentSecurityPolicyEnforcement;
94
95     // During create, callbacks may happen which remove the last reference to this object.
96     Ref<WorkerScriptLoader> protectedThis(*this);
97     m_threadableLoader = ThreadableLoader::create(*scriptExecutionContext, *this, WTFMove(*request), options);
98 }
99
100 const URL& WorkerScriptLoader::responseURL() const
101 {
102     ASSERT(!failed());
103     return m_responseURL;
104 }
105
106 std::unique_ptr<ResourceRequest> WorkerScriptLoader::createResourceRequest(const String& initiatorIdentifier)
107 {
108     auto request = std::make_unique<ResourceRequest>(m_url);
109     request->setHTTPMethod(ASCIILiteral("GET"));
110     request->setInitiatorIdentifier(initiatorIdentifier);
111     return request;
112 }
113     
114 void WorkerScriptLoader::didReceiveResponse(unsigned long identifier, const ResourceResponse& response)
115 {
116     if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) {
117         m_failed = true;
118         return;
119     }
120     m_responseURL = response.url();
121     m_responseEncoding = response.textEncodingName();
122     if (m_client)
123         m_client->didReceiveResponse(identifier, response);
124 }
125
126 void WorkerScriptLoader::didReceiveData(const char* data, int len)
127 {
128     if (m_failed)
129         return;
130
131     if (!m_decoder) {
132         if (!m_responseEncoding.isEmpty())
133             m_decoder = TextResourceDecoder::create(ASCIILiteral("text/javascript"), m_responseEncoding);
134         else
135             m_decoder = TextResourceDecoder::create(ASCIILiteral("text/javascript"), "UTF-8");
136     }
137
138     if (!len)
139         return;
140     
141     if (len == -1)
142         len = strlen(data);
143     
144     m_script.append(m_decoder->decode(data, len));
145 }
146
147 void WorkerScriptLoader::didFinishLoading(unsigned long identifier)
148 {
149     if (m_failed) {
150         notifyError();
151         return;
152     }
153
154     if (m_decoder)
155         m_script.append(m_decoder->flush());
156
157     m_identifier = identifier;
158     notifyFinished();
159 }
160
161 void WorkerScriptLoader::didFail(const ResourceError&)
162 {
163     notifyError();
164 }
165
166 void WorkerScriptLoader::notifyError()
167 {
168     m_failed = true;
169     notifyFinished();
170 }
171
172 String WorkerScriptLoader::script()
173 {
174     return m_script.toString();
175 }
176
177 void WorkerScriptLoader::notifyFinished()
178 {
179     if (!m_client || m_finishing)
180         return;
181
182     m_finishing = true;
183     m_client->notifyFinished();
184 }
185
186 } // namespace WebCore