ad73ad494a2c371ce87e6f1ceec1cf8c0c34aac4
[WebKit-https.git] / Source / WebKit2 / UIProcess / Network / CustomProtocols / soup / WebSoupCustomProtocolRequestManager.cpp
1 /*
2  * Copyright (C) 2013 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 "WebSoupCustomProtocolRequestManager.h"
22
23 #if ENABLE(CUSTOM_PROTOCOLS)
24
25 #include "APIData.h"
26 #include "CustomProtocolManagerMessages.h"
27 #include "WebContext.h"
28 #include <WebCore/ResourceError.h>
29 #include <WebCore/ResourceRequest.h>
30 #include <WebCore/ResourceResponse.h>
31
32 #if PLATFORM(GTK)
33 #include <WebCore/ErrorsGtk.h>
34 #endif
35
36 namespace WebKit {
37
38 const char* WebSoupCustomProtocolRequestManager::supplementName()
39 {
40     return "WebSoupCustomProtocolRequestManager";
41 }
42
43 PassRefPtr<WebSoupCustomProtocolRequestManager> WebSoupCustomProtocolRequestManager::create(WebContext* context)
44 {
45     return adoptRef(new WebSoupCustomProtocolRequestManager(context));
46 }
47
48 WebSoupCustomProtocolRequestManager::WebSoupCustomProtocolRequestManager(WebContext* context)
49     : WebContextSupplement(context)
50 {
51 }
52
53 WebSoupCustomProtocolRequestManager::~WebSoupCustomProtocolRequestManager()
54 {
55 }
56
57 void WebSoupCustomProtocolRequestManager::initializeClient(const WKSoupCustomProtocolRequestManagerClientBase* client)
58 {
59     m_client.initialize(client);
60 }
61
62 // WebContextSupplement
63 void WebSoupCustomProtocolRequestManager::contextDestroyed()
64 {
65 }
66
67 void WebSoupCustomProtocolRequestManager::processDidClose(WebProcessProxy*)
68 {
69 }
70
71 void WebSoupCustomProtocolRequestManager::refWebContextSupplement()
72 {
73     API::Object::ref();
74 }
75
76 void WebSoupCustomProtocolRequestManager::derefWebContextSupplement()
77 {
78     API::Object::deref();
79 }
80
81 void WebSoupCustomProtocolRequestManager::registerSchemeForCustomProtocol(const String& scheme)
82 {
83     if (!context())
84         return;
85
86     context()->registerSchemeForCustomProtocol(scheme);
87
88     ASSERT(!m_registeredSchemes.contains(scheme));
89     m_registeredSchemes.append(scheme);
90 }
91
92 void WebSoupCustomProtocolRequestManager::unregisterSchemeForCustomProtocol(const String& scheme)
93 {
94     if (!context())
95         return;
96
97     context()->unregisterSchemeForCustomProtocol(scheme);
98
99     ASSERT(m_registeredSchemes.contains(scheme));
100     m_registeredSchemes.remove(m_registeredSchemes.find(scheme));
101 }
102
103 void WebSoupCustomProtocolRequestManager::startLoading(uint64_t customProtocolID, const WebCore::ResourceRequest& request)
104 {
105     if (!m_client.startLoading(this, customProtocolID, request))
106         didFailWithError(customProtocolID, WebCore::cannotShowURLError(request));
107 }
108
109 void WebSoupCustomProtocolRequestManager::stopLoading(uint64_t customProtocolID)
110 {
111     m_client.stopLoading(this, customProtocolID);
112 }
113
114 void WebSoupCustomProtocolRequestManager::didReceiveResponse(uint64_t customProtocolID, const WebCore::ResourceResponse& response)
115 {
116     if (!context())
117         return;
118
119     context()->networkingProcessConnection()->send(Messages::CustomProtocolManager::DidReceiveResponse(customProtocolID, response, 0), 0);
120 }
121
122 void WebSoupCustomProtocolRequestManager::didLoadData(uint64_t customProtocolID, const API::Data* data)
123 {
124     if (!context())
125         return;
126
127     context()->networkingProcessConnection()->send(Messages::CustomProtocolManager::DidLoadData(customProtocolID, data->dataReference()), 0);
128 }
129
130 void WebSoupCustomProtocolRequestManager::didFailWithError(uint64_t customProtocolID, const WebCore::ResourceError& error)
131 {
132     if (!context())
133         return;
134
135     context()->networkingProcessConnection()->send(Messages::CustomProtocolManager::DidFailWithError(customProtocolID, error), 0);
136 }
137
138 void WebSoupCustomProtocolRequestManager::didFinishLoading(uint64_t customProtocolID)
139 {
140     if (!context())
141         return;
142
143     context()->networkingProcessConnection()->send(Messages::CustomProtocolManager::DidFinishLoading(customProtocolID), 0);
144 }
145
146 } // namespace WebKit
147
148 #endif // ENABLE(CUSTOM_PROTOCOLS)