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