1/*
2 * Copyright (C) 2012 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "NetworkResourceLoadParameters.h"
28
29#include "WebCoreArgumentCoders.h"
30
31namespace WebKit {
32using namespace WebCore;
33
34void NetworkResourceLoadParameters::encode(IPC::Encoder& encoder) const
35{
36 encoder << identifier;
37 encoder << webPageID;
38 encoder << webFrameID;
39 encoder << parentPID;
40 encoder << sessionID;
41 encoder << request;
42
43 encoder << static_cast<bool>(request.httpBody());
44 if (request.httpBody()) {
45 request.httpBody()->encode(encoder);
46
47 const Vector<FormDataElement>& elements = request.httpBody()->elements();
48 size_t fileCount = 0;
49 for (size_t i = 0, count = elements.size(); i < count; ++i) {
50 if (WTF::holds_alternative<FormDataElement::EncodedFileData>(elements[i].data))
51 ++fileCount;
52 }
53
54 SandboxExtension::HandleArray requestBodySandboxExtensions;
55 requestBodySandboxExtensions.allocate(fileCount);
56 size_t extensionIndex = 0;
57 for (size_t i = 0, count = elements.size(); i < count; ++i) {
58 const FormDataElement& element = elements[i];
59 if (auto* fileData = WTF::get_if<FormDataElement::EncodedFileData>(element.data)) {
60 const String& path = fileData->shouldGenerateFile ? fileData->generatedFilename : fileData->filename;
61 SandboxExtension::createHandle(path, SandboxExtension::Type::ReadOnly, requestBodySandboxExtensions[extensionIndex++]);
62 }
63 }
64 encoder << requestBodySandboxExtensions;
65 }
66
67 if (request.url().isLocalFile()) {
68 SandboxExtension::Handle requestSandboxExtension;
69 SandboxExtension::createHandle(request.url().fileSystemPath(), SandboxExtension::Type::ReadOnly, requestSandboxExtension);
70 encoder << requestSandboxExtension;
71 }
72
73 encoder.encodeEnum(contentSniffingPolicy);
74 encoder.encodeEnum(contentEncodingSniffingPolicy);
75 encoder.encodeEnum(storedCredentialsPolicy);
76 encoder.encodeEnum(clientCredentialPolicy);
77 encoder.encodeEnum(shouldPreconnectOnly);
78 encoder << shouldClearReferrerOnHTTPSToHTTPRedirect;
79 encoder << needsCertificateInfo;
80 encoder << isMainFrameNavigation;
81 encoder << isMainResourceNavigationForAnyFrame;
82 encoder << maximumBufferingTime;
83
84 encoder << static_cast<bool>(sourceOrigin);
85 if (sourceOrigin)
86 encoder << *sourceOrigin;
87 encoder << options;
88 encoder << cspResponseHeaders;
89 encoder << originalRequestHeaders;
90
91 encoder << shouldRestrictHTTPResponseAccess;
92
93 encoder.encodeEnum(preflightPolicy);
94
95 encoder << shouldEnableCrossOriginResourcePolicy;
96
97 encoder << frameAncestorOrigins;
98 encoder << isHTTPSUpgradeEnabled;
99
100#if ENABLE(CONTENT_EXTENSIONS)
101 encoder << mainDocumentURL;
102 encoder << userContentControllerIdentifier;
103#endif
104}
105
106bool NetworkResourceLoadParameters::decode(IPC::Decoder& decoder, NetworkResourceLoadParameters& result)
107{
108 if (!decoder.decode(result.identifier))
109 return false;
110
111 Optional<PageIdentifier> webPageID;
112 decoder >> webPageID;
113 if (!webPageID)
114 return false;
115 result.webPageID = *webPageID;
116
117 if (!decoder.decode(result.webFrameID))
118 return false;
119
120 if (!decoder.decode(result.parentPID))
121 return false;
122
123 if (!decoder.decode(result.sessionID))
124 return false;
125
126 if (!decoder.decode(result.request))
127 return false;
128
129 bool hasHTTPBody;
130 if (!decoder.decode(hasHTTPBody))
131 return false;
132
133 if (hasHTTPBody) {
134 RefPtr<FormData> formData = FormData::decode(decoder);
135 if (!formData)
136 return false;
137 result.request.setHTTPBody(WTFMove(formData));
138
139 Optional<SandboxExtension::HandleArray> requestBodySandboxExtensionHandles;
140 decoder >> requestBodySandboxExtensionHandles;
141 if (!requestBodySandboxExtensionHandles)
142 return false;
143 for (size_t i = 0; i < requestBodySandboxExtensionHandles->size(); ++i) {
144 if (auto extension = SandboxExtension::create(WTFMove(requestBodySandboxExtensionHandles->at(i))))
145 result.requestBodySandboxExtensions.append(WTFMove(extension));
146 }
147 }
148
149 if (result.request.url().isLocalFile()) {
150 Optional<SandboxExtension::Handle> resourceSandboxExtensionHandle;
151 decoder >> resourceSandboxExtensionHandle;
152 if (!resourceSandboxExtensionHandle)
153 return false;
154 result.resourceSandboxExtension = SandboxExtension::create(WTFMove(*resourceSandboxExtensionHandle));
155 }
156
157 if (!decoder.decodeEnum(result.contentSniffingPolicy))
158 return false;
159 if (!decoder.decodeEnum(result.contentEncodingSniffingPolicy))
160 return false;
161 if (!decoder.decodeEnum(result.storedCredentialsPolicy))
162 return false;
163 if (!decoder.decodeEnum(result.clientCredentialPolicy))
164 return false;
165 if (!decoder.decodeEnum(result.shouldPreconnectOnly))
166 return false;
167 if (!decoder.decode(result.shouldClearReferrerOnHTTPSToHTTPRedirect))
168 return false;
169 if (!decoder.decode(result.needsCertificateInfo))
170 return false;
171 if (!decoder.decode(result.isMainFrameNavigation))
172 return false;
173 if (!decoder.decode(result.isMainResourceNavigationForAnyFrame))
174 return false;
175 if (!decoder.decode(result.maximumBufferingTime))
176 return false;
177
178 bool hasSourceOrigin;
179 if (!decoder.decode(hasSourceOrigin))
180 return false;
181 if (hasSourceOrigin) {
182 result.sourceOrigin = SecurityOrigin::decode(decoder);
183 if (!result.sourceOrigin)
184 return false;
185 }
186
187 Optional<FetchOptions> options;
188 decoder >> options;
189 if (!options)
190 return false;
191 result.options = *options;
192
193 if (!decoder.decode(result.cspResponseHeaders))
194 return false;
195 if (!decoder.decode(result.originalRequestHeaders))
196 return false;
197
198 Optional<bool> shouldRestrictHTTPResponseAccess;
199 decoder >> shouldRestrictHTTPResponseAccess;
200 if (!shouldRestrictHTTPResponseAccess)
201 return false;
202 result.shouldRestrictHTTPResponseAccess = *shouldRestrictHTTPResponseAccess;
203
204 if (!decoder.decodeEnum(result.preflightPolicy))
205 return false;
206
207 Optional<bool> shouldEnableCrossOriginResourcePolicy;
208 decoder >> shouldEnableCrossOriginResourcePolicy;
209 if (!shouldEnableCrossOriginResourcePolicy)
210 return false;
211 result.shouldEnableCrossOriginResourcePolicy = *shouldEnableCrossOriginResourcePolicy;
212
213 if (!decoder.decode(result.frameAncestorOrigins))
214 return false;
215
216 Optional<bool> isHTTPSUpgradeEnabled;
217 decoder >> isHTTPSUpgradeEnabled;
218 if (!isHTTPSUpgradeEnabled)
219 return false;
220 result.isHTTPSUpgradeEnabled = *isHTTPSUpgradeEnabled;
221
222#if ENABLE(CONTENT_EXTENSIONS)
223 if (!decoder.decode(result.mainDocumentURL))
224 return false;
225
226 Optional<Optional<UserContentControllerIdentifier>> userContentControllerIdentifier;
227 decoder >> userContentControllerIdentifier;
228 if (!userContentControllerIdentifier)
229 return false;
230 result.userContentControllerIdentifier = *userContentControllerIdentifier;
231#endif
232
233 return true;
234}
235
236} // namespace WebKit
237