8b251b831d1847b448786292449feddfe0d08775
[WebKit-https.git] / Source / WebKit2 / WebProcess / soup / WebKitSoupRequestGeneric.cpp
1 /*
2  * Copyright (C) 2012 Igalia S.L.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21 #include "WebKitSoupRequestGeneric.h"
22
23 #include "WebProcess.h"
24 #include <wtf/text/CString.h>
25
26 using namespace WebKit;
27
28 G_DEFINE_TYPE(WebKitSoupRequestGeneric, webkit_soup_request_generic, SOUP_TYPE_REQUEST)
29
30 struct _WebKitSoupRequestGenericPrivate {
31     CString mimeType;
32     goffset contentLength;
33 };
34
35 static void webkitSoupRequestGenericFinalize(GObject* object)
36 {
37     WEBKIT_SOUP_REQUEST_GENERIC(object)->priv->~WebKitSoupRequestGenericPrivate();
38     G_OBJECT_CLASS(webkit_soup_request_generic_parent_class)->finalize(object);
39 }
40
41 static void webkit_soup_request_generic_init(WebKitSoupRequestGeneric* request)
42 {
43     WebKitSoupRequestGenericPrivate* priv = G_TYPE_INSTANCE_GET_PRIVATE(request, WEBKIT_TYPE_SOUP_REQUEST_GENERIC, WebKitSoupRequestGenericPrivate);
44     request->priv = priv;
45     new (priv) WebKitSoupRequestGenericPrivate();
46 }
47
48 static void webkitSoupRequestGenericSendAsync(SoupRequest* request, GCancellable* cancellable, GAsyncReadyCallback callback, gpointer userData)
49 {
50     GSimpleAsyncResult* result = g_simple_async_result_new(G_OBJECT(request), callback, userData, reinterpret_cast<void*>(webkitSoupRequestGenericSendAsync));
51     WebProcess::shared().soupRequestManager().send(result, cancellable);
52 }
53
54 static GInputStream* webkitSoupRequestGenericSendFinish(SoupRequest*, GAsyncResult* result, GError** error)
55 {
56     GSimpleAsyncResult* simpleResult = G_SIMPLE_ASYNC_RESULT(result);
57     g_warn_if_fail(g_simple_async_result_get_source_tag(simpleResult) == webkitSoupRequestGenericSendAsync);
58
59     if (g_simple_async_result_propagate_error(simpleResult, error))
60         return 0;
61
62     return WebProcess::shared().soupRequestManager().finish(simpleResult);
63 }
64
65 static goffset webkitSoupRequestGenericGetContentLength(SoupRequest* request)
66 {
67     return WEBKIT_SOUP_REQUEST_GENERIC(request)->priv->contentLength;
68 }
69
70 static const char* webkitSoupRequestGenericGetContentType(SoupRequest* request)
71 {
72     return WEBKIT_SOUP_REQUEST_GENERIC(request)->priv->mimeType.data();
73 }
74
75 static void webkit_soup_request_generic_class_init(WebKitSoupRequestGenericClass* requestGenericClass)
76 {
77     GObjectClass* gObjectClass = G_OBJECT_CLASS(requestGenericClass);
78     gObjectClass->finalize = webkitSoupRequestGenericFinalize;
79
80     SoupRequestClass* requestClass = SOUP_REQUEST_CLASS(requestGenericClass);
81     requestClass->send_async = webkitSoupRequestGenericSendAsync;
82     requestClass->send_finish = webkitSoupRequestGenericSendFinish;
83     requestClass->get_content_length = webkitSoupRequestGenericGetContentLength;
84     requestClass->get_content_type = webkitSoupRequestGenericGetContentType;
85
86     g_type_class_add_private(requestGenericClass, sizeof(WebKitSoupRequestGenericPrivate));
87 }
88
89 void webkitSoupRequestGenericSetContentLength(WebKitSoupRequestGeneric* request, goffset contentLength)
90 {
91     request->priv->contentLength = contentLength;
92 }
93
94 void webkitSoupRequestGenericSetContentType(WebKitSoupRequestGeneric* request, const char* mimeType)
95 {
96     request->priv->mimeType = mimeType;
97 }