[GTK] Add seccomp filters support
[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 #elif PLATFORM(GTK)
33 #include "SeccompFiltersWebProcessGtk.h"
34 #endif
35
36 #include "CertificateInfo.h"
37 #include "WebCookieManager.h"
38 #include "WebProcessCreationParameters.h"
39 #include <WebCore/FileSystem.h>
40 #include <WebCore/Language.h>
41 #include <WebCore/MemoryCache.h>
42 #include <WebCore/PageCache.h>
43 #include <WebCore/ResourceHandle.h>
44 #include <WebCore/SoupNetworkSession.h>
45 #include <libsoup/soup.h>
46 #include <wtf/RAMSize.h>
47 #include <wtf/glib/GRefPtr.h>
48 #include <wtf/glib/GUniquePtr.h>
49
50 namespace WebKit {
51
52 static uint64_t getCacheDiskFreeSize(SoupCache* cache)
53 {
54     ASSERT(cache);
55
56     GUniqueOutPtr<char> cacheDir;
57     g_object_get(G_OBJECT(cache), "cache-dir", &cacheDir.outPtr(), NULL);
58     if (!cacheDir)
59         return 0;
60
61     return WebCore::getVolumeFreeSizeForPath(cacheDir.get());
62 }
63
64 void WebProcess::platformSetCacheModel(CacheModel cacheModel)
65 {
66     unsigned cacheTotalCapacity = 0;
67     unsigned cacheMinDeadCapacity = 0;
68     unsigned cacheMaxDeadCapacity = 0;
69     auto deadDecodedDataDeletionInterval = std::chrono::seconds { 0 };
70     unsigned pageCacheSize = 0;
71
72     unsigned long urlCacheMemoryCapacity = 0;
73     unsigned long urlCacheDiskCapacity = 0;
74
75     uint64_t diskFreeSize = 0;
76     SoupCache* cache = nullptr;
77
78     if (!usesNetworkProcess()) {
79         cache = WebCore::SoupNetworkSession::defaultSession().cache();
80         diskFreeSize = getCacheDiskFreeSize(cache) / WTF::MB;
81     }
82
83     uint64_t memSize = WTF::ramSize() / WTF::MB;
84     calculateCacheSizes(cacheModel, memSize, diskFreeSize,
85                         cacheTotalCapacity, cacheMinDeadCapacity, cacheMaxDeadCapacity, deadDecodedDataDeletionInterval,
86                         pageCacheSize, urlCacheMemoryCapacity, urlCacheDiskCapacity);
87
88     auto& memoryCache = WebCore::MemoryCache::singleton();
89     memoryCache.setDisabled(cacheModel == CacheModelDocumentViewer);
90     memoryCache.setCapacities(cacheMinDeadCapacity, cacheMaxDeadCapacity, cacheTotalCapacity);
91     memoryCache.setDeadDecodedDataDeletionInterval(deadDecodedDataDeletionInterval);
92     WebCore::PageCache::singleton().setMaxSize(pageCacheSize);
93
94 #if PLATFORM(GTK)
95     WebCore::PageCache::singleton().setShouldClearBackingStores(true);
96 #endif
97
98     if (!usesNetworkProcess()) {
99         if (urlCacheDiskCapacity > soup_cache_get_max_size(cache))
100             soup_cache_set_max_size(cache, urlCacheDiskCapacity);
101     }
102 }
103
104 void WebProcess::platformClearResourceCaches(ResourceCachesToClear cachesToClear)
105 {
106     if (cachesToClear == InMemoryResourceCachesOnly)
107         return;
108
109     // If we're using the network process then it is the only one that needs to clear the disk cache.
110     if (usesNetworkProcess())
111         return;
112
113     soup_cache_clear(WebCore::SoupNetworkSession::defaultSession().cache());
114 }
115
116 static void setSoupSessionAcceptLanguage(const Vector<String>& languages)
117 {
118     WebCore::SoupNetworkSession::defaultSession().setAcceptLanguages(languages);
119 }
120
121 static void languageChanged(void*)
122 {
123     setSoupSessionAcceptLanguage(WebCore::userPreferredLanguages());
124 }
125
126 void WebProcess::platformInitializeWebProcess(WebProcessCreationParameters&& parameters)
127 {
128 #if ENABLE(SECCOMP_FILTERS)
129     {
130 #if PLATFORM(EFL)
131         SeccompFiltersWebProcessEfl seccompFilters(parameters);
132 #elif PLATFORM(GTK)
133         SeccompFiltersWebProcessGtk seccompFilters(parameters);
134 #endif
135         seccompFilters.initialize();
136     }
137 #endif
138
139     if (usesNetworkProcess())
140         return;
141
142     ASSERT(!parameters.diskCacheDirectory.isEmpty());
143
144     // We used to use the given cache directory for the soup cache, but now we use a subdirectory to avoid
145     // conflicts with other cache files in the same directory. Remove the old cache files if they still exist.
146     WebCore::SoupNetworkSession::defaultSession().clearCache(WebCore::directoryName(parameters.diskCacheDirectory));
147
148 #if ENABLE(NETWORK_CACHE)
149     // When network cache is enabled, the disk cache directory is the network process one.
150     CString diskCachePath = WebCore::pathByAppendingComponent(WebCore::directoryName(parameters.diskCacheDirectory), "webkit").utf8();
151 #else
152     CString diskCachePath = parameters.diskCacheDirectory.utf8();
153 #endif
154
155     GRefPtr<SoupCache> soupCache = adoptGRef(soup_cache_new(diskCachePath.data(), SOUP_CACHE_SINGLE_USER));
156     WebCore::SoupNetworkSession::defaultSession().setCache(soupCache.get());
157     // Set an initial huge max_size for the SoupCache so the call to soup_cache_load() won't evict any cached
158     // resource. The final size of the cache will be set by NetworkProcess::platformSetCacheModel().
159     unsigned initialMaxSize = soup_cache_get_max_size(soupCache.get());
160     soup_cache_set_max_size(soupCache.get(), G_MAXUINT);
161     soup_cache_load(soupCache.get());
162     soup_cache_set_max_size(soupCache.get(), initialMaxSize);
163
164     if (!parameters.cookiePersistentStoragePath.isEmpty()) {
165         supplement<WebCookieManager>()->setCookiePersistentStorage(parameters.cookiePersistentStoragePath,
166             parameters.cookiePersistentStorageType);
167     }
168     supplement<WebCookieManager>()->setHTTPCookieAcceptPolicy(parameters.cookieAcceptPolicy);
169
170     if (!parameters.languages.isEmpty())
171         setSoupSessionAcceptLanguage(parameters.languages);
172
173     setIgnoreTLSErrors(parameters.ignoreTLSErrors);
174
175     WebCore::addLanguageChangeObserver(this, languageChanged);
176 }
177
178 void WebProcess::platformTerminate()
179 {
180     if (!usesNetworkProcess())
181         WebCore::removeLanguageChangeObserver(this);
182 }
183
184 void WebProcess::setIgnoreTLSErrors(bool ignoreTLSErrors)
185 {
186     ASSERT(!usesNetworkProcess());
187     WebCore::ResourceHandle::setIgnoreSSLErrors(ignoreTLSErrors);
188 }
189
190 void WebProcess::allowSpecificHTTPSCertificateForHost(const WebCore::CertificateInfo& certificateInfo, const String& host)
191 {
192     ASSERT(!usesNetworkProcess());
193     WebCore::ResourceHandle::setClientCertificate(host, certificateInfo.certificate());
194 }
195
196 } // namespace WebKit