0701ebabfa2263eecd7efb1fd795b41cd4e58105
[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 "WebCookieManager.h"
35 #include "WebProcessCreationParameters.h"
36 #include <WebCore/FileSystem.h>
37 #include <WebCore/Language.h>
38 #include <WebCore/MemoryCache.h>
39 #include <WebCore/PageCache.h>
40 #include <WebCore/ResourceHandle.h>
41 #include <libsoup/soup.h>
42 #include <wtf/gobject/GOwnPtr.h>
43 #include <wtf/gobject/GRefPtr.h>
44 #include <wtf/text/CString.h>
45 #include <wtf/text/StringBuilder.h>
46
47 namespace WebKit {
48
49 static uint64_t getCacheDiskFreeSize(SoupCache* cache)
50 {
51     ASSERT(cache);
52
53     GOwnPtr<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 / 1024) * physPages) / 1024;
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     double deadDecodedDataDeletionInterval = 0;
86     unsigned pageCacheCapacity = 0;
87
88     unsigned long urlCacheMemoryCapacity = 0;
89     unsigned long urlCacheDiskCapacity = 0;
90
91     SoupSession* session = WebCore::ResourceHandle::defaultSession();
92     SoupCache* cache = SOUP_CACHE(soup_session_get_feature(session, SOUP_TYPE_CACHE));
93     uint64_t diskFreeSize = getCacheDiskFreeSize(cache) / 1024 / 1024;
94
95     uint64_t memSize = getMemorySize();
96     calculateCacheSizes(cacheModel, memSize, diskFreeSize,
97                         cacheTotalCapacity, cacheMinDeadCapacity, cacheMaxDeadCapacity, deadDecodedDataDeletionInterval,
98                         pageCacheCapacity, urlCacheMemoryCapacity, urlCacheDiskCapacity);
99
100     WebCore::memoryCache()->setCapacities(cacheMinDeadCapacity, cacheMaxDeadCapacity, cacheTotalCapacity);
101     WebCore::memoryCache()->setDeadDecodedDataDeletionInterval(deadDecodedDataDeletionInterval);
102     WebCore::pageCache()->setCapacity(pageCacheCapacity);
103
104     if (urlCacheDiskCapacity > soup_cache_get_max_size(cache))
105         soup_cache_set_max_size(cache, urlCacheDiskCapacity);
106 }
107
108 void WebProcess::platformClearResourceCaches(ResourceCachesToClear cachesToClear)
109 {
110     if (cachesToClear == InMemoryResourceCachesOnly)
111         return;
112
113     SoupSession* session = WebCore::ResourceHandle::defaultSession();
114     soup_cache_clear(SOUP_CACHE(soup_session_get_feature(session, SOUP_TYPE_CACHE)));
115 }
116
117 // This function is based on Epiphany code in ephy-embed-prefs.c.
118 static CString buildAcceptLanguages(Vector<String> languages)
119 {
120     // Ignore "C" locale.
121     size_t position = languages.find("c");
122     if (position != notFound)
123         languages.remove(position);
124
125     // Fallback to "en" if the list is empty.
126     if (languages.isEmpty())
127         return "en";
128
129     // Calculate deltas for the quality values.
130     int delta;
131     if (languages.size() < 10)
132         delta = 10;
133     else if (languages.size() < 20)
134         delta = 5;
135     else
136         delta = 1;
137
138     // Set quality values for each language.
139     StringBuilder builder;
140     for (size_t i = 0; i < languages.size(); ++i) {
141         if (i)
142             builder.append(", ");
143
144         builder.append(languages[i]);
145
146         int quality = 100 - i * delta;
147         if (quality > 0 && quality < 100) {
148             char buffer[8];
149             g_ascii_formatd(buffer, 8, "%.2f", quality / 100.0);
150             builder.append(String::format(";q=%s", buffer));
151         }
152     }
153
154     return builder.toString().utf8();
155 }
156
157 static void setSoupSessionAcceptLanguage(Vector<String> languages)
158 {
159     g_object_set(WebCore::ResourceHandle::defaultSession(), "accept-language", buildAcceptLanguages(languages).data(), NULL);
160 }
161
162 static void languageChanged(void*)
163 {
164     setSoupSessionAcceptLanguage(WebCore::userPreferredLanguages());
165 }
166
167 void WebProcess::platformInitializeWebProcess(const WebProcessCreationParameters& parameters, CoreIPC::MessageDecoder&)
168 {
169 #if ENABLE(SECCOMP_FILTERS)
170     {
171 #if PLATFORM(EFL)
172         SeccompFiltersWebProcessEfl seccompFilters(parameters);
173 #endif
174         seccompFilters.initialize();
175     }
176 #endif
177
178     ASSERT(!parameters.diskCacheDirectory.isEmpty());
179     GRefPtr<SoupCache> soupCache = adoptGRef(soup_cache_new(parameters.diskCacheDirectory.utf8().data(), SOUP_CACHE_SINGLE_USER));
180     soup_session_add_feature(WebCore::ResourceHandle::defaultSession(), SOUP_SESSION_FEATURE(soupCache.get()));
181     soup_cache_load(soupCache.get());
182
183     if (!parameters.languages.isEmpty())
184         setSoupSessionAcceptLanguage(parameters.languages);
185
186     for (size_t i = 0; i < parameters.urlSchemesRegistered.size(); i++)
187         m_soupRequestManager.registerURIScheme(parameters.urlSchemesRegistered[i]);
188
189     if (!parameters.cookiePersistentStoragePath.isEmpty()) {
190         supplement<WebCookieManager>()->setCookiePersistentStorage(parameters.cookiePersistentStoragePath,
191             parameters.cookiePersistentStorageType);
192     }
193     supplement<WebCookieManager>()->setHTTPCookieAcceptPolicy(parameters.cookieAcceptPolicy);
194
195     setIgnoreTLSErrors(parameters.ignoreTLSErrors);
196
197     WebCore::addLanguageChangeObserver(this, languageChanged);
198 }
199
200 void WebProcess::platformTerminate()
201 {
202     WebCore::removeLanguageChangeObserver(this);
203 }
204
205 void WebProcess::setIgnoreTLSErrors(bool ignoreTLSErrors)
206 {
207     WebCore::ResourceHandle::setIgnoreSSLErrors(ignoreTLSErrors);
208 }
209
210 } // namespace WebKit