[SOUP] Make WebKitSoupRequestGeneric independent from CustomProtocolManagerImpl
[WebKit-https.git] / Source / WebKit2 / WebProcess / soup / WebProcessSoup.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  * Portions Copyright (c) 2011 Motorola Mobility, Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY MOTOROLA INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MOTOROLA INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "WebProcess.h"
29
30 #if PLATFORM(EFL)
31 #include "SeccompFiltersWebProcessEfl.h"
32 #endif
33
34 #include "CertificateInfo.h"
35 #include "WebCookieManager.h"
36 #include "WebProcessCreationParameters.h"
37 #include <WebCore/FileSystem.h>
38 #include <WebCore/Language.h>
39 #include <WebCore/MemoryCache.h>
40 #include <WebCore/PageCache.h>
41 #include <WebCore/ResourceHandle.h>
42 #include <WebCore/SoupNetworkSession.h>
43 #include <libsoup/soup.h>
44 #include <wtf/RAMSize.h>
45 #include <wtf/glib/GRefPtr.h>
46 #include <wtf/glib/GUniquePtr.h>
47
48 namespace WebKit {
49
50 static uint64_t getCacheDiskFreeSize(SoupCache* cache)
51 {
52     ASSERT(cache);
53
54     GUniqueOutPtr<char> cacheDir;
55     g_object_get(G_OBJECT(cache), "cache-dir", &cacheDir.outPtr(), NULL);
56     if (!cacheDir)
57         return 0;
58
59     return WebCore::getVolumeFreeSizeForPath(cacheDir.get());
60 }
61
62 void WebProcess::platformSetCacheModel(CacheModel cacheModel)
63 {
64     unsigned cacheTotalCapacity = 0;
65     unsigned cacheMinDeadCapacity = 0;
66     unsigned cacheMaxDeadCapacity = 0;
67     auto deadDecodedDataDeletionInterval = std::chrono::seconds { 0 };
68     unsigned pageCacheSize = 0;
69
70     unsigned long urlCacheMemoryCapacity = 0;
71     unsigned long urlCacheDiskCapacity = 0;
72
73     uint64_t diskFreeSize = 0;
74     SoupCache* cache = nullptr;
75
76     if (!usesNetworkProcess()) {
77         cache = WebCore::SoupNetworkSession::defaultSession().cache();
78         diskFreeSize = getCacheDiskFreeSize(cache) / WTF::MB;
79     }
80
81     uint64_t memSize = WTF::ramSize() / WTF::MB;
82     calculateCacheSizes(cacheModel, memSize, diskFreeSize,
83                         cacheTotalCapacity, cacheMinDeadCapacity, cacheMaxDeadCapacity, deadDecodedDataDeletionInterval,
84                         pageCacheSize, urlCacheMemoryCapacity, urlCacheDiskCapacity);
85
86     auto& memoryCache = WebCore::MemoryCache::singleton();
87     memoryCache.setDisabled(cacheModel == CacheModelDocumentViewer);
88     memoryCache.setCapacities(cacheMinDeadCapacity, cacheMaxDeadCapacity, cacheTotalCapacity);
89     memoryCache.setDeadDecodedDataDeletionInterval(deadDecodedDataDeletionInterval);
90     WebCore::PageCache::singleton().setMaxSize(pageCacheSize);
91
92 #if PLATFORM(GTK)
93     WebCore::PageCache::singleton().setShouldClearBackingStores(true);
94 #endif
95
96     if (!usesNetworkProcess()) {
97         if (urlCacheDiskCapacity > soup_cache_get_max_size(cache))
98             soup_cache_set_max_size(cache, urlCacheDiskCapacity);
99     }
100 }
101
102 void WebProcess::platformClearResourceCaches(ResourceCachesToClear cachesToClear)
103 {
104     if (cachesToClear == InMemoryResourceCachesOnly)
105         return;
106
107     // If we're using the network process then it is the only one that needs to clear the disk cache.
108     if (usesNetworkProcess())
109         return;
110
111     soup_cache_clear(WebCore::SoupNetworkSession::defaultSession().cache());
112 }
113
114 static void setSoupSessionAcceptLanguage(const Vector<String>& languages)
115 {
116     WebCore::SoupNetworkSession::defaultSession().setAcceptLanguages(languages);
117 }
118
119 static void languageChanged(void*)
120 {
121     setSoupSessionAcceptLanguage(WebCore::userPreferredLanguages());
122 }
123
124 void WebProcess::platformInitializeWebProcess(WebProcessCreationParameters&& parameters)
125 {
126 #if ENABLE(SECCOMP_FILTERS)
127     {
128 #if PLATFORM(EFL)
129         SeccompFiltersWebProcessEfl seccompFilters(parameters);
130 #endif
131         seccompFilters.initialize();
132     }
133 #endif
134
135     if (usesNetworkProcess())
136         return;
137
138     ASSERT(!parameters.diskCacheDirectory.isEmpty());
139
140     // We used to use the given cache directory for the soup cache, but now we use a subdirectory to avoid
141     // conflicts with other cache files in the same directory. Remove the old cache files if they still exist.
142     WebCore::SoupNetworkSession::defaultSession().clearCache(WebCore::directoryName(parameters.diskCacheDirectory));
143
144 #if ENABLE(NETWORK_CACHE)
145     // When network cache is enabled, the disk cache directory is the network process one.
146     CString diskCachePath = WebCore::pathByAppendingComponent(WebCore::directoryName(parameters.diskCacheDirectory), "webkit").utf8();
147 #else
148     CString diskCachePath = parameters.diskCacheDirectory.utf8();
149 #endif
150
151     GRefPtr<SoupCache> soupCache = adoptGRef(soup_cache_new(diskCachePath.data(), SOUP_CACHE_SINGLE_USER));
152     WebCore::SoupNetworkSession::defaultSession().setCache(soupCache.get());
153     // Set an initial huge max_size for the SoupCache so the call to soup_cache_load() won't evict any cached
154     // resource. The final size of the cache will be set by NetworkProcess::platformSetCacheModel().
155     unsigned initialMaxSize = soup_cache_get_max_size(soupCache.get());
156     soup_cache_set_max_size(soupCache.get(), G_MAXUINT);
157     soup_cache_load(soupCache.get());
158     soup_cache_set_max_size(soupCache.get(), initialMaxSize);
159
160     if (!parameters.cookiePersistentStoragePath.isEmpty()) {
161         supplement<WebCookieManager>()->setCookiePersistentStorage(parameters.cookiePersistentStoragePath,
162             parameters.cookiePersistentStorageType);
163     }
164     supplement<WebCookieManager>()->setHTTPCookieAcceptPolicy(parameters.cookieAcceptPolicy);
165
166     if (!parameters.languages.isEmpty())
167         setSoupSessionAcceptLanguage(parameters.languages);
168
169     setIgnoreTLSErrors(parameters.ignoreTLSErrors);
170
171     WebCore::addLanguageChangeObserver(this, languageChanged);
172 }
173
174 void WebProcess::platformTerminate()
175 {
176     if (!usesNetworkProcess())
177         WebCore::removeLanguageChangeObserver(this);
178 }
179
180 void WebProcess::setIgnoreTLSErrors(bool ignoreTLSErrors)
181 {
182     ASSERT(!usesNetworkProcess());
183     WebCore::ResourceHandle::setIgnoreSSLErrors(ignoreTLSErrors);
184 }
185
186 void WebProcess::allowSpecificHTTPSCertificateForHost(const WebCore::CertificateInfo& certificateInfo, const String& host)
187 {
188     ASSERT(!usesNetworkProcess());
189     WebCore::ResourceHandle::setClientCertificate(host, certificateInfo.certificate());
190 }
191
192 } // namespace WebKit