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