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 NetworkProcess_h
27#define NetworkProcess_h
28
29#if ENABLE(NETWORK_PROCESS)
30
31#include "CacheModel.h"
32#include "ChildProcess.h"
33#include "DownloadManager.h"
34#include "MessageReceiverMap.h"
35#include "NetworkResourceLoadScheduler.h"
36#include <WebCore/SessionID.h>
37#include <memory>
38#include <wtf/Forward.h>
39#include <wtf/NeverDestroyed.h>
40
41namespace WebCore {
42class CertificateInfo;
43}
44
45namespace WebKit {
46class AuthenticationManager;
47class NetworkConnectionToWebProcess;
48class NetworkProcessSupplement;
49struct NetworkProcessCreationParameters;
50
51class NetworkProcess : public ChildProcess, private DownloadManager::Client {
52    WTF_MAKE_NONCOPYABLE(NetworkProcess);
53    friend class NeverDestroyed<NetworkProcess>;
54    friend class NeverDestroyed<DownloadManager>;
55public:
56    static NetworkProcess& shared();
57
58    template <typename T>
59    T* supplement()
60    {
61        return static_cast<T*>(m_supplements.get(T::supplementName()));
62    }
63
64    template <typename T>
65    void addSupplement()
66    {
67        m_supplements.add(T::supplementName(), std::make_unique<T>(this));
68    }
69
70    void removeNetworkConnectionToWebProcess(NetworkConnectionToWebProcess*);
71
72    NetworkResourceLoadScheduler& networkResourceLoadScheduler() { return m_networkResourceLoadScheduler; }
73
74    AuthenticationManager& authenticationManager();
75    DownloadManager& downloadManager();
76
77private:
78    NetworkProcess();
79    ~NetworkProcess();
80
81    void platformInitializeNetworkProcess(const NetworkProcessCreationParameters&);
82
83    virtual void terminate() override;
84    void platformTerminate();
85
86    static void lowMemoryHandler(bool critical);
87    static void platformLowMemoryHandler(bool critical);
88
89    // ChildProcess
90    virtual void initializeProcess(const ChildProcessInitializationParameters&) override;
91    virtual void initializeProcessName(const ChildProcessInitializationParameters&) override;
92    virtual void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
93    virtual void initializeConnection(IPC::Connection*) override;
94    virtual bool shouldTerminate() override;
95
96    // IPC::Connection::Client
97    virtual void didReceiveMessage(IPC::Connection*, IPC::MessageDecoder&) override;
98    virtual void didReceiveSyncMessage(IPC::Connection*, IPC::MessageDecoder&, std::unique_ptr<IPC::MessageEncoder>&);
99    virtual void didClose(IPC::Connection*) override;
100    virtual void didReceiveInvalidMessage(IPC::Connection*, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
101
102    // DownloadManager::Client
103    virtual void didCreateDownload() override;
104    virtual void didDestroyDownload() override;
105    virtual IPC::Connection* downloadProxyConnection() override;
106    virtual AuthenticationManager& downloadsAuthenticationManager() override;
107
108    // Message Handlers
109    void didReceiveNetworkProcessMessage(IPC::Connection*, IPC::MessageDecoder&);
110    void initializeNetworkProcess(const NetworkProcessCreationParameters&);
111    void createNetworkConnectionToWebProcess();
112    void ensurePrivateBrowsingSession(WebCore::SessionID);
113    void destroyPrivateBrowsingSession(WebCore::SessionID);
114    void downloadRequest(uint64_t downloadID, const WebCore::ResourceRequest&);
115    void cancelDownload(uint64_t downloadID);
116    void setCacheModel(uint32_t);
117    void allowSpecificHTTPSCertificateForHost(const WebCore::CertificateInfo&, const String& host);
118    void getNetworkProcessStatistics(uint64_t callbackID);
119    void clearCacheForAllOrigins(uint32_t cachesToClear);
120
121#if USE(SOUP)
122    void setIgnoreTLSErrors(bool);
123    void userPreferredLanguagesChanged(const Vector<String>&);
124#endif
125
126    // Platform Helpers
127    void platformSetCacheModel(CacheModel);
128
129    // Connections to WebProcesses.
130    Vector<RefPtr<NetworkConnectionToWebProcess>> m_webProcessConnections;
131
132    NetworkResourceLoadScheduler m_networkResourceLoadScheduler;
133
134    String m_diskCacheDirectory;
135    bool m_hasSetCacheModel;
136    CacheModel m_cacheModel;
137
138    typedef HashMap<const char*, std::unique_ptr<NetworkProcessSupplement>, PtrHash<const char*>> NetworkProcessSupplementMap;
139    NetworkProcessSupplementMap m_supplements;
140
141#if PLATFORM(COCOA)
142    void platformInitializeNetworkProcessCocoa(const NetworkProcessCreationParameters&);
143
144    // FIXME: We'd like to be able to do this without the #ifdef, but WorkQueue + BinarySemaphore isn't good enough since
145    // multiple requests to clear the cache can come in before previous requests complete, and we need to wait for all of them.
146    // In the future using WorkQueue and a counting semaphore would work, as would WorkQueue supporting the libdispatch concept of "work groups".
147    dispatch_group_t m_clearCacheDispatchGroup;
148#endif
149};
150
151} // namespace WebKit
152
153#endif // ENABLE(NETWORK_PROCESS)
154
155#endif // NetworkProcess_h
156