Implement encoding/decoding of ResourceRequest and
[WebKit-https.git] / WebKit2 / Shared / win / WebCoreArgumentCodersWin.cpp
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 "WebCoreArgumentCoders.h"
27
28 #if USE(CFNETWORK)
29 #include "ArgumentCodersCF.h"
30 #include <WebKitSystemInterface/WebKitSystemInterface.h>
31 #endif
32
33 namespace CoreIPC {
34
35 // FIXME: These coders should really go in a WebCoreArgumentCodersCFNetwork file.
36
37 void encodeResourceRequest(ArgumentEncoder* encoder, const WebCore::ResourceRequest& resourceRequest)
38 {
39 #if USE(CFNETWORK)
40     RetainPtr<CFDictionaryRef> dictionary(AdoptCF, wkCFURLRequestCreateSerializableRepresentation(resourceRequest.cfURLRequest(), CoreIPC::tokenNullTypeRef()));
41     encode(encoder, dictionary.get());
42 #endif
43 }
44
45 bool decodeResourceRequest(ArgumentDecoder* decoder, WebCore::ResourceRequest& resourceRequest)
46 {
47 #if USE(CFNETWORK)
48     RetainPtr<CFDictionaryRef> dictionary;
49     if (!decode(decoder, dictionary))
50         return false;
51
52     CFURLRequestRef cfURLRequest = wkCFURLRequestCreateFromSerializableRepresentation(dictionary.get(), CoreIPC::tokenNullTypeRef());
53     if (!cfURLRequest)
54         return false;
55
56     resourceRequest = WebCore::ResourceRequest(cfURLRequest);
57     return true;
58 #else
59     return false;
60 #endif
61 }
62
63 void encodeResourceResponse(ArgumentEncoder* encoder, const WebCore::ResourceResponse& resourceResponse)
64 {
65 #if USE(CFNETWORK)
66     bool responseIsPresent = resourceResponse.cfURLResponse();
67     encoder->encode(responseIsPresent);
68
69     if (!responseIsPresent)
70         return;
71
72     RetainPtr<CFDictionaryRef> dictionary(AdoptCF, wkCFURLResponseCreateSerializableRepresentation(resourceResponse.cfURLResponse(), CoreIPC::tokenNullTypeRef()));
73     encode(encoder, dictionary.get());
74 #endif
75 }
76
77 bool decodeResourceResponse(ArgumentDecoder* decoder, WebCore::ResourceResponse& resourceResponse)
78 {
79 #if USE(CFNETWORK)
80     bool responseIsPresent;
81     decoder->decode(responseIsPresent);
82
83     if (!responseIsPresent) {
84         resourceResponse = WebCore::ResourceResponse();
85         return true;
86     }
87
88     RetainPtr<CFDictionaryRef> dictionary;
89     if (!decode(decoder, dictionary))
90         return false;
91
92     CFURLResponseRef cfURLResponse = wkCFURLResponseCreateFromSerializableRepresentation(dictionary.get(), CoreIPC::tokenNullTypeRef());
93     if (!cfURLResponse)
94         return false;
95
96     resourceResponse = WebCore::ResourceResponse(cfURLResponse);
97     return true;
98 #else
99     return false;
100 #endif
101 }
102
103 } // namespace CoreIPC