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