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
43namespace WebCore {
44
45ThreadableLoaderOptions::ThreadableLoaderOptions()
46{
47 mode = FetchOptions::Mode::SameOrigin;
48}
49
50ThreadableLoaderOptions::~ThreadableLoaderOptions() = default;
51
52ThreadableLoaderOptions::ThreadableLoaderOptions(FetchOptions&& baseOptions)
53 : ResourceLoaderOptions { WTFMove(baseOptions) }
54{
55}
56
57ThreadableLoaderOptions::ThreadableLoaderOptions(const ResourceLoaderOptions& baseOptions, ContentSecurityPolicyEnforcement contentSecurityPolicyEnforcement, String&& initiator, ResponseFilteringPolicy filteringPolicy)
58 : ResourceLoaderOptions(baseOptions)
59 , contentSecurityPolicyEnforcement(contentSecurityPolicyEnforcement)
60 , initiator(WTFMove(initiator))
61 , filteringPolicy(filteringPolicy)
62{
63}
64
65ThreadableLoaderOptions ThreadableLoaderOptions::isolatedCopy() const
66{
67 ThreadableLoaderOptions copy;
68
69 // FetchOptions
70 copy.destination = this->destination;
71 copy.mode = this->mode;
72 copy.credentials = this->credentials;
73 copy.cache = this->cache;
74 copy.redirect = this->redirect;
75 copy.referrerPolicy = this->referrerPolicy;
76 copy.integrity = this->integrity.isolatedCopy();
77
78 // ResourceLoaderOptions
79 copy.sendLoadCallbacks = this->sendLoadCallbacks;
80 copy.sniffContent = this->sniffContent;
81 copy.dataBufferingPolicy = this->dataBufferingPolicy;
82 copy.storedCredentialsPolicy = this->storedCredentialsPolicy;
83 copy.securityCheck = this->securityCheck;
84 copy.certificateInfoPolicy = this->certificateInfoPolicy;
85 copy.contentSecurityPolicyImposition = this->contentSecurityPolicyImposition;
86 copy.defersLoadingPolicy = this->defersLoadingPolicy;
87 copy.cachingPolicy = this->cachingPolicy;
88 copy.sameOriginDataURLFlag = this->sameOriginDataURLFlag;
89 copy.initiatorContext = this->initiatorContext;
90 copy.clientCredentialPolicy = this->clientCredentialPolicy;
91 copy.maxRedirectCount = this->maxRedirectCount;
92 copy.preflightPolicy = this->preflightPolicy;
93
94 // ThreadableLoaderOptions
95 copy.contentSecurityPolicyEnforcement = this->contentSecurityPolicyEnforcement;
96 copy.initiator = this->initiator.isolatedCopy();
97 copy.filteringPolicy = this->filteringPolicy;
98
99 return copy;
100}
101
102
103RefPtr<ThreadableLoader> ThreadableLoader::create(ScriptExecutionContext& context, ThreadableLoaderClient& client, ResourceRequest&& request, const ThreadableLoaderOptions& options, String&& referrer)
104{
105 if (is<WorkerGlobalScope>(context))
106 return WorkerThreadableLoader::create(downcast<WorkerGlobalScope>(context), client, WorkerRunLoop::defaultMode(), WTFMove(request), options, WTFMove(referrer));
107
108 return DocumentThreadableLoader::create(downcast<Document>(context), client, WTFMove(request), options, WTFMove(referrer));
109}
110
111void ThreadableLoader::loadResourceSynchronously(ScriptExecutionContext& context, ResourceRequest&& request, ThreadableLoaderClient& client, const ThreadableLoaderOptions& options)
112{
113 if (is<WorkerGlobalScope>(context))
114 WorkerThreadableLoader::loadResourceSynchronously(downcast<WorkerGlobalScope>(context), WTFMove(request), client, options);
115 else
116 DocumentThreadableLoader::loadResourceSynchronously(downcast<Document>(context), WTFMove(request), client, options);
117 context.didLoadResourceSynchronously();
118}
119
120void ThreadableLoader::logError(ScriptExecutionContext& context, const ResourceError& error, const String& initiator)
121{
122 if (error.isCancellation())
123 return;
124
125 // FIXME: Some errors are returned with null URLs. This leads to poor console messages. We should do better for these errors.
126 if (error.failingURL().isNull())
127 return;
128
129 // We further reduce logging to some errors.
130 // FIXME: Log more errors when making so do not make some layout tests flaky.
131 if (error.domain() != errorDomainWebKitInternal && error.domain() != errorDomainWebKitServiceWorker && !error.isAccessControl())
132 return;
133
134 const char* messageStart;
135 if (initiator == cachedResourceRequestInitiators().eventsource)
136 messageStart = "EventSource cannot load ";
137 else if (initiator == cachedResourceRequestInitiators().fetch)
138 messageStart = "Fetch API cannot load ";
139 else if (initiator == cachedResourceRequestInitiators().xmlhttprequest)
140 messageStart = "XMLHttpRequest cannot load ";
141 else
142 messageStart = "Cannot load ";
143
144 String messageEnd = error.isAccessControl() ? " due to access control checks."_s : "."_s;
145 context.addConsoleMessage(MessageSource::JS, MessageLevel::Error, makeString(messageStart, error.failingURL().string(), messageEnd));
146}
147
148} // namespace WebCore
149