1/*
2 * Copyright (C) 2012, 2013 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#pragma once
27
28#include "MessageReceiver.h"
29#include "NetworkProcessSupplement.h"
30#include <wtf/HashMap.h>
31#include <wtf/HashSet.h>
32#include <wtf/text/StringHash.h>
33#include <wtf/text/WTFString.h>
34
35#if PLATFORM(COCOA)
36#include <wtf/RetainPtr.h>
37OBJC_CLASS NSURLSessionConfiguration;
38OBJC_CLASS WKCustomProtocol;
39#endif
40
41#if USE(SOUP)
42#include <wtf/glib/GRefPtr.h>
43
44typedef struct _GCancellable GCancellable;
45typedef struct _GInputStream GInputStream;
46typedef struct _GTask GTask;
47typedef struct _WebKitSoupRequestGeneric WebKitSoupRequestGeneric;
48#endif
49
50namespace IPC {
51class DataReference;
52} // namespace IPC
53
54namespace WebCore {
55class ResourceError;
56class ResourceRequest;
57class ResourceResponse;
58} // namespace WebCore
59
60namespace WebKit {
61
62class NetworkProcess;
63struct NetworkProcessCreationParameters;
64
65class LegacyCustomProtocolManager : public NetworkProcessSupplement, public IPC::MessageReceiver {
66 WTF_MAKE_NONCOPYABLE(LegacyCustomProtocolManager);
67public:
68 explicit LegacyCustomProtocolManager(NetworkProcess&);
69
70 static const char* supplementName();
71
72 void registerScheme(const String&);
73 void unregisterScheme(const String&);
74 bool supportsScheme(const String&);
75
76#if PLATFORM(COCOA)
77 typedef RetainPtr<WKCustomProtocol> CustomProtocol;
78#endif
79#if USE(SOUP)
80 struct WebSoupRequestAsyncData {
81 WebSoupRequestAsyncData(GRefPtr<GTask>&&, WebKitSoupRequestGeneric*);
82 ~WebSoupRequestAsyncData();
83
84 GRefPtr<GTask> task;
85 WebKitSoupRequestGeneric* request;
86 GRefPtr<GCancellable> cancellable;
87 GRefPtr<GInputStream> stream;
88 };
89 typedef std::unique_ptr<WebSoupRequestAsyncData> CustomProtocol;
90#endif
91
92 uint64_t addCustomProtocol(CustomProtocol&&);
93 void removeCustomProtocol(uint64_t customProtocolID);
94 void startLoading(uint64_t customProtocolID, const WebCore::ResourceRequest&);
95 void stopLoading(uint64_t customProtocolID);
96
97#if PLATFORM(COCOA)
98 void registerProtocolClass(NSURLSessionConfiguration*);
99#endif
100#if PLATFORM(COCOA) || USE(SOUP)
101 static void networkProcessCreated(NetworkProcess&);
102#endif
103
104private:
105 // NetworkProcessSupplement
106 void initialize(const NetworkProcessCreationParameters&) override;
107
108 // IPC::MessageReceiver
109 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
110
111 void didFailWithError(uint64_t customProtocolID, const WebCore::ResourceError&);
112 void didLoadData(uint64_t customProtocolID, const IPC::DataReference&);
113 void didReceiveResponse(uint64_t customProtocolID, const WebCore::ResourceResponse&, uint32_t cacheStoragePolicy);
114 void didFinishLoading(uint64_t customProtocolID);
115 void wasRedirectedToRequest(uint64_t customProtocolID, const WebCore::ResourceRequest&, const WebCore::ResourceResponse& redirectResponse);
116
117 void registerProtocolClass();
118
119 NetworkProcess& m_networkProcess;
120
121 typedef HashMap<uint64_t, CustomProtocol> CustomProtocolMap;
122 CustomProtocolMap m_customProtocolMap;
123 Lock m_customProtocolMapMutex;
124
125#if PLATFORM(COCOA)
126 HashSet<String, ASCIICaseInsensitiveHash> m_registeredSchemes;
127 Lock m_registeredSchemesMutex;
128
129 // WKCustomProtocol objects can be removed from the m_customProtocolMap from multiple threads.
130 // We return a RetainPtr here because it is unsafe to return a raw pointer since the object might immediately be destroyed from a different thread.
131 RetainPtr<WKCustomProtocol> protocolForID(uint64_t customProtocolID);
132#endif
133
134#if USE(SOUP)
135 GRefPtr<GPtrArray> m_registeredSchemes;
136#endif
137};
138
139} // namespace WebKit
140
141