Use "= default" to denote default constructor or destructor
[WebKit-https.git] / Source / WebCore / loader / ThreadableLoader.cpp
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "ThreadableLoader.h"
33
34 #include "CachedResourceRequestInitiators.h"
35 #include "Document.h"
36 #include "DocumentThreadableLoader.h"
37 #include "ResourceError.h"
38 #include "ScriptExecutionContext.h"
39 #include "WorkerGlobalScope.h"
40 #include "WorkerRunLoop.h"
41 #include "WorkerThreadableLoader.h"
42
43 namespace WebCore {
44
45 ThreadableLoaderOptions::ThreadableLoaderOptions()
46 {
47     mode = FetchOptions::Mode::SameOrigin;
48 }
49
50 ThreadableLoaderOptions::~ThreadableLoaderOptions() = default;
51
52 ThreadableLoaderOptions::ThreadableLoaderOptions(const ResourceLoaderOptions& baseOptions, PreflightPolicy preflightPolicy, ContentSecurityPolicyEnforcement contentSecurityPolicyEnforcement, String&& initiator, ResponseFilteringPolicy filteringPolicy)
53     : ResourceLoaderOptions(baseOptions)
54     , preflightPolicy(preflightPolicy)
55     , contentSecurityPolicyEnforcement(contentSecurityPolicyEnforcement)
56     , initiator(WTFMove(initiator))
57     , filteringPolicy(filteringPolicy)
58 {
59 }
60
61 ThreadableLoaderOptions ThreadableLoaderOptions::isolatedCopy() const
62 {
63     ThreadableLoaderOptions copy;
64
65     // FetchOptions
66     copy.destination = this->destination;
67     copy.mode = this->mode;
68     copy.credentials = this->credentials;
69     copy.cache = this->cache;
70     copy.redirect = this->redirect;
71     copy.referrerPolicy = this->referrerPolicy;
72     copy.integrity = this->integrity.isolatedCopy();
73
74     // ResourceLoaderOptions
75     copy.sendLoadCallbacks = this->sendLoadCallbacks;
76     copy.sniffContent = this->sniffContent;
77     copy.dataBufferingPolicy = this->dataBufferingPolicy;
78     copy.storedCredentialsPolicy = this->storedCredentialsPolicy;
79     copy.securityCheck = this->securityCheck;
80     copy.certificateInfoPolicy = this->certificateInfoPolicy;
81     copy.contentSecurityPolicyImposition = this->contentSecurityPolicyImposition;
82     copy.defersLoadingPolicy = this->defersLoadingPolicy;
83     copy.cachingPolicy = this->cachingPolicy;
84     copy.sameOriginDataURLFlag = this->sameOriginDataURLFlag;
85     copy.initiatorContext = this->initiatorContext;
86     copy.clientCredentialPolicy = this->clientCredentialPolicy;
87     copy.maxRedirectCount = this->maxRedirectCount;
88     copy.derivedCachedDataTypesToRetrieve.reserveInitialCapacity(this->derivedCachedDataTypesToRetrieve.size());
89     for (auto& derivedCachedDataType : this->derivedCachedDataTypesToRetrieve)
90         copy.derivedCachedDataTypesToRetrieve.uncheckedAppend(derivedCachedDataType.isolatedCopy());
91
92     // ThreadableLoaderOptions
93     copy.preflightPolicy = this->preflightPolicy;
94     copy.contentSecurityPolicyEnforcement = this->contentSecurityPolicyEnforcement;
95     copy.initiator = this->initiator.isolatedCopy();
96     copy.filteringPolicy = this->filteringPolicy;
97
98     return copy;
99 }
100
101
102 RefPtr<ThreadableLoader> ThreadableLoader::create(ScriptExecutionContext& context, ThreadableLoaderClient& client, ResourceRequest&& request, const ThreadableLoaderOptions& options, String&& referrer)
103 {
104     if (is<WorkerGlobalScope>(context))
105         return WorkerThreadableLoader::create(downcast<WorkerGlobalScope>(context), client, WorkerRunLoop::defaultMode(), WTFMove(request), options, referrer);
106
107     return DocumentThreadableLoader::create(downcast<Document>(context), client, WTFMove(request), options, WTFMove(referrer));
108 }
109
110 void ThreadableLoader::loadResourceSynchronously(ScriptExecutionContext& context, ResourceRequest&& request, ThreadableLoaderClient& client, const ThreadableLoaderOptions& options)
111 {
112     if (is<WorkerGlobalScope>(context))
113         WorkerThreadableLoader::loadResourceSynchronously(downcast<WorkerGlobalScope>(context), WTFMove(request), client, options);
114     else
115         DocumentThreadableLoader::loadResourceSynchronously(downcast<Document>(context), WTFMove(request), client, options);
116     context.didLoadResourceSynchronously();
117 }
118
119 void ThreadableLoader::logError(ScriptExecutionContext& context, const ResourceError& error, const String& initiator)
120 {
121     // FIXME: extend centralized logging to other clients than fetch, at least XHR and EventSource.
122     if (initiator != cachedResourceRequestInitiators().fetch)
123         return;
124
125     if (error.isCancellation())
126         return;
127
128     // FIXME: Some errors are returned with null URLs. This leads to poor console messages. We should do better for these errors.
129     if (error.failingURL().isNull())
130         return;
131
132     // We further reduce logging to some errors.
133     // FIXME: Log more errors when making so do not make some layout tests flaky.
134     if (error.domain() != errorDomainWebKitInternal && !error.isAccessControl())
135         return;
136
137     const char* messageStart;
138     if (initiator == cachedResourceRequestInitiators().fetch)
139         messageStart = "Fetch API cannot load ";
140     else
141         messageStart = "Cannot load ";
142
143     const char* messageMiddle = ". ";
144     String description = error.localizedDescription();
145     if (description.isEmpty()) {
146         // FIXME: We should probably define default description error message for all error types.
147         if (error.isAccessControl())
148             messageMiddle = ASCIILiteral(" due to access control checks.");
149         else
150             messageMiddle = ".";
151     }
152
153     context.addConsoleMessage(MessageSource::JS, MessageLevel::Error, makeString(messageStart, error.failingURL().string(), messageMiddle, description));
154 }
155
156 } // namespace WebCore