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