Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(369)

Side by Side Diff: third_party/WebKit/Source/platform/mojo/FetchAPIRequestStructTraits.cpp

Issue 2762303002: Enable transmitting Fetch Requests over Mojo (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "platform/mojo/FetchAPIRequestStructTraits.h"
6
7 #include "mojo/public/cpp/bindings/map_traits_wtf_hash_map.h"
8 #include "mojo/public/cpp/bindings/string_traits_wtf.h"
9 #include "platform/blob/BlobData.h"
10 #include "platform/mojo/KURLStructTraits.h"
11 #include "platform/mojo/ReferrerStructTraits.h"
12 #include "platform/weborigin/Referrer.h"
13 #include "public/platform/WebReferrerPolicy.h"
14
15 namespace mojo {
16
17 namespace {
18
19 // Struct traits context for the FetchAPIRequest type. Since getters are invoked
20 // twice when serializing the type, this reduces the load for heavy members.
21 struct FetchAPIRequestStructTraitsContext {
22 FetchAPIRequestStructTraitsContext() = default;
23 ~FetchAPIRequestStructTraitsContext() = default;
24
25 WTF::HashMap<WTF::String, WTF::String> headers;
26 };
27
28 } // namespace
29
30 using blink::mojom::FetchCredentialsMode;
31 using blink::mojom::FetchRedirectMode;
32 using blink::mojom::FetchRequestMode;
33 using blink::mojom::RequestContextFrameType;
34 using blink::mojom::RequestContextType;
35
36 FetchCredentialsMode
37 EnumTraits<FetchCredentialsMode, blink::WebURLRequest::FetchCredentialsMode>::
38 ToMojom(blink::WebURLRequest::FetchCredentialsMode input) {
39 switch (input) {
40 case blink::WebURLRequest::FetchCredentialsModeOmit:
41 return FetchCredentialsMode::OMIT;
42 case blink::WebURLRequest::FetchCredentialsModeSameOrigin:
43 return FetchCredentialsMode::SAME_ORIGIN;
44 case blink::WebURLRequest::FetchCredentialsModeInclude:
45 return FetchCredentialsMode::INCLUDE;
46 case blink::WebURLRequest::FetchCredentialsModePassword:
47 return FetchCredentialsMode::PASSWORD;
48 }
49
50 NOTREACHED();
51 return FetchCredentialsMode::OMIT;
52 }
53
54 bool EnumTraits<FetchCredentialsMode,
55 blink::WebURLRequest::FetchCredentialsMode>::
56 FromMojom(FetchCredentialsMode input,
57 blink::WebURLRequest::FetchCredentialsMode* out) {
58 switch (input) {
59 case FetchCredentialsMode::OMIT:
60 *out = blink::WebURLRequest::FetchCredentialsModeOmit;
61 return true;
62 case FetchCredentialsMode::SAME_ORIGIN:
63 *out = blink::WebURLRequest::FetchCredentialsModeSameOrigin;
64 return true;
65 case FetchCredentialsMode::INCLUDE:
66 *out = blink::WebURLRequest::FetchCredentialsModeInclude;
67 return true;
68 case FetchCredentialsMode::PASSWORD:
69 *out = blink::WebURLRequest::FetchCredentialsModePassword;
70 return true;
71 }
72
73 return false;
74 }
75
76 FetchRedirectMode
77 EnumTraits<FetchRedirectMode, blink::WebURLRequest::FetchRedirectMode>::ToMojom(
78 blink::WebURLRequest::FetchRedirectMode input) {
79 switch (input) {
80 case blink::WebURLRequest::FetchRedirectModeFollow:
81 return FetchRedirectMode::FOLLOW;
82 case blink::WebURLRequest::FetchRedirectModeError:
83 return FetchRedirectMode::ERROR;
84 case blink::WebURLRequest::FetchRedirectModeManual:
85 return FetchRedirectMode::MANUAL;
86 }
87
88 NOTREACHED();
89 return FetchRedirectMode::ERROR;
90 }
91
92 bool EnumTraits<FetchRedirectMode, blink::WebURLRequest::FetchRedirectMode>::
93 FromMojom(FetchRedirectMode input,
94 blink::WebURLRequest::FetchRedirectMode* out) {
95 switch (input) {
96 case FetchRedirectMode::FOLLOW:
97 *out = blink::WebURLRequest::FetchRedirectModeFollow;
98 return true;
99 case FetchRedirectMode::ERROR:
100 *out = blink::WebURLRequest::FetchRedirectModeError;
101 return true;
102 case FetchRedirectMode::MANUAL:
103 *out = blink::WebURLRequest::FetchRedirectModeManual;
104 return true;
105 }
106
107 return false;
108 }
109
110 FetchRequestMode
111 EnumTraits<FetchRequestMode, blink::WebURLRequest::FetchRequestMode>::ToMojom(
112 blink::WebURLRequest::FetchRequestMode input) {
113 switch (input) {
114 case blink::WebURLRequest::FetchRequestModeSameOrigin:
115 return FetchRequestMode::SAME_ORIGIN;
116 case blink::WebURLRequest::FetchRequestModeNoCORS:
117 return FetchRequestMode::NO_CORS;
118 case blink::WebURLRequest::FetchRequestModeCORS:
119 return FetchRequestMode::CORS;
120 case blink::WebURLRequest::FetchRequestModeCORSWithForcedPreflight:
121 return FetchRequestMode::CORS_WITH_FORCED_PREFLIGHT;
122 case blink::WebURLRequest::FetchRequestModeNavigate:
123 return FetchRequestMode::NAVIGATE;
124 }
125
126 NOTREACHED();
127 return FetchRequestMode::NO_CORS;
128 }
129
130 bool EnumTraits<FetchRequestMode, blink::WebURLRequest::FetchRequestMode>::
131 FromMojom(FetchRequestMode input,
132 blink::WebURLRequest::FetchRequestMode* out) {
133 switch (input) {
134 case FetchRequestMode::SAME_ORIGIN:
135 *out = blink::WebURLRequest::FetchRequestModeSameOrigin;
136 return true;
137 case FetchRequestMode::NO_CORS:
138 *out = blink::WebURLRequest::FetchRequestModeNoCORS;
139 return true;
140 case FetchRequestMode::CORS:
141 *out = blink::WebURLRequest::FetchRequestModeCORS;
142 return true;
143 case FetchRequestMode::CORS_WITH_FORCED_PREFLIGHT:
144 *out = blink::WebURLRequest::FetchRequestModeCORSWithForcedPreflight;
145 return true;
146 case FetchRequestMode::NAVIGATE:
147 *out = blink::WebURLRequest::FetchRequestModeNavigate;
148 return true;
149 }
150
151 return false;
152 }
153
154 RequestContextFrameType
155 EnumTraits<RequestContextFrameType, blink::WebURLRequest::FrameType>::ToMojom(
156 blink::WebURLRequest::FrameType input) {
157 switch (input) {
158 case blink::WebURLRequest::FrameTypeAuxiliary:
159 return RequestContextFrameType::AUXILIARY;
160 case blink::WebURLRequest::FrameTypeNested:
161 return RequestContextFrameType::NESTED;
162 case blink::WebURLRequest::FrameTypeNone:
163 return RequestContextFrameType::NONE;
164 case blink::WebURLRequest::FrameTypeTopLevel:
165 return RequestContextFrameType::TOP_LEVEL;
166 }
167
168 NOTREACHED();
169 return RequestContextFrameType::NONE;
170 }
171
172 bool EnumTraits<RequestContextFrameType, blink::WebURLRequest::FrameType>::
173 FromMojom(RequestContextFrameType input,
174 blink::WebURLRequest::FrameType* out) {
175 switch (input) {
176 case RequestContextFrameType::AUXILIARY:
177 *out = blink::WebURLRequest::FrameTypeAuxiliary;
178 return true;
179 case RequestContextFrameType::NESTED:
180 *out = blink::WebURLRequest::FrameTypeNested;
181 return true;
182 case RequestContextFrameType::NONE:
183 *out = blink::WebURLRequest::FrameTypeNone;
184 return true;
185 case RequestContextFrameType::TOP_LEVEL:
186 *out = blink::WebURLRequest::FrameTypeTopLevel;
187 return true;
188 }
189
190 return false;
191 }
192
193 RequestContextType
194 EnumTraits<RequestContextType, blink::WebURLRequest::RequestContext>::ToMojom(
195 blink::WebURLRequest::RequestContext input) {
196 switch (input) {
197 case blink::WebURLRequest::RequestContextUnspecified:
198 return RequestContextType::UNSPECIFIED;
199 case blink::WebURLRequest::RequestContextAudio:
200 return RequestContextType::AUDIO;
201 case blink::WebURLRequest::RequestContextBeacon:
202 return RequestContextType::BEACON;
203 case blink::WebURLRequest::RequestContextCSPReport:
204 return RequestContextType::CSP_REPORT;
205 case blink::WebURLRequest::RequestContextDownload:
206 return RequestContextType::DOWNLOAD;
207 case blink::WebURLRequest::RequestContextEmbed:
208 return RequestContextType::EMBED;
209 case blink::WebURLRequest::RequestContextEventSource:
210 return RequestContextType::EVENT_SOURCE;
211 case blink::WebURLRequest::RequestContextFavicon:
212 return RequestContextType::FAVICON;
213 case blink::WebURLRequest::RequestContextFetch:
214 return RequestContextType::FETCH;
215 case blink::WebURLRequest::RequestContextFont:
216 return RequestContextType::FONT;
217 case blink::WebURLRequest::RequestContextForm:
218 return RequestContextType::FORM;
219 case blink::WebURLRequest::RequestContextFrame:
220 return RequestContextType::FRAME;
221 case blink::WebURLRequest::RequestContextHyperlink:
222 return RequestContextType::HYPERLINK;
223 case blink::WebURLRequest::RequestContextIframe:
224 return RequestContextType::IFRAME;
225 case blink::WebURLRequest::RequestContextImage:
226 return RequestContextType::IMAGE;
227 case blink::WebURLRequest::RequestContextImageSet:
228 return RequestContextType::IMAGE_SET;
229 case blink::WebURLRequest::RequestContextImport:
230 return RequestContextType::IMPORT;
231 case blink::WebURLRequest::RequestContextInternal:
232 return RequestContextType::INTERNAL;
233 case blink::WebURLRequest::RequestContextLocation:
234 return RequestContextType::LOCATION;
235 case blink::WebURLRequest::RequestContextManifest:
236 return RequestContextType::MANIFEST;
237 case blink::WebURLRequest::RequestContextObject:
238 return RequestContextType::OBJECT;
239 case blink::WebURLRequest::RequestContextPing:
240 return RequestContextType::PING;
241 case blink::WebURLRequest::RequestContextPlugin:
242 return RequestContextType::PLUGIN;
243 case blink::WebURLRequest::RequestContextPrefetch:
244 return RequestContextType::PREFETCH;
245 case blink::WebURLRequest::RequestContextScript:
246 return RequestContextType::SCRIPT;
247 case blink::WebURLRequest::RequestContextServiceWorker:
248 return RequestContextType::SERVICE_WORKER;
249 case blink::WebURLRequest::RequestContextSharedWorker:
250 return RequestContextType::SHARED_WORKER;
251 case blink::WebURLRequest::RequestContextSubresource:
252 return RequestContextType::SUBRESOURCE;
253 case blink::WebURLRequest::RequestContextStyle:
254 return RequestContextType::STYLE;
255 case blink::WebURLRequest::RequestContextTrack:
256 return RequestContextType::TRACK;
257 case blink::WebURLRequest::RequestContextVideo:
258 return RequestContextType::VIDEO;
259 case blink::WebURLRequest::RequestContextWorker:
260 return RequestContextType::WORKER;
261 case blink::WebURLRequest::RequestContextXMLHttpRequest:
262 return RequestContextType::XML_HTTP_REQUEST;
263 case blink::WebURLRequest::RequestContextXSLT:
264 return RequestContextType::XSLT;
265 }
266
267 NOTREACHED();
268 return RequestContextType::UNSPECIFIED;
269 }
270
271 bool EnumTraits<RequestContextType, blink::WebURLRequest::RequestContext>::
272 FromMojom(RequestContextType input,
273 blink::WebURLRequest::RequestContext* out) {
274 switch (input) {
275 case RequestContextType::UNSPECIFIED:
276 *out = blink::WebURLRequest::RequestContextUnspecified;
277 return true;
278 case RequestContextType::AUDIO:
279 *out = blink::WebURLRequest::RequestContextAudio;
280 return true;
281 case RequestContextType::BEACON:
282 *out = blink::WebURLRequest::RequestContextBeacon;
283 return true;
284 case RequestContextType::CSP_REPORT:
285 *out = blink::WebURLRequest::RequestContextCSPReport;
286 return true;
287 case RequestContextType::DOWNLOAD:
288 *out = blink::WebURLRequest::RequestContextDownload;
289 return true;
290 case RequestContextType::EMBED:
291 *out = blink::WebURLRequest::RequestContextEmbed;
292 return true;
293 case RequestContextType::EVENT_SOURCE:
294 *out = blink::WebURLRequest::RequestContextEventSource;
295 return true;
296 case RequestContextType::FAVICON:
297 *out = blink::WebURLRequest::RequestContextFavicon;
298 return true;
299 case RequestContextType::FETCH:
300 *out = blink::WebURLRequest::RequestContextFetch;
301 return true;
302 case RequestContextType::FONT:
303 *out = blink::WebURLRequest::RequestContextFont;
304 return true;
305 case RequestContextType::FORM:
306 *out = blink::WebURLRequest::RequestContextForm;
307 return true;
308 case RequestContextType::FRAME:
309 *out = blink::WebURLRequest::RequestContextFrame;
310 return true;
311 case RequestContextType::HYPERLINK:
312 *out = blink::WebURLRequest::RequestContextHyperlink;
313 return true;
314 case RequestContextType::IFRAME:
315 *out = blink::WebURLRequest::RequestContextIframe;
316 return true;
317 case RequestContextType::IMAGE:
318 *out = blink::WebURLRequest::RequestContextImage;
319 return true;
320 case RequestContextType::IMAGE_SET:
321 *out = blink::WebURLRequest::RequestContextImageSet;
322 return true;
323 case RequestContextType::IMPORT:
324 *out = blink::WebURLRequest::RequestContextImport;
325 return true;
326 case RequestContextType::INTERNAL:
327 *out = blink::WebURLRequest::RequestContextInternal;
328 return true;
329 case RequestContextType::LOCATION:
330 *out = blink::WebURLRequest::RequestContextLocation;
331 return true;
332 case RequestContextType::MANIFEST:
333 *out = blink::WebURLRequest::RequestContextManifest;
334 return true;
335 case RequestContextType::OBJECT:
336 *out = blink::WebURLRequest::RequestContextObject;
337 return true;
338 case RequestContextType::PING:
339 *out = blink::WebURLRequest::RequestContextPing;
340 return true;
341 case RequestContextType::PLUGIN:
342 *out = blink::WebURLRequest::RequestContextPlugin;
343 return true;
344 case RequestContextType::PREFETCH:
345 *out = blink::WebURLRequest::RequestContextPrefetch;
346 return true;
347 case RequestContextType::SCRIPT:
348 *out = blink::WebURLRequest::RequestContextScript;
349 return true;
350 case RequestContextType::SERVICE_WORKER:
351 *out = blink::WebURLRequest::RequestContextServiceWorker;
352 return true;
353 case RequestContextType::SHARED_WORKER:
354 *out = blink::WebURLRequest::RequestContextSharedWorker;
355 return true;
356 case RequestContextType::SUBRESOURCE:
357 *out = blink::WebURLRequest::RequestContextSubresource;
358 return true;
359 case RequestContextType::STYLE:
360 *out = blink::WebURLRequest::RequestContextStyle;
361 return true;
362 case RequestContextType::TRACK:
363 *out = blink::WebURLRequest::RequestContextTrack;
364 return true;
365 case RequestContextType::VIDEO:
366 *out = blink::WebURLRequest::RequestContextVideo;
367 return true;
368 case RequestContextType::WORKER:
369 *out = blink::WebURLRequest::RequestContextWorker;
370 return true;
371 case RequestContextType::XML_HTTP_REQUEST:
372 *out = blink::WebURLRequest::RequestContextXMLHttpRequest;
373 return true;
374 case RequestContextType::XSLT:
375 *out = blink::WebURLRequest::RequestContextXSLT;
376 return true;
377 }
378
379 return false;
380 }
381
382 // static
383 void* StructTraits<blink::mojom::FetchAPIRequestDataView,
384 blink::WebServiceWorkerRequest>::
385 SetUpContext(const blink::WebServiceWorkerRequest& request) {
386 FetchAPIRequestStructTraitsContext* context =
387 new FetchAPIRequestStructTraitsContext();
388 for (const auto& pair : request.headers())
389 context->headers.insert(pair.key, pair.value);
dcheng 2017/03/28 06:52:24 Ditto here with MapTraits, or possibly typemapping
Peter Beverloo 2017/03/28 14:07:53 I believe this is also covered by crbug.com/705972
390
391 return context;
392 }
393
394 // static
395 void StructTraits<blink::mojom::FetchAPIRequestDataView,
396 blink::WebServiceWorkerRequest>::
397 TearDownContext(const blink::WebServiceWorkerRequest& request,
398 void* context) {
399 delete static_cast<FetchAPIRequestStructTraitsContext*>(context);
400 }
401
402 // static
403 blink::KURL StructTraits<blink::mojom::FetchAPIRequestDataView,
404 blink::WebServiceWorkerRequest>::
405 url(const blink::WebServiceWorkerRequest& request) {
406 return request.url();
407 }
408
409 // static
410 WTF::String StructTraits<blink::mojom::FetchAPIRequestDataView,
411 blink::WebServiceWorkerRequest>::
412 method(const blink::WebServiceWorkerRequest& request) {
413 return request.method();
414 }
415
416 // static
417 const WTF::HashMap<WTF::String, WTF::String>&
418 StructTraits<blink::mojom::FetchAPIRequestDataView,
419 blink::WebServiceWorkerRequest>::
420 headers(const blink::WebServiceWorkerRequest& request, void* context) {
421 DCHECK(context);
422 return static_cast<FetchAPIRequestStructTraitsContext*>(context)->headers;
423 }
424
425 // static
426 const blink::Referrer& StructTraits<blink::mojom::FetchAPIRequestDataView,
427 blink::WebServiceWorkerRequest>::
428 referrer(const blink::WebServiceWorkerRequest& request) {
429 return request.referrer();
430 }
431
432 // static
433 WTF::String StructTraits<blink::mojom::FetchAPIRequestDataView,
434 blink::WebServiceWorkerRequest>::
435 blob_uuid(const blink::WebServiceWorkerRequest& request) {
436 if (request.blobDataHandle())
437 return request.blobDataHandle()->uuid();
438
439 return WTF::String();
440 }
441
442 // static
443 uint64_t StructTraits<blink::mojom::FetchAPIRequestDataView,
444 blink::WebServiceWorkerRequest>::
445 blob_size(const blink::WebServiceWorkerRequest& request) {
446 if (request.blobDataHandle())
447 return request.blobDataHandle()->size();
448
449 return 0;
450 }
451
452 // static
453 WTF::String StructTraits<blink::mojom::FetchAPIRequestDataView,
454 blink::WebServiceWorkerRequest>::
455 client_id(const blink::WebServiceWorkerRequest& request) {
456 return request.clientId();
457 }
458
459 // static
460 bool StructTraits<blink::mojom::FetchAPIRequestDataView,
461 blink::WebServiceWorkerRequest>::
462 Read(blink::mojom::FetchAPIRequestDataView data,
463 blink::WebServiceWorkerRequest* out) {
464 blink::WebURLRequest::FetchRequestMode mode;
465 blink::WebURLRequest::RequestContext requestContext;
466 blink::WebURLRequest::FrameType frameType;
467 blink::KURL url;
468 WTF::String method;
469 WTF::HashMap<WTF::String, WTF::String> headers;
470 WTF::String blobUuid;
471 blink::Referrer referrer;
472 blink::WebURLRequest::FetchCredentialsMode credentialsMode;
473 blink::WebURLRequest::FetchRedirectMode redirectMode;
474 WTF::String clientId;
475
476 if (!data.ReadMode(&mode) || !data.ReadRequestContextType(&requestContext) ||
477 !data.ReadFrameType(&frameType) || !data.ReadUrl(&url) ||
478 !data.ReadMethod(&method) || !data.ReadHeaders(&headers) ||
479 !data.ReadBlobUuid(&blobUuid) || !data.ReadReferrer(&referrer) ||
480 !data.ReadCredentialsMode(&credentialsMode) ||
481 !data.ReadRedirectMode(&redirectMode) || !data.ReadClientId(&clientId)) {
482 return false;
483 }
484
485 out->setMode(mode);
486 out->setIsMainResourceLoad(data.is_main_resource_load());
487 out->setRequestContext(requestContext);
488 out->setFrameType(frameType);
489 out->setURL(url);
490 out->setMethod(method);
491 for (const auto& pair : headers)
492 out->setHeader(pair.key, pair.value);
493 out->setBlob(blobUuid, static_cast<long long>(data.blob_size()));
494 out->setReferrer(referrer.referrer, static_cast<blink::WebReferrerPolicy>(
495 referrer.referrerPolicy));
496 out->setCredentialsMode(credentialsMode);
497 out->setRedirectMode(redirectMode);
498 out->setClientId(clientId);
499 out->setIsReload(data.is_reload());
500 return true;
501 }
502
503 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698