5ba52c90aeb0fead6f448d7086f59a2ae6e9e952
[WebKit-https.git] / Source / WebKit / Shared / mac / WebCoreArgumentCodersMac.mm
1 /*
2  * Copyright (C) 2010-2018 Apple Inc. All rights reserved.
3  * Copyright (C) 2013 Company 100 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. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #import "config.h"
28 #import "WebCoreArgumentCoders.h"
29
30 #import "ArgumentCodersCF.h"
31 #import "ArgumentCodersCocoa.h"
32 #import "DataReference.h"
33 #import <WebCore/CertificateInfo.h>
34 #import <WebCore/ContentFilterUnblockHandler.h>
35 #import <WebCore/Credential.h>
36 #import <WebCore/KeyboardEvent.h>
37 #import <WebCore/ProtectionSpace.h>
38 #import <WebCore/ResourceError.h>
39 #import <WebCore/ResourceRequest.h>
40 #import <pal/spi/cf/CFNetworkSPI.h>
41 #import <pal/spi/cocoa/NSKeyedArchiverSPI.h>
42 #import <wtf/MachSendRight.h>
43
44 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
45 #import <WebCore/MediaPlaybackTargetContext.h>
46 #import <objc/runtime.h>
47
48 #import <pal/cocoa/AVFoundationSoftLink.h>
49 #endif
50
51 namespace IPC {
52
53 static RetainPtr<CFMutableDictionaryRef> createSerializableRepresentation(CFIndex version, CFTypeRef* objects, CFIndex objectCount, CFDictionaryRef protocolProperties, CFNumberRef expectedContentLength, CFStringRef mimeType, CFTypeRef tokenNull)
54 {
55     auto archiveListArray = adoptCF(CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks));
56
57     for (CFIndex i = 0; i < objectCount; ++i) {
58         CFTypeRef object = objects[i];
59         if (object) {
60             CFArrayAppendValue(archiveListArray.get(), object);
61             CFRelease(object);
62         } else {
63             // Append our token null representation.
64             CFArrayAppendValue(archiveListArray.get(), tokenNull);
65         }
66     }
67
68     auto dictionary = adoptCF(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
69
70     auto versionNumber = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &version));
71     CFDictionarySetValue(dictionary.get(), CFSTR("version"), versionNumber.get());
72     CFDictionarySetValue(dictionary.get(), CFSTR("archiveList"), archiveListArray.get());
73
74     if (protocolProperties) {
75         CFDictionarySetValue(dictionary.get(), CFSTR("protocolProperties"), protocolProperties);
76         CFRelease(protocolProperties);
77     }
78
79     if (expectedContentLength) {
80         CFDictionarySetValue(dictionary.get(), CFSTR("expectedContentLength"), expectedContentLength);
81         CFRelease(expectedContentLength);
82     }
83
84     if (mimeType) {
85         CFDictionarySetValue(dictionary.get(), CFSTR("mimeType"), mimeType);
86         CFRelease(mimeType);
87     }
88
89     CFAllocatorDeallocate(kCFAllocatorDefault, objects);
90
91     return dictionary;
92 }
93
94 static CFTypeRef dictionaryValueOfType(CFDictionaryRef dictionary, CFStringRef key, CFTypeID type)
95 {
96     CFTypeRef value = CFDictionaryGetValue(dictionary, key);
97     if (value && CFGetTypeID(value) == type)
98         return value;
99     return nullptr;
100 }
101
102 static bool createArchiveList(CFDictionaryRef representation, CFTypeRef tokenNull, CFIndex* version, CFTypeRef** objects, CFIndex* objectCount, CFDictionaryRef* protocolProperties, CFNumberRef* expectedContentLength, CFStringRef* mimeType)
103 {
104     CFNumberRef versionNumber = (CFNumberRef)dictionaryValueOfType(representation, CFSTR("version"), CFNumberGetTypeID());
105     if (!versionNumber)
106         return false;
107
108     if (!CFNumberGetValue(versionNumber, kCFNumberCFIndexType, version))
109         return false;
110
111     CFArrayRef archiveListArray = (CFArrayRef)dictionaryValueOfType(representation, CFSTR("archiveList"), CFArrayGetTypeID());
112     if (!archiveListArray)
113         return false;
114
115     *objectCount = CFArrayGetCount(archiveListArray);
116     *objects = (CFTypeRef*)malloc(sizeof(CFTypeRef) * *objectCount);
117     for (CFIndex i = 0; i < *objectCount; ++i) {
118         CFTypeRef object = CFArrayGetValueAtIndex(archiveListArray, i);
119         if (object == tokenNull)
120             (*objects)[i] = nullptr;
121         else
122             (*objects)[i] = object;
123     }
124
125     if (protocolProperties)
126         *protocolProperties = (CFDictionaryRef)dictionaryValueOfType(representation, CFSTR("protocolProperties"), CFDictionaryGetTypeID());
127
128     if (expectedContentLength)
129         *expectedContentLength = (CFNumberRef)dictionaryValueOfType(representation, CFSTR("expectedContentLength"), CFNumberGetTypeID());
130
131     if (mimeType)
132         *mimeType = (CFStringRef)dictionaryValueOfType(representation, CFSTR("mimeType"), CFStringGetTypeID());
133
134     return true;
135 }
136
137 static RetainPtr<CFDictionaryRef> createSerializableRepresentation(CFURLRequestRef cfRequest, CFTypeRef tokenNull)
138 {
139     CFIndex version;
140     CFTypeRef* objects;
141     CFIndex objectCount;
142     CFDictionaryRef protocolProperties;
143
144     // FIXME (12889518): Do not serialize HTTP message body.
145     // 1. It can be large and thus costly to send across.
146     // 2. It is misleading to provide a body with some requests, while others use body streams, which cannot be serialized at all.
147
148     _CFURLRequestCreateArchiveList(kCFAllocatorDefault, cfRequest, &version, &objects, &objectCount, &protocolProperties);
149
150     // This will deallocate the passed in arguments.
151     return createSerializableRepresentation(version, objects, objectCount, protocolProperties, nullptr, nullptr, tokenNull);
152 }
153
154 static RetainPtr<CFURLRequestRef> createCFURLRequestFromSerializableRepresentation(CFDictionaryRef representation, CFTypeRef tokenNull)
155 {
156     CFIndex version;
157     CFTypeRef* objects;
158     CFIndex objectCount;
159     CFDictionaryRef protocolProperties;
160
161     if (!createArchiveList(representation, tokenNull, &version, &objects, &objectCount, &protocolProperties, nullptr, nullptr))
162         return nullptr;
163
164     auto cfRequest = adoptCF(_CFURLRequestCreateFromArchiveList(kCFAllocatorDefault, version, objects, objectCount, protocolProperties));
165     free(objects);
166     return WTFMove(cfRequest);
167 }
168
169 static RetainPtr<CFDictionaryRef> createSerializableRepresentation(NSURLRequest *request, CFTypeRef tokenNull)
170 {
171     return createSerializableRepresentation([request _CFURLRequest], tokenNull);
172 }
173
174 static RetainPtr<NSURLRequest> createNSURLRequestFromSerializableRepresentation(CFDictionaryRef representation, CFTypeRef tokenNull)
175 {
176     auto cfRequest = createCFURLRequestFromSerializableRepresentation(representation, tokenNull);
177     if (!cfRequest)
178         return nullptr;
179     
180     return adoptNS([[NSURLRequest alloc] _initWithCFURLRequest:cfRequest.get()]);
181 }
182     
183 void ArgumentCoder<WebCore::ResourceRequest>::encodePlatformData(Encoder& encoder, const WebCore::ResourceRequest& resourceRequest)
184 {
185     RetainPtr<NSURLRequest> requestToSerialize = resourceRequest.nsURLRequest(WebCore::HTTPBodyUpdatePolicy::DoNotUpdateHTTPBody);
186
187     bool requestIsPresent = requestToSerialize;
188     encoder << requestIsPresent;
189
190     if (!requestIsPresent)
191         return;
192
193     // We don't send HTTP body over IPC for better performance.
194     // Also, it's not always possible to do, as streams can only be created in process that does networking.
195     if ([requestToSerialize HTTPBody] || [requestToSerialize HTTPBodyStream]) {
196         requestToSerialize = adoptNS([requestToSerialize mutableCopy]);
197         [(NSMutableURLRequest *)requestToSerialize setHTTPBody:nil];
198         [(NSMutableURLRequest *)requestToSerialize setHTTPBodyStream:nil];
199     }
200
201     RetainPtr<CFDictionaryRef> dictionary = createSerializableRepresentation(requestToSerialize.get(), IPC::tokenNullTypeRef());
202     IPC::encode(encoder, dictionary.get());
203
204     // The fallback array is part of NSURLRequest, but it is not encoded by WKNSURLRequestCreateSerializableRepresentation.
205     encoder << resourceRequest.responseContentDispositionEncodingFallbackArray();
206     encoder.encodeEnum(resourceRequest.requester());
207     encoder.encodeEnum(resourceRequest.cachePolicy());
208 }
209
210 bool ArgumentCoder<WebCore::ResourceRequest>::decodePlatformData(Decoder& decoder, WebCore::ResourceRequest& resourceRequest)
211 {
212     bool requestIsPresent;
213     if (!decoder.decode(requestIsPresent))
214         return false;
215
216     if (!requestIsPresent) {
217         resourceRequest = WebCore::ResourceRequest();
218         return true;
219     }
220
221     RetainPtr<CFDictionaryRef> dictionary;
222     if (!IPC::decode(decoder, dictionary))
223         return false;
224
225     RetainPtr<NSURLRequest> nsURLRequest = createNSURLRequestFromSerializableRepresentation(dictionary.get(), IPC::tokenNullTypeRef());
226     if (!nsURLRequest)
227         return false;
228
229     resourceRequest = WebCore::ResourceRequest(nsURLRequest.get());
230     
231     Vector<String> responseContentDispositionEncodingFallbackArray;
232     if (!decoder.decode(responseContentDispositionEncodingFallbackArray))
233         return false;
234
235     resourceRequest.setResponseContentDispositionEncodingFallbackArray(
236         responseContentDispositionEncodingFallbackArray.size() > 0 ? responseContentDispositionEncodingFallbackArray[0] : String(),
237         responseContentDispositionEncodingFallbackArray.size() > 1 ? responseContentDispositionEncodingFallbackArray[1] : String(),
238         responseContentDispositionEncodingFallbackArray.size() > 2 ? responseContentDispositionEncodingFallbackArray[2] : String()
239     );
240
241     WebCore::ResourceRequest::Requester requester;
242     if (!decoder.decodeEnum(requester))
243         return false;
244     resourceRequest.setRequester(requester);
245
246     WebCore::ResourceRequestCachePolicy cachePolicy;
247     if (!decoder.decodeEnum(cachePolicy))
248         return false;
249     resourceRequest.setCachePolicy(cachePolicy);
250
251     return true;
252 }
253
254 void ArgumentCoder<WebCore::CertificateInfo>::encode(Encoder& encoder, const WebCore::CertificateInfo& certificateInfo)
255 {
256     encoder.encodeEnum(certificateInfo.type());
257
258     switch (certificateInfo.type()) {
259 #if HAVE(SEC_TRUST_SERIALIZATION)
260     case WebCore::CertificateInfo::Type::Trust:
261         IPC::encode(encoder, certificateInfo.trust());
262         break;
263 #endif
264     case WebCore::CertificateInfo::Type::CertificateChain:
265         IPC::encode(encoder, certificateInfo.certificateChain());
266         break;
267     case WebCore::CertificateInfo::Type::None:
268         // Do nothing.
269         break;
270     }
271 }
272
273 bool ArgumentCoder<WebCore::CertificateInfo>::decode(Decoder& decoder, WebCore::CertificateInfo& certificateInfo)
274 {
275     WebCore::CertificateInfo::Type certificateInfoType;
276     if (!decoder.decodeEnum(certificateInfoType))
277         return false;
278
279     switch (certificateInfoType) {
280 #if HAVE(SEC_TRUST_SERIALIZATION)
281     case WebCore::CertificateInfo::Type::Trust: {
282         RetainPtr<SecTrustRef> trust;
283         if (!IPC::decode(decoder, trust))
284             return false;
285
286         certificateInfo = WebCore::CertificateInfo(WTFMove(trust));
287         return true;
288     }
289 #endif
290     case WebCore::CertificateInfo::Type::CertificateChain: {
291         RetainPtr<CFArrayRef> certificateChain;
292         if (!IPC::decode(decoder, certificateChain))
293             return false;
294
295         certificateInfo = WebCore::CertificateInfo(WTFMove(certificateChain));
296         return true;
297     }    
298     case WebCore::CertificateInfo::Type::None:
299         // Do nothing.
300         break;
301     }
302
303     return true;
304 }
305
306 static void encodeNSError(Encoder& encoder, NSError *nsError)
307 {
308     String domain = [nsError domain];
309     encoder << domain;
310
311     int64_t code = [nsError code];
312     encoder << code;
313
314     NSDictionary *userInfo = [nsError userInfo];
315
316     RetainPtr<CFMutableDictionaryRef> filteredUserInfo = adoptCF(CFDictionaryCreateMutable(kCFAllocatorDefault, userInfo.count, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
317
318     [userInfo enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL*) {
319         if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSURL class]] || [value isKindOfClass:[NSNumber class]])
320             CFDictionarySetValue(filteredUserInfo.get(), (__bridge CFTypeRef)key, (__bridge CFTypeRef)value);
321     }];
322
323     if (NSArray *clientIdentityAndCertificates = [userInfo objectForKey:@"NSErrorClientCertificateChainKey"]) {
324         ASSERT([clientIdentityAndCertificates isKindOfClass:[NSArray class]]);
325         ASSERT(^{
326             for (id object in clientIdentityAndCertificates) {
327                 if (CFGetTypeID((__bridge CFTypeRef)object) != SecIdentityGetTypeID() && CFGetTypeID((__bridge CFTypeRef)object) != SecCertificateGetTypeID())
328                     return false;
329             }
330             return true;
331         }());
332
333         // Turn SecIdentity members into SecCertificate to strip out private key information.
334         id clientCertificates = [NSMutableArray arrayWithCapacity:clientIdentityAndCertificates.count];
335         for (id object in clientIdentityAndCertificates) {
336             if (CFGetTypeID((__bridge CFTypeRef)object) != SecIdentityGetTypeID()) {
337                 [clientCertificates addObject:object];
338                 continue;
339             }
340             SecCertificateRef certificate = nil;
341             OSStatus status = SecIdentityCopyCertificate((SecIdentityRef)object, &certificate);
342             RetainPtr<SecCertificateRef> retainCertificate = adoptCF(certificate);
343             // The SecIdentity member is the key information of this attribute. Without it, we should nil
344             // the attribute.
345             if (status != errSecSuccess) {
346                 LOG_ERROR("Failed to encode nsError.userInfo[NSErrorClientCertificateChainKey]: %d", status);
347                 clientCertificates = nil;
348                 break;
349             }
350             [clientCertificates addObject:(__bridge id)certificate];
351         }
352         CFDictionarySetValue(filteredUserInfo.get(), CFSTR("NSErrorClientCertificateChainKey"), (__bridge CFTypeRef)clientCertificates);
353     }
354
355     id peerCertificateChain = [userInfo objectForKey:@"NSErrorPeerCertificateChainKey"];
356     if (!peerCertificateChain) {
357         if (SecTrustRef peerTrust = (__bridge SecTrustRef)[userInfo objectForKey:NSURLErrorFailingURLPeerTrustErrorKey]) {
358             CFIndex count = SecTrustGetCertificateCount(peerTrust);
359             peerCertificateChain = [NSMutableArray arrayWithCapacity:count];
360             for (CFIndex i = 0; i < count; ++i)
361                 [peerCertificateChain addObject:(__bridge id)SecTrustGetCertificateAtIndex(peerTrust, i)];
362         }
363     }
364     ASSERT(!peerCertificateChain || [peerCertificateChain isKindOfClass:[NSArray class]]);
365     if (peerCertificateChain)
366         CFDictionarySetValue(filteredUserInfo.get(), CFSTR("NSErrorPeerCertificateChainKey"), (__bridge CFTypeRef)peerCertificateChain);
367
368 #if HAVE(SEC_TRUST_SERIALIZATION)
369     if (SecTrustRef peerTrust = (__bridge SecTrustRef)[userInfo objectForKey:NSURLErrorFailingURLPeerTrustErrorKey])
370         CFDictionarySetValue(filteredUserInfo.get(), (__bridge CFStringRef)NSURLErrorFailingURLPeerTrustErrorKey, peerTrust);
371 #endif
372
373     IPC::encode(encoder, filteredUserInfo.get());
374
375     if (id underlyingError = [userInfo objectForKey:NSUnderlyingErrorKey]) {
376         ASSERT([underlyingError isKindOfClass:[NSError class]]);
377         encoder << true;
378         encodeNSError(encoder, underlyingError);
379     } else
380         encoder << false;
381 }
382
383 void ArgumentCoder<WebCore::ResourceError>::encodePlatformData(Encoder& encoder, const WebCore::ResourceError& resourceError)
384 {
385     encodeNSError(encoder, resourceError.nsError());
386 }
387
388 static bool decodeNSError(Decoder& decoder, RetainPtr<NSError>& nsError)
389 {
390     String domain;
391     if (!decoder.decode(domain))
392         return false;
393
394     int64_t code;
395     if (!decoder.decode(code))
396         return false;
397
398     RetainPtr<CFDictionaryRef> userInfo;
399     if (!IPC::decode(decoder, userInfo))
400         return false;
401
402     bool hasUnderlyingError = false;
403     if (!decoder.decode(hasUnderlyingError))
404         return false;
405
406     if (hasUnderlyingError) {
407         RetainPtr<NSError> underlyingNSError;
408         if (!decodeNSError(decoder, underlyingNSError))
409             return false;
410
411         auto mutableUserInfo = adoptCF(CFDictionaryCreateMutableCopy(kCFAllocatorDefault, CFDictionaryGetCount(userInfo.get()) + 1, userInfo.get()));
412         CFDictionarySetValue(mutableUserInfo.get(), (__bridge CFStringRef)NSUnderlyingErrorKey, (__bridge CFTypeRef)underlyingNSError.get());
413         userInfo = WTFMove(mutableUserInfo);
414     }
415
416     nsError = adoptNS([[NSError alloc] initWithDomain:domain code:code userInfo:(__bridge NSDictionary *)userInfo.get()]);
417     return true;
418 }
419
420 bool ArgumentCoder<WebCore::ResourceError>::decodePlatformData(Decoder& decoder, WebCore::ResourceError& resourceError)
421 {
422     RetainPtr<NSError> nsError;
423     if (!decodeNSError(decoder, nsError))
424         return false;
425
426     resourceError = WebCore::ResourceError(nsError.get());
427     return true;
428 }
429
430 void ArgumentCoder<WebCore::ProtectionSpace>::encodePlatformData(Encoder& encoder, const WebCore::ProtectionSpace& space)
431 {
432     encoder << space.nsSpace();
433 }
434
435 bool ArgumentCoder<WebCore::ProtectionSpace>::decodePlatformData(Decoder& decoder, WebCore::ProtectionSpace& space)
436 {
437     auto platformData = IPC::decode<NSURLProtectionSpace>(decoder);
438     if (!platformData)
439         return false;
440
441     space = WebCore::ProtectionSpace { platformData->get() };
442     return true;
443 }
444
445 void ArgumentCoder<WebCore::Credential>::encodePlatformData(Encoder& encoder, const WebCore::Credential& credential)
446 {
447     NSURLCredential *nsCredential = credential.nsCredential();
448     // NSURLCredential doesn't serialize identities correctly, so we encode the pieces individually
449     // in the identity case. See <rdar://problem/18802434>.
450     if (SecIdentityRef identity = nsCredential.identity) {
451         encoder << true;
452         IPC::encode(encoder, identity);
453
454         if (NSArray *certificates = nsCredential.certificates) {
455             encoder << true;
456             IPC::encode(encoder, (__bridge CFArrayRef)certificates);
457         } else
458             encoder << false;
459
460         encoder << static_cast<uint64_t>(nsCredential.persistence);
461         return;
462     }
463
464     encoder << false;
465     encoder << nsCredential;
466 }
467
468 bool ArgumentCoder<WebCore::Credential>::decodePlatformData(Decoder& decoder, WebCore::Credential& credential)
469 {
470     bool hasIdentity;
471     if (!decoder.decode(hasIdentity))
472         return false;
473
474     if (hasIdentity) {
475         RetainPtr<SecIdentityRef> identity;
476         if (!IPC::decode(decoder, identity))
477             return false;
478
479         RetainPtr<CFArrayRef> certificates;
480         bool hasCertificates;
481         if (!decoder.decode(hasCertificates))
482             return false;
483
484         if (hasCertificates) {
485             if (!IPC::decode(decoder, certificates))
486                 return false;
487         }
488
489         uint64_t persistence;
490         if (!decoder.decode(persistence))
491             return false;
492
493         credential = WebCore::Credential(adoptNS([[NSURLCredential alloc] initWithIdentity:identity.get() certificates:(__bridge NSArray *)certificates.get() persistence:(NSURLCredentialPersistence)persistence]).get());
494         return true;
495     }
496
497     auto nsCredential = IPC::decode<NSURLCredential>(decoder);
498     if (!nsCredential)
499         return false;
500
501     credential = WebCore::Credential { nsCredential->get() };
502     return true;
503 }
504
505 void ArgumentCoder<MachSendRight>::encode(Encoder& encoder, const MachSendRight& sendRight)
506 {
507     encoder << Attachment(sendRight.copySendRight().leakSendRight(), MACH_MSG_TYPE_MOVE_SEND);
508 }
509
510 void ArgumentCoder<MachSendRight>::encode(Encoder& encoder, MachSendRight&& sendRight)
511 {
512     encoder << Attachment(sendRight.leakSendRight(), MACH_MSG_TYPE_MOVE_SEND);
513 }
514
515 bool ArgumentCoder<MachSendRight>::decode(Decoder& decoder, MachSendRight& sendRight)
516 {
517     Attachment attachment;
518     if (!decoder.decode(attachment))
519         return false;
520
521     if (attachment.disposition() != MACH_MSG_TYPE_MOVE_SEND)
522         return false;
523
524     sendRight = MachSendRight::adopt(attachment.port());
525     return true;
526 }
527
528 void ArgumentCoder<WebCore::KeypressCommand>::encode(Encoder& encoder, const WebCore::KeypressCommand& keypressCommand)
529 {
530     encoder << keypressCommand.commandName << keypressCommand.text;
531 }
532     
533 Optional<WebCore::KeypressCommand> ArgumentCoder<WebCore::KeypressCommand>::decode(Decoder& decoder)
534 {
535     Optional<String> commandName;
536     decoder >> commandName;
537     if (!commandName)
538         return WTF::nullopt;
539     
540     Optional<String> text;
541     decoder >> text;
542     if (!text)
543         return WTF::nullopt;
544     
545     WebCore::KeypressCommand command;
546     command.commandName = WTFMove(*commandName);
547     command.text = WTFMove(*text);
548     return WTFMove(command);
549 }
550
551 #if ENABLE(CONTENT_FILTERING)
552
553 void ArgumentCoder<WebCore::ContentFilterUnblockHandler>::encode(Encoder& encoder, const WebCore::ContentFilterUnblockHandler& contentFilterUnblockHandler)
554 {
555     auto archiver = secureArchiver();
556     contentFilterUnblockHandler.encode(archiver.get());
557     IPC::encode(encoder, (__bridge CFDataRef)archiver.get().encodedData);
558 }
559
560 bool ArgumentCoder<WebCore::ContentFilterUnblockHandler>::decode(Decoder& decoder, WebCore::ContentFilterUnblockHandler& contentFilterUnblockHandler)
561 {
562     RetainPtr<CFDataRef> data;
563     if (!IPC::decode(decoder, data))
564         return false;
565
566     auto unarchiver = secureUnarchiverFromData((__bridge NSData *)data.get());
567     if (!WebCore::ContentFilterUnblockHandler::decode(unarchiver.get(), contentFilterUnblockHandler))
568         return false;
569
570     [unarchiver finishDecoding];
571     return true;
572 }
573
574 #endif
575
576 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
577
578 void ArgumentCoder<WebCore::MediaPlaybackTargetContext>::encodePlatformData(Encoder& encoder, const WebCore::MediaPlaybackTargetContext& target)
579 {
580     if ([PAL::getAVOutputContextClass() conformsToProtocol:@protocol(NSSecureCoding)])
581         encoder << target.avOutputContext();
582 }
583
584 bool ArgumentCoder<WebCore::MediaPlaybackTargetContext>::decodePlatformData(Decoder& decoder, WebCore::MediaPlaybackTargetContext& target)
585 {
586     if (![PAL::getAVOutputContextClass() conformsToProtocol:@protocol(NSSecureCoding)])
587         return false;
588
589     auto context = IPC::decode<AVOutputContext>(decoder, PAL::getAVOutputContextClass());
590     if (!context)
591         return false;
592
593     target = WebCore::MediaPlaybackTargetContext { context->get() };
594     return true;
595 }
596
597 #endif
598
599 } // namespace IPC