Make memoryCache() return a reference
[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/gobject/GRefPtr.h>
45 #include <wtf/gobject/GUniquePtr.h>
46
47 namespace WebKit {
48
49 static uint64_t getCacheDiskFreeSize(SoupCache* cache)
50 {
51     ASSERT(cache);
52
53     GUniqueOutPtr<char> cacheDir;
54     g_object_get(G_OBJECT(cache), "cache-dir", &cacheDir.outPtr(), NULL);
55     if (!cacheDir)
56         return 0;
57
58     return WebCore::getVolumeFreeSizeForPath(cacheDir.get());
59 }
60
61 static uint64_t getMemorySize()
62 {
63     static uint64_t kDefaultMemorySize = 512;
64 #if !OS(WINDOWS)
65     long pageSize = sysconf(_SC_PAGESIZE);
66     if (pageSize == -1)
67         return kDefaultMemorySize;
68
69     long physPages = sysconf(_SC_PHYS_PAGES);
70     if (physPages == -1)
71         return kDefaultMemorySize;
72
73     return ((pageSize / 1024LL) * physPages) / 1024LL;
74 #else
75     // Fallback to default for other platforms.
76     return kDefaultMemorySize;
77 #endif
78 }
79
80 void WebProcess::platformSetCacheModel(CacheModel cacheModel)
81 {
82     unsigned cacheTotalCapacity = 0;
83     unsigned cacheMinDeadCapacity = 0;
84     unsigned cacheMaxDeadCapacity = 0;
85     auto deadDecodedDataDeletionInterval = std::chrono::seconds { 0 };
86     unsigned pageCacheCapacity = 0;
87
88     unsigned long urlCacheMemoryCapacity = 0;
89     unsigned long urlCacheDiskCapacity = 0;
90
91     uint64_t diskFreeSize = 0;
92     SoupCache* cache = nullptr;
93
94     if (!usesNetworkProcess()) {
95         cache = WebCore::SoupNetworkSession::defaultSession().cache();
96         diskFreeSize = getCacheDiskFreeSize(cache) / 1024 / 1024;
97     }
98
99     uint64_t memSize = getMemorySize();
100     calculateCacheSizes(cacheModel, memSize, diskFreeSize,
101                         cacheTotalCapacity, cacheMinDeadCapacity, cacheMaxDeadCapacity, deadDecodedDataDeletionInterval,
102                         pageCacheCapacity, urlCacheMemoryCapacity, urlCacheDiskCapacity);
103
104     WebCore::memoryCache().setDisabled(cacheModel == CacheModelDocumentViewer);
105     WebCore::memoryCache().setCapacities(cacheMinDeadCapacity, cacheMaxDeadCapacity, cacheTotalCapacity);
106     WebCore::memoryCache().setDeadDecodedDataDeletionInterval(deadDecodedDataDeletionInterval);
107     WebCore::pageCache()->setCapacity(pageCacheCapacity);
108
109 #if PLATFORM(GTK)
110     WebCore::pageCache()->setShouldClearBackingStores(true);
111 #endif
112
113     if (!usesNetworkProcess()) {
114         if (urlCacheDiskCapacity > soup_cache_get_max_size(cache))
115             soup_cache_set_max_size(cache, urlCacheDiskCapacity);
116     }
117 }
118
119 void WebProcess::platformClearResourceCaches(ResourceCachesToClear cachesToClear)
120 {
121     if (cachesToClear == InMemoryResourceCachesOnly)
122         return;
123
124     // If we're using the network process then it is the only one that needs to clear the disk cache.
125     if (usesNetworkProcess())
126         return;
127
128     soup_cache_clear(WebCore::SoupNetworkSession::defaultSession().cache());
129 }
130
131 static void setSoupSessionAcceptLanguage(const Vector<String>& languages)
132 {
133     WebCore::SoupNetworkSession::defaultSession().setAcceptLanguages(languages);
134 }
135
136 static void languageChanged(void*)
137 {
138     setSoupSessionAcceptLanguage(WebCore::userPreferredLanguages());
139 }
140
141 void WebProcess::platformInitializeWebProcess(const WebProcessCreationParameters& parameters, IPC::MessageDecoder&)
142 {
143 #if ENABLE(SECCOMP_FILTERS)
144     {
145 #if PLATFORM(EFL)
146         SeccompFiltersWebProcessEfl seccompFilters(parameters);
147 #endif
148         seccompFilters.initialize();
149     }
150 #endif
151
152     if (usesNetworkProcess())
153         return;
154
155     ASSERT(!parameters.diskCacheDirectory.isEmpty());
156     GRefPtr<SoupCache> soupCache = adoptGRef(soup_cache_new(parameters.diskCacheDirectory.utf8().data(), SOUP_CACHE_SINGLE_USER));
157     WebCore::SoupNetworkSession::defaultSession().setCache(soupCache.get());
158     // Set an initial huge max_size for the SoupCache so the call to soup_cache_load() won't evict any cached
159     // resource. The final size of the cache will be set by NetworkProcess::platformSetCacheModel().
160     unsigned initialMaxSize = soup_cache_get_max_size(soupCache.get());
161     soup_cache_set_max_size(soupCache.get(), G_MAXUINT);
162     soup_cache_load(soupCache.get());
163     soup_cache_set_max_size(soupCache.get(), initialMaxSize);
164
165     if (!parameters.cookiePersistentStoragePath.isEmpty()) {
166         supplement<WebCookieManager>()->setCookiePersistentStorage(parameters.cookiePersistentStoragePath,
167             parameters.cookiePersistentStorageType);
168     }
169     supplement<WebCookieManager>()->setHTTPCookieAcceptPolicy(parameters.cookieAcceptPolicy);
170
171     if (!parameters.languages.isEmpty())
172         setSoupSessionAcceptLanguage(parameters.languages);
173
174     setIgnoreTLSErrors(parameters.ignoreTLSErrors);
175
176     WebCore::addLanguageChangeObserver(this, languageChanged);
177 }
178
179 void WebProcess::platformTerminate()
180 {
181     if (!usesNetworkProcess())
182         WebCore::removeLanguageChangeObserver(this);
183 }
184
185 void WebProcess::setIgnoreTLSErrors(bool ignoreTLSErrors)
186 {
187     ASSERT(!usesNetworkProcess());
188     WebCore::ResourceHandle::setIgnoreSSLErrors(ignoreTLSErrors);
189 }
190
191 void WebProcess::allowSpecificHTTPSCertificateForHost(const WebCore::CertificateInfo& certificateInfo, const String& host)
192 {
193     ASSERT(!usesNetworkProcess());
194     WebCore::ResourceHandle::setClientCertificate(host, certificateInfo.certificate());
195 }
196
197 } // namespace WebKit