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#ifndef CustomProtocolManager_h
27#define CustomProtocolManager_h
28
29#if ENABLE(CUSTOM_PROTOCOLS)
30
31#include "Connection.h"
32#include "NetworkProcessSupplement.h"
33#include "WebProcessSupplement.h"
34#include "WorkQueue.h"
35#include <wtf/text/WTFString.h>
36
37#if PLATFORM(COCOA)
38#include <wtf/HashMap.h>
39#include <wtf/HashSet.h>
40#include <wtf/RetainPtr.h>
41#include <wtf/Threading.h>
42OBJC_CLASS WKCustomProtocol;
43#else
44#include "CustomProtocolManagerImpl.h"
45#endif
46
47
48namespace IPC {
49class DataReference;
50} // namespace IPC
51
52namespace WebCore {
53class ResourceError;
54class ResourceResponse;
55} // namespace WebCore
56
57namespace WebKit {
58
59class ChildProcess;
60struct NetworkProcessCreationParameters;
61
62class CustomProtocolManager : public WebProcessSupplement, public NetworkProcessSupplement, public IPC::Connection::WorkQueueMessageReceiver {
63    WTF_MAKE_NONCOPYABLE(CustomProtocolManager);
64public:
65    explicit CustomProtocolManager(ChildProcess*);
66
67    static const char* supplementName();
68
69    ChildProcess* childProcess() const { return m_childProcess; }
70
71    void registerScheme(const String&);
72    void unregisterScheme(const String&);
73    bool supportsScheme(const String&);
74
75#if PLATFORM(COCOA)
76    void addCustomProtocol(WKCustomProtocol *);
77    void removeCustomProtocol(WKCustomProtocol *);
78#endif
79
80private:
81    // ChildProcessSupplement
82    void initializeConnection(IPC::Connection*) override;
83
84    // WebProcessSupplement
85    void initialize(const WebProcessCreationParameters&) override;
86
87#if ENABLE(NETWORK_PROCESS)
88    // NetworkProcessSupplement
89    void initialize(const NetworkProcessCreationParameters&) override;
90#endif
91
92    // IPC::MessageReceiver
93    virtual void didReceiveMessage(IPC::Connection*, IPC::MessageDecoder&) override;
94
95    void didFailWithError(uint64_t customProtocolID, const WebCore::ResourceError&);
96    void didLoadData(uint64_t customProtocolID, const IPC::DataReference&);
97    void didReceiveResponse(uint64_t customProtocolID, const WebCore::ResourceResponse&, uint32_t cacheStoragePolicy);
98    void didFinishLoading(uint64_t customProtocolID);
99
100    ChildProcess* m_childProcess;
101    RefPtr<WorkQueue> m_messageQueue;
102
103#if PLATFORM(COCOA)
104    HashSet<String> m_registeredSchemes;
105    Mutex m_registeredSchemesMutex;
106
107    typedef HashMap<uint64_t, RetainPtr<WKCustomProtocol>> CustomProtocolMap;
108    CustomProtocolMap m_customProtocolMap;
109    Mutex m_customProtocolMapMutex;
110
111    // WKCustomProtocol objects can be removed from the m_customProtocolMap from multiple threads.
112    // 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.
113    RetainPtr<WKCustomProtocol> protocolForID(uint64_t customProtocolID);
114#else
115    // FIXME: Move mac specific code to CustomProtocolManagerImpl.
116    std::unique_ptr<CustomProtocolManagerImpl> m_impl;
117#endif
118};
119
120} // namespace WebKit
121
122#endif // ENABLE(CUSTOM_PROTOCOLS)
123
124#endif // CustomProtocolManager_h
125