[Qt][WK2] Fix build after removal of MessageID.h
[WebKit-https.git] / Source / WebKit2 / WebProcess / soup / WebSoupRequestManager.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 "WebSoupRequestManager.h"
22
23 #include "DataReference.h"
24 #include "WebErrors.h"
25 #include "WebKitSoupRequestGeneric.h"
26 #include "WebKitSoupRequestInputStream.h"
27 #include "WebPageProxyMessages.h"
28 #include "WebProcess.h"
29 #include "WebSoupRequestManagerMessages.h"
30 #include "WebSoupRequestManagerProxyMessages.h"
31 #include <WebCore/ResourceHandle.h>
32 #include <WebCore/ResourceRequest.h>
33 #include <libsoup/soup-requester.h>
34 #include <wtf/gobject/GOwnPtr.h>
35 #include <wtf/text/CString.h>
36
37 namespace WebKit {
38
39 static uint64_t generateSoupRequestID()
40 {
41     static uint64_t uniqueSoupRequestID = 1;
42     return uniqueSoupRequestID++;
43 }
44
45 struct WebSoupRequestAsyncData {
46     WebSoupRequestAsyncData(GSimpleAsyncResult* result, WebKitSoupRequestGeneric* requestGeneric, GCancellable* cancellable)
47         : result(result)
48         , request(requestGeneric)
49         , cancellable(cancellable)
50     {
51         // If the struct contains a null request, it is because the request failed.
52         g_object_add_weak_pointer(G_OBJECT(request), reinterpret_cast<void**>(&request));
53     }
54
55     ~WebSoupRequestAsyncData()
56     {
57         if (request)
58             g_object_remove_weak_pointer(G_OBJECT(request), reinterpret_cast<void**>(&request));
59     }
60
61     bool requestFailed()
62     {
63         return g_cancellable_is_cancelled(cancellable.get()) || !request;
64     }
65
66     GRefPtr<GSimpleAsyncResult> releaseResult()
67     {
68         GSimpleAsyncResult* returnValue = result;
69         result = 0;
70         return adoptGRef(returnValue);
71     }
72
73     GSimpleAsyncResult* result;
74     WebKitSoupRequestGeneric* request;
75     GRefPtr<GCancellable> cancellable;
76     GRefPtr<GInputStream> stream;
77 };
78
79 WebSoupRequestManager::WebSoupRequestManager(WebProcess* process)
80     : m_process(process)
81     , m_schemes(adoptGRef(g_ptr_array_new_with_free_func(g_free)))
82 {
83     m_process->addMessageReceiver(Messages::WebSoupRequestManager::messageReceiverName(), this);
84 }
85
86 WebSoupRequestManager::~WebSoupRequestManager()
87 {
88 }
89
90 void WebSoupRequestManager::registerURIScheme(const String& scheme)
91 {
92     if (m_schemes->len)
93         g_ptr_array_remove_index_fast(m_schemes.get(), m_schemes->len - 1);
94     g_ptr_array_add(m_schemes.get(), g_strdup(scheme.utf8().data()));
95     g_ptr_array_add(m_schemes.get(), 0);
96
97     SoupSession* session = WebCore::ResourceHandle::defaultSession();
98     GRefPtr<SoupRequester> requester = SOUP_REQUESTER(soup_session_get_feature(session, SOUP_TYPE_REQUESTER));
99     if (requester)
100         soup_session_feature_remove_feature(SOUP_SESSION_FEATURE(requester.get()), WEBKIT_TYPE_SOUP_REQUEST_GENERIC);
101     else {
102         requester = adoptGRef(soup_requester_new());
103         soup_session_add_feature(session, SOUP_SESSION_FEATURE(requester.get()));
104     }
105     SoupRequestClass* genericRequestClass = static_cast<SoupRequestClass*>(g_type_class_ref(WEBKIT_TYPE_SOUP_REQUEST_GENERIC));
106     genericRequestClass->schemes = const_cast<const char**>(reinterpret_cast<char**>(m_schemes->pdata));
107     soup_session_feature_add_feature(SOUP_SESSION_FEATURE(requester.get()), WEBKIT_TYPE_SOUP_REQUEST_GENERIC);
108 }
109
110 void WebSoupRequestManager::didHandleURIRequest(const CoreIPC::DataReference& requestData, uint64_t contentLength, const String& mimeType, uint64_t requestID)
111 {
112     WebSoupRequestAsyncData* data = m_requestMap.get(requestID);
113     ASSERT(data);
114     GRefPtr<GSimpleAsyncResult> result = data->releaseResult();
115     ASSERT(result.get());
116
117     GRefPtr<WebKitSoupRequestGeneric> request = adoptGRef(WEBKIT_SOUP_REQUEST_GENERIC(g_async_result_get_source_object(G_ASYNC_RESULT(result.get()))));
118     webkitSoupRequestGenericSetContentLength(request.get(), contentLength ? contentLength : -1);
119     webkitSoupRequestGenericSetContentType(request.get(), !mimeType.isEmpty() ? mimeType.utf8().data() : 0);
120
121     GInputStream* dataStream;
122     if (!requestData.size()) {
123         // Empty reply, just create and empty GMemoryInputStream.
124         dataStream = g_memory_input_stream_new();
125         m_requestMap.remove(requestID);
126     } else if (requestData.size() == contentLength) {
127         // We don't expect more data, so we can just create a GMemoryInputStream with all the data.
128         dataStream = g_memory_input_stream_new_from_data(g_memdup(requestData.data(), requestData.size()), contentLength, g_free);
129         m_requestMap.remove(requestID);
130     } else {
131         // We expect more data chunks from the UI process.
132         dataStream = webkitSoupRequestInputStreamNew(contentLength);
133         data->stream = dataStream;
134         webkitSoupRequestInputStreamAddData(WEBKIT_SOUP_REQUEST_INPUT_STREAM(dataStream), requestData.data(), requestData.size());
135     }
136     g_simple_async_result_set_op_res_gpointer(result.get(), dataStream, g_object_unref);
137     g_simple_async_result_complete(result.get());
138 }
139
140 void WebSoupRequestManager::didReceiveURIRequestData(const CoreIPC::DataReference& requestData, uint64_t requestID)
141 {
142     WebSoupRequestAsyncData* data = m_requestMap.get(requestID);
143     // The data might have been removed from the request map if a previous chunk failed
144     // and a new message was sent by the UI process before being notified about the failure.
145     if (!data)
146         return;
147     ASSERT(data->stream.get());
148
149     if (data->requestFailed()) {
150         // ResourceRequest failed or it was cancelled. It doesn't matter here the error or if it was cancelled,
151         // because that's already handled by the resource handle client, we just want to notify the UI process
152         // to stop reading data from the user input stream. If UI process already sent all the data we simply
153         // finish silently.
154         if (!webkitSoupRequestInputStreamFinished(WEBKIT_SOUP_REQUEST_INPUT_STREAM(data->stream.get())))
155             m_process->connection()->send(Messages::WebSoupRequestManagerProxy::DidFailToLoadURIRequest(requestID), 0);
156         m_requestMap.remove(requestID);
157
158         return;
159     }
160
161     webkitSoupRequestInputStreamAddData(WEBKIT_SOUP_REQUEST_INPUT_STREAM(data->stream.get()), requestData.data(), requestData.size());
162     if (webkitSoupRequestInputStreamFinished(WEBKIT_SOUP_REQUEST_INPUT_STREAM(data->stream.get())))
163         m_requestMap.remove(requestID);
164 }
165
166 void WebSoupRequestManager::send(GSimpleAsyncResult* result, GCancellable* cancellable)
167 {
168     GRefPtr<WebKitSoupRequestGeneric> request = adoptGRef(WEBKIT_SOUP_REQUEST_GENERIC(g_async_result_get_source_object(G_ASYNC_RESULT(result))));
169     SoupRequest* soupRequest = SOUP_REQUEST(request.get());
170     GOwnPtr<char> uriString(soup_uri_to_string(soup_request_get_uri(soupRequest), FALSE));
171
172     uint64_t requestID = generateSoupRequestID();
173     m_requestMap.set(requestID, adoptPtr(new WebSoupRequestAsyncData(result, request.get(), cancellable)));
174
175     uint64_t initiatingPageID = WebCore::ResourceHandle::getSoupRequestInitiatingPageID(soupRequest);
176     m_process->connection()->send(Messages::WebPageProxy::DidReceiveURIRequest(String::fromUTF8(uriString.get()), requestID), initiatingPageID);
177 }
178
179 GInputStream* WebSoupRequestManager::finish(GSimpleAsyncResult* result)
180 {
181     return G_INPUT_STREAM(g_object_ref(g_simple_async_result_get_op_res_gpointer(result)));
182 }
183
184 } // namespace WebKit