[WK2] Make the WebSoupRequestManager a supplement to the WebProcess
[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 "WebSoupRequestManager.h"
25 #include <wtf/text/CString.h>
26
27 using namespace WebKit;
28
29 G_DEFINE_TYPE(WebKitSoupRequestGeneric, webkit_soup_request_generic, SOUP_TYPE_REQUEST)
30
31 struct _WebKitSoupRequestGenericPrivate {
32     CString mimeType;
33     goffset contentLength;
34 };
35
36 static void webkitSoupRequestGenericFinalize(GObject* object)
37 {
38     WEBKIT_SOUP_REQUEST_GENERIC(object)->priv->~WebKitSoupRequestGenericPrivate();
39     G_OBJECT_CLASS(webkit_soup_request_generic_parent_class)->finalize(object);
40 }
41
42 static void webkit_soup_request_generic_init(WebKitSoupRequestGeneric* request)
43 {
44     WebKitSoupRequestGenericPrivate* priv = G_TYPE_INSTANCE_GET_PRIVATE(request, WEBKIT_TYPE_SOUP_REQUEST_GENERIC, WebKitSoupRequestGenericPrivate);
45     request->priv = priv;
46     new (priv) WebKitSoupRequestGenericPrivate();
47 }
48
49 static void webkitSoupRequestGenericSendAsync(SoupRequest* request, GCancellable* cancellable, GAsyncReadyCallback callback, gpointer userData)
50 {
51     GSimpleAsyncResult* result = g_simple_async_result_new(G_OBJECT(request), callback, userData, reinterpret_cast<void*>(webkitSoupRequestGenericSendAsync));
52     WebProcess::shared().supplement<WebSoupRequestManager>()->send(result, cancellable);
53 }
54
55 static GInputStream* webkitSoupRequestGenericSendFinish(SoupRequest*, GAsyncResult* result, GError** error)
56 {
57     GSimpleAsyncResult* simpleResult = G_SIMPLE_ASYNC_RESULT(result);
58     g_warn_if_fail(g_simple_async_result_get_source_tag(simpleResult) == webkitSoupRequestGenericSendAsync);
59
60     if (g_simple_async_result_propagate_error(simpleResult, error))
61         return 0;
62
63     return WebProcess::shared().supplement<WebSoupRequestManager>()->finish(simpleResult);
64 }
65
66 static goffset webkitSoupRequestGenericGetContentLength(SoupRequest* request)
67 {
68     return WEBKIT_SOUP_REQUEST_GENERIC(request)->priv->contentLength;
69 }
70
71 static const char* webkitSoupRequestGenericGetContentType(SoupRequest* request)
72 {
73     return WEBKIT_SOUP_REQUEST_GENERIC(request)->priv->mimeType.data();
74 }
75
76 static void webkit_soup_request_generic_class_init(WebKitSoupRequestGenericClass* requestGenericClass)
77 {
78     GObjectClass* gObjectClass = G_OBJECT_CLASS(requestGenericClass);
79     gObjectClass->finalize = webkitSoupRequestGenericFinalize;
80
81     SoupRequestClass* requestClass = SOUP_REQUEST_CLASS(requestGenericClass);
82     requestClass->send_async = webkitSoupRequestGenericSendAsync;
83     requestClass->send_finish = webkitSoupRequestGenericSendFinish;
84     requestClass->get_content_length = webkitSoupRequestGenericGetContentLength;
85     requestClass->get_content_type = webkitSoupRequestGenericGetContentType;
86
87     g_type_class_add_private(requestGenericClass, sizeof(WebKitSoupRequestGenericPrivate));
88 }
89
90 void webkitSoupRequestGenericSetContentLength(WebKitSoupRequestGeneric* request, goffset contentLength)
91 {
92     request->priv->contentLength = contentLength;
93 }
94
95 void webkitSoupRequestGenericSetContentType(WebKitSoupRequestGeneric* request, const char* mimeType)
96 {
97     request->priv->mimeType = mimeType;
98 }