1/*
2 * Copyright (C) 2010 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 "WebCoreArgumentCoders.h"
28
29#if USE(CFNETWORK)
30#include "ArgumentCodersCF.h"
31#include "PlatformCertificateInfo.h"
32#include <WebCore/CertificateCFWin.h>
33#include <WebKitSystemInterface/WebKitSystemInterface.h>
34#endif
35
36namespace CoreIPC {
37
38// FIXME: These coders should really go in a WebCoreArgumentCodersCFNetwork file.
39
40void encodeResourceRequest(ArgumentEncoder* encoder, const WebCore::ResourceRequest& resourceRequest)
41{
42#if USE(CFNETWORK)
43    bool requestIsPresent = resourceRequest.cfURLRequest();
44    encoder->encode(requestIsPresent);
45
46    if (!requestIsPresent)
47        return;
48
49    RetainPtr<CFDictionaryRef> dictionary(AdoptCF, wkCFURLRequestCreateSerializableRepresentation(resourceRequest.cfURLRequest(), CoreIPC::tokenNullTypeRef()));
50    encode(encoder, dictionary.get());
51#endif
52}
53
54bool decodeResourceRequest(ArgumentDecoder* decoder, WebCore::ResourceRequest& resourceRequest)
55{
56#if USE(CFNETWORK)
57    bool requestIsPresent;
58    if (!decoder->decode(requestIsPresent))
59        return false;
60
61    if (!requestIsPresent) {
62        resourceRequest = WebCore::ResourceRequest();
63        return true;
64    }
65
66    RetainPtr<CFDictionaryRef> dictionary;
67    if (!decode(decoder, dictionary))
68        return false;
69
70    CFURLRequestRef cfURLRequest = wkCFURLRequestCreateFromSerializableRepresentation(dictionary.get(), CoreIPC::tokenNullTypeRef());
71    if (!cfURLRequest)
72        return false;
73
74    resourceRequest = WebCore::ResourceRequest(cfURLRequest);
75    return true;
76#else
77    return false;
78#endif
79}
80
81void encodeResourceResponse(ArgumentEncoder* encoder, const WebCore::ResourceResponse& resourceResponse)
82{
83#if USE(CFNETWORK)
84    bool responseIsPresent = resourceResponse.cfURLResponse();
85    encoder->encode(responseIsPresent);
86
87    if (!responseIsPresent)
88        return;
89
90    RetainPtr<CFDictionaryRef> dictionary(AdoptCF, wkCFURLResponseCreateSerializableRepresentation(resourceResponse.cfURLResponse(), CoreIPC::tokenNullTypeRef()));
91    encode(encoder, dictionary.get());
92#endif
93}
94
95bool decodeResourceResponse(ArgumentDecoder* decoder, WebCore::ResourceResponse& resourceResponse)
96{
97#if USE(CFNETWORK)
98    bool responseIsPresent;
99    if (!decoder->decode(responseIsPresent))
100        return false;
101
102    if (!responseIsPresent) {
103        resourceResponse = WebCore::ResourceResponse();
104        return true;
105    }
106
107    RetainPtr<CFDictionaryRef> dictionary;
108    if (!decode(decoder, dictionary))
109        return false;
110
111    CFURLResponseRef cfURLResponse = wkCFURLResponseCreateFromSerializableRepresentation(dictionary.get(), CoreIPC::tokenNullTypeRef());
112    if (!cfURLResponse)
113        return false;
114
115    resourceResponse = WebCore::ResourceResponse(cfURLResponse);
116    return true;
117#else
118    return false;
119#endif
120}
121
122void encodeResourceError(ArgumentEncoder* encoder, const WebCore::ResourceError& resourceError)
123{
124    encoder->encode(CoreIPC::In(resourceError.domain(), resourceError.errorCode(), resourceError.failingURL(), resourceError.localizedDescription()));
125
126#if USE(CFNETWORK)
127    encoder->encode(WebKit::PlatformCertificateInfo(resourceError.certificate()));
128#endif
129}
130
131bool decodeResourceError(ArgumentDecoder* decoder, WebCore::ResourceError& resourceError)
132{
133    String domain;
134    int errorCode;
135    String failingURL;
136    String localizedDescription;
137    if (!decoder->decode(CoreIPC::Out(domain, errorCode, failingURL, localizedDescription)))
138        return false;
139
140#if USE(CFNETWORK)
141    WebKit::PlatformCertificateInfo certificate;
142    if (!decoder->decode(certificate))
143        return false;
144
145    const Vector<PCCERT_CONTEXT> certificateChain = certificate.certificateChain();
146    if (!certificateChain.isEmpty()) {
147        ASSERT(certificateChain.size() == 1);
148        resourceError = WebCore::ResourceError(domain, errorCode, failingURL, localizedDescription, WebCore::copyCertificateToData(certificateChain.first()).get());
149        return true;
150    }
151#endif
152
153    resourceError = WebCore::ResourceError(domain, errorCode, failingURL, localizedDescription);
154    return true;
155}
156
157} // namespace CoreIPC
158