[Qt][WK2] Fix build after removal of MessageID.h
[WebKit-https.git] / Source / WebKit2 / WebProcess / soup / WebKitSoupRequestInputStream.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 "WebKitSoupRequestInputStream.h"
22
23 #include <wtf/OwnPtr.h>
24 #include <wtf/PassOwnPtr.h>
25 #include <wtf/Threading.h>
26 #include <wtf/gobject/GRefPtr.h>
27
28 struct AsyncReadData {
29     AsyncReadData(GSimpleAsyncResult* result, void* buffer, gsize count, GCancellable* cancellable)
30         : result(result)
31         , buffer(buffer)
32         , count(count)
33         , cancellable(cancellable)
34     {
35     }
36
37     GRefPtr<GSimpleAsyncResult> result;
38     void* buffer;
39     size_t count;
40     GRefPtr<GCancellable> cancellable;
41 };
42
43 struct _WebKitSoupRequestInputStreamPrivate {
44     uint64_t contentLength;
45     uint64_t bytesReceived;
46     uint64_t bytesRead;
47
48     Mutex readLock;
49     OwnPtr<AsyncReadData> pendingAsyncRead;
50 };
51
52 G_DEFINE_TYPE(WebKitSoupRequestInputStream, webkit_soup_request_input_stream, G_TYPE_MEMORY_INPUT_STREAM)
53
54 static void webkitSoupRequestInputStreamReadAsyncResultComplete(WebKitSoupRequestInputStream* stream, GSimpleAsyncResult* result, void* buffer, gsize count, GCancellable* cancellable)
55 {
56     GError* error = 0;
57     gssize bytesRead = G_INPUT_STREAM_GET_CLASS(stream)->read_fn(G_INPUT_STREAM(stream), buffer, count, cancellable, &error);
58     if (!error) {
59         g_simple_async_result_set_op_res_gssize(result, bytesRead);
60         stream->priv->bytesRead += bytesRead;
61     } else
62         g_simple_async_result_take_error(result, error);
63     g_simple_async_result_complete_in_idle(result);
64 }
65
66 static void webkitSoupRequestInputStreamPendingReadAsyncComplete(WebKitSoupRequestInputStream* stream)
67 {
68     if (!stream->priv->pendingAsyncRead)
69         return;
70
71     AsyncReadData* data = stream->priv->pendingAsyncRead.get();
72     webkitSoupRequestInputStreamReadAsyncResultComplete(stream, data->result.get(), data->buffer, data->count, data->cancellable.get());
73     stream->priv->pendingAsyncRead.clear();
74 }
75
76 static bool webkitSoupRequestInputStreamHasDataToRead(WebKitSoupRequestInputStream* stream)
77 {
78     return stream->priv->bytesRead < stream->priv->bytesReceived;
79 }
80
81 static bool webkitSoupRequestInputStreamIsWaitingForData(WebKitSoupRequestInputStream* stream)
82 {
83     return !stream->priv->contentLength || stream->priv->bytesReceived < stream->priv->contentLength;
84 }
85
86 static void webkitSoupRequestInputStreamReadAsync(GInputStream* inputStream, void* buffer, gsize count, int /*priority*/, GCancellable* cancellable, GAsyncReadyCallback callback, gpointer userData)
87 {
88     WebKitSoupRequestInputStream* stream = WEBKIT_SOUP_REQUEST_INPUT_STREAM(inputStream);
89     GRefPtr<GSimpleAsyncResult> result = adoptGRef(g_simple_async_result_new(G_OBJECT(stream), callback, userData, reinterpret_cast<void*>(webkitSoupRequestInputStreamReadAsync)));
90
91     MutexLocker locker(stream->priv->readLock);
92
93     if (!webkitSoupRequestInputStreamHasDataToRead(stream) && !webkitSoupRequestInputStreamIsWaitingForData(stream)) {
94         g_simple_async_result_set_op_res_gssize(result.get(), 0);
95         g_simple_async_result_complete_in_idle(result.get());
96         return;
97     }
98
99     if (webkitSoupRequestInputStreamHasDataToRead(stream)) {
100         webkitSoupRequestInputStreamReadAsyncResultComplete(stream, result.get(), buffer, count, cancellable);
101         return;
102     }
103
104     stream->priv->pendingAsyncRead = adoptPtr(new AsyncReadData(result.get(), buffer, count, cancellable));
105 }
106
107 static gssize webkitSoupRequestInputStreamReadFinish(GInputStream*, GAsyncResult* result, GError**)
108 {
109     GSimpleAsyncResult* simpleResult = G_SIMPLE_ASYNC_RESULT(result);
110     g_warn_if_fail(g_simple_async_result_get_source_tag(simpleResult) == webkitSoupRequestInputStreamReadAsync);
111
112     return g_simple_async_result_get_op_res_gssize(simpleResult);
113 }
114
115 static void webkitSoupRequestInputStreamFinalize(GObject* object)
116 {
117     WEBKIT_SOUP_REQUEST_INPUT_STREAM(object)->priv->~WebKitSoupRequestInputStreamPrivate();
118     G_OBJECT_CLASS(webkit_soup_request_input_stream_parent_class)->finalize(object);
119 }
120
121 static void webkit_soup_request_input_stream_init(WebKitSoupRequestInputStream* stream)
122 {
123     WebKitSoupRequestInputStreamPrivate* priv = G_TYPE_INSTANCE_GET_PRIVATE(stream, WEBKIT_TYPE_SOUP_REQUEST_INPUT_STREAM, WebKitSoupRequestInputStreamPrivate);
124     stream->priv = priv;
125     new (priv) WebKitSoupRequestInputStreamPrivate();
126 }
127
128 static void webkit_soup_request_input_stream_class_init(WebKitSoupRequestInputStreamClass* requestStreamClass)
129 {
130     GObjectClass* gObjectClass = G_OBJECT_CLASS(requestStreamClass);
131     gObjectClass->finalize = webkitSoupRequestInputStreamFinalize;
132
133     GInputStreamClass* inputStreamClass = G_INPUT_STREAM_CLASS(requestStreamClass);
134     inputStreamClass->read_async = webkitSoupRequestInputStreamReadAsync;
135     inputStreamClass->read_finish = webkitSoupRequestInputStreamReadFinish;
136
137     g_type_class_add_private(requestStreamClass, sizeof(WebKitSoupRequestInputStreamPrivate));
138 }
139
140 GInputStream* webkitSoupRequestInputStreamNew(uint64_t contentLength)
141 {
142     WebKitSoupRequestInputStream* stream = WEBKIT_SOUP_REQUEST_INPUT_STREAM(g_object_new(WEBKIT_TYPE_SOUP_REQUEST_INPUT_STREAM, NULL));
143     stream->priv->contentLength = contentLength;
144     return G_INPUT_STREAM(stream);
145 }
146
147 void webkitSoupRequestInputStreamAddData(WebKitSoupRequestInputStream* stream, const void* data, size_t dataLength)
148 {
149     if (webkitSoupRequestInputStreamFinished(stream))
150         return;
151
152     MutexLocker locker(stream->priv->readLock);
153
154     if (dataLength) {
155         // Truncate the dataLength to the contentLength if it's known.
156         if (stream->priv->contentLength && stream->priv->bytesReceived + dataLength > stream->priv->contentLength)
157             dataLength = stream->priv->contentLength - stream->priv->bytesReceived;
158         stream->priv->bytesReceived += dataLength;
159         g_memory_input_stream_add_data(G_MEMORY_INPUT_STREAM(stream), g_memdup(data, dataLength), dataLength, g_free);
160     } else {
161         // We have received all the data, set contentLength to bytesReceived to indicate we have finished.
162         stream->priv->contentLength = stream->priv->bytesReceived;
163         // If there's a pending read to complete, read_fn will return 0 because we haven't added more data to the
164         // memory input stream. And if there isn't a pending read, the next call to read_async will return 0 too, because
165         // webkitSoupRequestInputStreamFinished() is now TRUE.
166     }
167
168     webkitSoupRequestInputStreamPendingReadAsyncComplete(stream);
169 }
170
171 bool webkitSoupRequestInputStreamFinished(WebKitSoupRequestInputStream* stream)
172 {
173     return !webkitSoupRequestInputStreamIsWaitingForData(stream);
174 }