1/*
2 * Copyright (C) 2012 Igalia S.L.
3 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22#include "WebSoupRequestManagerProxy.h"
23
24#include "WebContext.h"
25#include "WebData.h"
26#include "WebSoupRequestManagerMessages.h"
27#include "WebSoupRequestManagerProxyMessages.h"
28
29namespace WebKit {
30
31const char* WebSoupRequestManagerProxy::supplementName()
32{
33    return "WebSoupRequestManagerProxy";
34}
35
36PassRefPtr<WebSoupRequestManagerProxy> WebSoupRequestManagerProxy::create(WebContext* context)
37{
38    return adoptRef(new WebSoupRequestManagerProxy(context));
39}
40
41WebSoupRequestManagerProxy::WebSoupRequestManagerProxy(WebContext* context)
42    : WebContextSupplement(context)
43    , m_loadFailed(false)
44{
45    WebContextSupplement::context()->addMessageReceiver(Messages::WebSoupRequestManagerProxy::messageReceiverName(), this);
46}
47
48WebSoupRequestManagerProxy::~WebSoupRequestManagerProxy()
49{
50}
51
52void WebSoupRequestManagerProxy::initializeClient(const WKSoupRequestManagerClient* client)
53{
54    m_client.initialize(client);
55}
56
57// WebContextSupplement
58
59void WebSoupRequestManagerProxy::contextDestroyed()
60{
61}
62
63void WebSoupRequestManagerProxy::processDidClose(WebProcessProxy*)
64{
65}
66
67void WebSoupRequestManagerProxy::refWebContextSupplement()
68{
69    APIObject::ref();
70}
71
72void WebSoupRequestManagerProxy::derefWebContextSupplement()
73{
74    APIObject::deref();
75}
76
77void WebSoupRequestManagerProxy::registerURIScheme(const String& scheme)
78{
79    if (!context())
80        return;
81
82    context()->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebSoupRequestManager::RegisterURIScheme(scheme));
83
84    ASSERT(!m_registeredURISchemes.contains(scheme));
85    m_registeredURISchemes.append(scheme);
86}
87
88void WebSoupRequestManagerProxy::didHandleURIRequest(const WebData* requestData, uint64_t contentLength, const String& mimeType, uint64_t requestID)
89{
90    if (!context())
91        return;
92
93    context()->sendToAllProcesses(Messages::WebSoupRequestManager::DidHandleURIRequest(requestData->dataReference(), contentLength, mimeType, requestID));
94}
95
96void WebSoupRequestManagerProxy::didReceiveURIRequestData(const WebData* requestData, uint64_t requestID)
97{
98    if (!context())
99        return;
100
101    if (m_loadFailed)
102        return;
103
104    context()->sendToAllProcesses(Messages::WebSoupRequestManager::DidReceiveURIRequestData(requestData->dataReference(), requestID));
105}
106
107void WebSoupRequestManagerProxy::didReceiveURIRequest(const String& uriString, WebPageProxy* initiaingPage, uint64_t requestID)
108{
109    if (!m_client.didReceiveURIRequest(this, WebURL::create(uriString).get(), initiaingPage, requestID))
110        didHandleURIRequest(WebData::create(0, 0).get(), 0, String(), requestID);
111}
112
113void WebSoupRequestManagerProxy::didFailToLoadURIRequest(uint64_t requestID)
114{
115    m_loadFailed = true;
116    m_client.didFailToLoadURIRequest(this, requestID);
117}
118
119void WebSoupRequestManagerProxy::didFailURIRequest(const WebCore::ResourceError& error, uint64_t requestID)
120{
121    if (!context())
122        return;
123
124    m_loadFailed = true;
125    context()->sendToAllProcesses(Messages::WebSoupRequestManager::DidFailURIRequest(error, requestID));
126}
127
128} // namespace WebKit
129