1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebURLSchemeHandlerProxy.h"
28
29#include "DataReference.h"
30#include "URLSchemeTaskParameters.h"
31#include "WebCoreArgumentCoders.h"
32#include "WebErrors.h"
33#include "WebLoaderStrategy.h"
34#include "WebPage.h"
35#include "WebPageProxyMessages.h"
36#include "WebProcess.h"
37#include <WebCore/ResourceError.h>
38#include <WebCore/ResourceLoader.h>
39#include <WebCore/ResourceRequest.h>
40#include <WebCore/ResourceResponse.h>
41
42namespace WebKit {
43using namespace WebCore;
44
45WebURLSchemeHandlerProxy::WebURLSchemeHandlerProxy(WebPage& page, uint64_t identifier)
46 : m_webPage(page)
47 , m_identifier(identifier)
48{
49}
50
51WebURLSchemeHandlerProxy::~WebURLSchemeHandlerProxy()
52{
53 ASSERT(m_tasks.isEmpty());
54}
55
56void WebURLSchemeHandlerProxy::startNewTask(ResourceLoader& loader)
57{
58 auto result = m_tasks.add(loader.identifier(), WebURLSchemeTaskProxy::create(*this, loader));
59 ASSERT(result.isNewEntry);
60
61 WebProcess::singleton().webLoaderStrategy().addURLSchemeTaskProxy(*result.iterator->value);
62 result.iterator->value->startLoading();
63}
64
65void WebURLSchemeHandlerProxy::loadSynchronously(ResourceLoadIdentifier loadIdentifier, const ResourceRequest& request, ResourceResponse& response, ResourceError& error, Vector<char>& data)
66{
67 IPC::DataReference dataReference;
68 if (!m_webPage.sendSync(Messages::WebPageProxy::LoadSynchronousURLSchemeTask(URLSchemeTaskParameters { m_identifier, loadIdentifier, request }), Messages::WebPageProxy::LoadSynchronousURLSchemeTask::Reply(response, error, dataReference))) {
69 error = failedCustomProtocolSyncLoad(request);
70 return;
71 }
72
73 data.resize(dataReference.size());
74 memcpy(data.data(), dataReference.data(), dataReference.size());
75}
76
77void WebURLSchemeHandlerProxy::stopAllTasks()
78{
79 while (!m_tasks.isEmpty())
80 m_tasks.begin()->value->stopLoading();
81}
82
83void WebURLSchemeHandlerProxy::taskDidPerformRedirection(uint64_t taskIdentifier, WebCore::ResourceResponse&& redirectResponse, WebCore::ResourceRequest&& newRequest)
84{
85 auto* task = m_tasks.get(taskIdentifier);
86 if (!task)
87 return;
88
89 task->didPerformRedirection(WTFMove(redirectResponse), WTFMove(newRequest));
90}
91
92void WebURLSchemeHandlerProxy::taskDidReceiveResponse(uint64_t taskIdentifier, const ResourceResponse& response)
93{
94 auto* task = m_tasks.get(taskIdentifier);
95 if (!task)
96 return;
97
98 task->didReceiveResponse(response);
99}
100
101void WebURLSchemeHandlerProxy::taskDidReceiveData(uint64_t taskIdentifier, size_t size, const uint8_t* data)
102{
103 auto* task = m_tasks.get(taskIdentifier);
104 if (!task)
105 return;
106
107 task->didReceiveData(size, data);
108}
109
110void WebURLSchemeHandlerProxy::taskDidComplete(uint64_t taskIdentifier, const ResourceError& error)
111{
112 if (auto task = removeTask(taskIdentifier))
113 task->didComplete(error);
114}
115
116void WebURLSchemeHandlerProxy::taskDidStopLoading(WebURLSchemeTaskProxy& task)
117{
118 ASSERT(m_tasks.get(task.identifier()) == &task);
119 removeTask(task.identifier());
120}
121
122RefPtr<WebURLSchemeTaskProxy> WebURLSchemeHandlerProxy::removeTask(uint64_t identifier)
123{
124 auto task = m_tasks.take(identifier);
125 if (!task)
126 return nullptr;
127
128 WebProcess::singleton().webLoaderStrategy().removeURLSchemeTaskProxy(*task);
129
130 return task;
131}
132
133} // namespace WebKit
134