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