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 "CustomProtocolManager.h"
22
23#if ENABLE(CUSTOM_PROTOCOLS)
24
25#include "ChildProcess.h"
26#include "CustomProtocolManagerImpl.h"
27#include "CustomProtocolManagerMessages.h"
28#include "WebProcessCreationParameters.h"
29#include <WebCore/NotImplemented.h>
30
31#if ENABLE(NETWORK_PROCESS)
32#include "NetworkProcessCreationParameters.h"
33#endif
34
35namespace WebKit {
36
37const char* CustomProtocolManager::supplementName()
38{
39    return "CustomProtocolManager";
40}
41
42CustomProtocolManager::CustomProtocolManager(ChildProcess* childProcess)
43    : m_childProcess(childProcess)
44    , m_messageQueue(WorkQueue::create("com.apple.WebKit.CustomProtocolManager"))
45    , m_impl(std::make_unique<CustomProtocolManagerImpl>(childProcess))
46{
47}
48
49void CustomProtocolManager::initializeConnection(IPC::Connection* connection)
50{
51    connection->addWorkQueueMessageReceiver(Messages::CustomProtocolManager::messageReceiverName(), m_messageQueue.get(), this);
52}
53
54void CustomProtocolManager::initialize(const WebProcessCreationParameters& parameters)
55{
56#if ENABLE(NETWORK_PROCESS)
57    ASSERT(parameters.urlSchemesRegisteredForCustomProtocols.isEmpty() || !parameters.usesNetworkProcess);
58    if (parameters.usesNetworkProcess) {
59        m_childProcess->parentProcessConnection()->removeWorkQueueMessageReceiver(Messages::CustomProtocolManager::messageReceiverName());
60        m_messageQueue = nullptr;
61        return;
62    }
63#endif
64    for (size_t i = 0; i < parameters.urlSchemesRegisteredForCustomProtocols.size(); ++i)
65        registerScheme(parameters.urlSchemesRegisteredForCustomProtocols[i]);
66}
67
68#if ENABLE(NETWORK_PROCESS)
69void CustomProtocolManager::initialize(const NetworkProcessCreationParameters& parameters)
70{
71    for (size_t i = 0; i < parameters.urlSchemesRegisteredForCustomProtocols.size(); ++i)
72        registerScheme(parameters.urlSchemesRegisteredForCustomProtocols[i]);
73}
74#endif
75
76void CustomProtocolManager::registerScheme(const String& scheme)
77{
78    m_impl->registerScheme(scheme);
79}
80
81void CustomProtocolManager::unregisterScheme(const String&)
82{
83    notImplemented();
84}
85
86bool CustomProtocolManager::supportsScheme(const String& scheme)
87{
88    return m_impl->supportsScheme(scheme);
89}
90
91void CustomProtocolManager::didFailWithError(uint64_t customProtocolID, const WebCore::ResourceError& error)
92{
93    m_impl->didFailWithError(customProtocolID, error);
94}
95
96void CustomProtocolManager::didLoadData(uint64_t customProtocolID, const IPC::DataReference& dataReference)
97{
98    m_impl->didLoadData(customProtocolID, dataReference);
99}
100
101void CustomProtocolManager::didReceiveResponse(uint64_t customProtocolID, const WebCore::ResourceResponse& response, uint32_t)
102{
103    m_impl->didReceiveResponse(customProtocolID, response);
104}
105
106void CustomProtocolManager::didFinishLoading(uint64_t customProtocolID)
107{
108    m_impl->didFinishLoading(customProtocolID);
109}
110
111} // namespace WebKit
112
113#endif // ENABLE(CUSTOM_PROTOCOLS)
114